blob: d4db0d253da8452f9a80d3e5ce5ae94911718e68 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
24static struct dir_stack_T *dir_stack = NULL;
25
26/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000029typedef struct qfline_S qfline_T;
30struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000031{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000032 qfline_T *qf_next; /* pointer to next error in the list */
33 qfline_T *qf_prev; /* pointer to previous error in the list */
34 linenr_T qf_lnum; /* line number where the error occurred */
35 int qf_fnum; /* file number for the line */
36 int qf_col; /* column where the error occurred */
37 int qf_nr; /* error number */
38 char_u *qf_pattern; /* search pattern for the error */
39 char_u *qf_text; /* description of the error */
40 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
41 char_u qf_cleared; /* set to TRUE if line has been deleted */
42 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000044 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
47/*
48 * There is a stack of error lists.
49 */
50#define LISTCOUNT 10
51
Bram Moolenaard12f5c12006-01-25 22:10:52 +000052typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_start; /* pointer to the first error */
55 qfline_T *qf_ptr; /* pointer to the current error */
56 int qf_count; /* number of errors (0 means no error list) */
57 int qf_index; /* current index in the error list */
58 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059 char_u *qf_title; /* title derived from the command that created
60 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaard12f5c12006-01-25 22:10:52 +000063struct qf_info_S
64{
65 /*
66 * Count of references to this list. Used only for location lists.
67 * When a location list window reference this list, qf_refcount
68 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
69 * reaches 0, the list is freed.
70 */
71 int qf_refcount;
72 int qf_listcount; /* current number of lists */
73 int qf_curlist; /* current error list */
74 qf_list_T qf_lists[LISTCOUNT];
75};
76
77static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar68b76a62005-03-25 21:53:48 +000079#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/*
82 * Structure used to hold the info of one part of 'errorformat'
83 */
Bram Moolenaar01265852006-03-20 21:50:15 +000084typedef struct efm_S efm_T;
85struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000086{
87 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000088 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
90 char_u prefix; /* prefix of this format line: */
91 /* 'D' enter directory */
92 /* 'X' leave directory */
93 /* 'A' start of multi-line message */
94 /* 'E' error message */
95 /* 'W' warning message */
96 /* 'I' informational message */
97 /* 'C' continuation line */
98 /* 'Z' end of multi-line message */
99 /* 'G' general, unspecific message */
100 /* 'P' push file (partial) message */
101 /* 'Q' pop/quit file (partial) message */
102 /* 'O' overread (partial) message */
103 char_u flags; /* additional flags given in prefix */
104 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000105 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000106 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107};
108
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200109static int qf_init_ext __ARGS((qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title));
Bram Moolenaarfb604092014-07-23 15:55:00 +0200110static void qf_store_title __ARGS((qf_info_T *qi, char_u *title));
Bram Moolenaar94116152012-11-28 17:41:59 +0100111static void qf_new_list __ARGS((qf_info_T *qi, char_u *qf_title));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000112static void ll_free_all __ARGS((qf_info_T **pqi));
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000113static int qf_add_entry __ARGS((qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000114static qf_info_T *ll_new_list __ARGS((void));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static void qf_msg __ARGS((qf_info_T *qi));
116static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117static char_u *qf_types __ARGS((int, int));
118static int qf_get_fnum __ARGS((char_u *, char_u *));
119static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
120static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
121static char_u *qf_guess_filepath __ARGS((char_u *));
122static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
123static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
124#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000125static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
Bram Moolenaar9c102382006-05-03 21:26:49 +0000126static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static win_T *qf_find_win __ARGS((qf_info_T *qi));
128static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
129static void qf_update_buffer __ARGS((qf_info_T *qi));
Bram Moolenaarfb604092014-07-23 15:55:00 +0200130static void qf_set_title_var __ARGS((qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000131static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#endif
133static char_u *get_mef_name __ARGS((void));
Bram Moolenaar7f51a822012-04-25 18:57:21 +0200134static void restore_start_dir __ARGS((char_u *dirname_start));
135static buf_T *load_dummy_buffer __ARGS((char_u *fname, char_u *dirname_start, char_u *resulting_dir));
136static void wipe_dummy_buffer __ARGS((buf_T *buf, char_u *dirname_start));
137static void unload_dummy_buffer __ARGS((buf_T *buf, char_u *dirname_start));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000138static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000140/* Quickfix window check helper macro */
141#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
142/* Location list window check helper macro */
143#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
144/*
145 * Return location list for window 'wp'
146 * For location list window, return the referenced location list
147 */
148#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
149
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000151 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200152 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 * Return -1 for error, number of errors for success.
154 */
155 int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200156qf_init(wp, efile, errorformat, newlist, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000157 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 char_u *efile;
159 char_u *errorformat;
160 int newlist; /* TRUE: start a new error list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200161 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163 qf_info_T *qi = &ql_info;
164
Bram Moolenaar86b68352004-12-27 21:59:20 +0000165 if (efile == NULL)
166 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000167
168 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000169 {
170 qi = ll_get_or_alloc_list(wp);
171 if (qi == NULL)
172 return FAIL;
173 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000174
175 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200176 (linenr_T)0, (linenr_T)0,
177 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000178}
179
180/*
181 * Read the errorfile "efile" into memory, line by line, building the error
182 * list.
183 * Alternative: when "efile" is null read errors from buffer "buf".
184 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200185 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
186 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000187 * Return -1 for error, number of errors for success.
188 */
189 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200190qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast,
191 qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000193 char_u *efile;
194 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000195 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000196 char_u *errorformat;
197 int newlist; /* TRUE: start a new error list */
198 linenr_T lnumfirst; /* first line number to use */
199 linenr_T lnumlast; /* last line number to use */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200200 char_u *qf_title;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000201{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 char_u *namebuf;
203 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000204 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 char_u *fmtstr = NULL;
206 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000207 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 int type = 0;
209 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000210 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 long lnum = 0L;
212 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000213 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000214 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000216 efm_T *fmt_first = NULL;
217 efm_T *fmt_last = NULL;
218 efm_T *fmt_ptr;
219 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 char_u *efm;
221 char_u *ptr;
222 char_u *srcptr;
223 int len;
224 int i;
225 int round;
226 int idx = 0;
227 int multiline = FALSE;
228 int multiignore = FALSE;
229 int multiscan = FALSE;
230 int retval = -1; /* default: return error flag */
231 char_u *directory = NULL;
232 char_u *currfile = NULL;
233 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000234 char_u *p_str = NULL;
235 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 struct dir_stack_T *file_stack = NULL;
237 regmatch_T regmatch;
238 static struct fmtpattern
239 {
240 char_u convchar;
241 char *pattern;
242 } fmt_pat[FMT_PATTERNS] =
243 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000244 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {'n', "\\d\\+"},
246 {'l', "\\d\\+"},
247 {'c', "\\d\\+"},
248 {'t', "."},
249 {'m', ".\\+"},
250 {'r', ".*"},
Bram Moolenaarf13de072012-06-01 18:34:41 +0200251 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000252 {'v', "\\d\\+"},
253 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 };
255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 namebuf = alloc(CMDBUFFSIZE + 1);
257 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000258 pattern = alloc(CMDBUFFSIZE + 1);
259 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 goto qf_init_end;
261
Bram Moolenaar86b68352004-12-27 21:59:20 +0000262 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 {
264 EMSG2(_(e_openerrf), efile);
265 goto qf_init_end;
266 }
267
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000268 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100270 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000271 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000273 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200274 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 ;
276
277/*
278 * Each part of the format string is copied and modified from errorformat to
279 * regex prog. Only a few % characters are allowed.
280 */
281 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000282 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000283 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 else
285 efm = errorformat;
286 /*
287 * Get some space to modify the format string into.
288 */
289 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
290 for (round = FMT_PATTERNS; round > 0; )
291 i += (int)STRLEN(fmt_pat[--round].pattern);
292#ifdef COLON_IN_FILENAME
293 i += 12; /* "%f" can become twelve chars longer */
294#else
295 i += 2; /* "%f" can become two chars longer */
296#endif
297 if ((fmtstr = alloc(i)) == NULL)
298 goto error2;
299
Bram Moolenaar01265852006-03-20 21:50:15 +0000300 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {
302 /*
303 * Allocate a new eformat structure and put it at the end of the list
304 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000305 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 if (fmt_ptr == NULL)
307 goto error2;
308 if (fmt_first == NULL) /* first one */
309 fmt_first = fmt_ptr;
310 else
311 fmt_last->next = fmt_ptr;
312 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
314 /*
315 * Isolate one part in the 'errorformat' option
316 */
317 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
318 if (efm[len] == '\\' && efm[len + 1] != NUL)
319 ++len;
320
321 /*
322 * Build regexp pattern from current 'errorformat' option
323 */
324 ptr = fmtstr;
325 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000326 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 for (efmp = efm; efmp < efm + len; ++efmp)
328 {
329 if (*efmp == '%')
330 {
331 ++efmp;
332 for (idx = 0; idx < FMT_PATTERNS; ++idx)
333 if (fmt_pat[idx].convchar == *efmp)
334 break;
335 if (idx < FMT_PATTERNS)
336 {
337 if (fmt_ptr->addr[idx])
338 {
339 sprintf((char *)errmsg,
340 _("E372: Too many %%%c in format string"), *efmp);
341 EMSG(errmsg);
342 goto error2;
343 }
344 if ((idx
345 && idx < 6
346 && vim_strchr((char_u *)"DXOPQ",
347 fmt_ptr->prefix) != NULL)
348 || (idx == 6
349 && vim_strchr((char_u *)"OPQ",
350 fmt_ptr->prefix) == NULL))
351 {
352 sprintf((char *)errmsg,
353 _("E373: Unexpected %%%c in format string"), *efmp);
354 EMSG(errmsg);
355 goto error2;
356 }
357 fmt_ptr->addr[idx] = (char_u)++round;
358 *ptr++ = '\\';
359 *ptr++ = '(';
360#ifdef BACKSLASH_IN_FILENAME
361 if (*efmp == 'f')
362 {
363 /* Also match "c:" in the file name, even when
364 * checking for a colon next: "%f:".
365 * "\%(\a:\)\=" */
366 STRCPY(ptr, "\\%(\\a:\\)\\=");
367 ptr += 10;
368 }
369#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000370 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000372 if (efmp[1] != '\\' && efmp[1] != '%')
373 {
374 /* A file name may contain spaces, but this isn't
375 * in "\f". For "%f:%l:%m" there may be a ":" in
376 * the file name. Use ".\{-1,}x" instead (x is
377 * the next character), the requirement that :999:
378 * follows should work. */
379 STRCPY(ptr, ".\\{-1,}");
380 ptr += 7;
381 }
382 else
383 {
384 /* File name followed by '\\' or '%': include as
385 * many file name chars as possible. */
386 STRCPY(ptr, "\\f\\+");
387 ptr += 4;
388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 }
390 else
391 {
392 srcptr = (char_u *)fmt_pat[idx].pattern;
393 while ((*ptr = *srcptr++) != NUL)
394 ++ptr;
395 }
396 *ptr++ = '\\';
397 *ptr++ = ')';
398 }
399 else if (*efmp == '*')
400 {
401 if (*++efmp == '[' || *efmp == '\\')
402 {
403 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
404 {
405 if (efmp[1] == '^')
406 *ptr++ = *++efmp;
407 if (efmp < efm + len)
408 {
409 *ptr++ = *++efmp; /* could be ']' */
410 while (efmp < efm + len
411 && (*ptr++ = *++efmp) != ']')
412 /* skip */;
413 if (efmp == efm + len)
414 {
415 EMSG(_("E374: Missing ] in format string"));
416 goto error2;
417 }
418 }
419 }
420 else if (efmp < efm + len) /* %*\D, %*\s etc. */
421 *ptr++ = *++efmp;
422 *ptr++ = '\\';
423 *ptr++ = '+';
424 }
425 else
426 {
427 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
428 sprintf((char *)errmsg,
429 _("E375: Unsupported %%%c in format string"), *efmp);
430 EMSG(errmsg);
431 goto error2;
432 }
433 }
434 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
435 *ptr++ = *efmp; /* regexp magic characters */
436 else if (*efmp == '#')
437 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000438 else if (*efmp == '>')
439 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 else if (efmp == efm + 1) /* analyse prefix */
441 {
442 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
443 fmt_ptr->flags = *efmp++;
444 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
445 fmt_ptr->prefix = *efmp;
446 else
447 {
448 sprintf((char *)errmsg,
449 _("E376: Invalid %%%c in format string prefix"), *efmp);
450 EMSG(errmsg);
451 goto error2;
452 }
453 }
454 else
455 {
456 sprintf((char *)errmsg,
457 _("E377: Invalid %%%c in format string"), *efmp);
458 EMSG(errmsg);
459 goto error2;
460 }
461 }
462 else /* copy normal character */
463 {
464 if (*efmp == '\\' && efmp + 1 < efm + len)
465 ++efmp;
466 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
467 *ptr++ = '\\'; /* escape regexp atoms */
468 if (*efmp)
469 *ptr++ = *efmp;
470 }
471 }
472 *ptr++ = '$';
473 *ptr = NUL;
474 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
475 goto error2;
476 /*
477 * Advance to next part
478 */
479 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
480 }
481 if (fmt_first == NULL) /* nothing found */
482 {
483 EMSG(_("E378: 'errorformat' contains no pattern"));
484 goto error2;
485 }
486
487 /*
488 * got_int is reset here, because it was probably set when killing the
489 * ":make" command, but we still want to read the errorfile then.
490 */
491 got_int = FALSE;
492
493 /* Always ignore case when looking for a matching error. */
494 regmatch.rm_ic = TRUE;
495
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000496 if (tv != NULL)
497 {
498 if (tv->v_type == VAR_STRING)
499 p_str = tv->vval.v_string;
500 else if (tv->v_type == VAR_LIST)
501 p_li = tv->vval.v_list->lv_first;
502 }
503
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 /*
505 * Read the lines in the error file one by one.
506 * Try to recognize one of the error formats in each line.
507 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000508 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000510 /* Get the next line. */
511 if (fd == NULL)
512 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000513 if (tv != NULL)
514 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000515 if (tv->v_type == VAR_STRING)
516 {
517 /* Get the next line from the supplied string */
518 char_u *p;
519
520 if (!*p_str) /* Reached the end of the string */
521 break;
522
523 p = vim_strchr(p_str, '\n');
524 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000525 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000526 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000527 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000528
529 if (len > CMDBUFFSIZE - 2)
530 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
531 else
532 vim_strncpy(IObuff, p_str, len);
533
534 p_str += len;
535 }
536 else if (tv->v_type == VAR_LIST)
537 {
538 /* Get the next line from the supplied list */
539 while (p_li && p_li->li_tv.v_type != VAR_STRING)
540 p_li = p_li->li_next; /* Skip non-string items */
541
542 if (!p_li) /* End of the list */
543 break;
544
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000545 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000546 if (len > CMDBUFFSIZE - 2)
547 len = CMDBUFFSIZE - 2;
548
549 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
550
551 p_li = p_li->li_next; /* next item */
552 }
553 }
554 else
555 {
556 /* Get the next line from the supplied buffer */
557 if (buflnum > lnumlast)
558 break;
559 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
560 CMDBUFFSIZE - 2);
561 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000562 }
563 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
564 break;
565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
Bram Moolenaar836082d2011-08-10 13:21:46 +0200567#ifdef FEAT_MBYTE
568 remove_bom(IObuff);
569#endif
570
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
572 *efmp = NUL;
573#ifdef USE_CRNL
574 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
575 *efmp = NUL;
576#endif
577
Bram Moolenaar01265852006-03-20 21:50:15 +0000578 /* If there was no %> item start at the first pattern */
579 if (fmt_start == NULL)
580 fmt_ptr = fmt_first;
581 else
582 {
583 fmt_ptr = fmt_start;
584 fmt_start = NULL;
585 }
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 /*
588 * Try to match each part of 'errorformat' until we find a complete
589 * match or no match.
590 */
591 valid = TRUE;
592restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000593 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100595 int r;
596
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 idx = fmt_ptr->prefix;
598 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
599 continue;
600 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000601 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (!multiscan)
603 errmsg[0] = NUL;
604 lnum = 0;
605 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000606 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 enr = -1;
608 type = 0;
609 tail = NULL;
610
611 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100612 r = vim_regexec(&regmatch, IObuff, (colnr_T)0);
613 fmt_ptr->prog = regmatch.regprog;
614 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
616 if ((idx == 'C' || idx == 'Z') && !multiline)
617 continue;
618 if (vim_strchr((char_u *)"EWI", idx) != NULL)
619 type = idx;
620 else
621 type = 0;
622 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000623 * Extract error message data from matched line.
624 * We check for an actual submatch, because "\[" and "\]" in
625 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 */
627 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
628 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000629 int c;
630
631 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
632 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000633
634 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000635 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000636 *regmatch.endp[i] = NUL;
637 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
638 *regmatch.endp[i] = c;
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000641 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 continue;
643 }
644 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000645 {
646 if (regmatch.startp[i] == NULL)
647 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000651 {
652 if (regmatch.startp[i] == NULL)
653 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000657 {
658 if (regmatch.startp[i] == NULL)
659 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000663 {
664 if (regmatch.startp[i] == NULL)
665 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000667 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000668 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 STRCPY(errmsg, IObuff);
670 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
671 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000672 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
673 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000675 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
677 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000678 {
679 if (regmatch.startp[i] == NULL)
680 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
684 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200685 char_u *match_ptr;
686
Bram Moolenaar4169da72006-06-20 18:49:32 +0000687 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
688 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200689 col = 0;
690 for (match_ptr = regmatch.startp[i];
691 match_ptr != regmatch.endp[i]; ++match_ptr)
692 {
693 ++col;
694 if (*match_ptr == TAB)
695 {
696 col += 7;
697 col -= col % 8;
698 }
699 }
700 ++col;
701 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
703 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
704 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000705 if (regmatch.startp[i] == NULL)
706 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000708 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000710 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
711 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000712 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
713 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000714 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
715 if (len > CMDBUFFSIZE - 5)
716 len = CMDBUFFSIZE - 5;
717 STRCPY(pattern, "^\\V");
718 STRNCAT(pattern, regmatch.startp[i], len);
719 pattern[len + 3] = '\\';
720 pattern[len + 4] = '$';
721 pattern[len + 5] = NUL;
722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 break;
724 }
725 }
726 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000727
Bram Moolenaar4770d092006-01-12 23:22:24 +0000728 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000730 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {
732 if (idx == 'D') /* enter directory */
733 {
734 if (*namebuf == NUL)
735 {
736 EMSG(_("E379: Missing or empty directory name"));
737 goto error2;
738 }
739 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
740 goto error2;
741 }
742 else if (idx == 'X') /* leave directory */
743 directory = qf_pop_dir(&dir_stack);
744 }
745 namebuf[0] = NUL; /* no match found, remove file name */
746 lnum = 0; /* don't jump to this line */
747 valid = FALSE;
748 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000749 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 multiline = multiignore = FALSE;
751 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000752 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000754 /* honor %> item */
755 if (fmt_ptr->conthere)
756 fmt_start = fmt_ptr;
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100761 multiignore = FALSE; /* reset continuation */
762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
764 { /* continuation of multi-line msg */
765 if (qfprev == NULL)
766 goto error2;
767 if (*errmsg && !multiignore)
768 {
769 len = (int)STRLEN(qfprev->qf_text);
770 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
771 == NULL)
772 goto error2;
773 STRCPY(ptr, qfprev->qf_text);
774 vim_free(qfprev->qf_text);
775 qfprev->qf_text = ptr;
776 *(ptr += len) = '\n';
777 STRCPY(++ptr, errmsg);
778 }
779 if (qfprev->qf_nr == -1)
780 qfprev->qf_nr = enr;
781 if (vim_isprintc(type) && !qfprev->qf_type)
782 qfprev->qf_type = type; /* only printable chars allowed */
783 if (!qfprev->qf_lnum)
784 qfprev->qf_lnum = lnum;
785 if (!qfprev->qf_col)
786 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 if (!qfprev->qf_fnum)
789 qfprev->qf_fnum = qf_get_fnum(directory,
790 *namebuf || directory ? namebuf
791 : currfile && valid ? currfile : 0);
792 if (idx == 'Z')
793 multiline = multiignore = FALSE;
794 line_breakcheck();
795 continue;
796 }
797 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
798 {
799 /* global file names */
800 valid = FALSE;
801 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
802 {
803 if (*namebuf && idx == 'P')
804 currfile = qf_push_dir(namebuf, &file_stack);
805 else if (idx == 'Q')
806 currfile = qf_pop_dir(&file_stack);
807 *namebuf = NUL;
808 if (tail && *tail)
809 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000810 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 multiscan = TRUE;
812 goto restofline;
813 }
814 }
815 }
816 if (fmt_ptr->flags == '-') /* generally exclude this line */
817 {
818 if (multiline)
819 multiignore = TRUE; /* also exclude continuation lines */
820 continue;
821 }
822 }
823
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000824 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000826 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000828 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000829 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 errmsg,
831 lnum,
832 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000834 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 enr,
836 type,
837 valid) == FAIL)
838 goto error2;
839 line_breakcheck();
840 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000841 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000843 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000845 /* no valid entry found */
846 qi->qf_lists[qi->qf_curlist].qf_ptr =
847 qi->qf_lists[qi->qf_curlist].qf_start;
848 qi->qf_lists[qi->qf_curlist].qf_index = 1;
849 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 else
852 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000853 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
854 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
855 qi->qf_lists[qi->qf_curlist].qf_ptr =
856 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000858 /* return number of matches */
859 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 goto qf_init_ok;
861 }
862 EMSG(_(e_readerrf));
863error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000864 qf_free(qi, qi->qf_curlist);
865 qi->qf_listcount--;
866 if (qi->qf_curlist > 0)
867 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000869 if (fd != NULL)
870 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
872 {
873 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +0200874 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 vim_free(fmt_ptr);
876 }
877 qf_clean_dir_stack(&dir_stack);
878 qf_clean_dir_stack(&file_stack);
879qf_init_end:
880 vim_free(namebuf);
881 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000882 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 vim_free(fmtstr);
884
885#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000886 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#endif
888
889 return retval;
890}
891
Bram Moolenaarfb604092014-07-23 15:55:00 +0200892 static void
893qf_store_title(qi, title)
894 qf_info_T *qi;
895 char_u *title;
896{
897 if (title != NULL)
898 {
899 char_u *p = alloc((int)STRLEN(title) + 2);
900
901 qi->qf_lists[qi->qf_curlist].qf_title = p;
902 if (p != NULL)
903 sprintf((char *)p, ":%s", (char *)title);
904 }
905}
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907/*
908 * Prepare for adding a new quickfix list.
909 */
910 static void
Bram Moolenaar94116152012-11-28 17:41:59 +0100911qf_new_list(qi, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000912 qf_info_T *qi;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200913 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914{
915 int i;
916
917 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +0200918 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 * the current entry. This makes it possible to browse in a tree-like
920 * way with ":grep'.
921 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000922 while (qi->qf_listcount > qi->qf_curlist + 1)
923 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
925 /*
926 * When the stack is full, remove to oldest entry
927 * Otherwise, add a new entry.
928 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000929 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000931 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000933 qi->qf_lists[i - 1] = qi->qf_lists[i];
934 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 }
936 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000937 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100938 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +0200939 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940}
941
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000942/*
943 * Free a location list
944 */
945 static void
946ll_free_all(pqi)
947 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000948{
949 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000950 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000951
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000952 qi = *pqi;
953 if (qi == NULL)
954 return;
955 *pqi = NULL; /* Remove reference to this list */
956
957 qi->qf_refcount--;
958 if (qi->qf_refcount < 1)
959 {
960 /* No references to this location list */
961 for (i = 0; i < qi->qf_listcount; ++i)
962 qf_free(qi, i);
963 vim_free(qi);
964 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000965}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000966
967 void
968qf_free_all(wp)
969 win_T *wp;
970{
971 int i;
972 qf_info_T *qi = &ql_info;
973
974 if (wp != NULL)
975 {
976 /* location list */
977 ll_free_all(&wp->w_llist);
978 ll_free_all(&wp->w_llist_ref);
979 }
980 else
981 /* quickfix list */
982 for (i = 0; i < qi->qf_listcount; ++i)
983 qf_free(qi, i);
984}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986/*
987 * Add an entry to the end of the list of errors.
988 * Returns OK or FAIL.
989 */
990 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000991qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
992 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000993 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000994 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 char_u *dir; /* optional directory name */
996 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000997 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 char_u *mesg; /* message */
999 long lnum; /* line number */
1000 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001001 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001002 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 int nr; /* error number */
1004 int type; /* type character */
1005 int valid; /* valid entry */
1006{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001007 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001009 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001011 if (bufnum != 0)
1012 qfp->qf_fnum = bufnum;
1013 else
1014 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1016 {
1017 vim_free(qfp);
1018 return FAIL;
1019 }
1020 qfp->qf_lnum = lnum;
1021 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001022 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001023 if (pattern == NULL || *pattern == NUL)
1024 qfp->qf_pattern = NULL;
1025 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1026 {
1027 vim_free(qfp->qf_text);
1028 vim_free(qfp);
1029 return FAIL;
1030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 qfp->qf_nr = nr;
1032 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1033 type = 0;
1034 qfp->qf_type = type;
1035 qfp->qf_valid = valid;
1036
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001037 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1038 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001040 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 qfp->qf_prev = qfp; /* first element points to itself */
1042 }
1043 else
1044 {
1045 qfp->qf_prev = *prevp;
1046 (*prevp)->qf_next = qfp;
1047 }
1048 qfp->qf_next = qfp; /* last element points to itself */
1049 qfp->qf_cleared = FALSE;
1050 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001051 ++qi->qf_lists[qi->qf_curlist].qf_count;
1052 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1053 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001055 qi->qf_lists[qi->qf_curlist].qf_index =
1056 qi->qf_lists[qi->qf_curlist].qf_count;
1057 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059
1060 return OK;
1061}
1062
1063/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001064 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001065 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001066 static qf_info_T *
1067ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001068{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001069 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001070
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001071 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1072 if (qi != NULL)
1073 {
1074 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1075 qi->qf_refcount++;
1076 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001077
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001078 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001079}
1080
1081/*
1082 * Return the location list for window 'wp'.
1083 * If not present, allocate a location list
1084 */
1085 static qf_info_T *
1086ll_get_or_alloc_list(wp)
1087 win_T *wp;
1088{
1089 if (IS_LL_WINDOW(wp))
1090 /* For a location list window, use the referenced location list */
1091 return wp->w_llist_ref;
1092
1093 /*
1094 * For a non-location list window, w_llist_ref should not point to a
1095 * location list.
1096 */
1097 ll_free_all(&wp->w_llist_ref);
1098
1099 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001100 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001101 return wp->w_llist;
1102}
1103
1104/*
1105 * Copy the location list from window "from" to window "to".
1106 */
1107 void
1108copy_loclist(from, to)
1109 win_T *from;
1110 win_T *to;
1111{
1112 qf_info_T *qi;
1113 int idx;
1114 int i;
1115
1116 /*
1117 * When copying from a location list window, copy the referenced
1118 * location list. For other windows, copy the location list for
1119 * that window.
1120 */
1121 if (IS_LL_WINDOW(from))
1122 qi = from->w_llist_ref;
1123 else
1124 qi = from->w_llist;
1125
1126 if (qi == NULL) /* no location list to copy */
1127 return;
1128
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001129 /* allocate a new location list */
1130 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001131 return;
1132
1133 to->w_llist->qf_listcount = qi->qf_listcount;
1134
1135 /* Copy the location lists one at a time */
1136 for (idx = 0; idx < qi->qf_listcount; idx++)
1137 {
1138 qf_list_T *from_qfl;
1139 qf_list_T *to_qfl;
1140
1141 to->w_llist->qf_curlist = idx;
1142
1143 from_qfl = &qi->qf_lists[idx];
1144 to_qfl = &to->w_llist->qf_lists[idx];
1145
1146 /* Some of the fields are populated by qf_add_entry() */
1147 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1148 to_qfl->qf_count = 0;
1149 to_qfl->qf_index = 0;
1150 to_qfl->qf_start = NULL;
1151 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001152 if (from_qfl->qf_title != NULL)
1153 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1154 else
1155 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001156
1157 if (from_qfl->qf_count)
1158 {
1159 qfline_T *from_qfp;
1160 qfline_T *prevp = NULL;
1161
1162 /* copy all the location entries in this list */
1163 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1164 ++i, from_qfp = from_qfp->qf_next)
1165 {
1166 if (qf_add_entry(to->w_llist, &prevp,
1167 NULL,
1168 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001169 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001170 from_qfp->qf_text,
1171 from_qfp->qf_lnum,
1172 from_qfp->qf_col,
1173 from_qfp->qf_viscol,
1174 from_qfp->qf_pattern,
1175 from_qfp->qf_nr,
1176 0,
1177 from_qfp->qf_valid) == FAIL)
1178 {
1179 qf_free_all(to);
1180 return;
1181 }
1182 /*
1183 * qf_add_entry() will not set the qf_num field, as the
1184 * directory and file names are not supplied. So the qf_fnum
1185 * field is copied here.
1186 */
1187 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1188 prevp->qf_type = from_qfp->qf_type; /* error type */
1189 if (from_qfl->qf_ptr == from_qfp)
1190 to_qfl->qf_ptr = prevp; /* current location */
1191 }
1192 }
1193
1194 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1195
1196 /* When no valid entries are present in the list, qf_ptr points to
1197 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001198 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001199 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001200 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001201 to_qfl->qf_index = 1;
1202 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001203 }
1204
1205 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1206}
1207
1208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 * get buffer number for file "dir.name"
1210 */
1211 static int
1212qf_get_fnum(directory, fname)
1213 char_u *directory;
1214 char_u *fname;
1215{
1216 if (fname == NULL || *fname == NUL) /* no file name */
1217 return 0;
1218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 char_u *ptr;
1220 int fnum;
1221
Bram Moolenaare60acc12011-05-10 16:41:25 +02001222#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001224#endif
1225#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (directory != NULL)
1227 slash_adjust(directory);
1228 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (directory != NULL && !vim_isAbsName(fname)
1231 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1232 {
1233 /*
1234 * Here we check if the file really exists.
1235 * This should normally be true, but if make works without
1236 * "leaving directory"-messages we might have missed a
1237 * directory change.
1238 */
1239 if (mch_getperm(ptr) < 0)
1240 {
1241 vim_free(ptr);
1242 directory = qf_guess_filepath(fname);
1243 if (directory)
1244 ptr = concat_fnames(directory, fname, TRUE);
1245 else
1246 ptr = vim_strsave(fname);
1247 }
1248 /* Use concatenated directory name and file name */
1249 fnum = buflist_add(ptr, 0);
1250 vim_free(ptr);
1251 return fnum;
1252 }
1253 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 }
1255}
1256
1257/*
1258 * push dirbuf onto the directory stack and return pointer to actual dir or
1259 * NULL on error
1260 */
1261 static char_u *
1262qf_push_dir(dirbuf, stackptr)
1263 char_u *dirbuf;
1264 struct dir_stack_T **stackptr;
1265{
1266 struct dir_stack_T *ds_new;
1267 struct dir_stack_T *ds_ptr;
1268
1269 /* allocate new stack element and hook it in */
1270 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1271 if (ds_new == NULL)
1272 return NULL;
1273
1274 ds_new->next = *stackptr;
1275 *stackptr = ds_new;
1276
1277 /* store directory on the stack */
1278 if (vim_isAbsName(dirbuf)
1279 || (*stackptr)->next == NULL
1280 || (*stackptr && dir_stack != *stackptr))
1281 (*stackptr)->dirname = vim_strsave(dirbuf);
1282 else
1283 {
1284 /* Okay we don't have an absolute path.
1285 * dirbuf must be a subdir of one of the directories on the stack.
1286 * Let's search...
1287 */
1288 ds_new = (*stackptr)->next;
1289 (*stackptr)->dirname = NULL;
1290 while (ds_new)
1291 {
1292 vim_free((*stackptr)->dirname);
1293 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1294 TRUE);
1295 if (mch_isdir((*stackptr)->dirname) == TRUE)
1296 break;
1297
1298 ds_new = ds_new->next;
1299 }
1300
1301 /* clean up all dirs we already left */
1302 while ((*stackptr)->next != ds_new)
1303 {
1304 ds_ptr = (*stackptr)->next;
1305 (*stackptr)->next = (*stackptr)->next->next;
1306 vim_free(ds_ptr->dirname);
1307 vim_free(ds_ptr);
1308 }
1309
1310 /* Nothing found -> it must be on top level */
1311 if (ds_new == NULL)
1312 {
1313 vim_free((*stackptr)->dirname);
1314 (*stackptr)->dirname = vim_strsave(dirbuf);
1315 }
1316 }
1317
1318 if ((*stackptr)->dirname != NULL)
1319 return (*stackptr)->dirname;
1320 else
1321 {
1322 ds_ptr = *stackptr;
1323 *stackptr = (*stackptr)->next;
1324 vim_free(ds_ptr);
1325 return NULL;
1326 }
1327}
1328
1329
1330/*
1331 * pop dirbuf from the directory stack and return previous directory or NULL if
1332 * stack is empty
1333 */
1334 static char_u *
1335qf_pop_dir(stackptr)
1336 struct dir_stack_T **stackptr;
1337{
1338 struct dir_stack_T *ds_ptr;
1339
1340 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1341 * What to do if it isn't? */
1342
1343 /* pop top element and free it */
1344 if (*stackptr != NULL)
1345 {
1346 ds_ptr = *stackptr;
1347 *stackptr = (*stackptr)->next;
1348 vim_free(ds_ptr->dirname);
1349 vim_free(ds_ptr);
1350 }
1351
1352 /* return NEW top element as current dir or NULL if stack is empty*/
1353 return *stackptr ? (*stackptr)->dirname : NULL;
1354}
1355
1356/*
1357 * clean up directory stack
1358 */
1359 static void
1360qf_clean_dir_stack(stackptr)
1361 struct dir_stack_T **stackptr;
1362{
1363 struct dir_stack_T *ds_ptr;
1364
1365 while ((ds_ptr = *stackptr) != NULL)
1366 {
1367 *stackptr = (*stackptr)->next;
1368 vim_free(ds_ptr->dirname);
1369 vim_free(ds_ptr);
1370 }
1371}
1372
1373/*
1374 * Check in which directory of the directory stack the given file can be
1375 * found.
1376 * Returns a pointer to the directory name or NULL if not found
1377 * Cleans up intermediate directory entries.
1378 *
1379 * TODO: How to solve the following problem?
1380 * If we have the this directory tree:
1381 * ./
1382 * ./aa
1383 * ./aa/bb
1384 * ./bb
1385 * ./bb/x.c
1386 * and make says:
1387 * making all in aa
1388 * making all in bb
1389 * x.c:9: Error
1390 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1391 * qf_guess_filepath will return NULL.
1392 */
1393 static char_u *
1394qf_guess_filepath(filename)
1395 char_u *filename;
1396{
1397 struct dir_stack_T *ds_ptr;
1398 struct dir_stack_T *ds_tmp;
1399 char_u *fullname;
1400
1401 /* no dirs on the stack - there's nothing we can do */
1402 if (dir_stack == NULL)
1403 return NULL;
1404
1405 ds_ptr = dir_stack->next;
1406 fullname = NULL;
1407 while (ds_ptr)
1408 {
1409 vim_free(fullname);
1410 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1411
1412 /* If concat_fnames failed, just go on. The worst thing that can happen
1413 * is that we delete the entire stack.
1414 */
1415 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1416 break;
1417
1418 ds_ptr = ds_ptr->next;
1419 }
1420
1421 vim_free(fullname);
1422
1423 /* clean up all dirs we already left */
1424 while (dir_stack->next != ds_ptr)
1425 {
1426 ds_tmp = dir_stack->next;
1427 dir_stack->next = dir_stack->next->next;
1428 vim_free(ds_tmp->dirname);
1429 vim_free(ds_tmp);
1430 }
1431
1432 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1433
1434}
1435
1436/*
1437 * jump to a quickfix line
1438 * if dir == FORWARD go "errornr" valid entries forward
1439 * if dir == BACKWARD go "errornr" valid entries backward
1440 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1441 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1442 * else if "errornr" is zero, redisplay the same line
1443 * else go to entry "errornr"
1444 */
1445 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001446qf_jump(qi, dir, errornr, forceit)
1447 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 int dir;
1449 int errornr;
1450 int forceit;
1451{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001452 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001453 qfline_T *qf_ptr;
1454 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 int qf_index;
1456 int old_qf_fnum;
1457 int old_qf_index;
1458 int prev_index;
1459 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1460 char_u *err = e_no_more_items;
1461 linenr_T i;
1462 buf_T *old_curbuf;
1463 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 colnr_T screen_col;
1465 colnr_T char_col;
1466 char_u *line;
1467#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001468 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001469 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 int opened_window = FALSE;
1471 win_T *win;
1472 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001473 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001475 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 int print_message = TRUE;
1477 int len;
1478#ifdef FEAT_FOLDING
1479 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1480#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001481 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001482 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001484 if (qi == NULL)
1485 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001486
1487 if (qi->qf_curlist >= qi->qf_listcount
1488 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 {
1490 EMSG(_(e_quickfix));
1491 return;
1492 }
1493
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001494 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001496 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 old_qf_index = qf_index;
1498 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1499 {
1500 while (errornr--)
1501 {
1502 old_qf_ptr = qf_ptr;
1503 prev_index = qf_index;
1504 old_qf_fnum = qf_ptr->qf_fnum;
1505 do
1506 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001507 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 || qf_ptr->qf_next == NULL)
1509 {
1510 qf_ptr = old_qf_ptr;
1511 qf_index = prev_index;
1512 if (err != NULL)
1513 {
1514 EMSG(_(err));
1515 goto theend;
1516 }
1517 errornr = 0;
1518 break;
1519 }
1520 ++qf_index;
1521 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1523 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1525 err = NULL;
1526 }
1527 }
1528 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1529 {
1530 while (errornr--)
1531 {
1532 old_qf_ptr = qf_ptr;
1533 prev_index = qf_index;
1534 old_qf_fnum = qf_ptr->qf_fnum;
1535 do
1536 {
1537 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1538 {
1539 qf_ptr = old_qf_ptr;
1540 qf_index = prev_index;
1541 if (err != NULL)
1542 {
1543 EMSG(_(err));
1544 goto theend;
1545 }
1546 errornr = 0;
1547 break;
1548 }
1549 --qf_index;
1550 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001551 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1552 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1554 err = NULL;
1555 }
1556 }
1557 else if (errornr != 0) /* go to specified number */
1558 {
1559 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1560 {
1561 --qf_index;
1562 qf_ptr = qf_ptr->qf_prev;
1563 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001564 while (errornr > qf_index && qf_index <
1565 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 && qf_ptr->qf_next != NULL)
1567 {
1568 ++qf_index;
1569 qf_ptr = qf_ptr->qf_next;
1570 }
1571 }
1572
1573#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001574 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1575 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 /* No need to print the error message if it's visible in the error
1577 * window */
1578 print_message = FALSE;
1579
1580 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001581 * For ":helpgrep" find a help window or open one.
1582 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001583 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001584 {
1585 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001586
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001587 if (cmdmod.tab != 0)
1588 wp = NULL;
1589 else
1590 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1591 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1592 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001593 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1594 win_enter(wp, TRUE);
1595 else
1596 {
1597 /*
1598 * Split off help window; put it at far top if no position
1599 * specified, the current window is vertically split and narrow.
1600 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001601 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001602# ifdef FEAT_VERTSPLIT
1603 if (cmdmod.split == 0 && curwin->w_width != Columns
1604 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001605 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001606# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001607 if (qi != &ql_info)
1608 flags |= WSP_NEWLOC; /* don't copy the location list */
1609
1610 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001611 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001612 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001613
1614 if (curwin->w_height < p_hh)
1615 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001616
1617 if (qi != &ql_info) /* not a quickfix list */
1618 {
1619 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001620 curwin->w_llist = qi;
1621 qi->qf_refcount++;
1622 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001623 }
1624
1625 if (!p_im)
1626 restart_edit = 0; /* don't want insert mode in help file */
1627 }
1628
1629 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 * If currently in the quickfix window, find another window to show the
1631 * file in.
1632 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001633 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001635 win_T *usable_win_ptr = NULL;
1636
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 /*
1638 * If there is no file specified, we don't know where to go.
1639 * But do advance, otherwise ":cn" gets stuck.
1640 */
1641 if (qf_ptr->qf_fnum == 0)
1642 goto theend;
1643
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001645
1646 ll_ref = curwin->w_llist_ref;
1647 if (ll_ref != NULL)
1648 {
1649 /* Find a window using the same location list that is not a
1650 * quickfix window. */
1651 FOR_ALL_WINDOWS(usable_win_ptr)
1652 if (usable_win_ptr->w_llist == ll_ref
1653 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001654 {
1655 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001656 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001657 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001658 }
1659
1660 if (!usable_win)
1661 {
1662 /* Locate a window showing a normal buffer */
1663 FOR_ALL_WINDOWS(win)
1664 if (win->w_buffer->b_p_bt[0] == NUL)
1665 {
1666 usable_win = 1;
1667 break;
1668 }
1669 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001670
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001672 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001673 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001675 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001676 {
1677 tabpage_T *tp;
1678 win_T *wp;
1679
1680 FOR_ALL_TAB_WINDOWS(tp, wp)
1681 {
1682 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1683 {
1684 goto_tabpage_win(tp, wp);
1685 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001686 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001687 }
1688 }
1689 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001690win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001691
1692 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001693 * If there is only one window and it is the quickfix window, create a
1694 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001695 */
1696 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001698 flags = WSP_ABOVE;
1699 if (ll_ref != NULL)
1700 flags |= WSP_NEWLOC;
1701 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 goto failed; /* not enough room for window */
1703 opened_window = TRUE; /* close it when fail */
1704 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001705 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001706 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001707 if (ll_ref != NULL)
1708 {
1709 /* The new window should use the location list from the
1710 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001711 curwin->w_llist = ll_ref;
1712 ll_ref->qf_refcount++;
1713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 else
1716 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001717 if (curwin->w_llist_ref != NULL)
1718 {
1719 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001720 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001721 if (win == NULL)
1722 {
1723 /* Find the window showing the selected file */
1724 FOR_ALL_WINDOWS(win)
1725 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1726 break;
1727 if (win == NULL)
1728 {
1729 /* Find a previous usable window */
1730 win = curwin;
1731 do
1732 {
1733 if (win->w_buffer->b_p_bt[0] == NUL)
1734 break;
1735 if (win->w_prev == NULL)
1736 win = lastwin; /* wrap around the top */
1737 else
1738 win = win->w_prev; /* go to previous window */
1739 } while (win != curwin);
1740 }
1741 }
1742 win_goto(win);
1743
1744 /* If the location list for the window is not set, then set it
1745 * to the location list from the location window */
1746 if (win->w_llist == NULL)
1747 {
1748 win->w_llist = ll_ref;
1749 ll_ref->qf_refcount++;
1750 }
1751 }
1752 else
1753 {
1754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 /*
1756 * Try to find a window that shows the right buffer.
1757 * Default to the window just above the quickfix buffer.
1758 */
1759 win = curwin;
1760 altwin = NULL;
1761 for (;;)
1762 {
1763 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1764 break;
1765 if (win->w_prev == NULL)
1766 win = lastwin; /* wrap around the top */
1767 else
1768 win = win->w_prev; /* go to previous window */
1769
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001770 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
1772 /* Didn't find it, go to the window before the quickfix
1773 * window. */
1774 if (altwin != NULL)
1775 win = altwin;
1776 else if (curwin->w_prev != NULL)
1777 win = curwin->w_prev;
1778 else
1779 win = curwin->w_next;
1780 break;
1781 }
1782
1783 /* Remember a usable window. */
1784 if (altwin == NULL && !win->w_p_pvw
1785 && win->w_buffer->b_p_bt[0] == NUL)
1786 altwin = win;
1787 }
1788
1789 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
1792 }
1793#endif
1794
1795 /*
1796 * If there is a file name,
1797 * read the wanted file if needed, and check autowrite etc.
1798 */
1799 old_curbuf = curbuf;
1800 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001801
1802 if (qf_ptr->qf_fnum != 0)
1803 {
1804 if (qf_ptr->qf_type == 1)
1805 {
1806 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1807 * set b_p_ro flag). */
1808 if (!can_abandon(curbuf, forceit))
1809 {
1810 EMSG(_(e_nowrtmsg));
1811 ok = FALSE;
1812 }
1813 else
1814 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001815 ECMD_HIDE + ECMD_SET_HELP,
1816 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001817 }
1818 else
1819 ok = buflist_getfile(qf_ptr->qf_fnum,
1820 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1821 }
1822
1823 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
1825 /* When not switched to another buffer, still need to set pc mark */
1826 if (curbuf == old_curbuf)
1827 setpcmark();
1828
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001829 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001831 /*
1832 * Go to line with error, unless qf_lnum is 0.
1833 */
1834 i = qf_ptr->qf_lnum;
1835 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001837 if (i > curbuf->b_ml.ml_line_count)
1838 i = curbuf->b_ml.ml_line_count;
1839 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001841 if (qf_ptr->qf_col > 0)
1842 {
1843 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1844 if (qf_ptr->qf_viscol == TRUE)
1845 {
1846 /*
1847 * Check each character from the beginning of the error
1848 * line up to the error column. For each tab character
1849 * found, reduce the error column value by the length of
1850 * a tab character.
1851 */
1852 line = ml_get_curline();
1853 screen_col = 0;
1854 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1855 {
1856 if (*line == NUL)
1857 break;
1858 if (*line++ == '\t')
1859 {
1860 curwin->w_cursor.col -= 7 - (screen_col % 8);
1861 screen_col += 8 - (screen_col % 8);
1862 }
1863 else
1864 ++screen_col;
1865 }
1866 }
1867 check_cursor();
1868 }
1869 else
1870 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001873 {
1874 pos_T save_cursor;
1875
1876 /* Move the cursor to the first line in the buffer */
1877 save_cursor = curwin->w_cursor;
1878 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001879 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1880 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001881 curwin->w_cursor = save_cursor;
1882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884#ifdef FEAT_FOLDING
1885 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1886 foldOpenCursor();
1887#endif
1888 if (print_message)
1889 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001890 /* Update the screen before showing the message, unless the screen
1891 * scrolled up. */
1892 if (!msg_scrolled)
1893 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001895 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1897 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1898 /* Add the message, skipping leading whitespace and newlines. */
1899 len = (int)STRLEN(IObuff);
1900 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1901
1902 /* Output the message. Overwrite to avoid scrolling when the 'O'
1903 * flag is present in 'shortmess'; But when not jumping, print the
1904 * whole message. */
1905 i = msg_scroll;
1906 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1907 msg_scroll = TRUE;
1908 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1909 msg_scroll = FALSE;
1910 msg_attr_keep(IObuff, 0, TRUE);
1911 msg_scroll = i;
1912 }
1913 }
1914 else
1915 {
1916#ifdef FEAT_WINDOWS
1917 if (opened_window)
1918 win_close(curwin, TRUE); /* Close opened window */
1919#endif
1920 if (qf_ptr->qf_fnum != 0)
1921 {
1922 /*
1923 * Couldn't open file, so put index back where it was. This could
1924 * happen if the file was readonly and we changed something.
1925 */
1926#ifdef FEAT_WINDOWS
1927failed:
1928#endif
1929 qf_ptr = old_qf_ptr;
1930 qf_index = old_qf_index;
1931 }
1932 }
1933theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001934 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1935 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936#ifdef FEAT_WINDOWS
1937 if (p_swb != old_swb && opened_window)
1938 {
1939 /* Restore old 'switchbuf' value, but not when an autocommand or
1940 * modeline has changed the value. */
1941 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001944 swb_flags = old_swb_flags;
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 else
1947 free_string_option(old_swb);
1948 }
1949#endif
1950}
1951
1952/*
1953 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001954 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 */
1956 void
1957qf_list(eap)
1958 exarg_T *eap;
1959{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001960 buf_T *buf;
1961 char_u *fname;
1962 qfline_T *qfp;
1963 int i;
1964 int idx1 = 1;
1965 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001966 char_u *arg = eap->arg;
1967 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001969 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001971 if (eap->cmdidx == CMD_llist)
1972 {
1973 qi = GET_LOC_LIST(curwin);
1974 if (qi == NULL)
1975 {
1976 EMSG(_(e_loclist));
1977 return;
1978 }
1979 }
1980
1981 if (qi->qf_curlist >= qi->qf_listcount
1982 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 EMSG(_(e_quickfix));
1985 return;
1986 }
1987 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1988 {
1989 EMSG(_(e_trailing));
1990 return;
1991 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001992 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 if (idx1 < 0)
1994 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1995 if (idx2 < 0)
1996 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1997
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001998 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002000 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2001 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
2003 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2004 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002005 msg_putchar('\n');
2006 if (got_int)
2007 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002008
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002009 fname = NULL;
2010 if (qfp->qf_fnum != 0
2011 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2012 {
2013 fname = buf->b_fname;
2014 if (qfp->qf_type == 1) /* :helpgrep */
2015 fname = gettail(fname);
2016 }
2017 if (fname == NULL)
2018 sprintf((char *)IObuff, "%2d", i);
2019 else
2020 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2021 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002022 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002023 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2024 if (qfp->qf_lnum == 0)
2025 IObuff[0] = NUL;
2026 else if (qfp->qf_col == 0)
2027 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2028 else
2029 sprintf((char *)IObuff, ":%ld col %d",
2030 qfp->qf_lnum, qfp->qf_col);
2031 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2032 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2033 msg_puts_attr(IObuff, hl_attr(HLF_N));
2034 if (qfp->qf_pattern != NULL)
2035 {
2036 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2037 STRCAT(IObuff, ":");
2038 msg_puts(IObuff);
2039 }
2040 msg_puts((char_u *)" ");
2041
2042 /* Remove newlines and leading whitespace from the text. For an
2043 * unrecognized line keep the indent, the compiler may mark a word
2044 * with ^^^^. */
2045 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2047 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002048 msg_prt_line(IObuff, FALSE);
2049 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002051
2052 qfp = qfp->qf_next;
2053 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 ui_breakcheck();
2055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056}
2057
2058/*
2059 * Remove newlines and leading whitespace from an error message.
2060 * Put the result in "buf[bufsize]".
2061 */
2062 static void
2063qf_fmt_text(text, buf, bufsize)
2064 char_u *text;
2065 char_u *buf;
2066 int bufsize;
2067{
2068 int i;
2069 char_u *p = text;
2070
2071 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2072 {
2073 if (*p == '\n')
2074 {
2075 buf[i] = ' ';
2076 while (*++p != NUL)
2077 if (!vim_iswhite(*p) && *p != '\n')
2078 break;
2079 }
2080 else
2081 buf[i] = *p++;
2082 }
2083 buf[i] = NUL;
2084}
2085
2086/*
2087 * ":colder [count]": Up in the quickfix stack.
2088 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002089 * ":lolder [count]": Up in the location list stack.
2090 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 */
2092 void
2093qf_age(eap)
2094 exarg_T *eap;
2095{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002096 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 int count;
2098
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002099 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2100 {
2101 qi = GET_LOC_LIST(curwin);
2102 if (qi == NULL)
2103 {
2104 EMSG(_(e_loclist));
2105 return;
2106 }
2107 }
2108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (eap->addr_count != 0)
2110 count = eap->line2;
2111 else
2112 count = 1;
2113 while (count--)
2114 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002115 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002117 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
2119 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002122 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124 else
2125 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002126 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
2128 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002129 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002131 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002134 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135}
2136
2137 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002138qf_msg(qi)
2139 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002142 qi->qf_curlist + 1, qi->qf_listcount,
2143 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002145 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146#endif
2147}
2148
2149/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002150 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 */
2152 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153qf_free(qi, idx)
2154 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 int idx;
2156{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002157 qfline_T *qfp;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002158 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002162 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002163 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002164 {
2165 vim_free(qi->qf_lists[idx].qf_start->qf_text);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002166 stop = (qi->qf_lists[idx].qf_start == qfp);
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002167 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2168 vim_free(qi->qf_lists[idx].qf_start);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002169 if (stop)
2170 /* Somehow qf_count may have an incorrect value, set it to 1
2171 * to avoid crashing when it's wrong.
2172 * TODO: Avoid qf_count being incorrect. */
2173 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002174 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002175 qi->qf_lists[idx].qf_start = qfp;
2176 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002178 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002179 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002180 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181}
2182
2183/*
2184 * qf_mark_adjust: adjust marks
2185 */
2186 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002187qf_mark_adjust(wp, line1, line2, amount, amount_after)
2188 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 linenr_T line1;
2190 linenr_T line2;
2191 long amount;
2192 long amount_after;
2193{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002194 int i;
2195 qfline_T *qfp;
2196 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002197 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002199 if (wp != NULL)
2200 {
2201 if (wp->w_llist == NULL)
2202 return;
2203 qi = wp->w_llist;
2204 }
2205
2206 for (idx = 0; idx < qi->qf_listcount; ++idx)
2207 if (qi->qf_lists[idx].qf_count)
2208 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2209 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 if (qfp->qf_fnum == curbuf->b_fnum)
2211 {
2212 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2213 {
2214 if (amount == MAXLNUM)
2215 qfp->qf_cleared = TRUE;
2216 else
2217 qfp->qf_lnum += amount;
2218 }
2219 else if (amount_after && qfp->qf_lnum > line2)
2220 qfp->qf_lnum += amount_after;
2221 }
2222}
2223
2224/*
2225 * Make a nice message out of the error character and the error number:
2226 * char number message
2227 * e or E 0 " error"
2228 * w or W 0 " warning"
2229 * i or I 0 " info"
2230 * 0 0 ""
2231 * other 0 " c"
2232 * e or E n " error n"
2233 * w or W n " warning n"
2234 * i or I n " info n"
2235 * 0 n " error n"
2236 * other n " c n"
2237 * 1 x "" :helpgrep
2238 */
2239 static char_u *
2240qf_types(c, nr)
2241 int c, nr;
2242{
2243 static char_u buf[20];
2244 static char_u cc[3];
2245 char_u *p;
2246
2247 if (c == 'W' || c == 'w')
2248 p = (char_u *)" warning";
2249 else if (c == 'I' || c == 'i')
2250 p = (char_u *)" info";
2251 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2252 p = (char_u *)" error";
2253 else if (c == 0 || c == 1)
2254 p = (char_u *)"";
2255 else
2256 {
2257 cc[0] = ' ';
2258 cc[1] = c;
2259 cc[2] = NUL;
2260 p = cc;
2261 }
2262
2263 if (nr <= 0)
2264 return p;
2265
2266 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2267 return buf;
2268}
2269
2270#if defined(FEAT_WINDOWS) || defined(PROTO)
2271/*
2272 * ":cwindow": open the quickfix window if we have errors to display,
2273 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002274 * ":lwindow": open the location list window if we have locations to display,
2275 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 */
2277 void
2278ex_cwindow(eap)
2279 exarg_T *eap;
2280{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002281 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 win_T *win;
2283
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002284 if (eap->cmdidx == CMD_lwindow)
2285 {
2286 qi = GET_LOC_LIST(curwin);
2287 if (qi == NULL)
2288 return;
2289 }
2290
2291 /* Look for an existing quickfix window. */
2292 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
2294 /*
2295 * If a quickfix window is open but we have no errors to display,
2296 * close the window. If a quickfix window is not open, then open
2297 * it if we have errors; otherwise, leave it closed.
2298 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002299 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002300 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002301 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 if (win != NULL)
2304 ex_cclose(eap);
2305 }
2306 else if (win == NULL)
2307 ex_copen(eap);
2308}
2309
2310/*
2311 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002312 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 void
2315ex_cclose(eap)
2316 exarg_T *eap;
2317{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002318 win_T *win = NULL;
2319 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002321 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2322 {
2323 qi = GET_LOC_LIST(curwin);
2324 if (qi == NULL)
2325 return;
2326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002328 /* Find existing quickfix window and close it. */
2329 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 if (win != NULL)
2331 win_close(win, FALSE);
2332}
2333
2334/*
2335 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002336 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 */
2338 void
2339ex_copen(eap)
2340 exarg_T *eap;
2341{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002342 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002345 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002346 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002347 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002349 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2350 {
2351 qi = GET_LOC_LIST(curwin);
2352 if (qi == NULL)
2353 {
2354 EMSG(_(e_loclist));
2355 return;
2356 }
2357 }
2358
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 if (eap->addr_count != 0)
2360 height = eap->line2;
2361 else
2362 height = QF_WINHEIGHT;
2363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#ifdef FEAT_GUI
2366 need_mouse_correct = TRUE;
2367#endif
2368
2369 /*
2370 * Find existing quickfix window, or open a new one.
2371 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002372 win = qf_find_win(qi);
2373
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002374 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002377 if (eap->addr_count != 0)
2378 {
2379#ifdef FEAT_VERTSPLIT
2380 if (cmdmod.split & WSP_VERT)
2381 {
2382 if (height != W_WIDTH(win))
2383 win_setwidth(height);
2384 }
2385 else
2386#endif
2387 if (height != win->w_height)
2388 win_setheight(height);
2389 }
2390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 else
2392 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002393 qf_buf = qf_find_buf(qi);
2394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 /* The current window becomes the previous window afterwards. */
2396 win = curwin;
2397
Bram Moolenaar77642c02012-11-20 17:55:10 +01002398 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2399 && cmdmod.split == 0)
2400 /* Create the new window at the very bottom, except when
2401 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002403 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002405 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002407 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002409 /*
2410 * For the location list window, create a reference to the
2411 * location list from the window 'win'.
2412 */
2413 curwin->w_llist_ref = win->w_llist;
2414 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002416
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002417 if (oldwin != curwin)
2418 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002419 if (qf_buf != NULL)
2420 /* Use the existing quickfix buffer */
2421 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002422 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002423 else
2424 {
2425 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002426 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002427 /* switch off 'swapfile' */
2428 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2429 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002430 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002431 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002432 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002433#ifdef FEAT_DIFF
2434 curwin->w_p_diff = FALSE;
2435#endif
2436#ifdef FEAT_FOLDING
2437 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2438 OPT_LOCAL);
2439#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002442 /* Only set the height when still in the same tab page and there is no
2443 * window to the side. */
2444 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002445#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002446 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002447#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002448 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 win_setheight(height);
2450 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2451 if (win_valid(win))
2452 prevwin = win;
2453 }
2454
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002455 qf_set_title_var(qi);
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 /*
2458 * Fill the buffer with the quickfix list.
2459 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002460 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002462 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 curwin->w_cursor.col = 0;
2464 check_cursor();
2465 update_topline(); /* scroll to show the line */
2466}
2467
2468/*
2469 * Return the number of the current entry (line number in the quickfix
2470 * window).
2471 */
2472 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002473qf_current_entry(wp)
2474 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002476 qf_info_T *qi = &ql_info;
2477
2478 if (IS_LL_WINDOW(wp))
2479 /* In the location list window, use the referenced location list */
2480 qi = wp->w_llist_ref;
2481
2482 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483}
2484
2485/*
2486 * Update the cursor position in the quickfix window to the current error.
2487 * Return TRUE if there is a quickfix window.
2488 */
2489 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002490qf_win_pos_update(qi, old_qf_index)
2491 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 int old_qf_index; /* previous qf_index or zero */
2493{
2494 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002495 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 /*
2498 * Put the cursor on the current error in the quickfix window, so that
2499 * it's viewable.
2500 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002501 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if (win != NULL
2503 && qf_index <= win->w_buffer->b_ml.ml_line_count
2504 && old_qf_index != qf_index)
2505 {
2506 win_T *old_curwin = curwin;
2507
2508 curwin = win;
2509 curbuf = win->w_buffer;
2510 if (qf_index > old_qf_index)
2511 {
2512 curwin->w_redraw_top = old_qf_index;
2513 curwin->w_redraw_bot = qf_index;
2514 }
2515 else
2516 {
2517 curwin->w_redraw_top = qf_index;
2518 curwin->w_redraw_bot = old_qf_index;
2519 }
2520 curwin->w_cursor.lnum = qf_index;
2521 curwin->w_cursor.col = 0;
2522 update_topline(); /* scroll to show the line */
2523 redraw_later(VALID);
2524 curwin->w_redr_status = TRUE; /* update ruler */
2525 curwin = old_curwin;
2526 curbuf = curwin->w_buffer;
2527 }
2528 return win != NULL;
2529}
2530
2531/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002532 * Check whether the given window is displaying the specified quickfix/location
2533 * list buffer
2534 */
2535 static int
2536is_qf_win(win, qi)
2537 win_T *win;
2538 qf_info_T *qi;
2539{
2540 /*
2541 * A window displaying the quickfix buffer will have the w_llist_ref field
2542 * set to NULL.
2543 * A window displaying a location list buffer will have the w_llist_ref
2544 * pointing to the location list.
2545 */
2546 if (bt_quickfix(win->w_buffer))
2547 if ((qi == &ql_info && win->w_llist_ref == NULL)
2548 || (qi != &ql_info && win->w_llist_ref == qi))
2549 return TRUE;
2550
2551 return FALSE;
2552}
2553
2554/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002555 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002556 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002557 */
2558 static win_T *
2559qf_find_win(qi)
2560 qf_info_T *qi;
2561{
2562 win_T *win;
2563
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002564 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002565 if (is_qf_win(win, qi))
2566 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002567
2568 return win;
2569}
2570
2571/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002572 * Find a quickfix buffer.
2573 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 */
2575 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002576qf_find_buf(qi)
2577 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002579 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002580 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
Bram Moolenaar9c102382006-05-03 21:26:49 +00002582 FOR_ALL_TAB_WINDOWS(tp, win)
2583 if (is_qf_win(win, qi))
2584 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002585
Bram Moolenaar9c102382006-05-03 21:26:49 +00002586 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587}
2588
2589/*
2590 * Find the quickfix buffer. If it exists, update the contents.
2591 */
2592 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002593qf_update_buffer(qi)
2594 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002597 win_T *win;
2598 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
2601 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002602 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 if (buf != NULL)
2604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 /* set curwin/curbuf to buf and save a few things */
2606 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002608 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002610 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002611 {
2612 curwin_save = curwin;
2613 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002614 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002615 curwin = curwin_save;
2616
2617 }
2618
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 /* restore curwin/curbuf and a few other things */
2620 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002622 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624}
2625
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002626/*
2627 * Set "w:quickfix_title" if "qi" has a title.
2628 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002629 static void
Bram Moolenaarfb604092014-07-23 15:55:00 +02002630qf_set_title_var(qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002631 qf_info_T *qi;
2632{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002633 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2634 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002635 qi->qf_lists[qi->qf_curlist].qf_title);
2636}
2637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638/*
2639 * Fill current buffer with quickfix errors, replacing any previous contents.
2640 * curbuf must be the quickfix buffer!
2641 */
2642 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002643qf_fill_buffer(qi)
2644 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002646 linenr_T lnum;
2647 qfline_T *qfp;
2648 buf_T *errbuf;
2649 int len;
2650 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652 /* delete all existing lines */
2653 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2654 (void)ml_delete((linenr_T)1, FALSE);
2655
2656 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002657 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002660 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2661 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 {
2663 if (qfp->qf_fnum != 0
2664 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2665 && errbuf->b_fname != NULL)
2666 {
2667 if (qfp->qf_type == 1) /* :helpgrep */
2668 STRCPY(IObuff, gettail(errbuf->b_fname));
2669 else
2670 STRCPY(IObuff, errbuf->b_fname);
2671 len = (int)STRLEN(IObuff);
2672 }
2673 else
2674 len = 0;
2675 IObuff[len++] = '|';
2676
2677 if (qfp->qf_lnum > 0)
2678 {
2679 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2680 len += (int)STRLEN(IObuff + len);
2681
2682 if (qfp->qf_col > 0)
2683 {
2684 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2685 len += (int)STRLEN(IObuff + len);
2686 }
2687
2688 sprintf((char *)IObuff + len, "%s",
2689 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2690 len += (int)STRLEN(IObuff + len);
2691 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002692 else if (qfp->qf_pattern != NULL)
2693 {
2694 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2695 len += (int)STRLEN(IObuff + len);
2696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 IObuff[len++] = '|';
2698 IObuff[len++] = ' ';
2699
2700 /* Remove newlines and leading whitespace from the text.
2701 * For an unrecognized line keep the indent, the compiler may
2702 * mark a word with ^^^^. */
2703 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2704 IObuff + len, IOSIZE - len);
2705
2706 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2707 == FAIL)
2708 break;
2709 qfp = qfp->qf_next;
2710 }
2711 /* Delete the empty line which is now at the end */
2712 (void)ml_delete(lnum + 1, FALSE);
2713 }
2714
2715 /* correct cursor position */
2716 check_lnums(TRUE);
2717
2718 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2719 * resembles reading a file into a buffer, it's more logical when using
2720 * autocommands. */
2721 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2722 curbuf->b_p_ma = FALSE;
2723
2724#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002725 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2727 FALSE, curbuf);
2728 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2729 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002730 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731#endif
2732
2733 /* make sure it will be redrawn */
2734 redraw_curbuf_later(NOT_VALID);
2735
2736 /* Restore KeyTyped, setting 'filetype' may reset it. */
2737 KeyTyped = old_KeyTyped;
2738}
2739
2740#endif /* FEAT_WINDOWS */
2741
2742/*
2743 * Return TRUE if "buf" is the quickfix buffer.
2744 */
2745 int
2746bt_quickfix(buf)
2747 buf_T *buf;
2748{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002749 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750}
2751
2752/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002753 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2754 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 */
2756 int
2757bt_nofile(buf)
2758 buf_T *buf;
2759{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002760 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2761 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762}
2763
2764/*
2765 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2766 */
2767 int
2768bt_dontwrite(buf)
2769 buf_T *buf;
2770{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002771 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772}
2773
2774 int
2775bt_dontwrite_msg(buf)
2776 buf_T *buf;
2777{
2778 if (bt_dontwrite(buf))
2779 {
2780 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2781 return TRUE;
2782 }
2783 return FALSE;
2784}
2785
2786/*
2787 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2788 * and 'bufhidden'.
2789 */
2790 int
2791buf_hide(buf)
2792 buf_T *buf;
2793{
2794 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2795 switch (buf->b_p_bh[0])
2796 {
2797 case 'u': /* "unload" */
2798 case 'w': /* "wipe" */
2799 case 'd': return FALSE; /* "delete" */
2800 case 'h': return TRUE; /* "hide" */
2801 }
2802 return (p_hid || cmdmod.hide);
2803}
2804
2805/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002806 * Return TRUE when using ":vimgrep" for ":grep".
2807 */
2808 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002809grep_internal(cmdidx)
2810 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002811{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002812 return ((cmdidx == CMD_grep
2813 || cmdidx == CMD_lgrep
2814 || cmdidx == CMD_grepadd
2815 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002816 && STRCMP("internal",
2817 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2818}
2819
2820/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002821 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 */
2823 void
2824ex_make(eap)
2825 exarg_T *eap;
2826{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002827 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 char_u *cmd;
2829 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002830 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002831 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002832 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002833#ifdef FEAT_AUTOCMD
2834 char_u *au_name = NULL;
2835
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002836 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2837 if (grep_internal(eap->cmdidx))
2838 {
2839 ex_vimgrep(eap);
2840 return;
2841 }
2842
Bram Moolenaar7c626922005-02-07 22:01:03 +00002843 switch (eap->cmdidx)
2844 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002845 case CMD_make: au_name = (char_u *)"make"; break;
2846 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2847 case CMD_grep: au_name = (char_u *)"grep"; break;
2848 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2849 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2850 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002851 default: break;
2852 }
2853 if (au_name != NULL)
2854 {
2855 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2856 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002857# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002858 if (did_throw || force_abort)
2859 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002860# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002861 }
2862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
Bram Moolenaara6557602006-02-04 22:43:20 +00002864 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2865 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002866 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002867
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002869 fname = get_mef_name();
2870 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002872 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
2874 /*
2875 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2876 */
2877 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2878 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002879 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 cmd = alloc(len);
2881 if (cmd == NULL)
2882 return;
2883 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2884 (char *)p_shq);
2885 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002886 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 /*
2888 * Output a newline if there's something else than the :make command that
2889 * was typed (in which case the cursor is in column 0).
2890 */
2891 if (msg_col == 0)
2892 msg_didout = FALSE;
2893 msg_start();
2894 MSG_PUTS(":!");
2895 msg_outtrans(cmd); /* show what we are doing */
2896
2897 /* let the shell know if we are redirecting output or not */
2898 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2899
2900#ifdef AMIGA
2901 out_flush();
2902 /* read window status report and redraw before message */
2903 (void)char_avail();
2904#endif
2905
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002906 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002907 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2908 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002909 && eap->cmdidx != CMD_lgrepadd),
2910 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002911 if (wp != NULL)
2912 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002913#ifdef FEAT_AUTOCMD
2914 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002915 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002916 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2917 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002918 if (qi->qf_curlist < qi->qf_listcount)
2919 res = qi->qf_lists[qi->qf_curlist].qf_count;
2920 else
2921 res = 0;
2922 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002923#endif
2924 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002925 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaar7c626922005-02-07 22:01:03 +00002927 mch_remove(fname);
2928 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 vim_free(cmd);
2930}
2931
2932/*
2933 * Return the name for the errorfile, in allocated memory.
2934 * Find a new unique name when 'makeef' contains "##".
2935 * Returns NULL for error.
2936 */
2937 static char_u *
2938get_mef_name()
2939{
2940 char_u *p;
2941 char_u *name;
2942 static int start = -1;
2943 static int off = 0;
2944#ifdef HAVE_LSTAT
2945 struct stat sb;
2946#endif
2947
2948 if (*p_mef == NUL)
2949 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002950 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 if (name == NULL)
2952 EMSG(_(e_notmp));
2953 return name;
2954 }
2955
2956 for (p = p_mef; *p; ++p)
2957 if (p[0] == '#' && p[1] == '#')
2958 break;
2959
2960 if (*p == NUL)
2961 return vim_strsave(p_mef);
2962
2963 /* Keep trying until the name doesn't exist yet. */
2964 for (;;)
2965 {
2966 if (start == -1)
2967 start = mch_get_pid();
2968 else
2969 off += 19;
2970
2971 name = alloc((unsigned)STRLEN(p_mef) + 30);
2972 if (name == NULL)
2973 break;
2974 STRCPY(name, p_mef);
2975 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2976 STRCAT(name, p + 2);
2977 if (mch_getperm(name) < 0
2978#ifdef HAVE_LSTAT
2979 /* Don't accept a symbolic link, its a security risk. */
2980 && mch_lstat((char *)name, &sb) < 0
2981#endif
2982 )
2983 break;
2984 vim_free(name);
2985 }
2986 return name;
2987}
2988
2989/*
2990 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002991 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 */
2993 void
2994ex_cc(eap)
2995 exarg_T *eap;
2996{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002997 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002998
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002999 if (eap->cmdidx == CMD_ll
3000 || eap->cmdidx == CMD_lrewind
3001 || eap->cmdidx == CMD_lfirst
3002 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003003 {
3004 qi = GET_LOC_LIST(curwin);
3005 if (qi == NULL)
3006 {
3007 EMSG(_(e_loclist));
3008 return;
3009 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003010 }
3011
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003012 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 eap->addr_count > 0
3014 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003015 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003017 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3018 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 ? 1
3020 : 32767,
3021 eap->forceit);
3022}
3023
3024/*
3025 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003026 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 */
3028 void
3029ex_cnext(eap)
3030 exarg_T *eap;
3031{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003032 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003033
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003034 if (eap->cmdidx == CMD_lnext
3035 || eap->cmdidx == CMD_lNext
3036 || eap->cmdidx == CMD_lprevious
3037 || eap->cmdidx == CMD_lnfile
3038 || eap->cmdidx == CMD_lNfile
3039 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003040 {
3041 qi = GET_LOC_LIST(curwin);
3042 if (qi == NULL)
3043 {
3044 EMSG(_(e_loclist));
3045 return;
3046 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003047 }
3048
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003049 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003051 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003053 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3054 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 ? BACKWARD_FILE
3056 : BACKWARD,
3057 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
3058}
3059
3060/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003061 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003062 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 */
3064 void
3065ex_cfile(eap)
3066 exarg_T *eap;
3067{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003068 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003069 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003070#ifdef FEAT_AUTOCMD
3071 char_u *au_name = NULL;
3072#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003073
3074 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003075 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003076 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003077
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003078#ifdef FEAT_AUTOCMD
3079 switch (eap->cmdidx)
3080 {
3081 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3082 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3083 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3084 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3085 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3086 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3087 default: break;
3088 }
3089 if (au_name != NULL)
3090 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3091#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003092#ifdef FEAT_BROWSE
3093 if (cmdmod.browse)
3094 {
3095 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3096 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3097 if (browse_file == NULL)
3098 return;
3099 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3100 vim_free(browse_file);
3101 }
3102 else
3103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003105 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003106
3107 /*
3108 * This function is used by the :cfile, :cgetfile and :caddfile
3109 * commands.
3110 * :cfile always creates a new quickfix list and jumps to the
3111 * first error.
3112 * :cgetfile creates a new quickfix list but doesn't jump to the
3113 * first error.
3114 * :caddfile adds to an existing quickfix list. If there is no
3115 * quickfix list then a new list is created.
3116 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003117 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003118 && eap->cmdidx != CMD_laddfile),
3119 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003120 && (eap->cmdidx == CMD_cfile
3121 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003122 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003123#ifdef FEAT_AUTOCMD
3124 if (au_name != NULL)
3125 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3126#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003127 if (wp != NULL)
3128 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003129 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003130 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003131
3132 else
3133 {
3134#ifdef FEAT_AUTOCMD
3135 if (au_name != NULL)
3136 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3137#endif
3138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139}
3140
3141/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003142 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003143 * ":vimgrepadd {pattern} file(s)"
3144 * ":lvimgrep {pattern} file(s)"
3145 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003146 */
3147 void
3148ex_vimgrep(eap)
3149 exarg_T *eap;
3150{
Bram Moolenaar81695252004-12-29 20:58:21 +00003151 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003152 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003153 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003154 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003155 char_u *s;
3156 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003157 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003158 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003159#ifdef FEAT_AUTOCMD
3160 qfline_T *cur_qf_start;
3161#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003162 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003163 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003164 buf_T *buf;
3165 int duplicate_name = FALSE;
3166 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003167 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003168 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003169 buf_T *first_match_buf = NULL;
3170 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003171 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003172#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3173 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003174#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003175 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003176 int flags = 0;
3177 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003178 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003179 char_u *dirname_start = NULL;
3180 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003181 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003182#ifdef FEAT_AUTOCMD
3183 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003184
3185 switch (eap->cmdidx)
3186 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003187 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3188 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3189 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003190 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003191 case CMD_grep: au_name = (char_u *)"grep"; break;
3192 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3193 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3194 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003195 default: break;
3196 }
3197 if (au_name != NULL)
3198 {
3199 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3200 curbuf->b_fname, TRUE, curbuf);
3201 if (did_throw || force_abort)
3202 return;
3203 }
3204#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003205
Bram Moolenaar754b5602006-02-09 23:53:20 +00003206 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003207 || eap->cmdidx == CMD_lvimgrep
3208 || eap->cmdidx == CMD_lgrepadd
3209 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003210 {
3211 qi = ll_get_or_alloc_list(curwin);
3212 if (qi == NULL)
3213 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003214 }
3215
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003216 if (eap->addr_count > 0)
3217 tomatch = eap->line2;
3218 else
3219 tomatch = MAXLNUM;
3220
Bram Moolenaar81695252004-12-29 20:58:21 +00003221 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003222 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003223 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003224 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003225 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003226 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003227 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003228 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003229
3230 if (s != NULL && *s == NUL)
3231 {
3232 /* Pattern is empty, use last search pattern. */
3233 if (last_search_pat() == NULL)
3234 {
3235 EMSG(_(e_noprevre));
3236 goto theend;
3237 }
3238 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3239 }
3240 else
3241 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3242
Bram Moolenaar86b68352004-12-27 21:59:20 +00003243 if (regmatch.regprog == NULL)
3244 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003245 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003246 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003247
3248 p = skipwhite(p);
3249 if (*p == NUL)
3250 {
3251 EMSG(_("E683: File name missing or invalid pattern"));
3252 goto theend;
3253 }
3254
Bram Moolenaar754b5602006-02-09 23:53:20 +00003255 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003256 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003257 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003258 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01003259 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003260 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003261 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003262 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003263 prevp->qf_next != prevp; prevp = prevp->qf_next)
3264 ;
3265
3266 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003267 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003268 goto theend;
3269 if (fcount == 0)
3270 {
3271 EMSG(_(e_nomatch));
3272 goto theend;
3273 }
3274
Bram Moolenaard9462e32011-04-11 21:35:11 +02003275 dirname_start = alloc(MAXPATHL);
3276 dirname_now = alloc(MAXPATHL);
3277 if (dirname_start == NULL || dirname_now == NULL)
3278 goto theend;
3279
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003280 /* Remember the current directory, because a BufRead autocommand that does
3281 * ":lcd %:p:h" changes the meaning of short path names. */
3282 mch_dirname(dirname_start, MAXPATHL);
3283
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003284#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003285 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003286 * changing the current quickfix list. */
3287 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3288#endif
3289
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003290 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003291 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003292 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003293 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003294 if (time(NULL) > seconds)
3295 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003296 /* Display the file name every second or so, show the user we are
3297 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003298 seconds = time(NULL);
3299 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003300 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003301 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003302 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003303 else
3304 {
3305 msg_outtrans(p);
3306 vim_free(p);
3307 }
3308 msg_clr_eos();
3309 msg_didout = FALSE; /* overwrite this message */
3310 msg_nowait = TRUE; /* don't wait for this message */
3311 msg_col = 0;
3312 out_flush();
3313 }
3314
Bram Moolenaar81695252004-12-29 20:58:21 +00003315 buf = buflist_findname_exp(fnames[fi]);
3316 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3317 {
3318 /* Remember that a buffer with this name already exists. */
3319 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003320 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003321 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003322
3323#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3324 /* Don't do Filetype autocommands to avoid loading syntax and
3325 * indent scripts, a great speed improvement. */
3326 save_ei = au_event_disable(",Filetype");
3327#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003328 /* Don't use modelines here, it's useless. */
3329 save_mls = p_mls;
3330 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003331
3332 /* Load file into a buffer, so that 'fileencoding' is detected,
3333 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003334 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003335
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003336 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003337#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3338 au_event_restore(save_ei);
3339#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003340 }
3341 else
3342 /* Use existing, loaded buffer. */
3343 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003344
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003345#ifdef FEAT_AUTOCMD
3346 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3347 {
3348 int idx;
3349
3350 /* Autocommands changed the quickfix list. Find the one we were
3351 * using and restore it. */
3352 for (idx = 0; idx < LISTCOUNT; ++idx)
3353 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3354 {
3355 qi->qf_curlist = idx;
3356 break;
3357 }
3358 if (idx == LISTCOUNT)
3359 {
3360 /* List cannot be found, create a new one. */
3361 qf_new_list(qi, *eap->cmdlinep);
3362 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3363 }
3364 }
3365#endif
3366
Bram Moolenaar81695252004-12-29 20:58:21 +00003367 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003368 {
3369 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003370 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003371 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003372 else
3373 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003374 /* Try for a match in all lines of the buffer.
3375 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003376 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003377 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3378 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003379 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003380 col = 0;
3381 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003382 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003383 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003384 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003385 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003386 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003387 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003388 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003389 ml_get_buf(buf,
3390 regmatch.startpos[0].lnum + lnum, FALSE),
3391 regmatch.startpos[0].lnum + lnum,
3392 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003393 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003394 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003395 0, /* nr */
3396 0, /* type */
3397 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003398 ) == FAIL)
3399 {
3400 got_int = TRUE;
3401 break;
3402 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003403 found_match = TRUE;
3404 if (--tomatch == 0)
3405 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003406 if ((flags & VGR_GLOBAL) == 0
3407 || regmatch.endpos[0].lnum > 0)
3408 break;
3409 col = regmatch.endpos[0].col
3410 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003411 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003412 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003413 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003414 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003415 if (got_int)
3416 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003417 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003418#ifdef FEAT_AUTOCMD
3419 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3420#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003421
3422 if (using_dummy)
3423 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003424 if (found_match && first_match_buf == NULL)
3425 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003426 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003427 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003428 /* Never keep a dummy buffer if there is another buffer
3429 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003430 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003431 buf = NULL;
3432 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003433 else if (!cmdmod.hide
3434 || buf->b_p_bh[0] == 'u' /* "unload" */
3435 || buf->b_p_bh[0] == 'w' /* "wipe" */
3436 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003437 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003438 /* When no match was found we don't need to remember the
3439 * buffer, wipe it out. If there was a match and it
3440 * wasn't the first one or we won't jump there: only
3441 * unload the buffer.
3442 * Ignore 'hidden' here, because it may lead to having too
3443 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003444 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003445 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003446 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003447 buf = NULL;
3448 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003449 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003450 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003451 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003452 buf = NULL;
3453 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003454 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003455
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003456 if (buf != NULL)
3457 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003458 /* If the buffer is still loaded we need to use the
3459 * directory we jumped to below. */
3460 if (buf == first_match_buf
3461 && target_dir == NULL
3462 && STRCMP(dirname_start, dirname_now) != 0)
3463 target_dir = vim_strsave(dirname_now);
3464
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003465 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003466 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003467 * need to be done (again). But not the window-local
3468 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003469 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003470#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003471 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3472 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003473#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003474 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003475 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003476 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003477 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003478 }
3479 }
3480
3481 FreeWild(fcount, fnames);
3482
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003483 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3484 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3485 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003486
3487#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003488 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003489#endif
3490
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003491#ifdef FEAT_AUTOCMD
3492 if (au_name != NULL)
3493 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3494 curbuf->b_fname, TRUE, curbuf);
3495#endif
3496
Bram Moolenaar86b68352004-12-27 21:59:20 +00003497 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003498 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003499 {
3500 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003501 {
3502 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003503 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003504 if (buf != curbuf)
3505 /* If we jumped to another buffer redrawing will already be
3506 * taken care of. */
3507 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003508
3509 /* Jump to the directory used after loading the buffer. */
3510 if (curbuf == first_match_buf && target_dir != NULL)
3511 {
3512 exarg_T ea;
3513
3514 ea.arg = target_dir;
3515 ea.cmdidx = CMD_lcd;
3516 ex_cd(&ea);
3517 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003518 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003519 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003520 else
3521 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003522
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003523 /* If we loaded a dummy buffer into the current window, the autocommands
3524 * may have messed up things, need to redraw and recompute folds. */
3525 if (redraw_for_dummy)
3526 {
3527#ifdef FEAT_FOLDING
3528 foldUpdateAll(curwin);
3529#else
3530 redraw_later(NOT_VALID);
3531#endif
3532 }
3533
Bram Moolenaar86b68352004-12-27 21:59:20 +00003534theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003535 vim_free(dirname_now);
3536 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003537 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003538 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003539}
3540
3541/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003542 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003543 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003544 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3545 * If "s" is not NULL terminate the pattern with a NUL.
3546 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003547 */
3548 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003549skip_vimgrep_pat(p, s, flags)
3550 char_u *p;
3551 char_u **s;
3552 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003553{
3554 int c;
3555
3556 if (vim_isIDc(*p))
3557 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003558 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003559 if (s != NULL)
3560 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003561 p = skiptowhite(p);
3562 if (s != NULL && *p != NUL)
3563 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003564 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003565 else
3566 {
3567 /* ":vimgrep /pattern/[g][j] fname" */
3568 if (s != NULL)
3569 *s = p + 1;
3570 c = *p;
3571 p = skip_regexp(p + 1, c, TRUE, NULL);
3572 if (*p != c)
3573 return NULL;
3574
3575 /* Truncate the pattern. */
3576 if (s != NULL)
3577 *p = NUL;
3578 ++p;
3579
3580 /* Find the flags */
3581 while (*p == 'g' || *p == 'j')
3582 {
3583 if (flags != NULL)
3584 {
3585 if (*p == 'g')
3586 *flags |= VGR_GLOBAL;
3587 else
3588 *flags |= VGR_NOJUMP;
3589 }
3590 ++p;
3591 }
3592 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003593 return p;
3594}
3595
3596/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003597 * Restore current working directory to "dirname_start" if they differ, taking
3598 * into account whether it is set locally or globally.
3599 */
3600 static void
3601restore_start_dir(dirname_start)
3602 char_u *dirname_start;
3603{
3604 char_u *dirname_now = alloc(MAXPATHL);
3605
3606 if (NULL != dirname_now)
3607 {
3608 mch_dirname(dirname_now, MAXPATHL);
3609 if (STRCMP(dirname_start, dirname_now) != 0)
3610 {
3611 /* If the directory has changed, change it back by building up an
3612 * appropriate ex command and executing it. */
3613 exarg_T ea;
3614
3615 ea.arg = dirname_start;
3616 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3617 ex_cd(&ea);
3618 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01003619 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003620 }
3621}
3622
3623/*
3624 * Load file "fname" into a dummy buffer and return the buffer pointer,
3625 * placing the directory resulting from the buffer load into the
3626 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3627 * prior to calling this function. Restores directory to "dirname_start" prior
3628 * to returning, if autocmds or the 'autochdir' option have changed it.
3629 *
3630 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3631 * or wipe_dummy_buffer() later!
3632 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003633 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003634 */
3635 static buf_T *
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003636load_dummy_buffer(fname, dirname_start, resulting_dir)
Bram Moolenaar81695252004-12-29 20:58:21 +00003637 char_u *fname;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003638 char_u *dirname_start; /* in: old directory */
3639 char_u *resulting_dir; /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003640{
3641 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003642 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003643 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003644 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003645
3646 /* Allocate a buffer without putting it in the buffer list. */
3647 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3648 if (newbuf == NULL)
3649 return NULL;
3650
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003651 /* Init the options. */
3652 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3653
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003654 /* need to open the memfile before putting the buffer in a window */
3655 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003656 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003657 /* set curwin/curbuf to buf and save a few things */
3658 aucmd_prepbuf(&aco, newbuf);
3659
3660 /* Need to set the filename for autocommands. */
3661 (void)setfname(curbuf, fname, NULL, FALSE);
3662
Bram Moolenaar81695252004-12-29 20:58:21 +00003663 /* Create swap file now to avoid the ATTENTION message. */
3664 check_need_swap(TRUE);
3665
3666 /* Remove the "dummy" flag, otherwise autocommands may not
3667 * work. */
3668 curbuf->b_flags &= ~BF_DUMMY;
3669
3670 if (readfile(fname, NULL,
3671 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3672 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003673 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003674 && !(curbuf->b_flags & BF_NEW))
3675 {
3676 failed = FALSE;
3677 if (curbuf != newbuf)
3678 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003679 /* Bloody autocommands changed the buffer! Can happen when
3680 * using netrw and editing a remote file. Use the current
3681 * buffer instead, delete the dummy one after restoring the
3682 * window stuff. */
3683 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003684 newbuf = curbuf;
3685 }
3686 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003687
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003688 /* restore curwin/curbuf and a few other things */
3689 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003690 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3691 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003692 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003693
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003694 /*
3695 * When autocommands/'autochdir' option changed directory: go back.
3696 * Let the caller know what the resulting dir was first, in case it is
3697 * important.
3698 */
3699 mch_dirname(resulting_dir, MAXPATHL);
3700 restore_start_dir(dirname_start);
3701
Bram Moolenaar81695252004-12-29 20:58:21 +00003702 if (!buf_valid(newbuf))
3703 return NULL;
3704 if (failed)
3705 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003706 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003707 return NULL;
3708 }
3709 return newbuf;
3710}
3711
3712/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003713 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3714 * directory to "dirname_start" prior to returning, if autocmds or the
3715 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003716 */
3717 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003718wipe_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003719 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003720 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003721{
3722 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003723 {
3724#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3725 cleanup_T cs;
3726
3727 /* Reset the error/interrupt/exception state here so that aborting()
3728 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3729 * work when got_int is set. */
3730 enter_cleanup(&cs);
3731#endif
3732
Bram Moolenaar81695252004-12-29 20:58:21 +00003733 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003734
3735#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3736 /* Restore the error/interrupt/exception state if not discarded by a
3737 * new aborting error, interrupt, or uncaught exception. */
3738 leave_cleanup(&cs);
3739#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003740 /* When autocommands/'autochdir' option changed directory: go back. */
3741 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003742 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003743}
3744
3745/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003746 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3747 * directory to "dirname_start" prior to returning, if autocmds or the
3748 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003749 */
3750 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003751unload_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003752 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003753 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003754{
3755 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003756 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003757 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003758
3759 /* When autocommands/'autochdir' option changed directory: go back. */
3760 restore_start_dir(dirname_start);
3761 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003762}
3763
Bram Moolenaar05159a02005-02-26 23:04:13 +00003764#if defined(FEAT_EVAL) || defined(PROTO)
3765/*
3766 * Add each quickfix error to list "list" as a dictionary.
3767 */
3768 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003769get_errorlist(wp, list)
3770 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003771 list_T *list;
3772{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003773 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003774 dict_T *dict;
3775 char_u buf[2];
3776 qfline_T *qfp;
3777 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003778 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003779
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003780 if (wp != NULL)
3781 {
3782 qi = GET_LOC_LIST(wp);
3783 if (qi == NULL)
3784 return FAIL;
3785 }
3786
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003787 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003788 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003789 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003790
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003791 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3792 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003793 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003794 /* Handle entries with a non-existing buffer number. */
3795 bufnum = qfp->qf_fnum;
3796 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3797 bufnum = 0;
3798
Bram Moolenaar05159a02005-02-26 23:04:13 +00003799 if ((dict = dict_alloc()) == NULL)
3800 return FAIL;
3801 if (list_append_dict(list, dict) == FAIL)
3802 return FAIL;
3803
3804 buf[0] = qfp->qf_type;
3805 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003806 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003807 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3808 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3809 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3810 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003811 || dict_add_nr_str(dict, "pattern", 0L,
3812 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3813 || dict_add_nr_str(dict, "text", 0L,
3814 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003815 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3816 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3817 return FAIL;
3818
3819 qfp = qfp->qf_next;
3820 }
3821 return OK;
3822}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003823
3824/*
3825 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003826 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003827 */
3828 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003829set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003830 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003831 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003832 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003833 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003834{
3835 listitem_T *li;
3836 dict_T *d;
3837 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003838 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003839 long lnum;
3840 int col, nr;
3841 int vcol;
3842 qfline_T *prevp = NULL;
3843 int valid, status;
3844 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003845 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003846 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003847
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003848 if (wp != NULL)
3849 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003850 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003851 if (qi == NULL)
3852 return FAIL;
3853 }
3854
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003855 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003856 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01003857 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003858 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003859 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003860 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003861 prevp->qf_next != prevp; prevp = prevp->qf_next)
3862 ;
3863 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02003864 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003865 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02003866 qf_store_title(qi, title);
3867 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003868
3869 for (li = list->lv_first; li != NULL; li = li->li_next)
3870 {
3871 if (li->li_tv.v_type != VAR_DICT)
3872 continue; /* Skip non-dict items */
3873
3874 d = li->li_tv.vval.v_dict;
3875 if (d == NULL)
3876 continue;
3877
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003878 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003879 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003880 lnum = get_dict_number(d, (char_u *)"lnum");
3881 col = get_dict_number(d, (char_u *)"col");
3882 vcol = get_dict_number(d, (char_u *)"vcol");
3883 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003884 type = get_dict_string(d, (char_u *)"type", TRUE);
3885 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3886 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003887 if (text == NULL)
3888 text = vim_strsave((char_u *)"");
3889
3890 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003891 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003892 valid = FALSE;
3893
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003894 /* Mark entries with non-existing buffer number as not valid. Give the
3895 * error message only once. */
3896 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3897 {
3898 if (!did_bufnr_emsg)
3899 {
3900 did_bufnr_emsg = TRUE;
3901 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3902 }
3903 valid = FALSE;
3904 bufnum = 0;
3905 }
3906
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003907 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003908 NULL, /* dir */
3909 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003910 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003911 text,
3912 lnum,
3913 col,
3914 vcol, /* vis_col */
3915 pattern, /* search pattern */
3916 nr,
3917 type == NULL ? NUL : *type,
3918 valid);
3919
3920 vim_free(filename);
3921 vim_free(pattern);
3922 vim_free(text);
3923 vim_free(type);
3924
3925 if (status == FAIL)
3926 {
3927 retval = FAIL;
3928 break;
3929 }
3930 }
3931
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003932 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003933 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003934 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3935 else
3936 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003937 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3938 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003939
3940#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003941 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003942#endif
3943
3944 return retval;
3945}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003946#endif
3947
Bram Moolenaar81695252004-12-29 20:58:21 +00003948/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003949 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003950 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003951 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003952 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003953 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003954 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003955 */
3956 void
3957ex_cbuffer(eap)
3958 exarg_T *eap;
3959{
3960 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003961 qf_info_T *qi = &ql_info;
3962
Bram Moolenaardb552d602006-03-23 22:59:57 +00003963 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3964 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003965 {
3966 qi = ll_get_or_alloc_list(curwin);
3967 if (qi == NULL)
3968 return;
3969 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003970
3971 if (*eap->arg == NUL)
3972 buf = curbuf;
3973 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3974 buf = buflist_findnr(atoi((char *)eap->arg));
3975 if (buf == NULL)
3976 EMSG(_(e_invarg));
3977 else if (buf->b_ml.ml_mfp == NULL)
3978 EMSG(_("E681: Buffer is not loaded"));
3979 else
3980 {
3981 if (eap->addr_count == 0)
3982 {
3983 eap->line1 = 1;
3984 eap->line2 = buf->b_ml.ml_line_count;
3985 }
3986 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3987 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3988 EMSG(_(e_invrange));
3989 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003990 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003991 char_u *qf_title = *eap->cmdlinep;
3992
3993 if (buf->b_sfname)
3994 {
3995 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3996 (char *)qf_title, (char *)buf->b_sfname);
3997 qf_title = IObuff;
3998 }
3999
Bram Moolenaardb552d602006-03-23 22:59:57 +00004000 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4001 (eap->cmdidx != CMD_caddbuffer
4002 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004003 eap->line1, eap->line2,
4004 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004005 && (eap->cmdidx == CMD_cbuffer
4006 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004007 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4008 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004009 }
4010}
4011
Bram Moolenaar1e015462005-09-25 22:16:38 +00004012#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004013/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004014 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4015 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004016 */
4017 void
4018ex_cexpr(eap)
4019 exarg_T *eap;
4020{
4021 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004022 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004023
Bram Moolenaardb552d602006-03-23 22:59:57 +00004024 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4025 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004026 {
4027 qi = ll_get_or_alloc_list(curwin);
4028 if (qi == NULL)
4029 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004030 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004031
Bram Moolenaar4770d092006-01-12 23:22:24 +00004032 /* Evaluate the expression. When the result is a string or a list we can
4033 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004034 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004035 if (tv != NULL)
4036 {
4037 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4038 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4039 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00004040 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
4041 (eap->cmdidx != CMD_caddexpr
4042 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004043 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004044 && (eap->cmdidx == CMD_cexpr
4045 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004046 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004047 }
4048 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004049 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004050 free_tv(tv);
4051 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004052}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004053#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004054
4055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 * ":helpgrep {pattern}"
4057 */
4058 void
4059ex_helpgrep(eap)
4060 exarg_T *eap;
4061{
4062 regmatch_T regmatch;
4063 char_u *save_cpo;
4064 char_u *p;
4065 int fcount;
4066 char_u **fnames;
4067 FILE *fd;
4068 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004069 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004071#ifdef FEAT_MULTI_LANG
4072 char_u *lang;
4073#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004074 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004075 int new_qi = FALSE;
4076 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004077#ifdef FEAT_AUTOCMD
4078 char_u *au_name = NULL;
4079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004081#ifdef FEAT_MULTI_LANG
4082 /* Check for a specified language */
4083 lang = check_help_lang(eap->arg);
4084#endif
4085
Bram Moolenaar73633f82012-01-20 13:39:07 +01004086#ifdef FEAT_AUTOCMD
4087 switch (eap->cmdidx)
4088 {
4089 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4090 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4091 default: break;
4092 }
4093 if (au_name != NULL)
4094 {
4095 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4096 curbuf->b_fname, TRUE, curbuf);
4097 if (did_throw || force_abort)
4098 return;
4099 }
4100#endif
4101
4102 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4103 save_cpo = p_cpo;
4104 p_cpo = empty_option;
4105
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004106 if (eap->cmdidx == CMD_lhelpgrep)
4107 {
4108 /* Find an existing help window */
4109 FOR_ALL_WINDOWS(wp)
4110 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4111 break;
4112
4113 if (wp == NULL) /* Help window not found */
4114 qi = NULL;
4115 else
4116 qi = wp->w_llist;
4117
4118 if (qi == NULL)
4119 {
4120 /* Allocate a new location list for help text matches */
4121 if ((qi = ll_new_list()) == NULL)
4122 return;
4123 new_qi = TRUE;
4124 }
4125 }
4126
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4128 regmatch.rm_ic = FALSE;
4129 if (regmatch.regprog != NULL)
4130 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004131#ifdef FEAT_MBYTE
4132 vimconv_T vc;
4133
4134 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4135 * differs. */
4136 vc.vc_type = CONV_NONE;
4137 if (!enc_utf8)
4138 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4139#endif
4140
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004142 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144 /* Go through all directories in 'runtimepath' */
4145 p = p_rtp;
4146 while (*p != NUL && !got_int)
4147 {
4148 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4149
4150 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4151 add_pathsep(NameBuff);
4152 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4153 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4154 &fnames, EW_FILE|EW_SILENT) == OK
4155 && fcount > 0)
4156 {
4157 for (fi = 0; fi < fcount && !got_int; ++fi)
4158 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004159#ifdef FEAT_MULTI_LANG
4160 /* Skip files for a different language. */
4161 if (lang != NULL
4162 && STRNICMP(lang, fnames[fi]
4163 + STRLEN(fnames[fi]) - 3, 2) != 0
4164 && !(STRNICMP(lang, "en", 2) == 0
4165 && STRNICMP("txt", fnames[fi]
4166 + STRLEN(fnames[fi]) - 3, 3) == 0))
4167 continue;
4168#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004169 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 if (fd != NULL)
4171 {
4172 lnum = 1;
4173 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4174 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004175 char_u *line = IObuff;
4176#ifdef FEAT_MBYTE
4177 /* Convert a line if 'encoding' is not utf-8 and
4178 * the line contains a non-ASCII character. */
4179 if (vc.vc_type != CONV_NONE
4180 && has_non_ascii(IObuff)) {
4181 line = string_convert(&vc, IObuff, NULL);
4182 if (line == NULL)
4183 line = IObuff;
4184 }
4185#endif
4186
4187 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004189 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
4191 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004192 while (l > 0 && line[l - 1] <= ' ')
4193 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004195 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 NULL, /* dir */
4197 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004198 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004199 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004201 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004202 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004203 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004204 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 0, /* nr */
4206 1, /* type */
4207 TRUE /* valid */
4208 ) == FAIL)
4209 {
4210 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004211#ifdef FEAT_MBYTE
4212 if (line != IObuff)
4213 vim_free(line);
4214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 break;
4216 }
4217 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004218#ifdef FEAT_MBYTE
4219 if (line != IObuff)
4220 vim_free(line);
4221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 ++lnum;
4223 line_breakcheck();
4224 }
4225 fclose(fd);
4226 }
4227 }
4228 FreeWild(fcount, fnames);
4229 }
4230 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004231
Bram Moolenaar473de612013-06-08 18:19:48 +02004232 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004233#ifdef FEAT_MBYTE
4234 if (vc.vc_type != CONV_NONE)
4235 convert_setup(&vc, NULL, NULL);
4236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004238 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4239 qi->qf_lists[qi->qf_curlist].qf_ptr =
4240 qi->qf_lists[qi->qf_curlist].qf_start;
4241 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004244 if (p_cpo == empty_option)
4245 p_cpo = save_cpo;
4246 else
4247 /* Darn, some plugin changed the value. */
4248 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
4250#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004251 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252#endif
4253
Bram Moolenaar73633f82012-01-20 13:39:07 +01004254#ifdef FEAT_AUTOCMD
4255 if (au_name != NULL)
4256 {
4257 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4258 curbuf->b_fname, TRUE, curbuf);
4259 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4260 /* autocommands made "qi" invalid */
4261 return;
4262 }
4263#endif
4264
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004266 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004267 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004268 else
4269 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004270
4271 if (eap->cmdidx == CMD_lhelpgrep)
4272 {
4273 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004274 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004275 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4276 {
4277 if (new_qi)
4278 ll_free_all(&qi);
4279 }
4280 else if (curwin->w_llist == NULL)
4281 curwin->w_llist = qi;
4282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283}
4284
4285#endif /* FEAT_QUICKFIX */