blob: 905173d5cd2123e82cf8e75e7d3f4fc56fd2c3a5 [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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100109static int qf_init_ext(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);
110static void qf_store_title(qf_info_T *qi, char_u *title);
111static void qf_new_list(qf_info_T *qi, char_u *qf_title);
112static void ll_free_all(qf_info_T **pqi);
113static int qf_add_entry(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);
114static qf_info_T *ll_new_list(void);
115static void qf_msg(qf_info_T *qi);
116static void qf_free(qf_info_T *qi, int idx);
117static char_u *qf_types(int, int);
118static int qf_get_fnum(char_u *, char_u *);
119static char_u *qf_push_dir(char_u *, struct dir_stack_T **);
120static char_u *qf_pop_dir(struct dir_stack_T **);
121static char_u *qf_guess_filepath(char_u *);
122static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
123static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100125static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
126static int is_qf_win(win_T *win, qf_info_T *qi);
127static win_T *qf_find_win(qf_info_T *qi);
128static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200129static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200131static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static char_u *get_mef_name(void);
134static void restore_start_dir(char_u *dirname_start);
135static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
136static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
137static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
138static qf_info_T *ll_get_or_alloc_list(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 Moolenaar05540972016-01-30 20:31:25 +0100156qf_init(
157 win_T *wp,
158 char_u *efile,
159 char_u *errorformat,
160 int newlist, /* TRUE: start a new error list */
161 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 Moolenaard12f5c12006-01-25 22:10:52 +0000165 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000166 {
167 qi = ll_get_or_alloc_list(wp);
168 if (qi == NULL)
169 return FAIL;
170 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000171
172 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200173 (linenr_T)0, (linenr_T)0,
174 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000175}
176
177/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200178 * Maximum number of bytes allowed per line while reading a errorfile.
179 */
180#define LINE_MAXLEN 4096
181
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200182 static char_u *
183qf_grow_linebuf(char_u **growbuf, int *growbufsiz, int newsz, int *allocsz)
184{
185 /*
186 * If the line exceeds LINE_MAXLEN exclude the last
187 * byte since it's not a NL character.
188 */
189 *allocsz = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
190 if (*growbuf == NULL)
191 {
192 *growbuf = alloc(*allocsz + 1);
193 if (*growbuf == NULL)
194 return NULL;
195 *growbufsiz = *allocsz;
196 }
197 else if (*allocsz > *growbufsiz)
198 {
199 *growbuf = vim_realloc(*growbuf, *allocsz + 1);
200 if (*growbuf == NULL)
201 return NULL;
202 *growbufsiz = *allocsz;
203 }
204 return *growbuf;
205}
206
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200207/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000208 * Read the errorfile "efile" into memory, line by line, building the error
209 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +0200210 * Alternative: when "efile" is NULL read errors from buffer "buf".
211 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000212 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200213 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
214 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000215 * Return -1 for error, number of errors for success.
216 */
217 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100218qf_init_ext(
219 qf_info_T *qi,
220 char_u *efile,
221 buf_T *buf,
222 typval_T *tv,
223 char_u *errorformat,
224 int newlist, /* TRUE: start a new error list */
225 linenr_T lnumfirst, /* first line number to use */
226 linenr_T lnumlast, /* last line number to use */
227 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000228{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 char_u *namebuf;
230 char_u *errmsg;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200231 int errmsglen;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000232 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 char_u *fmtstr = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200234 char_u *growbuf = NULL;
235 int growbuflen;
Bram Moolenaar9a3b3312016-05-01 20:20:49 +0200236 int growbufsiz = 0;
237 char_u *linebuf = NULL;
238 int linelen = 0;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200239 int discard;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 int type = 0;
243 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000244 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 long lnum = 0L;
246 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000247 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000248 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar864293a2016-06-02 13:40:04 +0200249#ifdef FEAT_WINDOWS
250 qfline_T *old_last = NULL;
251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000253 efm_T *fmt_first = NULL;
254 efm_T *fmt_last = NULL;
255 efm_T *fmt_ptr;
256 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 char_u *efm;
258 char_u *ptr;
259 char_u *srcptr;
260 int len;
261 int i;
262 int round;
263 int idx = 0;
264 int multiline = FALSE;
265 int multiignore = FALSE;
266 int multiscan = FALSE;
267 int retval = -1; /* default: return error flag */
268 char_u *directory = NULL;
269 char_u *currfile = NULL;
270 char_u *tail = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200271 char_u *p_buf = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000272 char_u *p_str = NULL;
273 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 struct dir_stack_T *file_stack = NULL;
275 regmatch_T regmatch;
276 static struct fmtpattern
277 {
278 char_u convchar;
279 char *pattern;
280 } fmt_pat[FMT_PATTERNS] =
281 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000282 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 {'n', "\\d\\+"},
284 {'l', "\\d\\+"},
285 {'c', "\\d\\+"},
286 {'t', "."},
287 {'m', ".\\+"},
288 {'r', ".*"},
Bram Moolenaarf13de072012-06-01 18:34:41 +0200289 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000290 {'v', "\\d\\+"},
291 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 };
293
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100294 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200295 errmsglen = CMDBUFFSIZE + 1;
296 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100297 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000298 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 goto qf_init_end;
300
Bram Moolenaar86b68352004-12-27 21:59:20 +0000301 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 {
303 EMSG2(_(e_openerrf), efile);
304 goto qf_init_end;
305 }
306
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000307 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100309 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000310 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +0200311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000313 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200314 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 ;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200316 old_last = qfprev;
317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318
319/*
320 * Each part of the format string is copied and modified from errorformat to
321 * regex prog. Only a few % characters are allowed.
322 */
323 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000324 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000325 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 else
327 efm = errorformat;
328 /*
329 * Get some space to modify the format string into.
330 */
331 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
332 for (round = FMT_PATTERNS; round > 0; )
333 i += (int)STRLEN(fmt_pat[--round].pattern);
334#ifdef COLON_IN_FILENAME
335 i += 12; /* "%f" can become twelve chars longer */
336#else
337 i += 2; /* "%f" can become two chars longer */
338#endif
339 if ((fmtstr = alloc(i)) == NULL)
340 goto error2;
341
Bram Moolenaar01265852006-03-20 21:50:15 +0000342 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 {
344 /*
345 * Allocate a new eformat structure and put it at the end of the list
346 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000347 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 if (fmt_ptr == NULL)
349 goto error2;
350 if (fmt_first == NULL) /* first one */
351 fmt_first = fmt_ptr;
352 else
353 fmt_last->next = fmt_ptr;
354 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
356 /*
357 * Isolate one part in the 'errorformat' option
358 */
359 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
360 if (efm[len] == '\\' && efm[len + 1] != NUL)
361 ++len;
362
363 /*
364 * Build regexp pattern from current 'errorformat' option
365 */
366 ptr = fmtstr;
367 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000368 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 for (efmp = efm; efmp < efm + len; ++efmp)
370 {
371 if (*efmp == '%')
372 {
373 ++efmp;
374 for (idx = 0; idx < FMT_PATTERNS; ++idx)
375 if (fmt_pat[idx].convchar == *efmp)
376 break;
377 if (idx < FMT_PATTERNS)
378 {
379 if (fmt_ptr->addr[idx])
380 {
381 sprintf((char *)errmsg,
382 _("E372: Too many %%%c in format string"), *efmp);
383 EMSG(errmsg);
384 goto error2;
385 }
386 if ((idx
387 && idx < 6
388 && vim_strchr((char_u *)"DXOPQ",
389 fmt_ptr->prefix) != NULL)
390 || (idx == 6
391 && vim_strchr((char_u *)"OPQ",
392 fmt_ptr->prefix) == NULL))
393 {
394 sprintf((char *)errmsg,
395 _("E373: Unexpected %%%c in format string"), *efmp);
396 EMSG(errmsg);
397 goto error2;
398 }
399 fmt_ptr->addr[idx] = (char_u)++round;
400 *ptr++ = '\\';
401 *ptr++ = '(';
402#ifdef BACKSLASH_IN_FILENAME
403 if (*efmp == 'f')
404 {
405 /* Also match "c:" in the file name, even when
406 * checking for a colon next: "%f:".
407 * "\%(\a:\)\=" */
408 STRCPY(ptr, "\\%(\\a:\\)\\=");
409 ptr += 10;
410 }
411#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000412 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000414 if (efmp[1] != '\\' && efmp[1] != '%')
415 {
416 /* A file name may contain spaces, but this isn't
417 * in "\f". For "%f:%l:%m" there may be a ":" in
418 * the file name. Use ".\{-1,}x" instead (x is
419 * the next character), the requirement that :999:
420 * follows should work. */
421 STRCPY(ptr, ".\\{-1,}");
422 ptr += 7;
423 }
424 else
425 {
426 /* File name followed by '\\' or '%': include as
427 * many file name chars as possible. */
428 STRCPY(ptr, "\\f\\+");
429 ptr += 4;
430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
432 else
433 {
434 srcptr = (char_u *)fmt_pat[idx].pattern;
435 while ((*ptr = *srcptr++) != NUL)
436 ++ptr;
437 }
438 *ptr++ = '\\';
439 *ptr++ = ')';
440 }
441 else if (*efmp == '*')
442 {
443 if (*++efmp == '[' || *efmp == '\\')
444 {
445 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
446 {
447 if (efmp[1] == '^')
448 *ptr++ = *++efmp;
449 if (efmp < efm + len)
450 {
451 *ptr++ = *++efmp; /* could be ']' */
452 while (efmp < efm + len
453 && (*ptr++ = *++efmp) != ']')
454 /* skip */;
455 if (efmp == efm + len)
456 {
457 EMSG(_("E374: Missing ] in format string"));
458 goto error2;
459 }
460 }
461 }
462 else if (efmp < efm + len) /* %*\D, %*\s etc. */
463 *ptr++ = *++efmp;
464 *ptr++ = '\\';
465 *ptr++ = '+';
466 }
467 else
468 {
469 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
470 sprintf((char *)errmsg,
471 _("E375: Unsupported %%%c in format string"), *efmp);
472 EMSG(errmsg);
473 goto error2;
474 }
475 }
476 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
477 *ptr++ = *efmp; /* regexp magic characters */
478 else if (*efmp == '#')
479 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000480 else if (*efmp == '>')
481 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 else if (efmp == efm + 1) /* analyse prefix */
483 {
484 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
485 fmt_ptr->flags = *efmp++;
486 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
487 fmt_ptr->prefix = *efmp;
488 else
489 {
490 sprintf((char *)errmsg,
491 _("E376: Invalid %%%c in format string prefix"), *efmp);
492 EMSG(errmsg);
493 goto error2;
494 }
495 }
496 else
497 {
498 sprintf((char *)errmsg,
499 _("E377: Invalid %%%c in format string"), *efmp);
500 EMSG(errmsg);
501 goto error2;
502 }
503 }
504 else /* copy normal character */
505 {
506 if (*efmp == '\\' && efmp + 1 < efm + len)
507 ++efmp;
508 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
509 *ptr++ = '\\'; /* escape regexp atoms */
510 if (*efmp)
511 *ptr++ = *efmp;
512 }
513 }
514 *ptr++ = '$';
515 *ptr = NUL;
516 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
517 goto error2;
518 /*
519 * Advance to next part
520 */
521 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
522 }
523 if (fmt_first == NULL) /* nothing found */
524 {
525 EMSG(_("E378: 'errorformat' contains no pattern"));
526 goto error2;
527 }
528
529 /*
530 * got_int is reset here, because it was probably set when killing the
531 * ":make" command, but we still want to read the errorfile then.
532 */
533 got_int = FALSE;
534
535 /* Always ignore case when looking for a matching error. */
536 regmatch.rm_ic = TRUE;
537
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000538 if (tv != NULL)
539 {
540 if (tv->v_type == VAR_STRING)
541 p_str = tv->vval.v_string;
542 else if (tv->v_type == VAR_LIST)
543 p_li = tv->vval.v_list->lv_first;
544 }
545
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 /*
547 * Read the lines in the error file one by one.
548 * Try to recognize one of the error formats in each line.
549 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000550 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000552 /* Get the next line. */
553 if (fd == NULL)
554 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000555 if (tv != NULL)
556 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000557 if (tv->v_type == VAR_STRING)
558 {
559 /* Get the next line from the supplied string */
560 char_u *p;
561
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200562 if (*p_str == NUL) /* Reached the end of the string */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000563 break;
564
565 p = vim_strchr(p_str, '\n');
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200566 if (p != NULL)
567 len = (int)(p - p_str) + 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000568 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000569 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000570
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200571 if (len > IOSIZE - 2)
572 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200573 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
574 &linelen);
575 if (linebuf == NULL)
576 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200577 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000578 else
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200579 {
580 linebuf = IObuff;
581 linelen = len;
582 }
583 vim_strncpy(linebuf, p_str, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000584
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200585 /*
586 * Increment using len in order to discard the rest of the
587 * line if it exceeds LINE_MAXLEN.
588 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000589 p_str += len;
590 }
591 else if (tv->v_type == VAR_LIST)
592 {
593 /* Get the next line from the supplied list */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200594 while (p_li != NULL
595 && (p_li->li_tv.v_type != VAR_STRING
596 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000597 p_li = p_li->li_next; /* Skip non-string items */
598
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200599 if (p_li == NULL) /* End of the list */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000600 break;
601
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000602 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200603 if (len > IOSIZE - 2)
604 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200605 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
606 &linelen);
607 if (linebuf == NULL)
608 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200609 }
610 else
611 {
612 linebuf = IObuff;
613 linelen = len;
614 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000615
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200616 vim_strncpy(linebuf, p_li->li_tv.vval.v_string, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000617
618 p_li = p_li->li_next; /* next item */
619 }
620 }
621 else
622 {
623 /* Get the next line from the supplied buffer */
624 if (buflnum > lnumlast)
625 break;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200626 p_buf = ml_get_buf(buf, buflnum++, FALSE);
627 linelen = (int)STRLEN(p_buf);
628 if (linelen > IOSIZE - 2)
629 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200630 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
631 &linelen);
632 if (linebuf == NULL)
633 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200634 }
635 else
636 linebuf = IObuff;
637 vim_strncpy(linebuf, p_buf, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000638 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000639 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200640 else
641 {
642 if (fgets((char *)IObuff, IOSIZE, fd) == NULL)
643 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000644
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200645 discard = FALSE;
646 linelen = (int)STRLEN(IObuff);
647 if (linelen == IOSIZE - 1 && (IObuff[linelen - 1] != '\n'
648#ifdef USE_CRNL
649 || IObuff[linelen - 1] != '\r'
650#endif
651 ))
652 {
653 /*
654 * The current line exceeds IObuff, continue reading using
655 * growbuf until EOL or LINE_MAXLEN bytes is read.
656 */
657 if (growbuf == NULL)
658 {
659 growbufsiz = 2 * (IOSIZE - 1);
660 growbuf = alloc(growbufsiz);
661 if (growbuf == NULL)
662 goto qf_init_end;
663 }
664
665 /* Copy the read part of the line, excluding null-terminator */
666 memcpy(growbuf, IObuff, IOSIZE - 1);
667 growbuflen = linelen;
668
669 for (;;)
670 {
671 if (fgets((char *)growbuf + growbuflen,
672 growbufsiz - growbuflen, fd) == NULL)
673 break;
Bram Moolenaard9db8b42016-05-08 12:52:05 +0200674 linelen = (int)STRLEN(growbuf + growbuflen);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200675 growbuflen += linelen;
676 if (growbuf[growbuflen - 1] == '\n'
677#ifdef USE_CRNL
678 || growbuf[growbuflen - 1] == '\r'
679#endif
680 )
681 break;
682 if (growbufsiz == LINE_MAXLEN)
683 {
684 discard = TRUE;
685 break;
686 }
687
688 growbufsiz = 2 * growbufsiz < LINE_MAXLEN
689 ? 2 * growbufsiz : LINE_MAXLEN;
690 growbuf = vim_realloc(growbuf, 2 * growbufsiz);
691 if (growbuf == NULL)
692 goto qf_init_end;
693 }
694
695 while (discard)
696 {
697 /*
698 * The current line is longer than LINE_MAXLEN, continue
699 * reading but discard everything until EOL or EOF is
700 * reached.
701 */
702 if (fgets((char *)IObuff, IOSIZE, fd) == NULL
703 || (int)STRLEN(IObuff) < IOSIZE - 1
704 || IObuff[IOSIZE - 1] == '\n'
705#ifdef USE_CRNL
706 || IObuff[IOSIZE - 1] == '\r'
707#endif
708 )
709 break;
710 }
711
712 linebuf = growbuf;
713 linelen = growbuflen;
714 }
715 else
716 linebuf = IObuff;
717 }
718
719 if (linelen > 0 && linebuf[linelen - 1] == '\n')
720 linebuf[linelen - 1] = NUL;
721#ifdef USE_CRNL
722 if (linelen > 0 && linebuf[linelen - 1] == '\r')
723 linebuf[linelen - 1] = NUL;
Bram Moolenaar836082d2011-08-10 13:21:46 +0200724#endif
725
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200726#ifdef FEAT_MBYTE
727 remove_bom(linebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#endif
729
Bram Moolenaar01265852006-03-20 21:50:15 +0000730 /* If there was no %> item start at the first pattern */
731 if (fmt_start == NULL)
732 fmt_ptr = fmt_first;
733 else
734 {
735 fmt_ptr = fmt_start;
736 fmt_start = NULL;
737 }
738
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 /*
740 * Try to match each part of 'errorformat' until we find a complete
741 * match or no match.
742 */
743 valid = TRUE;
744restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000745 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100747 int r;
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 idx = fmt_ptr->prefix;
750 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
751 continue;
752 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000753 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 if (!multiscan)
755 errmsg[0] = NUL;
756 lnum = 0;
757 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000758 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 enr = -1;
760 type = 0;
761 tail = NULL;
762
763 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200764 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100765 fmt_ptr->prog = regmatch.regprog;
766 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 {
768 if ((idx == 'C' || idx == 'Z') && !multiline)
769 continue;
770 if (vim_strchr((char_u *)"EWI", idx) != NULL)
771 type = idx;
772 else
773 type = 0;
774 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000775 * Extract error message data from matched line.
776 * We check for an actual submatch, because "\[" and "\]" in
777 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 */
779 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
780 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000781 int c;
782
783 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
784 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000785
786 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000787 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000788 *regmatch.endp[i] = NUL;
789 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
790 *regmatch.endp[i] = c;
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000793 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 continue;
795 }
796 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000797 {
798 if (regmatch.startp[i] == NULL)
799 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000803 {
804 if (regmatch.startp[i] == NULL)
805 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000809 {
810 if (regmatch.startp[i] == NULL)
811 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000815 {
816 if (regmatch.startp[i] == NULL)
817 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000819 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000820 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200821 {
822 if (linelen > errmsglen) {
823 /* linelen + null terminator */
824 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
825 goto qf_init_end;
826 errmsglen = linelen + 1;
827 }
828 vim_strncpy(errmsg, linebuf, linelen);
829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
831 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000832 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
833 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200835 if (len > errmsglen) {
836 /* len + null terminator */
837 if ((errmsg = vim_realloc(errmsg, len + 1))
838 == NULL)
839 goto qf_init_end;
840 errmsglen = len + 1;
841 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000842 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000845 {
846 if (regmatch.startp[i] == NULL)
847 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
851 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200852 char_u *match_ptr;
853
Bram Moolenaar4169da72006-06-20 18:49:32 +0000854 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
855 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200856 col = 0;
857 for (match_ptr = regmatch.startp[i];
858 match_ptr != regmatch.endp[i]; ++match_ptr)
859 {
860 ++col;
861 if (*match_ptr == TAB)
862 {
863 col += 7;
864 col -= col % 8;
865 }
866 }
867 ++col;
868 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
870 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
871 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000872 if (regmatch.startp[i] == NULL)
873 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000875 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000877 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
878 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000879 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
880 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000881 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
882 if (len > CMDBUFFSIZE - 5)
883 len = CMDBUFFSIZE - 5;
884 STRCPY(pattern, "^\\V");
885 STRNCAT(pattern, regmatch.startp[i], len);
886 pattern[len + 3] = '\\';
887 pattern[len + 4] = '$';
888 pattern[len + 5] = NUL;
889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 break;
891 }
892 }
893 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000894
Bram Moolenaar4770d092006-01-12 23:22:24 +0000895 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000897 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
899 if (idx == 'D') /* enter directory */
900 {
901 if (*namebuf == NUL)
902 {
903 EMSG(_("E379: Missing or empty directory name"));
904 goto error2;
905 }
906 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
907 goto error2;
908 }
909 else if (idx == 'X') /* leave directory */
910 directory = qf_pop_dir(&dir_stack);
911 }
912 namebuf[0] = NUL; /* no match found, remove file name */
913 lnum = 0; /* don't jump to this line */
914 valid = FALSE;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200915 if (linelen > errmsglen) {
916 /* linelen + null terminator */
917 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
918 goto qf_init_end;
919 errmsglen = linelen + 1;
920 }
921 /* copy whole line to error message */
922 vim_strncpy(errmsg, linebuf, linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000923 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 multiline = multiignore = FALSE;
925 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000926 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000928 /* honor %> item */
929 if (fmt_ptr->conthere)
930 fmt_start = fmt_ptr;
931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100935 multiignore = FALSE; /* reset continuation */
936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
938 { /* continuation of multi-line msg */
939 if (qfprev == NULL)
940 goto error2;
941 if (*errmsg && !multiignore)
942 {
943 len = (int)STRLEN(qfprev->qf_text);
944 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
945 == NULL)
946 goto error2;
947 STRCPY(ptr, qfprev->qf_text);
948 vim_free(qfprev->qf_text);
949 qfprev->qf_text = ptr;
950 *(ptr += len) = '\n';
951 STRCPY(++ptr, errmsg);
952 }
953 if (qfprev->qf_nr == -1)
954 qfprev->qf_nr = enr;
955 if (vim_isprintc(type) && !qfprev->qf_type)
956 qfprev->qf_type = type; /* only printable chars allowed */
957 if (!qfprev->qf_lnum)
958 qfprev->qf_lnum = lnum;
959 if (!qfprev->qf_col)
960 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000961 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (!qfprev->qf_fnum)
963 qfprev->qf_fnum = qf_get_fnum(directory,
964 *namebuf || directory ? namebuf
965 : currfile && valid ? currfile : 0);
966 if (idx == 'Z')
967 multiline = multiignore = FALSE;
968 line_breakcheck();
969 continue;
970 }
971 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
972 {
973 /* global file names */
974 valid = FALSE;
975 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
976 {
977 if (*namebuf && idx == 'P')
978 currfile = qf_push_dir(namebuf, &file_stack);
979 else if (idx == 'Q')
980 currfile = qf_pop_dir(&file_stack);
981 *namebuf = NUL;
982 if (tail && *tail)
983 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000984 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 multiscan = TRUE;
986 goto restofline;
987 }
988 }
989 }
990 if (fmt_ptr->flags == '-') /* generally exclude this line */
991 {
992 if (multiline)
993 multiignore = TRUE; /* also exclude continuation lines */
994 continue;
995 }
996 }
997
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000998 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001000 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001002 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001003 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 errmsg,
1005 lnum,
1006 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001007 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001008 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 enr,
1010 type,
1011 valid) == FAIL)
1012 goto error2;
1013 line_breakcheck();
1014 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00001015 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001017 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001019 /* no valid entry found */
1020 qi->qf_lists[qi->qf_curlist].qf_ptr =
1021 qi->qf_lists[qi->qf_curlist].qf_start;
1022 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1023 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 }
1025 else
1026 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001027 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1028 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1029 qi->qf_lists[qi->qf_curlist].qf_ptr =
1030 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001032 /* return number of matches */
1033 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 goto qf_init_ok;
1035 }
1036 EMSG(_(e_readerrf));
1037error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001038 qf_free(qi, qi->qf_curlist);
1039 qi->qf_listcount--;
1040 if (qi->qf_curlist > 0)
1041 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +00001043 if (fd != NULL)
1044 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
1046 {
1047 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02001048 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 vim_free(fmt_ptr);
1050 }
1051 qf_clean_dir_stack(&dir_stack);
1052 qf_clean_dir_stack(&file_stack);
1053qf_init_end:
1054 vim_free(namebuf);
1055 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001056 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 vim_free(fmtstr);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001058 vim_free(growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001061 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062#endif
1063
1064 return retval;
1065}
1066
Bram Moolenaarfb604092014-07-23 15:55:00 +02001067 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001068qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001069{
1070 if (title != NULL)
1071 {
1072 char_u *p = alloc((int)STRLEN(title) + 2);
1073
1074 qi->qf_lists[qi->qf_curlist].qf_title = p;
1075 if (p != NULL)
1076 sprintf((char *)p, ":%s", (char *)title);
1077 }
1078}
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080/*
1081 * Prepare for adding a new quickfix list.
1082 */
1083 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001084qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 int i;
1087
1088 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001089 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 * the current entry. This makes it possible to browse in a tree-like
1091 * way with ":grep'.
1092 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001093 while (qi->qf_listcount > qi->qf_curlist + 1)
1094 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
1096 /*
1097 * When the stack is full, remove to oldest entry
1098 * Otherwise, add a new entry.
1099 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001100 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001102 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001104 qi->qf_lists[i - 1] = qi->qf_lists[i];
1105 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001108 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001109 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001110 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111}
1112
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001113/*
1114 * Free a location list
1115 */
1116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001117ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001118{
1119 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001120 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001121
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001122 qi = *pqi;
1123 if (qi == NULL)
1124 return;
1125 *pqi = NULL; /* Remove reference to this list */
1126
1127 qi->qf_refcount--;
1128 if (qi->qf_refcount < 1)
1129 {
1130 /* No references to this location list */
1131 for (i = 0; i < qi->qf_listcount; ++i)
1132 qf_free(qi, i);
1133 vim_free(qi);
1134 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001135}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001136
1137 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001138qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001139{
1140 int i;
1141 qf_info_T *qi = &ql_info;
1142
1143 if (wp != NULL)
1144 {
1145 /* location list */
1146 ll_free_all(&wp->w_llist);
1147 ll_free_all(&wp->w_llist_ref);
1148 }
1149 else
1150 /* quickfix list */
1151 for (i = 0; i < qi->qf_listcount; ++i)
1152 qf_free(qi, i);
1153}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155/*
1156 * Add an entry to the end of the list of errors.
1157 * Returns OK or FAIL.
1158 */
1159 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001160qf_add_entry(
1161 qf_info_T *qi, /* quickfix list */
1162 qfline_T **prevp, /* pointer to previously added entry or NULL */
1163 char_u *dir, /* optional directory name */
1164 char_u *fname, /* file name or NULL */
1165 int bufnum, /* buffer number or zero */
1166 char_u *mesg, /* message */
1167 long lnum, /* line number */
1168 int col, /* column */
1169 int vis_col, /* using visual column */
1170 char_u *pattern, /* search pattern */
1171 int nr, /* error number */
1172 int type, /* type character */
1173 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001175 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001177 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001179 if (bufnum != 0)
1180 qfp->qf_fnum = bufnum;
1181 else
1182 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1184 {
1185 vim_free(qfp);
1186 return FAIL;
1187 }
1188 qfp->qf_lnum = lnum;
1189 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001190 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001191 if (pattern == NULL || *pattern == NUL)
1192 qfp->qf_pattern = NULL;
1193 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1194 {
1195 vim_free(qfp->qf_text);
1196 vim_free(qfp);
1197 return FAIL;
1198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 qfp->qf_nr = nr;
1200 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1201 type = 0;
1202 qfp->qf_type = type;
1203 qfp->qf_valid = valid;
1204
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001205 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1206 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001208 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001209 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1210 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 qfp->qf_prev = qfp; /* first element points to itself */
1212 }
1213 else
1214 {
1215 qfp->qf_prev = *prevp;
1216 (*prevp)->qf_next = qfp;
1217 }
1218 qfp->qf_next = qfp; /* last element points to itself */
1219 qfp->qf_cleared = FALSE;
1220 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001221 ++qi->qf_lists[qi->qf_curlist].qf_count;
1222 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1223 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001225 qi->qf_lists[qi->qf_curlist].qf_index =
1226 qi->qf_lists[qi->qf_curlist].qf_count;
1227 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229
1230 return OK;
1231}
1232
1233/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001234 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001235 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001236 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001237ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001238{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001239 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001240
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001241 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1242 if (qi != NULL)
1243 {
1244 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1245 qi->qf_refcount++;
1246 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001247
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001248 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001249}
1250
1251/*
1252 * Return the location list for window 'wp'.
1253 * If not present, allocate a location list
1254 */
1255 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001256ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001257{
1258 if (IS_LL_WINDOW(wp))
1259 /* For a location list window, use the referenced location list */
1260 return wp->w_llist_ref;
1261
1262 /*
1263 * For a non-location list window, w_llist_ref should not point to a
1264 * location list.
1265 */
1266 ll_free_all(&wp->w_llist_ref);
1267
1268 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001269 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001270 return wp->w_llist;
1271}
1272
1273/*
1274 * Copy the location list from window "from" to window "to".
1275 */
1276 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001277copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001278{
1279 qf_info_T *qi;
1280 int idx;
1281 int i;
1282
1283 /*
1284 * When copying from a location list window, copy the referenced
1285 * location list. For other windows, copy the location list for
1286 * that window.
1287 */
1288 if (IS_LL_WINDOW(from))
1289 qi = from->w_llist_ref;
1290 else
1291 qi = from->w_llist;
1292
1293 if (qi == NULL) /* no location list to copy */
1294 return;
1295
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001296 /* allocate a new location list */
1297 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001298 return;
1299
1300 to->w_llist->qf_listcount = qi->qf_listcount;
1301
1302 /* Copy the location lists one at a time */
1303 for (idx = 0; idx < qi->qf_listcount; idx++)
1304 {
1305 qf_list_T *from_qfl;
1306 qf_list_T *to_qfl;
1307
1308 to->w_llist->qf_curlist = idx;
1309
1310 from_qfl = &qi->qf_lists[idx];
1311 to_qfl = &to->w_llist->qf_lists[idx];
1312
1313 /* Some of the fields are populated by qf_add_entry() */
1314 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1315 to_qfl->qf_count = 0;
1316 to_qfl->qf_index = 0;
1317 to_qfl->qf_start = NULL;
1318 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001319 if (from_qfl->qf_title != NULL)
1320 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1321 else
1322 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001323
1324 if (from_qfl->qf_count)
1325 {
1326 qfline_T *from_qfp;
1327 qfline_T *prevp = NULL;
1328
1329 /* copy all the location entries in this list */
1330 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1331 ++i, from_qfp = from_qfp->qf_next)
1332 {
1333 if (qf_add_entry(to->w_llist, &prevp,
1334 NULL,
1335 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001336 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001337 from_qfp->qf_text,
1338 from_qfp->qf_lnum,
1339 from_qfp->qf_col,
1340 from_qfp->qf_viscol,
1341 from_qfp->qf_pattern,
1342 from_qfp->qf_nr,
1343 0,
1344 from_qfp->qf_valid) == FAIL)
1345 {
1346 qf_free_all(to);
1347 return;
1348 }
1349 /*
1350 * qf_add_entry() will not set the qf_num field, as the
1351 * directory and file names are not supplied. So the qf_fnum
1352 * field is copied here.
1353 */
1354 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1355 prevp->qf_type = from_qfp->qf_type; /* error type */
1356 if (from_qfl->qf_ptr == from_qfp)
1357 to_qfl->qf_ptr = prevp; /* current location */
1358 }
1359 }
1360
1361 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1362
1363 /* When no valid entries are present in the list, qf_ptr points to
1364 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001365 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001366 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001367 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001368 to_qfl->qf_index = 1;
1369 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001370 }
1371
1372 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1373}
1374
1375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 * get buffer number for file "dir.name"
1377 */
1378 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001379qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380{
1381 if (fname == NULL || *fname == NUL) /* no file name */
1382 return 0;
1383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 char_u *ptr;
1385 int fnum;
1386
Bram Moolenaare60acc12011-05-10 16:41:25 +02001387#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001389#endif
1390#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (directory != NULL)
1392 slash_adjust(directory);
1393 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (directory != NULL && !vim_isAbsName(fname)
1396 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1397 {
1398 /*
1399 * Here we check if the file really exists.
1400 * This should normally be true, but if make works without
1401 * "leaving directory"-messages we might have missed a
1402 * directory change.
1403 */
1404 if (mch_getperm(ptr) < 0)
1405 {
1406 vim_free(ptr);
1407 directory = qf_guess_filepath(fname);
1408 if (directory)
1409 ptr = concat_fnames(directory, fname, TRUE);
1410 else
1411 ptr = vim_strsave(fname);
1412 }
1413 /* Use concatenated directory name and file name */
1414 fnum = buflist_add(ptr, 0);
1415 vim_free(ptr);
1416 return fnum;
1417 }
1418 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420}
1421
1422/*
1423 * push dirbuf onto the directory stack and return pointer to actual dir or
1424 * NULL on error
1425 */
1426 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001427qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428{
1429 struct dir_stack_T *ds_new;
1430 struct dir_stack_T *ds_ptr;
1431
1432 /* allocate new stack element and hook it in */
1433 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1434 if (ds_new == NULL)
1435 return NULL;
1436
1437 ds_new->next = *stackptr;
1438 *stackptr = ds_new;
1439
1440 /* store directory on the stack */
1441 if (vim_isAbsName(dirbuf)
1442 || (*stackptr)->next == NULL
1443 || (*stackptr && dir_stack != *stackptr))
1444 (*stackptr)->dirname = vim_strsave(dirbuf);
1445 else
1446 {
1447 /* Okay we don't have an absolute path.
1448 * dirbuf must be a subdir of one of the directories on the stack.
1449 * Let's search...
1450 */
1451 ds_new = (*stackptr)->next;
1452 (*stackptr)->dirname = NULL;
1453 while (ds_new)
1454 {
1455 vim_free((*stackptr)->dirname);
1456 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1457 TRUE);
1458 if (mch_isdir((*stackptr)->dirname) == TRUE)
1459 break;
1460
1461 ds_new = ds_new->next;
1462 }
1463
1464 /* clean up all dirs we already left */
1465 while ((*stackptr)->next != ds_new)
1466 {
1467 ds_ptr = (*stackptr)->next;
1468 (*stackptr)->next = (*stackptr)->next->next;
1469 vim_free(ds_ptr->dirname);
1470 vim_free(ds_ptr);
1471 }
1472
1473 /* Nothing found -> it must be on top level */
1474 if (ds_new == NULL)
1475 {
1476 vim_free((*stackptr)->dirname);
1477 (*stackptr)->dirname = vim_strsave(dirbuf);
1478 }
1479 }
1480
1481 if ((*stackptr)->dirname != NULL)
1482 return (*stackptr)->dirname;
1483 else
1484 {
1485 ds_ptr = *stackptr;
1486 *stackptr = (*stackptr)->next;
1487 vim_free(ds_ptr);
1488 return NULL;
1489 }
1490}
1491
1492
1493/*
1494 * pop dirbuf from the directory stack and return previous directory or NULL if
1495 * stack is empty
1496 */
1497 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001498qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 struct dir_stack_T *ds_ptr;
1501
1502 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1503 * What to do if it isn't? */
1504
1505 /* pop top element and free it */
1506 if (*stackptr != NULL)
1507 {
1508 ds_ptr = *stackptr;
1509 *stackptr = (*stackptr)->next;
1510 vim_free(ds_ptr->dirname);
1511 vim_free(ds_ptr);
1512 }
1513
1514 /* return NEW top element as current dir or NULL if stack is empty*/
1515 return *stackptr ? (*stackptr)->dirname : NULL;
1516}
1517
1518/*
1519 * clean up directory stack
1520 */
1521 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001522qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524 struct dir_stack_T *ds_ptr;
1525
1526 while ((ds_ptr = *stackptr) != NULL)
1527 {
1528 *stackptr = (*stackptr)->next;
1529 vim_free(ds_ptr->dirname);
1530 vim_free(ds_ptr);
1531 }
1532}
1533
1534/*
1535 * Check in which directory of the directory stack the given file can be
1536 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001537 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 * Cleans up intermediate directory entries.
1539 *
1540 * TODO: How to solve the following problem?
1541 * If we have the this directory tree:
1542 * ./
1543 * ./aa
1544 * ./aa/bb
1545 * ./bb
1546 * ./bb/x.c
1547 * and make says:
1548 * making all in aa
1549 * making all in bb
1550 * x.c:9: Error
1551 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1552 * qf_guess_filepath will return NULL.
1553 */
1554 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001555qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556{
1557 struct dir_stack_T *ds_ptr;
1558 struct dir_stack_T *ds_tmp;
1559 char_u *fullname;
1560
1561 /* no dirs on the stack - there's nothing we can do */
1562 if (dir_stack == NULL)
1563 return NULL;
1564
1565 ds_ptr = dir_stack->next;
1566 fullname = NULL;
1567 while (ds_ptr)
1568 {
1569 vim_free(fullname);
1570 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1571
1572 /* If concat_fnames failed, just go on. The worst thing that can happen
1573 * is that we delete the entire stack.
1574 */
1575 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1576 break;
1577
1578 ds_ptr = ds_ptr->next;
1579 }
1580
1581 vim_free(fullname);
1582
1583 /* clean up all dirs we already left */
1584 while (dir_stack->next != ds_ptr)
1585 {
1586 ds_tmp = dir_stack->next;
1587 dir_stack->next = dir_stack->next->next;
1588 vim_free(ds_tmp->dirname);
1589 vim_free(ds_tmp);
1590 }
1591
1592 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1593
1594}
1595
1596/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001597 * When loading a file from the quickfix, the auto commands may modify it.
1598 * This may invalidate the current quickfix entry. This function checks
1599 * whether a entry is still present in the quickfix.
1600 * Similar to location list.
1601 */
1602 static int
1603is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1604{
1605 qf_list_T *qfl;
1606 qfline_T *qfp;
1607 int i;
1608
1609 qfl = &qi->qf_lists[qi->qf_curlist];
1610
1611 /* Search for the entry in the current list */
1612 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1613 ++i, qfp = qfp->qf_next)
1614 if (qfp == qf_ptr)
1615 break;
1616
1617 if (i == qfl->qf_count) /* Entry is not found */
1618 return FALSE;
1619
1620 return TRUE;
1621}
1622
1623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 * jump to a quickfix line
1625 * if dir == FORWARD go "errornr" valid entries forward
1626 * if dir == BACKWARD go "errornr" valid entries backward
1627 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1628 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1629 * else if "errornr" is zero, redisplay the same line
1630 * else go to entry "errornr"
1631 */
1632 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001633qf_jump(
1634 qf_info_T *qi,
1635 int dir,
1636 int errornr,
1637 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001639 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001640 qfline_T *qf_ptr;
1641 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 int qf_index;
1643 int old_qf_fnum;
1644 int old_qf_index;
1645 int prev_index;
1646 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1647 char_u *err = e_no_more_items;
1648 linenr_T i;
1649 buf_T *old_curbuf;
1650 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 colnr_T screen_col;
1652 colnr_T char_col;
1653 char_u *line;
1654#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001655 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001656 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 int opened_window = FALSE;
1658 win_T *win;
1659 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001660 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001662 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 int print_message = TRUE;
1664 int len;
1665#ifdef FEAT_FOLDING
1666 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1667#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001668 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001669 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001671 if (qi == NULL)
1672 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001673
1674 if (qi->qf_curlist >= qi->qf_listcount
1675 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {
1677 EMSG(_(e_quickfix));
1678 return;
1679 }
1680
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001681 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001683 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 old_qf_index = qf_index;
1685 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1686 {
1687 while (errornr--)
1688 {
1689 old_qf_ptr = qf_ptr;
1690 prev_index = qf_index;
1691 old_qf_fnum = qf_ptr->qf_fnum;
1692 do
1693 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001694 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 || qf_ptr->qf_next == NULL)
1696 {
1697 qf_ptr = old_qf_ptr;
1698 qf_index = prev_index;
1699 if (err != NULL)
1700 {
1701 EMSG(_(err));
1702 goto theend;
1703 }
1704 errornr = 0;
1705 break;
1706 }
1707 ++qf_index;
1708 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001709 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1710 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1712 err = NULL;
1713 }
1714 }
1715 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1716 {
1717 while (errornr--)
1718 {
1719 old_qf_ptr = qf_ptr;
1720 prev_index = qf_index;
1721 old_qf_fnum = qf_ptr->qf_fnum;
1722 do
1723 {
1724 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1725 {
1726 qf_ptr = old_qf_ptr;
1727 qf_index = prev_index;
1728 if (err != NULL)
1729 {
1730 EMSG(_(err));
1731 goto theend;
1732 }
1733 errornr = 0;
1734 break;
1735 }
1736 --qf_index;
1737 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001738 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1739 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1741 err = NULL;
1742 }
1743 }
1744 else if (errornr != 0) /* go to specified number */
1745 {
1746 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1747 {
1748 --qf_index;
1749 qf_ptr = qf_ptr->qf_prev;
1750 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001751 while (errornr > qf_index && qf_index <
1752 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 && qf_ptr->qf_next != NULL)
1754 {
1755 ++qf_index;
1756 qf_ptr = qf_ptr->qf_next;
1757 }
1758 }
1759
1760#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001761 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1762 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 /* No need to print the error message if it's visible in the error
1764 * window */
1765 print_message = FALSE;
1766
1767 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001768 * For ":helpgrep" find a help window or open one.
1769 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001770 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001771 {
1772 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001773
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001774 if (cmdmod.tab != 0)
1775 wp = NULL;
1776 else
1777 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1778 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1779 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001780 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1781 win_enter(wp, TRUE);
1782 else
1783 {
1784 /*
1785 * Split off help window; put it at far top if no position
1786 * specified, the current window is vertically split and narrow.
1787 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001788 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001789 if (cmdmod.split == 0 && curwin->w_width != Columns
1790 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001791 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001792 if (qi != &ql_info)
1793 flags |= WSP_NEWLOC; /* don't copy the location list */
1794
1795 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001796 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001797 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001798
1799 if (curwin->w_height < p_hh)
1800 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001801
1802 if (qi != &ql_info) /* not a quickfix list */
1803 {
1804 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001805 curwin->w_llist = qi;
1806 qi->qf_refcount++;
1807 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001808 }
1809
1810 if (!p_im)
1811 restart_edit = 0; /* don't want insert mode in help file */
1812 }
1813
1814 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 * If currently in the quickfix window, find another window to show the
1816 * file in.
1817 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001818 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001820 win_T *usable_win_ptr = NULL;
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 /*
1823 * If there is no file specified, we don't know where to go.
1824 * But do advance, otherwise ":cn" gets stuck.
1825 */
1826 if (qf_ptr->qf_fnum == 0)
1827 goto theend;
1828
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001829 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001830
1831 ll_ref = curwin->w_llist_ref;
1832 if (ll_ref != NULL)
1833 {
1834 /* Find a window using the same location list that is not a
1835 * quickfix window. */
1836 FOR_ALL_WINDOWS(usable_win_ptr)
1837 if (usable_win_ptr->w_llist == ll_ref
1838 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001839 {
1840 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001841 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001842 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001843 }
1844
1845 if (!usable_win)
1846 {
1847 /* Locate a window showing a normal buffer */
1848 FOR_ALL_WINDOWS(win)
1849 if (win->w_buffer->b_p_bt[0] == NUL)
1850 {
1851 usable_win = 1;
1852 break;
1853 }
1854 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001857 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001858 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001860 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001861 {
1862 tabpage_T *tp;
1863 win_T *wp;
1864
1865 FOR_ALL_TAB_WINDOWS(tp, wp)
1866 {
1867 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1868 {
1869 goto_tabpage_win(tp, wp);
1870 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001871 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001872 }
1873 }
1874 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001875win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001876
1877 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001878 * If there is only one window and it is the quickfix window, create a
1879 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001880 */
1881 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001883 flags = WSP_ABOVE;
1884 if (ll_ref != NULL)
1885 flags |= WSP_NEWLOC;
1886 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 goto failed; /* not enough room for window */
1888 opened_window = TRUE; /* close it when fail */
1889 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001890 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001891 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001892 if (ll_ref != NULL)
1893 {
1894 /* The new window should use the location list from the
1895 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001896 curwin->w_llist = ll_ref;
1897 ll_ref->qf_refcount++;
1898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 }
1900 else
1901 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001902 if (curwin->w_llist_ref != NULL)
1903 {
1904 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001905 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001906 if (win == NULL)
1907 {
1908 /* Find the window showing the selected file */
1909 FOR_ALL_WINDOWS(win)
1910 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1911 break;
1912 if (win == NULL)
1913 {
1914 /* Find a previous usable window */
1915 win = curwin;
1916 do
1917 {
1918 if (win->w_buffer->b_p_bt[0] == NUL)
1919 break;
1920 if (win->w_prev == NULL)
1921 win = lastwin; /* wrap around the top */
1922 else
1923 win = win->w_prev; /* go to previous window */
1924 } while (win != curwin);
1925 }
1926 }
1927 win_goto(win);
1928
1929 /* If the location list for the window is not set, then set it
1930 * to the location list from the location window */
1931 if (win->w_llist == NULL)
1932 {
1933 win->w_llist = ll_ref;
1934 ll_ref->qf_refcount++;
1935 }
1936 }
1937 else
1938 {
1939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 /*
1941 * Try to find a window that shows the right buffer.
1942 * Default to the window just above the quickfix buffer.
1943 */
1944 win = curwin;
1945 altwin = NULL;
1946 for (;;)
1947 {
1948 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1949 break;
1950 if (win->w_prev == NULL)
1951 win = lastwin; /* wrap around the top */
1952 else
1953 win = win->w_prev; /* go to previous window */
1954
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001955 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 /* Didn't find it, go to the window before the quickfix
1958 * window. */
1959 if (altwin != NULL)
1960 win = altwin;
1961 else if (curwin->w_prev != NULL)
1962 win = curwin->w_prev;
1963 else
1964 win = curwin->w_next;
1965 break;
1966 }
1967
1968 /* Remember a usable window. */
1969 if (altwin == NULL && !win->w_p_pvw
1970 && win->w_buffer->b_p_bt[0] == NUL)
1971 altwin = win;
1972 }
1973
1974 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977 }
1978#endif
1979
1980 /*
1981 * If there is a file name,
1982 * read the wanted file if needed, and check autowrite etc.
1983 */
1984 old_curbuf = curbuf;
1985 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001986
1987 if (qf_ptr->qf_fnum != 0)
1988 {
1989 if (qf_ptr->qf_type == 1)
1990 {
1991 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1992 * set b_p_ro flag). */
1993 if (!can_abandon(curbuf, forceit))
1994 {
1995 EMSG(_(e_nowrtmsg));
1996 ok = FALSE;
1997 }
1998 else
1999 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002000 ECMD_HIDE + ECMD_SET_HELP,
2001 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002002 }
2003 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002004 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002005 int old_qf_curlist = qi->qf_curlist;
2006 int is_abort = FALSE;
2007
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002008 ok = buflist_getfile(qf_ptr->qf_fnum,
2009 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002010 if (qi != &ql_info && !win_valid(oldwin))
2011 {
2012 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002013 is_abort = TRUE;
2014 opened_window = FALSE;
2015 }
2016 else if (old_qf_curlist != qi->qf_curlist
2017 || !is_qf_entry_present(qi, qf_ptr))
2018 {
2019 if (qi == &ql_info)
2020 EMSG(_("E925: Current quickfix was changed"));
2021 else
2022 EMSG(_("E926: Current location list was changed"));
2023 is_abort = TRUE;
2024 }
2025
2026 if (is_abort)
2027 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002028 ok = FALSE;
2029 qi = NULL;
2030 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002031 }
2032 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002033 }
2034
2035 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
2037 /* When not switched to another buffer, still need to set pc mark */
2038 if (curbuf == old_curbuf)
2039 setpcmark();
2040
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002041 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002043 /*
2044 * Go to line with error, unless qf_lnum is 0.
2045 */
2046 i = qf_ptr->qf_lnum;
2047 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002049 if (i > curbuf->b_ml.ml_line_count)
2050 i = curbuf->b_ml.ml_line_count;
2051 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002053 if (qf_ptr->qf_col > 0)
2054 {
2055 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002056#ifdef FEAT_VIRTUALEDIT
2057 curwin->w_cursor.coladd = 0;
2058#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002059 if (qf_ptr->qf_viscol == TRUE)
2060 {
2061 /*
2062 * Check each character from the beginning of the error
2063 * line up to the error column. For each tab character
2064 * found, reduce the error column value by the length of
2065 * a tab character.
2066 */
2067 line = ml_get_curline();
2068 screen_col = 0;
2069 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2070 {
2071 if (*line == NUL)
2072 break;
2073 if (*line++ == '\t')
2074 {
2075 curwin->w_cursor.col -= 7 - (screen_col % 8);
2076 screen_col += 8 - (screen_col % 8);
2077 }
2078 else
2079 ++screen_col;
2080 }
2081 }
2082 check_cursor();
2083 }
2084 else
2085 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
2087 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002088 {
2089 pos_T save_cursor;
2090
2091 /* Move the cursor to the first line in the buffer */
2092 save_cursor = curwin->w_cursor;
2093 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002094 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2095 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002096 curwin->w_cursor = save_cursor;
2097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099#ifdef FEAT_FOLDING
2100 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2101 foldOpenCursor();
2102#endif
2103 if (print_message)
2104 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002105 /* Update the screen before showing the message, unless the screen
2106 * scrolled up. */
2107 if (!msg_scrolled)
2108 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002110 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2112 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2113 /* Add the message, skipping leading whitespace and newlines. */
2114 len = (int)STRLEN(IObuff);
2115 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2116
2117 /* Output the message. Overwrite to avoid scrolling when the 'O'
2118 * flag is present in 'shortmess'; But when not jumping, print the
2119 * whole message. */
2120 i = msg_scroll;
2121 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2122 msg_scroll = TRUE;
2123 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2124 msg_scroll = FALSE;
2125 msg_attr_keep(IObuff, 0, TRUE);
2126 msg_scroll = i;
2127 }
2128 }
2129 else
2130 {
2131#ifdef FEAT_WINDOWS
2132 if (opened_window)
2133 win_close(curwin, TRUE); /* Close opened window */
2134#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002135 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
2137 /*
2138 * Couldn't open file, so put index back where it was. This could
2139 * happen if the file was readonly and we changed something.
2140 */
2141#ifdef FEAT_WINDOWS
2142failed:
2143#endif
2144 qf_ptr = old_qf_ptr;
2145 qf_index = old_qf_index;
2146 }
2147 }
2148theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002149 if (qi != NULL)
2150 {
2151 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2152 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154#ifdef FEAT_WINDOWS
2155 if (p_swb != old_swb && opened_window)
2156 {
2157 /* Restore old 'switchbuf' value, but not when an autocommand or
2158 * modeline has changed the value. */
2159 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002162 swb_flags = old_swb_flags;
2163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 else
2165 free_string_option(old_swb);
2166 }
2167#endif
2168}
2169
2170/*
2171 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002172 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 */
2174 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002175qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002177 buf_T *buf;
2178 char_u *fname;
2179 qfline_T *qfp;
2180 int i;
2181 int idx1 = 1;
2182 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002183 char_u *arg = eap->arg;
2184 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002186 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002188 if (eap->cmdidx == CMD_llist)
2189 {
2190 qi = GET_LOC_LIST(curwin);
2191 if (qi == NULL)
2192 {
2193 EMSG(_(e_loclist));
2194 return;
2195 }
2196 }
2197
2198 if (qi->qf_curlist >= qi->qf_listcount
2199 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
2201 EMSG(_(e_quickfix));
2202 return;
2203 }
2204 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2205 {
2206 EMSG(_(e_trailing));
2207 return;
2208 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002209 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 if (idx1 < 0)
2211 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2212 if (idx2 < 0)
2213 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2214
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002215 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002217 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2218 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2221 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002222 msg_putchar('\n');
2223 if (got_int)
2224 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002225
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002226 fname = NULL;
2227 if (qfp->qf_fnum != 0
2228 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2229 {
2230 fname = buf->b_fname;
2231 if (qfp->qf_type == 1) /* :helpgrep */
2232 fname = gettail(fname);
2233 }
2234 if (fname == NULL)
2235 sprintf((char *)IObuff, "%2d", i);
2236 else
2237 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2238 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002239 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002240 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2241 if (qfp->qf_lnum == 0)
2242 IObuff[0] = NUL;
2243 else if (qfp->qf_col == 0)
2244 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2245 else
2246 sprintf((char *)IObuff, ":%ld col %d",
2247 qfp->qf_lnum, qfp->qf_col);
2248 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2249 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2250 msg_puts_attr(IObuff, hl_attr(HLF_N));
2251 if (qfp->qf_pattern != NULL)
2252 {
2253 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2254 STRCAT(IObuff, ":");
2255 msg_puts(IObuff);
2256 }
2257 msg_puts((char_u *)" ");
2258
2259 /* Remove newlines and leading whitespace from the text. For an
2260 * unrecognized line keep the indent, the compiler may mark a word
2261 * with ^^^^. */
2262 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2264 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002265 msg_prt_line(IObuff, FALSE);
2266 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002268
2269 qfp = qfp->qf_next;
2270 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 ui_breakcheck();
2272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273}
2274
2275/*
2276 * Remove newlines and leading whitespace from an error message.
2277 * Put the result in "buf[bufsize]".
2278 */
2279 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002280qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281{
2282 int i;
2283 char_u *p = text;
2284
2285 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2286 {
2287 if (*p == '\n')
2288 {
2289 buf[i] = ' ';
2290 while (*++p != NUL)
2291 if (!vim_iswhite(*p) && *p != '\n')
2292 break;
2293 }
2294 else
2295 buf[i] = *p++;
2296 }
2297 buf[i] = NUL;
2298}
2299
2300/*
2301 * ":colder [count]": Up in the quickfix stack.
2302 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002303 * ":lolder [count]": Up in the location list stack.
2304 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 */
2306 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002307qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002309 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 int count;
2311
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002312 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2313 {
2314 qi = GET_LOC_LIST(curwin);
2315 if (qi == NULL)
2316 {
2317 EMSG(_(e_loclist));
2318 return;
2319 }
2320 }
2321
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (eap->addr_count != 0)
2323 count = eap->line2;
2324 else
2325 count = 1;
2326 while (count--)
2327 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002328 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002330 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
2332 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002333 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002335 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337 else
2338 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002339 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
2341 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002342 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002344 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002347 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348}
2349
2350 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002351qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352{
2353 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002354 qi->qf_curlist + 1, qi->qf_listcount,
2355 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002357 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358#endif
2359}
2360
2361/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002362 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 */
2364 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002365qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002367 qfline_T *qfp;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002368 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002370 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002372 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002373 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002374 {
2375 vim_free(qi->qf_lists[idx].qf_start->qf_text);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002376 stop = (qi->qf_lists[idx].qf_start == qfp);
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002377 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2378 vim_free(qi->qf_lists[idx].qf_start);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002379 if (stop)
2380 /* Somehow qf_count may have an incorrect value, set it to 1
2381 * to avoid crashing when it's wrong.
2382 * TODO: Avoid qf_count being incorrect. */
2383 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002384 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002385 qi->qf_lists[idx].qf_start = qfp;
2386 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002388 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002389 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002390 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391}
2392
2393/*
2394 * qf_mark_adjust: adjust marks
2395 */
2396 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002397qf_mark_adjust(
2398 win_T *wp,
2399 linenr_T line1,
2400 linenr_T line2,
2401 long amount,
2402 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002404 int i;
2405 qfline_T *qfp;
2406 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002407 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002409 if (wp != NULL)
2410 {
2411 if (wp->w_llist == NULL)
2412 return;
2413 qi = wp->w_llist;
2414 }
2415
2416 for (idx = 0; idx < qi->qf_listcount; ++idx)
2417 if (qi->qf_lists[idx].qf_count)
2418 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2419 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 if (qfp->qf_fnum == curbuf->b_fnum)
2421 {
2422 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2423 {
2424 if (amount == MAXLNUM)
2425 qfp->qf_cleared = TRUE;
2426 else
2427 qfp->qf_lnum += amount;
2428 }
2429 else if (amount_after && qfp->qf_lnum > line2)
2430 qfp->qf_lnum += amount_after;
2431 }
2432}
2433
2434/*
2435 * Make a nice message out of the error character and the error number:
2436 * char number message
2437 * e or E 0 " error"
2438 * w or W 0 " warning"
2439 * i or I 0 " info"
2440 * 0 0 ""
2441 * other 0 " c"
2442 * e or E n " error n"
2443 * w or W n " warning n"
2444 * i or I n " info n"
2445 * 0 n " error n"
2446 * other n " c n"
2447 * 1 x "" :helpgrep
2448 */
2449 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002450qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 static char_u buf[20];
2453 static char_u cc[3];
2454 char_u *p;
2455
2456 if (c == 'W' || c == 'w')
2457 p = (char_u *)" warning";
2458 else if (c == 'I' || c == 'i')
2459 p = (char_u *)" info";
2460 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2461 p = (char_u *)" error";
2462 else if (c == 0 || c == 1)
2463 p = (char_u *)"";
2464 else
2465 {
2466 cc[0] = ' ';
2467 cc[1] = c;
2468 cc[2] = NUL;
2469 p = cc;
2470 }
2471
2472 if (nr <= 0)
2473 return p;
2474
2475 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2476 return buf;
2477}
2478
2479#if defined(FEAT_WINDOWS) || defined(PROTO)
2480/*
2481 * ":cwindow": open the quickfix window if we have errors to display,
2482 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002483 * ":lwindow": open the location list window if we have locations to display,
2484 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 */
2486 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002487ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002489 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 win_T *win;
2491
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002492 if (eap->cmdidx == CMD_lwindow)
2493 {
2494 qi = GET_LOC_LIST(curwin);
2495 if (qi == NULL)
2496 return;
2497 }
2498
2499 /* Look for an existing quickfix window. */
2500 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 /*
2503 * If a quickfix window is open but we have no errors to display,
2504 * close the window. If a quickfix window is not open, then open
2505 * it if we have errors; otherwise, leave it closed.
2506 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002507 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002508 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002509 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
2511 if (win != NULL)
2512 ex_cclose(eap);
2513 }
2514 else if (win == NULL)
2515 ex_copen(eap);
2516}
2517
2518/*
2519 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002520 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002523ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002525 win_T *win = NULL;
2526 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002528 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2529 {
2530 qi = GET_LOC_LIST(curwin);
2531 if (qi == NULL)
2532 return;
2533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002535 /* Find existing quickfix window and close it. */
2536 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 if (win != NULL)
2538 win_close(win, FALSE);
2539}
2540
2541/*
2542 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002543 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 */
2545 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002546ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002548 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002551 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002552 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002553 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002555 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2556 {
2557 qi = GET_LOC_LIST(curwin);
2558 if (qi == NULL)
2559 {
2560 EMSG(_(e_loclist));
2561 return;
2562 }
2563 }
2564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 if (eap->addr_count != 0)
2566 height = eap->line2;
2567 else
2568 height = QF_WINHEIGHT;
2569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#ifdef FEAT_GUI
2572 need_mouse_correct = TRUE;
2573#endif
2574
2575 /*
2576 * Find existing quickfix window, or open a new one.
2577 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002578 win = qf_find_win(qi);
2579
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002580 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002583 if (eap->addr_count != 0)
2584 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002585 if (cmdmod.split & WSP_VERT)
2586 {
2587 if (height != W_WIDTH(win))
2588 win_setwidth(height);
2589 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002590 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002591 win_setheight(height);
2592 }
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 else
2595 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002596 qf_buf = qf_find_buf(qi);
2597
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 /* The current window becomes the previous window afterwards. */
2599 win = curwin;
2600
Bram Moolenaar77642c02012-11-20 17:55:10 +01002601 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2602 && cmdmod.split == 0)
2603 /* Create the new window at the very bottom, except when
2604 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002605 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002606 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002608 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002610 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002612 /*
2613 * For the location list window, create a reference to the
2614 * location list from the window 'win'.
2615 */
2616 curwin->w_llist_ref = win->w_llist;
2617 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002619
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002620 if (oldwin != curwin)
2621 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002622 if (qf_buf != NULL)
2623 /* Use the existing quickfix buffer */
2624 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002625 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002626 else
2627 {
2628 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002629 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002630 /* switch off 'swapfile' */
2631 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2632 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002633 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002634 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002635 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002636#ifdef FEAT_DIFF
2637 curwin->w_p_diff = FALSE;
2638#endif
2639#ifdef FEAT_FOLDING
2640 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2641 OPT_LOCAL);
2642#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002645 /* Only set the height when still in the same tab page and there is no
2646 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002647 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 win_setheight(height);
2649 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2650 if (win_valid(win))
2651 prevwin = win;
2652 }
2653
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002654 qf_set_title_var(qi);
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 /*
2657 * Fill the buffer with the quickfix list.
2658 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002659 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002661 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 curwin->w_cursor.col = 0;
2663 check_cursor();
2664 update_topline(); /* scroll to show the line */
2665}
2666
2667/*
2668 * Return the number of the current entry (line number in the quickfix
2669 * window).
2670 */
2671 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002672qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002674 qf_info_T *qi = &ql_info;
2675
2676 if (IS_LL_WINDOW(wp))
2677 /* In the location list window, use the referenced location list */
2678 qi = wp->w_llist_ref;
2679
2680 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681}
2682
2683/*
2684 * Update the cursor position in the quickfix window to the current error.
2685 * Return TRUE if there is a quickfix window.
2686 */
2687 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002688qf_win_pos_update(
2689 qf_info_T *qi,
2690 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002693 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
2695 /*
2696 * Put the cursor on the current error in the quickfix window, so that
2697 * it's viewable.
2698 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002699 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (win != NULL
2701 && qf_index <= win->w_buffer->b_ml.ml_line_count
2702 && old_qf_index != qf_index)
2703 {
2704 win_T *old_curwin = curwin;
2705
2706 curwin = win;
2707 curbuf = win->w_buffer;
2708 if (qf_index > old_qf_index)
2709 {
2710 curwin->w_redraw_top = old_qf_index;
2711 curwin->w_redraw_bot = qf_index;
2712 }
2713 else
2714 {
2715 curwin->w_redraw_top = qf_index;
2716 curwin->w_redraw_bot = old_qf_index;
2717 }
2718 curwin->w_cursor.lnum = qf_index;
2719 curwin->w_cursor.col = 0;
2720 update_topline(); /* scroll to show the line */
2721 redraw_later(VALID);
2722 curwin->w_redr_status = TRUE; /* update ruler */
2723 curwin = old_curwin;
2724 curbuf = curwin->w_buffer;
2725 }
2726 return win != NULL;
2727}
2728
2729/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002730 * Check whether the given window is displaying the specified quickfix/location
2731 * list buffer
2732 */
2733 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002734is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002735{
2736 /*
2737 * A window displaying the quickfix buffer will have the w_llist_ref field
2738 * set to NULL.
2739 * A window displaying a location list buffer will have the w_llist_ref
2740 * pointing to the location list.
2741 */
2742 if (bt_quickfix(win->w_buffer))
2743 if ((qi == &ql_info && win->w_llist_ref == NULL)
2744 || (qi != &ql_info && win->w_llist_ref == qi))
2745 return TRUE;
2746
2747 return FALSE;
2748}
2749
2750/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002751 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002752 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002753 */
2754 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002755qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002756{
2757 win_T *win;
2758
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002759 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002760 if (is_qf_win(win, qi))
2761 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002762
2763 return win;
2764}
2765
2766/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002767 * Find a quickfix buffer.
2768 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 */
2770 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002771qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002773 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002774 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaar9c102382006-05-03 21:26:49 +00002776 FOR_ALL_TAB_WINDOWS(tp, win)
2777 if (is_qf_win(win, qi))
2778 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002779
Bram Moolenaar9c102382006-05-03 21:26:49 +00002780 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781}
2782
2783/*
2784 * Find the quickfix buffer. If it exists, update the contents.
2785 */
2786 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002787qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002790 win_T *win;
2791 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002795 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (buf != NULL)
2797 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02002798 linenr_T old_line_count = buf->b_ml.ml_line_count;
2799
2800 if (old_last == NULL)
2801 /* set curwin/curbuf to buf and save a few things */
2802 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002804 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002805 {
2806 curwin_save = curwin;
2807 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002808 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002809 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002810 }
2811
Bram Moolenaar864293a2016-06-02 13:40:04 +02002812 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01002813
Bram Moolenaar864293a2016-06-02 13:40:04 +02002814 if (old_last == NULL)
2815 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002816 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002817
2818 /* restore curwin/curbuf and a few other things */
2819 aucmd_restbuf(&aco);
2820 }
2821
2822 /* Only redraw when added lines are visible. This avoids flickering
2823 * when the added lines are not visible. */
2824 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
2825 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827}
2828
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002829/*
2830 * Set "w:quickfix_title" if "qi" has a title.
2831 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002832 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002833qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002834{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002835 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2836 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002837 qi->qf_lists[qi->qf_curlist].qf_title);
2838}
2839
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840/*
2841 * Fill current buffer with quickfix errors, replacing any previous contents.
2842 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02002843 * If "old_last" is not NULL append the items after this one.
2844 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
2845 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 */
2847 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002848qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002850 linenr_T lnum;
2851 qfline_T *qfp;
2852 buf_T *errbuf;
2853 int len;
2854 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaar864293a2016-06-02 13:40:04 +02002856 if (old_last == NULL)
2857 {
2858 if (buf != curbuf)
2859 {
2860 EMSG2(_(e_intern2), "qf_fill_buffer()");
2861 return;
2862 }
2863
2864 /* delete all existing lines */
2865 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2866 (void)ml_delete((linenr_T)1, FALSE);
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002870 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 {
2872 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002873 if (old_last == NULL)
2874 {
2875 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2876 lnum = 0;
2877 }
2878 else
2879 {
2880 qfp = old_last->qf_next;
2881 lnum = buf->b_ml.ml_line_count;
2882 }
2883 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 if (qfp->qf_fnum != 0
2886 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2887 && errbuf->b_fname != NULL)
2888 {
2889 if (qfp->qf_type == 1) /* :helpgrep */
2890 STRCPY(IObuff, gettail(errbuf->b_fname));
2891 else
2892 STRCPY(IObuff, errbuf->b_fname);
2893 len = (int)STRLEN(IObuff);
2894 }
2895 else
2896 len = 0;
2897 IObuff[len++] = '|';
2898
2899 if (qfp->qf_lnum > 0)
2900 {
2901 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2902 len += (int)STRLEN(IObuff + len);
2903
2904 if (qfp->qf_col > 0)
2905 {
2906 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2907 len += (int)STRLEN(IObuff + len);
2908 }
2909
2910 sprintf((char *)IObuff + len, "%s",
2911 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2912 len += (int)STRLEN(IObuff + len);
2913 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002914 else if (qfp->qf_pattern != NULL)
2915 {
2916 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2917 len += (int)STRLEN(IObuff + len);
2918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 IObuff[len++] = '|';
2920 IObuff[len++] = ' ';
2921
2922 /* Remove newlines and leading whitespace from the text.
2923 * For an unrecognized line keep the indent, the compiler may
2924 * mark a word with ^^^^. */
2925 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2926 IObuff + len, IOSIZE - len);
2927
Bram Moolenaar864293a2016-06-02 13:40:04 +02002928 if (ml_append_buf(buf, lnum, IObuff,
2929 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02002931 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 qfp = qfp->qf_next;
2933 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02002934
2935 if (old_last == NULL)
2936 /* Delete the empty line which is now at the end */
2937 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
2939
2940 /* correct cursor position */
2941 check_lnums(TRUE);
2942
Bram Moolenaar864293a2016-06-02 13:40:04 +02002943 if (old_last == NULL)
2944 {
2945 /* Set the 'filetype' to "qf" each time after filling the buffer.
2946 * This resembles reading a file into a buffer, it's more logical when
2947 * using autocommands. */
2948 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2949 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
2951#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02002952 keep_filetype = TRUE; /* don't detect 'filetype' */
2953 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002955 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002957 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02002959 /* make sure it will be redrawn */
2960 redraw_curbuf_later(NOT_VALID);
2961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 /* Restore KeyTyped, setting 'filetype' may reset it. */
2964 KeyTyped = old_KeyTyped;
2965}
2966
2967#endif /* FEAT_WINDOWS */
2968
2969/*
2970 * Return TRUE if "buf" is the quickfix buffer.
2971 */
2972 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002973bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002975 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976}
2977
2978/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002979 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2980 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 */
2982 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002983bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002985 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2986 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987}
2988
2989/*
2990 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2991 */
2992 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002993bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002995 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996}
2997
2998 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002999bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 if (bt_dontwrite(buf))
3002 {
3003 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3004 return TRUE;
3005 }
3006 return FALSE;
3007}
3008
3009/*
3010 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3011 * and 'bufhidden'.
3012 */
3013 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003014buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
3016 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3017 switch (buf->b_p_bh[0])
3018 {
3019 case 'u': /* "unload" */
3020 case 'w': /* "wipe" */
3021 case 'd': return FALSE; /* "delete" */
3022 case 'h': return TRUE; /* "hide" */
3023 }
3024 return (p_hid || cmdmod.hide);
3025}
3026
3027/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003028 * Return TRUE when using ":vimgrep" for ":grep".
3029 */
3030 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003031grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003032{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003033 return ((cmdidx == CMD_grep
3034 || cmdidx == CMD_lgrep
3035 || cmdidx == CMD_grepadd
3036 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003037 && STRCMP("internal",
3038 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3039}
3040
3041/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003042 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 */
3044 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003045ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003047 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 char_u *cmd;
3049 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003050 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003051 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003052 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003053#ifdef FEAT_AUTOCMD
3054 char_u *au_name = NULL;
3055
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003056 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3057 if (grep_internal(eap->cmdidx))
3058 {
3059 ex_vimgrep(eap);
3060 return;
3061 }
3062
Bram Moolenaar7c626922005-02-07 22:01:03 +00003063 switch (eap->cmdidx)
3064 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003065 case CMD_make: au_name = (char_u *)"make"; break;
3066 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3067 case CMD_grep: au_name = (char_u *)"grep"; break;
3068 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3069 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3070 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003071 default: break;
3072 }
3073 if (au_name != NULL)
3074 {
3075 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3076 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003077# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003078 if (did_throw || force_abort)
3079 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003080# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003081 }
3082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
Bram Moolenaara6557602006-02-04 22:43:20 +00003084 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3085 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003086 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003087
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003089 fname = get_mef_name();
3090 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003092 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
3094 /*
3095 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3096 */
3097 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3098 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003099 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 cmd = alloc(len);
3101 if (cmd == NULL)
3102 return;
3103 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3104 (char *)p_shq);
3105 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003106 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 /*
3108 * Output a newline if there's something else than the :make command that
3109 * was typed (in which case the cursor is in column 0).
3110 */
3111 if (msg_col == 0)
3112 msg_didout = FALSE;
3113 msg_start();
3114 MSG_PUTS(":!");
3115 msg_outtrans(cmd); /* show what we are doing */
3116
3117 /* let the shell know if we are redirecting output or not */
3118 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3119
3120#ifdef AMIGA
3121 out_flush();
3122 /* read window status report and redraw before message */
3123 (void)char_avail();
3124#endif
3125
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003126 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003127 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3128 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003129 && eap->cmdidx != CMD_lgrepadd),
3130 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003131 if (wp != NULL)
3132 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003133#ifdef FEAT_AUTOCMD
3134 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003135 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003136 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3137 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003138 if (qi->qf_curlist < qi->qf_listcount)
3139 res = qi->qf_lists[qi->qf_curlist].qf_count;
3140 else
3141 res = 0;
3142 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003143#endif
3144 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003145 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
Bram Moolenaar7c626922005-02-07 22:01:03 +00003147 mch_remove(fname);
3148 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 vim_free(cmd);
3150}
3151
3152/*
3153 * Return the name for the errorfile, in allocated memory.
3154 * Find a new unique name when 'makeef' contains "##".
3155 * Returns NULL for error.
3156 */
3157 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003158get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
3160 char_u *p;
3161 char_u *name;
3162 static int start = -1;
3163 static int off = 0;
3164#ifdef HAVE_LSTAT
3165 struct stat sb;
3166#endif
3167
3168 if (*p_mef == NUL)
3169 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003170 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (name == NULL)
3172 EMSG(_(e_notmp));
3173 return name;
3174 }
3175
3176 for (p = p_mef; *p; ++p)
3177 if (p[0] == '#' && p[1] == '#')
3178 break;
3179
3180 if (*p == NUL)
3181 return vim_strsave(p_mef);
3182
3183 /* Keep trying until the name doesn't exist yet. */
3184 for (;;)
3185 {
3186 if (start == -1)
3187 start = mch_get_pid();
3188 else
3189 off += 19;
3190
3191 name = alloc((unsigned)STRLEN(p_mef) + 30);
3192 if (name == NULL)
3193 break;
3194 STRCPY(name, p_mef);
3195 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3196 STRCAT(name, p + 2);
3197 if (mch_getperm(name) < 0
3198#ifdef HAVE_LSTAT
3199 /* Don't accept a symbolic link, its a security risk. */
3200 && mch_lstat((char *)name, &sb) < 0
3201#endif
3202 )
3203 break;
3204 vim_free(name);
3205 }
3206 return name;
3207}
3208
3209/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003210 * Returns the number of valid entries in the current quickfix/location list.
3211 */
3212 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003213qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003214{
3215 qf_info_T *qi = &ql_info;
3216 qfline_T *qfp;
3217 int i, sz = 0;
3218 int prev_fnum = 0;
3219
3220 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3221 {
3222 /* Location list */
3223 qi = GET_LOC_LIST(curwin);
3224 if (qi == NULL)
3225 return 0;
3226 }
3227
3228 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3229 (i < qi->qf_lists[qi->qf_curlist].qf_count) && (qfp != NULL);
3230 ++i, qfp = qfp->qf_next)
3231 {
3232 if (qfp->qf_valid)
3233 {
3234 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3235 sz++; /* Count all valid entries */
3236 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3237 {
3238 /* Count the number of files */
3239 sz++;
3240 prev_fnum = qfp->qf_fnum;
3241 }
3242 }
3243 }
3244
3245 return sz;
3246}
3247
3248/*
3249 * Returns the current index of the quickfix/location list.
3250 * Returns 0 if there is an error.
3251 */
3252 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003253qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003254{
3255 qf_info_T *qi = &ql_info;
3256
3257 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3258 {
3259 /* Location list */
3260 qi = GET_LOC_LIST(curwin);
3261 if (qi == NULL)
3262 return 0;
3263 }
3264
3265 return qi->qf_lists[qi->qf_curlist].qf_index;
3266}
3267
3268/*
3269 * Returns the current index in the quickfix/location list (counting only valid
3270 * entries). If no valid entries are in the list, then returns 1.
3271 */
3272 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003273qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003274{
3275 qf_info_T *qi = &ql_info;
3276 qf_list_T *qfl;
3277 qfline_T *qfp;
3278 int i, eidx = 0;
3279 int prev_fnum = 0;
3280
3281 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3282 {
3283 /* Location list */
3284 qi = GET_LOC_LIST(curwin);
3285 if (qi == NULL)
3286 return 1;
3287 }
3288
3289 qfl = &qi->qf_lists[qi->qf_curlist];
3290 qfp = qfl->qf_start;
3291
3292 /* check if the list has valid errors */
3293 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3294 return 1;
3295
3296 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3297 {
3298 if (qfp->qf_valid)
3299 {
3300 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3301 {
3302 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3303 {
3304 /* Count the number of files */
3305 eidx++;
3306 prev_fnum = qfp->qf_fnum;
3307 }
3308 }
3309 else
3310 eidx++;
3311 }
3312 }
3313
3314 return eidx ? eidx : 1;
3315}
3316
3317/*
3318 * Get the 'n'th valid error entry in the quickfix or location list.
3319 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3320 * For :cdo and :ldo returns the 'n'th valid error entry.
3321 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3322 */
3323 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003324qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003325{
3326 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3327 qfline_T *qfp = qfl->qf_start;
3328 int i, eidx;
3329 int prev_fnum = 0;
3330
3331 /* check if the list has valid errors */
3332 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3333 return 1;
3334
3335 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp!= NULL;
3336 i++, qfp = qfp->qf_next)
3337 {
3338 if (qfp->qf_valid)
3339 {
3340 if (fdo)
3341 {
3342 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3343 {
3344 /* Count the number of files */
3345 eidx++;
3346 prev_fnum = qfp->qf_fnum;
3347 }
3348 }
3349 else
3350 eidx++;
3351 }
3352
3353 if (eidx == n)
3354 break;
3355 }
3356
3357 if (i <= qfl->qf_count)
3358 return i;
3359 else
3360 return 1;
3361}
3362
3363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003365 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003366 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 */
3368 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003369ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003371 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003372 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003373
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003374 if (eap->cmdidx == CMD_ll
3375 || eap->cmdidx == CMD_lrewind
3376 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003377 || eap->cmdidx == CMD_llast
3378 || eap->cmdidx == CMD_ldo
3379 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003380 {
3381 qi = GET_LOC_LIST(curwin);
3382 if (qi == NULL)
3383 {
3384 EMSG(_(e_loclist));
3385 return;
3386 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003387 }
3388
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003389 if (eap->addr_count > 0)
3390 errornr = (int)eap->line2;
3391 else
3392 {
3393 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3394 errornr = 0;
3395 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3396 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3397 errornr = 1;
3398 else
3399 errornr = 32767;
3400 }
3401
3402 /* For cdo and ldo commands, jump to the nth valid error.
3403 * For cfdo and lfdo commands, jump to the nth valid file entry.
3404 */
3405 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3406 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3407 errornr = qf_get_nth_valid_entry(qi,
3408 eap->addr_count > 0 ? (int)eap->line1 : 1,
3409 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3410
3411 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412}
3413
3414/*
3415 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003416 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003417 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 */
3419 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003420ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003422 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003423 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003424
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003425 if (eap->cmdidx == CMD_lnext
3426 || eap->cmdidx == CMD_lNext
3427 || eap->cmdidx == CMD_lprevious
3428 || eap->cmdidx == CMD_lnfile
3429 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003430 || eap->cmdidx == CMD_lpfile
3431 || eap->cmdidx == CMD_ldo
3432 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003433 {
3434 qi = GET_LOC_LIST(curwin);
3435 if (qi == NULL)
3436 {
3437 EMSG(_(e_loclist));
3438 return;
3439 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003440 }
3441
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003442 if (eap->addr_count > 0 &&
3443 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3444 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3445 errornr = (int)eap->line2;
3446 else
3447 errornr = 1;
3448
3449 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3450 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003452 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3453 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003455 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3456 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 ? BACKWARD_FILE
3458 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003459 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460}
3461
3462/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003463 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003464 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 */
3466 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003467ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003469 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003470 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003471#ifdef FEAT_AUTOCMD
3472 char_u *au_name = NULL;
3473#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003474
3475 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003476 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003477 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003478
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003479#ifdef FEAT_AUTOCMD
3480 switch (eap->cmdidx)
3481 {
3482 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3483 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3484 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3485 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3486 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3487 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3488 default: break;
3489 }
3490 if (au_name != NULL)
3491 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3492#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003493#ifdef FEAT_BROWSE
3494 if (cmdmod.browse)
3495 {
3496 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3497 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3498 if (browse_file == NULL)
3499 return;
3500 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3501 vim_free(browse_file);
3502 }
3503 else
3504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003506 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003507
3508 /*
3509 * This function is used by the :cfile, :cgetfile and :caddfile
3510 * commands.
3511 * :cfile always creates a new quickfix list and jumps to the
3512 * first error.
3513 * :cgetfile creates a new quickfix list but doesn't jump to the
3514 * first error.
3515 * :caddfile adds to an existing quickfix list. If there is no
3516 * quickfix list then a new list is created.
3517 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003518 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003519 && eap->cmdidx != CMD_laddfile),
3520 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003521 && (eap->cmdidx == CMD_cfile
3522 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003523 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003524#ifdef FEAT_AUTOCMD
3525 if (au_name != NULL)
3526 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3527#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003528 if (wp != NULL)
3529 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003530 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003531 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003532
3533 else
3534 {
3535#ifdef FEAT_AUTOCMD
3536 if (au_name != NULL)
3537 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3538#endif
3539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540}
3541
3542/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003543 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003544 * ":vimgrepadd {pattern} file(s)"
3545 * ":lvimgrep {pattern} file(s)"
3546 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003547 */
3548 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003549ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003550{
Bram Moolenaar81695252004-12-29 20:58:21 +00003551 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003552 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003553 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003554 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003555 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003556 char_u *s;
3557 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003558 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003559 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003560#ifdef FEAT_AUTOCMD
3561 qfline_T *cur_qf_start;
3562#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003563 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003564 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003565 buf_T *buf;
3566 int duplicate_name = FALSE;
3567 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003568 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003569 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003570 buf_T *first_match_buf = NULL;
3571 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003572 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003573#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3574 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003575#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003576 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003577 int flags = 0;
3578 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003579 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003580 char_u *dirname_start = NULL;
3581 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003582 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003583#ifdef FEAT_AUTOCMD
3584 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003585
3586 switch (eap->cmdidx)
3587 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003588 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3589 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3590 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003591 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003592 case CMD_grep: au_name = (char_u *)"grep"; break;
3593 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3594 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3595 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003596 default: break;
3597 }
3598 if (au_name != NULL)
3599 {
3600 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3601 curbuf->b_fname, TRUE, curbuf);
3602 if (did_throw || force_abort)
3603 return;
3604 }
3605#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003606
Bram Moolenaar754b5602006-02-09 23:53:20 +00003607 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003608 || eap->cmdidx == CMD_lvimgrep
3609 || eap->cmdidx == CMD_lgrepadd
3610 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003611 {
3612 qi = ll_get_or_alloc_list(curwin);
3613 if (qi == NULL)
3614 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003615 }
3616
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003617 if (eap->addr_count > 0)
3618 tomatch = eap->line2;
3619 else
3620 tomatch = MAXLNUM;
3621
Bram Moolenaar81695252004-12-29 20:58:21 +00003622 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003623 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003624 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003625 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003626 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003627 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003628 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003629 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003630 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003631
3632 if (s != NULL && *s == NUL)
3633 {
3634 /* Pattern is empty, use last search pattern. */
3635 if (last_search_pat() == NULL)
3636 {
3637 EMSG(_(e_noprevre));
3638 goto theend;
3639 }
3640 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3641 }
3642 else
3643 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3644
Bram Moolenaar86b68352004-12-27 21:59:20 +00003645 if (regmatch.regprog == NULL)
3646 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003647 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003648 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003649
3650 p = skipwhite(p);
3651 if (*p == NUL)
3652 {
3653 EMSG(_("E683: File name missing or invalid pattern"));
3654 goto theend;
3655 }
3656
Bram Moolenaar754b5602006-02-09 23:53:20 +00003657 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003658 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003659 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003660 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003661 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003662 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003663 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003664 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003665 prevp->qf_next != prevp; prevp = prevp->qf_next)
3666 ;
3667
3668 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003669 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003670 goto theend;
3671 if (fcount == 0)
3672 {
3673 EMSG(_(e_nomatch));
3674 goto theend;
3675 }
3676
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003677 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3678 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003679 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003680 {
3681 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003682 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003683 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003684
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003685 /* Remember the current directory, because a BufRead autocommand that does
3686 * ":lcd %:p:h" changes the meaning of short path names. */
3687 mch_dirname(dirname_start, MAXPATHL);
3688
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003689#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003690 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003691 * changing the current quickfix list. */
3692 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3693#endif
3694
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003695 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003696 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003697 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003698 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003699 if (time(NULL) > seconds)
3700 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003701 /* Display the file name every second or so, show the user we are
3702 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003703 seconds = time(NULL);
3704 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003705 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003706 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003707 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003708 else
3709 {
3710 msg_outtrans(p);
3711 vim_free(p);
3712 }
3713 msg_clr_eos();
3714 msg_didout = FALSE; /* overwrite this message */
3715 msg_nowait = TRUE; /* don't wait for this message */
3716 msg_col = 0;
3717 out_flush();
3718 }
3719
Bram Moolenaar81695252004-12-29 20:58:21 +00003720 buf = buflist_findname_exp(fnames[fi]);
3721 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3722 {
3723 /* Remember that a buffer with this name already exists. */
3724 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003725 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003726 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003727
3728#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3729 /* Don't do Filetype autocommands to avoid loading syntax and
3730 * indent scripts, a great speed improvement. */
3731 save_ei = au_event_disable(",Filetype");
3732#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003733 /* Don't use modelines here, it's useless. */
3734 save_mls = p_mls;
3735 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003736
3737 /* Load file into a buffer, so that 'fileencoding' is detected,
3738 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003739 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003740
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003741 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003742#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3743 au_event_restore(save_ei);
3744#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003745 }
3746 else
3747 /* Use existing, loaded buffer. */
3748 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003749
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003750#ifdef FEAT_AUTOCMD
3751 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3752 {
3753 int idx;
3754
3755 /* Autocommands changed the quickfix list. Find the one we were
3756 * using and restore it. */
3757 for (idx = 0; idx < LISTCOUNT; ++idx)
3758 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3759 {
3760 qi->qf_curlist = idx;
3761 break;
3762 }
3763 if (idx == LISTCOUNT)
3764 {
3765 /* List cannot be found, create a new one. */
3766 qf_new_list(qi, *eap->cmdlinep);
3767 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3768 }
3769 }
3770#endif
3771
Bram Moolenaar81695252004-12-29 20:58:21 +00003772 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003773 {
3774 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003775 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003776 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003777 else
3778 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003779 /* Try for a match in all lines of the buffer.
3780 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003781 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003782 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3783 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003784 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003785 col = 0;
3786 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003787 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003788 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003789 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003790 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003791 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003792 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003793 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003794 ml_get_buf(buf,
3795 regmatch.startpos[0].lnum + lnum, FALSE),
3796 regmatch.startpos[0].lnum + lnum,
3797 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003798 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003799 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003800 0, /* nr */
3801 0, /* type */
3802 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003803 ) == FAIL)
3804 {
3805 got_int = TRUE;
3806 break;
3807 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003808 found_match = TRUE;
3809 if (--tomatch == 0)
3810 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003811 if ((flags & VGR_GLOBAL) == 0
3812 || regmatch.endpos[0].lnum > 0)
3813 break;
3814 col = regmatch.endpos[0].col
3815 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003816 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003817 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003818 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003819 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003820 if (got_int)
3821 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003822 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003823#ifdef FEAT_AUTOCMD
3824 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3825#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003826
3827 if (using_dummy)
3828 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003829 if (found_match && first_match_buf == NULL)
3830 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003831 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003832 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003833 /* Never keep a dummy buffer if there is another buffer
3834 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003835 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003836 buf = NULL;
3837 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003838 else if (!cmdmod.hide
3839 || buf->b_p_bh[0] == 'u' /* "unload" */
3840 || buf->b_p_bh[0] == 'w' /* "wipe" */
3841 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003842 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003843 /* When no match was found we don't need to remember the
3844 * buffer, wipe it out. If there was a match and it
3845 * wasn't the first one or we won't jump there: only
3846 * unload the buffer.
3847 * Ignore 'hidden' here, because it may lead to having too
3848 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003849 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003850 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003851 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003852 buf = NULL;
3853 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003854 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003855 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003856 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003857 buf = NULL;
3858 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003859 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003860
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003861 if (buf != NULL)
3862 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003863 /* If the buffer is still loaded we need to use the
3864 * directory we jumped to below. */
3865 if (buf == first_match_buf
3866 && target_dir == NULL
3867 && STRCMP(dirname_start, dirname_now) != 0)
3868 target_dir = vim_strsave(dirname_now);
3869
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003870 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003871 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003872 * need to be done (again). But not the window-local
3873 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003874 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003875#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003876 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3877 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003878#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003879 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003880 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003881 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003882 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003883 }
3884 }
3885
3886 FreeWild(fcount, fnames);
3887
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003888 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3889 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3890 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003891
3892#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02003893 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003894#endif
3895
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003896#ifdef FEAT_AUTOCMD
3897 if (au_name != NULL)
3898 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3899 curbuf->b_fname, TRUE, curbuf);
3900#endif
3901
Bram Moolenaar86b68352004-12-27 21:59:20 +00003902 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003903 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003904 {
3905 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003906 {
3907 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003908 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003909 if (buf != curbuf)
3910 /* If we jumped to another buffer redrawing will already be
3911 * taken care of. */
3912 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003913
3914 /* Jump to the directory used after loading the buffer. */
3915 if (curbuf == first_match_buf && target_dir != NULL)
3916 {
3917 exarg_T ea;
3918
3919 ea.arg = target_dir;
3920 ea.cmdidx = CMD_lcd;
3921 ex_cd(&ea);
3922 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003923 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003924 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003925 else
3926 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003927
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003928 /* If we loaded a dummy buffer into the current window, the autocommands
3929 * may have messed up things, need to redraw and recompute folds. */
3930 if (redraw_for_dummy)
3931 {
3932#ifdef FEAT_FOLDING
3933 foldUpdateAll(curwin);
3934#else
3935 redraw_later(NOT_VALID);
3936#endif
3937 }
3938
Bram Moolenaar86b68352004-12-27 21:59:20 +00003939theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01003940 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003941 vim_free(dirname_now);
3942 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003943 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003944 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003945}
3946
3947/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003948 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003949 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003950 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3951 * If "s" is not NULL terminate the pattern with a NUL.
3952 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003953 */
3954 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003955skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003956{
3957 int c;
3958
3959 if (vim_isIDc(*p))
3960 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003961 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003962 if (s != NULL)
3963 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003964 p = skiptowhite(p);
3965 if (s != NULL && *p != NUL)
3966 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003967 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003968 else
3969 {
3970 /* ":vimgrep /pattern/[g][j] fname" */
3971 if (s != NULL)
3972 *s = p + 1;
3973 c = *p;
3974 p = skip_regexp(p + 1, c, TRUE, NULL);
3975 if (*p != c)
3976 return NULL;
3977
3978 /* Truncate the pattern. */
3979 if (s != NULL)
3980 *p = NUL;
3981 ++p;
3982
3983 /* Find the flags */
3984 while (*p == 'g' || *p == 'j')
3985 {
3986 if (flags != NULL)
3987 {
3988 if (*p == 'g')
3989 *flags |= VGR_GLOBAL;
3990 else
3991 *flags |= VGR_NOJUMP;
3992 }
3993 ++p;
3994 }
3995 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003996 return p;
3997}
3998
3999/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004000 * Restore current working directory to "dirname_start" if they differ, taking
4001 * into account whether it is set locally or globally.
4002 */
4003 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004004restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004005{
4006 char_u *dirname_now = alloc(MAXPATHL);
4007
4008 if (NULL != dirname_now)
4009 {
4010 mch_dirname(dirname_now, MAXPATHL);
4011 if (STRCMP(dirname_start, dirname_now) != 0)
4012 {
4013 /* If the directory has changed, change it back by building up an
4014 * appropriate ex command and executing it. */
4015 exarg_T ea;
4016
4017 ea.arg = dirname_start;
4018 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4019 ex_cd(&ea);
4020 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004021 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004022 }
4023}
4024
4025/*
4026 * Load file "fname" into a dummy buffer and return the buffer pointer,
4027 * placing the directory resulting from the buffer load into the
4028 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4029 * prior to calling this function. Restores directory to "dirname_start" prior
4030 * to returning, if autocmds or the 'autochdir' option have changed it.
4031 *
4032 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4033 * or wipe_dummy_buffer() later!
4034 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004035 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004036 */
4037 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004038load_dummy_buffer(
4039 char_u *fname,
4040 char_u *dirname_start, /* in: old directory */
4041 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004042{
4043 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004044 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004045 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004046 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004047
4048 /* Allocate a buffer without putting it in the buffer list. */
4049 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4050 if (newbuf == NULL)
4051 return NULL;
4052
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004053 /* Init the options. */
4054 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4055
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004056 /* need to open the memfile before putting the buffer in a window */
4057 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004058 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004059 /* set curwin/curbuf to buf and save a few things */
4060 aucmd_prepbuf(&aco, newbuf);
4061
4062 /* Need to set the filename for autocommands. */
4063 (void)setfname(curbuf, fname, NULL, FALSE);
4064
Bram Moolenaar81695252004-12-29 20:58:21 +00004065 /* Create swap file now to avoid the ATTENTION message. */
4066 check_need_swap(TRUE);
4067
4068 /* Remove the "dummy" flag, otherwise autocommands may not
4069 * work. */
4070 curbuf->b_flags &= ~BF_DUMMY;
4071
4072 if (readfile(fname, NULL,
4073 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4074 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004075 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004076 && !(curbuf->b_flags & BF_NEW))
4077 {
4078 failed = FALSE;
4079 if (curbuf != newbuf)
4080 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004081 /* Bloody autocommands changed the buffer! Can happen when
4082 * using netrw and editing a remote file. Use the current
4083 * buffer instead, delete the dummy one after restoring the
4084 * window stuff. */
4085 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004086 newbuf = curbuf;
4087 }
4088 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004089
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004090 /* restore curwin/curbuf and a few other things */
4091 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004092 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
4093 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004094 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004095
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004096 /*
4097 * When autocommands/'autochdir' option changed directory: go back.
4098 * Let the caller know what the resulting dir was first, in case it is
4099 * important.
4100 */
4101 mch_dirname(resulting_dir, MAXPATHL);
4102 restore_start_dir(dirname_start);
4103
Bram Moolenaar81695252004-12-29 20:58:21 +00004104 if (!buf_valid(newbuf))
4105 return NULL;
4106 if (failed)
4107 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004108 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004109 return NULL;
4110 }
4111 return newbuf;
4112}
4113
4114/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004115 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4116 * directory to "dirname_start" prior to returning, if autocmds or the
4117 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004118 */
4119 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004120wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004121{
4122 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004123 {
4124#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4125 cleanup_T cs;
4126
4127 /* Reset the error/interrupt/exception state here so that aborting()
4128 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4129 * work when got_int is set. */
4130 enter_cleanup(&cs);
4131#endif
4132
Bram Moolenaar81695252004-12-29 20:58:21 +00004133 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004134
4135#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4136 /* Restore the error/interrupt/exception state if not discarded by a
4137 * new aborting error, interrupt, or uncaught exception. */
4138 leave_cleanup(&cs);
4139#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004140 /* When autocommands/'autochdir' option changed directory: go back. */
4141 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004142 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004143}
4144
4145/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004146 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4147 * directory to "dirname_start" prior to returning, if autocmds or the
4148 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004149 */
4150 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004151unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004152{
4153 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004154 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004155 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004156
4157 /* When autocommands/'autochdir' option changed directory: go back. */
4158 restore_start_dir(dirname_start);
4159 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004160}
4161
Bram Moolenaar05159a02005-02-26 23:04:13 +00004162#if defined(FEAT_EVAL) || defined(PROTO)
4163/*
4164 * Add each quickfix error to list "list" as a dictionary.
4165 */
4166 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004167get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004168{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004169 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004170 dict_T *dict;
4171 char_u buf[2];
4172 qfline_T *qfp;
4173 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004174 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004175
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004176 if (wp != NULL)
4177 {
4178 qi = GET_LOC_LIST(wp);
4179 if (qi == NULL)
4180 return FAIL;
4181 }
4182
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004183 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004184 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004185 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004186
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004187 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4188 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004189 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004190 /* Handle entries with a non-existing buffer number. */
4191 bufnum = qfp->qf_fnum;
4192 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4193 bufnum = 0;
4194
Bram Moolenaar05159a02005-02-26 23:04:13 +00004195 if ((dict = dict_alloc()) == NULL)
4196 return FAIL;
4197 if (list_append_dict(list, dict) == FAIL)
4198 return FAIL;
4199
4200 buf[0] = qfp->qf_type;
4201 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004202 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004203 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4204 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4205 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4206 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004207 || dict_add_nr_str(dict, "pattern", 0L,
4208 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4209 || dict_add_nr_str(dict, "text", 0L,
4210 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004211 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4212 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4213 return FAIL;
4214
4215 qfp = qfp->qf_next;
4216 }
4217 return OK;
4218}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004219
4220/*
4221 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004222 * of dictionaries. "title" will be copied to w:quickfix_title.
4223 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004224 */
4225 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004226set_errorlist(
4227 win_T *wp,
4228 list_T *list,
4229 int action,
4230 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004231{
4232 listitem_T *li;
4233 dict_T *d;
4234 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004235 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004236 long lnum;
4237 int col, nr;
4238 int vcol;
4239 qfline_T *prevp = NULL;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004240#ifdef FEAT_WINDOWS
4241 qfline_T *old_last = NULL;
4242#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004243 int valid, status;
4244 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004245 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004246 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004247
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004248 if (wp != NULL)
4249 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004250 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004251 if (qi == NULL)
4252 return FAIL;
4253 }
4254
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004255 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004256 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004257 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004258 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004259 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004260 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004261 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004262 prevp->qf_next != prevp; prevp = prevp->qf_next)
4263 ;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004264#ifdef FEAT_WINDOWS
4265 old_last = prevp;
4266#endif
4267 }
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004268 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004269 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004270 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004271 qf_store_title(qi, title);
4272 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004273
4274 for (li = list->lv_first; li != NULL; li = li->li_next)
4275 {
4276 if (li->li_tv.v_type != VAR_DICT)
4277 continue; /* Skip non-dict items */
4278
4279 d = li->li_tv.vval.v_dict;
4280 if (d == NULL)
4281 continue;
4282
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004283 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004284 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004285 lnum = get_dict_number(d, (char_u *)"lnum");
4286 col = get_dict_number(d, (char_u *)"col");
4287 vcol = get_dict_number(d, (char_u *)"vcol");
4288 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004289 type = get_dict_string(d, (char_u *)"type", TRUE);
4290 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4291 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004292 if (text == NULL)
4293 text = vim_strsave((char_u *)"");
4294
4295 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004296 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004297 valid = FALSE;
4298
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004299 /* Mark entries with non-existing buffer number as not valid. Give the
4300 * error message only once. */
4301 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4302 {
4303 if (!did_bufnr_emsg)
4304 {
4305 did_bufnr_emsg = TRUE;
4306 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4307 }
4308 valid = FALSE;
4309 bufnum = 0;
4310 }
4311
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004312 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004313 NULL, /* dir */
4314 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004315 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004316 text,
4317 lnum,
4318 col,
4319 vcol, /* vis_col */
4320 pattern, /* search pattern */
4321 nr,
4322 type == NULL ? NUL : *type,
4323 valid);
4324
4325 vim_free(filename);
4326 vim_free(pattern);
4327 vim_free(text);
4328 vim_free(type);
4329
4330 if (status == FAIL)
4331 {
4332 retval = FAIL;
4333 break;
4334 }
4335 }
4336
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004337 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004338 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004339 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4340 else
4341 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004342 if (action != 'a') {
4343 qi->qf_lists[qi->qf_curlist].qf_ptr =
4344 qi->qf_lists[qi->qf_curlist].qf_start;
4345 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4346 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4347 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004348
4349#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004350 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004351 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004352#endif
4353
4354 return retval;
4355}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004356#endif
4357
Bram Moolenaar81695252004-12-29 20:58:21 +00004358/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004359 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004360 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004361 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004362 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004363 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004364 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004365 */
4366 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004367ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004368{
4369 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004370 qf_info_T *qi = &ql_info;
4371
Bram Moolenaardb552d602006-03-23 22:59:57 +00004372 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4373 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004374 {
4375 qi = ll_get_or_alloc_list(curwin);
4376 if (qi == NULL)
4377 return;
4378 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004379
4380 if (*eap->arg == NUL)
4381 buf = curbuf;
4382 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4383 buf = buflist_findnr(atoi((char *)eap->arg));
4384 if (buf == NULL)
4385 EMSG(_(e_invarg));
4386 else if (buf->b_ml.ml_mfp == NULL)
4387 EMSG(_("E681: Buffer is not loaded"));
4388 else
4389 {
4390 if (eap->addr_count == 0)
4391 {
4392 eap->line1 = 1;
4393 eap->line2 = buf->b_ml.ml_line_count;
4394 }
4395 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4396 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4397 EMSG(_(e_invrange));
4398 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004399 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004400 char_u *qf_title = *eap->cmdlinep;
4401
4402 if (buf->b_sfname)
4403 {
4404 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4405 (char *)qf_title, (char *)buf->b_sfname);
4406 qf_title = IObuff;
4407 }
4408
Bram Moolenaardb552d602006-03-23 22:59:57 +00004409 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4410 (eap->cmdidx != CMD_caddbuffer
4411 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004412 eap->line1, eap->line2,
4413 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004414 && (eap->cmdidx == CMD_cbuffer
4415 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004416 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4417 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004418 }
4419}
4420
Bram Moolenaar1e015462005-09-25 22:16:38 +00004421#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004422/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004423 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4424 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004425 */
4426 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004427ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004428{
4429 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004430 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004431
Bram Moolenaardb552d602006-03-23 22:59:57 +00004432 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4433 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004434 {
4435 qi = ll_get_or_alloc_list(curwin);
4436 if (qi == NULL)
4437 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004438 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004439
Bram Moolenaar4770d092006-01-12 23:22:24 +00004440 /* Evaluate the expression. When the result is a string or a list we can
4441 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004442 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004443 if (tv != NULL)
4444 {
4445 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4446 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4447 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004448 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004449 (eap->cmdidx != CMD_caddexpr
4450 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004451 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004452 && (eap->cmdidx == CMD_cexpr
4453 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004454 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004455 }
4456 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004457 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004458 free_tv(tv);
4459 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004460}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004461#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004462
4463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 * ":helpgrep {pattern}"
4465 */
4466 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004467ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
4469 regmatch_T regmatch;
4470 char_u *save_cpo;
4471 char_u *p;
4472 int fcount;
4473 char_u **fnames;
4474 FILE *fd;
4475 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004476 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004478#ifdef FEAT_MULTI_LANG
4479 char_u *lang;
4480#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004481 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004482 int new_qi = FALSE;
4483 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004484#ifdef FEAT_AUTOCMD
4485 char_u *au_name = NULL;
4486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004488#ifdef FEAT_MULTI_LANG
4489 /* Check for a specified language */
4490 lang = check_help_lang(eap->arg);
4491#endif
4492
Bram Moolenaar73633f82012-01-20 13:39:07 +01004493#ifdef FEAT_AUTOCMD
4494 switch (eap->cmdidx)
4495 {
4496 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4497 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4498 default: break;
4499 }
4500 if (au_name != NULL)
4501 {
4502 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4503 curbuf->b_fname, TRUE, curbuf);
4504 if (did_throw || force_abort)
4505 return;
4506 }
4507#endif
4508
4509 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4510 save_cpo = p_cpo;
4511 p_cpo = empty_option;
4512
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004513 if (eap->cmdidx == CMD_lhelpgrep)
4514 {
4515 /* Find an existing help window */
4516 FOR_ALL_WINDOWS(wp)
4517 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4518 break;
4519
4520 if (wp == NULL) /* Help window not found */
4521 qi = NULL;
4522 else
4523 qi = wp->w_llist;
4524
4525 if (qi == NULL)
4526 {
4527 /* Allocate a new location list for help text matches */
4528 if ((qi = ll_new_list()) == NULL)
4529 return;
4530 new_qi = TRUE;
4531 }
4532 }
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4535 regmatch.rm_ic = FALSE;
4536 if (regmatch.regprog != NULL)
4537 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004538#ifdef FEAT_MBYTE
4539 vimconv_T vc;
4540
4541 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4542 * differs. */
4543 vc.vc_type = CONV_NONE;
4544 if (!enc_utf8)
4545 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4546#endif
4547
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004549 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
4551 /* Go through all directories in 'runtimepath' */
4552 p = p_rtp;
4553 while (*p != NUL && !got_int)
4554 {
4555 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4556
4557 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4558 add_pathsep(NameBuff);
4559 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4560 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4561 &fnames, EW_FILE|EW_SILENT) == OK
4562 && fcount > 0)
4563 {
4564 for (fi = 0; fi < fcount && !got_int; ++fi)
4565 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004566#ifdef FEAT_MULTI_LANG
4567 /* Skip files for a different language. */
4568 if (lang != NULL
4569 && STRNICMP(lang, fnames[fi]
4570 + STRLEN(fnames[fi]) - 3, 2) != 0
4571 && !(STRNICMP(lang, "en", 2) == 0
4572 && STRNICMP("txt", fnames[fi]
4573 + STRLEN(fnames[fi]) - 3, 3) == 0))
4574 continue;
4575#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004576 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (fd != NULL)
4578 {
4579 lnum = 1;
4580 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4581 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004582 char_u *line = IObuff;
4583#ifdef FEAT_MBYTE
4584 /* Convert a line if 'encoding' is not utf-8 and
4585 * the line contains a non-ASCII character. */
4586 if (vc.vc_type != CONV_NONE
4587 && has_non_ascii(IObuff)) {
4588 line = string_convert(&vc, IObuff, NULL);
4589 if (line == NULL)
4590 line = IObuff;
4591 }
4592#endif
4593
4594 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004596 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004599 while (l > 0 && line[l - 1] <= ' ')
4600 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004602 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 NULL, /* dir */
4604 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004605 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004606 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004608 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004609 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004610 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004611 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 0, /* nr */
4613 1, /* type */
4614 TRUE /* valid */
4615 ) == FAIL)
4616 {
4617 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004618#ifdef FEAT_MBYTE
4619 if (line != IObuff)
4620 vim_free(line);
4621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 break;
4623 }
4624 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004625#ifdef FEAT_MBYTE
4626 if (line != IObuff)
4627 vim_free(line);
4628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 ++lnum;
4630 line_breakcheck();
4631 }
4632 fclose(fd);
4633 }
4634 }
4635 FreeWild(fcount, fnames);
4636 }
4637 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004638
Bram Moolenaar473de612013-06-08 18:19:48 +02004639 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004640#ifdef FEAT_MBYTE
4641 if (vc.vc_type != CONV_NONE)
4642 convert_setup(&vc, NULL, NULL);
4643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004645 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4646 qi->qf_lists[qi->qf_curlist].qf_ptr =
4647 qi->qf_lists[qi->qf_curlist].qf_start;
4648 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
4650
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004651 if (p_cpo == empty_option)
4652 p_cpo = save_cpo;
4653 else
4654 /* Darn, some plugin changed the value. */
4655 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
4657#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004658 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659#endif
4660
Bram Moolenaar73633f82012-01-20 13:39:07 +01004661#ifdef FEAT_AUTOCMD
4662 if (au_name != NULL)
4663 {
4664 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4665 curbuf->b_fname, TRUE, curbuf);
4666 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4667 /* autocommands made "qi" invalid */
4668 return;
4669 }
4670#endif
4671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004673 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004674 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004675 else
4676 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004677
4678 if (eap->cmdidx == CMD_lhelpgrep)
4679 {
4680 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004681 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004682 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4683 {
4684 if (new_qi)
4685 ll_free_all(&qi);
4686 }
4687 else if (curwin->w_llist == NULL)
4688 curwin->w_llist = qi;
4689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690}
4691
4692#endif /* FEAT_QUICKFIX */