blob: e424593965b679e3fcea5a55716c129efeb1cba6 [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);
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200626 len = (int)STRLEN(p_buf);
627 if (len > IOSIZE - 2)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200628 {
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
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200635 {
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200636 linebuf = IObuff;
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200637 linelen = len;
638 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200639 vim_strncpy(linebuf, p_buf, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000640 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000641 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200642 else
643 {
644 if (fgets((char *)IObuff, IOSIZE, fd) == NULL)
645 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000646
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200647 discard = FALSE;
648 linelen = (int)STRLEN(IObuff);
Bram Moolenaarb37662a2016-06-02 22:18:47 +0200649 if (linelen == IOSIZE - 1 && !(IObuff[linelen - 1] == '\n'
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200650#ifdef USE_CRNL
Bram Moolenaarb37662a2016-06-02 22:18:47 +0200651 || IObuff[linelen - 1] == '\r'
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200652#endif
653 ))
654 {
655 /*
656 * The current line exceeds IObuff, continue reading using
657 * growbuf until EOL or LINE_MAXLEN bytes is read.
658 */
659 if (growbuf == NULL)
660 {
661 growbufsiz = 2 * (IOSIZE - 1);
662 growbuf = alloc(growbufsiz);
663 if (growbuf == NULL)
664 goto qf_init_end;
665 }
666
667 /* Copy the read part of the line, excluding null-terminator */
668 memcpy(growbuf, IObuff, IOSIZE - 1);
669 growbuflen = linelen;
670
671 for (;;)
672 {
673 if (fgets((char *)growbuf + growbuflen,
674 growbufsiz - growbuflen, fd) == NULL)
675 break;
Bram Moolenaard9db8b42016-05-08 12:52:05 +0200676 linelen = (int)STRLEN(growbuf + growbuflen);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200677 growbuflen += linelen;
678 if (growbuf[growbuflen - 1] == '\n'
679#ifdef USE_CRNL
680 || growbuf[growbuflen - 1] == '\r'
681#endif
682 )
683 break;
684 if (growbufsiz == LINE_MAXLEN)
685 {
686 discard = TRUE;
687 break;
688 }
689
690 growbufsiz = 2 * growbufsiz < LINE_MAXLEN
691 ? 2 * growbufsiz : LINE_MAXLEN;
692 growbuf = vim_realloc(growbuf, 2 * growbufsiz);
693 if (growbuf == NULL)
694 goto qf_init_end;
695 }
696
697 while (discard)
698 {
699 /*
700 * The current line is longer than LINE_MAXLEN, continue
701 * reading but discard everything until EOL or EOF is
702 * reached.
703 */
704 if (fgets((char *)IObuff, IOSIZE, fd) == NULL
705 || (int)STRLEN(IObuff) < IOSIZE - 1
706 || IObuff[IOSIZE - 1] == '\n'
707#ifdef USE_CRNL
708 || IObuff[IOSIZE - 1] == '\r'
709#endif
710 )
711 break;
712 }
713
714 linebuf = growbuf;
715 linelen = growbuflen;
716 }
717 else
718 linebuf = IObuff;
719 }
720
721 if (linelen > 0 && linebuf[linelen - 1] == '\n')
722 linebuf[linelen - 1] = NUL;
723#ifdef USE_CRNL
724 if (linelen > 0 && linebuf[linelen - 1] == '\r')
725 linebuf[linelen - 1] = NUL;
Bram Moolenaar836082d2011-08-10 13:21:46 +0200726#endif
727
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200728#ifdef FEAT_MBYTE
729 remove_bom(linebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730#endif
731
Bram Moolenaar01265852006-03-20 21:50:15 +0000732 /* If there was no %> item start at the first pattern */
733 if (fmt_start == NULL)
734 fmt_ptr = fmt_first;
735 else
736 {
737 fmt_ptr = fmt_start;
738 fmt_start = NULL;
739 }
740
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 /*
742 * Try to match each part of 'errorformat' until we find a complete
743 * match or no match.
744 */
745 valid = TRUE;
746restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000747 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100749 int r;
750
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 idx = fmt_ptr->prefix;
752 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
753 continue;
754 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000755 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 if (!multiscan)
757 errmsg[0] = NUL;
758 lnum = 0;
759 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000760 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 enr = -1;
762 type = 0;
763 tail = NULL;
764
765 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200766 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100767 fmt_ptr->prog = regmatch.regprog;
768 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {
770 if ((idx == 'C' || idx == 'Z') && !multiline)
771 continue;
772 if (vim_strchr((char_u *)"EWI", idx) != NULL)
773 type = idx;
774 else
775 type = 0;
776 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000777 * Extract error message data from matched line.
778 * We check for an actual submatch, because "\[" and "\]" in
779 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 */
781 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
782 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000783 int c;
784
785 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
786 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000787
788 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000789 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000790 *regmatch.endp[i] = NUL;
791 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
792 *regmatch.endp[i] = c;
793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000795 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 continue;
797 }
798 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000799 {
800 if (regmatch.startp[i] == NULL)
801 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000805 {
806 if (regmatch.startp[i] == NULL)
807 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000811 {
812 if (regmatch.startp[i] == NULL)
813 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000817 {
818 if (regmatch.startp[i] == NULL)
819 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000821 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000822 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200823 {
824 if (linelen > errmsglen) {
825 /* linelen + null terminator */
826 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
827 goto qf_init_end;
828 errmsglen = linelen + 1;
829 }
830 vim_strncpy(errmsg, linebuf, linelen);
831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
833 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000834 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
835 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200837 if (len > errmsglen) {
838 /* len + null terminator */
839 if ((errmsg = vim_realloc(errmsg, len + 1))
840 == NULL)
841 goto qf_init_end;
842 errmsglen = len + 1;
843 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000844 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000847 {
848 if (regmatch.startp[i] == NULL)
849 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
853 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200854 char_u *match_ptr;
855
Bram Moolenaar4169da72006-06-20 18:49:32 +0000856 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
857 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200858 col = 0;
859 for (match_ptr = regmatch.startp[i];
860 match_ptr != regmatch.endp[i]; ++match_ptr)
861 {
862 ++col;
863 if (*match_ptr == TAB)
864 {
865 col += 7;
866 col -= col % 8;
867 }
868 }
869 ++col;
870 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
873 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000874 if (regmatch.startp[i] == NULL)
875 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000877 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000879 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
880 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000881 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
882 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000883 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
884 if (len > CMDBUFFSIZE - 5)
885 len = CMDBUFFSIZE - 5;
886 STRCPY(pattern, "^\\V");
887 STRNCAT(pattern, regmatch.startp[i], len);
888 pattern[len + 3] = '\\';
889 pattern[len + 4] = '$';
890 pattern[len + 5] = NUL;
891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 break;
893 }
894 }
895 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000896
Bram Moolenaar4770d092006-01-12 23:22:24 +0000897 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000899 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
901 if (idx == 'D') /* enter directory */
902 {
903 if (*namebuf == NUL)
904 {
905 EMSG(_("E379: Missing or empty directory name"));
906 goto error2;
907 }
908 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
909 goto error2;
910 }
911 else if (idx == 'X') /* leave directory */
912 directory = qf_pop_dir(&dir_stack);
913 }
914 namebuf[0] = NUL; /* no match found, remove file name */
915 lnum = 0; /* don't jump to this line */
916 valid = FALSE;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200917 if (linelen > errmsglen) {
918 /* linelen + null terminator */
919 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
920 goto qf_init_end;
921 errmsglen = linelen + 1;
922 }
923 /* copy whole line to error message */
924 vim_strncpy(errmsg, linebuf, linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000925 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 multiline = multiignore = FALSE;
927 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000928 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000930 /* honor %> item */
931 if (fmt_ptr->conthere)
932 fmt_start = fmt_ptr;
933
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100937 multiignore = FALSE; /* reset continuation */
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
940 { /* continuation of multi-line msg */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200941 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
942
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (qfprev == NULL)
944 goto error2;
945 if (*errmsg && !multiignore)
946 {
947 len = (int)STRLEN(qfprev->qf_text);
948 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
949 == NULL)
950 goto error2;
951 STRCPY(ptr, qfprev->qf_text);
952 vim_free(qfprev->qf_text);
953 qfprev->qf_text = ptr;
954 *(ptr += len) = '\n';
955 STRCPY(++ptr, errmsg);
956 }
957 if (qfprev->qf_nr == -1)
958 qfprev->qf_nr = enr;
959 if (vim_isprintc(type) && !qfprev->qf_type)
960 qfprev->qf_type = type; /* only printable chars allowed */
961 if (!qfprev->qf_lnum)
962 qfprev->qf_lnum = lnum;
963 if (!qfprev->qf_col)
964 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000965 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (!qfprev->qf_fnum)
967 qfprev->qf_fnum = qf_get_fnum(directory,
968 *namebuf || directory ? namebuf
969 : currfile && valid ? currfile : 0);
970 if (idx == 'Z')
971 multiline = multiignore = FALSE;
972 line_breakcheck();
973 continue;
974 }
975 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
976 {
977 /* global file names */
978 valid = FALSE;
979 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
980 {
981 if (*namebuf && idx == 'P')
982 currfile = qf_push_dir(namebuf, &file_stack);
983 else if (idx == 'Q')
984 currfile = qf_pop_dir(&file_stack);
985 *namebuf = NUL;
986 if (tail && *tail)
987 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000988 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 multiscan = TRUE;
990 goto restofline;
991 }
992 }
993 }
994 if (fmt_ptr->flags == '-') /* generally exclude this line */
995 {
996 if (multiline)
997 multiignore = TRUE; /* also exclude continuation lines */
998 continue;
999 }
1000 }
1001
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001002 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001004 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001006 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001007 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 errmsg,
1009 lnum,
1010 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001011 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001012 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 enr,
1014 type,
1015 valid) == FAIL)
1016 goto error2;
1017 line_breakcheck();
1018 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00001019 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001021 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001023 /* no valid entry found */
1024 qi->qf_lists[qi->qf_curlist].qf_ptr =
1025 qi->qf_lists[qi->qf_curlist].qf_start;
1026 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1027 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 else
1030 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001031 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1032 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1033 qi->qf_lists[qi->qf_curlist].qf_ptr =
1034 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001036 /* return number of matches */
1037 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 goto qf_init_ok;
1039 }
1040 EMSG(_(e_readerrf));
1041error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001042 qf_free(qi, qi->qf_curlist);
1043 qi->qf_listcount--;
1044 if (qi->qf_curlist > 0)
1045 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +00001047 if (fd != NULL)
1048 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
1050 {
1051 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02001052 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 vim_free(fmt_ptr);
1054 }
1055 qf_clean_dir_stack(&dir_stack);
1056 qf_clean_dir_stack(&file_stack);
1057qf_init_end:
1058 vim_free(namebuf);
1059 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001060 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 vim_free(fmtstr);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001062 vim_free(growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
1064#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001065 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#endif
1067
1068 return retval;
1069}
1070
Bram Moolenaarfb604092014-07-23 15:55:00 +02001071 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001072qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001073{
1074 if (title != NULL)
1075 {
1076 char_u *p = alloc((int)STRLEN(title) + 2);
1077
1078 qi->qf_lists[qi->qf_curlist].qf_title = p;
1079 if (p != NULL)
1080 sprintf((char *)p, ":%s", (char *)title);
1081 }
1082}
1083
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084/*
1085 * Prepare for adding a new quickfix list.
1086 */
1087 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001088qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
1090 int i;
1091
1092 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001093 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 * the current entry. This makes it possible to browse in a tree-like
1095 * way with ":grep'.
1096 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001097 while (qi->qf_listcount > qi->qf_curlist + 1)
1098 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
1100 /*
1101 * When the stack is full, remove to oldest entry
1102 * Otherwise, add a new entry.
1103 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001104 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001106 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001108 qi->qf_lists[i - 1] = qi->qf_lists[i];
1109 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
1111 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001112 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001113 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001114 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115}
1116
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001117/*
1118 * Free a location list
1119 */
1120 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001121ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001122{
1123 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001124 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001125
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001126 qi = *pqi;
1127 if (qi == NULL)
1128 return;
1129 *pqi = NULL; /* Remove reference to this list */
1130
1131 qi->qf_refcount--;
1132 if (qi->qf_refcount < 1)
1133 {
1134 /* No references to this location list */
1135 for (i = 0; i < qi->qf_listcount; ++i)
1136 qf_free(qi, i);
1137 vim_free(qi);
1138 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001139}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001140
1141 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001142qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001143{
1144 int i;
1145 qf_info_T *qi = &ql_info;
1146
1147 if (wp != NULL)
1148 {
1149 /* location list */
1150 ll_free_all(&wp->w_llist);
1151 ll_free_all(&wp->w_llist_ref);
1152 }
1153 else
1154 /* quickfix list */
1155 for (i = 0; i < qi->qf_listcount; ++i)
1156 qf_free(qi, i);
1157}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159/*
1160 * Add an entry to the end of the list of errors.
1161 * Returns OK or FAIL.
1162 */
1163 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001164qf_add_entry(
1165 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001166 char_u *dir, /* optional directory name */
1167 char_u *fname, /* file name or NULL */
1168 int bufnum, /* buffer number or zero */
1169 char_u *mesg, /* message */
1170 long lnum, /* line number */
1171 int col, /* column */
1172 int vis_col, /* using visual column */
1173 char_u *pattern, /* search pattern */
1174 int nr, /* error number */
1175 int type, /* type character */
1176 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001178 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001179 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001181 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001183 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001184 {
1185 buf_T *buf = buflist_findnr(bufnum);
1186
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001187 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001188 if (buf != NULL)
1189 buf->b_has_qf_entry = TRUE;
1190 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001191 else
1192 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1194 {
1195 vim_free(qfp);
1196 return FAIL;
1197 }
1198 qfp->qf_lnum = lnum;
1199 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001200 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001201 if (pattern == NULL || *pattern == NUL)
1202 qfp->qf_pattern = NULL;
1203 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1204 {
1205 vim_free(qfp->qf_text);
1206 vim_free(qfp);
1207 return FAIL;
1208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 qfp->qf_nr = nr;
1210 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1211 type = 0;
1212 qfp->qf_type = type;
1213 qfp->qf_valid = valid;
1214
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001215 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001216 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1217 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001219 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001220 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1221 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001222 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224 else
1225 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001226 qfp->qf_prev = *lastp;
1227 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001229 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001231 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001232 ++qi->qf_lists[qi->qf_curlist].qf_count;
1233 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1234 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001236 qi->qf_lists[qi->qf_curlist].qf_index =
1237 qi->qf_lists[qi->qf_curlist].qf_count;
1238 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240
1241 return OK;
1242}
1243
1244/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001245 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001246 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001247 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001248ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001249{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001250 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001251
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001252 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1253 if (qi != NULL)
1254 {
1255 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1256 qi->qf_refcount++;
1257 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001258
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001259 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001260}
1261
1262/*
1263 * Return the location list for window 'wp'.
1264 * If not present, allocate a location list
1265 */
1266 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001267ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001268{
1269 if (IS_LL_WINDOW(wp))
1270 /* For a location list window, use the referenced location list */
1271 return wp->w_llist_ref;
1272
1273 /*
1274 * For a non-location list window, w_llist_ref should not point to a
1275 * location list.
1276 */
1277 ll_free_all(&wp->w_llist_ref);
1278
1279 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001280 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001281 return wp->w_llist;
1282}
1283
1284/*
1285 * Copy the location list from window "from" to window "to".
1286 */
1287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001288copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001289{
1290 qf_info_T *qi;
1291 int idx;
1292 int i;
1293
1294 /*
1295 * When copying from a location list window, copy the referenced
1296 * location list. For other windows, copy the location list for
1297 * that window.
1298 */
1299 if (IS_LL_WINDOW(from))
1300 qi = from->w_llist_ref;
1301 else
1302 qi = from->w_llist;
1303
1304 if (qi == NULL) /* no location list to copy */
1305 return;
1306
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001307 /* allocate a new location list */
1308 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001309 return;
1310
1311 to->w_llist->qf_listcount = qi->qf_listcount;
1312
1313 /* Copy the location lists one at a time */
1314 for (idx = 0; idx < qi->qf_listcount; idx++)
1315 {
1316 qf_list_T *from_qfl;
1317 qf_list_T *to_qfl;
1318
1319 to->w_llist->qf_curlist = idx;
1320
1321 from_qfl = &qi->qf_lists[idx];
1322 to_qfl = &to->w_llist->qf_lists[idx];
1323
1324 /* Some of the fields are populated by qf_add_entry() */
1325 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1326 to_qfl->qf_count = 0;
1327 to_qfl->qf_index = 0;
1328 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001329 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001330 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001331 if (from_qfl->qf_title != NULL)
1332 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1333 else
1334 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001335
1336 if (from_qfl->qf_count)
1337 {
1338 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001339 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001340
1341 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001342 for (i = 0, from_qfp = from_qfl->qf_start;
1343 i < from_qfl->qf_count && from_qfp != NULL;
1344 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001345 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001346 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001347 NULL,
1348 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001349 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001350 from_qfp->qf_text,
1351 from_qfp->qf_lnum,
1352 from_qfp->qf_col,
1353 from_qfp->qf_viscol,
1354 from_qfp->qf_pattern,
1355 from_qfp->qf_nr,
1356 0,
1357 from_qfp->qf_valid) == FAIL)
1358 {
1359 qf_free_all(to);
1360 return;
1361 }
1362 /*
1363 * qf_add_entry() will not set the qf_num field, as the
1364 * directory and file names are not supplied. So the qf_fnum
1365 * field is copied here.
1366 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001367 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001368 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1369 prevp->qf_type = from_qfp->qf_type; /* error type */
1370 if (from_qfl->qf_ptr == from_qfp)
1371 to_qfl->qf_ptr = prevp; /* current location */
1372 }
1373 }
1374
1375 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1376
1377 /* When no valid entries are present in the list, qf_ptr points to
1378 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001379 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001380 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001381 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001382 to_qfl->qf_index = 1;
1383 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001384 }
1385
1386 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1387}
1388
1389/*
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001390 * Get buffer number for file "dir.name".
1391 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 */
1393 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001394qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001396 char_u *ptr;
1397 buf_T *buf;
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (fname == NULL || *fname == NUL) /* no file name */
1400 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
Bram Moolenaare60acc12011-05-10 16:41:25 +02001402#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001403 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001404#endif
1405#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001406 if (directory != NULL)
1407 slash_adjust(directory);
1408 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001409#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001410 if (directory != NULL && !vim_isAbsName(fname)
1411 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1412 {
1413 /*
1414 * Here we check if the file really exists.
1415 * This should normally be true, but if make works without
1416 * "leaving directory"-messages we might have missed a
1417 * directory change.
1418 */
1419 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 vim_free(ptr);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001422 directory = qf_guess_filepath(fname);
1423 if (directory)
1424 ptr = concat_fnames(directory, fname, TRUE);
1425 else
1426 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001428 /* Use concatenated directory name and file name */
1429 buf = buflist_new(ptr, NULL, (linenr_T)0, 0);
1430 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001432 else
1433 buf = buflist_new(fname, NULL, (linenr_T)0, 0);
1434 if (buf == NULL)
1435 return 0;
1436 buf->b_has_qf_entry = TRUE;
1437 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438}
1439
1440/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001441 * Push dirbuf onto the directory stack and return pointer to actual dir or
1442 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 */
1444 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001445qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 struct dir_stack_T *ds_new;
1448 struct dir_stack_T *ds_ptr;
1449
1450 /* allocate new stack element and hook it in */
1451 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1452 if (ds_new == NULL)
1453 return NULL;
1454
1455 ds_new->next = *stackptr;
1456 *stackptr = ds_new;
1457
1458 /* store directory on the stack */
1459 if (vim_isAbsName(dirbuf)
1460 || (*stackptr)->next == NULL
1461 || (*stackptr && dir_stack != *stackptr))
1462 (*stackptr)->dirname = vim_strsave(dirbuf);
1463 else
1464 {
1465 /* Okay we don't have an absolute path.
1466 * dirbuf must be a subdir of one of the directories on the stack.
1467 * Let's search...
1468 */
1469 ds_new = (*stackptr)->next;
1470 (*stackptr)->dirname = NULL;
1471 while (ds_new)
1472 {
1473 vim_free((*stackptr)->dirname);
1474 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1475 TRUE);
1476 if (mch_isdir((*stackptr)->dirname) == TRUE)
1477 break;
1478
1479 ds_new = ds_new->next;
1480 }
1481
1482 /* clean up all dirs we already left */
1483 while ((*stackptr)->next != ds_new)
1484 {
1485 ds_ptr = (*stackptr)->next;
1486 (*stackptr)->next = (*stackptr)->next->next;
1487 vim_free(ds_ptr->dirname);
1488 vim_free(ds_ptr);
1489 }
1490
1491 /* Nothing found -> it must be on top level */
1492 if (ds_new == NULL)
1493 {
1494 vim_free((*stackptr)->dirname);
1495 (*stackptr)->dirname = vim_strsave(dirbuf);
1496 }
1497 }
1498
1499 if ((*stackptr)->dirname != NULL)
1500 return (*stackptr)->dirname;
1501 else
1502 {
1503 ds_ptr = *stackptr;
1504 *stackptr = (*stackptr)->next;
1505 vim_free(ds_ptr);
1506 return NULL;
1507 }
1508}
1509
1510
1511/*
1512 * pop dirbuf from the directory stack and return previous directory or NULL if
1513 * stack is empty
1514 */
1515 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001516qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 struct dir_stack_T *ds_ptr;
1519
1520 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1521 * What to do if it isn't? */
1522
1523 /* pop top element and free it */
1524 if (*stackptr != NULL)
1525 {
1526 ds_ptr = *stackptr;
1527 *stackptr = (*stackptr)->next;
1528 vim_free(ds_ptr->dirname);
1529 vim_free(ds_ptr);
1530 }
1531
1532 /* return NEW top element as current dir or NULL if stack is empty*/
1533 return *stackptr ? (*stackptr)->dirname : NULL;
1534}
1535
1536/*
1537 * clean up directory stack
1538 */
1539 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001540qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
1542 struct dir_stack_T *ds_ptr;
1543
1544 while ((ds_ptr = *stackptr) != NULL)
1545 {
1546 *stackptr = (*stackptr)->next;
1547 vim_free(ds_ptr->dirname);
1548 vim_free(ds_ptr);
1549 }
1550}
1551
1552/*
1553 * Check in which directory of the directory stack the given file can be
1554 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001555 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 * Cleans up intermediate directory entries.
1557 *
1558 * TODO: How to solve the following problem?
1559 * If we have the this directory tree:
1560 * ./
1561 * ./aa
1562 * ./aa/bb
1563 * ./bb
1564 * ./bb/x.c
1565 * and make says:
1566 * making all in aa
1567 * making all in bb
1568 * x.c:9: Error
1569 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1570 * qf_guess_filepath will return NULL.
1571 */
1572 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001573qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575 struct dir_stack_T *ds_ptr;
1576 struct dir_stack_T *ds_tmp;
1577 char_u *fullname;
1578
1579 /* no dirs on the stack - there's nothing we can do */
1580 if (dir_stack == NULL)
1581 return NULL;
1582
1583 ds_ptr = dir_stack->next;
1584 fullname = NULL;
1585 while (ds_ptr)
1586 {
1587 vim_free(fullname);
1588 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1589
1590 /* If concat_fnames failed, just go on. The worst thing that can happen
1591 * is that we delete the entire stack.
1592 */
1593 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1594 break;
1595
1596 ds_ptr = ds_ptr->next;
1597 }
1598
1599 vim_free(fullname);
1600
1601 /* clean up all dirs we already left */
1602 while (dir_stack->next != ds_ptr)
1603 {
1604 ds_tmp = dir_stack->next;
1605 dir_stack->next = dir_stack->next->next;
1606 vim_free(ds_tmp->dirname);
1607 vim_free(ds_tmp);
1608 }
1609
1610 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1611
1612}
1613
1614/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001615 * When loading a file from the quickfix, the auto commands may modify it.
1616 * This may invalidate the current quickfix entry. This function checks
1617 * whether a entry is still present in the quickfix.
1618 * Similar to location list.
1619 */
1620 static int
1621is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1622{
1623 qf_list_T *qfl;
1624 qfline_T *qfp;
1625 int i;
1626
1627 qfl = &qi->qf_lists[qi->qf_curlist];
1628
1629 /* Search for the entry in the current list */
1630 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1631 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001632 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001633 break;
1634
1635 if (i == qfl->qf_count) /* Entry is not found */
1636 return FALSE;
1637
1638 return TRUE;
1639}
1640
1641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 * jump to a quickfix line
1643 * if dir == FORWARD go "errornr" valid entries forward
1644 * if dir == BACKWARD go "errornr" valid entries backward
1645 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1646 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1647 * else if "errornr" is zero, redisplay the same line
1648 * else go to entry "errornr"
1649 */
1650 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001651qf_jump(
1652 qf_info_T *qi,
1653 int dir,
1654 int errornr,
1655 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001657 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001658 qfline_T *qf_ptr;
1659 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 int qf_index;
1661 int old_qf_fnum;
1662 int old_qf_index;
1663 int prev_index;
1664 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1665 char_u *err = e_no_more_items;
1666 linenr_T i;
1667 buf_T *old_curbuf;
1668 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 colnr_T screen_col;
1670 colnr_T char_col;
1671 char_u *line;
1672#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001673 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001674 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 int opened_window = FALSE;
1676 win_T *win;
1677 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001678 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001680 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 int print_message = TRUE;
1682 int len;
1683#ifdef FEAT_FOLDING
1684 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1685#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001686 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001687 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001689 if (qi == NULL)
1690 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001691
1692 if (qi->qf_curlist >= qi->qf_listcount
1693 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695 EMSG(_(e_quickfix));
1696 return;
1697 }
1698
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001699 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001701 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 old_qf_index = qf_index;
1703 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1704 {
1705 while (errornr--)
1706 {
1707 old_qf_ptr = qf_ptr;
1708 prev_index = qf_index;
1709 old_qf_fnum = qf_ptr->qf_fnum;
1710 do
1711 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001712 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 || qf_ptr->qf_next == NULL)
1714 {
1715 qf_ptr = old_qf_ptr;
1716 qf_index = prev_index;
1717 if (err != NULL)
1718 {
1719 EMSG(_(err));
1720 goto theend;
1721 }
1722 errornr = 0;
1723 break;
1724 }
1725 ++qf_index;
1726 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001727 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1728 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1730 err = NULL;
1731 }
1732 }
1733 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1734 {
1735 while (errornr--)
1736 {
1737 old_qf_ptr = qf_ptr;
1738 prev_index = qf_index;
1739 old_qf_fnum = qf_ptr->qf_fnum;
1740 do
1741 {
1742 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1743 {
1744 qf_ptr = old_qf_ptr;
1745 qf_index = prev_index;
1746 if (err != NULL)
1747 {
1748 EMSG(_(err));
1749 goto theend;
1750 }
1751 errornr = 0;
1752 break;
1753 }
1754 --qf_index;
1755 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001756 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1757 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1759 err = NULL;
1760 }
1761 }
1762 else if (errornr != 0) /* go to specified number */
1763 {
1764 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1765 {
1766 --qf_index;
1767 qf_ptr = qf_ptr->qf_prev;
1768 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001769 while (errornr > qf_index && qf_index <
1770 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 && qf_ptr->qf_next != NULL)
1772 {
1773 ++qf_index;
1774 qf_ptr = qf_ptr->qf_next;
1775 }
1776 }
1777
1778#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001779 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1780 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 /* No need to print the error message if it's visible in the error
1782 * window */
1783 print_message = FALSE;
1784
1785 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001786 * For ":helpgrep" find a help window or open one.
1787 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001788 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001789 {
1790 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001791
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001792 if (cmdmod.tab != 0)
1793 wp = NULL;
1794 else
1795 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1796 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1797 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001798 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1799 win_enter(wp, TRUE);
1800 else
1801 {
1802 /*
1803 * Split off help window; put it at far top if no position
1804 * specified, the current window is vertically split and narrow.
1805 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001806 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001807 if (cmdmod.split == 0 && curwin->w_width != Columns
1808 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001809 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001810 if (qi != &ql_info)
1811 flags |= WSP_NEWLOC; /* don't copy the location list */
1812
1813 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001814 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001815 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001816
1817 if (curwin->w_height < p_hh)
1818 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001819
1820 if (qi != &ql_info) /* not a quickfix list */
1821 {
1822 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001823 curwin->w_llist = qi;
1824 qi->qf_refcount++;
1825 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001826 }
1827
1828 if (!p_im)
1829 restart_edit = 0; /* don't want insert mode in help file */
1830 }
1831
1832 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 * If currently in the quickfix window, find another window to show the
1834 * file in.
1835 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001836 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001838 win_T *usable_win_ptr = NULL;
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 /*
1841 * If there is no file specified, we don't know where to go.
1842 * But do advance, otherwise ":cn" gets stuck.
1843 */
1844 if (qf_ptr->qf_fnum == 0)
1845 goto theend;
1846
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001847 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001848
1849 ll_ref = curwin->w_llist_ref;
1850 if (ll_ref != NULL)
1851 {
1852 /* Find a window using the same location list that is not a
1853 * quickfix window. */
1854 FOR_ALL_WINDOWS(usable_win_ptr)
1855 if (usable_win_ptr->w_llist == ll_ref
1856 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001857 {
1858 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001859 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001860 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001861 }
1862
1863 if (!usable_win)
1864 {
1865 /* Locate a window showing a normal buffer */
1866 FOR_ALL_WINDOWS(win)
1867 if (win->w_buffer->b_p_bt[0] == NUL)
1868 {
1869 usable_win = 1;
1870 break;
1871 }
1872 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001875 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001876 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001878 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001879 {
1880 tabpage_T *tp;
1881 win_T *wp;
1882
1883 FOR_ALL_TAB_WINDOWS(tp, wp)
1884 {
1885 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1886 {
1887 goto_tabpage_win(tp, wp);
1888 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001889 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001890 }
1891 }
1892 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001893win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001894
1895 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001896 * If there is only one window and it is the quickfix window, create a
1897 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001898 */
1899 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001901 flags = WSP_ABOVE;
1902 if (ll_ref != NULL)
1903 flags |= WSP_NEWLOC;
1904 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 goto failed; /* not enough room for window */
1906 opened_window = TRUE; /* close it when fail */
1907 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001908 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001909 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001910 if (ll_ref != NULL)
1911 {
1912 /* The new window should use the location list from the
1913 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001914 curwin->w_llist = ll_ref;
1915 ll_ref->qf_refcount++;
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 }
1918 else
1919 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001920 if (curwin->w_llist_ref != NULL)
1921 {
1922 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001923 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001924 if (win == NULL)
1925 {
1926 /* Find the window showing the selected file */
1927 FOR_ALL_WINDOWS(win)
1928 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1929 break;
1930 if (win == NULL)
1931 {
1932 /* Find a previous usable window */
1933 win = curwin;
1934 do
1935 {
1936 if (win->w_buffer->b_p_bt[0] == NUL)
1937 break;
1938 if (win->w_prev == NULL)
1939 win = lastwin; /* wrap around the top */
1940 else
1941 win = win->w_prev; /* go to previous window */
1942 } while (win != curwin);
1943 }
1944 }
1945 win_goto(win);
1946
1947 /* If the location list for the window is not set, then set it
1948 * to the location list from the location window */
1949 if (win->w_llist == NULL)
1950 {
1951 win->w_llist = ll_ref;
1952 ll_ref->qf_refcount++;
1953 }
1954 }
1955 else
1956 {
1957
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 /*
1959 * Try to find a window that shows the right buffer.
1960 * Default to the window just above the quickfix buffer.
1961 */
1962 win = curwin;
1963 altwin = NULL;
1964 for (;;)
1965 {
1966 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1967 break;
1968 if (win->w_prev == NULL)
1969 win = lastwin; /* wrap around the top */
1970 else
1971 win = win->w_prev; /* go to previous window */
1972
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 /* Didn't find it, go to the window before the quickfix
1976 * window. */
1977 if (altwin != NULL)
1978 win = altwin;
1979 else if (curwin->w_prev != NULL)
1980 win = curwin->w_prev;
1981 else
1982 win = curwin->w_next;
1983 break;
1984 }
1985
1986 /* Remember a usable window. */
1987 if (altwin == NULL && !win->w_p_pvw
1988 && win->w_buffer->b_p_bt[0] == NUL)
1989 altwin = win;
1990 }
1991
1992 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
1995 }
1996#endif
1997
1998 /*
1999 * If there is a file name,
2000 * read the wanted file if needed, and check autowrite etc.
2001 */
2002 old_curbuf = curbuf;
2003 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002004
2005 if (qf_ptr->qf_fnum != 0)
2006 {
2007 if (qf_ptr->qf_type == 1)
2008 {
2009 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2010 * set b_p_ro flag). */
2011 if (!can_abandon(curbuf, forceit))
2012 {
2013 EMSG(_(e_nowrtmsg));
2014 ok = FALSE;
2015 }
2016 else
2017 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002018 ECMD_HIDE + ECMD_SET_HELP,
2019 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002020 }
2021 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002022 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002023 int old_qf_curlist = qi->qf_curlist;
2024 int is_abort = FALSE;
2025
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002026 ok = buflist_getfile(qf_ptr->qf_fnum,
2027 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002028 if (qi != &ql_info && !win_valid(oldwin))
2029 {
2030 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002031 is_abort = TRUE;
2032 opened_window = FALSE;
2033 }
2034 else if (old_qf_curlist != qi->qf_curlist
2035 || !is_qf_entry_present(qi, qf_ptr))
2036 {
2037 if (qi == &ql_info)
2038 EMSG(_("E925: Current quickfix was changed"));
2039 else
2040 EMSG(_("E926: Current location list was changed"));
2041 is_abort = TRUE;
2042 }
2043
2044 if (is_abort)
2045 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002046 ok = FALSE;
2047 qi = NULL;
2048 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002049 }
2050 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002051 }
2052
2053 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
2055 /* When not switched to another buffer, still need to set pc mark */
2056 if (curbuf == old_curbuf)
2057 setpcmark();
2058
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002059 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002061 /*
2062 * Go to line with error, unless qf_lnum is 0.
2063 */
2064 i = qf_ptr->qf_lnum;
2065 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002067 if (i > curbuf->b_ml.ml_line_count)
2068 i = curbuf->b_ml.ml_line_count;
2069 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002071 if (qf_ptr->qf_col > 0)
2072 {
2073 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002074#ifdef FEAT_VIRTUALEDIT
2075 curwin->w_cursor.coladd = 0;
2076#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002077 if (qf_ptr->qf_viscol == TRUE)
2078 {
2079 /*
2080 * Check each character from the beginning of the error
2081 * line up to the error column. For each tab character
2082 * found, reduce the error column value by the length of
2083 * a tab character.
2084 */
2085 line = ml_get_curline();
2086 screen_col = 0;
2087 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2088 {
2089 if (*line == NUL)
2090 break;
2091 if (*line++ == '\t')
2092 {
2093 curwin->w_cursor.col -= 7 - (screen_col % 8);
2094 screen_col += 8 - (screen_col % 8);
2095 }
2096 else
2097 ++screen_col;
2098 }
2099 }
2100 check_cursor();
2101 }
2102 else
2103 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002106 {
2107 pos_T save_cursor;
2108
2109 /* Move the cursor to the first line in the buffer */
2110 save_cursor = curwin->w_cursor;
2111 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002112 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2113 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002114 curwin->w_cursor = save_cursor;
2115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117#ifdef FEAT_FOLDING
2118 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2119 foldOpenCursor();
2120#endif
2121 if (print_message)
2122 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002123 /* Update the screen before showing the message, unless the screen
2124 * scrolled up. */
2125 if (!msg_scrolled)
2126 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002128 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2130 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2131 /* Add the message, skipping leading whitespace and newlines. */
2132 len = (int)STRLEN(IObuff);
2133 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2134
2135 /* Output the message. Overwrite to avoid scrolling when the 'O'
2136 * flag is present in 'shortmess'; But when not jumping, print the
2137 * whole message. */
2138 i = msg_scroll;
2139 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2140 msg_scroll = TRUE;
2141 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2142 msg_scroll = FALSE;
2143 msg_attr_keep(IObuff, 0, TRUE);
2144 msg_scroll = i;
2145 }
2146 }
2147 else
2148 {
2149#ifdef FEAT_WINDOWS
2150 if (opened_window)
2151 win_close(curwin, TRUE); /* Close opened window */
2152#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002153 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
2155 /*
2156 * Couldn't open file, so put index back where it was. This could
2157 * happen if the file was readonly and we changed something.
2158 */
2159#ifdef FEAT_WINDOWS
2160failed:
2161#endif
2162 qf_ptr = old_qf_ptr;
2163 qf_index = old_qf_index;
2164 }
2165 }
2166theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002167 if (qi != NULL)
2168 {
2169 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2170 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172#ifdef FEAT_WINDOWS
2173 if (p_swb != old_swb && opened_window)
2174 {
2175 /* Restore old 'switchbuf' value, but not when an autocommand or
2176 * modeline has changed the value. */
2177 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002180 swb_flags = old_swb_flags;
2181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 else
2183 free_string_option(old_swb);
2184 }
2185#endif
2186}
2187
2188/*
2189 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 */
2192 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002193qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002195 buf_T *buf;
2196 char_u *fname;
2197 qfline_T *qfp;
2198 int i;
2199 int idx1 = 1;
2200 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002201 char_u *arg = eap->arg;
2202 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002204 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002206 if (eap->cmdidx == CMD_llist)
2207 {
2208 qi = GET_LOC_LIST(curwin);
2209 if (qi == NULL)
2210 {
2211 EMSG(_(e_loclist));
2212 return;
2213 }
2214 }
2215
2216 if (qi->qf_curlist >= qi->qf_listcount
2217 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
2219 EMSG(_(e_quickfix));
2220 return;
2221 }
2222 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2223 {
2224 EMSG(_(e_trailing));
2225 return;
2226 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (idx1 < 0)
2229 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2230 if (idx2 < 0)
2231 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2232
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002235 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2236 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
2238 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2239 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002240 msg_putchar('\n');
2241 if (got_int)
2242 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002243
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002244 fname = NULL;
2245 if (qfp->qf_fnum != 0
2246 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2247 {
2248 fname = buf->b_fname;
2249 if (qfp->qf_type == 1) /* :helpgrep */
2250 fname = gettail(fname);
2251 }
2252 if (fname == NULL)
2253 sprintf((char *)IObuff, "%2d", i);
2254 else
2255 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2256 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002257 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002258 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2259 if (qfp->qf_lnum == 0)
2260 IObuff[0] = NUL;
2261 else if (qfp->qf_col == 0)
2262 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2263 else
2264 sprintf((char *)IObuff, ":%ld col %d",
2265 qfp->qf_lnum, qfp->qf_col);
2266 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2267 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2268 msg_puts_attr(IObuff, hl_attr(HLF_N));
2269 if (qfp->qf_pattern != NULL)
2270 {
2271 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2272 STRCAT(IObuff, ":");
2273 msg_puts(IObuff);
2274 }
2275 msg_puts((char_u *)" ");
2276
2277 /* Remove newlines and leading whitespace from the text. For an
2278 * unrecognized line keep the indent, the compiler may mark a word
2279 * with ^^^^. */
2280 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2282 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002283 msg_prt_line(IObuff, FALSE);
2284 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002286
2287 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002288 if (qfp == NULL)
2289 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002290 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 ui_breakcheck();
2292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293}
2294
2295/*
2296 * Remove newlines and leading whitespace from an error message.
2297 * Put the result in "buf[bufsize]".
2298 */
2299 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002300qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 int i;
2303 char_u *p = text;
2304
2305 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2306 {
2307 if (*p == '\n')
2308 {
2309 buf[i] = ' ';
2310 while (*++p != NUL)
2311 if (!vim_iswhite(*p) && *p != '\n')
2312 break;
2313 }
2314 else
2315 buf[i] = *p++;
2316 }
2317 buf[i] = NUL;
2318}
2319
2320/*
2321 * ":colder [count]": Up in the quickfix stack.
2322 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002323 * ":lolder [count]": Up in the location list stack.
2324 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 */
2326 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002327qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002329 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 int count;
2331
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002332 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2333 {
2334 qi = GET_LOC_LIST(curwin);
2335 if (qi == NULL)
2336 {
2337 EMSG(_(e_loclist));
2338 return;
2339 }
2340 }
2341
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (eap->addr_count != 0)
2343 count = eap->line2;
2344 else
2345 count = 1;
2346 while (count--)
2347 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002348 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002350 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002355 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357 else
2358 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002359 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
2361 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002362 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002364 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002367 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368}
2369
2370 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002371qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002374 qi->qf_curlist + 1, qi->qf_listcount,
2375 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002377 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378#endif
2379}
2380
2381/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002382 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 */
2384 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002385qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002387 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002388 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002389 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002391 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002393 qfp = qi->qf_lists[idx].qf_start;
2394 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002395 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002396 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002397 vim_free(qfp->qf_text);
2398 stop = (qfp == qfpnext);
2399 vim_free(qfp->qf_pattern);
2400 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002401 if (stop)
2402 /* Somehow qf_count may have an incorrect value, set it to 1
2403 * to avoid crashing when it's wrong.
2404 * TODO: Avoid qf_count being incorrect. */
2405 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002406 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002407 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002408 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002410 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002411 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002412 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413}
2414
2415/*
2416 * qf_mark_adjust: adjust marks
2417 */
2418 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002419qf_mark_adjust(
2420 win_T *wp,
2421 linenr_T line1,
2422 linenr_T line2,
2423 long amount,
2424 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002426 int i;
2427 qfline_T *qfp;
2428 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002429 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002430 int found_one = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002432 if (!curbuf->b_has_qf_entry)
2433 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002434 if (wp != NULL)
2435 {
2436 if (wp->w_llist == NULL)
2437 return;
2438 qi = wp->w_llist;
2439 }
2440
2441 for (idx = 0; idx < qi->qf_listcount; ++idx)
2442 if (qi->qf_lists[idx].qf_count)
2443 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002444 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2445 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 if (qfp->qf_fnum == curbuf->b_fnum)
2447 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002448 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2450 {
2451 if (amount == MAXLNUM)
2452 qfp->qf_cleared = TRUE;
2453 else
2454 qfp->qf_lnum += amount;
2455 }
2456 else if (amount_after && qfp->qf_lnum > line2)
2457 qfp->qf_lnum += amount_after;
2458 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002459
2460 if (!found_one)
2461 curbuf->b_has_qf_entry = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462}
2463
2464/*
2465 * Make a nice message out of the error character and the error number:
2466 * char number message
2467 * e or E 0 " error"
2468 * w or W 0 " warning"
2469 * i or I 0 " info"
2470 * 0 0 ""
2471 * other 0 " c"
2472 * e or E n " error n"
2473 * w or W n " warning n"
2474 * i or I n " info n"
2475 * 0 n " error n"
2476 * other n " c n"
2477 * 1 x "" :helpgrep
2478 */
2479 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002480qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 static char_u buf[20];
2483 static char_u cc[3];
2484 char_u *p;
2485
2486 if (c == 'W' || c == 'w')
2487 p = (char_u *)" warning";
2488 else if (c == 'I' || c == 'i')
2489 p = (char_u *)" info";
2490 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2491 p = (char_u *)" error";
2492 else if (c == 0 || c == 1)
2493 p = (char_u *)"";
2494 else
2495 {
2496 cc[0] = ' ';
2497 cc[1] = c;
2498 cc[2] = NUL;
2499 p = cc;
2500 }
2501
2502 if (nr <= 0)
2503 return p;
2504
2505 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2506 return buf;
2507}
2508
2509#if defined(FEAT_WINDOWS) || defined(PROTO)
2510/*
2511 * ":cwindow": open the quickfix window if we have errors to display,
2512 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002513 * ":lwindow": open the location list window if we have locations to display,
2514 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 */
2516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002517ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002519 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 win_T *win;
2521
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002522 if (eap->cmdidx == CMD_lwindow)
2523 {
2524 qi = GET_LOC_LIST(curwin);
2525 if (qi == NULL)
2526 return;
2527 }
2528
2529 /* Look for an existing quickfix window. */
2530 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
2532 /*
2533 * If a quickfix window is open but we have no errors to display,
2534 * close the window. If a quickfix window is not open, then open
2535 * it if we have errors; otherwise, leave it closed.
2536 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002537 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002538 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002539 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
2541 if (win != NULL)
2542 ex_cclose(eap);
2543 }
2544 else if (win == NULL)
2545 ex_copen(eap);
2546}
2547
2548/*
2549 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002550 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002553ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002555 win_T *win = NULL;
2556 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002558 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2559 {
2560 qi = GET_LOC_LIST(curwin);
2561 if (qi == NULL)
2562 return;
2563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002565 /* Find existing quickfix window and close it. */
2566 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (win != NULL)
2568 win_close(win, FALSE);
2569}
2570
2571/*
2572 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002573 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 */
2575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002576ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002578 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002581 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002582 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002583 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002585 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2586 {
2587 qi = GET_LOC_LIST(curwin);
2588 if (qi == NULL)
2589 {
2590 EMSG(_(e_loclist));
2591 return;
2592 }
2593 }
2594
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (eap->addr_count != 0)
2596 height = eap->line2;
2597 else
2598 height = QF_WINHEIGHT;
2599
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601#ifdef FEAT_GUI
2602 need_mouse_correct = TRUE;
2603#endif
2604
2605 /*
2606 * Find existing quickfix window, or open a new one.
2607 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002608 win = qf_find_win(qi);
2609
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002610 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002613 if (eap->addr_count != 0)
2614 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002615 if (cmdmod.split & WSP_VERT)
2616 {
2617 if (height != W_WIDTH(win))
2618 win_setwidth(height);
2619 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002620 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002621 win_setheight(height);
2622 }
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 else
2625 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002626 qf_buf = qf_find_buf(qi);
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 /* The current window becomes the previous window afterwards. */
2629 win = curwin;
2630
Bram Moolenaar77642c02012-11-20 17:55:10 +01002631 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2632 && cmdmod.split == 0)
2633 /* Create the new window at the very bottom, except when
2634 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002635 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002636 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002638 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002640 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002642 /*
2643 * For the location list window, create a reference to the
2644 * location list from the window 'win'.
2645 */
2646 curwin->w_llist_ref = win->w_llist;
2647 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002649
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002650 if (oldwin != curwin)
2651 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002652 if (qf_buf != NULL)
2653 /* Use the existing quickfix buffer */
2654 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002655 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002656 else
2657 {
2658 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002659 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002660 /* switch off 'swapfile' */
2661 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2662 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002663 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002664 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002665 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002666#ifdef FEAT_DIFF
2667 curwin->w_p_diff = FALSE;
2668#endif
2669#ifdef FEAT_FOLDING
2670 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2671 OPT_LOCAL);
2672#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002675 /* Only set the height when still in the same tab page and there is no
2676 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002677 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 win_setheight(height);
2679 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2680 if (win_valid(win))
2681 prevwin = win;
2682 }
2683
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002684 qf_set_title_var(qi);
2685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 /*
2687 * Fill the buffer with the quickfix list.
2688 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002689 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002691 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 curwin->w_cursor.col = 0;
2693 check_cursor();
2694 update_topline(); /* scroll to show the line */
2695}
2696
2697/*
2698 * Return the number of the current entry (line number in the quickfix
2699 * window).
2700 */
2701 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002702qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002704 qf_info_T *qi = &ql_info;
2705
2706 if (IS_LL_WINDOW(wp))
2707 /* In the location list window, use the referenced location list */
2708 qi = wp->w_llist_ref;
2709
2710 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711}
2712
2713/*
2714 * Update the cursor position in the quickfix window to the current error.
2715 * Return TRUE if there is a quickfix window.
2716 */
2717 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002718qf_win_pos_update(
2719 qf_info_T *qi,
2720 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002723 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 /*
2726 * Put the cursor on the current error in the quickfix window, so that
2727 * it's viewable.
2728 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002729 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if (win != NULL
2731 && qf_index <= win->w_buffer->b_ml.ml_line_count
2732 && old_qf_index != qf_index)
2733 {
2734 win_T *old_curwin = curwin;
2735
2736 curwin = win;
2737 curbuf = win->w_buffer;
2738 if (qf_index > old_qf_index)
2739 {
2740 curwin->w_redraw_top = old_qf_index;
2741 curwin->w_redraw_bot = qf_index;
2742 }
2743 else
2744 {
2745 curwin->w_redraw_top = qf_index;
2746 curwin->w_redraw_bot = old_qf_index;
2747 }
2748 curwin->w_cursor.lnum = qf_index;
2749 curwin->w_cursor.col = 0;
2750 update_topline(); /* scroll to show the line */
2751 redraw_later(VALID);
2752 curwin->w_redr_status = TRUE; /* update ruler */
2753 curwin = old_curwin;
2754 curbuf = curwin->w_buffer;
2755 }
2756 return win != NULL;
2757}
2758
2759/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002760 * Check whether the given window is displaying the specified quickfix/location
2761 * list buffer
2762 */
2763 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002764is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002765{
2766 /*
2767 * A window displaying the quickfix buffer will have the w_llist_ref field
2768 * set to NULL.
2769 * A window displaying a location list buffer will have the w_llist_ref
2770 * pointing to the location list.
2771 */
2772 if (bt_quickfix(win->w_buffer))
2773 if ((qi == &ql_info && win->w_llist_ref == NULL)
2774 || (qi != &ql_info && win->w_llist_ref == qi))
2775 return TRUE;
2776
2777 return FALSE;
2778}
2779
2780/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002781 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002782 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783 */
2784 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002785qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002786{
2787 win_T *win;
2788
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002789 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002790 if (is_qf_win(win, qi))
2791 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002792
2793 return win;
2794}
2795
2796/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002797 * Find a quickfix buffer.
2798 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 */
2800 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002801qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002803 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002804 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
Bram Moolenaar9c102382006-05-03 21:26:49 +00002806 FOR_ALL_TAB_WINDOWS(tp, win)
2807 if (is_qf_win(win, qi))
2808 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002809
Bram Moolenaar9c102382006-05-03 21:26:49 +00002810 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811}
2812
2813/*
2814 * Find the quickfix buffer. If it exists, update the contents.
2815 */
2816 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002817qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
2819 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002820 win_T *win;
2821 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
2824 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002825 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 if (buf != NULL)
2827 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02002828 linenr_T old_line_count = buf->b_ml.ml_line_count;
2829
2830 if (old_last == NULL)
2831 /* set curwin/curbuf to buf and save a few things */
2832 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002834 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002835 {
2836 curwin_save = curwin;
2837 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002838 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002839 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002840 }
2841
Bram Moolenaar864293a2016-06-02 13:40:04 +02002842 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01002843
Bram Moolenaar864293a2016-06-02 13:40:04 +02002844 if (old_last == NULL)
2845 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002846 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002847
2848 /* restore curwin/curbuf and a few other things */
2849 aucmd_restbuf(&aco);
2850 }
2851
2852 /* Only redraw when added lines are visible. This avoids flickering
2853 * when the added lines are not visible. */
2854 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
2855 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857}
2858
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002859/*
2860 * Set "w:quickfix_title" if "qi" has a title.
2861 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002862 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002863qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002864{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002865 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2866 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002867 qi->qf_lists[qi->qf_curlist].qf_title);
2868}
2869
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870/*
2871 * Fill current buffer with quickfix errors, replacing any previous contents.
2872 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02002873 * If "old_last" is not NULL append the items after this one.
2874 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
2875 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 */
2877 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002878qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002880 linenr_T lnum;
2881 qfline_T *qfp;
2882 buf_T *errbuf;
2883 int len;
2884 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
Bram Moolenaar864293a2016-06-02 13:40:04 +02002886 if (old_last == NULL)
2887 {
2888 if (buf != curbuf)
2889 {
2890 EMSG2(_(e_intern2), "qf_fill_buffer()");
2891 return;
2892 }
2893
2894 /* delete all existing lines */
2895 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2896 (void)ml_delete((linenr_T)1, FALSE);
2897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
2899 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002900 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
2902 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002903 if (old_last == NULL)
2904 {
2905 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2906 lnum = 0;
2907 }
2908 else
2909 {
2910 qfp = old_last->qf_next;
2911 lnum = buf->b_ml.ml_line_count;
2912 }
2913 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 {
2915 if (qfp->qf_fnum != 0
2916 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2917 && errbuf->b_fname != NULL)
2918 {
2919 if (qfp->qf_type == 1) /* :helpgrep */
2920 STRCPY(IObuff, gettail(errbuf->b_fname));
2921 else
2922 STRCPY(IObuff, errbuf->b_fname);
2923 len = (int)STRLEN(IObuff);
2924 }
2925 else
2926 len = 0;
2927 IObuff[len++] = '|';
2928
2929 if (qfp->qf_lnum > 0)
2930 {
2931 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2932 len += (int)STRLEN(IObuff + len);
2933
2934 if (qfp->qf_col > 0)
2935 {
2936 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2937 len += (int)STRLEN(IObuff + len);
2938 }
2939
2940 sprintf((char *)IObuff + len, "%s",
2941 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2942 len += (int)STRLEN(IObuff + len);
2943 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002944 else if (qfp->qf_pattern != NULL)
2945 {
2946 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2947 len += (int)STRLEN(IObuff + len);
2948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 IObuff[len++] = '|';
2950 IObuff[len++] = ' ';
2951
2952 /* Remove newlines and leading whitespace from the text.
2953 * For an unrecognized line keep the indent, the compiler may
2954 * mark a word with ^^^^. */
2955 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2956 IObuff + len, IOSIZE - len);
2957
Bram Moolenaar864293a2016-06-02 13:40:04 +02002958 if (ml_append_buf(buf, lnum, IObuff,
2959 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02002961 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002963 if (qfp == NULL)
2964 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02002966
2967 if (old_last == NULL)
2968 /* Delete the empty line which is now at the end */
2969 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
2971
2972 /* correct cursor position */
2973 check_lnums(TRUE);
2974
Bram Moolenaar864293a2016-06-02 13:40:04 +02002975 if (old_last == NULL)
2976 {
2977 /* Set the 'filetype' to "qf" each time after filling the buffer.
2978 * This resembles reading a file into a buffer, it's more logical when
2979 * using autocommands. */
2980 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2981 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
2983#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02002984 keep_filetype = TRUE; /* don't detect 'filetype' */
2985 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002987 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002989 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02002991 /* make sure it will be redrawn */
2992 redraw_curbuf_later(NOT_VALID);
2993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 /* Restore KeyTyped, setting 'filetype' may reset it. */
2996 KeyTyped = old_KeyTyped;
2997}
2998
2999#endif /* FEAT_WINDOWS */
3000
3001/*
3002 * Return TRUE if "buf" is the quickfix buffer.
3003 */
3004 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003005bt_quickfix(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] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008}
3009
3010/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003011 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3012 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 */
3014 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003015bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003017 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3018 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019}
3020
3021/*
3022 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3023 */
3024 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003025bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003027 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028}
3029
3030 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003031bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
3033 if (bt_dontwrite(buf))
3034 {
3035 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3036 return TRUE;
3037 }
3038 return FALSE;
3039}
3040
3041/*
3042 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3043 * and 'bufhidden'.
3044 */
3045 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003046buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
3048 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3049 switch (buf->b_p_bh[0])
3050 {
3051 case 'u': /* "unload" */
3052 case 'w': /* "wipe" */
3053 case 'd': return FALSE; /* "delete" */
3054 case 'h': return TRUE; /* "hide" */
3055 }
3056 return (p_hid || cmdmod.hide);
3057}
3058
3059/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003060 * Return TRUE when using ":vimgrep" for ":grep".
3061 */
3062 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003063grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003064{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003065 return ((cmdidx == CMD_grep
3066 || cmdidx == CMD_lgrep
3067 || cmdidx == CMD_grepadd
3068 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003069 && STRCMP("internal",
3070 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3071}
3072
3073/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003074 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 */
3076 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003077ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003079 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 char_u *cmd;
3081 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003082 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003083 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003084 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003085#ifdef FEAT_AUTOCMD
3086 char_u *au_name = NULL;
3087
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003088 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3089 if (grep_internal(eap->cmdidx))
3090 {
3091 ex_vimgrep(eap);
3092 return;
3093 }
3094
Bram Moolenaar7c626922005-02-07 22:01:03 +00003095 switch (eap->cmdidx)
3096 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003097 case CMD_make: au_name = (char_u *)"make"; break;
3098 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3099 case CMD_grep: au_name = (char_u *)"grep"; break;
3100 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3101 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3102 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003103 default: break;
3104 }
3105 if (au_name != NULL)
3106 {
3107 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3108 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003109# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003110 if (did_throw || force_abort)
3111 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003112# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003113 }
3114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
Bram Moolenaara6557602006-02-04 22:43:20 +00003116 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3117 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003118 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003121 fname = get_mef_name();
3122 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003124 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
3126 /*
3127 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3128 */
3129 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3130 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003131 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 cmd = alloc(len);
3133 if (cmd == NULL)
3134 return;
3135 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3136 (char *)p_shq);
3137 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003138 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 /*
3140 * Output a newline if there's something else than the :make command that
3141 * was typed (in which case the cursor is in column 0).
3142 */
3143 if (msg_col == 0)
3144 msg_didout = FALSE;
3145 msg_start();
3146 MSG_PUTS(":!");
3147 msg_outtrans(cmd); /* show what we are doing */
3148
3149 /* let the shell know if we are redirecting output or not */
3150 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3151
3152#ifdef AMIGA
3153 out_flush();
3154 /* read window status report and redraw before message */
3155 (void)char_avail();
3156#endif
3157
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003158 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003159 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3160 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003161 && eap->cmdidx != CMD_lgrepadd),
3162 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003163 if (wp != NULL)
3164 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003165#ifdef FEAT_AUTOCMD
3166 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003167 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003168 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3169 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003170 if (qi->qf_curlist < qi->qf_listcount)
3171 res = qi->qf_lists[qi->qf_curlist].qf_count;
3172 else
3173 res = 0;
3174 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003175#endif
3176 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003177 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
Bram Moolenaar7c626922005-02-07 22:01:03 +00003179 mch_remove(fname);
3180 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 vim_free(cmd);
3182}
3183
3184/*
3185 * Return the name for the errorfile, in allocated memory.
3186 * Find a new unique name when 'makeef' contains "##".
3187 * Returns NULL for error.
3188 */
3189 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003190get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192 char_u *p;
3193 char_u *name;
3194 static int start = -1;
3195 static int off = 0;
3196#ifdef HAVE_LSTAT
3197 struct stat sb;
3198#endif
3199
3200 if (*p_mef == NUL)
3201 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003202 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (name == NULL)
3204 EMSG(_(e_notmp));
3205 return name;
3206 }
3207
3208 for (p = p_mef; *p; ++p)
3209 if (p[0] == '#' && p[1] == '#')
3210 break;
3211
3212 if (*p == NUL)
3213 return vim_strsave(p_mef);
3214
3215 /* Keep trying until the name doesn't exist yet. */
3216 for (;;)
3217 {
3218 if (start == -1)
3219 start = mch_get_pid();
3220 else
3221 off += 19;
3222
3223 name = alloc((unsigned)STRLEN(p_mef) + 30);
3224 if (name == NULL)
3225 break;
3226 STRCPY(name, p_mef);
3227 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3228 STRCAT(name, p + 2);
3229 if (mch_getperm(name) < 0
3230#ifdef HAVE_LSTAT
3231 /* Don't accept a symbolic link, its a security risk. */
3232 && mch_lstat((char *)name, &sb) < 0
3233#endif
3234 )
3235 break;
3236 vim_free(name);
3237 }
3238 return name;
3239}
3240
3241/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003242 * Returns the number of valid entries in the current quickfix/location list.
3243 */
3244 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003245qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003246{
3247 qf_info_T *qi = &ql_info;
3248 qfline_T *qfp;
3249 int i, sz = 0;
3250 int prev_fnum = 0;
3251
3252 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3253 {
3254 /* Location list */
3255 qi = GET_LOC_LIST(curwin);
3256 if (qi == NULL)
3257 return 0;
3258 }
3259
3260 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003261 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003262 ++i, qfp = qfp->qf_next)
3263 {
3264 if (qfp->qf_valid)
3265 {
3266 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3267 sz++; /* Count all valid entries */
3268 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3269 {
3270 /* Count the number of files */
3271 sz++;
3272 prev_fnum = qfp->qf_fnum;
3273 }
3274 }
3275 }
3276
3277 return sz;
3278}
3279
3280/*
3281 * Returns the current index of the quickfix/location list.
3282 * Returns 0 if there is an error.
3283 */
3284 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003285qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003286{
3287 qf_info_T *qi = &ql_info;
3288
3289 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3290 {
3291 /* Location list */
3292 qi = GET_LOC_LIST(curwin);
3293 if (qi == NULL)
3294 return 0;
3295 }
3296
3297 return qi->qf_lists[qi->qf_curlist].qf_index;
3298}
3299
3300/*
3301 * Returns the current index in the quickfix/location list (counting only valid
3302 * entries). If no valid entries are in the list, then returns 1.
3303 */
3304 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003305qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003306{
3307 qf_info_T *qi = &ql_info;
3308 qf_list_T *qfl;
3309 qfline_T *qfp;
3310 int i, eidx = 0;
3311 int prev_fnum = 0;
3312
3313 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3314 {
3315 /* Location list */
3316 qi = GET_LOC_LIST(curwin);
3317 if (qi == NULL)
3318 return 1;
3319 }
3320
3321 qfl = &qi->qf_lists[qi->qf_curlist];
3322 qfp = qfl->qf_start;
3323
3324 /* check if the list has valid errors */
3325 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3326 return 1;
3327
3328 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3329 {
3330 if (qfp->qf_valid)
3331 {
3332 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3333 {
3334 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3335 {
3336 /* Count the number of files */
3337 eidx++;
3338 prev_fnum = qfp->qf_fnum;
3339 }
3340 }
3341 else
3342 eidx++;
3343 }
3344 }
3345
3346 return eidx ? eidx : 1;
3347}
3348
3349/*
3350 * Get the 'n'th valid error entry in the quickfix or location list.
3351 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3352 * For :cdo and :ldo returns the 'n'th valid error entry.
3353 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3354 */
3355 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003356qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003357{
3358 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3359 qfline_T *qfp = qfl->qf_start;
3360 int i, eidx;
3361 int prev_fnum = 0;
3362
3363 /* check if the list has valid errors */
3364 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3365 return 1;
3366
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003367 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003368 i++, qfp = qfp->qf_next)
3369 {
3370 if (qfp->qf_valid)
3371 {
3372 if (fdo)
3373 {
3374 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3375 {
3376 /* Count the number of files */
3377 eidx++;
3378 prev_fnum = qfp->qf_fnum;
3379 }
3380 }
3381 else
3382 eidx++;
3383 }
3384
3385 if (eidx == n)
3386 break;
3387 }
3388
3389 if (i <= qfl->qf_count)
3390 return i;
3391 else
3392 return 1;
3393}
3394
3395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003397 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003398 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 */
3400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003401ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003403 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003404 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003405
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003406 if (eap->cmdidx == CMD_ll
3407 || eap->cmdidx == CMD_lrewind
3408 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003409 || eap->cmdidx == CMD_llast
3410 || eap->cmdidx == CMD_ldo
3411 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003412 {
3413 qi = GET_LOC_LIST(curwin);
3414 if (qi == NULL)
3415 {
3416 EMSG(_(e_loclist));
3417 return;
3418 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003419 }
3420
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003421 if (eap->addr_count > 0)
3422 errornr = (int)eap->line2;
3423 else
3424 {
3425 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3426 errornr = 0;
3427 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3428 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3429 errornr = 1;
3430 else
3431 errornr = 32767;
3432 }
3433
3434 /* For cdo and ldo commands, jump to the nth valid error.
3435 * For cfdo and lfdo commands, jump to the nth valid file entry.
3436 */
3437 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3438 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3439 errornr = qf_get_nth_valid_entry(qi,
3440 eap->addr_count > 0 ? (int)eap->line1 : 1,
3441 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3442
3443 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444}
3445
3446/*
3447 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003448 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003449 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 */
3451 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003452ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003454 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003455 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003456
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003457 if (eap->cmdidx == CMD_lnext
3458 || eap->cmdidx == CMD_lNext
3459 || eap->cmdidx == CMD_lprevious
3460 || eap->cmdidx == CMD_lnfile
3461 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003462 || eap->cmdidx == CMD_lpfile
3463 || eap->cmdidx == CMD_ldo
3464 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003465 {
3466 qi = GET_LOC_LIST(curwin);
3467 if (qi == NULL)
3468 {
3469 EMSG(_(e_loclist));
3470 return;
3471 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003472 }
3473
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003474 if (eap->addr_count > 0 &&
3475 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3476 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3477 errornr = (int)eap->line2;
3478 else
3479 errornr = 1;
3480
3481 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3482 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003484 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3485 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003487 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3488 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 ? BACKWARD_FILE
3490 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003491 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492}
3493
3494/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003495 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003496 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
3498 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003499ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003501 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003502 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003503#ifdef FEAT_AUTOCMD
3504 char_u *au_name = NULL;
3505#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003506
3507 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003508 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003509 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003510
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003511#ifdef FEAT_AUTOCMD
3512 switch (eap->cmdidx)
3513 {
3514 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3515 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3516 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3517 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3518 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3519 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3520 default: break;
3521 }
3522 if (au_name != NULL)
3523 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3524#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003525#ifdef FEAT_BROWSE
3526 if (cmdmod.browse)
3527 {
3528 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3529 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3530 if (browse_file == NULL)
3531 return;
3532 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3533 vim_free(browse_file);
3534 }
3535 else
3536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003538 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003539
3540 /*
3541 * This function is used by the :cfile, :cgetfile and :caddfile
3542 * commands.
3543 * :cfile always creates a new quickfix list and jumps to the
3544 * first error.
3545 * :cgetfile creates a new quickfix list but doesn't jump to the
3546 * first error.
3547 * :caddfile adds to an existing quickfix list. If there is no
3548 * quickfix list then a new list is created.
3549 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003550 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003551 && eap->cmdidx != CMD_laddfile),
3552 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003553 && (eap->cmdidx == CMD_cfile
3554 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003555 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003556#ifdef FEAT_AUTOCMD
3557 if (au_name != NULL)
3558 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3559#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003560 if (wp != NULL)
3561 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003562 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003563 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003564
3565 else
3566 {
3567#ifdef FEAT_AUTOCMD
3568 if (au_name != NULL)
3569 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3570#endif
3571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572}
3573
3574/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003575 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003576 * ":vimgrepadd {pattern} file(s)"
3577 * ":lvimgrep {pattern} file(s)"
3578 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003579 */
3580 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003581ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003582{
Bram Moolenaar81695252004-12-29 20:58:21 +00003583 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003584 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003585 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003586 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003587 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003588 char_u *s;
3589 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003590 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003591 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003592#ifdef FEAT_AUTOCMD
3593 qfline_T *cur_qf_start;
3594#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003595 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003596 buf_T *buf;
3597 int duplicate_name = FALSE;
3598 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003599 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003600 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003601 buf_T *first_match_buf = NULL;
3602 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003603 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003604#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3605 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003606#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003607 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003608 int flags = 0;
3609 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003610 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003611 char_u *dirname_start = NULL;
3612 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003613 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003614#ifdef FEAT_AUTOCMD
3615 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003616
3617 switch (eap->cmdidx)
3618 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003619 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3620 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3621 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003622 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003623 case CMD_grep: au_name = (char_u *)"grep"; break;
3624 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3625 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3626 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003627 default: break;
3628 }
3629 if (au_name != NULL)
3630 {
3631 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3632 curbuf->b_fname, TRUE, curbuf);
3633 if (did_throw || force_abort)
3634 return;
3635 }
3636#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003637
Bram Moolenaar754b5602006-02-09 23:53:20 +00003638 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003639 || eap->cmdidx == CMD_lvimgrep
3640 || eap->cmdidx == CMD_lgrepadd
3641 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003642 {
3643 qi = ll_get_or_alloc_list(curwin);
3644 if (qi == NULL)
3645 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003646 }
3647
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003648 if (eap->addr_count > 0)
3649 tomatch = eap->line2;
3650 else
3651 tomatch = MAXLNUM;
3652
Bram Moolenaar81695252004-12-29 20:58:21 +00003653 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003654 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003655 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003656 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003657 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003658 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003659 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003660 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003661 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003662
3663 if (s != NULL && *s == NUL)
3664 {
3665 /* Pattern is empty, use last search pattern. */
3666 if (last_search_pat() == NULL)
3667 {
3668 EMSG(_(e_noprevre));
3669 goto theend;
3670 }
3671 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3672 }
3673 else
3674 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3675
Bram Moolenaar86b68352004-12-27 21:59:20 +00003676 if (regmatch.regprog == NULL)
3677 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003678 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003679 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003680
3681 p = skipwhite(p);
3682 if (*p == NUL)
3683 {
3684 EMSG(_("E683: File name missing or invalid pattern"));
3685 goto theend;
3686 }
3687
Bram Moolenaar754b5602006-02-09 23:53:20 +00003688 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003689 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003690 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003691 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003692 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003693
3694 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003695 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003696 goto theend;
3697 if (fcount == 0)
3698 {
3699 EMSG(_(e_nomatch));
3700 goto theend;
3701 }
3702
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003703 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3704 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003705 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003706 {
3707 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003708 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003709 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003710
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003711 /* Remember the current directory, because a BufRead autocommand that does
3712 * ":lcd %:p:h" changes the meaning of short path names. */
3713 mch_dirname(dirname_start, MAXPATHL);
3714
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003715#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003716 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003717 * changing the current quickfix list. */
3718 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3719#endif
3720
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003721 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003722 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003723 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003724 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003725 if (time(NULL) > seconds)
3726 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003727 /* Display the file name every second or so, show the user we are
3728 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003729 seconds = time(NULL);
3730 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003731 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003732 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003733 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003734 else
3735 {
3736 msg_outtrans(p);
3737 vim_free(p);
3738 }
3739 msg_clr_eos();
3740 msg_didout = FALSE; /* overwrite this message */
3741 msg_nowait = TRUE; /* don't wait for this message */
3742 msg_col = 0;
3743 out_flush();
3744 }
3745
Bram Moolenaar81695252004-12-29 20:58:21 +00003746 buf = buflist_findname_exp(fnames[fi]);
3747 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3748 {
3749 /* Remember that a buffer with this name already exists. */
3750 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003751 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003752 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003753
3754#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3755 /* Don't do Filetype autocommands to avoid loading syntax and
3756 * indent scripts, a great speed improvement. */
3757 save_ei = au_event_disable(",Filetype");
3758#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003759 /* Don't use modelines here, it's useless. */
3760 save_mls = p_mls;
3761 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003762
3763 /* Load file into a buffer, so that 'fileencoding' is detected,
3764 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003765 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003766
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003767 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003768#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3769 au_event_restore(save_ei);
3770#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003771 }
3772 else
3773 /* Use existing, loaded buffer. */
3774 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003775
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003776#ifdef FEAT_AUTOCMD
3777 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3778 {
3779 int idx;
3780
3781 /* Autocommands changed the quickfix list. Find the one we were
3782 * using and restore it. */
3783 for (idx = 0; idx < LISTCOUNT; ++idx)
3784 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3785 {
3786 qi->qf_curlist = idx;
3787 break;
3788 }
3789 if (idx == LISTCOUNT)
3790 {
3791 /* List cannot be found, create a new one. */
3792 qf_new_list(qi, *eap->cmdlinep);
3793 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3794 }
3795 }
3796#endif
3797
Bram Moolenaar81695252004-12-29 20:58:21 +00003798 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003799 {
3800 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003801 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003802 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003803 else
3804 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003805 /* Try for a match in all lines of the buffer.
3806 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003807 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003808 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3809 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003810 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003811 col = 0;
3812 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003813 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003814 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003815 ;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003816 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003817 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003818 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003819 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003820 ml_get_buf(buf,
3821 regmatch.startpos[0].lnum + lnum, FALSE),
3822 regmatch.startpos[0].lnum + lnum,
3823 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003824 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003825 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003826 0, /* nr */
3827 0, /* type */
3828 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003829 ) == FAIL)
3830 {
3831 got_int = TRUE;
3832 break;
3833 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003834 found_match = TRUE;
3835 if (--tomatch == 0)
3836 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003837 if ((flags & VGR_GLOBAL) == 0
3838 || regmatch.endpos[0].lnum > 0)
3839 break;
3840 col = regmatch.endpos[0].col
3841 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003842 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003843 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003844 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003845 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003846 if (got_int)
3847 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003848 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003849#ifdef FEAT_AUTOCMD
3850 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3851#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003852
3853 if (using_dummy)
3854 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003855 if (found_match && first_match_buf == NULL)
3856 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003857 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003858 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003859 /* Never keep a dummy buffer if there is another buffer
3860 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003861 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003862 buf = NULL;
3863 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003864 else if (!cmdmod.hide
3865 || buf->b_p_bh[0] == 'u' /* "unload" */
3866 || buf->b_p_bh[0] == 'w' /* "wipe" */
3867 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003868 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003869 /* When no match was found we don't need to remember the
3870 * buffer, wipe it out. If there was a match and it
3871 * wasn't the first one or we won't jump there: only
3872 * unload the buffer.
3873 * Ignore 'hidden' here, because it may lead to having too
3874 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003875 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003876 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003877 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003878 buf = NULL;
3879 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003880 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003881 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003882 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003883 buf = NULL;
3884 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003885 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003886
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003887 if (buf != NULL)
3888 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003889 /* If the buffer is still loaded we need to use the
3890 * directory we jumped to below. */
3891 if (buf == first_match_buf
3892 && target_dir == NULL
3893 && STRCMP(dirname_start, dirname_now) != 0)
3894 target_dir = vim_strsave(dirname_now);
3895
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003896 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003897 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003898 * need to be done (again). But not the window-local
3899 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003900 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003901#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003902 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3903 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003904#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003905 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003906 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003907 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003908 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003909 }
3910 }
3911
3912 FreeWild(fcount, fnames);
3913
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003914 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3915 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3916 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003917
3918#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02003919 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003920#endif
3921
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003922#ifdef FEAT_AUTOCMD
3923 if (au_name != NULL)
3924 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3925 curbuf->b_fname, TRUE, curbuf);
3926#endif
3927
Bram Moolenaar86b68352004-12-27 21:59:20 +00003928 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003929 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003930 {
3931 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003932 {
3933 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003934 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003935 if (buf != curbuf)
3936 /* If we jumped to another buffer redrawing will already be
3937 * taken care of. */
3938 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003939
3940 /* Jump to the directory used after loading the buffer. */
3941 if (curbuf == first_match_buf && target_dir != NULL)
3942 {
3943 exarg_T ea;
3944
3945 ea.arg = target_dir;
3946 ea.cmdidx = CMD_lcd;
3947 ex_cd(&ea);
3948 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003949 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003950 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003951 else
3952 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003953
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003954 /* If we loaded a dummy buffer into the current window, the autocommands
3955 * may have messed up things, need to redraw and recompute folds. */
3956 if (redraw_for_dummy)
3957 {
3958#ifdef FEAT_FOLDING
3959 foldUpdateAll(curwin);
3960#else
3961 redraw_later(NOT_VALID);
3962#endif
3963 }
3964
Bram Moolenaar86b68352004-12-27 21:59:20 +00003965theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01003966 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003967 vim_free(dirname_now);
3968 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003969 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003970 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003971}
3972
3973/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003974 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003975 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003976 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3977 * If "s" is not NULL terminate the pattern with a NUL.
3978 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003979 */
3980 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003981skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003982{
3983 int c;
3984
3985 if (vim_isIDc(*p))
3986 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003987 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003988 if (s != NULL)
3989 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003990 p = skiptowhite(p);
3991 if (s != NULL && *p != NUL)
3992 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003993 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003994 else
3995 {
3996 /* ":vimgrep /pattern/[g][j] fname" */
3997 if (s != NULL)
3998 *s = p + 1;
3999 c = *p;
4000 p = skip_regexp(p + 1, c, TRUE, NULL);
4001 if (*p != c)
4002 return NULL;
4003
4004 /* Truncate the pattern. */
4005 if (s != NULL)
4006 *p = NUL;
4007 ++p;
4008
4009 /* Find the flags */
4010 while (*p == 'g' || *p == 'j')
4011 {
4012 if (flags != NULL)
4013 {
4014 if (*p == 'g')
4015 *flags |= VGR_GLOBAL;
4016 else
4017 *flags |= VGR_NOJUMP;
4018 }
4019 ++p;
4020 }
4021 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004022 return p;
4023}
4024
4025/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004026 * Restore current working directory to "dirname_start" if they differ, taking
4027 * into account whether it is set locally or globally.
4028 */
4029 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004030restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004031{
4032 char_u *dirname_now = alloc(MAXPATHL);
4033
4034 if (NULL != dirname_now)
4035 {
4036 mch_dirname(dirname_now, MAXPATHL);
4037 if (STRCMP(dirname_start, dirname_now) != 0)
4038 {
4039 /* If the directory has changed, change it back by building up an
4040 * appropriate ex command and executing it. */
4041 exarg_T ea;
4042
4043 ea.arg = dirname_start;
4044 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4045 ex_cd(&ea);
4046 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004047 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004048 }
4049}
4050
4051/*
4052 * Load file "fname" into a dummy buffer and return the buffer pointer,
4053 * placing the directory resulting from the buffer load into the
4054 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4055 * prior to calling this function. Restores directory to "dirname_start" prior
4056 * to returning, if autocmds or the 'autochdir' option have changed it.
4057 *
4058 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4059 * or wipe_dummy_buffer() later!
4060 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004061 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004062 */
4063 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004064load_dummy_buffer(
4065 char_u *fname,
4066 char_u *dirname_start, /* in: old directory */
4067 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004068{
4069 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004070 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004071 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004072 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004073
4074 /* Allocate a buffer without putting it in the buffer list. */
4075 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4076 if (newbuf == NULL)
4077 return NULL;
4078
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004079 /* Init the options. */
4080 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4081
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004082 /* need to open the memfile before putting the buffer in a window */
4083 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004084 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004085 /* set curwin/curbuf to buf and save a few things */
4086 aucmd_prepbuf(&aco, newbuf);
4087
4088 /* Need to set the filename for autocommands. */
4089 (void)setfname(curbuf, fname, NULL, FALSE);
4090
Bram Moolenaar81695252004-12-29 20:58:21 +00004091 /* Create swap file now to avoid the ATTENTION message. */
4092 check_need_swap(TRUE);
4093
4094 /* Remove the "dummy" flag, otherwise autocommands may not
4095 * work. */
4096 curbuf->b_flags &= ~BF_DUMMY;
4097
4098 if (readfile(fname, NULL,
4099 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4100 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004101 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004102 && !(curbuf->b_flags & BF_NEW))
4103 {
4104 failed = FALSE;
4105 if (curbuf != newbuf)
4106 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004107 /* Bloody autocommands changed the buffer! Can happen when
4108 * using netrw and editing a remote file. Use the current
4109 * buffer instead, delete the dummy one after restoring the
4110 * window stuff. */
4111 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004112 newbuf = curbuf;
4113 }
4114 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004115
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004116 /* restore curwin/curbuf and a few other things */
4117 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004118 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
4119 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004120 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004121
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004122 /*
4123 * When autocommands/'autochdir' option changed directory: go back.
4124 * Let the caller know what the resulting dir was first, in case it is
4125 * important.
4126 */
4127 mch_dirname(resulting_dir, MAXPATHL);
4128 restore_start_dir(dirname_start);
4129
Bram Moolenaar81695252004-12-29 20:58:21 +00004130 if (!buf_valid(newbuf))
4131 return NULL;
4132 if (failed)
4133 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004134 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004135 return NULL;
4136 }
4137 return newbuf;
4138}
4139
4140/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004141 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4142 * directory to "dirname_start" prior to returning, if autocmds or the
4143 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004144 */
4145 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004146wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004147{
4148 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004149 {
4150#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4151 cleanup_T cs;
4152
4153 /* Reset the error/interrupt/exception state here so that aborting()
4154 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4155 * work when got_int is set. */
4156 enter_cleanup(&cs);
4157#endif
4158
Bram Moolenaar81695252004-12-29 20:58:21 +00004159 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004160
4161#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4162 /* Restore the error/interrupt/exception state if not discarded by a
4163 * new aborting error, interrupt, or uncaught exception. */
4164 leave_cleanup(&cs);
4165#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004166 /* When autocommands/'autochdir' option changed directory: go back. */
4167 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004168 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004169}
4170
4171/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004172 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4173 * directory to "dirname_start" prior to returning, if autocmds or the
4174 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004175 */
4176 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004177unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004178{
4179 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004180 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004181 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004182
4183 /* When autocommands/'autochdir' option changed directory: go back. */
4184 restore_start_dir(dirname_start);
4185 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004186}
4187
Bram Moolenaar05159a02005-02-26 23:04:13 +00004188#if defined(FEAT_EVAL) || defined(PROTO)
4189/*
4190 * Add each quickfix error to list "list" as a dictionary.
4191 */
4192 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004193get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004194{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004195 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004196 dict_T *dict;
4197 char_u buf[2];
4198 qfline_T *qfp;
4199 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004200 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004201
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004202 if (wp != NULL)
4203 {
4204 qi = GET_LOC_LIST(wp);
4205 if (qi == NULL)
4206 return FAIL;
4207 }
4208
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004209 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004210 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004211 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004212
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004213 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4214 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004215 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004216 /* Handle entries with a non-existing buffer number. */
4217 bufnum = qfp->qf_fnum;
4218 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4219 bufnum = 0;
4220
Bram Moolenaar05159a02005-02-26 23:04:13 +00004221 if ((dict = dict_alloc()) == NULL)
4222 return FAIL;
4223 if (list_append_dict(list, dict) == FAIL)
4224 return FAIL;
4225
4226 buf[0] = qfp->qf_type;
4227 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004228 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004229 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4230 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4231 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4232 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004233 || dict_add_nr_str(dict, "pattern", 0L,
4234 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4235 || dict_add_nr_str(dict, "text", 0L,
4236 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004237 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4238 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4239 return FAIL;
4240
4241 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004242 if (qfp == NULL)
4243 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004244 }
4245 return OK;
4246}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004247
4248/*
4249 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004250 * of dictionaries. "title" will be copied to w:quickfix_title.
4251 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004252 */
4253 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004254set_errorlist(
4255 win_T *wp,
4256 list_T *list,
4257 int action,
4258 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004259{
4260 listitem_T *li;
4261 dict_T *d;
4262 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004263 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004264 long lnum;
4265 int col, nr;
4266 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004267#ifdef FEAT_WINDOWS
4268 qfline_T *old_last = NULL;
4269#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004270 int valid, status;
4271 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004272 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004273 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004274
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004275 if (wp != NULL)
4276 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004277 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004278 if (qi == NULL)
4279 return FAIL;
4280 }
4281
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004282 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004283 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004284 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004285#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004286 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4287 /* Adding to existing list, use last entry. */
4288 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004289#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004290 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004291 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004292 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004293 qf_store_title(qi, title);
4294 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004295
4296 for (li = list->lv_first; li != NULL; li = li->li_next)
4297 {
4298 if (li->li_tv.v_type != VAR_DICT)
4299 continue; /* Skip non-dict items */
4300
4301 d = li->li_tv.vval.v_dict;
4302 if (d == NULL)
4303 continue;
4304
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004305 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004306 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004307 lnum = get_dict_number(d, (char_u *)"lnum");
4308 col = get_dict_number(d, (char_u *)"col");
4309 vcol = get_dict_number(d, (char_u *)"vcol");
4310 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004311 type = get_dict_string(d, (char_u *)"type", TRUE);
4312 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4313 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004314 if (text == NULL)
4315 text = vim_strsave((char_u *)"");
4316
4317 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004318 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004319 valid = FALSE;
4320
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004321 /* Mark entries with non-existing buffer number as not valid. Give the
4322 * error message only once. */
4323 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4324 {
4325 if (!did_bufnr_emsg)
4326 {
4327 did_bufnr_emsg = TRUE;
4328 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4329 }
4330 valid = FALSE;
4331 bufnum = 0;
4332 }
4333
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004334 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004335 NULL, /* dir */
4336 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004337 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004338 text,
4339 lnum,
4340 col,
4341 vcol, /* vis_col */
4342 pattern, /* search pattern */
4343 nr,
4344 type == NULL ? NUL : *type,
4345 valid);
4346
4347 vim_free(filename);
4348 vim_free(pattern);
4349 vim_free(text);
4350 vim_free(type);
4351
4352 if (status == FAIL)
4353 {
4354 retval = FAIL;
4355 break;
4356 }
4357 }
4358
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004359 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004360 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004361 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4362 else
4363 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004364 if (action != 'a') {
4365 qi->qf_lists[qi->qf_curlist].qf_ptr =
4366 qi->qf_lists[qi->qf_curlist].qf_start;
4367 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4368 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4369 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004370
4371#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004372 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004373 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004374#endif
4375
4376 return retval;
4377}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004378#endif
4379
Bram Moolenaar81695252004-12-29 20:58:21 +00004380/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004381 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004382 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004383 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004384 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004385 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004386 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004387 */
4388 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004389ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004390{
4391 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004392 qf_info_T *qi = &ql_info;
4393
Bram Moolenaardb552d602006-03-23 22:59:57 +00004394 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4395 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004396 {
4397 qi = ll_get_or_alloc_list(curwin);
4398 if (qi == NULL)
4399 return;
4400 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004401
4402 if (*eap->arg == NUL)
4403 buf = curbuf;
4404 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4405 buf = buflist_findnr(atoi((char *)eap->arg));
4406 if (buf == NULL)
4407 EMSG(_(e_invarg));
4408 else if (buf->b_ml.ml_mfp == NULL)
4409 EMSG(_("E681: Buffer is not loaded"));
4410 else
4411 {
4412 if (eap->addr_count == 0)
4413 {
4414 eap->line1 = 1;
4415 eap->line2 = buf->b_ml.ml_line_count;
4416 }
4417 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4418 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4419 EMSG(_(e_invrange));
4420 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004421 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004422 char_u *qf_title = *eap->cmdlinep;
4423
4424 if (buf->b_sfname)
4425 {
4426 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4427 (char *)qf_title, (char *)buf->b_sfname);
4428 qf_title = IObuff;
4429 }
4430
Bram Moolenaardb552d602006-03-23 22:59:57 +00004431 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4432 (eap->cmdidx != CMD_caddbuffer
4433 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004434 eap->line1, eap->line2,
4435 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004436 && (eap->cmdidx == CMD_cbuffer
4437 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004438 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4439 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004440 }
4441}
4442
Bram Moolenaar1e015462005-09-25 22:16:38 +00004443#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004444/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004445 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4446 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004447 */
4448 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004449ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004450{
4451 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004452 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004453
Bram Moolenaardb552d602006-03-23 22:59:57 +00004454 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4455 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004456 {
4457 qi = ll_get_or_alloc_list(curwin);
4458 if (qi == NULL)
4459 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004460 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004461
Bram Moolenaar4770d092006-01-12 23:22:24 +00004462 /* Evaluate the expression. When the result is a string or a list we can
4463 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004464 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004465 if (tv != NULL)
4466 {
4467 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4468 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4469 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004470 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004471 (eap->cmdidx != CMD_caddexpr
4472 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004473 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004474 && (eap->cmdidx == CMD_cexpr
4475 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004476 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004477 }
4478 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004479 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004480 free_tv(tv);
4481 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004482}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004483#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004484
4485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 * ":helpgrep {pattern}"
4487 */
4488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004489ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490{
4491 regmatch_T regmatch;
4492 char_u *save_cpo;
4493 char_u *p;
4494 int fcount;
4495 char_u **fnames;
4496 FILE *fd;
4497 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004499#ifdef FEAT_MULTI_LANG
4500 char_u *lang;
4501#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004502 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004503 int new_qi = FALSE;
4504 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004505#ifdef FEAT_AUTOCMD
4506 char_u *au_name = NULL;
4507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004509#ifdef FEAT_MULTI_LANG
4510 /* Check for a specified language */
4511 lang = check_help_lang(eap->arg);
4512#endif
4513
Bram Moolenaar73633f82012-01-20 13:39:07 +01004514#ifdef FEAT_AUTOCMD
4515 switch (eap->cmdidx)
4516 {
4517 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4518 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4519 default: break;
4520 }
4521 if (au_name != NULL)
4522 {
4523 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4524 curbuf->b_fname, TRUE, curbuf);
4525 if (did_throw || force_abort)
4526 return;
4527 }
4528#endif
4529
4530 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4531 save_cpo = p_cpo;
4532 p_cpo = empty_option;
4533
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004534 if (eap->cmdidx == CMD_lhelpgrep)
4535 {
4536 /* Find an existing help window */
4537 FOR_ALL_WINDOWS(wp)
4538 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4539 break;
4540
4541 if (wp == NULL) /* Help window not found */
4542 qi = NULL;
4543 else
4544 qi = wp->w_llist;
4545
4546 if (qi == NULL)
4547 {
4548 /* Allocate a new location list for help text matches */
4549 if ((qi = ll_new_list()) == NULL)
4550 return;
4551 new_qi = TRUE;
4552 }
4553 }
4554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4556 regmatch.rm_ic = FALSE;
4557 if (regmatch.regprog != NULL)
4558 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004559#ifdef FEAT_MBYTE
4560 vimconv_T vc;
4561
4562 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4563 * differs. */
4564 vc.vc_type = CONV_NONE;
4565 if (!enc_utf8)
4566 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4567#endif
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004570 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
4572 /* Go through all directories in 'runtimepath' */
4573 p = p_rtp;
4574 while (*p != NUL && !got_int)
4575 {
4576 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4577
4578 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4579 add_pathsep(NameBuff);
4580 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4581 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4582 &fnames, EW_FILE|EW_SILENT) == OK
4583 && fcount > 0)
4584 {
4585 for (fi = 0; fi < fcount && !got_int; ++fi)
4586 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004587#ifdef FEAT_MULTI_LANG
4588 /* Skip files for a different language. */
4589 if (lang != NULL
4590 && STRNICMP(lang, fnames[fi]
4591 + STRLEN(fnames[fi]) - 3, 2) != 0
4592 && !(STRNICMP(lang, "en", 2) == 0
4593 && STRNICMP("txt", fnames[fi]
4594 + STRLEN(fnames[fi]) - 3, 3) == 0))
4595 continue;
4596#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004597 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (fd != NULL)
4599 {
4600 lnum = 1;
4601 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4602 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004603 char_u *line = IObuff;
4604#ifdef FEAT_MBYTE
4605 /* Convert a line if 'encoding' is not utf-8 and
4606 * the line contains a non-ASCII character. */
4607 if (vc.vc_type != CONV_NONE
4608 && has_non_ascii(IObuff)) {
4609 line = string_convert(&vc, IObuff, NULL);
4610 if (line == NULL)
4611 line = IObuff;
4612 }
4613#endif
4614
4615 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004617 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004620 while (l > 0 && line[l - 1] <= ' ')
4621 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004623 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 NULL, /* dir */
4625 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004626 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004627 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004629 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004630 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004631 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004632 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 0, /* nr */
4634 1, /* type */
4635 TRUE /* valid */
4636 ) == FAIL)
4637 {
4638 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004639#ifdef FEAT_MBYTE
4640 if (line != IObuff)
4641 vim_free(line);
4642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 break;
4644 }
4645 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004646#ifdef FEAT_MBYTE
4647 if (line != IObuff)
4648 vim_free(line);
4649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 ++lnum;
4651 line_breakcheck();
4652 }
4653 fclose(fd);
4654 }
4655 }
4656 FreeWild(fcount, fnames);
4657 }
4658 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004659
Bram Moolenaar473de612013-06-08 18:19:48 +02004660 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004661#ifdef FEAT_MBYTE
4662 if (vc.vc_type != CONV_NONE)
4663 convert_setup(&vc, NULL, NULL);
4664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004666 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4667 qi->qf_lists[qi->qf_curlist].qf_ptr =
4668 qi->qf_lists[qi->qf_curlist].qf_start;
4669 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
4671
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004672 if (p_cpo == empty_option)
4673 p_cpo = save_cpo;
4674 else
4675 /* Darn, some plugin changed the value. */
4676 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004679 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680#endif
4681
Bram Moolenaar73633f82012-01-20 13:39:07 +01004682#ifdef FEAT_AUTOCMD
4683 if (au_name != NULL)
4684 {
4685 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4686 curbuf->b_fname, TRUE, curbuf);
4687 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4688 /* autocommands made "qi" invalid */
4689 return;
4690 }
4691#endif
4692
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004694 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004695 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004696 else
4697 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004698
4699 if (eap->cmdidx == CMD_lhelpgrep)
4700 {
4701 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004702 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004703 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4704 {
4705 if (new_qi)
4706 ll_free_all(&qi);
4707 }
4708 else if (curwin->w_llist == NULL)
4709 curwin->w_llist = qi;
4710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711}
4712
4713#endif /* FEAT_QUICKFIX */