blob: 4b216adf75b098683149b3830157bb1a5e66f6de [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)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100756 multiignore = FALSE; /* reset continuation */
757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
759 { /* continuation of multi-line msg */
760 if (qfprev == NULL)
761 goto error2;
762 if (*errmsg && !multiignore)
763 {
764 len = (int)STRLEN(qfprev->qf_text);
765 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
766 == NULL)
767 goto error2;
768 STRCPY(ptr, qfprev->qf_text);
769 vim_free(qfprev->qf_text);
770 qfprev->qf_text = ptr;
771 *(ptr += len) = '\n';
772 STRCPY(++ptr, errmsg);
773 }
774 if (qfprev->qf_nr == -1)
775 qfprev->qf_nr = enr;
776 if (vim_isprintc(type) && !qfprev->qf_type)
777 qfprev->qf_type = type; /* only printable chars allowed */
778 if (!qfprev->qf_lnum)
779 qfprev->qf_lnum = lnum;
780 if (!qfprev->qf_col)
781 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (!qfprev->qf_fnum)
784 qfprev->qf_fnum = qf_get_fnum(directory,
785 *namebuf || directory ? namebuf
786 : currfile && valid ? currfile : 0);
787 if (idx == 'Z')
788 multiline = multiignore = FALSE;
789 line_breakcheck();
790 continue;
791 }
792 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
793 {
794 /* global file names */
795 valid = FALSE;
796 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
797 {
798 if (*namebuf && idx == 'P')
799 currfile = qf_push_dir(namebuf, &file_stack);
800 else if (idx == 'Q')
801 currfile = qf_pop_dir(&file_stack);
802 *namebuf = NUL;
803 if (tail && *tail)
804 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000805 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 multiscan = TRUE;
807 goto restofline;
808 }
809 }
810 }
811 if (fmt_ptr->flags == '-') /* generally exclude this line */
812 {
813 if (multiline)
814 multiignore = TRUE; /* also exclude continuation lines */
815 continue;
816 }
817 }
818
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000819 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000821 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000823 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000824 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 errmsg,
826 lnum,
827 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000829 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 enr,
831 type,
832 valid) == FAIL)
833 goto error2;
834 line_breakcheck();
835 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000836 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000838 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000840 /* no valid entry found */
841 qi->qf_lists[qi->qf_curlist].qf_ptr =
842 qi->qf_lists[qi->qf_curlist].qf_start;
843 qi->qf_lists[qi->qf_curlist].qf_index = 1;
844 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846 else
847 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000848 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
849 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
850 qi->qf_lists[qi->qf_curlist].qf_ptr =
851 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000853 /* return number of matches */
854 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 goto qf_init_ok;
856 }
857 EMSG(_(e_readerrf));
858error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000859 qf_free(qi, qi->qf_curlist);
860 qi->qf_listcount--;
861 if (qi->qf_curlist > 0)
862 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000864 if (fd != NULL)
865 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
867 {
868 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +0200869 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 vim_free(fmt_ptr);
871 }
872 qf_clean_dir_stack(&dir_stack);
873 qf_clean_dir_stack(&file_stack);
874qf_init_end:
875 vim_free(namebuf);
876 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000877 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 vim_free(fmtstr);
879
880#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000881 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882#endif
883
884 return retval;
885}
886
887/*
888 * Prepare for adding a new quickfix list.
889 */
890 static void
Bram Moolenaar94116152012-11-28 17:41:59 +0100891qf_new_list(qi, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000892 qf_info_T *qi;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200893 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
895 int i;
896
897 /*
898 * If the current entry is not the last entry, delete entries below
899 * the current entry. This makes it possible to browse in a tree-like
900 * way with ":grep'.
901 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000902 while (qi->qf_listcount > qi->qf_curlist + 1)
903 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
905 /*
906 * When the stack is full, remove to oldest entry
907 * Otherwise, add a new entry.
908 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000909 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000911 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000913 qi->qf_lists[i - 1] = qi->qf_lists[i];
914 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000917 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100918 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200919 if (qf_title != NULL)
920 {
Bram Moolenaar5e109c42010-07-26 22:51:28 +0200921 char_u *p = alloc((int)STRLEN(qf_title) + 2);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200922
923 qi->qf_lists[qi->qf_curlist].qf_title = p;
924 if (p != NULL)
925 sprintf((char *)p, ":%s", (char *)qf_title);
926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927}
928
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000929/*
930 * Free a location list
931 */
932 static void
933ll_free_all(pqi)
934 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000935{
936 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000937 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000938
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000939 qi = *pqi;
940 if (qi == NULL)
941 return;
942 *pqi = NULL; /* Remove reference to this list */
943
944 qi->qf_refcount--;
945 if (qi->qf_refcount < 1)
946 {
947 /* No references to this location list */
948 for (i = 0; i < qi->qf_listcount; ++i)
949 qf_free(qi, i);
950 vim_free(qi);
951 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000952}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000953
954 void
955qf_free_all(wp)
956 win_T *wp;
957{
958 int i;
959 qf_info_T *qi = &ql_info;
960
961 if (wp != NULL)
962 {
963 /* location list */
964 ll_free_all(&wp->w_llist);
965 ll_free_all(&wp->w_llist_ref);
966 }
967 else
968 /* quickfix list */
969 for (i = 0; i < qi->qf_listcount; ++i)
970 qf_free(qi, i);
971}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973/*
974 * Add an entry to the end of the list of errors.
975 * Returns OK or FAIL.
976 */
977 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000978qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
979 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000980 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000981 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 char_u *dir; /* optional directory name */
983 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000984 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 char_u *mesg; /* message */
986 long lnum; /* line number */
987 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000988 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000989 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 int nr; /* error number */
991 int type; /* type character */
992 int valid; /* valid entry */
993{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000994 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000996 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000998 if (bufnum != 0)
999 qfp->qf_fnum = bufnum;
1000 else
1001 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1003 {
1004 vim_free(qfp);
1005 return FAIL;
1006 }
1007 qfp->qf_lnum = lnum;
1008 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001009 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001010 if (pattern == NULL || *pattern == NUL)
1011 qfp->qf_pattern = NULL;
1012 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1013 {
1014 vim_free(qfp->qf_text);
1015 vim_free(qfp);
1016 return FAIL;
1017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 qfp->qf_nr = nr;
1019 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1020 type = 0;
1021 qfp->qf_type = type;
1022 qfp->qf_valid = valid;
1023
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001024 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1025 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001027 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 qfp->qf_prev = qfp; /* first element points to itself */
1029 }
1030 else
1031 {
1032 qfp->qf_prev = *prevp;
1033 (*prevp)->qf_next = qfp;
1034 }
1035 qfp->qf_next = qfp; /* last element points to itself */
1036 qfp->qf_cleared = FALSE;
1037 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001038 ++qi->qf_lists[qi->qf_curlist].qf_count;
1039 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1040 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001042 qi->qf_lists[qi->qf_curlist].qf_index =
1043 qi->qf_lists[qi->qf_curlist].qf_count;
1044 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 }
1046
1047 return OK;
1048}
1049
1050/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001051 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001052 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001053 static qf_info_T *
1054ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001055{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001056 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001057
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001058 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1059 if (qi != NULL)
1060 {
1061 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1062 qi->qf_refcount++;
1063 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001064
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001065 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001066}
1067
1068/*
1069 * Return the location list for window 'wp'.
1070 * If not present, allocate a location list
1071 */
1072 static qf_info_T *
1073ll_get_or_alloc_list(wp)
1074 win_T *wp;
1075{
1076 if (IS_LL_WINDOW(wp))
1077 /* For a location list window, use the referenced location list */
1078 return wp->w_llist_ref;
1079
1080 /*
1081 * For a non-location list window, w_llist_ref should not point to a
1082 * location list.
1083 */
1084 ll_free_all(&wp->w_llist_ref);
1085
1086 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001087 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001088 return wp->w_llist;
1089}
1090
1091/*
1092 * Copy the location list from window "from" to window "to".
1093 */
1094 void
1095copy_loclist(from, to)
1096 win_T *from;
1097 win_T *to;
1098{
1099 qf_info_T *qi;
1100 int idx;
1101 int i;
1102
1103 /*
1104 * When copying from a location list window, copy the referenced
1105 * location list. For other windows, copy the location list for
1106 * that window.
1107 */
1108 if (IS_LL_WINDOW(from))
1109 qi = from->w_llist_ref;
1110 else
1111 qi = from->w_llist;
1112
1113 if (qi == NULL) /* no location list to copy */
1114 return;
1115
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001116 /* allocate a new location list */
1117 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001118 return;
1119
1120 to->w_llist->qf_listcount = qi->qf_listcount;
1121
1122 /* Copy the location lists one at a time */
1123 for (idx = 0; idx < qi->qf_listcount; idx++)
1124 {
1125 qf_list_T *from_qfl;
1126 qf_list_T *to_qfl;
1127
1128 to->w_llist->qf_curlist = idx;
1129
1130 from_qfl = &qi->qf_lists[idx];
1131 to_qfl = &to->w_llist->qf_lists[idx];
1132
1133 /* Some of the fields are populated by qf_add_entry() */
1134 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1135 to_qfl->qf_count = 0;
1136 to_qfl->qf_index = 0;
1137 to_qfl->qf_start = NULL;
1138 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001139 if (from_qfl->qf_title != NULL)
1140 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1141 else
1142 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001143
1144 if (from_qfl->qf_count)
1145 {
1146 qfline_T *from_qfp;
1147 qfline_T *prevp = NULL;
1148
1149 /* copy all the location entries in this list */
1150 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1151 ++i, from_qfp = from_qfp->qf_next)
1152 {
1153 if (qf_add_entry(to->w_llist, &prevp,
1154 NULL,
1155 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001156 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001157 from_qfp->qf_text,
1158 from_qfp->qf_lnum,
1159 from_qfp->qf_col,
1160 from_qfp->qf_viscol,
1161 from_qfp->qf_pattern,
1162 from_qfp->qf_nr,
1163 0,
1164 from_qfp->qf_valid) == FAIL)
1165 {
1166 qf_free_all(to);
1167 return;
1168 }
1169 /*
1170 * qf_add_entry() will not set the qf_num field, as the
1171 * directory and file names are not supplied. So the qf_fnum
1172 * field is copied here.
1173 */
1174 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1175 prevp->qf_type = from_qfp->qf_type; /* error type */
1176 if (from_qfl->qf_ptr == from_qfp)
1177 to_qfl->qf_ptr = prevp; /* current location */
1178 }
1179 }
1180
1181 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1182
1183 /* When no valid entries are present in the list, qf_ptr points to
1184 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001185 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001186 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001187 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001188 to_qfl->qf_index = 1;
1189 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001190 }
1191
1192 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1193}
1194
1195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 * get buffer number for file "dir.name"
1197 */
1198 static int
1199qf_get_fnum(directory, fname)
1200 char_u *directory;
1201 char_u *fname;
1202{
1203 if (fname == NULL || *fname == NUL) /* no file name */
1204 return 0;
1205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 char_u *ptr;
1207 int fnum;
1208
Bram Moolenaare60acc12011-05-10 16:41:25 +02001209#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001211#endif
1212#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (directory != NULL)
1214 slash_adjust(directory);
1215 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (directory != NULL && !vim_isAbsName(fname)
1218 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1219 {
1220 /*
1221 * Here we check if the file really exists.
1222 * This should normally be true, but if make works without
1223 * "leaving directory"-messages we might have missed a
1224 * directory change.
1225 */
1226 if (mch_getperm(ptr) < 0)
1227 {
1228 vim_free(ptr);
1229 directory = qf_guess_filepath(fname);
1230 if (directory)
1231 ptr = concat_fnames(directory, fname, TRUE);
1232 else
1233 ptr = vim_strsave(fname);
1234 }
1235 /* Use concatenated directory name and file name */
1236 fnum = buflist_add(ptr, 0);
1237 vim_free(ptr);
1238 return fnum;
1239 }
1240 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242}
1243
1244/*
1245 * push dirbuf onto the directory stack and return pointer to actual dir or
1246 * NULL on error
1247 */
1248 static char_u *
1249qf_push_dir(dirbuf, stackptr)
1250 char_u *dirbuf;
1251 struct dir_stack_T **stackptr;
1252{
1253 struct dir_stack_T *ds_new;
1254 struct dir_stack_T *ds_ptr;
1255
1256 /* allocate new stack element and hook it in */
1257 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1258 if (ds_new == NULL)
1259 return NULL;
1260
1261 ds_new->next = *stackptr;
1262 *stackptr = ds_new;
1263
1264 /* store directory on the stack */
1265 if (vim_isAbsName(dirbuf)
1266 || (*stackptr)->next == NULL
1267 || (*stackptr && dir_stack != *stackptr))
1268 (*stackptr)->dirname = vim_strsave(dirbuf);
1269 else
1270 {
1271 /* Okay we don't have an absolute path.
1272 * dirbuf must be a subdir of one of the directories on the stack.
1273 * Let's search...
1274 */
1275 ds_new = (*stackptr)->next;
1276 (*stackptr)->dirname = NULL;
1277 while (ds_new)
1278 {
1279 vim_free((*stackptr)->dirname);
1280 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1281 TRUE);
1282 if (mch_isdir((*stackptr)->dirname) == TRUE)
1283 break;
1284
1285 ds_new = ds_new->next;
1286 }
1287
1288 /* clean up all dirs we already left */
1289 while ((*stackptr)->next != ds_new)
1290 {
1291 ds_ptr = (*stackptr)->next;
1292 (*stackptr)->next = (*stackptr)->next->next;
1293 vim_free(ds_ptr->dirname);
1294 vim_free(ds_ptr);
1295 }
1296
1297 /* Nothing found -> it must be on top level */
1298 if (ds_new == NULL)
1299 {
1300 vim_free((*stackptr)->dirname);
1301 (*stackptr)->dirname = vim_strsave(dirbuf);
1302 }
1303 }
1304
1305 if ((*stackptr)->dirname != NULL)
1306 return (*stackptr)->dirname;
1307 else
1308 {
1309 ds_ptr = *stackptr;
1310 *stackptr = (*stackptr)->next;
1311 vim_free(ds_ptr);
1312 return NULL;
1313 }
1314}
1315
1316
1317/*
1318 * pop dirbuf from the directory stack and return previous directory or NULL if
1319 * stack is empty
1320 */
1321 static char_u *
1322qf_pop_dir(stackptr)
1323 struct dir_stack_T **stackptr;
1324{
1325 struct dir_stack_T *ds_ptr;
1326
1327 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1328 * What to do if it isn't? */
1329
1330 /* pop top element and free it */
1331 if (*stackptr != NULL)
1332 {
1333 ds_ptr = *stackptr;
1334 *stackptr = (*stackptr)->next;
1335 vim_free(ds_ptr->dirname);
1336 vim_free(ds_ptr);
1337 }
1338
1339 /* return NEW top element as current dir or NULL if stack is empty*/
1340 return *stackptr ? (*stackptr)->dirname : NULL;
1341}
1342
1343/*
1344 * clean up directory stack
1345 */
1346 static void
1347qf_clean_dir_stack(stackptr)
1348 struct dir_stack_T **stackptr;
1349{
1350 struct dir_stack_T *ds_ptr;
1351
1352 while ((ds_ptr = *stackptr) != NULL)
1353 {
1354 *stackptr = (*stackptr)->next;
1355 vim_free(ds_ptr->dirname);
1356 vim_free(ds_ptr);
1357 }
1358}
1359
1360/*
1361 * Check in which directory of the directory stack the given file can be
1362 * found.
1363 * Returns a pointer to the directory name or NULL if not found
1364 * Cleans up intermediate directory entries.
1365 *
1366 * TODO: How to solve the following problem?
1367 * If we have the this directory tree:
1368 * ./
1369 * ./aa
1370 * ./aa/bb
1371 * ./bb
1372 * ./bb/x.c
1373 * and make says:
1374 * making all in aa
1375 * making all in bb
1376 * x.c:9: Error
1377 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1378 * qf_guess_filepath will return NULL.
1379 */
1380 static char_u *
1381qf_guess_filepath(filename)
1382 char_u *filename;
1383{
1384 struct dir_stack_T *ds_ptr;
1385 struct dir_stack_T *ds_tmp;
1386 char_u *fullname;
1387
1388 /* no dirs on the stack - there's nothing we can do */
1389 if (dir_stack == NULL)
1390 return NULL;
1391
1392 ds_ptr = dir_stack->next;
1393 fullname = NULL;
1394 while (ds_ptr)
1395 {
1396 vim_free(fullname);
1397 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1398
1399 /* If concat_fnames failed, just go on. The worst thing that can happen
1400 * is that we delete the entire stack.
1401 */
1402 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1403 break;
1404
1405 ds_ptr = ds_ptr->next;
1406 }
1407
1408 vim_free(fullname);
1409
1410 /* clean up all dirs we already left */
1411 while (dir_stack->next != ds_ptr)
1412 {
1413 ds_tmp = dir_stack->next;
1414 dir_stack->next = dir_stack->next->next;
1415 vim_free(ds_tmp->dirname);
1416 vim_free(ds_tmp);
1417 }
1418
1419 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1420
1421}
1422
1423/*
1424 * jump to a quickfix line
1425 * if dir == FORWARD go "errornr" valid entries forward
1426 * if dir == BACKWARD go "errornr" valid entries backward
1427 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1428 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1429 * else if "errornr" is zero, redisplay the same line
1430 * else go to entry "errornr"
1431 */
1432 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001433qf_jump(qi, dir, errornr, forceit)
1434 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 int dir;
1436 int errornr;
1437 int forceit;
1438{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001439 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001440 qfline_T *qf_ptr;
1441 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 int qf_index;
1443 int old_qf_fnum;
1444 int old_qf_index;
1445 int prev_index;
1446 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1447 char_u *err = e_no_more_items;
1448 linenr_T i;
1449 buf_T *old_curbuf;
1450 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 colnr_T screen_col;
1452 colnr_T char_col;
1453 char_u *line;
1454#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001455 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001456 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 int opened_window = FALSE;
1458 win_T *win;
1459 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001460 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001462 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 int print_message = TRUE;
1464 int len;
1465#ifdef FEAT_FOLDING
1466 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1467#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001468 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001469 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001471 if (qi == NULL)
1472 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001473
1474 if (qi->qf_curlist >= qi->qf_listcount
1475 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 EMSG(_(e_quickfix));
1478 return;
1479 }
1480
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001481 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001483 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 old_qf_index = qf_index;
1485 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1486 {
1487 while (errornr--)
1488 {
1489 old_qf_ptr = qf_ptr;
1490 prev_index = qf_index;
1491 old_qf_fnum = qf_ptr->qf_fnum;
1492 do
1493 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001494 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 || qf_ptr->qf_next == NULL)
1496 {
1497 qf_ptr = old_qf_ptr;
1498 qf_index = prev_index;
1499 if (err != NULL)
1500 {
1501 EMSG(_(err));
1502 goto theend;
1503 }
1504 errornr = 0;
1505 break;
1506 }
1507 ++qf_index;
1508 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001509 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1510 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1512 err = NULL;
1513 }
1514 }
1515 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1516 {
1517 while (errornr--)
1518 {
1519 old_qf_ptr = qf_ptr;
1520 prev_index = qf_index;
1521 old_qf_fnum = qf_ptr->qf_fnum;
1522 do
1523 {
1524 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1525 {
1526 qf_ptr = old_qf_ptr;
1527 qf_index = prev_index;
1528 if (err != NULL)
1529 {
1530 EMSG(_(err));
1531 goto theend;
1532 }
1533 errornr = 0;
1534 break;
1535 }
1536 --qf_index;
1537 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001538 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1539 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1541 err = NULL;
1542 }
1543 }
1544 else if (errornr != 0) /* go to specified number */
1545 {
1546 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1547 {
1548 --qf_index;
1549 qf_ptr = qf_ptr->qf_prev;
1550 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001551 while (errornr > qf_index && qf_index <
1552 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 && qf_ptr->qf_next != NULL)
1554 {
1555 ++qf_index;
1556 qf_ptr = qf_ptr->qf_next;
1557 }
1558 }
1559
1560#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001561 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1562 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 /* No need to print the error message if it's visible in the error
1564 * window */
1565 print_message = FALSE;
1566
1567 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001568 * For ":helpgrep" find a help window or open one.
1569 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001570 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001571 {
1572 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001573
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001574 if (cmdmod.tab != 0)
1575 wp = NULL;
1576 else
1577 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1578 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1579 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001580 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1581 win_enter(wp, TRUE);
1582 else
1583 {
1584 /*
1585 * Split off help window; put it at far top if no position
1586 * specified, the current window is vertically split and narrow.
1587 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001588 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001589# ifdef FEAT_VERTSPLIT
1590 if (cmdmod.split == 0 && curwin->w_width != Columns
1591 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001592 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001593# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001594 if (qi != &ql_info)
1595 flags |= WSP_NEWLOC; /* don't copy the location list */
1596
1597 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001598 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001599 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001600
1601 if (curwin->w_height < p_hh)
1602 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001603
1604 if (qi != &ql_info) /* not a quickfix list */
1605 {
1606 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001607 curwin->w_llist = qi;
1608 qi->qf_refcount++;
1609 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001610 }
1611
1612 if (!p_im)
1613 restart_edit = 0; /* don't want insert mode in help file */
1614 }
1615
1616 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 * If currently in the quickfix window, find another window to show the
1618 * file in.
1619 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001620 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001622 win_T *usable_win_ptr = NULL;
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 /*
1625 * If there is no file specified, we don't know where to go.
1626 * But do advance, otherwise ":cn" gets stuck.
1627 */
1628 if (qf_ptr->qf_fnum == 0)
1629 goto theend;
1630
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001632
1633 ll_ref = curwin->w_llist_ref;
1634 if (ll_ref != NULL)
1635 {
1636 /* Find a window using the same location list that is not a
1637 * quickfix window. */
1638 FOR_ALL_WINDOWS(usable_win_ptr)
1639 if (usable_win_ptr->w_llist == ll_ref
1640 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001641 {
1642 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001643 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001644 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001645 }
1646
1647 if (!usable_win)
1648 {
1649 /* Locate a window showing a normal buffer */
1650 FOR_ALL_WINDOWS(win)
1651 if (win->w_buffer->b_p_bt[0] == NUL)
1652 {
1653 usable_win = 1;
1654 break;
1655 }
1656 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001659 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001660 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001662 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001663 {
1664 tabpage_T *tp;
1665 win_T *wp;
1666
1667 FOR_ALL_TAB_WINDOWS(tp, wp)
1668 {
1669 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1670 {
1671 goto_tabpage_win(tp, wp);
1672 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001673 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001674 }
1675 }
1676 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001677win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001678
1679 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001680 * If there is only one window and it is the quickfix window, create a
1681 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001682 */
1683 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001685 flags = WSP_ABOVE;
1686 if (ll_ref != NULL)
1687 flags |= WSP_NEWLOC;
1688 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 goto failed; /* not enough room for window */
1690 opened_window = TRUE; /* close it when fail */
1691 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001692 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001693 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001694 if (ll_ref != NULL)
1695 {
1696 /* The new window should use the location list from the
1697 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001698 curwin->w_llist = ll_ref;
1699 ll_ref->qf_refcount++;
1700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702 else
1703 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001704 if (curwin->w_llist_ref != NULL)
1705 {
1706 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001707 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001708 if (win == NULL)
1709 {
1710 /* Find the window showing the selected file */
1711 FOR_ALL_WINDOWS(win)
1712 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1713 break;
1714 if (win == NULL)
1715 {
1716 /* Find a previous usable window */
1717 win = curwin;
1718 do
1719 {
1720 if (win->w_buffer->b_p_bt[0] == NUL)
1721 break;
1722 if (win->w_prev == NULL)
1723 win = lastwin; /* wrap around the top */
1724 else
1725 win = win->w_prev; /* go to previous window */
1726 } while (win != curwin);
1727 }
1728 }
1729 win_goto(win);
1730
1731 /* If the location list for the window is not set, then set it
1732 * to the location list from the location window */
1733 if (win->w_llist == NULL)
1734 {
1735 win->w_llist = ll_ref;
1736 ll_ref->qf_refcount++;
1737 }
1738 }
1739 else
1740 {
1741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 /*
1743 * Try to find a window that shows the right buffer.
1744 * Default to the window just above the quickfix buffer.
1745 */
1746 win = curwin;
1747 altwin = NULL;
1748 for (;;)
1749 {
1750 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1751 break;
1752 if (win->w_prev == NULL)
1753 win = lastwin; /* wrap around the top */
1754 else
1755 win = win->w_prev; /* go to previous window */
1756
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001757 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 /* Didn't find it, go to the window before the quickfix
1760 * window. */
1761 if (altwin != NULL)
1762 win = altwin;
1763 else if (curwin->w_prev != NULL)
1764 win = curwin->w_prev;
1765 else
1766 win = curwin->w_next;
1767 break;
1768 }
1769
1770 /* Remember a usable window. */
1771 if (altwin == NULL && !win->w_p_pvw
1772 && win->w_buffer->b_p_bt[0] == NUL)
1773 altwin = win;
1774 }
1775
1776 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 }
1779 }
1780#endif
1781
1782 /*
1783 * If there is a file name,
1784 * read the wanted file if needed, and check autowrite etc.
1785 */
1786 old_curbuf = curbuf;
1787 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001788
1789 if (qf_ptr->qf_fnum != 0)
1790 {
1791 if (qf_ptr->qf_type == 1)
1792 {
1793 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1794 * set b_p_ro flag). */
1795 if (!can_abandon(curbuf, forceit))
1796 {
1797 EMSG(_(e_nowrtmsg));
1798 ok = FALSE;
1799 }
1800 else
1801 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001802 ECMD_HIDE + ECMD_SET_HELP,
1803 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001804 }
1805 else
1806 ok = buflist_getfile(qf_ptr->qf_fnum,
1807 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1808 }
1809
1810 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 {
1812 /* When not switched to another buffer, still need to set pc mark */
1813 if (curbuf == old_curbuf)
1814 setpcmark();
1815
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001816 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001818 /*
1819 * Go to line with error, unless qf_lnum is 0.
1820 */
1821 i = qf_ptr->qf_lnum;
1822 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001824 if (i > curbuf->b_ml.ml_line_count)
1825 i = curbuf->b_ml.ml_line_count;
1826 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001828 if (qf_ptr->qf_col > 0)
1829 {
1830 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1831 if (qf_ptr->qf_viscol == TRUE)
1832 {
1833 /*
1834 * Check each character from the beginning of the error
1835 * line up to the error column. For each tab character
1836 * found, reduce the error column value by the length of
1837 * a tab character.
1838 */
1839 line = ml_get_curline();
1840 screen_col = 0;
1841 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1842 {
1843 if (*line == NUL)
1844 break;
1845 if (*line++ == '\t')
1846 {
1847 curwin->w_cursor.col -= 7 - (screen_col % 8);
1848 screen_col += 8 - (screen_col % 8);
1849 }
1850 else
1851 ++screen_col;
1852 }
1853 }
1854 check_cursor();
1855 }
1856 else
1857 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001860 {
1861 pos_T save_cursor;
1862
1863 /* Move the cursor to the first line in the buffer */
1864 save_cursor = curwin->w_cursor;
1865 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001866 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1867 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001868 curwin->w_cursor = save_cursor;
1869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
1871#ifdef FEAT_FOLDING
1872 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1873 foldOpenCursor();
1874#endif
1875 if (print_message)
1876 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001877 /* Update the screen before showing the message, unless the screen
1878 * scrolled up. */
1879 if (!msg_scrolled)
1880 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001882 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1884 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1885 /* Add the message, skipping leading whitespace and newlines. */
1886 len = (int)STRLEN(IObuff);
1887 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1888
1889 /* Output the message. Overwrite to avoid scrolling when the 'O'
1890 * flag is present in 'shortmess'; But when not jumping, print the
1891 * whole message. */
1892 i = msg_scroll;
1893 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1894 msg_scroll = TRUE;
1895 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1896 msg_scroll = FALSE;
1897 msg_attr_keep(IObuff, 0, TRUE);
1898 msg_scroll = i;
1899 }
1900 }
1901 else
1902 {
1903#ifdef FEAT_WINDOWS
1904 if (opened_window)
1905 win_close(curwin, TRUE); /* Close opened window */
1906#endif
1907 if (qf_ptr->qf_fnum != 0)
1908 {
1909 /*
1910 * Couldn't open file, so put index back where it was. This could
1911 * happen if the file was readonly and we changed something.
1912 */
1913#ifdef FEAT_WINDOWS
1914failed:
1915#endif
1916 qf_ptr = old_qf_ptr;
1917 qf_index = old_qf_index;
1918 }
1919 }
1920theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001921 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1922 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#ifdef FEAT_WINDOWS
1924 if (p_swb != old_swb && opened_window)
1925 {
1926 /* Restore old 'switchbuf' value, but not when an autocommand or
1927 * modeline has changed the value. */
1928 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001931 swb_flags = old_swb_flags;
1932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 else
1934 free_string_option(old_swb);
1935 }
1936#endif
1937}
1938
1939/*
1940 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001941 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 */
1943 void
1944qf_list(eap)
1945 exarg_T *eap;
1946{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001947 buf_T *buf;
1948 char_u *fname;
1949 qfline_T *qfp;
1950 int i;
1951 int idx1 = 1;
1952 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001953 char_u *arg = eap->arg;
1954 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001956 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001958 if (eap->cmdidx == CMD_llist)
1959 {
1960 qi = GET_LOC_LIST(curwin);
1961 if (qi == NULL)
1962 {
1963 EMSG(_(e_loclist));
1964 return;
1965 }
1966 }
1967
1968 if (qi->qf_curlist >= qi->qf_listcount
1969 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 EMSG(_(e_quickfix));
1972 return;
1973 }
1974 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1975 {
1976 EMSG(_(e_trailing));
1977 return;
1978 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001979 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (idx1 < 0)
1981 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1982 if (idx2 < 0)
1983 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1984
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001985 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001987 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1988 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
1990 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1991 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001992 msg_putchar('\n');
1993 if (got_int)
1994 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001995
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001996 fname = NULL;
1997 if (qfp->qf_fnum != 0
1998 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1999 {
2000 fname = buf->b_fname;
2001 if (qfp->qf_type == 1) /* :helpgrep */
2002 fname = gettail(fname);
2003 }
2004 if (fname == NULL)
2005 sprintf((char *)IObuff, "%2d", i);
2006 else
2007 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2008 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002010 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2011 if (qfp->qf_lnum == 0)
2012 IObuff[0] = NUL;
2013 else if (qfp->qf_col == 0)
2014 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2015 else
2016 sprintf((char *)IObuff, ":%ld col %d",
2017 qfp->qf_lnum, qfp->qf_col);
2018 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2019 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2020 msg_puts_attr(IObuff, hl_attr(HLF_N));
2021 if (qfp->qf_pattern != NULL)
2022 {
2023 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2024 STRCAT(IObuff, ":");
2025 msg_puts(IObuff);
2026 }
2027 msg_puts((char_u *)" ");
2028
2029 /* Remove newlines and leading whitespace from the text. For an
2030 * unrecognized line keep the indent, the compiler may mark a word
2031 * with ^^^^. */
2032 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2034 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002035 msg_prt_line(IObuff, FALSE);
2036 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002038
2039 qfp = qfp->qf_next;
2040 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 ui_breakcheck();
2042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043}
2044
2045/*
2046 * Remove newlines and leading whitespace from an error message.
2047 * Put the result in "buf[bufsize]".
2048 */
2049 static void
2050qf_fmt_text(text, buf, bufsize)
2051 char_u *text;
2052 char_u *buf;
2053 int bufsize;
2054{
2055 int i;
2056 char_u *p = text;
2057
2058 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2059 {
2060 if (*p == '\n')
2061 {
2062 buf[i] = ' ';
2063 while (*++p != NUL)
2064 if (!vim_iswhite(*p) && *p != '\n')
2065 break;
2066 }
2067 else
2068 buf[i] = *p++;
2069 }
2070 buf[i] = NUL;
2071}
2072
2073/*
2074 * ":colder [count]": Up in the quickfix stack.
2075 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002076 * ":lolder [count]": Up in the location list stack.
2077 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 */
2079 void
2080qf_age(eap)
2081 exarg_T *eap;
2082{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002083 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 int count;
2085
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002086 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2087 {
2088 qi = GET_LOC_LIST(curwin);
2089 if (qi == NULL)
2090 {
2091 EMSG(_(e_loclist));
2092 return;
2093 }
2094 }
2095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 if (eap->addr_count != 0)
2097 count = eap->line2;
2098 else
2099 count = 1;
2100 while (count--)
2101 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002102 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002104 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
2106 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002107 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002109 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111 else
2112 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002113 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
2115 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
2120 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002121 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122}
2123
2124 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002125qf_msg(qi)
2126 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002129 qi->qf_curlist + 1, qi->qf_listcount,
2130 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002132 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133#endif
2134}
2135
2136/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002137 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 */
2139 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002140qf_free(qi, idx)
2141 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 int idx;
2143{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002144 qfline_T *qfp;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002145 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002147 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002150 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002151 {
2152 vim_free(qi->qf_lists[idx].qf_start->qf_text);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002153 stop = (qi->qf_lists[idx].qf_start == qfp);
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002154 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2155 vim_free(qi->qf_lists[idx].qf_start);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002156 if (stop)
2157 /* Somehow qf_count may have an incorrect value, set it to 1
2158 * to avoid crashing when it's wrong.
2159 * TODO: Avoid qf_count being incorrect. */
2160 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002161 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002162 qi->qf_lists[idx].qf_start = qfp;
2163 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002165 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002166 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167}
2168
2169/*
2170 * qf_mark_adjust: adjust marks
2171 */
2172 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002173qf_mark_adjust(wp, line1, line2, amount, amount_after)
2174 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 linenr_T line1;
2176 linenr_T line2;
2177 long amount;
2178 long amount_after;
2179{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002180 int i;
2181 qfline_T *qfp;
2182 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002183 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002185 if (wp != NULL)
2186 {
2187 if (wp->w_llist == NULL)
2188 return;
2189 qi = wp->w_llist;
2190 }
2191
2192 for (idx = 0; idx < qi->qf_listcount; ++idx)
2193 if (qi->qf_lists[idx].qf_count)
2194 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2195 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if (qfp->qf_fnum == curbuf->b_fnum)
2197 {
2198 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2199 {
2200 if (amount == MAXLNUM)
2201 qfp->qf_cleared = TRUE;
2202 else
2203 qfp->qf_lnum += amount;
2204 }
2205 else if (amount_after && qfp->qf_lnum > line2)
2206 qfp->qf_lnum += amount_after;
2207 }
2208}
2209
2210/*
2211 * Make a nice message out of the error character and the error number:
2212 * char number message
2213 * e or E 0 " error"
2214 * w or W 0 " warning"
2215 * i or I 0 " info"
2216 * 0 0 ""
2217 * other 0 " c"
2218 * e or E n " error n"
2219 * w or W n " warning n"
2220 * i or I n " info n"
2221 * 0 n " error n"
2222 * other n " c n"
2223 * 1 x "" :helpgrep
2224 */
2225 static char_u *
2226qf_types(c, nr)
2227 int c, nr;
2228{
2229 static char_u buf[20];
2230 static char_u cc[3];
2231 char_u *p;
2232
2233 if (c == 'W' || c == 'w')
2234 p = (char_u *)" warning";
2235 else if (c == 'I' || c == 'i')
2236 p = (char_u *)" info";
2237 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2238 p = (char_u *)" error";
2239 else if (c == 0 || c == 1)
2240 p = (char_u *)"";
2241 else
2242 {
2243 cc[0] = ' ';
2244 cc[1] = c;
2245 cc[2] = NUL;
2246 p = cc;
2247 }
2248
2249 if (nr <= 0)
2250 return p;
2251
2252 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2253 return buf;
2254}
2255
2256#if defined(FEAT_WINDOWS) || defined(PROTO)
2257/*
2258 * ":cwindow": open the quickfix window if we have errors to display,
2259 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002260 * ":lwindow": open the location list window if we have locations to display,
2261 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 */
2263 void
2264ex_cwindow(eap)
2265 exarg_T *eap;
2266{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002267 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 win_T *win;
2269
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002270 if (eap->cmdidx == CMD_lwindow)
2271 {
2272 qi = GET_LOC_LIST(curwin);
2273 if (qi == NULL)
2274 return;
2275 }
2276
2277 /* Look for an existing quickfix window. */
2278 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
2280 /*
2281 * If a quickfix window is open but we have no errors to display,
2282 * close the window. If a quickfix window is not open, then open
2283 * it if we have errors; otherwise, leave it closed.
2284 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002285 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002286 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002287 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 if (win != NULL)
2290 ex_cclose(eap);
2291 }
2292 else if (win == NULL)
2293 ex_copen(eap);
2294}
2295
2296/*
2297 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002298 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 void
2301ex_cclose(eap)
2302 exarg_T *eap;
2303{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002304 win_T *win = NULL;
2305 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002307 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2308 {
2309 qi = GET_LOC_LIST(curwin);
2310 if (qi == NULL)
2311 return;
2312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002314 /* Find existing quickfix window and close it. */
2315 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 if (win != NULL)
2317 win_close(win, FALSE);
2318}
2319
2320/*
2321 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002322 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 */
2324 void
2325ex_copen(eap)
2326 exarg_T *eap;
2327{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002328 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002331 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002332 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002333 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002335 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2336 {
2337 qi = GET_LOC_LIST(curwin);
2338 if (qi == NULL)
2339 {
2340 EMSG(_(e_loclist));
2341 return;
2342 }
2343 }
2344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (eap->addr_count != 0)
2346 height = eap->line2;
2347 else
2348 height = QF_WINHEIGHT;
2349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#ifdef FEAT_GUI
2352 need_mouse_correct = TRUE;
2353#endif
2354
2355 /*
2356 * Find existing quickfix window, or open a new one.
2357 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002358 win = qf_find_win(qi);
2359
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002360 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 win_goto(win);
2362 else
2363 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002364 qf_buf = qf_find_buf(qi);
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 /* The current window becomes the previous window afterwards. */
2367 win = curwin;
2368
Bram Moolenaar77642c02012-11-20 17:55:10 +01002369 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2370 && cmdmod.split == 0)
2371 /* Create the new window at the very bottom, except when
2372 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002373 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002374 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002376 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002378 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002380 /*
2381 * For the location list window, create a reference to the
2382 * location list from the window 'win'.
2383 */
2384 curwin->w_llist_ref = win->w_llist;
2385 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002387
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002388 if (oldwin != curwin)
2389 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002390 if (qf_buf != NULL)
2391 /* Use the existing quickfix buffer */
2392 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002393 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002394 else
2395 {
2396 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002397 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002398 /* switch off 'swapfile' */
2399 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2400 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002401 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002402 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002403 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002404#ifdef FEAT_DIFF
2405 curwin->w_p_diff = FALSE;
2406#endif
2407#ifdef FEAT_FOLDING
2408 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2409 OPT_LOCAL);
2410#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002413 /* Only set the height when still in the same tab page and there is no
2414 * window to the side. */
2415 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002416#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002417 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002418#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002419 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 win_setheight(height);
2421 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2422 if (win_valid(win))
2423 prevwin = win;
2424 }
2425
2426 /*
2427 * Fill the buffer with the quickfix list.
2428 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002429 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002431 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002432 qf_set_title(qi);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002433
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002434 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 curwin->w_cursor.col = 0;
2436 check_cursor();
2437 update_topline(); /* scroll to show the line */
2438}
2439
2440/*
2441 * Return the number of the current entry (line number in the quickfix
2442 * window).
2443 */
2444 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002445qf_current_entry(wp)
2446 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002448 qf_info_T *qi = &ql_info;
2449
2450 if (IS_LL_WINDOW(wp))
2451 /* In the location list window, use the referenced location list */
2452 qi = wp->w_llist_ref;
2453
2454 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455}
2456
2457/*
2458 * Update the cursor position in the quickfix window to the current error.
2459 * Return TRUE if there is a quickfix window.
2460 */
2461 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002462qf_win_pos_update(qi, old_qf_index)
2463 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 int old_qf_index; /* previous qf_index or zero */
2465{
2466 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002467 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
2469 /*
2470 * Put the cursor on the current error in the quickfix window, so that
2471 * it's viewable.
2472 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002473 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (win != NULL
2475 && qf_index <= win->w_buffer->b_ml.ml_line_count
2476 && old_qf_index != qf_index)
2477 {
2478 win_T *old_curwin = curwin;
2479
2480 curwin = win;
2481 curbuf = win->w_buffer;
2482 if (qf_index > old_qf_index)
2483 {
2484 curwin->w_redraw_top = old_qf_index;
2485 curwin->w_redraw_bot = qf_index;
2486 }
2487 else
2488 {
2489 curwin->w_redraw_top = qf_index;
2490 curwin->w_redraw_bot = old_qf_index;
2491 }
2492 curwin->w_cursor.lnum = qf_index;
2493 curwin->w_cursor.col = 0;
2494 update_topline(); /* scroll to show the line */
2495 redraw_later(VALID);
2496 curwin->w_redr_status = TRUE; /* update ruler */
2497 curwin = old_curwin;
2498 curbuf = curwin->w_buffer;
2499 }
2500 return win != NULL;
2501}
2502
2503/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002504 * Check whether the given window is displaying the specified quickfix/location
2505 * list buffer
2506 */
2507 static int
2508is_qf_win(win, qi)
2509 win_T *win;
2510 qf_info_T *qi;
2511{
2512 /*
2513 * A window displaying the quickfix buffer will have the w_llist_ref field
2514 * set to NULL.
2515 * A window displaying a location list buffer will have the w_llist_ref
2516 * pointing to the location list.
2517 */
2518 if (bt_quickfix(win->w_buffer))
2519 if ((qi == &ql_info && win->w_llist_ref == NULL)
2520 || (qi != &ql_info && win->w_llist_ref == qi))
2521 return TRUE;
2522
2523 return FALSE;
2524}
2525
2526/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002527 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002528 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002529 */
2530 static win_T *
2531qf_find_win(qi)
2532 qf_info_T *qi;
2533{
2534 win_T *win;
2535
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002536 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002537 if (is_qf_win(win, qi))
2538 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002539
2540 return win;
2541}
2542
2543/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002544 * Find a quickfix buffer.
2545 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 */
2547 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002548qf_find_buf(qi)
2549 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002551 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002552 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar9c102382006-05-03 21:26:49 +00002554 FOR_ALL_TAB_WINDOWS(tp, win)
2555 if (is_qf_win(win, qi))
2556 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002557
Bram Moolenaar9c102382006-05-03 21:26:49 +00002558 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559}
2560
2561/*
2562 * Find the quickfix buffer. If it exists, update the contents.
2563 */
2564 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002565qf_update_buffer(qi)
2566 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
2568 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002569 win_T *win;
2570 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002574 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (buf != NULL)
2576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 /* set curwin/curbuf to buf and save a few things */
2578 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002580 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002582 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL
2583 && (win = qf_find_win(qi)) != NULL)
2584 {
2585 curwin_save = curwin;
2586 curwin = win;
2587 qf_set_title(qi);
2588 curwin = curwin_save;
2589
2590 }
2591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 /* restore curwin/curbuf and a few other things */
2593 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002595 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597}
2598
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002599 static void
2600qf_set_title(qi)
2601 qf_info_T *qi;
2602{
2603 set_internal_string_var((char_u *)"w:quickfix_title",
2604 qi->qf_lists[qi->qf_curlist].qf_title);
2605}
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607/*
2608 * Fill current buffer with quickfix errors, replacing any previous contents.
2609 * curbuf must be the quickfix buffer!
2610 */
2611 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002612qf_fill_buffer(qi)
2613 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002615 linenr_T lnum;
2616 qfline_T *qfp;
2617 buf_T *errbuf;
2618 int len;
2619 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621 /* delete all existing lines */
2622 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2623 (void)ml_delete((linenr_T)1, FALSE);
2624
2625 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002626 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
2628 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002629 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2630 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 if (qfp->qf_fnum != 0
2633 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2634 && errbuf->b_fname != NULL)
2635 {
2636 if (qfp->qf_type == 1) /* :helpgrep */
2637 STRCPY(IObuff, gettail(errbuf->b_fname));
2638 else
2639 STRCPY(IObuff, errbuf->b_fname);
2640 len = (int)STRLEN(IObuff);
2641 }
2642 else
2643 len = 0;
2644 IObuff[len++] = '|';
2645
2646 if (qfp->qf_lnum > 0)
2647 {
2648 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2649 len += (int)STRLEN(IObuff + len);
2650
2651 if (qfp->qf_col > 0)
2652 {
2653 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2654 len += (int)STRLEN(IObuff + len);
2655 }
2656
2657 sprintf((char *)IObuff + len, "%s",
2658 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2659 len += (int)STRLEN(IObuff + len);
2660 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002661 else if (qfp->qf_pattern != NULL)
2662 {
2663 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2664 len += (int)STRLEN(IObuff + len);
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 IObuff[len++] = '|';
2667 IObuff[len++] = ' ';
2668
2669 /* Remove newlines and leading whitespace from the text.
2670 * For an unrecognized line keep the indent, the compiler may
2671 * mark a word with ^^^^. */
2672 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2673 IObuff + len, IOSIZE - len);
2674
2675 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2676 == FAIL)
2677 break;
2678 qfp = qfp->qf_next;
2679 }
2680 /* Delete the empty line which is now at the end */
2681 (void)ml_delete(lnum + 1, FALSE);
2682 }
2683
2684 /* correct cursor position */
2685 check_lnums(TRUE);
2686
2687 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2688 * resembles reading a file into a buffer, it's more logical when using
2689 * autocommands. */
2690 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2691 curbuf->b_p_ma = FALSE;
2692
2693#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002694 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2696 FALSE, curbuf);
2697 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2698 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002699 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700#endif
2701
2702 /* make sure it will be redrawn */
2703 redraw_curbuf_later(NOT_VALID);
2704
2705 /* Restore KeyTyped, setting 'filetype' may reset it. */
2706 KeyTyped = old_KeyTyped;
2707}
2708
2709#endif /* FEAT_WINDOWS */
2710
2711/*
2712 * Return TRUE if "buf" is the quickfix buffer.
2713 */
2714 int
2715bt_quickfix(buf)
2716 buf_T *buf;
2717{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002718 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719}
2720
2721/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002722 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2723 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 */
2725 int
2726bt_nofile(buf)
2727 buf_T *buf;
2728{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002729 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2730 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731}
2732
2733/*
2734 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2735 */
2736 int
2737bt_dontwrite(buf)
2738 buf_T *buf;
2739{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002740 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741}
2742
2743 int
2744bt_dontwrite_msg(buf)
2745 buf_T *buf;
2746{
2747 if (bt_dontwrite(buf))
2748 {
2749 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2750 return TRUE;
2751 }
2752 return FALSE;
2753}
2754
2755/*
2756 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2757 * and 'bufhidden'.
2758 */
2759 int
2760buf_hide(buf)
2761 buf_T *buf;
2762{
2763 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2764 switch (buf->b_p_bh[0])
2765 {
2766 case 'u': /* "unload" */
2767 case 'w': /* "wipe" */
2768 case 'd': return FALSE; /* "delete" */
2769 case 'h': return TRUE; /* "hide" */
2770 }
2771 return (p_hid || cmdmod.hide);
2772}
2773
2774/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002775 * Return TRUE when using ":vimgrep" for ":grep".
2776 */
2777 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002778grep_internal(cmdidx)
2779 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002780{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002781 return ((cmdidx == CMD_grep
2782 || cmdidx == CMD_lgrep
2783 || cmdidx == CMD_grepadd
2784 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002785 && STRCMP("internal",
2786 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2787}
2788
2789/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002790 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 */
2792 void
2793ex_make(eap)
2794 exarg_T *eap;
2795{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002796 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 char_u *cmd;
2798 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002799 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002800 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002801 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002802#ifdef FEAT_AUTOCMD
2803 char_u *au_name = NULL;
2804
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002805 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2806 if (grep_internal(eap->cmdidx))
2807 {
2808 ex_vimgrep(eap);
2809 return;
2810 }
2811
Bram Moolenaar7c626922005-02-07 22:01:03 +00002812 switch (eap->cmdidx)
2813 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002814 case CMD_make: au_name = (char_u *)"make"; break;
2815 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2816 case CMD_grep: au_name = (char_u *)"grep"; break;
2817 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2818 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2819 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002820 default: break;
2821 }
2822 if (au_name != NULL)
2823 {
2824 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2825 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002826# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002827 if (did_throw || force_abort)
2828 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002829# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002830 }
2831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
Bram Moolenaara6557602006-02-04 22:43:20 +00002833 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2834 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002835 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002836
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002838 fname = get_mef_name();
2839 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002841 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842
2843 /*
2844 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2845 */
2846 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2847 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002848 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 cmd = alloc(len);
2850 if (cmd == NULL)
2851 return;
2852 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2853 (char *)p_shq);
2854 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002855 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 /*
2857 * Output a newline if there's something else than the :make command that
2858 * was typed (in which case the cursor is in column 0).
2859 */
2860 if (msg_col == 0)
2861 msg_didout = FALSE;
2862 msg_start();
2863 MSG_PUTS(":!");
2864 msg_outtrans(cmd); /* show what we are doing */
2865
2866 /* let the shell know if we are redirecting output or not */
2867 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2868
2869#ifdef AMIGA
2870 out_flush();
2871 /* read window status report and redraw before message */
2872 (void)char_avail();
2873#endif
2874
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002875 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002876 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2877 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002878 && eap->cmdidx != CMD_lgrepadd),
2879 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002880 if (wp != NULL)
2881 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002882#ifdef FEAT_AUTOCMD
2883 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002884 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002885 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2886 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002887 if (qi->qf_curlist < qi->qf_listcount)
2888 res = qi->qf_lists[qi->qf_curlist].qf_count;
2889 else
2890 res = 0;
2891 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002892#endif
2893 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002894 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaar7c626922005-02-07 22:01:03 +00002896 mch_remove(fname);
2897 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 vim_free(cmd);
2899}
2900
2901/*
2902 * Return the name for the errorfile, in allocated memory.
2903 * Find a new unique name when 'makeef' contains "##".
2904 * Returns NULL for error.
2905 */
2906 static char_u *
2907get_mef_name()
2908{
2909 char_u *p;
2910 char_u *name;
2911 static int start = -1;
2912 static int off = 0;
2913#ifdef HAVE_LSTAT
2914 struct stat sb;
2915#endif
2916
2917 if (*p_mef == NUL)
2918 {
2919 name = vim_tempname('e');
2920 if (name == NULL)
2921 EMSG(_(e_notmp));
2922 return name;
2923 }
2924
2925 for (p = p_mef; *p; ++p)
2926 if (p[0] == '#' && p[1] == '#')
2927 break;
2928
2929 if (*p == NUL)
2930 return vim_strsave(p_mef);
2931
2932 /* Keep trying until the name doesn't exist yet. */
2933 for (;;)
2934 {
2935 if (start == -1)
2936 start = mch_get_pid();
2937 else
2938 off += 19;
2939
2940 name = alloc((unsigned)STRLEN(p_mef) + 30);
2941 if (name == NULL)
2942 break;
2943 STRCPY(name, p_mef);
2944 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2945 STRCAT(name, p + 2);
2946 if (mch_getperm(name) < 0
2947#ifdef HAVE_LSTAT
2948 /* Don't accept a symbolic link, its a security risk. */
2949 && mch_lstat((char *)name, &sb) < 0
2950#endif
2951 )
2952 break;
2953 vim_free(name);
2954 }
2955 return name;
2956}
2957
2958/*
2959 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002960 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 */
2962 void
2963ex_cc(eap)
2964 exarg_T *eap;
2965{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002966 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002967
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002968 if (eap->cmdidx == CMD_ll
2969 || eap->cmdidx == CMD_lrewind
2970 || eap->cmdidx == CMD_lfirst
2971 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002972 {
2973 qi = GET_LOC_LIST(curwin);
2974 if (qi == NULL)
2975 {
2976 EMSG(_(e_loclist));
2977 return;
2978 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002979 }
2980
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002981 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 eap->addr_count > 0
2983 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002984 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002986 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2987 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 ? 1
2989 : 32767,
2990 eap->forceit);
2991}
2992
2993/*
2994 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002995 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 */
2997 void
2998ex_cnext(eap)
2999 exarg_T *eap;
3000{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003001 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003002
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003003 if (eap->cmdidx == CMD_lnext
3004 || eap->cmdidx == CMD_lNext
3005 || eap->cmdidx == CMD_lprevious
3006 || eap->cmdidx == CMD_lnfile
3007 || eap->cmdidx == CMD_lNfile
3008 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003009 {
3010 qi = GET_LOC_LIST(curwin);
3011 if (qi == NULL)
3012 {
3013 EMSG(_(e_loclist));
3014 return;
3015 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003016 }
3017
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003018 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003020 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003022 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3023 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 ? BACKWARD_FILE
3025 : BACKWARD,
3026 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
3027}
3028
3029/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003030 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003031 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 */
3033 void
3034ex_cfile(eap)
3035 exarg_T *eap;
3036{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003037 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003038 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003039#ifdef FEAT_AUTOCMD
3040 char_u *au_name = NULL;
3041#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003042
3043 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003044 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003045 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003046
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003047#ifdef FEAT_AUTOCMD
3048 switch (eap->cmdidx)
3049 {
3050 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3051 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3052 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3053 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3054 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3055 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3056 default: break;
3057 }
3058 if (au_name != NULL)
3059 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3060#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003061#ifdef FEAT_BROWSE
3062 if (cmdmod.browse)
3063 {
3064 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3065 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3066 if (browse_file == NULL)
3067 return;
3068 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3069 vim_free(browse_file);
3070 }
3071 else
3072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003074 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003075
3076 /*
3077 * This function is used by the :cfile, :cgetfile and :caddfile
3078 * commands.
3079 * :cfile always creates a new quickfix list and jumps to the
3080 * first error.
3081 * :cgetfile creates a new quickfix list but doesn't jump to the
3082 * first error.
3083 * :caddfile adds to an existing quickfix list. If there is no
3084 * quickfix list then a new list is created.
3085 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003086 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003087 && eap->cmdidx != CMD_laddfile),
3088 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003089 && (eap->cmdidx == CMD_cfile
3090 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003091 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003092#ifdef FEAT_AUTOCMD
3093 if (au_name != NULL)
3094 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3095#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003096 if (wp != NULL)
3097 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003098 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003099 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003100
3101 else
3102 {
3103#ifdef FEAT_AUTOCMD
3104 if (au_name != NULL)
3105 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3106#endif
3107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108}
3109
3110/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003111 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003112 * ":vimgrepadd {pattern} file(s)"
3113 * ":lvimgrep {pattern} file(s)"
3114 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003115 */
3116 void
3117ex_vimgrep(eap)
3118 exarg_T *eap;
3119{
Bram Moolenaar81695252004-12-29 20:58:21 +00003120 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003121 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003122 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003123 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003124 char_u *s;
3125 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003126 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003127 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003128#ifdef FEAT_AUTOCMD
3129 qfline_T *cur_qf_start;
3130#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003131 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003132 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003133 buf_T *buf;
3134 int duplicate_name = FALSE;
3135 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003136 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003137 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003138 buf_T *first_match_buf = NULL;
3139 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003140 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003141#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3142 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003143#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003144 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003145 int flags = 0;
3146 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003147 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003148 char_u *dirname_start = NULL;
3149 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003150 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003151#ifdef FEAT_AUTOCMD
3152 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003153
3154 switch (eap->cmdidx)
3155 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003156 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3157 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3158 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003159 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003160 case CMD_grep: au_name = (char_u *)"grep"; break;
3161 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3162 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3163 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003164 default: break;
3165 }
3166 if (au_name != NULL)
3167 {
3168 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3169 curbuf->b_fname, TRUE, curbuf);
3170 if (did_throw || force_abort)
3171 return;
3172 }
3173#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003174
Bram Moolenaar754b5602006-02-09 23:53:20 +00003175 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003176 || eap->cmdidx == CMD_lvimgrep
3177 || eap->cmdidx == CMD_lgrepadd
3178 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003179 {
3180 qi = ll_get_or_alloc_list(curwin);
3181 if (qi == NULL)
3182 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003183 }
3184
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003185 if (eap->addr_count > 0)
3186 tomatch = eap->line2;
3187 else
3188 tomatch = MAXLNUM;
3189
Bram Moolenaar81695252004-12-29 20:58:21 +00003190 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003191 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003192 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003193 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003194 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003195 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003196 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003197 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003198
3199 if (s != NULL && *s == NUL)
3200 {
3201 /* Pattern is empty, use last search pattern. */
3202 if (last_search_pat() == NULL)
3203 {
3204 EMSG(_(e_noprevre));
3205 goto theend;
3206 }
3207 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3208 }
3209 else
3210 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3211
Bram Moolenaar86b68352004-12-27 21:59:20 +00003212 if (regmatch.regprog == NULL)
3213 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003214 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003215 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003216
3217 p = skipwhite(p);
3218 if (*p == NUL)
3219 {
3220 EMSG(_("E683: File name missing or invalid pattern"));
3221 goto theend;
3222 }
3223
Bram Moolenaar754b5602006-02-09 23:53:20 +00003224 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003225 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003226 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003227 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01003228 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003229 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003230 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003231 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003232 prevp->qf_next != prevp; prevp = prevp->qf_next)
3233 ;
3234
3235 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003236 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003237 goto theend;
3238 if (fcount == 0)
3239 {
3240 EMSG(_(e_nomatch));
3241 goto theend;
3242 }
3243
Bram Moolenaard9462e32011-04-11 21:35:11 +02003244 dirname_start = alloc(MAXPATHL);
3245 dirname_now = alloc(MAXPATHL);
3246 if (dirname_start == NULL || dirname_now == NULL)
3247 goto theend;
3248
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003249 /* Remember the current directory, because a BufRead autocommand that does
3250 * ":lcd %:p:h" changes the meaning of short path names. */
3251 mch_dirname(dirname_start, MAXPATHL);
3252
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003253#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003254 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003255 * changing the current quickfix list. */
3256 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3257#endif
3258
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003259 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003260 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003261 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003262 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003263 if (time(NULL) > seconds)
3264 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003265 /* Display the file name every second or so, show the user we are
3266 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003267 seconds = time(NULL);
3268 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003269 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003270 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003271 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003272 else
3273 {
3274 msg_outtrans(p);
3275 vim_free(p);
3276 }
3277 msg_clr_eos();
3278 msg_didout = FALSE; /* overwrite this message */
3279 msg_nowait = TRUE; /* don't wait for this message */
3280 msg_col = 0;
3281 out_flush();
3282 }
3283
Bram Moolenaar81695252004-12-29 20:58:21 +00003284 buf = buflist_findname_exp(fnames[fi]);
3285 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3286 {
3287 /* Remember that a buffer with this name already exists. */
3288 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003289 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003290 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003291
3292#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3293 /* Don't do Filetype autocommands to avoid loading syntax and
3294 * indent scripts, a great speed improvement. */
3295 save_ei = au_event_disable(",Filetype");
3296#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003297 /* Don't use modelines here, it's useless. */
3298 save_mls = p_mls;
3299 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003300
3301 /* Load file into a buffer, so that 'fileencoding' is detected,
3302 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003303 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003304
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003305 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003306#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3307 au_event_restore(save_ei);
3308#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003309 }
3310 else
3311 /* Use existing, loaded buffer. */
3312 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003313
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003314#ifdef FEAT_AUTOCMD
3315 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3316 {
3317 int idx;
3318
3319 /* Autocommands changed the quickfix list. Find the one we were
3320 * using and restore it. */
3321 for (idx = 0; idx < LISTCOUNT; ++idx)
3322 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3323 {
3324 qi->qf_curlist = idx;
3325 break;
3326 }
3327 if (idx == LISTCOUNT)
3328 {
3329 /* List cannot be found, create a new one. */
3330 qf_new_list(qi, *eap->cmdlinep);
3331 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3332 }
3333 }
3334#endif
3335
Bram Moolenaar81695252004-12-29 20:58:21 +00003336 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003337 {
3338 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003339 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003340 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003341 else
3342 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003343 /* Try for a match in all lines of the buffer.
3344 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003345 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003346 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3347 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003348 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003349 col = 0;
3350 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003351 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003352 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003353 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003354 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003355 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003356 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003357 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003358 ml_get_buf(buf,
3359 regmatch.startpos[0].lnum + lnum, FALSE),
3360 regmatch.startpos[0].lnum + lnum,
3361 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003362 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003363 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003364 0, /* nr */
3365 0, /* type */
3366 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003367 ) == FAIL)
3368 {
3369 got_int = TRUE;
3370 break;
3371 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003372 found_match = TRUE;
3373 if (--tomatch == 0)
3374 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003375 if ((flags & VGR_GLOBAL) == 0
3376 || regmatch.endpos[0].lnum > 0)
3377 break;
3378 col = regmatch.endpos[0].col
3379 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003380 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003381 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003382 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003383 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003384 if (got_int)
3385 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003386 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003387#ifdef FEAT_AUTOCMD
3388 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3389#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003390
3391 if (using_dummy)
3392 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003393 if (found_match && first_match_buf == NULL)
3394 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003395 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003396 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003397 /* Never keep a dummy buffer if there is another buffer
3398 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003399 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003400 buf = NULL;
3401 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003402 else if (!cmdmod.hide
3403 || buf->b_p_bh[0] == 'u' /* "unload" */
3404 || buf->b_p_bh[0] == 'w' /* "wipe" */
3405 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003406 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003407 /* When no match was found we don't need to remember the
3408 * buffer, wipe it out. If there was a match and it
3409 * wasn't the first one or we won't jump there: only
3410 * unload the buffer.
3411 * Ignore 'hidden' here, because it may lead to having too
3412 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003413 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003414 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003415 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003416 buf = NULL;
3417 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003418 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003419 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003420 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003421 buf = NULL;
3422 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003423 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003424
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003425 if (buf != NULL)
3426 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003427 /* If the buffer is still loaded we need to use the
3428 * directory we jumped to below. */
3429 if (buf == first_match_buf
3430 && target_dir == NULL
3431 && STRCMP(dirname_start, dirname_now) != 0)
3432 target_dir = vim_strsave(dirname_now);
3433
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003434 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003435 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003436 * need to be done (again). But not the window-local
3437 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003438 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003439#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003440 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3441 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003442#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003443 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003444 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003445 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003446 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003447 }
3448 }
3449
3450 FreeWild(fcount, fnames);
3451
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003452 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3453 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3454 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003455
3456#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003457 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003458#endif
3459
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003460#ifdef FEAT_AUTOCMD
3461 if (au_name != NULL)
3462 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3463 curbuf->b_fname, TRUE, curbuf);
3464#endif
3465
Bram Moolenaar86b68352004-12-27 21:59:20 +00003466 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003467 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003468 {
3469 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003470 {
3471 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003472 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003473 if (buf != curbuf)
3474 /* If we jumped to another buffer redrawing will already be
3475 * taken care of. */
3476 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003477
3478 /* Jump to the directory used after loading the buffer. */
3479 if (curbuf == first_match_buf && target_dir != NULL)
3480 {
3481 exarg_T ea;
3482
3483 ea.arg = target_dir;
3484 ea.cmdidx = CMD_lcd;
3485 ex_cd(&ea);
3486 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003487 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003488 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003489 else
3490 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003491
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003492 /* If we loaded a dummy buffer into the current window, the autocommands
3493 * may have messed up things, need to redraw and recompute folds. */
3494 if (redraw_for_dummy)
3495 {
3496#ifdef FEAT_FOLDING
3497 foldUpdateAll(curwin);
3498#else
3499 redraw_later(NOT_VALID);
3500#endif
3501 }
3502
Bram Moolenaar86b68352004-12-27 21:59:20 +00003503theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003504 vim_free(dirname_now);
3505 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003506 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003507 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003508}
3509
3510/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003511 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003512 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003513 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3514 * If "s" is not NULL terminate the pattern with a NUL.
3515 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003516 */
3517 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003518skip_vimgrep_pat(p, s, flags)
3519 char_u *p;
3520 char_u **s;
3521 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003522{
3523 int c;
3524
3525 if (vim_isIDc(*p))
3526 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003527 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003528 if (s != NULL)
3529 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003530 p = skiptowhite(p);
3531 if (s != NULL && *p != NUL)
3532 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003533 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003534 else
3535 {
3536 /* ":vimgrep /pattern/[g][j] fname" */
3537 if (s != NULL)
3538 *s = p + 1;
3539 c = *p;
3540 p = skip_regexp(p + 1, c, TRUE, NULL);
3541 if (*p != c)
3542 return NULL;
3543
3544 /* Truncate the pattern. */
3545 if (s != NULL)
3546 *p = NUL;
3547 ++p;
3548
3549 /* Find the flags */
3550 while (*p == 'g' || *p == 'j')
3551 {
3552 if (flags != NULL)
3553 {
3554 if (*p == 'g')
3555 *flags |= VGR_GLOBAL;
3556 else
3557 *flags |= VGR_NOJUMP;
3558 }
3559 ++p;
3560 }
3561 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003562 return p;
3563}
3564
3565/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003566 * Restore current working directory to "dirname_start" if they differ, taking
3567 * into account whether it is set locally or globally.
3568 */
3569 static void
3570restore_start_dir(dirname_start)
3571 char_u *dirname_start;
3572{
3573 char_u *dirname_now = alloc(MAXPATHL);
3574
3575 if (NULL != dirname_now)
3576 {
3577 mch_dirname(dirname_now, MAXPATHL);
3578 if (STRCMP(dirname_start, dirname_now) != 0)
3579 {
3580 /* If the directory has changed, change it back by building up an
3581 * appropriate ex command and executing it. */
3582 exarg_T ea;
3583
3584 ea.arg = dirname_start;
3585 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3586 ex_cd(&ea);
3587 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01003588 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003589 }
3590}
3591
3592/*
3593 * Load file "fname" into a dummy buffer and return the buffer pointer,
3594 * placing the directory resulting from the buffer load into the
3595 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3596 * prior to calling this function. Restores directory to "dirname_start" prior
3597 * to returning, if autocmds or the 'autochdir' option have changed it.
3598 *
3599 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3600 * or wipe_dummy_buffer() later!
3601 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003602 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003603 */
3604 static buf_T *
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003605load_dummy_buffer(fname, dirname_start, resulting_dir)
Bram Moolenaar81695252004-12-29 20:58:21 +00003606 char_u *fname;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003607 char_u *dirname_start; /* in: old directory */
3608 char_u *resulting_dir; /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003609{
3610 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003611 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003612 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003613 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003614
3615 /* Allocate a buffer without putting it in the buffer list. */
3616 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3617 if (newbuf == NULL)
3618 return NULL;
3619
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003620 /* Init the options. */
3621 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3622
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003623 /* need to open the memfile before putting the buffer in a window */
3624 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003625 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003626 /* set curwin/curbuf to buf and save a few things */
3627 aucmd_prepbuf(&aco, newbuf);
3628
3629 /* Need to set the filename for autocommands. */
3630 (void)setfname(curbuf, fname, NULL, FALSE);
3631
Bram Moolenaar81695252004-12-29 20:58:21 +00003632 /* Create swap file now to avoid the ATTENTION message. */
3633 check_need_swap(TRUE);
3634
3635 /* Remove the "dummy" flag, otherwise autocommands may not
3636 * work. */
3637 curbuf->b_flags &= ~BF_DUMMY;
3638
3639 if (readfile(fname, NULL,
3640 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3641 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003642 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003643 && !(curbuf->b_flags & BF_NEW))
3644 {
3645 failed = FALSE;
3646 if (curbuf != newbuf)
3647 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003648 /* Bloody autocommands changed the buffer! Can happen when
3649 * using netrw and editing a remote file. Use the current
3650 * buffer instead, delete the dummy one after restoring the
3651 * window stuff. */
3652 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003653 newbuf = curbuf;
3654 }
3655 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003656
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003657 /* restore curwin/curbuf and a few other things */
3658 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003659 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3660 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003661 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003662
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003663 /*
3664 * When autocommands/'autochdir' option changed directory: go back.
3665 * Let the caller know what the resulting dir was first, in case it is
3666 * important.
3667 */
3668 mch_dirname(resulting_dir, MAXPATHL);
3669 restore_start_dir(dirname_start);
3670
Bram Moolenaar81695252004-12-29 20:58:21 +00003671 if (!buf_valid(newbuf))
3672 return NULL;
3673 if (failed)
3674 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003675 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003676 return NULL;
3677 }
3678 return newbuf;
3679}
3680
3681/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003682 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3683 * directory to "dirname_start" prior to returning, if autocmds or the
3684 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003685 */
3686 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003687wipe_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003688 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003689 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003690{
3691 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003692 {
3693#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3694 cleanup_T cs;
3695
3696 /* Reset the error/interrupt/exception state here so that aborting()
3697 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3698 * work when got_int is set. */
3699 enter_cleanup(&cs);
3700#endif
3701
Bram Moolenaar81695252004-12-29 20:58:21 +00003702 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003703
3704#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3705 /* Restore the error/interrupt/exception state if not discarded by a
3706 * new aborting error, interrupt, or uncaught exception. */
3707 leave_cleanup(&cs);
3708#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003709 /* When autocommands/'autochdir' option changed directory: go back. */
3710 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003711 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003712}
3713
3714/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003715 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3716 * directory to "dirname_start" prior to returning, if autocmds or the
3717 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003718 */
3719 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003720unload_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003721 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003722 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003723{
3724 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003725 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003726 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003727
3728 /* When autocommands/'autochdir' option changed directory: go back. */
3729 restore_start_dir(dirname_start);
3730 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003731}
3732
Bram Moolenaar05159a02005-02-26 23:04:13 +00003733#if defined(FEAT_EVAL) || defined(PROTO)
3734/*
3735 * Add each quickfix error to list "list" as a dictionary.
3736 */
3737 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003738get_errorlist(wp, list)
3739 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003740 list_T *list;
3741{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003742 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003743 dict_T *dict;
3744 char_u buf[2];
3745 qfline_T *qfp;
3746 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003747 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003748
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003749 if (wp != NULL)
3750 {
3751 qi = GET_LOC_LIST(wp);
3752 if (qi == NULL)
3753 return FAIL;
3754 }
3755
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003756 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003757 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003758 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003759
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003760 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3761 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003762 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003763 /* Handle entries with a non-existing buffer number. */
3764 bufnum = qfp->qf_fnum;
3765 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3766 bufnum = 0;
3767
Bram Moolenaar05159a02005-02-26 23:04:13 +00003768 if ((dict = dict_alloc()) == NULL)
3769 return FAIL;
3770 if (list_append_dict(list, dict) == FAIL)
3771 return FAIL;
3772
3773 buf[0] = qfp->qf_type;
3774 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003775 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003776 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3777 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3778 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3779 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003780 || dict_add_nr_str(dict, "pattern", 0L,
3781 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3782 || dict_add_nr_str(dict, "text", 0L,
3783 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003784 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3785 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3786 return FAIL;
3787
3788 qfp = qfp->qf_next;
3789 }
3790 return OK;
3791}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003792
3793/*
3794 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003795 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003796 */
3797 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003798set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003799 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003800 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003801 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003802 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003803{
3804 listitem_T *li;
3805 dict_T *d;
3806 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003807 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003808 long lnum;
3809 int col, nr;
3810 int vcol;
3811 qfline_T *prevp = NULL;
3812 int valid, status;
3813 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003814 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003815 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003816
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003817 if (wp != NULL)
3818 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003819 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003820 if (qi == NULL)
3821 return FAIL;
3822 }
3823
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003824 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003825 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01003826 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003827 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003828 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003829 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003830 prevp->qf_next != prevp; prevp = prevp->qf_next)
3831 ;
3832 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003833 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003834
3835 for (li = list->lv_first; li != NULL; li = li->li_next)
3836 {
3837 if (li->li_tv.v_type != VAR_DICT)
3838 continue; /* Skip non-dict items */
3839
3840 d = li->li_tv.vval.v_dict;
3841 if (d == NULL)
3842 continue;
3843
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003844 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003845 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003846 lnum = get_dict_number(d, (char_u *)"lnum");
3847 col = get_dict_number(d, (char_u *)"col");
3848 vcol = get_dict_number(d, (char_u *)"vcol");
3849 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003850 type = get_dict_string(d, (char_u *)"type", TRUE);
3851 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3852 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003853 if (text == NULL)
3854 text = vim_strsave((char_u *)"");
3855
3856 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003857 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003858 valid = FALSE;
3859
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003860 /* Mark entries with non-existing buffer number as not valid. Give the
3861 * error message only once. */
3862 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3863 {
3864 if (!did_bufnr_emsg)
3865 {
3866 did_bufnr_emsg = TRUE;
3867 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3868 }
3869 valid = FALSE;
3870 bufnum = 0;
3871 }
3872
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003873 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003874 NULL, /* dir */
3875 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003876 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003877 text,
3878 lnum,
3879 col,
3880 vcol, /* vis_col */
3881 pattern, /* search pattern */
3882 nr,
3883 type == NULL ? NUL : *type,
3884 valid);
3885
3886 vim_free(filename);
3887 vim_free(pattern);
3888 vim_free(text);
3889 vim_free(type);
3890
3891 if (status == FAIL)
3892 {
3893 retval = FAIL;
3894 break;
3895 }
3896 }
3897
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003898 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003899 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003900 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3901 else
3902 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003903 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3904 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003905
3906#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003907 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003908#endif
3909
3910 return retval;
3911}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003912#endif
3913
Bram Moolenaar81695252004-12-29 20:58:21 +00003914/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003915 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003916 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003917 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003918 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003919 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003920 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003921 */
3922 void
3923ex_cbuffer(eap)
3924 exarg_T *eap;
3925{
3926 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003927 qf_info_T *qi = &ql_info;
3928
Bram Moolenaardb552d602006-03-23 22:59:57 +00003929 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3930 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003931 {
3932 qi = ll_get_or_alloc_list(curwin);
3933 if (qi == NULL)
3934 return;
3935 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003936
3937 if (*eap->arg == NUL)
3938 buf = curbuf;
3939 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3940 buf = buflist_findnr(atoi((char *)eap->arg));
3941 if (buf == NULL)
3942 EMSG(_(e_invarg));
3943 else if (buf->b_ml.ml_mfp == NULL)
3944 EMSG(_("E681: Buffer is not loaded"));
3945 else
3946 {
3947 if (eap->addr_count == 0)
3948 {
3949 eap->line1 = 1;
3950 eap->line2 = buf->b_ml.ml_line_count;
3951 }
3952 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3953 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3954 EMSG(_(e_invrange));
3955 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003956 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003957 char_u *qf_title = *eap->cmdlinep;
3958
3959 if (buf->b_sfname)
3960 {
3961 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3962 (char *)qf_title, (char *)buf->b_sfname);
3963 qf_title = IObuff;
3964 }
3965
Bram Moolenaardb552d602006-03-23 22:59:57 +00003966 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3967 (eap->cmdidx != CMD_caddbuffer
3968 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003969 eap->line1, eap->line2,
3970 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003971 && (eap->cmdidx == CMD_cbuffer
3972 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003973 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3974 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003975 }
3976}
3977
Bram Moolenaar1e015462005-09-25 22:16:38 +00003978#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003979/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003980 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3981 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003982 */
3983 void
3984ex_cexpr(eap)
3985 exarg_T *eap;
3986{
3987 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003988 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003989
Bram Moolenaardb552d602006-03-23 22:59:57 +00003990 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3991 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003992 {
3993 qi = ll_get_or_alloc_list(curwin);
3994 if (qi == NULL)
3995 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003996 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003997
Bram Moolenaar4770d092006-01-12 23:22:24 +00003998 /* Evaluate the expression. When the result is a string or a list we can
3999 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004000 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004001 if (tv != NULL)
4002 {
4003 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4004 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4005 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00004006 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
4007 (eap->cmdidx != CMD_caddexpr
4008 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004009 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004010 && (eap->cmdidx == CMD_cexpr
4011 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004012 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004013 }
4014 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004015 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004016 free_tv(tv);
4017 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004018}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004019#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004020
4021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 * ":helpgrep {pattern}"
4023 */
4024 void
4025ex_helpgrep(eap)
4026 exarg_T *eap;
4027{
4028 regmatch_T regmatch;
4029 char_u *save_cpo;
4030 char_u *p;
4031 int fcount;
4032 char_u **fnames;
4033 FILE *fd;
4034 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004035 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004037#ifdef FEAT_MULTI_LANG
4038 char_u *lang;
4039#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004040 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004041 int new_qi = FALSE;
4042 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004043#ifdef FEAT_AUTOCMD
4044 char_u *au_name = NULL;
4045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004047#ifdef FEAT_MULTI_LANG
4048 /* Check for a specified language */
4049 lang = check_help_lang(eap->arg);
4050#endif
4051
Bram Moolenaar73633f82012-01-20 13:39:07 +01004052#ifdef FEAT_AUTOCMD
4053 switch (eap->cmdidx)
4054 {
4055 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4056 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4057 default: break;
4058 }
4059 if (au_name != NULL)
4060 {
4061 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4062 curbuf->b_fname, TRUE, curbuf);
4063 if (did_throw || force_abort)
4064 return;
4065 }
4066#endif
4067
4068 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4069 save_cpo = p_cpo;
4070 p_cpo = empty_option;
4071
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004072 if (eap->cmdidx == CMD_lhelpgrep)
4073 {
4074 /* Find an existing help window */
4075 FOR_ALL_WINDOWS(wp)
4076 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4077 break;
4078
4079 if (wp == NULL) /* Help window not found */
4080 qi = NULL;
4081 else
4082 qi = wp->w_llist;
4083
4084 if (qi == NULL)
4085 {
4086 /* Allocate a new location list for help text matches */
4087 if ((qi = ll_new_list()) == NULL)
4088 return;
4089 new_qi = TRUE;
4090 }
4091 }
4092
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4094 regmatch.rm_ic = FALSE;
4095 if (regmatch.regprog != NULL)
4096 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004097#ifdef FEAT_MBYTE
4098 vimconv_T vc;
4099
4100 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4101 * differs. */
4102 vc.vc_type = CONV_NONE;
4103 if (!enc_utf8)
4104 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4105#endif
4106
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004108 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
4110 /* Go through all directories in 'runtimepath' */
4111 p = p_rtp;
4112 while (*p != NUL && !got_int)
4113 {
4114 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4115
4116 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4117 add_pathsep(NameBuff);
4118 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4119 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4120 &fnames, EW_FILE|EW_SILENT) == OK
4121 && fcount > 0)
4122 {
4123 for (fi = 0; fi < fcount && !got_int; ++fi)
4124 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004125#ifdef FEAT_MULTI_LANG
4126 /* Skip files for a different language. */
4127 if (lang != NULL
4128 && STRNICMP(lang, fnames[fi]
4129 + STRLEN(fnames[fi]) - 3, 2) != 0
4130 && !(STRNICMP(lang, "en", 2) == 0
4131 && STRNICMP("txt", fnames[fi]
4132 + STRLEN(fnames[fi]) - 3, 3) == 0))
4133 continue;
4134#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004135 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 if (fd != NULL)
4137 {
4138 lnum = 1;
4139 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4140 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004141 char_u *line = IObuff;
4142#ifdef FEAT_MBYTE
4143 /* Convert a line if 'encoding' is not utf-8 and
4144 * the line contains a non-ASCII character. */
4145 if (vc.vc_type != CONV_NONE
4146 && has_non_ascii(IObuff)) {
4147 line = string_convert(&vc, IObuff, NULL);
4148 if (line == NULL)
4149 line = IObuff;
4150 }
4151#endif
4152
4153 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004155 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004158 while (l > 0 && line[l - 1] <= ' ')
4159 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004161 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 NULL, /* dir */
4163 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004164 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004165 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004167 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004168 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004169 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004170 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 0, /* nr */
4172 1, /* type */
4173 TRUE /* valid */
4174 ) == FAIL)
4175 {
4176 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004177#ifdef FEAT_MBYTE
4178 if (line != IObuff)
4179 vim_free(line);
4180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 break;
4182 }
4183 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004184#ifdef FEAT_MBYTE
4185 if (line != IObuff)
4186 vim_free(line);
4187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 ++lnum;
4189 line_breakcheck();
4190 }
4191 fclose(fd);
4192 }
4193 }
4194 FreeWild(fcount, fnames);
4195 }
4196 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004197
Bram Moolenaar473de612013-06-08 18:19:48 +02004198 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004199#ifdef FEAT_MBYTE
4200 if (vc.vc_type != CONV_NONE)
4201 convert_setup(&vc, NULL, NULL);
4202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004204 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4205 qi->qf_lists[qi->qf_curlist].qf_ptr =
4206 qi->qf_lists[qi->qf_curlist].qf_start;
4207 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004210 if (p_cpo == empty_option)
4211 p_cpo = save_cpo;
4212 else
4213 /* Darn, some plugin changed the value. */
4214 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004217 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218#endif
4219
Bram Moolenaar73633f82012-01-20 13:39:07 +01004220#ifdef FEAT_AUTOCMD
4221 if (au_name != NULL)
4222 {
4223 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4224 curbuf->b_fname, TRUE, curbuf);
4225 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4226 /* autocommands made "qi" invalid */
4227 return;
4228 }
4229#endif
4230
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004232 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004233 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004234 else
4235 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004236
4237 if (eap->cmdidx == CMD_lhelpgrep)
4238 {
4239 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004240 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004241 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4242 {
4243 if (new_qi)
4244 ll_free_all(&qi);
4245 }
4246 else if (curwin->w_llist == NULL)
4247 curwin->w_llist = qi;
4248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249}
4250
4251#endif /* FEAT_QUICKFIX */