blob: b11ddd255b9b039d89f20d48f2704c9f0bc75140 [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 Moolenaar94116152012-11-28 17:41:59 +0100110static void qf_new_list __ARGS((qf_info_T *qi, char_u *qf_title));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000111static void ll_free_all __ARGS((qf_info_T **pqi));
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000112static 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 +0000113static qf_info_T *ll_new_list __ARGS((void));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000114static void qf_msg __ARGS((qf_info_T *qi));
115static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static char_u *qf_types __ARGS((int, int));
117static int qf_get_fnum __ARGS((char_u *, char_u *));
118static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
119static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
120static char_u *qf_guess_filepath __ARGS((char_u *));
121static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
122static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
123#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000124static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
Bram Moolenaar9c102382006-05-03 21:26:49 +0000125static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000126static win_T *qf_find_win __ARGS((qf_info_T *qi));
127static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
128static void qf_update_buffer __ARGS((qf_info_T *qi));
Bram Moolenaarc95e3262011-08-10 18:36:54 +0200129static void qf_set_title __ARGS((qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132static char_u *get_mef_name __ARGS((void));
Bram Moolenaar7f51a822012-04-25 18:57:21 +0200133static void restore_start_dir __ARGS((char_u *dirname_start));
134static buf_T *load_dummy_buffer __ARGS((char_u *fname, char_u *dirname_start, char_u *resulting_dir));
135static void wipe_dummy_buffer __ARGS((buf_T *buf, char_u *dirname_start));
136static void unload_dummy_buffer __ARGS((buf_T *buf, char_u *dirname_start));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000137static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000139/* Quickfix window check helper macro */
140#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
141/* Location list window check helper macro */
142#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
143/*
144 * Return location list for window 'wp'
145 * For location list window, return the referenced location list
146 */
147#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000150 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200151 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 * Return -1 for error, number of errors for success.
153 */
154 int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200155qf_init(wp, efile, errorformat, newlist, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000156 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 char_u *efile;
158 char_u *errorformat;
159 int newlist; /* TRUE: start a new error list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200160 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162 qf_info_T *qi = &ql_info;
163
Bram Moolenaar86b68352004-12-27 21:59:20 +0000164 if (efile == NULL)
165 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000166
167 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000168 {
169 qi = ll_get_or_alloc_list(wp);
170 if (qi == NULL)
171 return FAIL;
172 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000173
174 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200175 (linenr_T)0, (linenr_T)0,
176 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000177}
178
179/*
180 * Read the errorfile "efile" into memory, line by line, building the error
181 * list.
182 * Alternative: when "efile" is null read errors from buffer "buf".
183 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200184 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
185 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000186 * Return -1 for error, number of errors for success.
187 */
188 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200189qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast,
190 qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000191 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000192 char_u *efile;
193 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000194 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000195 char_u *errorformat;
196 int newlist; /* TRUE: start a new error list */
197 linenr_T lnumfirst; /* first line number to use */
198 linenr_T lnumlast; /* last line number to use */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200199 char_u *qf_title;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000200{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 char_u *namebuf;
202 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000203 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 char_u *fmtstr = NULL;
205 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000206 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 int type = 0;
208 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000209 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 long lnum = 0L;
211 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000212 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000213 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000215 efm_T *fmt_first = NULL;
216 efm_T *fmt_last = NULL;
217 efm_T *fmt_ptr;
218 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 char_u *efm;
220 char_u *ptr;
221 char_u *srcptr;
222 int len;
223 int i;
224 int round;
225 int idx = 0;
226 int multiline = FALSE;
227 int multiignore = FALSE;
228 int multiscan = FALSE;
229 int retval = -1; /* default: return error flag */
230 char_u *directory = NULL;
231 char_u *currfile = NULL;
232 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000233 char_u *p_str = NULL;
234 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 struct dir_stack_T *file_stack = NULL;
236 regmatch_T regmatch;
237 static struct fmtpattern
238 {
239 char_u convchar;
240 char *pattern;
241 } fmt_pat[FMT_PATTERNS] =
242 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000243 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 {'n', "\\d\\+"},
245 {'l', "\\d\\+"},
246 {'c', "\\d\\+"},
247 {'t', "."},
248 {'m', ".\\+"},
249 {'r', ".*"},
Bram Moolenaarf13de072012-06-01 18:34:41 +0200250 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000251 {'v', "\\d\\+"},
252 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 };
254
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 namebuf = alloc(CMDBUFFSIZE + 1);
256 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000257 pattern = alloc(CMDBUFFSIZE + 1);
258 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 goto qf_init_end;
260
Bram Moolenaar86b68352004-12-27 21:59:20 +0000261 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
263 EMSG2(_(e_openerrf), efile);
264 goto qf_init_end;
265 }
266
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000267 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100269 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000270 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000272 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200273 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 ;
275
276/*
277 * Each part of the format string is copied and modified from errorformat to
278 * regex prog. Only a few % characters are allowed.
279 */
280 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000281 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000282 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 else
284 efm = errorformat;
285 /*
286 * Get some space to modify the format string into.
287 */
288 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
289 for (round = FMT_PATTERNS; round > 0; )
290 i += (int)STRLEN(fmt_pat[--round].pattern);
291#ifdef COLON_IN_FILENAME
292 i += 12; /* "%f" can become twelve chars longer */
293#else
294 i += 2; /* "%f" can become two chars longer */
295#endif
296 if ((fmtstr = alloc(i)) == NULL)
297 goto error2;
298
Bram Moolenaar01265852006-03-20 21:50:15 +0000299 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 {
301 /*
302 * Allocate a new eformat structure and put it at the end of the list
303 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000304 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 if (fmt_ptr == NULL)
306 goto error2;
307 if (fmt_first == NULL) /* first one */
308 fmt_first = fmt_ptr;
309 else
310 fmt_last->next = fmt_ptr;
311 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
313 /*
314 * Isolate one part in the 'errorformat' option
315 */
316 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
317 if (efm[len] == '\\' && efm[len + 1] != NUL)
318 ++len;
319
320 /*
321 * Build regexp pattern from current 'errorformat' option
322 */
323 ptr = fmtstr;
324 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000325 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 for (efmp = efm; efmp < efm + len; ++efmp)
327 {
328 if (*efmp == '%')
329 {
330 ++efmp;
331 for (idx = 0; idx < FMT_PATTERNS; ++idx)
332 if (fmt_pat[idx].convchar == *efmp)
333 break;
334 if (idx < FMT_PATTERNS)
335 {
336 if (fmt_ptr->addr[idx])
337 {
338 sprintf((char *)errmsg,
339 _("E372: Too many %%%c in format string"), *efmp);
340 EMSG(errmsg);
341 goto error2;
342 }
343 if ((idx
344 && idx < 6
345 && vim_strchr((char_u *)"DXOPQ",
346 fmt_ptr->prefix) != NULL)
347 || (idx == 6
348 && vim_strchr((char_u *)"OPQ",
349 fmt_ptr->prefix) == NULL))
350 {
351 sprintf((char *)errmsg,
352 _("E373: Unexpected %%%c in format string"), *efmp);
353 EMSG(errmsg);
354 goto error2;
355 }
356 fmt_ptr->addr[idx] = (char_u)++round;
357 *ptr++ = '\\';
358 *ptr++ = '(';
359#ifdef BACKSLASH_IN_FILENAME
360 if (*efmp == 'f')
361 {
362 /* Also match "c:" in the file name, even when
363 * checking for a colon next: "%f:".
364 * "\%(\a:\)\=" */
365 STRCPY(ptr, "\\%(\\a:\\)\\=");
366 ptr += 10;
367 }
368#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000369 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000371 if (efmp[1] != '\\' && efmp[1] != '%')
372 {
373 /* A file name may contain spaces, but this isn't
374 * in "\f". For "%f:%l:%m" there may be a ":" in
375 * the file name. Use ".\{-1,}x" instead (x is
376 * the next character), the requirement that :999:
377 * follows should work. */
378 STRCPY(ptr, ".\\{-1,}");
379 ptr += 7;
380 }
381 else
382 {
383 /* File name followed by '\\' or '%': include as
384 * many file name chars as possible. */
385 STRCPY(ptr, "\\f\\+");
386 ptr += 4;
387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 }
389 else
390 {
391 srcptr = (char_u *)fmt_pat[idx].pattern;
392 while ((*ptr = *srcptr++) != NUL)
393 ++ptr;
394 }
395 *ptr++ = '\\';
396 *ptr++ = ')';
397 }
398 else if (*efmp == '*')
399 {
400 if (*++efmp == '[' || *efmp == '\\')
401 {
402 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
403 {
404 if (efmp[1] == '^')
405 *ptr++ = *++efmp;
406 if (efmp < efm + len)
407 {
408 *ptr++ = *++efmp; /* could be ']' */
409 while (efmp < efm + len
410 && (*ptr++ = *++efmp) != ']')
411 /* skip */;
412 if (efmp == efm + len)
413 {
414 EMSG(_("E374: Missing ] in format string"));
415 goto error2;
416 }
417 }
418 }
419 else if (efmp < efm + len) /* %*\D, %*\s etc. */
420 *ptr++ = *++efmp;
421 *ptr++ = '\\';
422 *ptr++ = '+';
423 }
424 else
425 {
426 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
427 sprintf((char *)errmsg,
428 _("E375: Unsupported %%%c in format string"), *efmp);
429 EMSG(errmsg);
430 goto error2;
431 }
432 }
433 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
434 *ptr++ = *efmp; /* regexp magic characters */
435 else if (*efmp == '#')
436 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000437 else if (*efmp == '>')
438 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 else if (efmp == efm + 1) /* analyse prefix */
440 {
441 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
442 fmt_ptr->flags = *efmp++;
443 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
444 fmt_ptr->prefix = *efmp;
445 else
446 {
447 sprintf((char *)errmsg,
448 _("E376: Invalid %%%c in format string prefix"), *efmp);
449 EMSG(errmsg);
450 goto error2;
451 }
452 }
453 else
454 {
455 sprintf((char *)errmsg,
456 _("E377: Invalid %%%c in format string"), *efmp);
457 EMSG(errmsg);
458 goto error2;
459 }
460 }
461 else /* copy normal character */
462 {
463 if (*efmp == '\\' && efmp + 1 < efm + len)
464 ++efmp;
465 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
466 *ptr++ = '\\'; /* escape regexp atoms */
467 if (*efmp)
468 *ptr++ = *efmp;
469 }
470 }
471 *ptr++ = '$';
472 *ptr = NUL;
473 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
474 goto error2;
475 /*
476 * Advance to next part
477 */
478 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
479 }
480 if (fmt_first == NULL) /* nothing found */
481 {
482 EMSG(_("E378: 'errorformat' contains no pattern"));
483 goto error2;
484 }
485
486 /*
487 * got_int is reset here, because it was probably set when killing the
488 * ":make" command, but we still want to read the errorfile then.
489 */
490 got_int = FALSE;
491
492 /* Always ignore case when looking for a matching error. */
493 regmatch.rm_ic = TRUE;
494
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000495 if (tv != NULL)
496 {
497 if (tv->v_type == VAR_STRING)
498 p_str = tv->vval.v_string;
499 else if (tv->v_type == VAR_LIST)
500 p_li = tv->vval.v_list->lv_first;
501 }
502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 /*
504 * Read the lines in the error file one by one.
505 * Try to recognize one of the error formats in each line.
506 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000507 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000509 /* Get the next line. */
510 if (fd == NULL)
511 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000512 if (tv != NULL)
513 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000514 if (tv->v_type == VAR_STRING)
515 {
516 /* Get the next line from the supplied string */
517 char_u *p;
518
519 if (!*p_str) /* Reached the end of the string */
520 break;
521
522 p = vim_strchr(p_str, '\n');
523 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000524 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000525 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000526 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000527
528 if (len > CMDBUFFSIZE - 2)
529 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
530 else
531 vim_strncpy(IObuff, p_str, len);
532
533 p_str += len;
534 }
535 else if (tv->v_type == VAR_LIST)
536 {
537 /* Get the next line from the supplied list */
538 while (p_li && p_li->li_tv.v_type != VAR_STRING)
539 p_li = p_li->li_next; /* Skip non-string items */
540
541 if (!p_li) /* End of the list */
542 break;
543
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000544 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000545 if (len > CMDBUFFSIZE - 2)
546 len = CMDBUFFSIZE - 2;
547
548 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
549
550 p_li = p_li->li_next; /* next item */
551 }
552 }
553 else
554 {
555 /* Get the next line from the supplied buffer */
556 if (buflnum > lnumlast)
557 break;
558 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
559 CMDBUFFSIZE - 2);
560 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000561 }
562 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
563 break;
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
Bram Moolenaar836082d2011-08-10 13:21:46 +0200566#ifdef FEAT_MBYTE
567 remove_bom(IObuff);
568#endif
569
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
571 *efmp = NUL;
572#ifdef USE_CRNL
573 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
574 *efmp = NUL;
575#endif
576
Bram Moolenaar01265852006-03-20 21:50:15 +0000577 /* If there was no %> item start at the first pattern */
578 if (fmt_start == NULL)
579 fmt_ptr = fmt_first;
580 else
581 {
582 fmt_ptr = fmt_start;
583 fmt_start = NULL;
584 }
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 /*
587 * Try to match each part of 'errorformat' until we find a complete
588 * match or no match.
589 */
590 valid = TRUE;
591restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000592 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {
594 idx = fmt_ptr->prefix;
595 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
596 continue;
597 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000598 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (!multiscan)
600 errmsg[0] = NUL;
601 lnum = 0;
602 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000603 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 enr = -1;
605 type = 0;
606 tail = NULL;
607
608 regmatch.regprog = fmt_ptr->prog;
609 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
610 {
611 if ((idx == 'C' || idx == 'Z') && !multiline)
612 continue;
613 if (vim_strchr((char_u *)"EWI", idx) != NULL)
614 type = idx;
615 else
616 type = 0;
617 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000618 * Extract error message data from matched line.
619 * We check for an actual submatch, because "\[" and "\]" in
620 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 */
622 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
623 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000624 int c;
625
626 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
627 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000628
629 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000630 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000631 *regmatch.endp[i] = NUL;
632 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
633 *regmatch.endp[i] = c;
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000636 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 continue;
638 }
639 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000640 {
641 if (regmatch.startp[i] == NULL)
642 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000646 {
647 if (regmatch.startp[i] == NULL)
648 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000652 {
653 if (regmatch.startp[i] == NULL)
654 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000658 {
659 if (regmatch.startp[i] == NULL)
660 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000662 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000663 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 STRCPY(errmsg, IObuff);
665 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
666 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000667 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
668 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000670 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
672 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000673 {
674 if (regmatch.startp[i] == NULL)
675 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
679 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200680 char_u *match_ptr;
681
Bram Moolenaar4169da72006-06-20 18:49:32 +0000682 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
683 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200684 col = 0;
685 for (match_ptr = regmatch.startp[i];
686 match_ptr != regmatch.endp[i]; ++match_ptr)
687 {
688 ++col;
689 if (*match_ptr == TAB)
690 {
691 col += 7;
692 col -= col % 8;
693 }
694 }
695 ++col;
696 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
699 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000700 if (regmatch.startp[i] == NULL)
701 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000705 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
706 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000707 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
708 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000709 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
710 if (len > CMDBUFFSIZE - 5)
711 len = CMDBUFFSIZE - 5;
712 STRCPY(pattern, "^\\V");
713 STRNCAT(pattern, regmatch.startp[i], len);
714 pattern[len + 3] = '\\';
715 pattern[len + 4] = '$';
716 pattern[len + 5] = NUL;
717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 break;
719 }
720 }
721 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000722
Bram Moolenaar4770d092006-01-12 23:22:24 +0000723 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000725 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
727 if (idx == 'D') /* enter directory */
728 {
729 if (*namebuf == NUL)
730 {
731 EMSG(_("E379: Missing or empty directory name"));
732 goto error2;
733 }
734 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
735 goto error2;
736 }
737 else if (idx == 'X') /* leave directory */
738 directory = qf_pop_dir(&dir_stack);
739 }
740 namebuf[0] = NUL; /* no match found, remove file name */
741 lnum = 0; /* don't jump to this line */
742 valid = FALSE;
743 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000744 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 multiline = multiignore = FALSE;
746 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000747 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000749 /* honor %> item */
750 if (fmt_ptr->conthere)
751 fmt_start = fmt_ptr;
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
754 multiline = TRUE; /* start of a multi-line message */
755 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
756 { /* continuation of multi-line msg */
757 if (qfprev == NULL)
758 goto error2;
759 if (*errmsg && !multiignore)
760 {
761 len = (int)STRLEN(qfprev->qf_text);
762 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
763 == NULL)
764 goto error2;
765 STRCPY(ptr, qfprev->qf_text);
766 vim_free(qfprev->qf_text);
767 qfprev->qf_text = ptr;
768 *(ptr += len) = '\n';
769 STRCPY(++ptr, errmsg);
770 }
771 if (qfprev->qf_nr == -1)
772 qfprev->qf_nr = enr;
773 if (vim_isprintc(type) && !qfprev->qf_type)
774 qfprev->qf_type = type; /* only printable chars allowed */
775 if (!qfprev->qf_lnum)
776 qfprev->qf_lnum = lnum;
777 if (!qfprev->qf_col)
778 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (!qfprev->qf_fnum)
781 qfprev->qf_fnum = qf_get_fnum(directory,
782 *namebuf || directory ? namebuf
783 : currfile && valid ? currfile : 0);
784 if (idx == 'Z')
785 multiline = multiignore = FALSE;
786 line_breakcheck();
787 continue;
788 }
789 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
790 {
791 /* global file names */
792 valid = FALSE;
793 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
794 {
795 if (*namebuf && idx == 'P')
796 currfile = qf_push_dir(namebuf, &file_stack);
797 else if (idx == 'Q')
798 currfile = qf_pop_dir(&file_stack);
799 *namebuf = NUL;
800 if (tail && *tail)
801 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000802 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 multiscan = TRUE;
804 goto restofline;
805 }
806 }
807 }
808 if (fmt_ptr->flags == '-') /* generally exclude this line */
809 {
810 if (multiline)
811 multiignore = TRUE; /* also exclude continuation lines */
812 continue;
813 }
814 }
815
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000816 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000818 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000820 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000821 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 errmsg,
823 lnum,
824 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000826 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 enr,
828 type,
829 valid) == FAIL)
830 goto error2;
831 line_breakcheck();
832 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000833 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000835 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000837 /* no valid entry found */
838 qi->qf_lists[qi->qf_curlist].qf_ptr =
839 qi->qf_lists[qi->qf_curlist].qf_start;
840 qi->qf_lists[qi->qf_curlist].qf_index = 1;
841 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 else
844 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000845 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
846 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
847 qi->qf_lists[qi->qf_curlist].qf_ptr =
848 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000850 /* return number of matches */
851 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 goto qf_init_ok;
853 }
854 EMSG(_(e_readerrf));
855error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000856 qf_free(qi, qi->qf_curlist);
857 qi->qf_listcount--;
858 if (qi->qf_curlist > 0)
859 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000861 if (fd != NULL)
862 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
864 {
865 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +0200866 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 vim_free(fmt_ptr);
868 }
869 qf_clean_dir_stack(&dir_stack);
870 qf_clean_dir_stack(&file_stack);
871qf_init_end:
872 vim_free(namebuf);
873 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000874 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 vim_free(fmtstr);
876
877#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000878 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
880
881 return retval;
882}
883
884/*
885 * Prepare for adding a new quickfix list.
886 */
887 static void
Bram Moolenaar94116152012-11-28 17:41:59 +0100888qf_new_list(qi, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000889 qf_info_T *qi;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200890 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 int i;
893
894 /*
895 * If the current entry is not the last entry, delete entries below
896 * the current entry. This makes it possible to browse in a tree-like
897 * way with ":grep'.
898 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000899 while (qi->qf_listcount > qi->qf_curlist + 1)
900 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 /*
903 * When the stack is full, remove to oldest entry
904 * Otherwise, add a new entry.
905 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000906 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000908 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000910 qi->qf_lists[i - 1] = qi->qf_lists[i];
911 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000914 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100915 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200916 if (qf_title != NULL)
917 {
Bram Moolenaar5e109c42010-07-26 22:51:28 +0200918 char_u *p = alloc((int)STRLEN(qf_title) + 2);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200919
920 qi->qf_lists[qi->qf_curlist].qf_title = p;
921 if (p != NULL)
922 sprintf((char *)p, ":%s", (char *)qf_title);
923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924}
925
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000926/*
927 * Free a location list
928 */
929 static void
930ll_free_all(pqi)
931 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000932{
933 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000934 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000935
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000936 qi = *pqi;
937 if (qi == NULL)
938 return;
939 *pqi = NULL; /* Remove reference to this list */
940
941 qi->qf_refcount--;
942 if (qi->qf_refcount < 1)
943 {
944 /* No references to this location list */
945 for (i = 0; i < qi->qf_listcount; ++i)
946 qf_free(qi, i);
947 vim_free(qi);
948 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000949}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000950
951 void
952qf_free_all(wp)
953 win_T *wp;
954{
955 int i;
956 qf_info_T *qi = &ql_info;
957
958 if (wp != NULL)
959 {
960 /* location list */
961 ll_free_all(&wp->w_llist);
962 ll_free_all(&wp->w_llist_ref);
963 }
964 else
965 /* quickfix list */
966 for (i = 0; i < qi->qf_listcount; ++i)
967 qf_free(qi, i);
968}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970/*
971 * Add an entry to the end of the list of errors.
972 * Returns OK or FAIL.
973 */
974 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000975qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
976 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000977 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000978 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 char_u *dir; /* optional directory name */
980 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000981 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 char_u *mesg; /* message */
983 long lnum; /* line number */
984 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000985 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000986 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 int nr; /* error number */
988 int type; /* type character */
989 int valid; /* valid entry */
990{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000991 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000993 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000995 if (bufnum != 0)
996 qfp->qf_fnum = bufnum;
997 else
998 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1000 {
1001 vim_free(qfp);
1002 return FAIL;
1003 }
1004 qfp->qf_lnum = lnum;
1005 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001006 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001007 if (pattern == NULL || *pattern == NUL)
1008 qfp->qf_pattern = NULL;
1009 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1010 {
1011 vim_free(qfp->qf_text);
1012 vim_free(qfp);
1013 return FAIL;
1014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 qfp->qf_nr = nr;
1016 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1017 type = 0;
1018 qfp->qf_type = type;
1019 qfp->qf_valid = valid;
1020
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001021 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1022 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001024 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 qfp->qf_prev = qfp; /* first element points to itself */
1026 }
1027 else
1028 {
1029 qfp->qf_prev = *prevp;
1030 (*prevp)->qf_next = qfp;
1031 }
1032 qfp->qf_next = qfp; /* last element points to itself */
1033 qfp->qf_cleared = FALSE;
1034 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001035 ++qi->qf_lists[qi->qf_curlist].qf_count;
1036 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1037 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001039 qi->qf_lists[qi->qf_curlist].qf_index =
1040 qi->qf_lists[qi->qf_curlist].qf_count;
1041 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
1043
1044 return OK;
1045}
1046
1047/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001048 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001049 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001050 static qf_info_T *
1051ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001052{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001053 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001054
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001055 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1056 if (qi != NULL)
1057 {
1058 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1059 qi->qf_refcount++;
1060 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001061
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001062 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001063}
1064
1065/*
1066 * Return the location list for window 'wp'.
1067 * If not present, allocate a location list
1068 */
1069 static qf_info_T *
1070ll_get_or_alloc_list(wp)
1071 win_T *wp;
1072{
1073 if (IS_LL_WINDOW(wp))
1074 /* For a location list window, use the referenced location list */
1075 return wp->w_llist_ref;
1076
1077 /*
1078 * For a non-location list window, w_llist_ref should not point to a
1079 * location list.
1080 */
1081 ll_free_all(&wp->w_llist_ref);
1082
1083 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001084 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001085 return wp->w_llist;
1086}
1087
1088/*
1089 * Copy the location list from window "from" to window "to".
1090 */
1091 void
1092copy_loclist(from, to)
1093 win_T *from;
1094 win_T *to;
1095{
1096 qf_info_T *qi;
1097 int idx;
1098 int i;
1099
1100 /*
1101 * When copying from a location list window, copy the referenced
1102 * location list. For other windows, copy the location list for
1103 * that window.
1104 */
1105 if (IS_LL_WINDOW(from))
1106 qi = from->w_llist_ref;
1107 else
1108 qi = from->w_llist;
1109
1110 if (qi == NULL) /* no location list to copy */
1111 return;
1112
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001113 /* allocate a new location list */
1114 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001115 return;
1116
1117 to->w_llist->qf_listcount = qi->qf_listcount;
1118
1119 /* Copy the location lists one at a time */
1120 for (idx = 0; idx < qi->qf_listcount; idx++)
1121 {
1122 qf_list_T *from_qfl;
1123 qf_list_T *to_qfl;
1124
1125 to->w_llist->qf_curlist = idx;
1126
1127 from_qfl = &qi->qf_lists[idx];
1128 to_qfl = &to->w_llist->qf_lists[idx];
1129
1130 /* Some of the fields are populated by qf_add_entry() */
1131 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1132 to_qfl->qf_count = 0;
1133 to_qfl->qf_index = 0;
1134 to_qfl->qf_start = NULL;
1135 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001136 if (from_qfl->qf_title != NULL)
1137 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1138 else
1139 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001140
1141 if (from_qfl->qf_count)
1142 {
1143 qfline_T *from_qfp;
1144 qfline_T *prevp = NULL;
1145
1146 /* copy all the location entries in this list */
1147 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1148 ++i, from_qfp = from_qfp->qf_next)
1149 {
1150 if (qf_add_entry(to->w_llist, &prevp,
1151 NULL,
1152 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001153 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001154 from_qfp->qf_text,
1155 from_qfp->qf_lnum,
1156 from_qfp->qf_col,
1157 from_qfp->qf_viscol,
1158 from_qfp->qf_pattern,
1159 from_qfp->qf_nr,
1160 0,
1161 from_qfp->qf_valid) == FAIL)
1162 {
1163 qf_free_all(to);
1164 return;
1165 }
1166 /*
1167 * qf_add_entry() will not set the qf_num field, as the
1168 * directory and file names are not supplied. So the qf_fnum
1169 * field is copied here.
1170 */
1171 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1172 prevp->qf_type = from_qfp->qf_type; /* error type */
1173 if (from_qfl->qf_ptr == from_qfp)
1174 to_qfl->qf_ptr = prevp; /* current location */
1175 }
1176 }
1177
1178 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1179
1180 /* When no valid entries are present in the list, qf_ptr points to
1181 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001182 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001183 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001184 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001185 to_qfl->qf_index = 1;
1186 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001187 }
1188
1189 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1190}
1191
1192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 * get buffer number for file "dir.name"
1194 */
1195 static int
1196qf_get_fnum(directory, fname)
1197 char_u *directory;
1198 char_u *fname;
1199{
1200 if (fname == NULL || *fname == NUL) /* no file name */
1201 return 0;
1202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 char_u *ptr;
1204 int fnum;
1205
Bram Moolenaare60acc12011-05-10 16:41:25 +02001206#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001208#endif
1209#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (directory != NULL)
1211 slash_adjust(directory);
1212 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (directory != NULL && !vim_isAbsName(fname)
1215 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1216 {
1217 /*
1218 * Here we check if the file really exists.
1219 * This should normally be true, but if make works without
1220 * "leaving directory"-messages we might have missed a
1221 * directory change.
1222 */
1223 if (mch_getperm(ptr) < 0)
1224 {
1225 vim_free(ptr);
1226 directory = qf_guess_filepath(fname);
1227 if (directory)
1228 ptr = concat_fnames(directory, fname, TRUE);
1229 else
1230 ptr = vim_strsave(fname);
1231 }
1232 /* Use concatenated directory name and file name */
1233 fnum = buflist_add(ptr, 0);
1234 vim_free(ptr);
1235 return fnum;
1236 }
1237 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239}
1240
1241/*
1242 * push dirbuf onto the directory stack and return pointer to actual dir or
1243 * NULL on error
1244 */
1245 static char_u *
1246qf_push_dir(dirbuf, stackptr)
1247 char_u *dirbuf;
1248 struct dir_stack_T **stackptr;
1249{
1250 struct dir_stack_T *ds_new;
1251 struct dir_stack_T *ds_ptr;
1252
1253 /* allocate new stack element and hook it in */
1254 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1255 if (ds_new == NULL)
1256 return NULL;
1257
1258 ds_new->next = *stackptr;
1259 *stackptr = ds_new;
1260
1261 /* store directory on the stack */
1262 if (vim_isAbsName(dirbuf)
1263 || (*stackptr)->next == NULL
1264 || (*stackptr && dir_stack != *stackptr))
1265 (*stackptr)->dirname = vim_strsave(dirbuf);
1266 else
1267 {
1268 /* Okay we don't have an absolute path.
1269 * dirbuf must be a subdir of one of the directories on the stack.
1270 * Let's search...
1271 */
1272 ds_new = (*stackptr)->next;
1273 (*stackptr)->dirname = NULL;
1274 while (ds_new)
1275 {
1276 vim_free((*stackptr)->dirname);
1277 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1278 TRUE);
1279 if (mch_isdir((*stackptr)->dirname) == TRUE)
1280 break;
1281
1282 ds_new = ds_new->next;
1283 }
1284
1285 /* clean up all dirs we already left */
1286 while ((*stackptr)->next != ds_new)
1287 {
1288 ds_ptr = (*stackptr)->next;
1289 (*stackptr)->next = (*stackptr)->next->next;
1290 vim_free(ds_ptr->dirname);
1291 vim_free(ds_ptr);
1292 }
1293
1294 /* Nothing found -> it must be on top level */
1295 if (ds_new == NULL)
1296 {
1297 vim_free((*stackptr)->dirname);
1298 (*stackptr)->dirname = vim_strsave(dirbuf);
1299 }
1300 }
1301
1302 if ((*stackptr)->dirname != NULL)
1303 return (*stackptr)->dirname;
1304 else
1305 {
1306 ds_ptr = *stackptr;
1307 *stackptr = (*stackptr)->next;
1308 vim_free(ds_ptr);
1309 return NULL;
1310 }
1311}
1312
1313
1314/*
1315 * pop dirbuf from the directory stack and return previous directory or NULL if
1316 * stack is empty
1317 */
1318 static char_u *
1319qf_pop_dir(stackptr)
1320 struct dir_stack_T **stackptr;
1321{
1322 struct dir_stack_T *ds_ptr;
1323
1324 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1325 * What to do if it isn't? */
1326
1327 /* pop top element and free it */
1328 if (*stackptr != NULL)
1329 {
1330 ds_ptr = *stackptr;
1331 *stackptr = (*stackptr)->next;
1332 vim_free(ds_ptr->dirname);
1333 vim_free(ds_ptr);
1334 }
1335
1336 /* return NEW top element as current dir or NULL if stack is empty*/
1337 return *stackptr ? (*stackptr)->dirname : NULL;
1338}
1339
1340/*
1341 * clean up directory stack
1342 */
1343 static void
1344qf_clean_dir_stack(stackptr)
1345 struct dir_stack_T **stackptr;
1346{
1347 struct dir_stack_T *ds_ptr;
1348
1349 while ((ds_ptr = *stackptr) != NULL)
1350 {
1351 *stackptr = (*stackptr)->next;
1352 vim_free(ds_ptr->dirname);
1353 vim_free(ds_ptr);
1354 }
1355}
1356
1357/*
1358 * Check in which directory of the directory stack the given file can be
1359 * found.
1360 * Returns a pointer to the directory name or NULL if not found
1361 * Cleans up intermediate directory entries.
1362 *
1363 * TODO: How to solve the following problem?
1364 * If we have the this directory tree:
1365 * ./
1366 * ./aa
1367 * ./aa/bb
1368 * ./bb
1369 * ./bb/x.c
1370 * and make says:
1371 * making all in aa
1372 * making all in bb
1373 * x.c:9: Error
1374 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1375 * qf_guess_filepath will return NULL.
1376 */
1377 static char_u *
1378qf_guess_filepath(filename)
1379 char_u *filename;
1380{
1381 struct dir_stack_T *ds_ptr;
1382 struct dir_stack_T *ds_tmp;
1383 char_u *fullname;
1384
1385 /* no dirs on the stack - there's nothing we can do */
1386 if (dir_stack == NULL)
1387 return NULL;
1388
1389 ds_ptr = dir_stack->next;
1390 fullname = NULL;
1391 while (ds_ptr)
1392 {
1393 vim_free(fullname);
1394 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1395
1396 /* If concat_fnames failed, just go on. The worst thing that can happen
1397 * is that we delete the entire stack.
1398 */
1399 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1400 break;
1401
1402 ds_ptr = ds_ptr->next;
1403 }
1404
1405 vim_free(fullname);
1406
1407 /* clean up all dirs we already left */
1408 while (dir_stack->next != ds_ptr)
1409 {
1410 ds_tmp = dir_stack->next;
1411 dir_stack->next = dir_stack->next->next;
1412 vim_free(ds_tmp->dirname);
1413 vim_free(ds_tmp);
1414 }
1415
1416 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1417
1418}
1419
1420/*
1421 * jump to a quickfix line
1422 * if dir == FORWARD go "errornr" valid entries forward
1423 * if dir == BACKWARD go "errornr" valid entries backward
1424 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1425 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1426 * else if "errornr" is zero, redisplay the same line
1427 * else go to entry "errornr"
1428 */
1429 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001430qf_jump(qi, dir, errornr, forceit)
1431 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 int dir;
1433 int errornr;
1434 int forceit;
1435{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001436 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001437 qfline_T *qf_ptr;
1438 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 int qf_index;
1440 int old_qf_fnum;
1441 int old_qf_index;
1442 int prev_index;
1443 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1444 char_u *err = e_no_more_items;
1445 linenr_T i;
1446 buf_T *old_curbuf;
1447 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 colnr_T screen_col;
1449 colnr_T char_col;
1450 char_u *line;
1451#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001452 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001453 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 int opened_window = FALSE;
1455 win_T *win;
1456 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001457 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001459 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 int print_message = TRUE;
1461 int len;
1462#ifdef FEAT_FOLDING
1463 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1464#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001465 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001466 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001468 if (qi == NULL)
1469 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001470
1471 if (qi->qf_curlist >= qi->qf_listcount
1472 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 {
1474 EMSG(_(e_quickfix));
1475 return;
1476 }
1477
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001478 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001480 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 old_qf_index = qf_index;
1482 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1483 {
1484 while (errornr--)
1485 {
1486 old_qf_ptr = qf_ptr;
1487 prev_index = qf_index;
1488 old_qf_fnum = qf_ptr->qf_fnum;
1489 do
1490 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001491 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 || qf_ptr->qf_next == NULL)
1493 {
1494 qf_ptr = old_qf_ptr;
1495 qf_index = prev_index;
1496 if (err != NULL)
1497 {
1498 EMSG(_(err));
1499 goto theend;
1500 }
1501 errornr = 0;
1502 break;
1503 }
1504 ++qf_index;
1505 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001506 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1507 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1509 err = NULL;
1510 }
1511 }
1512 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1513 {
1514 while (errornr--)
1515 {
1516 old_qf_ptr = qf_ptr;
1517 prev_index = qf_index;
1518 old_qf_fnum = qf_ptr->qf_fnum;
1519 do
1520 {
1521 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1522 {
1523 qf_ptr = old_qf_ptr;
1524 qf_index = prev_index;
1525 if (err != NULL)
1526 {
1527 EMSG(_(err));
1528 goto theend;
1529 }
1530 errornr = 0;
1531 break;
1532 }
1533 --qf_index;
1534 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001535 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1536 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1538 err = NULL;
1539 }
1540 }
1541 else if (errornr != 0) /* go to specified number */
1542 {
1543 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1544 {
1545 --qf_index;
1546 qf_ptr = qf_ptr->qf_prev;
1547 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548 while (errornr > qf_index && qf_index <
1549 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 && qf_ptr->qf_next != NULL)
1551 {
1552 ++qf_index;
1553 qf_ptr = qf_ptr->qf_next;
1554 }
1555 }
1556
1557#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001558 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1559 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 /* No need to print the error message if it's visible in the error
1561 * window */
1562 print_message = FALSE;
1563
1564 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001565 * For ":helpgrep" find a help window or open one.
1566 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001567 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001568 {
1569 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001570
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001571 if (cmdmod.tab != 0)
1572 wp = NULL;
1573 else
1574 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1575 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1576 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001577 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1578 win_enter(wp, TRUE);
1579 else
1580 {
1581 /*
1582 * Split off help window; put it at far top if no position
1583 * specified, the current window is vertically split and narrow.
1584 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001585 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001586# ifdef FEAT_VERTSPLIT
1587 if (cmdmod.split == 0 && curwin->w_width != Columns
1588 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001589 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001590# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001591 if (qi != &ql_info)
1592 flags |= WSP_NEWLOC; /* don't copy the location list */
1593
1594 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001595 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001596 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001597
1598 if (curwin->w_height < p_hh)
1599 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001600
1601 if (qi != &ql_info) /* not a quickfix list */
1602 {
1603 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001604 curwin->w_llist = qi;
1605 qi->qf_refcount++;
1606 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001607 }
1608
1609 if (!p_im)
1610 restart_edit = 0; /* don't want insert mode in help file */
1611 }
1612
1613 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 * If currently in the quickfix window, find another window to show the
1615 * file in.
1616 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001617 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
1619 /*
1620 * If there is no file specified, we don't know where to go.
1621 * But do advance, otherwise ":cn" gets stuck.
1622 */
1623 if (qf_ptr->qf_fnum == 0)
1624 goto theend;
1625
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001626 /* Locate a window showing a normal buffer */
1627 usable_win = 0;
1628 FOR_ALL_WINDOWS(win)
1629 if (win->w_buffer->b_p_bt[0] == NUL)
1630 {
1631 usable_win = 1;
1632 break;
1633 }
1634
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001636 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001637 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001639 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001640 {
1641 tabpage_T *tp;
1642 win_T *wp;
1643
1644 FOR_ALL_TAB_WINDOWS(tp, wp)
1645 {
1646 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1647 {
1648 goto_tabpage_win(tp, wp);
1649 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001650 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001651 }
1652 }
1653 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001654win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001655
1656 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001657 * If there is only one window and it is the quickfix window, create a
1658 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001659 */
1660 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001662 ll_ref = curwin->w_llist_ref;
1663
Bram Moolenaar884ae642009-02-22 01:37:59 +00001664 flags = WSP_ABOVE;
1665 if (ll_ref != NULL)
1666 flags |= WSP_NEWLOC;
1667 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 goto failed; /* not enough room for window */
1669 opened_window = TRUE; /* close it when fail */
1670 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001671 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001672 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001673 if (ll_ref != NULL)
1674 {
1675 /* The new window should use the location list from the
1676 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001677 curwin->w_llist = ll_ref;
1678 ll_ref->qf_refcount++;
1679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
1681 else
1682 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001683 if (curwin->w_llist_ref != NULL)
1684 {
1685 /* In a location window */
1686 ll_ref = curwin->w_llist_ref;
1687
1688 /* Find the window with the same location list */
1689 FOR_ALL_WINDOWS(win)
1690 if (win->w_llist == ll_ref)
1691 break;
1692 if (win == NULL)
1693 {
1694 /* Find the window showing the selected file */
1695 FOR_ALL_WINDOWS(win)
1696 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1697 break;
1698 if (win == NULL)
1699 {
1700 /* Find a previous usable window */
1701 win = curwin;
1702 do
1703 {
1704 if (win->w_buffer->b_p_bt[0] == NUL)
1705 break;
1706 if (win->w_prev == NULL)
1707 win = lastwin; /* wrap around the top */
1708 else
1709 win = win->w_prev; /* go to previous window */
1710 } while (win != curwin);
1711 }
1712 }
1713 win_goto(win);
1714
1715 /* If the location list for the window is not set, then set it
1716 * to the location list from the location window */
1717 if (win->w_llist == NULL)
1718 {
1719 win->w_llist = ll_ref;
1720 ll_ref->qf_refcount++;
1721 }
1722 }
1723 else
1724 {
1725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 /*
1727 * Try to find a window that shows the right buffer.
1728 * Default to the window just above the quickfix buffer.
1729 */
1730 win = curwin;
1731 altwin = NULL;
1732 for (;;)
1733 {
1734 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1735 break;
1736 if (win->w_prev == NULL)
1737 win = lastwin; /* wrap around the top */
1738 else
1739 win = win->w_prev; /* go to previous window */
1740
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001741 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 {
1743 /* Didn't find it, go to the window before the quickfix
1744 * window. */
1745 if (altwin != NULL)
1746 win = altwin;
1747 else if (curwin->w_prev != NULL)
1748 win = curwin->w_prev;
1749 else
1750 win = curwin->w_next;
1751 break;
1752 }
1753
1754 /* Remember a usable window. */
1755 if (altwin == NULL && !win->w_p_pvw
1756 && win->w_buffer->b_p_bt[0] == NUL)
1757 altwin = win;
1758 }
1759
1760 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
1763 }
1764#endif
1765
1766 /*
1767 * If there is a file name,
1768 * read the wanted file if needed, and check autowrite etc.
1769 */
1770 old_curbuf = curbuf;
1771 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001772
1773 if (qf_ptr->qf_fnum != 0)
1774 {
1775 if (qf_ptr->qf_type == 1)
1776 {
1777 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1778 * set b_p_ro flag). */
1779 if (!can_abandon(curbuf, forceit))
1780 {
1781 EMSG(_(e_nowrtmsg));
1782 ok = FALSE;
1783 }
1784 else
1785 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001786 ECMD_HIDE + ECMD_SET_HELP,
1787 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001788 }
1789 else
1790 ok = buflist_getfile(qf_ptr->qf_fnum,
1791 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1792 }
1793
1794 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
1796 /* When not switched to another buffer, still need to set pc mark */
1797 if (curbuf == old_curbuf)
1798 setpcmark();
1799
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001800 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001802 /*
1803 * Go to line with error, unless qf_lnum is 0.
1804 */
1805 i = qf_ptr->qf_lnum;
1806 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001808 if (i > curbuf->b_ml.ml_line_count)
1809 i = curbuf->b_ml.ml_line_count;
1810 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001812 if (qf_ptr->qf_col > 0)
1813 {
1814 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1815 if (qf_ptr->qf_viscol == TRUE)
1816 {
1817 /*
1818 * Check each character from the beginning of the error
1819 * line up to the error column. For each tab character
1820 * found, reduce the error column value by the length of
1821 * a tab character.
1822 */
1823 line = ml_get_curline();
1824 screen_col = 0;
1825 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1826 {
1827 if (*line == NUL)
1828 break;
1829 if (*line++ == '\t')
1830 {
1831 curwin->w_cursor.col -= 7 - (screen_col % 8);
1832 screen_col += 8 - (screen_col % 8);
1833 }
1834 else
1835 ++screen_col;
1836 }
1837 }
1838 check_cursor();
1839 }
1840 else
1841 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
1843 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001844 {
1845 pos_T save_cursor;
1846
1847 /* Move the cursor to the first line in the buffer */
1848 save_cursor = curwin->w_cursor;
1849 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001850 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1851 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001852 curwin->w_cursor = save_cursor;
1853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855#ifdef FEAT_FOLDING
1856 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1857 foldOpenCursor();
1858#endif
1859 if (print_message)
1860 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001861 /* Update the screen before showing the message, unless the screen
1862 * scrolled up. */
1863 if (!msg_scrolled)
1864 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001866 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1868 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1869 /* Add the message, skipping leading whitespace and newlines. */
1870 len = (int)STRLEN(IObuff);
1871 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1872
1873 /* Output the message. Overwrite to avoid scrolling when the 'O'
1874 * flag is present in 'shortmess'; But when not jumping, print the
1875 * whole message. */
1876 i = msg_scroll;
1877 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1878 msg_scroll = TRUE;
1879 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1880 msg_scroll = FALSE;
1881 msg_attr_keep(IObuff, 0, TRUE);
1882 msg_scroll = i;
1883 }
1884 }
1885 else
1886 {
1887#ifdef FEAT_WINDOWS
1888 if (opened_window)
1889 win_close(curwin, TRUE); /* Close opened window */
1890#endif
1891 if (qf_ptr->qf_fnum != 0)
1892 {
1893 /*
1894 * Couldn't open file, so put index back where it was. This could
1895 * happen if the file was readonly and we changed something.
1896 */
1897#ifdef FEAT_WINDOWS
1898failed:
1899#endif
1900 qf_ptr = old_qf_ptr;
1901 qf_index = old_qf_index;
1902 }
1903 }
1904theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001905 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1906 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907#ifdef FEAT_WINDOWS
1908 if (p_swb != old_swb && opened_window)
1909 {
1910 /* Restore old 'switchbuf' value, but not when an autocommand or
1911 * modeline has changed the value. */
1912 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001915 swb_flags = old_swb_flags;
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 else
1918 free_string_option(old_swb);
1919 }
1920#endif
1921}
1922
1923/*
1924 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001925 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 */
1927 void
1928qf_list(eap)
1929 exarg_T *eap;
1930{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001931 buf_T *buf;
1932 char_u *fname;
1933 qfline_T *qfp;
1934 int i;
1935 int idx1 = 1;
1936 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001937 char_u *arg = eap->arg;
1938 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001940 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 if (eap->cmdidx == CMD_llist)
1943 {
1944 qi = GET_LOC_LIST(curwin);
1945 if (qi == NULL)
1946 {
1947 EMSG(_(e_loclist));
1948 return;
1949 }
1950 }
1951
1952 if (qi->qf_curlist >= qi->qf_listcount
1953 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
1955 EMSG(_(e_quickfix));
1956 return;
1957 }
1958 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1959 {
1960 EMSG(_(e_trailing));
1961 return;
1962 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001963 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (idx1 < 0)
1965 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1966 if (idx2 < 0)
1967 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1968
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001969 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001971 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1972 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1975 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001976 msg_putchar('\n');
1977 if (got_int)
1978 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001979
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001980 fname = NULL;
1981 if (qfp->qf_fnum != 0
1982 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1983 {
1984 fname = buf->b_fname;
1985 if (qfp->qf_type == 1) /* :helpgrep */
1986 fname = gettail(fname);
1987 }
1988 if (fname == NULL)
1989 sprintf((char *)IObuff, "%2d", i);
1990 else
1991 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1992 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001993 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001994 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1995 if (qfp->qf_lnum == 0)
1996 IObuff[0] = NUL;
1997 else if (qfp->qf_col == 0)
1998 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1999 else
2000 sprintf((char *)IObuff, ":%ld col %d",
2001 qfp->qf_lnum, qfp->qf_col);
2002 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2003 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2004 msg_puts_attr(IObuff, hl_attr(HLF_N));
2005 if (qfp->qf_pattern != NULL)
2006 {
2007 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2008 STRCAT(IObuff, ":");
2009 msg_puts(IObuff);
2010 }
2011 msg_puts((char_u *)" ");
2012
2013 /* Remove newlines and leading whitespace from the text. For an
2014 * unrecognized line keep the indent, the compiler may mark a word
2015 * with ^^^^. */
2016 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2018 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002019 msg_prt_line(IObuff, FALSE);
2020 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002022
2023 qfp = qfp->qf_next;
2024 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 ui_breakcheck();
2026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027}
2028
2029/*
2030 * Remove newlines and leading whitespace from an error message.
2031 * Put the result in "buf[bufsize]".
2032 */
2033 static void
2034qf_fmt_text(text, buf, bufsize)
2035 char_u *text;
2036 char_u *buf;
2037 int bufsize;
2038{
2039 int i;
2040 char_u *p = text;
2041
2042 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2043 {
2044 if (*p == '\n')
2045 {
2046 buf[i] = ' ';
2047 while (*++p != NUL)
2048 if (!vim_iswhite(*p) && *p != '\n')
2049 break;
2050 }
2051 else
2052 buf[i] = *p++;
2053 }
2054 buf[i] = NUL;
2055}
2056
2057/*
2058 * ":colder [count]": Up in the quickfix stack.
2059 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002060 * ":lolder [count]": Up in the location list stack.
2061 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 */
2063 void
2064qf_age(eap)
2065 exarg_T *eap;
2066{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002067 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 int count;
2069
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002070 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2071 {
2072 qi = GET_LOC_LIST(curwin);
2073 if (qi == NULL)
2074 {
2075 EMSG(_(e_loclist));
2076 return;
2077 }
2078 }
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 if (eap->addr_count != 0)
2081 count = eap->line2;
2082 else
2083 count = 1;
2084 while (count--)
2085 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002086 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
2090 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002091 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002093 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095 else
2096 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002097 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
2099 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002100 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002102 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
2104 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002105 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106}
2107
2108 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002109qf_msg(qi)
2110 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111{
2112 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002113 qi->qf_curlist + 1, qi->qf_listcount,
2114 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002116 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#endif
2118}
2119
2120/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002121 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 */
2123 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002124qf_free(qi, idx)
2125 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 int idx;
2127{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002128 qfline_T *qfp;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002129 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002131 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002133 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002134 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002135 {
2136 vim_free(qi->qf_lists[idx].qf_start->qf_text);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002137 stop = (qi->qf_lists[idx].qf_start == qfp);
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002138 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2139 vim_free(qi->qf_lists[idx].qf_start);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002140 if (stop)
2141 /* Somehow qf_count may have an incorrect value, set it to 1
2142 * to avoid crashing when it's wrong.
2143 * TODO: Avoid qf_count being incorrect. */
2144 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002145 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002146 qi->qf_lists[idx].qf_start = qfp;
2147 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002149 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002150 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151}
2152
2153/*
2154 * qf_mark_adjust: adjust marks
2155 */
2156 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157qf_mark_adjust(wp, line1, line2, amount, amount_after)
2158 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 linenr_T line1;
2160 linenr_T line2;
2161 long amount;
2162 long amount_after;
2163{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002164 int i;
2165 qfline_T *qfp;
2166 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002167 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002169 if (wp != NULL)
2170 {
2171 if (wp->w_llist == NULL)
2172 return;
2173 qi = wp->w_llist;
2174 }
2175
2176 for (idx = 0; idx < qi->qf_listcount; ++idx)
2177 if (qi->qf_lists[idx].qf_count)
2178 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2179 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (qfp->qf_fnum == curbuf->b_fnum)
2181 {
2182 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2183 {
2184 if (amount == MAXLNUM)
2185 qfp->qf_cleared = TRUE;
2186 else
2187 qfp->qf_lnum += amount;
2188 }
2189 else if (amount_after && qfp->qf_lnum > line2)
2190 qfp->qf_lnum += amount_after;
2191 }
2192}
2193
2194/*
2195 * Make a nice message out of the error character and the error number:
2196 * char number message
2197 * e or E 0 " error"
2198 * w or W 0 " warning"
2199 * i or I 0 " info"
2200 * 0 0 ""
2201 * other 0 " c"
2202 * e or E n " error n"
2203 * w or W n " warning n"
2204 * i or I n " info n"
2205 * 0 n " error n"
2206 * other n " c n"
2207 * 1 x "" :helpgrep
2208 */
2209 static char_u *
2210qf_types(c, nr)
2211 int c, nr;
2212{
2213 static char_u buf[20];
2214 static char_u cc[3];
2215 char_u *p;
2216
2217 if (c == 'W' || c == 'w')
2218 p = (char_u *)" warning";
2219 else if (c == 'I' || c == 'i')
2220 p = (char_u *)" info";
2221 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2222 p = (char_u *)" error";
2223 else if (c == 0 || c == 1)
2224 p = (char_u *)"";
2225 else
2226 {
2227 cc[0] = ' ';
2228 cc[1] = c;
2229 cc[2] = NUL;
2230 p = cc;
2231 }
2232
2233 if (nr <= 0)
2234 return p;
2235
2236 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2237 return buf;
2238}
2239
2240#if defined(FEAT_WINDOWS) || defined(PROTO)
2241/*
2242 * ":cwindow": open the quickfix window if we have errors to display,
2243 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002244 * ":lwindow": open the location list window if we have locations to display,
2245 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 */
2247 void
2248ex_cwindow(eap)
2249 exarg_T *eap;
2250{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002251 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 win_T *win;
2253
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002254 if (eap->cmdidx == CMD_lwindow)
2255 {
2256 qi = GET_LOC_LIST(curwin);
2257 if (qi == NULL)
2258 return;
2259 }
2260
2261 /* Look for an existing quickfix window. */
2262 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264 /*
2265 * If a quickfix window is open but we have no errors to display,
2266 * close the window. If a quickfix window is not open, then open
2267 * it if we have errors; otherwise, leave it closed.
2268 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002269 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002270 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002271 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 if (win != NULL)
2274 ex_cclose(eap);
2275 }
2276 else if (win == NULL)
2277 ex_copen(eap);
2278}
2279
2280/*
2281 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002282 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 void
2285ex_cclose(eap)
2286 exarg_T *eap;
2287{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002288 win_T *win = NULL;
2289 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002291 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2292 {
2293 qi = GET_LOC_LIST(curwin);
2294 if (qi == NULL)
2295 return;
2296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002298 /* Find existing quickfix window and close it. */
2299 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (win != NULL)
2301 win_close(win, FALSE);
2302}
2303
2304/*
2305 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002306 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 */
2308 void
2309ex_copen(eap)
2310 exarg_T *eap;
2311{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002312 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002315 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002316 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002317 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002319 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2320 {
2321 qi = GET_LOC_LIST(curwin);
2322 if (qi == NULL)
2323 {
2324 EMSG(_(e_loclist));
2325 return;
2326 }
2327 }
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 if (eap->addr_count != 0)
2330 height = eap->line2;
2331 else
2332 height = QF_WINHEIGHT;
2333
2334#ifdef FEAT_VISUAL
2335 reset_VIsual_and_resel(); /* stop Visual mode */
2336#endif
2337#ifdef FEAT_GUI
2338 need_mouse_correct = TRUE;
2339#endif
2340
2341 /*
2342 * Find existing quickfix window, or open a new one.
2343 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002344 win = qf_find_win(qi);
2345
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002346 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 win_goto(win);
2348 else
2349 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002350 qf_buf = qf_find_buf(qi);
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 /* The current window becomes the previous window afterwards. */
2353 win = curwin;
2354
Bram Moolenaar77642c02012-11-20 17:55:10 +01002355 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2356 && cmdmod.split == 0)
2357 /* Create the new window at the very bottom, except when
2358 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002359 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002360 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002362 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002364 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002366 /*
2367 * For the location list window, create a reference to the
2368 * location list from the window 'win'.
2369 */
2370 curwin->w_llist_ref = win->w_llist;
2371 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002373
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002374 if (oldwin != curwin)
2375 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002376 if (qf_buf != NULL)
2377 /* Use the existing quickfix buffer */
2378 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002379 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002380 else
2381 {
2382 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002383 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002384 /* switch off 'swapfile' */
2385 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2386 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002387 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002388 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002389 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002390#ifdef FEAT_DIFF
2391 curwin->w_p_diff = FALSE;
2392#endif
2393#ifdef FEAT_FOLDING
2394 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2395 OPT_LOCAL);
2396#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002399 /* Only set the height when still in the same tab page and there is no
2400 * window to the side. */
2401 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002402#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002403 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002404#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002405 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 win_setheight(height);
2407 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2408 if (win_valid(win))
2409 prevwin = win;
2410 }
2411
2412 /*
2413 * Fill the buffer with the quickfix list.
2414 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002415 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002417 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002418 qf_set_title(qi);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002419
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002420 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 curwin->w_cursor.col = 0;
2422 check_cursor();
2423 update_topline(); /* scroll to show the line */
2424}
2425
2426/*
2427 * Return the number of the current entry (line number in the quickfix
2428 * window).
2429 */
2430 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002431qf_current_entry(wp)
2432 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002434 qf_info_T *qi = &ql_info;
2435
2436 if (IS_LL_WINDOW(wp))
2437 /* In the location list window, use the referenced location list */
2438 qi = wp->w_llist_ref;
2439
2440 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441}
2442
2443/*
2444 * Update the cursor position in the quickfix window to the current error.
2445 * Return TRUE if there is a quickfix window.
2446 */
2447 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002448qf_win_pos_update(qi, old_qf_index)
2449 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 int old_qf_index; /* previous qf_index or zero */
2451{
2452 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002453 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455 /*
2456 * Put the cursor on the current error in the quickfix window, so that
2457 * it's viewable.
2458 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002459 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 if (win != NULL
2461 && qf_index <= win->w_buffer->b_ml.ml_line_count
2462 && old_qf_index != qf_index)
2463 {
2464 win_T *old_curwin = curwin;
2465
2466 curwin = win;
2467 curbuf = win->w_buffer;
2468 if (qf_index > old_qf_index)
2469 {
2470 curwin->w_redraw_top = old_qf_index;
2471 curwin->w_redraw_bot = qf_index;
2472 }
2473 else
2474 {
2475 curwin->w_redraw_top = qf_index;
2476 curwin->w_redraw_bot = old_qf_index;
2477 }
2478 curwin->w_cursor.lnum = qf_index;
2479 curwin->w_cursor.col = 0;
2480 update_topline(); /* scroll to show the line */
2481 redraw_later(VALID);
2482 curwin->w_redr_status = TRUE; /* update ruler */
2483 curwin = old_curwin;
2484 curbuf = curwin->w_buffer;
2485 }
2486 return win != NULL;
2487}
2488
2489/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002490 * Check whether the given window is displaying the specified quickfix/location
2491 * list buffer
2492 */
2493 static int
2494is_qf_win(win, qi)
2495 win_T *win;
2496 qf_info_T *qi;
2497{
2498 /*
2499 * A window displaying the quickfix buffer will have the w_llist_ref field
2500 * set to NULL.
2501 * A window displaying a location list buffer will have the w_llist_ref
2502 * pointing to the location list.
2503 */
2504 if (bt_quickfix(win->w_buffer))
2505 if ((qi == &ql_info && win->w_llist_ref == NULL)
2506 || (qi != &ql_info && win->w_llist_ref == qi))
2507 return TRUE;
2508
2509 return FALSE;
2510}
2511
2512/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002513 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002514 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002515 */
2516 static win_T *
2517qf_find_win(qi)
2518 qf_info_T *qi;
2519{
2520 win_T *win;
2521
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002522 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002523 if (is_qf_win(win, qi))
2524 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002525
2526 return win;
2527}
2528
2529/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002530 * Find a quickfix buffer.
2531 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 */
2533 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002534qf_find_buf(qi)
2535 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002537 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002538 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaar9c102382006-05-03 21:26:49 +00002540 FOR_ALL_TAB_WINDOWS(tp, win)
2541 if (is_qf_win(win, qi))
2542 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002543
Bram Moolenaar9c102382006-05-03 21:26:49 +00002544 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545}
2546
2547/*
2548 * Find the quickfix buffer. If it exists, update the contents.
2549 */
2550 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002551qf_update_buffer(qi)
2552 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
2554 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002555 win_T *win;
2556 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002560 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (buf != NULL)
2562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 /* set curwin/curbuf to buf and save a few things */
2564 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002566 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002568 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL
2569 && (win = qf_find_win(qi)) != NULL)
2570 {
2571 curwin_save = curwin;
2572 curwin = win;
2573 qf_set_title(qi);
2574 curwin = curwin_save;
2575
2576 }
2577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 /* restore curwin/curbuf and a few other things */
2579 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002581 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583}
2584
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002585 static void
2586qf_set_title(qi)
2587 qf_info_T *qi;
2588{
2589 set_internal_string_var((char_u *)"w:quickfix_title",
2590 qi->qf_lists[qi->qf_curlist].qf_title);
2591}
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593/*
2594 * Fill current buffer with quickfix errors, replacing any previous contents.
2595 * curbuf must be the quickfix buffer!
2596 */
2597 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002598qf_fill_buffer(qi)
2599 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002601 linenr_T lnum;
2602 qfline_T *qfp;
2603 buf_T *errbuf;
2604 int len;
2605 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
2607 /* delete all existing lines */
2608 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2609 (void)ml_delete((linenr_T)1, FALSE);
2610
2611 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002612 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
2614 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002615 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2616 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 if (qfp->qf_fnum != 0
2619 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2620 && errbuf->b_fname != NULL)
2621 {
2622 if (qfp->qf_type == 1) /* :helpgrep */
2623 STRCPY(IObuff, gettail(errbuf->b_fname));
2624 else
2625 STRCPY(IObuff, errbuf->b_fname);
2626 len = (int)STRLEN(IObuff);
2627 }
2628 else
2629 len = 0;
2630 IObuff[len++] = '|';
2631
2632 if (qfp->qf_lnum > 0)
2633 {
2634 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2635 len += (int)STRLEN(IObuff + len);
2636
2637 if (qfp->qf_col > 0)
2638 {
2639 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2640 len += (int)STRLEN(IObuff + len);
2641 }
2642
2643 sprintf((char *)IObuff + len, "%s",
2644 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2645 len += (int)STRLEN(IObuff + len);
2646 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002647 else if (qfp->qf_pattern != NULL)
2648 {
2649 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2650 len += (int)STRLEN(IObuff + len);
2651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 IObuff[len++] = '|';
2653 IObuff[len++] = ' ';
2654
2655 /* Remove newlines and leading whitespace from the text.
2656 * For an unrecognized line keep the indent, the compiler may
2657 * mark a word with ^^^^. */
2658 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2659 IObuff + len, IOSIZE - len);
2660
2661 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2662 == FAIL)
2663 break;
2664 qfp = qfp->qf_next;
2665 }
2666 /* Delete the empty line which is now at the end */
2667 (void)ml_delete(lnum + 1, FALSE);
2668 }
2669
2670 /* correct cursor position */
2671 check_lnums(TRUE);
2672
2673 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2674 * resembles reading a file into a buffer, it's more logical when using
2675 * autocommands. */
2676 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2677 curbuf->b_p_ma = FALSE;
2678
2679#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002680 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2682 FALSE, curbuf);
2683 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2684 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002685 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686#endif
2687
2688 /* make sure it will be redrawn */
2689 redraw_curbuf_later(NOT_VALID);
2690
2691 /* Restore KeyTyped, setting 'filetype' may reset it. */
2692 KeyTyped = old_KeyTyped;
2693}
2694
2695#endif /* FEAT_WINDOWS */
2696
2697/*
2698 * Return TRUE if "buf" is the quickfix buffer.
2699 */
2700 int
2701bt_quickfix(buf)
2702 buf_T *buf;
2703{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002704 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705}
2706
2707/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002708 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2709 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 */
2711 int
2712bt_nofile(buf)
2713 buf_T *buf;
2714{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002715 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2716 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717}
2718
2719/*
2720 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2721 */
2722 int
2723bt_dontwrite(buf)
2724 buf_T *buf;
2725{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002726 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727}
2728
2729 int
2730bt_dontwrite_msg(buf)
2731 buf_T *buf;
2732{
2733 if (bt_dontwrite(buf))
2734 {
2735 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2736 return TRUE;
2737 }
2738 return FALSE;
2739}
2740
2741/*
2742 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2743 * and 'bufhidden'.
2744 */
2745 int
2746buf_hide(buf)
2747 buf_T *buf;
2748{
2749 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2750 switch (buf->b_p_bh[0])
2751 {
2752 case 'u': /* "unload" */
2753 case 'w': /* "wipe" */
2754 case 'd': return FALSE; /* "delete" */
2755 case 'h': return TRUE; /* "hide" */
2756 }
2757 return (p_hid || cmdmod.hide);
2758}
2759
2760/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002761 * Return TRUE when using ":vimgrep" for ":grep".
2762 */
2763 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002764grep_internal(cmdidx)
2765 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002766{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002767 return ((cmdidx == CMD_grep
2768 || cmdidx == CMD_lgrep
2769 || cmdidx == CMD_grepadd
2770 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002771 && STRCMP("internal",
2772 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2773}
2774
2775/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002776 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 */
2778 void
2779ex_make(eap)
2780 exarg_T *eap;
2781{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002782 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 char_u *cmd;
2784 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002785 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002786 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002787 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002788#ifdef FEAT_AUTOCMD
2789 char_u *au_name = NULL;
2790
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002791 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2792 if (grep_internal(eap->cmdidx))
2793 {
2794 ex_vimgrep(eap);
2795 return;
2796 }
2797
Bram Moolenaar7c626922005-02-07 22:01:03 +00002798 switch (eap->cmdidx)
2799 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002800 case CMD_make: au_name = (char_u *)"make"; break;
2801 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2802 case CMD_grep: au_name = (char_u *)"grep"; break;
2803 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2804 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2805 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002806 default: break;
2807 }
2808 if (au_name != NULL)
2809 {
2810 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2811 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002812# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002813 if (did_throw || force_abort)
2814 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002815# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002816 }
2817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
Bram Moolenaara6557602006-02-04 22:43:20 +00002819 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2820 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002821 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002822
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002824 fname = get_mef_name();
2825 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002827 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
2829 /*
2830 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2831 */
2832 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2833 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002834 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 cmd = alloc(len);
2836 if (cmd == NULL)
2837 return;
2838 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2839 (char *)p_shq);
2840 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002841 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 /*
2843 * Output a newline if there's something else than the :make command that
2844 * was typed (in which case the cursor is in column 0).
2845 */
2846 if (msg_col == 0)
2847 msg_didout = FALSE;
2848 msg_start();
2849 MSG_PUTS(":!");
2850 msg_outtrans(cmd); /* show what we are doing */
2851
2852 /* let the shell know if we are redirecting output or not */
2853 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2854
2855#ifdef AMIGA
2856 out_flush();
2857 /* read window status report and redraw before message */
2858 (void)char_avail();
2859#endif
2860
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002861 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002862 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2863 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002864 && eap->cmdidx != CMD_lgrepadd),
2865 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002866 if (wp != NULL)
2867 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002868#ifdef FEAT_AUTOCMD
2869 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002870 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002871 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2872 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002873 if (qi->qf_curlist < qi->qf_listcount)
2874 res = qi->qf_lists[qi->qf_curlist].qf_count;
2875 else
2876 res = 0;
2877 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002878#endif
2879 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002880 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
Bram Moolenaar7c626922005-02-07 22:01:03 +00002882 mch_remove(fname);
2883 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 vim_free(cmd);
2885}
2886
2887/*
2888 * Return the name for the errorfile, in allocated memory.
2889 * Find a new unique name when 'makeef' contains "##".
2890 * Returns NULL for error.
2891 */
2892 static char_u *
2893get_mef_name()
2894{
2895 char_u *p;
2896 char_u *name;
2897 static int start = -1;
2898 static int off = 0;
2899#ifdef HAVE_LSTAT
2900 struct stat sb;
2901#endif
2902
2903 if (*p_mef == NUL)
2904 {
2905 name = vim_tempname('e');
2906 if (name == NULL)
2907 EMSG(_(e_notmp));
2908 return name;
2909 }
2910
2911 for (p = p_mef; *p; ++p)
2912 if (p[0] == '#' && p[1] == '#')
2913 break;
2914
2915 if (*p == NUL)
2916 return vim_strsave(p_mef);
2917
2918 /* Keep trying until the name doesn't exist yet. */
2919 for (;;)
2920 {
2921 if (start == -1)
2922 start = mch_get_pid();
2923 else
2924 off += 19;
2925
2926 name = alloc((unsigned)STRLEN(p_mef) + 30);
2927 if (name == NULL)
2928 break;
2929 STRCPY(name, p_mef);
2930 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2931 STRCAT(name, p + 2);
2932 if (mch_getperm(name) < 0
2933#ifdef HAVE_LSTAT
2934 /* Don't accept a symbolic link, its a security risk. */
2935 && mch_lstat((char *)name, &sb) < 0
2936#endif
2937 )
2938 break;
2939 vim_free(name);
2940 }
2941 return name;
2942}
2943
2944/*
2945 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002946 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 */
2948 void
2949ex_cc(eap)
2950 exarg_T *eap;
2951{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002952 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002953
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002954 if (eap->cmdidx == CMD_ll
2955 || eap->cmdidx == CMD_lrewind
2956 || eap->cmdidx == CMD_lfirst
2957 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002958 {
2959 qi = GET_LOC_LIST(curwin);
2960 if (qi == NULL)
2961 {
2962 EMSG(_(e_loclist));
2963 return;
2964 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002965 }
2966
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002967 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 eap->addr_count > 0
2969 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002970 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002972 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2973 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 ? 1
2975 : 32767,
2976 eap->forceit);
2977}
2978
2979/*
2980 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002981 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 */
2983 void
2984ex_cnext(eap)
2985 exarg_T *eap;
2986{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002987 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002988
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002989 if (eap->cmdidx == CMD_lnext
2990 || eap->cmdidx == CMD_lNext
2991 || eap->cmdidx == CMD_lprevious
2992 || eap->cmdidx == CMD_lnfile
2993 || eap->cmdidx == CMD_lNfile
2994 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002995 {
2996 qi = GET_LOC_LIST(curwin);
2997 if (qi == NULL)
2998 {
2999 EMSG(_(e_loclist));
3000 return;
3001 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003002 }
3003
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003004 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003006 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003008 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3009 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 ? BACKWARD_FILE
3011 : BACKWARD,
3012 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
3013}
3014
3015/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003016 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003017 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 */
3019 void
3020ex_cfile(eap)
3021 exarg_T *eap;
3022{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003023 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003024 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003025#ifdef FEAT_AUTOCMD
3026 char_u *au_name = NULL;
3027#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003028
3029 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003030 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003031 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003032
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003033#ifdef FEAT_AUTOCMD
3034 switch (eap->cmdidx)
3035 {
3036 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3037 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3038 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3039 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3040 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3041 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3042 default: break;
3043 }
3044 if (au_name != NULL)
3045 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3046#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003047#ifdef FEAT_BROWSE
3048 if (cmdmod.browse)
3049 {
3050 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3051 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3052 if (browse_file == NULL)
3053 return;
3054 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3055 vim_free(browse_file);
3056 }
3057 else
3058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003060 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003061
3062 /*
3063 * This function is used by the :cfile, :cgetfile and :caddfile
3064 * commands.
3065 * :cfile always creates a new quickfix list and jumps to the
3066 * first error.
3067 * :cgetfile creates a new quickfix list but doesn't jump to the
3068 * first error.
3069 * :caddfile adds to an existing quickfix list. If there is no
3070 * quickfix list then a new list is created.
3071 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003072 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003073 && eap->cmdidx != CMD_laddfile),
3074 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003075 && (eap->cmdidx == CMD_cfile
3076 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003077 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003078#ifdef FEAT_AUTOCMD
3079 if (au_name != NULL)
3080 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3081#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003082 if (wp != NULL)
3083 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003084 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003085 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003086
3087 else
3088 {
3089#ifdef FEAT_AUTOCMD
3090 if (au_name != NULL)
3091 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3092#endif
3093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094}
3095
3096/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003097 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003098 * ":vimgrepadd {pattern} file(s)"
3099 * ":lvimgrep {pattern} file(s)"
3100 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003101 */
3102 void
3103ex_vimgrep(eap)
3104 exarg_T *eap;
3105{
Bram Moolenaar81695252004-12-29 20:58:21 +00003106 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003107 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003108 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003109 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003110 char_u *s;
3111 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003112 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003113 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003114#ifdef FEAT_AUTOCMD
3115 qfline_T *cur_qf_start;
3116#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003117 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003118 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003119 buf_T *buf;
3120 int duplicate_name = FALSE;
3121 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003122 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003123 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003124 buf_T *first_match_buf = NULL;
3125 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003126 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003127#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3128 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003129#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003130 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003131 int flags = 0;
3132 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003133 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003134 char_u *dirname_start = NULL;
3135 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003136 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003137#ifdef FEAT_AUTOCMD
3138 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003139
3140 switch (eap->cmdidx)
3141 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003142 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3143 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3144 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003145 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003146 case CMD_grep: au_name = (char_u *)"grep"; break;
3147 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3148 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3149 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003150 default: break;
3151 }
3152 if (au_name != NULL)
3153 {
3154 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3155 curbuf->b_fname, TRUE, curbuf);
3156 if (did_throw || force_abort)
3157 return;
3158 }
3159#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003160
Bram Moolenaar754b5602006-02-09 23:53:20 +00003161 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003162 || eap->cmdidx == CMD_lvimgrep
3163 || eap->cmdidx == CMD_lgrepadd
3164 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003165 {
3166 qi = ll_get_or_alloc_list(curwin);
3167 if (qi == NULL)
3168 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003169 }
3170
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003171 if (eap->addr_count > 0)
3172 tomatch = eap->line2;
3173 else
3174 tomatch = MAXLNUM;
3175
Bram Moolenaar81695252004-12-29 20:58:21 +00003176 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003177 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003178 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003179 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003180 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003181 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003182 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003183 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003184
3185 if (s != NULL && *s == NUL)
3186 {
3187 /* Pattern is empty, use last search pattern. */
3188 if (last_search_pat() == NULL)
3189 {
3190 EMSG(_(e_noprevre));
3191 goto theend;
3192 }
3193 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3194 }
3195 else
3196 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3197
Bram Moolenaar86b68352004-12-27 21:59:20 +00003198 if (regmatch.regprog == NULL)
3199 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003200 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003201 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003202
3203 p = skipwhite(p);
3204 if (*p == NUL)
3205 {
3206 EMSG(_("E683: File name missing or invalid pattern"));
3207 goto theend;
3208 }
3209
Bram Moolenaar754b5602006-02-09 23:53:20 +00003210 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003211 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003212 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003213 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01003214 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003215 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003216 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003217 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003218 prevp->qf_next != prevp; prevp = prevp->qf_next)
3219 ;
3220
3221 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003222 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003223 goto theend;
3224 if (fcount == 0)
3225 {
3226 EMSG(_(e_nomatch));
3227 goto theend;
3228 }
3229
Bram Moolenaard9462e32011-04-11 21:35:11 +02003230 dirname_start = alloc(MAXPATHL);
3231 dirname_now = alloc(MAXPATHL);
3232 if (dirname_start == NULL || dirname_now == NULL)
3233 goto theend;
3234
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003235 /* Remember the current directory, because a BufRead autocommand that does
3236 * ":lcd %:p:h" changes the meaning of short path names. */
3237 mch_dirname(dirname_start, MAXPATHL);
3238
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003239#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003240 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003241 * changing the current quickfix list. */
3242 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3243#endif
3244
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003245 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003246 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003247 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003248 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003249 if (time(NULL) > seconds)
3250 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003251 /* Display the file name every second or so, show the user we are
3252 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003253 seconds = time(NULL);
3254 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003255 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003256 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003257 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003258 else
3259 {
3260 msg_outtrans(p);
3261 vim_free(p);
3262 }
3263 msg_clr_eos();
3264 msg_didout = FALSE; /* overwrite this message */
3265 msg_nowait = TRUE; /* don't wait for this message */
3266 msg_col = 0;
3267 out_flush();
3268 }
3269
Bram Moolenaar81695252004-12-29 20:58:21 +00003270 buf = buflist_findname_exp(fnames[fi]);
3271 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3272 {
3273 /* Remember that a buffer with this name already exists. */
3274 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003275 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003276 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003277
3278#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3279 /* Don't do Filetype autocommands to avoid loading syntax and
3280 * indent scripts, a great speed improvement. */
3281 save_ei = au_event_disable(",Filetype");
3282#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003283 /* Don't use modelines here, it's useless. */
3284 save_mls = p_mls;
3285 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003286
3287 /* Load file into a buffer, so that 'fileencoding' is detected,
3288 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003289 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003290
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003291 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003292#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3293 au_event_restore(save_ei);
3294#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003295 }
3296 else
3297 /* Use existing, loaded buffer. */
3298 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003299
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003300#ifdef FEAT_AUTOCMD
3301 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3302 {
3303 int idx;
3304
3305 /* Autocommands changed the quickfix list. Find the one we were
3306 * using and restore it. */
3307 for (idx = 0; idx < LISTCOUNT; ++idx)
3308 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3309 {
3310 qi->qf_curlist = idx;
3311 break;
3312 }
3313 if (idx == LISTCOUNT)
3314 {
3315 /* List cannot be found, create a new one. */
3316 qf_new_list(qi, *eap->cmdlinep);
3317 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3318 }
3319 }
3320#endif
3321
Bram Moolenaar81695252004-12-29 20:58:21 +00003322 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003323 {
3324 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003325 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003326 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003327 else
3328 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003329 /* Try for a match in all lines of the buffer.
3330 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003331 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003332 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3333 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003334 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003335 col = 0;
3336 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003337 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003338 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003339 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003340 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003341 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003342 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003343 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003344 ml_get_buf(buf,
3345 regmatch.startpos[0].lnum + lnum, FALSE),
3346 regmatch.startpos[0].lnum + lnum,
3347 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003348 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003349 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003350 0, /* nr */
3351 0, /* type */
3352 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003353 ) == FAIL)
3354 {
3355 got_int = TRUE;
3356 break;
3357 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003358 found_match = TRUE;
3359 if (--tomatch == 0)
3360 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003361 if ((flags & VGR_GLOBAL) == 0
3362 || regmatch.endpos[0].lnum > 0)
3363 break;
3364 col = regmatch.endpos[0].col
3365 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003366 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003367 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003368 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003369 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003370 if (got_int)
3371 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003372 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003373#ifdef FEAT_AUTOCMD
3374 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3375#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003376
3377 if (using_dummy)
3378 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003379 if (found_match && first_match_buf == NULL)
3380 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003381 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003382 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003383 /* Never keep a dummy buffer if there is another buffer
3384 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003385 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003386 buf = NULL;
3387 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003388 else if (!cmdmod.hide
3389 || buf->b_p_bh[0] == 'u' /* "unload" */
3390 || buf->b_p_bh[0] == 'w' /* "wipe" */
3391 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003392 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003393 /* When no match was found we don't need to remember the
3394 * buffer, wipe it out. If there was a match and it
3395 * wasn't the first one or we won't jump there: only
3396 * unload the buffer.
3397 * Ignore 'hidden' here, because it may lead to having too
3398 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003399 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003400 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003401 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003402 buf = NULL;
3403 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003404 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003405 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003406 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003407 buf = NULL;
3408 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003409 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003410
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003411 if (buf != NULL)
3412 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003413 /* If the buffer is still loaded we need to use the
3414 * directory we jumped to below. */
3415 if (buf == first_match_buf
3416 && target_dir == NULL
3417 && STRCMP(dirname_start, dirname_now) != 0)
3418 target_dir = vim_strsave(dirname_now);
3419
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003420 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003421 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003422 * need to be done (again). But not the window-local
3423 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003424 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003425#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003426 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3427 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003428#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003429 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003430 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003431 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003432 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003433 }
3434 }
3435
3436 FreeWild(fcount, fnames);
3437
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003438 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3439 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3440 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003441
3442#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003443 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003444#endif
3445
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003446#ifdef FEAT_AUTOCMD
3447 if (au_name != NULL)
3448 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3449 curbuf->b_fname, TRUE, curbuf);
3450#endif
3451
Bram Moolenaar86b68352004-12-27 21:59:20 +00003452 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003453 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003454 {
3455 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003456 {
3457 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003458 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003459 if (buf != curbuf)
3460 /* If we jumped to another buffer redrawing will already be
3461 * taken care of. */
3462 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003463
3464 /* Jump to the directory used after loading the buffer. */
3465 if (curbuf == first_match_buf && target_dir != NULL)
3466 {
3467 exarg_T ea;
3468
3469 ea.arg = target_dir;
3470 ea.cmdidx = CMD_lcd;
3471 ex_cd(&ea);
3472 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003473 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003474 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003475 else
3476 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003477
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003478 /* If we loaded a dummy buffer into the current window, the autocommands
3479 * may have messed up things, need to redraw and recompute folds. */
3480 if (redraw_for_dummy)
3481 {
3482#ifdef FEAT_FOLDING
3483 foldUpdateAll(curwin);
3484#else
3485 redraw_later(NOT_VALID);
3486#endif
3487 }
3488
Bram Moolenaar86b68352004-12-27 21:59:20 +00003489theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003490 vim_free(dirname_now);
3491 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003492 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003493 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003494}
3495
3496/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003497 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003498 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003499 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3500 * If "s" is not NULL terminate the pattern with a NUL.
3501 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003502 */
3503 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003504skip_vimgrep_pat(p, s, flags)
3505 char_u *p;
3506 char_u **s;
3507 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003508{
3509 int c;
3510
3511 if (vim_isIDc(*p))
3512 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003513 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003514 if (s != NULL)
3515 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003516 p = skiptowhite(p);
3517 if (s != NULL && *p != NUL)
3518 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003519 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003520 else
3521 {
3522 /* ":vimgrep /pattern/[g][j] fname" */
3523 if (s != NULL)
3524 *s = p + 1;
3525 c = *p;
3526 p = skip_regexp(p + 1, c, TRUE, NULL);
3527 if (*p != c)
3528 return NULL;
3529
3530 /* Truncate the pattern. */
3531 if (s != NULL)
3532 *p = NUL;
3533 ++p;
3534
3535 /* Find the flags */
3536 while (*p == 'g' || *p == 'j')
3537 {
3538 if (flags != NULL)
3539 {
3540 if (*p == 'g')
3541 *flags |= VGR_GLOBAL;
3542 else
3543 *flags |= VGR_NOJUMP;
3544 }
3545 ++p;
3546 }
3547 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003548 return p;
3549}
3550
3551/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003552 * Restore current working directory to "dirname_start" if they differ, taking
3553 * into account whether it is set locally or globally.
3554 */
3555 static void
3556restore_start_dir(dirname_start)
3557 char_u *dirname_start;
3558{
3559 char_u *dirname_now = alloc(MAXPATHL);
3560
3561 if (NULL != dirname_now)
3562 {
3563 mch_dirname(dirname_now, MAXPATHL);
3564 if (STRCMP(dirname_start, dirname_now) != 0)
3565 {
3566 /* If the directory has changed, change it back by building up an
3567 * appropriate ex command and executing it. */
3568 exarg_T ea;
3569
3570 ea.arg = dirname_start;
3571 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3572 ex_cd(&ea);
3573 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01003574 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003575 }
3576}
3577
3578/*
3579 * Load file "fname" into a dummy buffer and return the buffer pointer,
3580 * placing the directory resulting from the buffer load into the
3581 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3582 * prior to calling this function. Restores directory to "dirname_start" prior
3583 * to returning, if autocmds or the 'autochdir' option have changed it.
3584 *
3585 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3586 * or wipe_dummy_buffer() later!
3587 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003588 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003589 */
3590 static buf_T *
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003591load_dummy_buffer(fname, dirname_start, resulting_dir)
Bram Moolenaar81695252004-12-29 20:58:21 +00003592 char_u *fname;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003593 char_u *dirname_start; /* in: old directory */
3594 char_u *resulting_dir; /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003595{
3596 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003597 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003598 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003599 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003600
3601 /* Allocate a buffer without putting it in the buffer list. */
3602 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3603 if (newbuf == NULL)
3604 return NULL;
3605
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003606 /* Init the options. */
3607 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3608
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003609 /* need to open the memfile before putting the buffer in a window */
3610 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003611 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003612 /* set curwin/curbuf to buf and save a few things */
3613 aucmd_prepbuf(&aco, newbuf);
3614
3615 /* Need to set the filename for autocommands. */
3616 (void)setfname(curbuf, fname, NULL, FALSE);
3617
Bram Moolenaar81695252004-12-29 20:58:21 +00003618 /* Create swap file now to avoid the ATTENTION message. */
3619 check_need_swap(TRUE);
3620
3621 /* Remove the "dummy" flag, otherwise autocommands may not
3622 * work. */
3623 curbuf->b_flags &= ~BF_DUMMY;
3624
3625 if (readfile(fname, NULL,
3626 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3627 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003628 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003629 && !(curbuf->b_flags & BF_NEW))
3630 {
3631 failed = FALSE;
3632 if (curbuf != newbuf)
3633 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003634 /* Bloody autocommands changed the buffer! Can happen when
3635 * using netrw and editing a remote file. Use the current
3636 * buffer instead, delete the dummy one after restoring the
3637 * window stuff. */
3638 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003639 newbuf = curbuf;
3640 }
3641 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003642
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003643 /* restore curwin/curbuf and a few other things */
3644 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003645 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3646 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003647 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003648
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003649 /*
3650 * When autocommands/'autochdir' option changed directory: go back.
3651 * Let the caller know what the resulting dir was first, in case it is
3652 * important.
3653 */
3654 mch_dirname(resulting_dir, MAXPATHL);
3655 restore_start_dir(dirname_start);
3656
Bram Moolenaar81695252004-12-29 20:58:21 +00003657 if (!buf_valid(newbuf))
3658 return NULL;
3659 if (failed)
3660 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003661 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003662 return NULL;
3663 }
3664 return newbuf;
3665}
3666
3667/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003668 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3669 * directory to "dirname_start" prior to returning, if autocmds or the
3670 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003671 */
3672 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003673wipe_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003674 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003675 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003676{
3677 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003678 {
3679#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3680 cleanup_T cs;
3681
3682 /* Reset the error/interrupt/exception state here so that aborting()
3683 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3684 * work when got_int is set. */
3685 enter_cleanup(&cs);
3686#endif
3687
Bram Moolenaar81695252004-12-29 20:58:21 +00003688 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003689
3690#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3691 /* Restore the error/interrupt/exception state if not discarded by a
3692 * new aborting error, interrupt, or uncaught exception. */
3693 leave_cleanup(&cs);
3694#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003695 /* When autocommands/'autochdir' option changed directory: go back. */
3696 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003697 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003698}
3699
3700/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003701 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3702 * directory to "dirname_start" prior to returning, if autocmds or the
3703 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003704 */
3705 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003706unload_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003707 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003708 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003709{
3710 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003711 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003712 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003713
3714 /* When autocommands/'autochdir' option changed directory: go back. */
3715 restore_start_dir(dirname_start);
3716 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003717}
3718
Bram Moolenaar05159a02005-02-26 23:04:13 +00003719#if defined(FEAT_EVAL) || defined(PROTO)
3720/*
3721 * Add each quickfix error to list "list" as a dictionary.
3722 */
3723 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003724get_errorlist(wp, list)
3725 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003726 list_T *list;
3727{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003728 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003729 dict_T *dict;
3730 char_u buf[2];
3731 qfline_T *qfp;
3732 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003733 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003734
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003735 if (wp != NULL)
3736 {
3737 qi = GET_LOC_LIST(wp);
3738 if (qi == NULL)
3739 return FAIL;
3740 }
3741
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003742 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003743 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003744 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003745
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003746 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3747 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003748 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003749 /* Handle entries with a non-existing buffer number. */
3750 bufnum = qfp->qf_fnum;
3751 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3752 bufnum = 0;
3753
Bram Moolenaar05159a02005-02-26 23:04:13 +00003754 if ((dict = dict_alloc()) == NULL)
3755 return FAIL;
3756 if (list_append_dict(list, dict) == FAIL)
3757 return FAIL;
3758
3759 buf[0] = qfp->qf_type;
3760 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003761 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003762 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3763 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3764 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3765 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003766 || dict_add_nr_str(dict, "pattern", 0L,
3767 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3768 || dict_add_nr_str(dict, "text", 0L,
3769 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003770 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3771 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3772 return FAIL;
3773
3774 qfp = qfp->qf_next;
3775 }
3776 return OK;
3777}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003778
3779/*
3780 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003781 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003782 */
3783 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003784set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003785 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003786 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003787 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003788 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003789{
3790 listitem_T *li;
3791 dict_T *d;
3792 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003793 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003794 long lnum;
3795 int col, nr;
3796 int vcol;
3797 qfline_T *prevp = NULL;
3798 int valid, status;
3799 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003800 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003801 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003802
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003803 if (wp != NULL)
3804 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003805 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003806 if (qi == NULL)
3807 return FAIL;
3808 }
3809
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003810 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003811 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01003812 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003813 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003814 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003815 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003816 prevp->qf_next != prevp; prevp = prevp->qf_next)
3817 ;
3818 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003819 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003820
3821 for (li = list->lv_first; li != NULL; li = li->li_next)
3822 {
3823 if (li->li_tv.v_type != VAR_DICT)
3824 continue; /* Skip non-dict items */
3825
3826 d = li->li_tv.vval.v_dict;
3827 if (d == NULL)
3828 continue;
3829
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003830 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003831 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003832 lnum = get_dict_number(d, (char_u *)"lnum");
3833 col = get_dict_number(d, (char_u *)"col");
3834 vcol = get_dict_number(d, (char_u *)"vcol");
3835 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003836 type = get_dict_string(d, (char_u *)"type", TRUE);
3837 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3838 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003839 if (text == NULL)
3840 text = vim_strsave((char_u *)"");
3841
3842 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003843 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003844 valid = FALSE;
3845
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003846 /* Mark entries with non-existing buffer number as not valid. Give the
3847 * error message only once. */
3848 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3849 {
3850 if (!did_bufnr_emsg)
3851 {
3852 did_bufnr_emsg = TRUE;
3853 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3854 }
3855 valid = FALSE;
3856 bufnum = 0;
3857 }
3858
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003859 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003860 NULL, /* dir */
3861 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003862 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003863 text,
3864 lnum,
3865 col,
3866 vcol, /* vis_col */
3867 pattern, /* search pattern */
3868 nr,
3869 type == NULL ? NUL : *type,
3870 valid);
3871
3872 vim_free(filename);
3873 vim_free(pattern);
3874 vim_free(text);
3875 vim_free(type);
3876
3877 if (status == FAIL)
3878 {
3879 retval = FAIL;
3880 break;
3881 }
3882 }
3883
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003884 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003885 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003886 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3887 else
3888 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003889 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3890 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003891
3892#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003893 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003894#endif
3895
3896 return retval;
3897}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003898#endif
3899
Bram Moolenaar81695252004-12-29 20:58:21 +00003900/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003901 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003902 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003903 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003904 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003905 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003906 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003907 */
3908 void
3909ex_cbuffer(eap)
3910 exarg_T *eap;
3911{
3912 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003913 qf_info_T *qi = &ql_info;
3914
Bram Moolenaardb552d602006-03-23 22:59:57 +00003915 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3916 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003917 {
3918 qi = ll_get_or_alloc_list(curwin);
3919 if (qi == NULL)
3920 return;
3921 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003922
3923 if (*eap->arg == NUL)
3924 buf = curbuf;
3925 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3926 buf = buflist_findnr(atoi((char *)eap->arg));
3927 if (buf == NULL)
3928 EMSG(_(e_invarg));
3929 else if (buf->b_ml.ml_mfp == NULL)
3930 EMSG(_("E681: Buffer is not loaded"));
3931 else
3932 {
3933 if (eap->addr_count == 0)
3934 {
3935 eap->line1 = 1;
3936 eap->line2 = buf->b_ml.ml_line_count;
3937 }
3938 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3939 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3940 EMSG(_(e_invrange));
3941 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003942 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003943 char_u *qf_title = *eap->cmdlinep;
3944
3945 if (buf->b_sfname)
3946 {
3947 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3948 (char *)qf_title, (char *)buf->b_sfname);
3949 qf_title = IObuff;
3950 }
3951
Bram Moolenaardb552d602006-03-23 22:59:57 +00003952 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3953 (eap->cmdidx != CMD_caddbuffer
3954 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003955 eap->line1, eap->line2,
3956 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003957 && (eap->cmdidx == CMD_cbuffer
3958 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003959 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3960 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003961 }
3962}
3963
Bram Moolenaar1e015462005-09-25 22:16:38 +00003964#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003965/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003966 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3967 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003968 */
3969 void
3970ex_cexpr(eap)
3971 exarg_T *eap;
3972{
3973 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003974 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003975
Bram Moolenaardb552d602006-03-23 22:59:57 +00003976 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3977 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003978 {
3979 qi = ll_get_or_alloc_list(curwin);
3980 if (qi == NULL)
3981 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003982 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003983
Bram Moolenaar4770d092006-01-12 23:22:24 +00003984 /* Evaluate the expression. When the result is a string or a list we can
3985 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003986 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003987 if (tv != NULL)
3988 {
3989 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3990 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3991 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003992 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3993 (eap->cmdidx != CMD_caddexpr
3994 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003995 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003996 && (eap->cmdidx == CMD_cexpr
3997 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003998 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003999 }
4000 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004001 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004002 free_tv(tv);
4003 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004004}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004005#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004006
4007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 * ":helpgrep {pattern}"
4009 */
4010 void
4011ex_helpgrep(eap)
4012 exarg_T *eap;
4013{
4014 regmatch_T regmatch;
4015 char_u *save_cpo;
4016 char_u *p;
4017 int fcount;
4018 char_u **fnames;
4019 FILE *fd;
4020 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004021 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004023#ifdef FEAT_MULTI_LANG
4024 char_u *lang;
4025#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004026 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004027 int new_qi = FALSE;
4028 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004029#ifdef FEAT_AUTOCMD
4030 char_u *au_name = NULL;
4031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004033#ifdef FEAT_MULTI_LANG
4034 /* Check for a specified language */
4035 lang = check_help_lang(eap->arg);
4036#endif
4037
Bram Moolenaar73633f82012-01-20 13:39:07 +01004038#ifdef FEAT_AUTOCMD
4039 switch (eap->cmdidx)
4040 {
4041 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4042 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4043 default: break;
4044 }
4045 if (au_name != NULL)
4046 {
4047 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4048 curbuf->b_fname, TRUE, curbuf);
4049 if (did_throw || force_abort)
4050 return;
4051 }
4052#endif
4053
4054 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4055 save_cpo = p_cpo;
4056 p_cpo = empty_option;
4057
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004058 if (eap->cmdidx == CMD_lhelpgrep)
4059 {
4060 /* Find an existing help window */
4061 FOR_ALL_WINDOWS(wp)
4062 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4063 break;
4064
4065 if (wp == NULL) /* Help window not found */
4066 qi = NULL;
4067 else
4068 qi = wp->w_llist;
4069
4070 if (qi == NULL)
4071 {
4072 /* Allocate a new location list for help text matches */
4073 if ((qi = ll_new_list()) == NULL)
4074 return;
4075 new_qi = TRUE;
4076 }
4077 }
4078
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4080 regmatch.rm_ic = FALSE;
4081 if (regmatch.regprog != NULL)
4082 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004083#ifdef FEAT_MBYTE
4084 vimconv_T vc;
4085
4086 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4087 * differs. */
4088 vc.vc_type = CONV_NONE;
4089 if (!enc_utf8)
4090 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4091#endif
4092
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004094 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096 /* Go through all directories in 'runtimepath' */
4097 p = p_rtp;
4098 while (*p != NUL && !got_int)
4099 {
4100 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4101
4102 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4103 add_pathsep(NameBuff);
4104 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4105 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4106 &fnames, EW_FILE|EW_SILENT) == OK
4107 && fcount > 0)
4108 {
4109 for (fi = 0; fi < fcount && !got_int; ++fi)
4110 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004111#ifdef FEAT_MULTI_LANG
4112 /* Skip files for a different language. */
4113 if (lang != NULL
4114 && STRNICMP(lang, fnames[fi]
4115 + STRLEN(fnames[fi]) - 3, 2) != 0
4116 && !(STRNICMP(lang, "en", 2) == 0
4117 && STRNICMP("txt", fnames[fi]
4118 + STRLEN(fnames[fi]) - 3, 3) == 0))
4119 continue;
4120#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004121 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (fd != NULL)
4123 {
4124 lnum = 1;
4125 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4126 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004127 char_u *line = IObuff;
4128#ifdef FEAT_MBYTE
4129 /* Convert a line if 'encoding' is not utf-8 and
4130 * the line contains a non-ASCII character. */
4131 if (vc.vc_type != CONV_NONE
4132 && has_non_ascii(IObuff)) {
4133 line = string_convert(&vc, IObuff, NULL);
4134 if (line == NULL)
4135 line = IObuff;
4136 }
4137#endif
4138
4139 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004141 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004144 while (l > 0 && line[l - 1] <= ' ')
4145 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004147 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 NULL, /* dir */
4149 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004150 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004151 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004153 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004154 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004155 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004156 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 0, /* nr */
4158 1, /* type */
4159 TRUE /* valid */
4160 ) == FAIL)
4161 {
4162 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004163#ifdef FEAT_MBYTE
4164 if (line != IObuff)
4165 vim_free(line);
4166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 break;
4168 }
4169 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004170#ifdef FEAT_MBYTE
4171 if (line != IObuff)
4172 vim_free(line);
4173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 ++lnum;
4175 line_breakcheck();
4176 }
4177 fclose(fd);
4178 }
4179 }
4180 FreeWild(fcount, fnames);
4181 }
4182 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004183
Bram Moolenaar473de612013-06-08 18:19:48 +02004184 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004185#ifdef FEAT_MBYTE
4186 if (vc.vc_type != CONV_NONE)
4187 convert_setup(&vc, NULL, NULL);
4188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004190 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4191 qi->qf_lists[qi->qf_curlist].qf_ptr =
4192 qi->qf_lists[qi->qf_curlist].qf_start;
4193 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004196 if (p_cpo == empty_option)
4197 p_cpo = save_cpo;
4198 else
4199 /* Darn, some plugin changed the value. */
4200 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
4202#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004203 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204#endif
4205
Bram Moolenaar73633f82012-01-20 13:39:07 +01004206#ifdef FEAT_AUTOCMD
4207 if (au_name != NULL)
4208 {
4209 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4210 curbuf->b_fname, TRUE, curbuf);
4211 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4212 /* autocommands made "qi" invalid */
4213 return;
4214 }
4215#endif
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004218 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004219 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004220 else
4221 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004222
4223 if (eap->cmdidx == CMD_lhelpgrep)
4224 {
4225 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004226 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004227 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4228 {
4229 if (new_qi)
4230 ll_free_all(&qi);
4231 }
4232 else if (curwin->w_llist == NULL)
4233 curwin->w_llist = qi;
4234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235}
4236
4237#endif /* FEAT_QUICKFIX */