blob: 5228c73e6180c96cf5ed81756be390601685598f [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 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020055 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000056 qfline_T *qf_ptr; /* pointer to the current error */
57 int qf_count; /* number of errors (0 means no error list) */
58 int qf_index; /* current index in the error list */
59 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020060 char_u *qf_title; /* title derived from the command that created
61 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000062} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
Bram Moolenaard12f5c12006-01-25 22:10:52 +000064struct qf_info_S
65{
66 /*
67 * Count of references to this list. Used only for location lists.
68 * When a location list window reference this list, qf_refcount
69 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
70 * reaches 0, the list is freed.
71 */
72 int qf_refcount;
73 int qf_listcount; /* current number of lists */
74 int qf_curlist; /* current error list */
75 qf_list_T qf_lists[LISTCOUNT];
76};
77
78static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaar68b76a62005-03-25 21:53:48 +000080#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
82/*
83 * Structure used to hold the info of one part of 'errorformat'
84 */
Bram Moolenaar01265852006-03-20 21:50:15 +000085typedef struct efm_S efm_T;
86struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000087{
88 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000089 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
91 char_u prefix; /* prefix of this format line: */
92 /* 'D' enter directory */
93 /* 'X' leave directory */
94 /* 'A' start of multi-line message */
95 /* 'E' error message */
96 /* 'W' warning message */
97 /* 'I' informational message */
98 /* 'C' continuation line */
99 /* 'Z' end of multi-line message */
100 /* 'G' general, unspecific message */
101 /* 'P' push file (partial) message */
102 /* 'Q' pop/quit file (partial) message */
103 /* 'O' overread (partial) message */
104 char_u flags; /* additional flags given in prefix */
105 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000106 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000107 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108};
109
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100110static 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);
111static void qf_store_title(qf_info_T *qi, char_u *title);
112static void qf_new_list(qf_info_T *qi, char_u *qf_title);
113static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200114static int qf_add_entry(qf_info_T *qi, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static qf_info_T *ll_new_list(void);
116static void qf_msg(qf_info_T *qi);
117static void qf_free(qf_info_T *qi, int idx);
118static char_u *qf_types(int, int);
119static int qf_get_fnum(char_u *, char_u *);
120static char_u *qf_push_dir(char_u *, struct dir_stack_T **);
121static char_u *qf_pop_dir(struct dir_stack_T **);
122static char_u *qf_guess_filepath(char_u *);
123static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
124static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
127static int is_qf_win(win_T *win, qf_info_T *qi);
128static win_T *qf_find_win(qf_info_T *qi);
129static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200130static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200132static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static char_u *get_mef_name(void);
135static void restore_start_dir(char_u *dirname_start);
136static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
137static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
138static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
139static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000141/* Quickfix window check helper macro */
142#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
143/* Location list window check helper macro */
144#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
145/*
146 * Return location list for window 'wp'
147 * For location list window, return the referenced location list
148 */
149#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000152 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200153 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 * Return -1 for error, number of errors for success.
155 */
156 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100157qf_init(
158 win_T *wp,
159 char_u *efile,
160 char_u *errorformat,
161 int newlist, /* TRUE: start a new error list */
162 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164 qf_info_T *qi = &ql_info;
165
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000166 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000167 {
168 qi = ll_get_or_alloc_list(wp);
169 if (qi == NULL)
170 return FAIL;
171 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172
173 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200174 (linenr_T)0, (linenr_T)0,
175 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000176}
177
178/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200179 * Maximum number of bytes allowed per line while reading a errorfile.
180 */
181#define LINE_MAXLEN 4096
182
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200183 static char_u *
184qf_grow_linebuf(char_u **growbuf, int *growbufsiz, int newsz, int *allocsz)
185{
186 /*
187 * If the line exceeds LINE_MAXLEN exclude the last
188 * byte since it's not a NL character.
189 */
190 *allocsz = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
191 if (*growbuf == NULL)
192 {
193 *growbuf = alloc(*allocsz + 1);
194 if (*growbuf == NULL)
195 return NULL;
196 *growbufsiz = *allocsz;
197 }
198 else if (*allocsz > *growbufsiz)
199 {
200 *growbuf = vim_realloc(*growbuf, *allocsz + 1);
201 if (*growbuf == NULL)
202 return NULL;
203 *growbufsiz = *allocsz;
204 }
205 return *growbuf;
206}
207
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200208/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000209 * Read the errorfile "efile" into memory, line by line, building the error
210 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +0200211 * Alternative: when "efile" is NULL read errors from buffer "buf".
212 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000213 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200214 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
215 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000216 * Return -1 for error, number of errors for success.
217 */
218 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100219qf_init_ext(
220 qf_info_T *qi,
221 char_u *efile,
222 buf_T *buf,
223 typval_T *tv,
224 char_u *errorformat,
225 int newlist, /* TRUE: start a new error list */
226 linenr_T lnumfirst, /* first line number to use */
227 linenr_T lnumlast, /* last line number to use */
228 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000229{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 char_u *namebuf;
231 char_u *errmsg;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200232 int errmsglen;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000233 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 char_u *fmtstr = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200235 char_u *growbuf = NULL;
236 int growbuflen;
Bram Moolenaar9a3b3312016-05-01 20:20:49 +0200237 int growbufsiz = 0;
238 char_u *linebuf = NULL;
239 int linelen = 0;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200240 int discard;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000242 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 int type = 0;
244 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000245 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 long lnum = 0L;
247 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000248 FILE *fd = NULL;
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 Moolenaar83e6d7a2016-06-02 22:08:05 +0200310#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000311 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +0200312 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200313 /* Adding to existing list, use last entry. */
314 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200315 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
318/*
319 * Each part of the format string is copied and modified from errorformat to
320 * regex prog. Only a few % characters are allowed.
321 */
322 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000323 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000324 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 else
326 efm = errorformat;
327 /*
328 * Get some space to modify the format string into.
329 */
330 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
331 for (round = FMT_PATTERNS; round > 0; )
332 i += (int)STRLEN(fmt_pat[--round].pattern);
333#ifdef COLON_IN_FILENAME
334 i += 12; /* "%f" can become twelve chars longer */
335#else
336 i += 2; /* "%f" can become two chars longer */
337#endif
338 if ((fmtstr = alloc(i)) == NULL)
339 goto error2;
340
Bram Moolenaar01265852006-03-20 21:50:15 +0000341 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
343 /*
344 * Allocate a new eformat structure and put it at the end of the list
345 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000346 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (fmt_ptr == NULL)
348 goto error2;
349 if (fmt_first == NULL) /* first one */
350 fmt_first = fmt_ptr;
351 else
352 fmt_last->next = fmt_ptr;
353 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
355 /*
356 * Isolate one part in the 'errorformat' option
357 */
358 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
359 if (efm[len] == '\\' && efm[len + 1] != NUL)
360 ++len;
361
362 /*
363 * Build regexp pattern from current 'errorformat' option
364 */
365 ptr = fmtstr;
366 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000367 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 for (efmp = efm; efmp < efm + len; ++efmp)
369 {
370 if (*efmp == '%')
371 {
372 ++efmp;
373 for (idx = 0; idx < FMT_PATTERNS; ++idx)
374 if (fmt_pat[idx].convchar == *efmp)
375 break;
376 if (idx < FMT_PATTERNS)
377 {
378 if (fmt_ptr->addr[idx])
379 {
380 sprintf((char *)errmsg,
381 _("E372: Too many %%%c in format string"), *efmp);
382 EMSG(errmsg);
383 goto error2;
384 }
385 if ((idx
386 && idx < 6
387 && vim_strchr((char_u *)"DXOPQ",
388 fmt_ptr->prefix) != NULL)
389 || (idx == 6
390 && vim_strchr((char_u *)"OPQ",
391 fmt_ptr->prefix) == NULL))
392 {
393 sprintf((char *)errmsg,
394 _("E373: Unexpected %%%c in format string"), *efmp);
395 EMSG(errmsg);
396 goto error2;
397 }
398 fmt_ptr->addr[idx] = (char_u)++round;
399 *ptr++ = '\\';
400 *ptr++ = '(';
401#ifdef BACKSLASH_IN_FILENAME
402 if (*efmp == 'f')
403 {
404 /* Also match "c:" in the file name, even when
405 * checking for a colon next: "%f:".
406 * "\%(\a:\)\=" */
407 STRCPY(ptr, "\\%(\\a:\\)\\=");
408 ptr += 10;
409 }
410#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000411 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000413 if (efmp[1] != '\\' && efmp[1] != '%')
414 {
415 /* A file name may contain spaces, but this isn't
416 * in "\f". For "%f:%l:%m" there may be a ":" in
417 * the file name. Use ".\{-1,}x" instead (x is
418 * the next character), the requirement that :999:
419 * follows should work. */
420 STRCPY(ptr, ".\\{-1,}");
421 ptr += 7;
422 }
423 else
424 {
425 /* File name followed by '\\' or '%': include as
426 * many file name chars as possible. */
427 STRCPY(ptr, "\\f\\+");
428 ptr += 4;
429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 }
431 else
432 {
433 srcptr = (char_u *)fmt_pat[idx].pattern;
434 while ((*ptr = *srcptr++) != NUL)
435 ++ptr;
436 }
437 *ptr++ = '\\';
438 *ptr++ = ')';
439 }
440 else if (*efmp == '*')
441 {
442 if (*++efmp == '[' || *efmp == '\\')
443 {
444 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
445 {
446 if (efmp[1] == '^')
447 *ptr++ = *++efmp;
448 if (efmp < efm + len)
449 {
450 *ptr++ = *++efmp; /* could be ']' */
451 while (efmp < efm + len
452 && (*ptr++ = *++efmp) != ']')
453 /* skip */;
454 if (efmp == efm + len)
455 {
456 EMSG(_("E374: Missing ] in format string"));
457 goto error2;
458 }
459 }
460 }
461 else if (efmp < efm + len) /* %*\D, %*\s etc. */
462 *ptr++ = *++efmp;
463 *ptr++ = '\\';
464 *ptr++ = '+';
465 }
466 else
467 {
468 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
469 sprintf((char *)errmsg,
470 _("E375: Unsupported %%%c in format string"), *efmp);
471 EMSG(errmsg);
472 goto error2;
473 }
474 }
475 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
476 *ptr++ = *efmp; /* regexp magic characters */
477 else if (*efmp == '#')
478 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000479 else if (*efmp == '>')
480 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 else if (efmp == efm + 1) /* analyse prefix */
482 {
483 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
484 fmt_ptr->flags = *efmp++;
485 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
486 fmt_ptr->prefix = *efmp;
487 else
488 {
489 sprintf((char *)errmsg,
490 _("E376: Invalid %%%c in format string prefix"), *efmp);
491 EMSG(errmsg);
492 goto error2;
493 }
494 }
495 else
496 {
497 sprintf((char *)errmsg,
498 _("E377: Invalid %%%c in format string"), *efmp);
499 EMSG(errmsg);
500 goto error2;
501 }
502 }
503 else /* copy normal character */
504 {
505 if (*efmp == '\\' && efmp + 1 < efm + len)
506 ++efmp;
507 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
508 *ptr++ = '\\'; /* escape regexp atoms */
509 if (*efmp)
510 *ptr++ = *efmp;
511 }
512 }
513 *ptr++ = '$';
514 *ptr = NUL;
515 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
516 goto error2;
517 /*
518 * Advance to next part
519 */
520 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
521 }
522 if (fmt_first == NULL) /* nothing found */
523 {
524 EMSG(_("E378: 'errorformat' contains no pattern"));
525 goto error2;
526 }
527
528 /*
529 * got_int is reset here, because it was probably set when killing the
530 * ":make" command, but we still want to read the errorfile then.
531 */
532 got_int = FALSE;
533
534 /* Always ignore case when looking for a matching error. */
535 regmatch.rm_ic = TRUE;
536
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000537 if (tv != NULL)
538 {
539 if (tv->v_type == VAR_STRING)
540 p_str = tv->vval.v_string;
541 else if (tv->v_type == VAR_LIST)
542 p_li = tv->vval.v_list->lv_first;
543 }
544
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 /*
546 * Read the lines in the error file one by one.
547 * Try to recognize one of the error formats in each line.
548 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000549 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000551 /* Get the next line. */
552 if (fd == NULL)
553 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000554 if (tv != NULL)
555 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000556 if (tv->v_type == VAR_STRING)
557 {
558 /* Get the next line from the supplied string */
559 char_u *p;
560
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200561 if (*p_str == NUL) /* Reached the end of the string */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000562 break;
563
564 p = vim_strchr(p_str, '\n');
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200565 if (p != NULL)
566 len = (int)(p - p_str) + 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000567 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000568 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000569
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200570 if (len > IOSIZE - 2)
571 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200572 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
573 &linelen);
574 if (linebuf == NULL)
575 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200576 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000577 else
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200578 {
579 linebuf = IObuff;
580 linelen = len;
581 }
582 vim_strncpy(linebuf, p_str, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000583
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200584 /*
585 * Increment using len in order to discard the rest of the
586 * line if it exceeds LINE_MAXLEN.
587 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000588 p_str += len;
589 }
590 else if (tv->v_type == VAR_LIST)
591 {
592 /* Get the next line from the supplied list */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200593 while (p_li != NULL
594 && (p_li->li_tv.v_type != VAR_STRING
595 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000596 p_li = p_li->li_next; /* Skip non-string items */
597
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200598 if (p_li == NULL) /* End of the list */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000599 break;
600
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000601 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200602 if (len > IOSIZE - 2)
603 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200604 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
605 &linelen);
606 if (linebuf == NULL)
607 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200608 }
609 else
610 {
611 linebuf = IObuff;
612 linelen = len;
613 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200615 vim_strncpy(linebuf, p_li->li_tv.vval.v_string, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000616
617 p_li = p_li->li_next; /* next item */
618 }
619 }
620 else
621 {
622 /* Get the next line from the supplied buffer */
623 if (buflnum > lnumlast)
624 break;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200625 p_buf = ml_get_buf(buf, buflnum++, FALSE);
626 linelen = (int)STRLEN(p_buf);
627 if (linelen > IOSIZE - 2)
628 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200629 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
630 &linelen);
631 if (linebuf == NULL)
632 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200633 }
634 else
635 linebuf = IObuff;
636 vim_strncpy(linebuf, p_buf, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000637 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000638 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200639 else
640 {
641 if (fgets((char *)IObuff, IOSIZE, fd) == NULL)
642 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000643
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200644 discard = FALSE;
645 linelen = (int)STRLEN(IObuff);
646 if (linelen == IOSIZE - 1 && (IObuff[linelen - 1] != '\n'
647#ifdef USE_CRNL
648 || IObuff[linelen - 1] != '\r'
649#endif
650 ))
651 {
652 /*
653 * The current line exceeds IObuff, continue reading using
654 * growbuf until EOL or LINE_MAXLEN bytes is read.
655 */
656 if (growbuf == NULL)
657 {
658 growbufsiz = 2 * (IOSIZE - 1);
659 growbuf = alloc(growbufsiz);
660 if (growbuf == NULL)
661 goto qf_init_end;
662 }
663
664 /* Copy the read part of the line, excluding null-terminator */
665 memcpy(growbuf, IObuff, IOSIZE - 1);
666 growbuflen = linelen;
667
668 for (;;)
669 {
670 if (fgets((char *)growbuf + growbuflen,
671 growbufsiz - growbuflen, fd) == NULL)
672 break;
Bram Moolenaard9db8b42016-05-08 12:52:05 +0200673 linelen = (int)STRLEN(growbuf + growbuflen);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200674 growbuflen += linelen;
675 if (growbuf[growbuflen - 1] == '\n'
676#ifdef USE_CRNL
677 || growbuf[growbuflen - 1] == '\r'
678#endif
679 )
680 break;
681 if (growbufsiz == LINE_MAXLEN)
682 {
683 discard = TRUE;
684 break;
685 }
686
687 growbufsiz = 2 * growbufsiz < LINE_MAXLEN
688 ? 2 * growbufsiz : LINE_MAXLEN;
689 growbuf = vim_realloc(growbuf, 2 * growbufsiz);
690 if (growbuf == NULL)
691 goto qf_init_end;
692 }
693
694 while (discard)
695 {
696 /*
697 * The current line is longer than LINE_MAXLEN, continue
698 * reading but discard everything until EOL or EOF is
699 * reached.
700 */
701 if (fgets((char *)IObuff, IOSIZE, fd) == NULL
702 || (int)STRLEN(IObuff) < IOSIZE - 1
703 || IObuff[IOSIZE - 1] == '\n'
704#ifdef USE_CRNL
705 || IObuff[IOSIZE - 1] == '\r'
706#endif
707 )
708 break;
709 }
710
711 linebuf = growbuf;
712 linelen = growbuflen;
713 }
714 else
715 linebuf = IObuff;
716 }
717
718 if (linelen > 0 && linebuf[linelen - 1] == '\n')
719 linebuf[linelen - 1] = NUL;
720#ifdef USE_CRNL
721 if (linelen > 0 && linebuf[linelen - 1] == '\r')
722 linebuf[linelen - 1] = NUL;
Bram Moolenaar836082d2011-08-10 13:21:46 +0200723#endif
724
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200725#ifdef FEAT_MBYTE
726 remove_bom(linebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#endif
728
Bram Moolenaar01265852006-03-20 21:50:15 +0000729 /* If there was no %> item start at the first pattern */
730 if (fmt_start == NULL)
731 fmt_ptr = fmt_first;
732 else
733 {
734 fmt_ptr = fmt_start;
735 fmt_start = NULL;
736 }
737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 /*
739 * Try to match each part of 'errorformat' until we find a complete
740 * match or no match.
741 */
742 valid = TRUE;
743restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000744 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100746 int r;
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 idx = fmt_ptr->prefix;
749 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
750 continue;
751 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000752 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (!multiscan)
754 errmsg[0] = NUL;
755 lnum = 0;
756 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000757 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 enr = -1;
759 type = 0;
760 tail = NULL;
761
762 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200763 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100764 fmt_ptr->prog = regmatch.regprog;
765 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 {
767 if ((idx == 'C' || idx == 'Z') && !multiline)
768 continue;
769 if (vim_strchr((char_u *)"EWI", idx) != NULL)
770 type = idx;
771 else
772 type = 0;
773 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000774 * Extract error message data from matched line.
775 * We check for an actual submatch, because "\[" and "\]" in
776 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 */
778 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
779 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000780 int c;
781
782 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
783 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000784
785 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000786 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000787 *regmatch.endp[i] = NUL;
788 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
789 *regmatch.endp[i] = c;
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000792 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 continue;
794 }
795 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000796 {
797 if (regmatch.startp[i] == NULL)
798 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000802 {
803 if (regmatch.startp[i] == NULL)
804 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000808 {
809 if (regmatch.startp[i] == NULL)
810 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000814 {
815 if (regmatch.startp[i] == NULL)
816 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000818 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000819 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200820 {
821 if (linelen > errmsglen) {
822 /* linelen + null terminator */
823 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
824 goto qf_init_end;
825 errmsglen = linelen + 1;
826 }
827 vim_strncpy(errmsg, linebuf, linelen);
828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
830 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000831 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
832 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200834 if (len > errmsglen) {
835 /* len + null terminator */
836 if ((errmsg = vim_realloc(errmsg, len + 1))
837 == NULL)
838 goto qf_init_end;
839 errmsglen = len + 1;
840 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000841 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000844 {
845 if (regmatch.startp[i] == NULL)
846 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
850 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200851 char_u *match_ptr;
852
Bram Moolenaar4169da72006-06-20 18:49:32 +0000853 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
854 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200855 col = 0;
856 for (match_ptr = regmatch.startp[i];
857 match_ptr != regmatch.endp[i]; ++match_ptr)
858 {
859 ++col;
860 if (*match_ptr == TAB)
861 {
862 col += 7;
863 col -= col % 8;
864 }
865 }
866 ++col;
867 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
870 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000871 if (regmatch.startp[i] == NULL)
872 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000874 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000876 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
877 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000878 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
879 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000880 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
881 if (len > CMDBUFFSIZE - 5)
882 len = CMDBUFFSIZE - 5;
883 STRCPY(pattern, "^\\V");
884 STRNCAT(pattern, regmatch.startp[i], len);
885 pattern[len + 3] = '\\';
886 pattern[len + 4] = '$';
887 pattern[len + 5] = NUL;
888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 break;
890 }
891 }
892 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000893
Bram Moolenaar4770d092006-01-12 23:22:24 +0000894 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000896 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
898 if (idx == 'D') /* enter directory */
899 {
900 if (*namebuf == NUL)
901 {
902 EMSG(_("E379: Missing or empty directory name"));
903 goto error2;
904 }
905 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
906 goto error2;
907 }
908 else if (idx == 'X') /* leave directory */
909 directory = qf_pop_dir(&dir_stack);
910 }
911 namebuf[0] = NUL; /* no match found, remove file name */
912 lnum = 0; /* don't jump to this line */
913 valid = FALSE;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200914 if (linelen > errmsglen) {
915 /* linelen + null terminator */
916 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
917 goto qf_init_end;
918 errmsglen = linelen + 1;
919 }
920 /* copy whole line to error message */
921 vim_strncpy(errmsg, linebuf, linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000922 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 multiline = multiignore = FALSE;
924 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000925 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000927 /* honor %> item */
928 if (fmt_ptr->conthere)
929 fmt_start = fmt_ptr;
930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100934 multiignore = FALSE; /* reset continuation */
935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
937 { /* continuation of multi-line msg */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200938 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
939
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (qfprev == NULL)
941 goto error2;
942 if (*errmsg && !multiignore)
943 {
944 len = (int)STRLEN(qfprev->qf_text);
945 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
946 == NULL)
947 goto error2;
948 STRCPY(ptr, qfprev->qf_text);
949 vim_free(qfprev->qf_text);
950 qfprev->qf_text = ptr;
951 *(ptr += len) = '\n';
952 STRCPY(++ptr, errmsg);
953 }
954 if (qfprev->qf_nr == -1)
955 qfprev->qf_nr = enr;
956 if (vim_isprintc(type) && !qfprev->qf_type)
957 qfprev->qf_type = type; /* only printable chars allowed */
958 if (!qfprev->qf_lnum)
959 qfprev->qf_lnum = lnum;
960 if (!qfprev->qf_col)
961 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000962 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 if (!qfprev->qf_fnum)
964 qfprev->qf_fnum = qf_get_fnum(directory,
965 *namebuf || directory ? namebuf
966 : currfile && valid ? currfile : 0);
967 if (idx == 'Z')
968 multiline = multiignore = FALSE;
969 line_breakcheck();
970 continue;
971 }
972 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
973 {
974 /* global file names */
975 valid = FALSE;
976 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
977 {
978 if (*namebuf && idx == 'P')
979 currfile = qf_push_dir(namebuf, &file_stack);
980 else if (idx == 'Q')
981 currfile = qf_pop_dir(&file_stack);
982 *namebuf = NUL;
983 if (tail && *tail)
984 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000985 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 multiscan = TRUE;
987 goto restofline;
988 }
989 }
990 }
991 if (fmt_ptr->flags == '-') /* generally exclude this line */
992 {
993 if (multiline)
994 multiignore = TRUE; /* also exclude continuation lines */
995 continue;
996 }
997 }
998
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200999 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001001 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001003 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001004 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 errmsg,
1006 lnum,
1007 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001008 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001009 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 enr,
1011 type,
1012 valid) == FAIL)
1013 goto error2;
1014 line_breakcheck();
1015 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00001016 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001018 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001020 /* no valid entry found */
1021 qi->qf_lists[qi->qf_curlist].qf_ptr =
1022 qi->qf_lists[qi->qf_curlist].qf_start;
1023 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1024 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 else
1027 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001028 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1029 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1030 qi->qf_lists[qi->qf_curlist].qf_ptr =
1031 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001033 /* return number of matches */
1034 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 goto qf_init_ok;
1036 }
1037 EMSG(_(e_readerrf));
1038error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001039 qf_free(qi, qi->qf_curlist);
1040 qi->qf_listcount--;
1041 if (qi->qf_curlist > 0)
1042 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +00001044 if (fd != NULL)
1045 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
1047 {
1048 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02001049 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 vim_free(fmt_ptr);
1051 }
1052 qf_clean_dir_stack(&dir_stack);
1053 qf_clean_dir_stack(&file_stack);
1054qf_init_end:
1055 vim_free(namebuf);
1056 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001057 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 vim_free(fmtstr);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001059 vim_free(growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
1061#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001062 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#endif
1064
1065 return retval;
1066}
1067
Bram Moolenaarfb604092014-07-23 15:55:00 +02001068 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001069qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001070{
1071 if (title != NULL)
1072 {
1073 char_u *p = alloc((int)STRLEN(title) + 2);
1074
1075 qi->qf_lists[qi->qf_curlist].qf_title = p;
1076 if (p != NULL)
1077 sprintf((char *)p, ":%s", (char *)title);
1078 }
1079}
1080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081/*
1082 * Prepare for adding a new quickfix list.
1083 */
1084 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001085qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
1087 int i;
1088
1089 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001090 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 * the current entry. This makes it possible to browse in a tree-like
1092 * way with ":grep'.
1093 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001094 while (qi->qf_listcount > qi->qf_curlist + 1)
1095 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
1097 /*
1098 * When the stack is full, remove to oldest entry
1099 * Otherwise, add a new entry.
1100 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001101 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001103 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001105 qi->qf_lists[i - 1] = qi->qf_lists[i];
1106 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
1108 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001109 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001110 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001111 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112}
1113
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001114/*
1115 * Free a location list
1116 */
1117 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001118ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001119{
1120 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001121 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001122
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001123 qi = *pqi;
1124 if (qi == NULL)
1125 return;
1126 *pqi = NULL; /* Remove reference to this list */
1127
1128 qi->qf_refcount--;
1129 if (qi->qf_refcount < 1)
1130 {
1131 /* No references to this location list */
1132 for (i = 0; i < qi->qf_listcount; ++i)
1133 qf_free(qi, i);
1134 vim_free(qi);
1135 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001136}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001137
1138 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001139qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001140{
1141 int i;
1142 qf_info_T *qi = &ql_info;
1143
1144 if (wp != NULL)
1145 {
1146 /* location list */
1147 ll_free_all(&wp->w_llist);
1148 ll_free_all(&wp->w_llist_ref);
1149 }
1150 else
1151 /* quickfix list */
1152 for (i = 0; i < qi->qf_listcount; ++i)
1153 qf_free(qi, i);
1154}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156/*
1157 * Add an entry to the end of the list of errors.
1158 * Returns OK or FAIL.
1159 */
1160 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001161qf_add_entry(
1162 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001163 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 Moolenaar83e6d7a2016-06-02 22:08:05 +02001176 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001178 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001180 if (bufnum != 0)
1181 qfp->qf_fnum = bufnum;
1182 else
1183 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1185 {
1186 vim_free(qfp);
1187 return FAIL;
1188 }
1189 qfp->qf_lnum = lnum;
1190 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001191 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001192 if (pattern == NULL || *pattern == NUL)
1193 qfp->qf_pattern = NULL;
1194 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1195 {
1196 vim_free(qfp->qf_text);
1197 vim_free(qfp);
1198 return FAIL;
1199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 qfp->qf_nr = nr;
1201 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1202 type = 0;
1203 qfp->qf_type = type;
1204 qfp->qf_valid = valid;
1205
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001206 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001207 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1208 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001210 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001211 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1212 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001213 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 }
1215 else
1216 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001217 qfp->qf_prev = *lastp;
1218 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001220 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001222 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001223 ++qi->qf_lists[qi->qf_curlist].qf_count;
1224 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1225 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001227 qi->qf_lists[qi->qf_curlist].qf_index =
1228 qi->qf_lists[qi->qf_curlist].qf_count;
1229 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 }
1231
1232 return OK;
1233}
1234
1235/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001236 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001237 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001238 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001239ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001240{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001241 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001242
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001243 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1244 if (qi != NULL)
1245 {
1246 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1247 qi->qf_refcount++;
1248 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001249
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001250 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001251}
1252
1253/*
1254 * Return the location list for window 'wp'.
1255 * If not present, allocate a location list
1256 */
1257 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001258ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001259{
1260 if (IS_LL_WINDOW(wp))
1261 /* For a location list window, use the referenced location list */
1262 return wp->w_llist_ref;
1263
1264 /*
1265 * For a non-location list window, w_llist_ref should not point to a
1266 * location list.
1267 */
1268 ll_free_all(&wp->w_llist_ref);
1269
1270 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001271 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001272 return wp->w_llist;
1273}
1274
1275/*
1276 * Copy the location list from window "from" to window "to".
1277 */
1278 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001279copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001280{
1281 qf_info_T *qi;
1282 int idx;
1283 int i;
1284
1285 /*
1286 * When copying from a location list window, copy the referenced
1287 * location list. For other windows, copy the location list for
1288 * that window.
1289 */
1290 if (IS_LL_WINDOW(from))
1291 qi = from->w_llist_ref;
1292 else
1293 qi = from->w_llist;
1294
1295 if (qi == NULL) /* no location list to copy */
1296 return;
1297
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001298 /* allocate a new location list */
1299 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001300 return;
1301
1302 to->w_llist->qf_listcount = qi->qf_listcount;
1303
1304 /* Copy the location lists one at a time */
1305 for (idx = 0; idx < qi->qf_listcount; idx++)
1306 {
1307 qf_list_T *from_qfl;
1308 qf_list_T *to_qfl;
1309
1310 to->w_llist->qf_curlist = idx;
1311
1312 from_qfl = &qi->qf_lists[idx];
1313 to_qfl = &to->w_llist->qf_lists[idx];
1314
1315 /* Some of the fields are populated by qf_add_entry() */
1316 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1317 to_qfl->qf_count = 0;
1318 to_qfl->qf_index = 0;
1319 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001320 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001321 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001322 if (from_qfl->qf_title != NULL)
1323 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1324 else
1325 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001326
1327 if (from_qfl->qf_count)
1328 {
1329 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001330 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001331
1332 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001333 for (i = 0, from_qfp = from_qfl->qf_start;
1334 i < from_qfl->qf_count && from_qfp != NULL;
1335 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001336 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001337 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001338 NULL,
1339 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001340 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001341 from_qfp->qf_text,
1342 from_qfp->qf_lnum,
1343 from_qfp->qf_col,
1344 from_qfp->qf_viscol,
1345 from_qfp->qf_pattern,
1346 from_qfp->qf_nr,
1347 0,
1348 from_qfp->qf_valid) == FAIL)
1349 {
1350 qf_free_all(to);
1351 return;
1352 }
1353 /*
1354 * qf_add_entry() will not set the qf_num field, as the
1355 * directory and file names are not supplied. So the qf_fnum
1356 * field is copied here.
1357 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001358 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001359 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1360 prevp->qf_type = from_qfp->qf_type; /* error type */
1361 if (from_qfl->qf_ptr == from_qfp)
1362 to_qfl->qf_ptr = prevp; /* current location */
1363 }
1364 }
1365
1366 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1367
1368 /* When no valid entries are present in the list, qf_ptr points to
1369 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001370 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001371 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001372 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001373 to_qfl->qf_index = 1;
1374 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001375 }
1376
1377 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1378}
1379
1380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 * get buffer number for file "dir.name"
1382 */
1383 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001384qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
1386 if (fname == NULL || *fname == NUL) /* no file name */
1387 return 0;
1388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 char_u *ptr;
1390 int fnum;
1391
Bram Moolenaare60acc12011-05-10 16:41:25 +02001392#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001394#endif
1395#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 if (directory != NULL)
1397 slash_adjust(directory);
1398 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (directory != NULL && !vim_isAbsName(fname)
1401 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1402 {
1403 /*
1404 * Here we check if the file really exists.
1405 * This should normally be true, but if make works without
1406 * "leaving directory"-messages we might have missed a
1407 * directory change.
1408 */
1409 if (mch_getperm(ptr) < 0)
1410 {
1411 vim_free(ptr);
1412 directory = qf_guess_filepath(fname);
1413 if (directory)
1414 ptr = concat_fnames(directory, fname, TRUE);
1415 else
1416 ptr = vim_strsave(fname);
1417 }
1418 /* Use concatenated directory name and file name */
1419 fnum = buflist_add(ptr, 0);
1420 vim_free(ptr);
1421 return fnum;
1422 }
1423 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425}
1426
1427/*
1428 * push dirbuf onto the directory stack and return pointer to actual dir or
1429 * NULL on error
1430 */
1431 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001432qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 struct dir_stack_T *ds_new;
1435 struct dir_stack_T *ds_ptr;
1436
1437 /* allocate new stack element and hook it in */
1438 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1439 if (ds_new == NULL)
1440 return NULL;
1441
1442 ds_new->next = *stackptr;
1443 *stackptr = ds_new;
1444
1445 /* store directory on the stack */
1446 if (vim_isAbsName(dirbuf)
1447 || (*stackptr)->next == NULL
1448 || (*stackptr && dir_stack != *stackptr))
1449 (*stackptr)->dirname = vim_strsave(dirbuf);
1450 else
1451 {
1452 /* Okay we don't have an absolute path.
1453 * dirbuf must be a subdir of one of the directories on the stack.
1454 * Let's search...
1455 */
1456 ds_new = (*stackptr)->next;
1457 (*stackptr)->dirname = NULL;
1458 while (ds_new)
1459 {
1460 vim_free((*stackptr)->dirname);
1461 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1462 TRUE);
1463 if (mch_isdir((*stackptr)->dirname) == TRUE)
1464 break;
1465
1466 ds_new = ds_new->next;
1467 }
1468
1469 /* clean up all dirs we already left */
1470 while ((*stackptr)->next != ds_new)
1471 {
1472 ds_ptr = (*stackptr)->next;
1473 (*stackptr)->next = (*stackptr)->next->next;
1474 vim_free(ds_ptr->dirname);
1475 vim_free(ds_ptr);
1476 }
1477
1478 /* Nothing found -> it must be on top level */
1479 if (ds_new == NULL)
1480 {
1481 vim_free((*stackptr)->dirname);
1482 (*stackptr)->dirname = vim_strsave(dirbuf);
1483 }
1484 }
1485
1486 if ((*stackptr)->dirname != NULL)
1487 return (*stackptr)->dirname;
1488 else
1489 {
1490 ds_ptr = *stackptr;
1491 *stackptr = (*stackptr)->next;
1492 vim_free(ds_ptr);
1493 return NULL;
1494 }
1495}
1496
1497
1498/*
1499 * pop dirbuf from the directory stack and return previous directory or NULL if
1500 * stack is empty
1501 */
1502 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001503qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 struct dir_stack_T *ds_ptr;
1506
1507 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1508 * What to do if it isn't? */
1509
1510 /* pop top element and free it */
1511 if (*stackptr != NULL)
1512 {
1513 ds_ptr = *stackptr;
1514 *stackptr = (*stackptr)->next;
1515 vim_free(ds_ptr->dirname);
1516 vim_free(ds_ptr);
1517 }
1518
1519 /* return NEW top element as current dir or NULL if stack is empty*/
1520 return *stackptr ? (*stackptr)->dirname : NULL;
1521}
1522
1523/*
1524 * clean up directory stack
1525 */
1526 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001527qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 struct dir_stack_T *ds_ptr;
1530
1531 while ((ds_ptr = *stackptr) != NULL)
1532 {
1533 *stackptr = (*stackptr)->next;
1534 vim_free(ds_ptr->dirname);
1535 vim_free(ds_ptr);
1536 }
1537}
1538
1539/*
1540 * Check in which directory of the directory stack the given file can be
1541 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001542 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 * Cleans up intermediate directory entries.
1544 *
1545 * TODO: How to solve the following problem?
1546 * If we have the this directory tree:
1547 * ./
1548 * ./aa
1549 * ./aa/bb
1550 * ./bb
1551 * ./bb/x.c
1552 * and make says:
1553 * making all in aa
1554 * making all in bb
1555 * x.c:9: Error
1556 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1557 * qf_guess_filepath will return NULL.
1558 */
1559 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001560qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562 struct dir_stack_T *ds_ptr;
1563 struct dir_stack_T *ds_tmp;
1564 char_u *fullname;
1565
1566 /* no dirs on the stack - there's nothing we can do */
1567 if (dir_stack == NULL)
1568 return NULL;
1569
1570 ds_ptr = dir_stack->next;
1571 fullname = NULL;
1572 while (ds_ptr)
1573 {
1574 vim_free(fullname);
1575 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1576
1577 /* If concat_fnames failed, just go on. The worst thing that can happen
1578 * is that we delete the entire stack.
1579 */
1580 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1581 break;
1582
1583 ds_ptr = ds_ptr->next;
1584 }
1585
1586 vim_free(fullname);
1587
1588 /* clean up all dirs we already left */
1589 while (dir_stack->next != ds_ptr)
1590 {
1591 ds_tmp = dir_stack->next;
1592 dir_stack->next = dir_stack->next->next;
1593 vim_free(ds_tmp->dirname);
1594 vim_free(ds_tmp);
1595 }
1596
1597 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1598
1599}
1600
1601/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001602 * When loading a file from the quickfix, the auto commands may modify it.
1603 * This may invalidate the current quickfix entry. This function checks
1604 * whether a entry is still present in the quickfix.
1605 * Similar to location list.
1606 */
1607 static int
1608is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1609{
1610 qf_list_T *qfl;
1611 qfline_T *qfp;
1612 int i;
1613
1614 qfl = &qi->qf_lists[qi->qf_curlist];
1615
1616 /* Search for the entry in the current list */
1617 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1618 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001619 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001620 break;
1621
1622 if (i == qfl->qf_count) /* Entry is not found */
1623 return FALSE;
1624
1625 return TRUE;
1626}
1627
1628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 * jump to a quickfix line
1630 * if dir == FORWARD go "errornr" valid entries forward
1631 * if dir == BACKWARD go "errornr" valid entries backward
1632 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1633 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1634 * else if "errornr" is zero, redisplay the same line
1635 * else go to entry "errornr"
1636 */
1637 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001638qf_jump(
1639 qf_info_T *qi,
1640 int dir,
1641 int errornr,
1642 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001645 qfline_T *qf_ptr;
1646 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 int qf_index;
1648 int old_qf_fnum;
1649 int old_qf_index;
1650 int prev_index;
1651 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1652 char_u *err = e_no_more_items;
1653 linenr_T i;
1654 buf_T *old_curbuf;
1655 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 colnr_T screen_col;
1657 colnr_T char_col;
1658 char_u *line;
1659#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001660 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001661 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 int opened_window = FALSE;
1663 win_T *win;
1664 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001665 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001667 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 int print_message = TRUE;
1669 int len;
1670#ifdef FEAT_FOLDING
1671 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1672#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001673 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001674 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001676 if (qi == NULL)
1677 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001678
1679 if (qi->qf_curlist >= qi->qf_listcount
1680 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 {
1682 EMSG(_(e_quickfix));
1683 return;
1684 }
1685
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001686 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001688 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 old_qf_index = qf_index;
1690 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1691 {
1692 while (errornr--)
1693 {
1694 old_qf_ptr = qf_ptr;
1695 prev_index = qf_index;
1696 old_qf_fnum = qf_ptr->qf_fnum;
1697 do
1698 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001699 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 || qf_ptr->qf_next == NULL)
1701 {
1702 qf_ptr = old_qf_ptr;
1703 qf_index = prev_index;
1704 if (err != NULL)
1705 {
1706 EMSG(_(err));
1707 goto theend;
1708 }
1709 errornr = 0;
1710 break;
1711 }
1712 ++qf_index;
1713 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001714 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1715 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1717 err = NULL;
1718 }
1719 }
1720 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1721 {
1722 while (errornr--)
1723 {
1724 old_qf_ptr = qf_ptr;
1725 prev_index = qf_index;
1726 old_qf_fnum = qf_ptr->qf_fnum;
1727 do
1728 {
1729 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1730 {
1731 qf_ptr = old_qf_ptr;
1732 qf_index = prev_index;
1733 if (err != NULL)
1734 {
1735 EMSG(_(err));
1736 goto theend;
1737 }
1738 errornr = 0;
1739 break;
1740 }
1741 --qf_index;
1742 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001743 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1744 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1746 err = NULL;
1747 }
1748 }
1749 else if (errornr != 0) /* go to specified number */
1750 {
1751 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1752 {
1753 --qf_index;
1754 qf_ptr = qf_ptr->qf_prev;
1755 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001756 while (errornr > qf_index && qf_index <
1757 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 && qf_ptr->qf_next != NULL)
1759 {
1760 ++qf_index;
1761 qf_ptr = qf_ptr->qf_next;
1762 }
1763 }
1764
1765#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001766 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1767 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 /* No need to print the error message if it's visible in the error
1769 * window */
1770 print_message = FALSE;
1771
1772 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001773 * For ":helpgrep" find a help window or open one.
1774 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001775 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001776 {
1777 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001778
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001779 if (cmdmod.tab != 0)
1780 wp = NULL;
1781 else
1782 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1783 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1784 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001785 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1786 win_enter(wp, TRUE);
1787 else
1788 {
1789 /*
1790 * Split off help window; put it at far top if no position
1791 * specified, the current window is vertically split and narrow.
1792 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001793 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001794 if (cmdmod.split == 0 && curwin->w_width != Columns
1795 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001796 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001797 if (qi != &ql_info)
1798 flags |= WSP_NEWLOC; /* don't copy the location list */
1799
1800 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001801 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001802 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001803
1804 if (curwin->w_height < p_hh)
1805 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001806
1807 if (qi != &ql_info) /* not a quickfix list */
1808 {
1809 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001810 curwin->w_llist = qi;
1811 qi->qf_refcount++;
1812 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001813 }
1814
1815 if (!p_im)
1816 restart_edit = 0; /* don't want insert mode in help file */
1817 }
1818
1819 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 * If currently in the quickfix window, find another window to show the
1821 * file in.
1822 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001823 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001825 win_T *usable_win_ptr = NULL;
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 /*
1828 * If there is no file specified, we don't know where to go.
1829 * But do advance, otherwise ":cn" gets stuck.
1830 */
1831 if (qf_ptr->qf_fnum == 0)
1832 goto theend;
1833
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001834 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001835
1836 ll_ref = curwin->w_llist_ref;
1837 if (ll_ref != NULL)
1838 {
1839 /* Find a window using the same location list that is not a
1840 * quickfix window. */
1841 FOR_ALL_WINDOWS(usable_win_ptr)
1842 if (usable_win_ptr->w_llist == ll_ref
1843 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001844 {
1845 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001846 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001847 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001848 }
1849
1850 if (!usable_win)
1851 {
1852 /* Locate a window showing a normal buffer */
1853 FOR_ALL_WINDOWS(win)
1854 if (win->w_buffer->b_p_bt[0] == NUL)
1855 {
1856 usable_win = 1;
1857 break;
1858 }
1859 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001860
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001862 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001863 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001865 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001866 {
1867 tabpage_T *tp;
1868 win_T *wp;
1869
1870 FOR_ALL_TAB_WINDOWS(tp, wp)
1871 {
1872 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1873 {
1874 goto_tabpage_win(tp, wp);
1875 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001876 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001877 }
1878 }
1879 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001880win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001881
1882 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001883 * If there is only one window and it is the quickfix window, create a
1884 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001885 */
1886 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001888 flags = WSP_ABOVE;
1889 if (ll_ref != NULL)
1890 flags |= WSP_NEWLOC;
1891 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 goto failed; /* not enough room for window */
1893 opened_window = TRUE; /* close it when fail */
1894 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001895 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001896 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001897 if (ll_ref != NULL)
1898 {
1899 /* The new window should use the location list from the
1900 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001901 curwin->w_llist = ll_ref;
1902 ll_ref->qf_refcount++;
1903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
1905 else
1906 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001907 if (curwin->w_llist_ref != NULL)
1908 {
1909 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001910 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001911 if (win == NULL)
1912 {
1913 /* Find the window showing the selected file */
1914 FOR_ALL_WINDOWS(win)
1915 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1916 break;
1917 if (win == NULL)
1918 {
1919 /* Find a previous usable window */
1920 win = curwin;
1921 do
1922 {
1923 if (win->w_buffer->b_p_bt[0] == NUL)
1924 break;
1925 if (win->w_prev == NULL)
1926 win = lastwin; /* wrap around the top */
1927 else
1928 win = win->w_prev; /* go to previous window */
1929 } while (win != curwin);
1930 }
1931 }
1932 win_goto(win);
1933
1934 /* If the location list for the window is not set, then set it
1935 * to the location list from the location window */
1936 if (win->w_llist == NULL)
1937 {
1938 win->w_llist = ll_ref;
1939 ll_ref->qf_refcount++;
1940 }
1941 }
1942 else
1943 {
1944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 /*
1946 * Try to find a window that shows the right buffer.
1947 * Default to the window just above the quickfix buffer.
1948 */
1949 win = curwin;
1950 altwin = NULL;
1951 for (;;)
1952 {
1953 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1954 break;
1955 if (win->w_prev == NULL)
1956 win = lastwin; /* wrap around the top */
1957 else
1958 win = win->w_prev; /* go to previous window */
1959
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001960 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
1962 /* Didn't find it, go to the window before the quickfix
1963 * window. */
1964 if (altwin != NULL)
1965 win = altwin;
1966 else if (curwin->w_prev != NULL)
1967 win = curwin->w_prev;
1968 else
1969 win = curwin->w_next;
1970 break;
1971 }
1972
1973 /* Remember a usable window. */
1974 if (altwin == NULL && !win->w_p_pvw
1975 && win->w_buffer->b_p_bt[0] == NUL)
1976 altwin = win;
1977 }
1978
1979 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982 }
1983#endif
1984
1985 /*
1986 * If there is a file name,
1987 * read the wanted file if needed, and check autowrite etc.
1988 */
1989 old_curbuf = curbuf;
1990 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001991
1992 if (qf_ptr->qf_fnum != 0)
1993 {
1994 if (qf_ptr->qf_type == 1)
1995 {
1996 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1997 * set b_p_ro flag). */
1998 if (!can_abandon(curbuf, forceit))
1999 {
2000 EMSG(_(e_nowrtmsg));
2001 ok = FALSE;
2002 }
2003 else
2004 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002005 ECMD_HIDE + ECMD_SET_HELP,
2006 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002007 }
2008 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002009 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002010 int old_qf_curlist = qi->qf_curlist;
2011 int is_abort = FALSE;
2012
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002013 ok = buflist_getfile(qf_ptr->qf_fnum,
2014 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002015 if (qi != &ql_info && !win_valid(oldwin))
2016 {
2017 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002018 is_abort = TRUE;
2019 opened_window = FALSE;
2020 }
2021 else if (old_qf_curlist != qi->qf_curlist
2022 || !is_qf_entry_present(qi, qf_ptr))
2023 {
2024 if (qi == &ql_info)
2025 EMSG(_("E925: Current quickfix was changed"));
2026 else
2027 EMSG(_("E926: Current location list was changed"));
2028 is_abort = TRUE;
2029 }
2030
2031 if (is_abort)
2032 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002033 ok = FALSE;
2034 qi = NULL;
2035 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002036 }
2037 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002038 }
2039
2040 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 /* When not switched to another buffer, still need to set pc mark */
2043 if (curbuf == old_curbuf)
2044 setpcmark();
2045
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002046 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002048 /*
2049 * Go to line with error, unless qf_lnum is 0.
2050 */
2051 i = qf_ptr->qf_lnum;
2052 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002054 if (i > curbuf->b_ml.ml_line_count)
2055 i = curbuf->b_ml.ml_line_count;
2056 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002058 if (qf_ptr->qf_col > 0)
2059 {
2060 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002061#ifdef FEAT_VIRTUALEDIT
2062 curwin->w_cursor.coladd = 0;
2063#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002064 if (qf_ptr->qf_viscol == TRUE)
2065 {
2066 /*
2067 * Check each character from the beginning of the error
2068 * line up to the error column. For each tab character
2069 * found, reduce the error column value by the length of
2070 * a tab character.
2071 */
2072 line = ml_get_curline();
2073 screen_col = 0;
2074 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2075 {
2076 if (*line == NUL)
2077 break;
2078 if (*line++ == '\t')
2079 {
2080 curwin->w_cursor.col -= 7 - (screen_col % 8);
2081 screen_col += 8 - (screen_col % 8);
2082 }
2083 else
2084 ++screen_col;
2085 }
2086 }
2087 check_cursor();
2088 }
2089 else
2090 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
2092 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002093 {
2094 pos_T save_cursor;
2095
2096 /* Move the cursor to the first line in the buffer */
2097 save_cursor = curwin->w_cursor;
2098 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002099 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2100 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002101 curwin->w_cursor = save_cursor;
2102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
2104#ifdef FEAT_FOLDING
2105 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2106 foldOpenCursor();
2107#endif
2108 if (print_message)
2109 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002110 /* Update the screen before showing the message, unless the screen
2111 * scrolled up. */
2112 if (!msg_scrolled)
2113 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002115 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2117 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2118 /* Add the message, skipping leading whitespace and newlines. */
2119 len = (int)STRLEN(IObuff);
2120 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2121
2122 /* Output the message. Overwrite to avoid scrolling when the 'O'
2123 * flag is present in 'shortmess'; But when not jumping, print the
2124 * whole message. */
2125 i = msg_scroll;
2126 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2127 msg_scroll = TRUE;
2128 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2129 msg_scroll = FALSE;
2130 msg_attr_keep(IObuff, 0, TRUE);
2131 msg_scroll = i;
2132 }
2133 }
2134 else
2135 {
2136#ifdef FEAT_WINDOWS
2137 if (opened_window)
2138 win_close(curwin, TRUE); /* Close opened window */
2139#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002140 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
2142 /*
2143 * Couldn't open file, so put index back where it was. This could
2144 * happen if the file was readonly and we changed something.
2145 */
2146#ifdef FEAT_WINDOWS
2147failed:
2148#endif
2149 qf_ptr = old_qf_ptr;
2150 qf_index = old_qf_index;
2151 }
2152 }
2153theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002154 if (qi != NULL)
2155 {
2156 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2157 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159#ifdef FEAT_WINDOWS
2160 if (p_swb != old_swb && opened_window)
2161 {
2162 /* Restore old 'switchbuf' value, but not when an autocommand or
2163 * modeline has changed the value. */
2164 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002167 swb_flags = old_swb_flags;
2168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 else
2170 free_string_option(old_swb);
2171 }
2172#endif
2173}
2174
2175/*
2176 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002177 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 */
2179 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002180qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002182 buf_T *buf;
2183 char_u *fname;
2184 qfline_T *qfp;
2185 int i;
2186 int idx1 = 1;
2187 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002188 char_u *arg = eap->arg;
2189 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002191 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002193 if (eap->cmdidx == CMD_llist)
2194 {
2195 qi = GET_LOC_LIST(curwin);
2196 if (qi == NULL)
2197 {
2198 EMSG(_(e_loclist));
2199 return;
2200 }
2201 }
2202
2203 if (qi->qf_curlist >= qi->qf_listcount
2204 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
2206 EMSG(_(e_quickfix));
2207 return;
2208 }
2209 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2210 {
2211 EMSG(_(e_trailing));
2212 return;
2213 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002214 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (idx1 < 0)
2216 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2217 if (idx2 < 0)
2218 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2219
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002220 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002222 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2223 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
2225 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2226 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002227 msg_putchar('\n');
2228 if (got_int)
2229 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002230
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002231 fname = NULL;
2232 if (qfp->qf_fnum != 0
2233 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2234 {
2235 fname = buf->b_fname;
2236 if (qfp->qf_type == 1) /* :helpgrep */
2237 fname = gettail(fname);
2238 }
2239 if (fname == NULL)
2240 sprintf((char *)IObuff, "%2d", i);
2241 else
2242 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2243 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002244 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002245 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2246 if (qfp->qf_lnum == 0)
2247 IObuff[0] = NUL;
2248 else if (qfp->qf_col == 0)
2249 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2250 else
2251 sprintf((char *)IObuff, ":%ld col %d",
2252 qfp->qf_lnum, qfp->qf_col);
2253 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2254 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2255 msg_puts_attr(IObuff, hl_attr(HLF_N));
2256 if (qfp->qf_pattern != NULL)
2257 {
2258 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2259 STRCAT(IObuff, ":");
2260 msg_puts(IObuff);
2261 }
2262 msg_puts((char_u *)" ");
2263
2264 /* Remove newlines and leading whitespace from the text. For an
2265 * unrecognized line keep the indent, the compiler may mark a word
2266 * with ^^^^. */
2267 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2269 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002270 msg_prt_line(IObuff, FALSE);
2271 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002273
2274 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002275 if (qfp == NULL)
2276 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002277 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 ui_breakcheck();
2279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280}
2281
2282/*
2283 * Remove newlines and leading whitespace from an error message.
2284 * Put the result in "buf[bufsize]".
2285 */
2286 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002287qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 int i;
2290 char_u *p = text;
2291
2292 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2293 {
2294 if (*p == '\n')
2295 {
2296 buf[i] = ' ';
2297 while (*++p != NUL)
2298 if (!vim_iswhite(*p) && *p != '\n')
2299 break;
2300 }
2301 else
2302 buf[i] = *p++;
2303 }
2304 buf[i] = NUL;
2305}
2306
2307/*
2308 * ":colder [count]": Up in the quickfix stack.
2309 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002310 * ":lolder [count]": Up in the location list stack.
2311 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 */
2313 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002314qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002316 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 int count;
2318
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002319 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2320 {
2321 qi = GET_LOC_LIST(curwin);
2322 if (qi == NULL)
2323 {
2324 EMSG(_(e_loclist));
2325 return;
2326 }
2327 }
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 if (eap->addr_count != 0)
2330 count = eap->line2;
2331 else
2332 count = 1;
2333 while (count--)
2334 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002335 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002337 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002342 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
2344 else
2345 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002346 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002351 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002354 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355}
2356
2357 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002358qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
2360 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002361 qi->qf_curlist + 1, qi->qf_listcount,
2362 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002364 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366}
2367
2368/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002369 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 */
2371 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002372qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002374 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002375 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002376 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002378 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002380 qfp = qi->qf_lists[idx].qf_start;
2381 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002382 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002383 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002384 vim_free(qfp->qf_text);
2385 stop = (qfp == qfpnext);
2386 vim_free(qfp->qf_pattern);
2387 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002388 if (stop)
2389 /* Somehow qf_count may have an incorrect value, set it to 1
2390 * to avoid crashing when it's wrong.
2391 * TODO: Avoid qf_count being incorrect. */
2392 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002393 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002394 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002395 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002397 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002398 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002399 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400}
2401
2402/*
2403 * qf_mark_adjust: adjust marks
2404 */
2405 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002406qf_mark_adjust(
2407 win_T *wp,
2408 linenr_T line1,
2409 linenr_T line2,
2410 long amount,
2411 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002413 int i;
2414 qfline_T *qfp;
2415 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002416 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002418 if (wp != NULL)
2419 {
2420 if (wp->w_llist == NULL)
2421 return;
2422 qi = wp->w_llist;
2423 }
2424
2425 for (idx = 0; idx < qi->qf_listcount; ++idx)
2426 if (qi->qf_lists[idx].qf_count)
2427 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002428 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2429 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (qfp->qf_fnum == curbuf->b_fnum)
2431 {
2432 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2433 {
2434 if (amount == MAXLNUM)
2435 qfp->qf_cleared = TRUE;
2436 else
2437 qfp->qf_lnum += amount;
2438 }
2439 else if (amount_after && qfp->qf_lnum > line2)
2440 qfp->qf_lnum += amount_after;
2441 }
2442}
2443
2444/*
2445 * Make a nice message out of the error character and the error number:
2446 * char number message
2447 * e or E 0 " error"
2448 * w or W 0 " warning"
2449 * i or I 0 " info"
2450 * 0 0 ""
2451 * other 0 " c"
2452 * e or E n " error n"
2453 * w or W n " warning n"
2454 * i or I n " info n"
2455 * 0 n " error n"
2456 * other n " c n"
2457 * 1 x "" :helpgrep
2458 */
2459 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002460qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462 static char_u buf[20];
2463 static char_u cc[3];
2464 char_u *p;
2465
2466 if (c == 'W' || c == 'w')
2467 p = (char_u *)" warning";
2468 else if (c == 'I' || c == 'i')
2469 p = (char_u *)" info";
2470 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2471 p = (char_u *)" error";
2472 else if (c == 0 || c == 1)
2473 p = (char_u *)"";
2474 else
2475 {
2476 cc[0] = ' ';
2477 cc[1] = c;
2478 cc[2] = NUL;
2479 p = cc;
2480 }
2481
2482 if (nr <= 0)
2483 return p;
2484
2485 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2486 return buf;
2487}
2488
2489#if defined(FEAT_WINDOWS) || defined(PROTO)
2490/*
2491 * ":cwindow": open the quickfix window if we have errors to display,
2492 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002493 * ":lwindow": open the location list window if we have locations to display,
2494 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 */
2496 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002497ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002499 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 win_T *win;
2501
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002502 if (eap->cmdidx == CMD_lwindow)
2503 {
2504 qi = GET_LOC_LIST(curwin);
2505 if (qi == NULL)
2506 return;
2507 }
2508
2509 /* Look for an existing quickfix window. */
2510 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512 /*
2513 * If a quickfix window is open but we have no errors to display,
2514 * close the window. If a quickfix window is not open, then open
2515 * it if we have errors; otherwise, leave it closed.
2516 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002517 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002518 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002519 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 if (win != NULL)
2522 ex_cclose(eap);
2523 }
2524 else if (win == NULL)
2525 ex_copen(eap);
2526}
2527
2528/*
2529 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002530 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002533ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002535 win_T *win = NULL;
2536 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002538 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2539 {
2540 qi = GET_LOC_LIST(curwin);
2541 if (qi == NULL)
2542 return;
2543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002545 /* Find existing quickfix window and close it. */
2546 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (win != NULL)
2548 win_close(win, FALSE);
2549}
2550
2551/*
2552 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002553 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 */
2555 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002556ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002558 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002561 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002562 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002563 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002565 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2566 {
2567 qi = GET_LOC_LIST(curwin);
2568 if (qi == NULL)
2569 {
2570 EMSG(_(e_loclist));
2571 return;
2572 }
2573 }
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (eap->addr_count != 0)
2576 height = eap->line2;
2577 else
2578 height = QF_WINHEIGHT;
2579
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581#ifdef FEAT_GUI
2582 need_mouse_correct = TRUE;
2583#endif
2584
2585 /*
2586 * Find existing quickfix window, or open a new one.
2587 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002588 win = qf_find_win(qi);
2589
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002590 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002593 if (eap->addr_count != 0)
2594 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002595 if (cmdmod.split & WSP_VERT)
2596 {
2597 if (height != W_WIDTH(win))
2598 win_setwidth(height);
2599 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002600 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002601 win_setheight(height);
2602 }
2603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 else
2605 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002606 qf_buf = qf_find_buf(qi);
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 /* The current window becomes the previous window afterwards. */
2609 win = curwin;
2610
Bram Moolenaar77642c02012-11-20 17:55:10 +01002611 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2612 && cmdmod.split == 0)
2613 /* Create the new window at the very bottom, except when
2614 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002615 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002616 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002618 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002620 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002622 /*
2623 * For the location list window, create a reference to the
2624 * location list from the window 'win'.
2625 */
2626 curwin->w_llist_ref = win->w_llist;
2627 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002629
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002630 if (oldwin != curwin)
2631 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002632 if (qf_buf != NULL)
2633 /* Use the existing quickfix buffer */
2634 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002635 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002636 else
2637 {
2638 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002639 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002640 /* switch off 'swapfile' */
2641 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2642 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002643 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002644 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002645 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002646#ifdef FEAT_DIFF
2647 curwin->w_p_diff = FALSE;
2648#endif
2649#ifdef FEAT_FOLDING
2650 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2651 OPT_LOCAL);
2652#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002655 /* Only set the height when still in the same tab page and there is no
2656 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002657 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 win_setheight(height);
2659 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2660 if (win_valid(win))
2661 prevwin = win;
2662 }
2663
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002664 qf_set_title_var(qi);
2665
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 /*
2667 * Fill the buffer with the quickfix list.
2668 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002669 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002671 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 curwin->w_cursor.col = 0;
2673 check_cursor();
2674 update_topline(); /* scroll to show the line */
2675}
2676
2677/*
2678 * Return the number of the current entry (line number in the quickfix
2679 * window).
2680 */
2681 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002682qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002684 qf_info_T *qi = &ql_info;
2685
2686 if (IS_LL_WINDOW(wp))
2687 /* In the location list window, use the referenced location list */
2688 qi = wp->w_llist_ref;
2689
2690 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691}
2692
2693/*
2694 * Update the cursor position in the quickfix window to the current error.
2695 * Return TRUE if there is a quickfix window.
2696 */
2697 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002698qf_win_pos_update(
2699 qf_info_T *qi,
2700 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002703 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 /*
2706 * Put the cursor on the current error in the quickfix window, so that
2707 * it's viewable.
2708 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002709 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (win != NULL
2711 && qf_index <= win->w_buffer->b_ml.ml_line_count
2712 && old_qf_index != qf_index)
2713 {
2714 win_T *old_curwin = curwin;
2715
2716 curwin = win;
2717 curbuf = win->w_buffer;
2718 if (qf_index > old_qf_index)
2719 {
2720 curwin->w_redraw_top = old_qf_index;
2721 curwin->w_redraw_bot = qf_index;
2722 }
2723 else
2724 {
2725 curwin->w_redraw_top = qf_index;
2726 curwin->w_redraw_bot = old_qf_index;
2727 }
2728 curwin->w_cursor.lnum = qf_index;
2729 curwin->w_cursor.col = 0;
2730 update_topline(); /* scroll to show the line */
2731 redraw_later(VALID);
2732 curwin->w_redr_status = TRUE; /* update ruler */
2733 curwin = old_curwin;
2734 curbuf = curwin->w_buffer;
2735 }
2736 return win != NULL;
2737}
2738
2739/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002740 * Check whether the given window is displaying the specified quickfix/location
2741 * list buffer
2742 */
2743 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002744is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002745{
2746 /*
2747 * A window displaying the quickfix buffer will have the w_llist_ref field
2748 * set to NULL.
2749 * A window displaying a location list buffer will have the w_llist_ref
2750 * pointing to the location list.
2751 */
2752 if (bt_quickfix(win->w_buffer))
2753 if ((qi == &ql_info && win->w_llist_ref == NULL)
2754 || (qi != &ql_info && win->w_llist_ref == qi))
2755 return TRUE;
2756
2757 return FALSE;
2758}
2759
2760/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002761 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002762 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002763 */
2764 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002765qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002766{
2767 win_T *win;
2768
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002769 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002770 if (is_qf_win(win, qi))
2771 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002772
2773 return win;
2774}
2775
2776/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002777 * Find a quickfix buffer.
2778 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 */
2780 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002781qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002783 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002784 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
Bram Moolenaar9c102382006-05-03 21:26:49 +00002786 FOR_ALL_TAB_WINDOWS(tp, win)
2787 if (is_qf_win(win, qi))
2788 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002789
Bram Moolenaar9c102382006-05-03 21:26:49 +00002790 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791}
2792
2793/*
2794 * Find the quickfix buffer. If it exists, update the contents.
2795 */
2796 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002797qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
2799 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002800 win_T *win;
2801 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
2804 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002805 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (buf != NULL)
2807 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02002808 linenr_T old_line_count = buf->b_ml.ml_line_count;
2809
2810 if (old_last == NULL)
2811 /* set curwin/curbuf to buf and save a few things */
2812 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002814 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002815 {
2816 curwin_save = curwin;
2817 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002818 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002819 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002820 }
2821
Bram Moolenaar864293a2016-06-02 13:40:04 +02002822 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01002823
Bram Moolenaar864293a2016-06-02 13:40:04 +02002824 if (old_last == NULL)
2825 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002826 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002827
2828 /* restore curwin/curbuf and a few other things */
2829 aucmd_restbuf(&aco);
2830 }
2831
2832 /* Only redraw when added lines are visible. This avoids flickering
2833 * when the added lines are not visible. */
2834 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
2835 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837}
2838
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002839/*
2840 * Set "w:quickfix_title" if "qi" has a title.
2841 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002843qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002844{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002845 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2846 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002847 qi->qf_lists[qi->qf_curlist].qf_title);
2848}
2849
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850/*
2851 * Fill current buffer with quickfix errors, replacing any previous contents.
2852 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02002853 * If "old_last" is not NULL append the items after this one.
2854 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
2855 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 */
2857 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002858qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002860 linenr_T lnum;
2861 qfline_T *qfp;
2862 buf_T *errbuf;
2863 int len;
2864 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
Bram Moolenaar864293a2016-06-02 13:40:04 +02002866 if (old_last == NULL)
2867 {
2868 if (buf != curbuf)
2869 {
2870 EMSG2(_(e_intern2), "qf_fill_buffer()");
2871 return;
2872 }
2873
2874 /* delete all existing lines */
2875 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2876 (void)ml_delete((linenr_T)1, FALSE);
2877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
2879 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002880 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
2882 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002883 if (old_last == NULL)
2884 {
2885 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2886 lnum = 0;
2887 }
2888 else
2889 {
2890 qfp = old_last->qf_next;
2891 lnum = buf->b_ml.ml_line_count;
2892 }
2893 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
2895 if (qfp->qf_fnum != 0
2896 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2897 && errbuf->b_fname != NULL)
2898 {
2899 if (qfp->qf_type == 1) /* :helpgrep */
2900 STRCPY(IObuff, gettail(errbuf->b_fname));
2901 else
2902 STRCPY(IObuff, errbuf->b_fname);
2903 len = (int)STRLEN(IObuff);
2904 }
2905 else
2906 len = 0;
2907 IObuff[len++] = '|';
2908
2909 if (qfp->qf_lnum > 0)
2910 {
2911 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2912 len += (int)STRLEN(IObuff + len);
2913
2914 if (qfp->qf_col > 0)
2915 {
2916 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2917 len += (int)STRLEN(IObuff + len);
2918 }
2919
2920 sprintf((char *)IObuff + len, "%s",
2921 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2922 len += (int)STRLEN(IObuff + len);
2923 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002924 else if (qfp->qf_pattern != NULL)
2925 {
2926 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2927 len += (int)STRLEN(IObuff + len);
2928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 IObuff[len++] = '|';
2930 IObuff[len++] = ' ';
2931
2932 /* Remove newlines and leading whitespace from the text.
2933 * For an unrecognized line keep the indent, the compiler may
2934 * mark a word with ^^^^. */
2935 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2936 IObuff + len, IOSIZE - len);
2937
Bram Moolenaar864293a2016-06-02 13:40:04 +02002938 if (ml_append_buf(buf, lnum, IObuff,
2939 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02002941 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002943 if (qfp == NULL)
2944 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02002946
2947 if (old_last == NULL)
2948 /* Delete the empty line which is now at the end */
2949 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
2951
2952 /* correct cursor position */
2953 check_lnums(TRUE);
2954
Bram Moolenaar864293a2016-06-02 13:40:04 +02002955 if (old_last == NULL)
2956 {
2957 /* Set the 'filetype' to "qf" each time after filling the buffer.
2958 * This resembles reading a file into a buffer, it's more logical when
2959 * using autocommands. */
2960 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2961 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02002964 keep_filetype = TRUE; /* don't detect 'filetype' */
2965 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002967 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002969 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02002971 /* make sure it will be redrawn */
2972 redraw_curbuf_later(NOT_VALID);
2973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
2975 /* Restore KeyTyped, setting 'filetype' may reset it. */
2976 KeyTyped = old_KeyTyped;
2977}
2978
2979#endif /* FEAT_WINDOWS */
2980
2981/*
2982 * Return TRUE if "buf" is the quickfix buffer.
2983 */
2984 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002985bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002987 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988}
2989
2990/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002991 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2992 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 */
2994 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002995bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002997 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2998 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999}
3000
3001/*
3002 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3003 */
3004 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003005bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003007 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008}
3009
3010 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003011bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 if (bt_dontwrite(buf))
3014 {
3015 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3016 return TRUE;
3017 }
3018 return FALSE;
3019}
3020
3021/*
3022 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3023 * and 'bufhidden'.
3024 */
3025 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003026buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3029 switch (buf->b_p_bh[0])
3030 {
3031 case 'u': /* "unload" */
3032 case 'w': /* "wipe" */
3033 case 'd': return FALSE; /* "delete" */
3034 case 'h': return TRUE; /* "hide" */
3035 }
3036 return (p_hid || cmdmod.hide);
3037}
3038
3039/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003040 * Return TRUE when using ":vimgrep" for ":grep".
3041 */
3042 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003043grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003044{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003045 return ((cmdidx == CMD_grep
3046 || cmdidx == CMD_lgrep
3047 || cmdidx == CMD_grepadd
3048 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003049 && STRCMP("internal",
3050 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3051}
3052
3053/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003054 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 */
3056 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003057ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003059 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 char_u *cmd;
3061 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003062 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003063 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003064 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003065#ifdef FEAT_AUTOCMD
3066 char_u *au_name = NULL;
3067
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003068 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3069 if (grep_internal(eap->cmdidx))
3070 {
3071 ex_vimgrep(eap);
3072 return;
3073 }
3074
Bram Moolenaar7c626922005-02-07 22:01:03 +00003075 switch (eap->cmdidx)
3076 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003077 case CMD_make: au_name = (char_u *)"make"; break;
3078 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3079 case CMD_grep: au_name = (char_u *)"grep"; break;
3080 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3081 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3082 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003083 default: break;
3084 }
3085 if (au_name != NULL)
3086 {
3087 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3088 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003089# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003090 if (did_throw || force_abort)
3091 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003092# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003093 }
3094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
Bram Moolenaara6557602006-02-04 22:43:20 +00003096 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3097 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003098 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003099
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003101 fname = get_mef_name();
3102 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003104 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
3106 /*
3107 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3108 */
3109 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3110 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003111 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 cmd = alloc(len);
3113 if (cmd == NULL)
3114 return;
3115 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3116 (char *)p_shq);
3117 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003118 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 /*
3120 * Output a newline if there's something else than the :make command that
3121 * was typed (in which case the cursor is in column 0).
3122 */
3123 if (msg_col == 0)
3124 msg_didout = FALSE;
3125 msg_start();
3126 MSG_PUTS(":!");
3127 msg_outtrans(cmd); /* show what we are doing */
3128
3129 /* let the shell know if we are redirecting output or not */
3130 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3131
3132#ifdef AMIGA
3133 out_flush();
3134 /* read window status report and redraw before message */
3135 (void)char_avail();
3136#endif
3137
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003138 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003139 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3140 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003141 && eap->cmdidx != CMD_lgrepadd),
3142 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003143 if (wp != NULL)
3144 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003145#ifdef FEAT_AUTOCMD
3146 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003147 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003148 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3149 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003150 if (qi->qf_curlist < qi->qf_listcount)
3151 res = qi->qf_lists[qi->qf_curlist].qf_count;
3152 else
3153 res = 0;
3154 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003155#endif
3156 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003157 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaar7c626922005-02-07 22:01:03 +00003159 mch_remove(fname);
3160 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 vim_free(cmd);
3162}
3163
3164/*
3165 * Return the name for the errorfile, in allocated memory.
3166 * Find a new unique name when 'makeef' contains "##".
3167 * Returns NULL for error.
3168 */
3169 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003170get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 char_u *p;
3173 char_u *name;
3174 static int start = -1;
3175 static int off = 0;
3176#ifdef HAVE_LSTAT
3177 struct stat sb;
3178#endif
3179
3180 if (*p_mef == NUL)
3181 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003182 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 if (name == NULL)
3184 EMSG(_(e_notmp));
3185 return name;
3186 }
3187
3188 for (p = p_mef; *p; ++p)
3189 if (p[0] == '#' && p[1] == '#')
3190 break;
3191
3192 if (*p == NUL)
3193 return vim_strsave(p_mef);
3194
3195 /* Keep trying until the name doesn't exist yet. */
3196 for (;;)
3197 {
3198 if (start == -1)
3199 start = mch_get_pid();
3200 else
3201 off += 19;
3202
3203 name = alloc((unsigned)STRLEN(p_mef) + 30);
3204 if (name == NULL)
3205 break;
3206 STRCPY(name, p_mef);
3207 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3208 STRCAT(name, p + 2);
3209 if (mch_getperm(name) < 0
3210#ifdef HAVE_LSTAT
3211 /* Don't accept a symbolic link, its a security risk. */
3212 && mch_lstat((char *)name, &sb) < 0
3213#endif
3214 )
3215 break;
3216 vim_free(name);
3217 }
3218 return name;
3219}
3220
3221/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003222 * Returns the number of valid entries in the current quickfix/location list.
3223 */
3224 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003225qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003226{
3227 qf_info_T *qi = &ql_info;
3228 qfline_T *qfp;
3229 int i, sz = 0;
3230 int prev_fnum = 0;
3231
3232 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3233 {
3234 /* Location list */
3235 qi = GET_LOC_LIST(curwin);
3236 if (qi == NULL)
3237 return 0;
3238 }
3239
3240 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003241 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003242 ++i, qfp = qfp->qf_next)
3243 {
3244 if (qfp->qf_valid)
3245 {
3246 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3247 sz++; /* Count all valid entries */
3248 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3249 {
3250 /* Count the number of files */
3251 sz++;
3252 prev_fnum = qfp->qf_fnum;
3253 }
3254 }
3255 }
3256
3257 return sz;
3258}
3259
3260/*
3261 * Returns the current index of the quickfix/location list.
3262 * Returns 0 if there is an error.
3263 */
3264 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003265qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003266{
3267 qf_info_T *qi = &ql_info;
3268
3269 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3270 {
3271 /* Location list */
3272 qi = GET_LOC_LIST(curwin);
3273 if (qi == NULL)
3274 return 0;
3275 }
3276
3277 return qi->qf_lists[qi->qf_curlist].qf_index;
3278}
3279
3280/*
3281 * Returns the current index in the quickfix/location list (counting only valid
3282 * entries). If no valid entries are in the list, then returns 1.
3283 */
3284 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003285qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003286{
3287 qf_info_T *qi = &ql_info;
3288 qf_list_T *qfl;
3289 qfline_T *qfp;
3290 int i, eidx = 0;
3291 int prev_fnum = 0;
3292
3293 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3294 {
3295 /* Location list */
3296 qi = GET_LOC_LIST(curwin);
3297 if (qi == NULL)
3298 return 1;
3299 }
3300
3301 qfl = &qi->qf_lists[qi->qf_curlist];
3302 qfp = qfl->qf_start;
3303
3304 /* check if the list has valid errors */
3305 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3306 return 1;
3307
3308 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3309 {
3310 if (qfp->qf_valid)
3311 {
3312 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3313 {
3314 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3315 {
3316 /* Count the number of files */
3317 eidx++;
3318 prev_fnum = qfp->qf_fnum;
3319 }
3320 }
3321 else
3322 eidx++;
3323 }
3324 }
3325
3326 return eidx ? eidx : 1;
3327}
3328
3329/*
3330 * Get the 'n'th valid error entry in the quickfix or location list.
3331 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3332 * For :cdo and :ldo returns the 'n'th valid error entry.
3333 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3334 */
3335 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003336qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003337{
3338 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3339 qfline_T *qfp = qfl->qf_start;
3340 int i, eidx;
3341 int prev_fnum = 0;
3342
3343 /* check if the list has valid errors */
3344 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3345 return 1;
3346
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003347 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003348 i++, qfp = qfp->qf_next)
3349 {
3350 if (qfp->qf_valid)
3351 {
3352 if (fdo)
3353 {
3354 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3355 {
3356 /* Count the number of files */
3357 eidx++;
3358 prev_fnum = qfp->qf_fnum;
3359 }
3360 }
3361 else
3362 eidx++;
3363 }
3364
3365 if (eidx == n)
3366 break;
3367 }
3368
3369 if (i <= qfl->qf_count)
3370 return i;
3371 else
3372 return 1;
3373}
3374
3375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003377 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003378 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 */
3380 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003381ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003383 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003384 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003385
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003386 if (eap->cmdidx == CMD_ll
3387 || eap->cmdidx == CMD_lrewind
3388 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003389 || eap->cmdidx == CMD_llast
3390 || eap->cmdidx == CMD_ldo
3391 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003392 {
3393 qi = GET_LOC_LIST(curwin);
3394 if (qi == NULL)
3395 {
3396 EMSG(_(e_loclist));
3397 return;
3398 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003399 }
3400
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003401 if (eap->addr_count > 0)
3402 errornr = (int)eap->line2;
3403 else
3404 {
3405 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3406 errornr = 0;
3407 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3408 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3409 errornr = 1;
3410 else
3411 errornr = 32767;
3412 }
3413
3414 /* For cdo and ldo commands, jump to the nth valid error.
3415 * For cfdo and lfdo commands, jump to the nth valid file entry.
3416 */
3417 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3418 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3419 errornr = qf_get_nth_valid_entry(qi,
3420 eap->addr_count > 0 ? (int)eap->line1 : 1,
3421 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3422
3423 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424}
3425
3426/*
3427 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003428 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003429 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
3431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003432ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003434 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003435 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003436
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003437 if (eap->cmdidx == CMD_lnext
3438 || eap->cmdidx == CMD_lNext
3439 || eap->cmdidx == CMD_lprevious
3440 || eap->cmdidx == CMD_lnfile
3441 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003442 || eap->cmdidx == CMD_lpfile
3443 || eap->cmdidx == CMD_ldo
3444 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003445 {
3446 qi = GET_LOC_LIST(curwin);
3447 if (qi == NULL)
3448 {
3449 EMSG(_(e_loclist));
3450 return;
3451 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003452 }
3453
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003454 if (eap->addr_count > 0 &&
3455 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3456 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3457 errornr = (int)eap->line2;
3458 else
3459 errornr = 1;
3460
3461 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3462 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003464 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3465 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003467 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3468 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 ? BACKWARD_FILE
3470 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003471 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472}
3473
3474/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003475 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003476 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 */
3478 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003479ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003481 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003482 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003483#ifdef FEAT_AUTOCMD
3484 char_u *au_name = NULL;
3485#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003486
3487 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003488 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003489 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003490
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003491#ifdef FEAT_AUTOCMD
3492 switch (eap->cmdidx)
3493 {
3494 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3495 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3496 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3497 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3498 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3499 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3500 default: break;
3501 }
3502 if (au_name != NULL)
3503 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3504#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003505#ifdef FEAT_BROWSE
3506 if (cmdmod.browse)
3507 {
3508 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3509 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3510 if (browse_file == NULL)
3511 return;
3512 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3513 vim_free(browse_file);
3514 }
3515 else
3516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003518 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003519
3520 /*
3521 * This function is used by the :cfile, :cgetfile and :caddfile
3522 * commands.
3523 * :cfile always creates a new quickfix list and jumps to the
3524 * first error.
3525 * :cgetfile creates a new quickfix list but doesn't jump to the
3526 * first error.
3527 * :caddfile adds to an existing quickfix list. If there is no
3528 * quickfix list then a new list is created.
3529 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003530 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003531 && eap->cmdidx != CMD_laddfile),
3532 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003533 && (eap->cmdidx == CMD_cfile
3534 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003535 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003536#ifdef FEAT_AUTOCMD
3537 if (au_name != NULL)
3538 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3539#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003540 if (wp != NULL)
3541 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003542 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003543 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003544
3545 else
3546 {
3547#ifdef FEAT_AUTOCMD
3548 if (au_name != NULL)
3549 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3550#endif
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552}
3553
3554/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003555 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003556 * ":vimgrepadd {pattern} file(s)"
3557 * ":lvimgrep {pattern} file(s)"
3558 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003559 */
3560 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003561ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003562{
Bram Moolenaar81695252004-12-29 20:58:21 +00003563 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003564 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003565 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003566 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003567 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003568 char_u *s;
3569 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003570 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003571 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003572#ifdef FEAT_AUTOCMD
3573 qfline_T *cur_qf_start;
3574#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003575 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003576 buf_T *buf;
3577 int duplicate_name = FALSE;
3578 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003579 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003580 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003581 buf_T *first_match_buf = NULL;
3582 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003583 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003584#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3585 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003586#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003587 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003588 int flags = 0;
3589 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003590 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003591 char_u *dirname_start = NULL;
3592 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003593 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003594#ifdef FEAT_AUTOCMD
3595 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003596
3597 switch (eap->cmdidx)
3598 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003599 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3600 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3601 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003602 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003603 case CMD_grep: au_name = (char_u *)"grep"; break;
3604 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3605 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3606 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003607 default: break;
3608 }
3609 if (au_name != NULL)
3610 {
3611 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3612 curbuf->b_fname, TRUE, curbuf);
3613 if (did_throw || force_abort)
3614 return;
3615 }
3616#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003617
Bram Moolenaar754b5602006-02-09 23:53:20 +00003618 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003619 || eap->cmdidx == CMD_lvimgrep
3620 || eap->cmdidx == CMD_lgrepadd
3621 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003622 {
3623 qi = ll_get_or_alloc_list(curwin);
3624 if (qi == NULL)
3625 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003626 }
3627
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003628 if (eap->addr_count > 0)
3629 tomatch = eap->line2;
3630 else
3631 tomatch = MAXLNUM;
3632
Bram Moolenaar81695252004-12-29 20:58:21 +00003633 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003634 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003635 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003636 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003637 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003638 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003639 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003640 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003641 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003642
3643 if (s != NULL && *s == NUL)
3644 {
3645 /* Pattern is empty, use last search pattern. */
3646 if (last_search_pat() == NULL)
3647 {
3648 EMSG(_(e_noprevre));
3649 goto theend;
3650 }
3651 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3652 }
3653 else
3654 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3655
Bram Moolenaar86b68352004-12-27 21:59:20 +00003656 if (regmatch.regprog == NULL)
3657 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003658 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003659 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003660
3661 p = skipwhite(p);
3662 if (*p == NUL)
3663 {
3664 EMSG(_("E683: File name missing or invalid pattern"));
3665 goto theend;
3666 }
3667
Bram Moolenaar754b5602006-02-09 23:53:20 +00003668 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003669 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003670 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003671 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003672 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003673
3674 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003675 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003676 goto theend;
3677 if (fcount == 0)
3678 {
3679 EMSG(_(e_nomatch));
3680 goto theend;
3681 }
3682
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003683 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3684 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003685 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003686 {
3687 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003688 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003689 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003690
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003691 /* Remember the current directory, because a BufRead autocommand that does
3692 * ":lcd %:p:h" changes the meaning of short path names. */
3693 mch_dirname(dirname_start, MAXPATHL);
3694
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003695#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003696 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003697 * changing the current quickfix list. */
3698 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3699#endif
3700
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003701 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003702 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003703 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003704 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003705 if (time(NULL) > seconds)
3706 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003707 /* Display the file name every second or so, show the user we are
3708 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003709 seconds = time(NULL);
3710 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003711 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003712 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003713 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003714 else
3715 {
3716 msg_outtrans(p);
3717 vim_free(p);
3718 }
3719 msg_clr_eos();
3720 msg_didout = FALSE; /* overwrite this message */
3721 msg_nowait = TRUE; /* don't wait for this message */
3722 msg_col = 0;
3723 out_flush();
3724 }
3725
Bram Moolenaar81695252004-12-29 20:58:21 +00003726 buf = buflist_findname_exp(fnames[fi]);
3727 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3728 {
3729 /* Remember that a buffer with this name already exists. */
3730 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003731 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003732 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003733
3734#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3735 /* Don't do Filetype autocommands to avoid loading syntax and
3736 * indent scripts, a great speed improvement. */
3737 save_ei = au_event_disable(",Filetype");
3738#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003739 /* Don't use modelines here, it's useless. */
3740 save_mls = p_mls;
3741 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003742
3743 /* Load file into a buffer, so that 'fileencoding' is detected,
3744 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003745 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003746
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003747 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003748#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3749 au_event_restore(save_ei);
3750#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003751 }
3752 else
3753 /* Use existing, loaded buffer. */
3754 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003755
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003756#ifdef FEAT_AUTOCMD
3757 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3758 {
3759 int idx;
3760
3761 /* Autocommands changed the quickfix list. Find the one we were
3762 * using and restore it. */
3763 for (idx = 0; idx < LISTCOUNT; ++idx)
3764 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3765 {
3766 qi->qf_curlist = idx;
3767 break;
3768 }
3769 if (idx == LISTCOUNT)
3770 {
3771 /* List cannot be found, create a new one. */
3772 qf_new_list(qi, *eap->cmdlinep);
3773 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3774 }
3775 }
3776#endif
3777
Bram Moolenaar81695252004-12-29 20:58:21 +00003778 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003779 {
3780 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003781 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003782 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003783 else
3784 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003785 /* Try for a match in all lines of the buffer.
3786 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003787 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003788 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3789 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003790 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003791 col = 0;
3792 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003793 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003794 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003795 ;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003796 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003797 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003798 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003799 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003800 ml_get_buf(buf,
3801 regmatch.startpos[0].lnum + lnum, FALSE),
3802 regmatch.startpos[0].lnum + lnum,
3803 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003804 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003805 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003806 0, /* nr */
3807 0, /* type */
3808 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003809 ) == FAIL)
3810 {
3811 got_int = TRUE;
3812 break;
3813 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003814 found_match = TRUE;
3815 if (--tomatch == 0)
3816 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003817 if ((flags & VGR_GLOBAL) == 0
3818 || regmatch.endpos[0].lnum > 0)
3819 break;
3820 col = regmatch.endpos[0].col
3821 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003822 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003823 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003824 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003825 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003826 if (got_int)
3827 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003828 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003829#ifdef FEAT_AUTOCMD
3830 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3831#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003832
3833 if (using_dummy)
3834 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003835 if (found_match && first_match_buf == NULL)
3836 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003837 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003838 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003839 /* Never keep a dummy buffer if there is another buffer
3840 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003841 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003842 buf = NULL;
3843 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003844 else if (!cmdmod.hide
3845 || buf->b_p_bh[0] == 'u' /* "unload" */
3846 || buf->b_p_bh[0] == 'w' /* "wipe" */
3847 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003848 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003849 /* When no match was found we don't need to remember the
3850 * buffer, wipe it out. If there was a match and it
3851 * wasn't the first one or we won't jump there: only
3852 * unload the buffer.
3853 * Ignore 'hidden' here, because it may lead to having too
3854 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003855 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003856 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003857 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003858 buf = NULL;
3859 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003860 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003861 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003862 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003863 buf = NULL;
3864 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003865 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003866
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003867 if (buf != NULL)
3868 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003869 /* If the buffer is still loaded we need to use the
3870 * directory we jumped to below. */
3871 if (buf == first_match_buf
3872 && target_dir == NULL
3873 && STRCMP(dirname_start, dirname_now) != 0)
3874 target_dir = vim_strsave(dirname_now);
3875
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003876 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003877 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003878 * need to be done (again). But not the window-local
3879 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003880 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003881#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003882 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3883 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003884#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003885 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003886 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003887 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003888 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003889 }
3890 }
3891
3892 FreeWild(fcount, fnames);
3893
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003894 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3895 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3896 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003897
3898#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02003899 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003900#endif
3901
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003902#ifdef FEAT_AUTOCMD
3903 if (au_name != NULL)
3904 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3905 curbuf->b_fname, TRUE, curbuf);
3906#endif
3907
Bram Moolenaar86b68352004-12-27 21:59:20 +00003908 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003909 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003910 {
3911 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003912 {
3913 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003914 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003915 if (buf != curbuf)
3916 /* If we jumped to another buffer redrawing will already be
3917 * taken care of. */
3918 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003919
3920 /* Jump to the directory used after loading the buffer. */
3921 if (curbuf == first_match_buf && target_dir != NULL)
3922 {
3923 exarg_T ea;
3924
3925 ea.arg = target_dir;
3926 ea.cmdidx = CMD_lcd;
3927 ex_cd(&ea);
3928 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003929 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003930 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003931 else
3932 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003933
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003934 /* If we loaded a dummy buffer into the current window, the autocommands
3935 * may have messed up things, need to redraw and recompute folds. */
3936 if (redraw_for_dummy)
3937 {
3938#ifdef FEAT_FOLDING
3939 foldUpdateAll(curwin);
3940#else
3941 redraw_later(NOT_VALID);
3942#endif
3943 }
3944
Bram Moolenaar86b68352004-12-27 21:59:20 +00003945theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01003946 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003947 vim_free(dirname_now);
3948 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003949 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003950 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003951}
3952
3953/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003954 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003955 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003956 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3957 * If "s" is not NULL terminate the pattern with a NUL.
3958 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003959 */
3960 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003961skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003962{
3963 int c;
3964
3965 if (vim_isIDc(*p))
3966 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003967 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003968 if (s != NULL)
3969 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003970 p = skiptowhite(p);
3971 if (s != NULL && *p != NUL)
3972 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003973 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003974 else
3975 {
3976 /* ":vimgrep /pattern/[g][j] fname" */
3977 if (s != NULL)
3978 *s = p + 1;
3979 c = *p;
3980 p = skip_regexp(p + 1, c, TRUE, NULL);
3981 if (*p != c)
3982 return NULL;
3983
3984 /* Truncate the pattern. */
3985 if (s != NULL)
3986 *p = NUL;
3987 ++p;
3988
3989 /* Find the flags */
3990 while (*p == 'g' || *p == 'j')
3991 {
3992 if (flags != NULL)
3993 {
3994 if (*p == 'g')
3995 *flags |= VGR_GLOBAL;
3996 else
3997 *flags |= VGR_NOJUMP;
3998 }
3999 ++p;
4000 }
4001 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004002 return p;
4003}
4004
4005/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004006 * Restore current working directory to "dirname_start" if they differ, taking
4007 * into account whether it is set locally or globally.
4008 */
4009 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004010restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004011{
4012 char_u *dirname_now = alloc(MAXPATHL);
4013
4014 if (NULL != dirname_now)
4015 {
4016 mch_dirname(dirname_now, MAXPATHL);
4017 if (STRCMP(dirname_start, dirname_now) != 0)
4018 {
4019 /* If the directory has changed, change it back by building up an
4020 * appropriate ex command and executing it. */
4021 exarg_T ea;
4022
4023 ea.arg = dirname_start;
4024 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4025 ex_cd(&ea);
4026 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004027 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004028 }
4029}
4030
4031/*
4032 * Load file "fname" into a dummy buffer and return the buffer pointer,
4033 * placing the directory resulting from the buffer load into the
4034 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4035 * prior to calling this function. Restores directory to "dirname_start" prior
4036 * to returning, if autocmds or the 'autochdir' option have changed it.
4037 *
4038 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4039 * or wipe_dummy_buffer() later!
4040 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004041 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004042 */
4043 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004044load_dummy_buffer(
4045 char_u *fname,
4046 char_u *dirname_start, /* in: old directory */
4047 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004048{
4049 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004050 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004051 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004052 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004053
4054 /* Allocate a buffer without putting it in the buffer list. */
4055 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4056 if (newbuf == NULL)
4057 return NULL;
4058
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004059 /* Init the options. */
4060 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4061
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004062 /* need to open the memfile before putting the buffer in a window */
4063 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004064 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004065 /* set curwin/curbuf to buf and save a few things */
4066 aucmd_prepbuf(&aco, newbuf);
4067
4068 /* Need to set the filename for autocommands. */
4069 (void)setfname(curbuf, fname, NULL, FALSE);
4070
Bram Moolenaar81695252004-12-29 20:58:21 +00004071 /* Create swap file now to avoid the ATTENTION message. */
4072 check_need_swap(TRUE);
4073
4074 /* Remove the "dummy" flag, otherwise autocommands may not
4075 * work. */
4076 curbuf->b_flags &= ~BF_DUMMY;
4077
4078 if (readfile(fname, NULL,
4079 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4080 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004081 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004082 && !(curbuf->b_flags & BF_NEW))
4083 {
4084 failed = FALSE;
4085 if (curbuf != newbuf)
4086 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004087 /* Bloody autocommands changed the buffer! Can happen when
4088 * using netrw and editing a remote file. Use the current
4089 * buffer instead, delete the dummy one after restoring the
4090 * window stuff. */
4091 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004092 newbuf = curbuf;
4093 }
4094 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004095
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004096 /* restore curwin/curbuf and a few other things */
4097 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004098 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
4099 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004100 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004101
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004102 /*
4103 * When autocommands/'autochdir' option changed directory: go back.
4104 * Let the caller know what the resulting dir was first, in case it is
4105 * important.
4106 */
4107 mch_dirname(resulting_dir, MAXPATHL);
4108 restore_start_dir(dirname_start);
4109
Bram Moolenaar81695252004-12-29 20:58:21 +00004110 if (!buf_valid(newbuf))
4111 return NULL;
4112 if (failed)
4113 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004114 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004115 return NULL;
4116 }
4117 return newbuf;
4118}
4119
4120/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004121 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4122 * directory to "dirname_start" prior to returning, if autocmds or the
4123 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004124 */
4125 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004126wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004127{
4128 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004129 {
4130#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4131 cleanup_T cs;
4132
4133 /* Reset the error/interrupt/exception state here so that aborting()
4134 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4135 * work when got_int is set. */
4136 enter_cleanup(&cs);
4137#endif
4138
Bram Moolenaar81695252004-12-29 20:58:21 +00004139 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004140
4141#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4142 /* Restore the error/interrupt/exception state if not discarded by a
4143 * new aborting error, interrupt, or uncaught exception. */
4144 leave_cleanup(&cs);
4145#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004146 /* When autocommands/'autochdir' option changed directory: go back. */
4147 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004148 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004149}
4150
4151/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004152 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4153 * directory to "dirname_start" prior to returning, if autocmds or the
4154 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004155 */
4156 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004157unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004158{
4159 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004160 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004161 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004162
4163 /* When autocommands/'autochdir' option changed directory: go back. */
4164 restore_start_dir(dirname_start);
4165 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004166}
4167
Bram Moolenaar05159a02005-02-26 23:04:13 +00004168#if defined(FEAT_EVAL) || defined(PROTO)
4169/*
4170 * Add each quickfix error to list "list" as a dictionary.
4171 */
4172 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004173get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004174{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004175 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004176 dict_T *dict;
4177 char_u buf[2];
4178 qfline_T *qfp;
4179 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004180 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004181
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004182 if (wp != NULL)
4183 {
4184 qi = GET_LOC_LIST(wp);
4185 if (qi == NULL)
4186 return FAIL;
4187 }
4188
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004189 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004190 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004191 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004192
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004193 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4194 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004195 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004196 /* Handle entries with a non-existing buffer number. */
4197 bufnum = qfp->qf_fnum;
4198 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4199 bufnum = 0;
4200
Bram Moolenaar05159a02005-02-26 23:04:13 +00004201 if ((dict = dict_alloc()) == NULL)
4202 return FAIL;
4203 if (list_append_dict(list, dict) == FAIL)
4204 return FAIL;
4205
4206 buf[0] = qfp->qf_type;
4207 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004208 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004209 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4210 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4211 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4212 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004213 || dict_add_nr_str(dict, "pattern", 0L,
4214 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4215 || dict_add_nr_str(dict, "text", 0L,
4216 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004217 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4218 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4219 return FAIL;
4220
4221 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004222 if (qfp == NULL)
4223 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004224 }
4225 return OK;
4226}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004227
4228/*
4229 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004230 * of dictionaries. "title" will be copied to w:quickfix_title.
4231 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004232 */
4233 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004234set_errorlist(
4235 win_T *wp,
4236 list_T *list,
4237 int action,
4238 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004239{
4240 listitem_T *li;
4241 dict_T *d;
4242 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004243 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004244 long lnum;
4245 int col, nr;
4246 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004247#ifdef FEAT_WINDOWS
4248 qfline_T *old_last = NULL;
4249#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004250 int valid, status;
4251 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004252 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004253 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004254
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004255 if (wp != NULL)
4256 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004257 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004258 if (qi == NULL)
4259 return FAIL;
4260 }
4261
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004262 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004263 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004264 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004265#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004266 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4267 /* Adding to existing list, use last entry. */
4268 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004269#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004270 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004271 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004272 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004273 qf_store_title(qi, title);
4274 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004275
4276 for (li = list->lv_first; li != NULL; li = li->li_next)
4277 {
4278 if (li->li_tv.v_type != VAR_DICT)
4279 continue; /* Skip non-dict items */
4280
4281 d = li->li_tv.vval.v_dict;
4282 if (d == NULL)
4283 continue;
4284
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004285 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004286 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004287 lnum = get_dict_number(d, (char_u *)"lnum");
4288 col = get_dict_number(d, (char_u *)"col");
4289 vcol = get_dict_number(d, (char_u *)"vcol");
4290 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004291 type = get_dict_string(d, (char_u *)"type", TRUE);
4292 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4293 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004294 if (text == NULL)
4295 text = vim_strsave((char_u *)"");
4296
4297 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004298 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004299 valid = FALSE;
4300
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004301 /* Mark entries with non-existing buffer number as not valid. Give the
4302 * error message only once. */
4303 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4304 {
4305 if (!did_bufnr_emsg)
4306 {
4307 did_bufnr_emsg = TRUE;
4308 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4309 }
4310 valid = FALSE;
4311 bufnum = 0;
4312 }
4313
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004314 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004315 NULL, /* dir */
4316 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004317 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004318 text,
4319 lnum,
4320 col,
4321 vcol, /* vis_col */
4322 pattern, /* search pattern */
4323 nr,
4324 type == NULL ? NUL : *type,
4325 valid);
4326
4327 vim_free(filename);
4328 vim_free(pattern);
4329 vim_free(text);
4330 vim_free(type);
4331
4332 if (status == FAIL)
4333 {
4334 retval = FAIL;
4335 break;
4336 }
4337 }
4338
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004339 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004340 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004341 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4342 else
4343 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004344 if (action != 'a') {
4345 qi->qf_lists[qi->qf_curlist].qf_ptr =
4346 qi->qf_lists[qi->qf_curlist].qf_start;
4347 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4348 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4349 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004350
4351#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004352 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004353 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004354#endif
4355
4356 return retval;
4357}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004358#endif
4359
Bram Moolenaar81695252004-12-29 20:58:21 +00004360/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004361 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004362 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004363 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004364 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004365 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004366 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004367 */
4368 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004369ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004370{
4371 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004372 qf_info_T *qi = &ql_info;
4373
Bram Moolenaardb552d602006-03-23 22:59:57 +00004374 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4375 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004376 {
4377 qi = ll_get_or_alloc_list(curwin);
4378 if (qi == NULL)
4379 return;
4380 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004381
4382 if (*eap->arg == NUL)
4383 buf = curbuf;
4384 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4385 buf = buflist_findnr(atoi((char *)eap->arg));
4386 if (buf == NULL)
4387 EMSG(_(e_invarg));
4388 else if (buf->b_ml.ml_mfp == NULL)
4389 EMSG(_("E681: Buffer is not loaded"));
4390 else
4391 {
4392 if (eap->addr_count == 0)
4393 {
4394 eap->line1 = 1;
4395 eap->line2 = buf->b_ml.ml_line_count;
4396 }
4397 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4398 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4399 EMSG(_(e_invrange));
4400 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004401 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004402 char_u *qf_title = *eap->cmdlinep;
4403
4404 if (buf->b_sfname)
4405 {
4406 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4407 (char *)qf_title, (char *)buf->b_sfname);
4408 qf_title = IObuff;
4409 }
4410
Bram Moolenaardb552d602006-03-23 22:59:57 +00004411 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4412 (eap->cmdidx != CMD_caddbuffer
4413 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004414 eap->line1, eap->line2,
4415 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004416 && (eap->cmdidx == CMD_cbuffer
4417 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004418 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4419 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004420 }
4421}
4422
Bram Moolenaar1e015462005-09-25 22:16:38 +00004423#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004424/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004425 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4426 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004427 */
4428 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004429ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004430{
4431 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004432 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004433
Bram Moolenaardb552d602006-03-23 22:59:57 +00004434 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4435 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004436 {
4437 qi = ll_get_or_alloc_list(curwin);
4438 if (qi == NULL)
4439 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004440 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004441
Bram Moolenaar4770d092006-01-12 23:22:24 +00004442 /* Evaluate the expression. When the result is a string or a list we can
4443 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004444 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004445 if (tv != NULL)
4446 {
4447 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4448 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4449 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004450 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004451 (eap->cmdidx != CMD_caddexpr
4452 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004453 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004454 && (eap->cmdidx == CMD_cexpr
4455 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004456 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004457 }
4458 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004459 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004460 free_tv(tv);
4461 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004462}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004463#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004464
4465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 * ":helpgrep {pattern}"
4467 */
4468 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004469ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470{
4471 regmatch_T regmatch;
4472 char_u *save_cpo;
4473 char_u *p;
4474 int fcount;
4475 char_u **fnames;
4476 FILE *fd;
4477 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004479#ifdef FEAT_MULTI_LANG
4480 char_u *lang;
4481#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004482 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004483 int new_qi = FALSE;
4484 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004485#ifdef FEAT_AUTOCMD
4486 char_u *au_name = NULL;
4487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004489#ifdef FEAT_MULTI_LANG
4490 /* Check for a specified language */
4491 lang = check_help_lang(eap->arg);
4492#endif
4493
Bram Moolenaar73633f82012-01-20 13:39:07 +01004494#ifdef FEAT_AUTOCMD
4495 switch (eap->cmdidx)
4496 {
4497 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4498 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4499 default: break;
4500 }
4501 if (au_name != NULL)
4502 {
4503 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4504 curbuf->b_fname, TRUE, curbuf);
4505 if (did_throw || force_abort)
4506 return;
4507 }
4508#endif
4509
4510 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4511 save_cpo = p_cpo;
4512 p_cpo = empty_option;
4513
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004514 if (eap->cmdidx == CMD_lhelpgrep)
4515 {
4516 /* Find an existing help window */
4517 FOR_ALL_WINDOWS(wp)
4518 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4519 break;
4520
4521 if (wp == NULL) /* Help window not found */
4522 qi = NULL;
4523 else
4524 qi = wp->w_llist;
4525
4526 if (qi == NULL)
4527 {
4528 /* Allocate a new location list for help text matches */
4529 if ((qi = ll_new_list()) == NULL)
4530 return;
4531 new_qi = TRUE;
4532 }
4533 }
4534
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4536 regmatch.rm_ic = FALSE;
4537 if (regmatch.regprog != NULL)
4538 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004539#ifdef FEAT_MBYTE
4540 vimconv_T vc;
4541
4542 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4543 * differs. */
4544 vc.vc_type = CONV_NONE;
4545 if (!enc_utf8)
4546 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4547#endif
4548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004550 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 /* Go through all directories in 'runtimepath' */
4553 p = p_rtp;
4554 while (*p != NUL && !got_int)
4555 {
4556 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4557
4558 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4559 add_pathsep(NameBuff);
4560 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4561 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4562 &fnames, EW_FILE|EW_SILENT) == OK
4563 && fcount > 0)
4564 {
4565 for (fi = 0; fi < fcount && !got_int; ++fi)
4566 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004567#ifdef FEAT_MULTI_LANG
4568 /* Skip files for a different language. */
4569 if (lang != NULL
4570 && STRNICMP(lang, fnames[fi]
4571 + STRLEN(fnames[fi]) - 3, 2) != 0
4572 && !(STRNICMP(lang, "en", 2) == 0
4573 && STRNICMP("txt", fnames[fi]
4574 + STRLEN(fnames[fi]) - 3, 3) == 0))
4575 continue;
4576#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004577 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (fd != NULL)
4579 {
4580 lnum = 1;
4581 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4582 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004583 char_u *line = IObuff;
4584#ifdef FEAT_MBYTE
4585 /* Convert a line if 'encoding' is not utf-8 and
4586 * the line contains a non-ASCII character. */
4587 if (vc.vc_type != CONV_NONE
4588 && has_non_ascii(IObuff)) {
4589 line = string_convert(&vc, IObuff, NULL);
4590 if (line == NULL)
4591 line = IObuff;
4592 }
4593#endif
4594
4595 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004597 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
4599 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004600 while (l > 0 && line[l - 1] <= ' ')
4601 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004603 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 NULL, /* dir */
4605 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004606 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004607 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004609 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004610 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004611 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004612 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 0, /* nr */
4614 1, /* type */
4615 TRUE /* valid */
4616 ) == FAIL)
4617 {
4618 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004619#ifdef FEAT_MBYTE
4620 if (line != IObuff)
4621 vim_free(line);
4622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 break;
4624 }
4625 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004626#ifdef FEAT_MBYTE
4627 if (line != IObuff)
4628 vim_free(line);
4629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 ++lnum;
4631 line_breakcheck();
4632 }
4633 fclose(fd);
4634 }
4635 }
4636 FreeWild(fcount, fnames);
4637 }
4638 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004639
Bram Moolenaar473de612013-06-08 18:19:48 +02004640 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004641#ifdef FEAT_MBYTE
4642 if (vc.vc_type != CONV_NONE)
4643 convert_setup(&vc, NULL, NULL);
4644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004646 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4647 qi->qf_lists[qi->qf_curlist].qf_ptr =
4648 qi->qf_lists[qi->qf_curlist].qf_start;
4649 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
4651
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004652 if (p_cpo == empty_option)
4653 p_cpo = save_cpo;
4654 else
4655 /* Darn, some plugin changed the value. */
4656 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
4658#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004659 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660#endif
4661
Bram Moolenaar73633f82012-01-20 13:39:07 +01004662#ifdef FEAT_AUTOCMD
4663 if (au_name != NULL)
4664 {
4665 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4666 curbuf->b_fname, TRUE, curbuf);
4667 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4668 /* autocommands made "qi" invalid */
4669 return;
4670 }
4671#endif
4672
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004674 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004675 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004676 else
4677 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004678
4679 if (eap->cmdidx == CMD_lhelpgrep)
4680 {
4681 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004682 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004683 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4684 {
4685 if (new_qi)
4686 ll_free_all(&qi);
4687 }
4688 else if (curwin->w_llist == NULL)
4689 curwin->w_llist = qi;
4690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691}
4692
4693#endif /* FEAT_QUICKFIX */