blob: b72f7d1f681e0e70e30296b2b83966a13430b42e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
24static struct dir_stack_T *dir_stack = NULL;
25
26/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000029typedef struct qfline_S qfline_T;
30struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000031{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000032 qfline_T *qf_next; /* pointer to next error in the list */
33 qfline_T *qf_prev; /* pointer to previous error in the list */
34 linenr_T qf_lnum; /* line number where the error occurred */
35 int qf_fnum; /* file number for the line */
36 int qf_col; /* column where the error occurred */
37 int qf_nr; /* error number */
38 char_u *qf_pattern; /* search pattern for the error */
39 char_u *qf_text; /* description of the error */
40 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
41 char_u qf_cleared; /* set to TRUE if line has been deleted */
42 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000044 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
47/*
48 * There is a stack of error lists.
49 */
50#define LISTCOUNT 10
51
Bram Moolenaard12f5c12006-01-25 22:10:52 +000052typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020055 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000056 qfline_T *qf_ptr; /* pointer to the current error */
57 int qf_count; /* number of errors (0 means no error list) */
58 int qf_index; /* current index in the error list */
59 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020060 char_u *qf_title; /* title derived from the command that created
61 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000062} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
Bram Moolenaard12f5c12006-01-25 22:10:52 +000064struct qf_info_S
65{
66 /*
67 * Count of references to this list. Used only for location lists.
68 * When a location list window reference this list, qf_refcount
69 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
70 * reaches 0, the list is freed.
71 */
72 int qf_refcount;
73 int qf_listcount; /* current number of lists */
74 int qf_curlist; /* current error list */
75 qf_list_T qf_lists[LISTCOUNT];
76};
77
78static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaar68b76a62005-03-25 21:53:48 +000080#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
82/*
83 * Structure used to hold the info of one part of 'errorformat'
84 */
Bram Moolenaar01265852006-03-20 21:50:15 +000085typedef struct efm_S efm_T;
86struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000087{
88 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000089 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
91 char_u prefix; /* prefix of this format line: */
92 /* 'D' enter directory */
93 /* 'X' leave directory */
94 /* 'A' start of multi-line message */
95 /* 'E' error message */
96 /* 'W' warning message */
97 /* 'I' informational message */
98 /* 'C' continuation line */
99 /* 'Z' end of multi-line message */
100 /* 'G' general, unspecific message */
101 /* 'P' push file (partial) message */
102 /* 'Q' pop/quit file (partial) message */
103 /* 'O' overread (partial) message */
104 char_u flags; /* additional flags given in prefix */
105 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000106 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000107 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108};
109
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100110static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title);
111static void qf_store_title(qf_info_T *qi, char_u *title);
112static void qf_new_list(qf_info_T *qi, char_u *qf_title);
113static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200114static int qf_add_entry(qf_info_T *qi, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static qf_info_T *ll_new_list(void);
116static void qf_msg(qf_info_T *qi);
117static void qf_free(qf_info_T *qi, int idx);
118static char_u *qf_types(int, int);
119static int qf_get_fnum(char_u *, char_u *);
120static char_u *qf_push_dir(char_u *, struct dir_stack_T **);
121static char_u *qf_pop_dir(struct dir_stack_T **);
122static char_u *qf_guess_filepath(char_u *);
123static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
124static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
127static int is_qf_win(win_T *win, qf_info_T *qi);
128static win_T *qf_find_win(qf_info_T *qi);
129static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200130static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200132static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static char_u *get_mef_name(void);
135static void restore_start_dir(char_u *dirname_start);
136static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
137static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
138static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
139static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000141/* Quickfix window check helper macro */
142#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
143/* Location list window check helper macro */
144#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
145/*
146 * Return location list for window 'wp'
147 * For location list window, return the referenced location list
148 */
149#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000152 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200153 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 * Return -1 for error, number of errors for success.
155 */
156 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100157qf_init(
158 win_T *wp,
159 char_u *efile,
160 char_u *errorformat,
161 int newlist, /* TRUE: start a new error list */
162 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164 qf_info_T *qi = &ql_info;
165
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000166 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000167 {
168 qi = ll_get_or_alloc_list(wp);
169 if (qi == NULL)
170 return FAIL;
171 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172
173 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200174 (linenr_T)0, (linenr_T)0,
175 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000176}
177
178/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200179 * Maximum number of bytes allowed per line while reading a errorfile.
180 */
181#define LINE_MAXLEN 4096
182
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200183 static char_u *
184qf_grow_linebuf(char_u **growbuf, int *growbufsiz, int newsz, int *allocsz)
185{
186 /*
187 * If the line exceeds LINE_MAXLEN exclude the last
188 * byte since it's not a NL character.
189 */
190 *allocsz = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
191 if (*growbuf == NULL)
192 {
193 *growbuf = alloc(*allocsz + 1);
194 if (*growbuf == NULL)
195 return NULL;
196 *growbufsiz = *allocsz;
197 }
198 else if (*allocsz > *growbufsiz)
199 {
200 *growbuf = vim_realloc(*growbuf, *allocsz + 1);
201 if (*growbuf == NULL)
202 return NULL;
203 *growbufsiz = *allocsz;
204 }
205 return *growbuf;
206}
207
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200208/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000209 * Read the errorfile "efile" into memory, line by line, building the error
210 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +0200211 * Alternative: when "efile" is NULL read errors from buffer "buf".
212 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000213 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200214 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
215 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000216 * Return -1 for error, number of errors for success.
217 */
218 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100219qf_init_ext(
220 qf_info_T *qi,
221 char_u *efile,
222 buf_T *buf,
223 typval_T *tv,
224 char_u *errorformat,
225 int newlist, /* TRUE: start a new error list */
226 linenr_T lnumfirst, /* first line number to use */
227 linenr_T lnumlast, /* last line number to use */
228 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000229{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 char_u *namebuf;
231 char_u *errmsg;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200232 int errmsglen;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000233 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 char_u *fmtstr = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200235 char_u *growbuf = NULL;
236 int growbuflen;
Bram Moolenaar9a3b3312016-05-01 20:20:49 +0200237 int growbufsiz = 0;
238 char_u *linebuf = NULL;
239 int linelen = 0;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200240 int discard;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000242 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 int type = 0;
244 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000245 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 long lnum = 0L;
247 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000248 FILE *fd = NULL;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200249#ifdef FEAT_WINDOWS
250 qfline_T *old_last = NULL;
251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000253 efm_T *fmt_first = NULL;
254 efm_T *fmt_last = NULL;
255 efm_T *fmt_ptr;
256 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 char_u *efm;
258 char_u *ptr;
259 char_u *srcptr;
260 int len;
261 int i;
262 int round;
263 int idx = 0;
264 int multiline = FALSE;
265 int multiignore = FALSE;
266 int multiscan = FALSE;
267 int retval = -1; /* default: return error flag */
268 char_u *directory = NULL;
269 char_u *currfile = NULL;
270 char_u *tail = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200271 char_u *p_buf = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000272 char_u *p_str = NULL;
273 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 struct dir_stack_T *file_stack = NULL;
275 regmatch_T regmatch;
276 static struct fmtpattern
277 {
278 char_u convchar;
279 char *pattern;
280 } fmt_pat[FMT_PATTERNS] =
281 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000282 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 {'n', "\\d\\+"},
284 {'l', "\\d\\+"},
285 {'c', "\\d\\+"},
286 {'t', "."},
287 {'m', ".\\+"},
288 {'r', ".*"},
Bram Moolenaarf13de072012-06-01 18:34:41 +0200289 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000290 {'v', "\\d\\+"},
291 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 };
293
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100294 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200295 errmsglen = CMDBUFFSIZE + 1;
296 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100297 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000298 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 goto qf_init_end;
300
Bram Moolenaar86b68352004-12-27 21:59:20 +0000301 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 {
303 EMSG2(_(e_openerrf), efile);
304 goto qf_init_end;
305 }
306
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000307 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100309 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200310#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000311 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +0200312 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200313 /* Adding to existing list, use last entry. */
314 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200315 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
318/*
319 * Each part of the format string is copied and modified from errorformat to
320 * regex prog. Only a few % characters are allowed.
321 */
322 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000323 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000324 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 else
326 efm = errorformat;
327 /*
328 * Get some space to modify the format string into.
329 */
330 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
331 for (round = FMT_PATTERNS; round > 0; )
332 i += (int)STRLEN(fmt_pat[--round].pattern);
333#ifdef COLON_IN_FILENAME
334 i += 12; /* "%f" can become twelve chars longer */
335#else
336 i += 2; /* "%f" can become two chars longer */
337#endif
338 if ((fmtstr = alloc(i)) == NULL)
339 goto error2;
340
Bram Moolenaar01265852006-03-20 21:50:15 +0000341 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
343 /*
344 * Allocate a new eformat structure and put it at the end of the list
345 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000346 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (fmt_ptr == NULL)
348 goto error2;
349 if (fmt_first == NULL) /* first one */
350 fmt_first = fmt_ptr;
351 else
352 fmt_last->next = fmt_ptr;
353 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
355 /*
356 * Isolate one part in the 'errorformat' option
357 */
358 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
359 if (efm[len] == '\\' && efm[len + 1] != NUL)
360 ++len;
361
362 /*
363 * Build regexp pattern from current 'errorformat' option
364 */
365 ptr = fmtstr;
366 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000367 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 for (efmp = efm; efmp < efm + len; ++efmp)
369 {
370 if (*efmp == '%')
371 {
372 ++efmp;
373 for (idx = 0; idx < FMT_PATTERNS; ++idx)
374 if (fmt_pat[idx].convchar == *efmp)
375 break;
376 if (idx < FMT_PATTERNS)
377 {
378 if (fmt_ptr->addr[idx])
379 {
380 sprintf((char *)errmsg,
381 _("E372: Too many %%%c in format string"), *efmp);
382 EMSG(errmsg);
383 goto error2;
384 }
385 if ((idx
386 && idx < 6
387 && vim_strchr((char_u *)"DXOPQ",
388 fmt_ptr->prefix) != NULL)
389 || (idx == 6
390 && vim_strchr((char_u *)"OPQ",
391 fmt_ptr->prefix) == NULL))
392 {
393 sprintf((char *)errmsg,
394 _("E373: Unexpected %%%c in format string"), *efmp);
395 EMSG(errmsg);
396 goto error2;
397 }
398 fmt_ptr->addr[idx] = (char_u)++round;
399 *ptr++ = '\\';
400 *ptr++ = '(';
401#ifdef BACKSLASH_IN_FILENAME
402 if (*efmp == 'f')
403 {
404 /* Also match "c:" in the file name, even when
405 * checking for a colon next: "%f:".
406 * "\%(\a:\)\=" */
407 STRCPY(ptr, "\\%(\\a:\\)\\=");
408 ptr += 10;
409 }
410#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000411 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000413 if (efmp[1] != '\\' && efmp[1] != '%')
414 {
415 /* A file name may contain spaces, but this isn't
416 * in "\f". For "%f:%l:%m" there may be a ":" in
417 * the file name. Use ".\{-1,}x" instead (x is
418 * the next character), the requirement that :999:
419 * follows should work. */
420 STRCPY(ptr, ".\\{-1,}");
421 ptr += 7;
422 }
423 else
424 {
425 /* File name followed by '\\' or '%': include as
426 * many file name chars as possible. */
427 STRCPY(ptr, "\\f\\+");
428 ptr += 4;
429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 }
431 else
432 {
433 srcptr = (char_u *)fmt_pat[idx].pattern;
434 while ((*ptr = *srcptr++) != NUL)
435 ++ptr;
436 }
437 *ptr++ = '\\';
438 *ptr++ = ')';
439 }
440 else if (*efmp == '*')
441 {
442 if (*++efmp == '[' || *efmp == '\\')
443 {
444 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
445 {
446 if (efmp[1] == '^')
447 *ptr++ = *++efmp;
448 if (efmp < efm + len)
449 {
450 *ptr++ = *++efmp; /* could be ']' */
451 while (efmp < efm + len
452 && (*ptr++ = *++efmp) != ']')
453 /* skip */;
454 if (efmp == efm + len)
455 {
456 EMSG(_("E374: Missing ] in format string"));
457 goto error2;
458 }
459 }
460 }
461 else if (efmp < efm + len) /* %*\D, %*\s etc. */
462 *ptr++ = *++efmp;
463 *ptr++ = '\\';
464 *ptr++ = '+';
465 }
466 else
467 {
468 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
469 sprintf((char *)errmsg,
470 _("E375: Unsupported %%%c in format string"), *efmp);
471 EMSG(errmsg);
472 goto error2;
473 }
474 }
475 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
476 *ptr++ = *efmp; /* regexp magic characters */
477 else if (*efmp == '#')
478 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000479 else if (*efmp == '>')
480 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 else if (efmp == efm + 1) /* analyse prefix */
482 {
483 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
484 fmt_ptr->flags = *efmp++;
485 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
486 fmt_ptr->prefix = *efmp;
487 else
488 {
489 sprintf((char *)errmsg,
490 _("E376: Invalid %%%c in format string prefix"), *efmp);
491 EMSG(errmsg);
492 goto error2;
493 }
494 }
495 else
496 {
497 sprintf((char *)errmsg,
498 _("E377: Invalid %%%c in format string"), *efmp);
499 EMSG(errmsg);
500 goto error2;
501 }
502 }
503 else /* copy normal character */
504 {
505 if (*efmp == '\\' && efmp + 1 < efm + len)
506 ++efmp;
507 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
508 *ptr++ = '\\'; /* escape regexp atoms */
509 if (*efmp)
510 *ptr++ = *efmp;
511 }
512 }
513 *ptr++ = '$';
514 *ptr = NUL;
515 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
516 goto error2;
517 /*
518 * Advance to next part
519 */
520 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
521 }
522 if (fmt_first == NULL) /* nothing found */
523 {
524 EMSG(_("E378: 'errorformat' contains no pattern"));
525 goto error2;
526 }
527
528 /*
529 * got_int is reset here, because it was probably set when killing the
530 * ":make" command, but we still want to read the errorfile then.
531 */
532 got_int = FALSE;
533
534 /* Always ignore case when looking for a matching error. */
535 regmatch.rm_ic = TRUE;
536
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000537 if (tv != NULL)
538 {
539 if (tv->v_type == VAR_STRING)
540 p_str = tv->vval.v_string;
541 else if (tv->v_type == VAR_LIST)
542 p_li = tv->vval.v_list->lv_first;
543 }
544
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 /*
546 * Read the lines in the error file one by one.
547 * Try to recognize one of the error formats in each line.
548 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000549 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000551 /* Get the next line. */
552 if (fd == NULL)
553 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000554 if (tv != NULL)
555 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000556 if (tv->v_type == VAR_STRING)
557 {
558 /* Get the next line from the supplied string */
559 char_u *p;
560
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200561 if (*p_str == NUL) /* Reached the end of the string */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000562 break;
563
564 p = vim_strchr(p_str, '\n');
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200565 if (p != NULL)
566 len = (int)(p - p_str) + 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000567 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000568 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000569
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200570 if (len > IOSIZE - 2)
571 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200572 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
573 &linelen);
574 if (linebuf == NULL)
575 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200576 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000577 else
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200578 {
579 linebuf = IObuff;
580 linelen = len;
581 }
582 vim_strncpy(linebuf, p_str, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000583
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200584 /*
585 * Increment using len in order to discard the rest of the
586 * line if it exceeds LINE_MAXLEN.
587 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000588 p_str += len;
589 }
590 else if (tv->v_type == VAR_LIST)
591 {
592 /* Get the next line from the supplied list */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200593 while (p_li != NULL
594 && (p_li->li_tv.v_type != VAR_STRING
595 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000596 p_li = p_li->li_next; /* Skip non-string items */
597
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200598 if (p_li == NULL) /* End of the list */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000599 break;
600
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000601 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200602 if (len > IOSIZE - 2)
603 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200604 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
605 &linelen);
606 if (linebuf == NULL)
607 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200608 }
609 else
610 {
611 linebuf = IObuff;
612 linelen = len;
613 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200615 vim_strncpy(linebuf, p_li->li_tv.vval.v_string, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000616
617 p_li = p_li->li_next; /* next item */
618 }
619 }
620 else
621 {
622 /* Get the next line from the supplied buffer */
623 if (buflnum > lnumlast)
624 break;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200625 p_buf = ml_get_buf(buf, buflnum++, FALSE);
626 linelen = (int)STRLEN(p_buf);
627 if (linelen > IOSIZE - 2)
628 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200629 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
630 &linelen);
631 if (linebuf == NULL)
632 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200633 }
634 else
635 linebuf = IObuff;
636 vim_strncpy(linebuf, p_buf, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000637 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000638 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200639 else
640 {
641 if (fgets((char *)IObuff, IOSIZE, fd) == NULL)
642 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000643
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200644 discard = FALSE;
645 linelen = (int)STRLEN(IObuff);
Bram Moolenaarb37662a2016-06-02 22:18:47 +0200646 if (linelen == IOSIZE - 1 && !(IObuff[linelen - 1] == '\n'
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200647#ifdef USE_CRNL
Bram Moolenaarb37662a2016-06-02 22:18:47 +0200648 || IObuff[linelen - 1] == '\r'
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200649#endif
650 ))
651 {
652 /*
653 * The current line exceeds IObuff, continue reading using
654 * growbuf until EOL or LINE_MAXLEN bytes is read.
655 */
656 if (growbuf == NULL)
657 {
658 growbufsiz = 2 * (IOSIZE - 1);
659 growbuf = alloc(growbufsiz);
660 if (growbuf == NULL)
661 goto qf_init_end;
662 }
663
664 /* Copy the read part of the line, excluding null-terminator */
665 memcpy(growbuf, IObuff, IOSIZE - 1);
666 growbuflen = linelen;
667
668 for (;;)
669 {
670 if (fgets((char *)growbuf + growbuflen,
671 growbufsiz - growbuflen, fd) == NULL)
672 break;
Bram Moolenaard9db8b42016-05-08 12:52:05 +0200673 linelen = (int)STRLEN(growbuf + growbuflen);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200674 growbuflen += linelen;
675 if (growbuf[growbuflen - 1] == '\n'
676#ifdef USE_CRNL
677 || growbuf[growbuflen - 1] == '\r'
678#endif
679 )
680 break;
681 if (growbufsiz == LINE_MAXLEN)
682 {
683 discard = TRUE;
684 break;
685 }
686
687 growbufsiz = 2 * growbufsiz < LINE_MAXLEN
688 ? 2 * growbufsiz : LINE_MAXLEN;
689 growbuf = vim_realloc(growbuf, 2 * growbufsiz);
690 if (growbuf == NULL)
691 goto qf_init_end;
692 }
693
694 while (discard)
695 {
696 /*
697 * The current line is longer than LINE_MAXLEN, continue
698 * reading but discard everything until EOL or EOF is
699 * reached.
700 */
701 if (fgets((char *)IObuff, IOSIZE, fd) == NULL
702 || (int)STRLEN(IObuff) < IOSIZE - 1
703 || IObuff[IOSIZE - 1] == '\n'
704#ifdef USE_CRNL
705 || IObuff[IOSIZE - 1] == '\r'
706#endif
707 )
708 break;
709 }
710
711 linebuf = growbuf;
712 linelen = growbuflen;
713 }
714 else
715 linebuf = IObuff;
716 }
717
718 if (linelen > 0 && linebuf[linelen - 1] == '\n')
719 linebuf[linelen - 1] = NUL;
720#ifdef USE_CRNL
721 if (linelen > 0 && linebuf[linelen - 1] == '\r')
722 linebuf[linelen - 1] = NUL;
Bram Moolenaar836082d2011-08-10 13:21:46 +0200723#endif
724
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200725#ifdef FEAT_MBYTE
726 remove_bom(linebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#endif
728
Bram Moolenaar01265852006-03-20 21:50:15 +0000729 /* If there was no %> item start at the first pattern */
730 if (fmt_start == NULL)
731 fmt_ptr = fmt_first;
732 else
733 {
734 fmt_ptr = fmt_start;
735 fmt_start = NULL;
736 }
737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 /*
739 * Try to match each part of 'errorformat' until we find a complete
740 * match or no match.
741 */
742 valid = TRUE;
743restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000744 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100746 int r;
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 idx = fmt_ptr->prefix;
749 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
750 continue;
751 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000752 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (!multiscan)
754 errmsg[0] = NUL;
755 lnum = 0;
756 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000757 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 enr = -1;
759 type = 0;
760 tail = NULL;
761
762 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200763 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100764 fmt_ptr->prog = regmatch.regprog;
765 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 {
767 if ((idx == 'C' || idx == 'Z') && !multiline)
768 continue;
769 if (vim_strchr((char_u *)"EWI", idx) != NULL)
770 type = idx;
771 else
772 type = 0;
773 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000774 * Extract error message data from matched line.
775 * We check for an actual submatch, because "\[" and "\]" in
776 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 */
778 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
779 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000780 int c;
781
782 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
783 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000784
785 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000786 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000787 *regmatch.endp[i] = NUL;
788 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
789 *regmatch.endp[i] = c;
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000792 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 continue;
794 }
795 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000796 {
797 if (regmatch.startp[i] == NULL)
798 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000802 {
803 if (regmatch.startp[i] == NULL)
804 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000808 {
809 if (regmatch.startp[i] == NULL)
810 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000814 {
815 if (regmatch.startp[i] == NULL)
816 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000818 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000819 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200820 {
821 if (linelen > errmsglen) {
822 /* linelen + null terminator */
823 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
824 goto qf_init_end;
825 errmsglen = linelen + 1;
826 }
827 vim_strncpy(errmsg, linebuf, linelen);
828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
830 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000831 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
832 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200834 if (len > errmsglen) {
835 /* len + null terminator */
836 if ((errmsg = vim_realloc(errmsg, len + 1))
837 == NULL)
838 goto qf_init_end;
839 errmsglen = len + 1;
840 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000841 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000844 {
845 if (regmatch.startp[i] == NULL)
846 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
850 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200851 char_u *match_ptr;
852
Bram Moolenaar4169da72006-06-20 18:49:32 +0000853 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
854 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200855 col = 0;
856 for (match_ptr = regmatch.startp[i];
857 match_ptr != regmatch.endp[i]; ++match_ptr)
858 {
859 ++col;
860 if (*match_ptr == TAB)
861 {
862 col += 7;
863 col -= col % 8;
864 }
865 }
866 ++col;
867 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
870 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000871 if (regmatch.startp[i] == NULL)
872 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000874 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000876 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
877 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000878 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
879 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000880 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
881 if (len > CMDBUFFSIZE - 5)
882 len = CMDBUFFSIZE - 5;
883 STRCPY(pattern, "^\\V");
884 STRNCAT(pattern, regmatch.startp[i], len);
885 pattern[len + 3] = '\\';
886 pattern[len + 4] = '$';
887 pattern[len + 5] = NUL;
888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 break;
890 }
891 }
892 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000893
Bram Moolenaar4770d092006-01-12 23:22:24 +0000894 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000896 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
898 if (idx == 'D') /* enter directory */
899 {
900 if (*namebuf == NUL)
901 {
902 EMSG(_("E379: Missing or empty directory name"));
903 goto error2;
904 }
905 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
906 goto error2;
907 }
908 else if (idx == 'X') /* leave directory */
909 directory = qf_pop_dir(&dir_stack);
910 }
911 namebuf[0] = NUL; /* no match found, remove file name */
912 lnum = 0; /* don't jump to this line */
913 valid = FALSE;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200914 if (linelen > errmsglen) {
915 /* linelen + null terminator */
916 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
917 goto qf_init_end;
918 errmsglen = linelen + 1;
919 }
920 /* copy whole line to error message */
921 vim_strncpy(errmsg, linebuf, linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000922 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 multiline = multiignore = FALSE;
924 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000925 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000927 /* honor %> item */
928 if (fmt_ptr->conthere)
929 fmt_start = fmt_ptr;
930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100934 multiignore = FALSE; /* reset continuation */
935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
937 { /* continuation of multi-line msg */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200938 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
939
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (qfprev == NULL)
941 goto error2;
942 if (*errmsg && !multiignore)
943 {
944 len = (int)STRLEN(qfprev->qf_text);
945 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
946 == NULL)
947 goto error2;
948 STRCPY(ptr, qfprev->qf_text);
949 vim_free(qfprev->qf_text);
950 qfprev->qf_text = ptr;
951 *(ptr += len) = '\n';
952 STRCPY(++ptr, errmsg);
953 }
954 if (qfprev->qf_nr == -1)
955 qfprev->qf_nr = enr;
956 if (vim_isprintc(type) && !qfprev->qf_type)
957 qfprev->qf_type = type; /* only printable chars allowed */
958 if (!qfprev->qf_lnum)
959 qfprev->qf_lnum = lnum;
960 if (!qfprev->qf_col)
961 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000962 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 if (!qfprev->qf_fnum)
964 qfprev->qf_fnum = qf_get_fnum(directory,
965 *namebuf || directory ? namebuf
966 : currfile && valid ? currfile : 0);
967 if (idx == 'Z')
968 multiline = multiignore = FALSE;
969 line_breakcheck();
970 continue;
971 }
972 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
973 {
974 /* global file names */
975 valid = FALSE;
976 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
977 {
978 if (*namebuf && idx == 'P')
979 currfile = qf_push_dir(namebuf, &file_stack);
980 else if (idx == 'Q')
981 currfile = qf_pop_dir(&file_stack);
982 *namebuf = NUL;
983 if (tail && *tail)
984 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000985 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 multiscan = TRUE;
987 goto restofline;
988 }
989 }
990 }
991 if (fmt_ptr->flags == '-') /* generally exclude this line */
992 {
993 if (multiline)
994 multiignore = TRUE; /* also exclude continuation lines */
995 continue;
996 }
997 }
998
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200999 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001001 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001003 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001004 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 errmsg,
1006 lnum,
1007 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001008 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001009 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 enr,
1011 type,
1012 valid) == FAIL)
1013 goto error2;
1014 line_breakcheck();
1015 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00001016 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001018 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001020 /* no valid entry found */
1021 qi->qf_lists[qi->qf_curlist].qf_ptr =
1022 qi->qf_lists[qi->qf_curlist].qf_start;
1023 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1024 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 else
1027 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001028 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1029 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1030 qi->qf_lists[qi->qf_curlist].qf_ptr =
1031 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001033 /* return number of matches */
1034 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 goto qf_init_ok;
1036 }
1037 EMSG(_(e_readerrf));
1038error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001039 qf_free(qi, qi->qf_curlist);
1040 qi->qf_listcount--;
1041 if (qi->qf_curlist > 0)
1042 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +00001044 if (fd != NULL)
1045 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
1047 {
1048 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02001049 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 vim_free(fmt_ptr);
1051 }
1052 qf_clean_dir_stack(&dir_stack);
1053 qf_clean_dir_stack(&file_stack);
1054qf_init_end:
1055 vim_free(namebuf);
1056 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001057 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 vim_free(fmtstr);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001059 vim_free(growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
1061#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001062 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#endif
1064
1065 return retval;
1066}
1067
Bram Moolenaarfb604092014-07-23 15:55:00 +02001068 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001069qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001070{
1071 if (title != NULL)
1072 {
1073 char_u *p = alloc((int)STRLEN(title) + 2);
1074
1075 qi->qf_lists[qi->qf_curlist].qf_title = p;
1076 if (p != NULL)
1077 sprintf((char *)p, ":%s", (char *)title);
1078 }
1079}
1080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081/*
1082 * Prepare for adding a new quickfix list.
1083 */
1084 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001085qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
1087 int i;
1088
1089 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001090 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 * the current entry. This makes it possible to browse in a tree-like
1092 * way with ":grep'.
1093 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001094 while (qi->qf_listcount > qi->qf_curlist + 1)
1095 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
1097 /*
1098 * When the stack is full, remove to oldest entry
1099 * Otherwise, add a new entry.
1100 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001101 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001103 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001105 qi->qf_lists[i - 1] = qi->qf_lists[i];
1106 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
1108 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001109 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001110 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001111 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112}
1113
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001114/*
1115 * Free a location list
1116 */
1117 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001118ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001119{
1120 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001121 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001122
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001123 qi = *pqi;
1124 if (qi == NULL)
1125 return;
1126 *pqi = NULL; /* Remove reference to this list */
1127
1128 qi->qf_refcount--;
1129 if (qi->qf_refcount < 1)
1130 {
1131 /* No references to this location list */
1132 for (i = 0; i < qi->qf_listcount; ++i)
1133 qf_free(qi, i);
1134 vim_free(qi);
1135 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001136}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001137
1138 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001139qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001140{
1141 int i;
1142 qf_info_T *qi = &ql_info;
1143
1144 if (wp != NULL)
1145 {
1146 /* location list */
1147 ll_free_all(&wp->w_llist);
1148 ll_free_all(&wp->w_llist_ref);
1149 }
1150 else
1151 /* quickfix list */
1152 for (i = 0; i < qi->qf_listcount; ++i)
1153 qf_free(qi, i);
1154}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156/*
1157 * Add an entry to the end of the list of errors.
1158 * Returns OK or FAIL.
1159 */
1160 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001161qf_add_entry(
1162 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001163 char_u *dir, /* optional directory name */
1164 char_u *fname, /* file name or NULL */
1165 int bufnum, /* buffer number or zero */
1166 char_u *mesg, /* message */
1167 long lnum, /* line number */
1168 int col, /* column */
1169 int vis_col, /* using visual column */
1170 char_u *pattern, /* search pattern */
1171 int nr, /* error number */
1172 int type, /* type character */
1173 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001175 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001176 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001178 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001180 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001181 {
1182 buf_T *buf = buflist_findnr(bufnum);
1183
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001184 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001185 if (buf != NULL)
1186 buf->b_has_qf_entry = TRUE;
1187 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001188 else
1189 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1191 {
1192 vim_free(qfp);
1193 return FAIL;
1194 }
1195 qfp->qf_lnum = lnum;
1196 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001197 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001198 if (pattern == NULL || *pattern == NUL)
1199 qfp->qf_pattern = NULL;
1200 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1201 {
1202 vim_free(qfp->qf_text);
1203 vim_free(qfp);
1204 return FAIL;
1205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 qfp->qf_nr = nr;
1207 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1208 type = 0;
1209 qfp->qf_type = type;
1210 qfp->qf_valid = valid;
1211
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001212 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001213 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1214 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001216 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001217 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1218 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001219 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221 else
1222 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001223 qfp->qf_prev = *lastp;
1224 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001226 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001228 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001229 ++qi->qf_lists[qi->qf_curlist].qf_count;
1230 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1231 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001233 qi->qf_lists[qi->qf_curlist].qf_index =
1234 qi->qf_lists[qi->qf_curlist].qf_count;
1235 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 }
1237
1238 return OK;
1239}
1240
1241/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001242 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001243 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001244 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001245ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001246{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001247 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001248
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001249 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1250 if (qi != NULL)
1251 {
1252 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1253 qi->qf_refcount++;
1254 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001255
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001256 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001257}
1258
1259/*
1260 * Return the location list for window 'wp'.
1261 * If not present, allocate a location list
1262 */
1263 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001264ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001265{
1266 if (IS_LL_WINDOW(wp))
1267 /* For a location list window, use the referenced location list */
1268 return wp->w_llist_ref;
1269
1270 /*
1271 * For a non-location list window, w_llist_ref should not point to a
1272 * location list.
1273 */
1274 ll_free_all(&wp->w_llist_ref);
1275
1276 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001277 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001278 return wp->w_llist;
1279}
1280
1281/*
1282 * Copy the location list from window "from" to window "to".
1283 */
1284 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001285copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001286{
1287 qf_info_T *qi;
1288 int idx;
1289 int i;
1290
1291 /*
1292 * When copying from a location list window, copy the referenced
1293 * location list. For other windows, copy the location list for
1294 * that window.
1295 */
1296 if (IS_LL_WINDOW(from))
1297 qi = from->w_llist_ref;
1298 else
1299 qi = from->w_llist;
1300
1301 if (qi == NULL) /* no location list to copy */
1302 return;
1303
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001304 /* allocate a new location list */
1305 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001306 return;
1307
1308 to->w_llist->qf_listcount = qi->qf_listcount;
1309
1310 /* Copy the location lists one at a time */
1311 for (idx = 0; idx < qi->qf_listcount; idx++)
1312 {
1313 qf_list_T *from_qfl;
1314 qf_list_T *to_qfl;
1315
1316 to->w_llist->qf_curlist = idx;
1317
1318 from_qfl = &qi->qf_lists[idx];
1319 to_qfl = &to->w_llist->qf_lists[idx];
1320
1321 /* Some of the fields are populated by qf_add_entry() */
1322 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1323 to_qfl->qf_count = 0;
1324 to_qfl->qf_index = 0;
1325 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001326 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001327 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001328 if (from_qfl->qf_title != NULL)
1329 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1330 else
1331 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001332
1333 if (from_qfl->qf_count)
1334 {
1335 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001336 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001337
1338 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001339 for (i = 0, from_qfp = from_qfl->qf_start;
1340 i < from_qfl->qf_count && from_qfp != NULL;
1341 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001342 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001343 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001344 NULL,
1345 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001346 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001347 from_qfp->qf_text,
1348 from_qfp->qf_lnum,
1349 from_qfp->qf_col,
1350 from_qfp->qf_viscol,
1351 from_qfp->qf_pattern,
1352 from_qfp->qf_nr,
1353 0,
1354 from_qfp->qf_valid) == FAIL)
1355 {
1356 qf_free_all(to);
1357 return;
1358 }
1359 /*
1360 * qf_add_entry() will not set the qf_num field, as the
1361 * directory and file names are not supplied. So the qf_fnum
1362 * field is copied here.
1363 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001364 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001365 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1366 prevp->qf_type = from_qfp->qf_type; /* error type */
1367 if (from_qfl->qf_ptr == from_qfp)
1368 to_qfl->qf_ptr = prevp; /* current location */
1369 }
1370 }
1371
1372 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1373
1374 /* When no valid entries are present in the list, qf_ptr points to
1375 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001376 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001377 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001378 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001379 to_qfl->qf_index = 1;
1380 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001381 }
1382
1383 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1384}
1385
1386/*
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001387 * Get buffer number for file "dir.name".
1388 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 */
1390 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001391qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001393 char_u *ptr;
1394 buf_T *buf;
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 if (fname == NULL || *fname == NUL) /* no file name */
1397 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaare60acc12011-05-10 16:41:25 +02001399#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001400 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001401#endif
1402#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001403 if (directory != NULL)
1404 slash_adjust(directory);
1405 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001406#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001407 if (directory != NULL && !vim_isAbsName(fname)
1408 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1409 {
1410 /*
1411 * Here we check if the file really exists.
1412 * This should normally be true, but if make works without
1413 * "leaving directory"-messages we might have missed a
1414 * directory change.
1415 */
1416 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 vim_free(ptr);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001419 directory = qf_guess_filepath(fname);
1420 if (directory)
1421 ptr = concat_fnames(directory, fname, TRUE);
1422 else
1423 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001425 /* Use concatenated directory name and file name */
1426 buf = buflist_new(ptr, NULL, (linenr_T)0, 0);
1427 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001429 else
1430 buf = buflist_new(fname, NULL, (linenr_T)0, 0);
1431 if (buf == NULL)
1432 return 0;
1433 buf->b_has_qf_entry = TRUE;
1434 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435}
1436
1437/*
1438 * push dirbuf onto the directory stack and return pointer to actual dir or
1439 * NULL on error
1440 */
1441 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001442qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
1444 struct dir_stack_T *ds_new;
1445 struct dir_stack_T *ds_ptr;
1446
1447 /* allocate new stack element and hook it in */
1448 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1449 if (ds_new == NULL)
1450 return NULL;
1451
1452 ds_new->next = *stackptr;
1453 *stackptr = ds_new;
1454
1455 /* store directory on the stack */
1456 if (vim_isAbsName(dirbuf)
1457 || (*stackptr)->next == NULL
1458 || (*stackptr && dir_stack != *stackptr))
1459 (*stackptr)->dirname = vim_strsave(dirbuf);
1460 else
1461 {
1462 /* Okay we don't have an absolute path.
1463 * dirbuf must be a subdir of one of the directories on the stack.
1464 * Let's search...
1465 */
1466 ds_new = (*stackptr)->next;
1467 (*stackptr)->dirname = NULL;
1468 while (ds_new)
1469 {
1470 vim_free((*stackptr)->dirname);
1471 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1472 TRUE);
1473 if (mch_isdir((*stackptr)->dirname) == TRUE)
1474 break;
1475
1476 ds_new = ds_new->next;
1477 }
1478
1479 /* clean up all dirs we already left */
1480 while ((*stackptr)->next != ds_new)
1481 {
1482 ds_ptr = (*stackptr)->next;
1483 (*stackptr)->next = (*stackptr)->next->next;
1484 vim_free(ds_ptr->dirname);
1485 vim_free(ds_ptr);
1486 }
1487
1488 /* Nothing found -> it must be on top level */
1489 if (ds_new == NULL)
1490 {
1491 vim_free((*stackptr)->dirname);
1492 (*stackptr)->dirname = vim_strsave(dirbuf);
1493 }
1494 }
1495
1496 if ((*stackptr)->dirname != NULL)
1497 return (*stackptr)->dirname;
1498 else
1499 {
1500 ds_ptr = *stackptr;
1501 *stackptr = (*stackptr)->next;
1502 vim_free(ds_ptr);
1503 return NULL;
1504 }
1505}
1506
1507
1508/*
1509 * pop dirbuf from the directory stack and return previous directory or NULL if
1510 * stack is empty
1511 */
1512 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001513qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
1515 struct dir_stack_T *ds_ptr;
1516
1517 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1518 * What to do if it isn't? */
1519
1520 /* pop top element and free it */
1521 if (*stackptr != NULL)
1522 {
1523 ds_ptr = *stackptr;
1524 *stackptr = (*stackptr)->next;
1525 vim_free(ds_ptr->dirname);
1526 vim_free(ds_ptr);
1527 }
1528
1529 /* return NEW top element as current dir or NULL if stack is empty*/
1530 return *stackptr ? (*stackptr)->dirname : NULL;
1531}
1532
1533/*
1534 * clean up directory stack
1535 */
1536 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001537qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 struct dir_stack_T *ds_ptr;
1540
1541 while ((ds_ptr = *stackptr) != NULL)
1542 {
1543 *stackptr = (*stackptr)->next;
1544 vim_free(ds_ptr->dirname);
1545 vim_free(ds_ptr);
1546 }
1547}
1548
1549/*
1550 * Check in which directory of the directory stack the given file can be
1551 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001552 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 * Cleans up intermediate directory entries.
1554 *
1555 * TODO: How to solve the following problem?
1556 * If we have the this directory tree:
1557 * ./
1558 * ./aa
1559 * ./aa/bb
1560 * ./bb
1561 * ./bb/x.c
1562 * and make says:
1563 * making all in aa
1564 * making all in bb
1565 * x.c:9: Error
1566 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1567 * qf_guess_filepath will return NULL.
1568 */
1569 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001570qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 struct dir_stack_T *ds_ptr;
1573 struct dir_stack_T *ds_tmp;
1574 char_u *fullname;
1575
1576 /* no dirs on the stack - there's nothing we can do */
1577 if (dir_stack == NULL)
1578 return NULL;
1579
1580 ds_ptr = dir_stack->next;
1581 fullname = NULL;
1582 while (ds_ptr)
1583 {
1584 vim_free(fullname);
1585 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1586
1587 /* If concat_fnames failed, just go on. The worst thing that can happen
1588 * is that we delete the entire stack.
1589 */
1590 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1591 break;
1592
1593 ds_ptr = ds_ptr->next;
1594 }
1595
1596 vim_free(fullname);
1597
1598 /* clean up all dirs we already left */
1599 while (dir_stack->next != ds_ptr)
1600 {
1601 ds_tmp = dir_stack->next;
1602 dir_stack->next = dir_stack->next->next;
1603 vim_free(ds_tmp->dirname);
1604 vim_free(ds_tmp);
1605 }
1606
1607 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1608
1609}
1610
1611/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001612 * When loading a file from the quickfix, the auto commands may modify it.
1613 * This may invalidate the current quickfix entry. This function checks
1614 * whether a entry is still present in the quickfix.
1615 * Similar to location list.
1616 */
1617 static int
1618is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1619{
1620 qf_list_T *qfl;
1621 qfline_T *qfp;
1622 int i;
1623
1624 qfl = &qi->qf_lists[qi->qf_curlist];
1625
1626 /* Search for the entry in the current list */
1627 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1628 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001629 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001630 break;
1631
1632 if (i == qfl->qf_count) /* Entry is not found */
1633 return FALSE;
1634
1635 return TRUE;
1636}
1637
1638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 * jump to a quickfix line
1640 * if dir == FORWARD go "errornr" valid entries forward
1641 * if dir == BACKWARD go "errornr" valid entries backward
1642 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1643 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1644 * else if "errornr" is zero, redisplay the same line
1645 * else go to entry "errornr"
1646 */
1647 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001648qf_jump(
1649 qf_info_T *qi,
1650 int dir,
1651 int errornr,
1652 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001654 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001655 qfline_T *qf_ptr;
1656 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 int qf_index;
1658 int old_qf_fnum;
1659 int old_qf_index;
1660 int prev_index;
1661 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1662 char_u *err = e_no_more_items;
1663 linenr_T i;
1664 buf_T *old_curbuf;
1665 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 colnr_T screen_col;
1667 colnr_T char_col;
1668 char_u *line;
1669#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001670 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001671 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 int opened_window = FALSE;
1673 win_T *win;
1674 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001675 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001677 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 int print_message = TRUE;
1679 int len;
1680#ifdef FEAT_FOLDING
1681 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1682#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001683 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001684 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001686 if (qi == NULL)
1687 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001688
1689 if (qi->qf_curlist >= qi->qf_listcount
1690 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
1692 EMSG(_(e_quickfix));
1693 return;
1694 }
1695
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001696 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001698 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 old_qf_index = qf_index;
1700 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1701 {
1702 while (errornr--)
1703 {
1704 old_qf_ptr = qf_ptr;
1705 prev_index = qf_index;
1706 old_qf_fnum = qf_ptr->qf_fnum;
1707 do
1708 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001709 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 || qf_ptr->qf_next == NULL)
1711 {
1712 qf_ptr = old_qf_ptr;
1713 qf_index = prev_index;
1714 if (err != NULL)
1715 {
1716 EMSG(_(err));
1717 goto theend;
1718 }
1719 errornr = 0;
1720 break;
1721 }
1722 ++qf_index;
1723 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001724 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1725 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1727 err = NULL;
1728 }
1729 }
1730 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1731 {
1732 while (errornr--)
1733 {
1734 old_qf_ptr = qf_ptr;
1735 prev_index = qf_index;
1736 old_qf_fnum = qf_ptr->qf_fnum;
1737 do
1738 {
1739 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1740 {
1741 qf_ptr = old_qf_ptr;
1742 qf_index = prev_index;
1743 if (err != NULL)
1744 {
1745 EMSG(_(err));
1746 goto theend;
1747 }
1748 errornr = 0;
1749 break;
1750 }
1751 --qf_index;
1752 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001753 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1754 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1756 err = NULL;
1757 }
1758 }
1759 else if (errornr != 0) /* go to specified number */
1760 {
1761 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1762 {
1763 --qf_index;
1764 qf_ptr = qf_ptr->qf_prev;
1765 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001766 while (errornr > qf_index && qf_index <
1767 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 && qf_ptr->qf_next != NULL)
1769 {
1770 ++qf_index;
1771 qf_ptr = qf_ptr->qf_next;
1772 }
1773 }
1774
1775#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001776 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1777 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 /* No need to print the error message if it's visible in the error
1779 * window */
1780 print_message = FALSE;
1781
1782 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001783 * For ":helpgrep" find a help window or open one.
1784 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001785 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001786 {
1787 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001788
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001789 if (cmdmod.tab != 0)
1790 wp = NULL;
1791 else
1792 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1793 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1794 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001795 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1796 win_enter(wp, TRUE);
1797 else
1798 {
1799 /*
1800 * Split off help window; put it at far top if no position
1801 * specified, the current window is vertically split and narrow.
1802 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001803 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001804 if (cmdmod.split == 0 && curwin->w_width != Columns
1805 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001806 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001807 if (qi != &ql_info)
1808 flags |= WSP_NEWLOC; /* don't copy the location list */
1809
1810 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001811 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001812 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001813
1814 if (curwin->w_height < p_hh)
1815 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001816
1817 if (qi != &ql_info) /* not a quickfix list */
1818 {
1819 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001820 curwin->w_llist = qi;
1821 qi->qf_refcount++;
1822 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001823 }
1824
1825 if (!p_im)
1826 restart_edit = 0; /* don't want insert mode in help file */
1827 }
1828
1829 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 * If currently in the quickfix window, find another window to show the
1831 * file in.
1832 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001833 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001835 win_T *usable_win_ptr = NULL;
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 /*
1838 * If there is no file specified, we don't know where to go.
1839 * But do advance, otherwise ":cn" gets stuck.
1840 */
1841 if (qf_ptr->qf_fnum == 0)
1842 goto theend;
1843
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001844 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001845
1846 ll_ref = curwin->w_llist_ref;
1847 if (ll_ref != NULL)
1848 {
1849 /* Find a window using the same location list that is not a
1850 * quickfix window. */
1851 FOR_ALL_WINDOWS(usable_win_ptr)
1852 if (usable_win_ptr->w_llist == ll_ref
1853 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001854 {
1855 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001856 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001857 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001858 }
1859
1860 if (!usable_win)
1861 {
1862 /* Locate a window showing a normal buffer */
1863 FOR_ALL_WINDOWS(win)
1864 if (win->w_buffer->b_p_bt[0] == NUL)
1865 {
1866 usable_win = 1;
1867 break;
1868 }
1869 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001870
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001872 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001873 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001875 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001876 {
1877 tabpage_T *tp;
1878 win_T *wp;
1879
1880 FOR_ALL_TAB_WINDOWS(tp, wp)
1881 {
1882 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1883 {
1884 goto_tabpage_win(tp, wp);
1885 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001886 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001887 }
1888 }
1889 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001890win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001891
1892 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001893 * If there is only one window and it is the quickfix window, create a
1894 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001895 */
1896 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001898 flags = WSP_ABOVE;
1899 if (ll_ref != NULL)
1900 flags |= WSP_NEWLOC;
1901 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 goto failed; /* not enough room for window */
1903 opened_window = TRUE; /* close it when fail */
1904 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001905 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001906 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001907 if (ll_ref != NULL)
1908 {
1909 /* The new window should use the location list from the
1910 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001911 curwin->w_llist = ll_ref;
1912 ll_ref->qf_refcount++;
1913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 }
1915 else
1916 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001917 if (curwin->w_llist_ref != NULL)
1918 {
1919 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001920 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001921 if (win == NULL)
1922 {
1923 /* Find the window showing the selected file */
1924 FOR_ALL_WINDOWS(win)
1925 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1926 break;
1927 if (win == NULL)
1928 {
1929 /* Find a previous usable window */
1930 win = curwin;
1931 do
1932 {
1933 if (win->w_buffer->b_p_bt[0] == NUL)
1934 break;
1935 if (win->w_prev == NULL)
1936 win = lastwin; /* wrap around the top */
1937 else
1938 win = win->w_prev; /* go to previous window */
1939 } while (win != curwin);
1940 }
1941 }
1942 win_goto(win);
1943
1944 /* If the location list for the window is not set, then set it
1945 * to the location list from the location window */
1946 if (win->w_llist == NULL)
1947 {
1948 win->w_llist = ll_ref;
1949 ll_ref->qf_refcount++;
1950 }
1951 }
1952 else
1953 {
1954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 /*
1956 * Try to find a window that shows the right buffer.
1957 * Default to the window just above the quickfix buffer.
1958 */
1959 win = curwin;
1960 altwin = NULL;
1961 for (;;)
1962 {
1963 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1964 break;
1965 if (win->w_prev == NULL)
1966 win = lastwin; /* wrap around the top */
1967 else
1968 win = win->w_prev; /* go to previous window */
1969
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001970 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 /* Didn't find it, go to the window before the quickfix
1973 * window. */
1974 if (altwin != NULL)
1975 win = altwin;
1976 else if (curwin->w_prev != NULL)
1977 win = curwin->w_prev;
1978 else
1979 win = curwin->w_next;
1980 break;
1981 }
1982
1983 /* Remember a usable window. */
1984 if (altwin == NULL && !win->w_p_pvw
1985 && win->w_buffer->b_p_bt[0] == NUL)
1986 altwin = win;
1987 }
1988
1989 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
1992 }
1993#endif
1994
1995 /*
1996 * If there is a file name,
1997 * read the wanted file if needed, and check autowrite etc.
1998 */
1999 old_curbuf = curbuf;
2000 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002001
2002 if (qf_ptr->qf_fnum != 0)
2003 {
2004 if (qf_ptr->qf_type == 1)
2005 {
2006 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2007 * set b_p_ro flag). */
2008 if (!can_abandon(curbuf, forceit))
2009 {
2010 EMSG(_(e_nowrtmsg));
2011 ok = FALSE;
2012 }
2013 else
2014 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002015 ECMD_HIDE + ECMD_SET_HELP,
2016 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002017 }
2018 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002019 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002020 int old_qf_curlist = qi->qf_curlist;
2021 int is_abort = FALSE;
2022
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002023 ok = buflist_getfile(qf_ptr->qf_fnum,
2024 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002025 if (qi != &ql_info && !win_valid(oldwin))
2026 {
2027 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002028 is_abort = TRUE;
2029 opened_window = FALSE;
2030 }
2031 else if (old_qf_curlist != qi->qf_curlist
2032 || !is_qf_entry_present(qi, qf_ptr))
2033 {
2034 if (qi == &ql_info)
2035 EMSG(_("E925: Current quickfix was changed"));
2036 else
2037 EMSG(_("E926: Current location list was changed"));
2038 is_abort = TRUE;
2039 }
2040
2041 if (is_abort)
2042 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002043 ok = FALSE;
2044 qi = NULL;
2045 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002046 }
2047 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002048 }
2049
2050 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
2052 /* When not switched to another buffer, still need to set pc mark */
2053 if (curbuf == old_curbuf)
2054 setpcmark();
2055
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002056 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002058 /*
2059 * Go to line with error, unless qf_lnum is 0.
2060 */
2061 i = qf_ptr->qf_lnum;
2062 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002064 if (i > curbuf->b_ml.ml_line_count)
2065 i = curbuf->b_ml.ml_line_count;
2066 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002068 if (qf_ptr->qf_col > 0)
2069 {
2070 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002071#ifdef FEAT_VIRTUALEDIT
2072 curwin->w_cursor.coladd = 0;
2073#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002074 if (qf_ptr->qf_viscol == TRUE)
2075 {
2076 /*
2077 * Check each character from the beginning of the error
2078 * line up to the error column. For each tab character
2079 * found, reduce the error column value by the length of
2080 * a tab character.
2081 */
2082 line = ml_get_curline();
2083 screen_col = 0;
2084 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2085 {
2086 if (*line == NUL)
2087 break;
2088 if (*line++ == '\t')
2089 {
2090 curwin->w_cursor.col -= 7 - (screen_col % 8);
2091 screen_col += 8 - (screen_col % 8);
2092 }
2093 else
2094 ++screen_col;
2095 }
2096 }
2097 check_cursor();
2098 }
2099 else
2100 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002103 {
2104 pos_T save_cursor;
2105
2106 /* Move the cursor to the first line in the buffer */
2107 save_cursor = curwin->w_cursor;
2108 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002109 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2110 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002111 curwin->w_cursor = save_cursor;
2112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114#ifdef FEAT_FOLDING
2115 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2116 foldOpenCursor();
2117#endif
2118 if (print_message)
2119 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002120 /* Update the screen before showing the message, unless the screen
2121 * scrolled up. */
2122 if (!msg_scrolled)
2123 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002125 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2127 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2128 /* Add the message, skipping leading whitespace and newlines. */
2129 len = (int)STRLEN(IObuff);
2130 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2131
2132 /* Output the message. Overwrite to avoid scrolling when the 'O'
2133 * flag is present in 'shortmess'; But when not jumping, print the
2134 * whole message. */
2135 i = msg_scroll;
2136 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2137 msg_scroll = TRUE;
2138 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2139 msg_scroll = FALSE;
2140 msg_attr_keep(IObuff, 0, TRUE);
2141 msg_scroll = i;
2142 }
2143 }
2144 else
2145 {
2146#ifdef FEAT_WINDOWS
2147 if (opened_window)
2148 win_close(curwin, TRUE); /* Close opened window */
2149#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002150 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
2152 /*
2153 * Couldn't open file, so put index back where it was. This could
2154 * happen if the file was readonly and we changed something.
2155 */
2156#ifdef FEAT_WINDOWS
2157failed:
2158#endif
2159 qf_ptr = old_qf_ptr;
2160 qf_index = old_qf_index;
2161 }
2162 }
2163theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002164 if (qi != NULL)
2165 {
2166 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2167 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#ifdef FEAT_WINDOWS
2170 if (p_swb != old_swb && opened_window)
2171 {
2172 /* Restore old 'switchbuf' value, but not when an autocommand or
2173 * modeline has changed the value. */
2174 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002177 swb_flags = old_swb_flags;
2178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 else
2180 free_string_option(old_swb);
2181 }
2182#endif
2183}
2184
2185/*
2186 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002187 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 */
2189 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002190qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002192 buf_T *buf;
2193 char_u *fname;
2194 qfline_T *qfp;
2195 int i;
2196 int idx1 = 1;
2197 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002198 char_u *arg = eap->arg;
2199 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002201 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002203 if (eap->cmdidx == CMD_llist)
2204 {
2205 qi = GET_LOC_LIST(curwin);
2206 if (qi == NULL)
2207 {
2208 EMSG(_(e_loclist));
2209 return;
2210 }
2211 }
2212
2213 if (qi->qf_curlist >= qi->qf_listcount
2214 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 {
2216 EMSG(_(e_quickfix));
2217 return;
2218 }
2219 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2220 {
2221 EMSG(_(e_trailing));
2222 return;
2223 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002224 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 if (idx1 < 0)
2226 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2227 if (idx2 < 0)
2228 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2229
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002230 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002232 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2233 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
2235 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2236 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002237 msg_putchar('\n');
2238 if (got_int)
2239 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002240
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002241 fname = NULL;
2242 if (qfp->qf_fnum != 0
2243 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2244 {
2245 fname = buf->b_fname;
2246 if (qfp->qf_type == 1) /* :helpgrep */
2247 fname = gettail(fname);
2248 }
2249 if (fname == NULL)
2250 sprintf((char *)IObuff, "%2d", i);
2251 else
2252 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2253 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002254 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002255 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2256 if (qfp->qf_lnum == 0)
2257 IObuff[0] = NUL;
2258 else if (qfp->qf_col == 0)
2259 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2260 else
2261 sprintf((char *)IObuff, ":%ld col %d",
2262 qfp->qf_lnum, qfp->qf_col);
2263 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2264 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2265 msg_puts_attr(IObuff, hl_attr(HLF_N));
2266 if (qfp->qf_pattern != NULL)
2267 {
2268 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2269 STRCAT(IObuff, ":");
2270 msg_puts(IObuff);
2271 }
2272 msg_puts((char_u *)" ");
2273
2274 /* Remove newlines and leading whitespace from the text. For an
2275 * unrecognized line keep the indent, the compiler may mark a word
2276 * with ^^^^. */
2277 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2279 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002280 msg_prt_line(IObuff, FALSE);
2281 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002283
2284 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002285 if (qfp == NULL)
2286 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002287 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 ui_breakcheck();
2289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290}
2291
2292/*
2293 * Remove newlines and leading whitespace from an error message.
2294 * Put the result in "buf[bufsize]".
2295 */
2296 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002297qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
2299 int i;
2300 char_u *p = text;
2301
2302 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2303 {
2304 if (*p == '\n')
2305 {
2306 buf[i] = ' ';
2307 while (*++p != NUL)
2308 if (!vim_iswhite(*p) && *p != '\n')
2309 break;
2310 }
2311 else
2312 buf[i] = *p++;
2313 }
2314 buf[i] = NUL;
2315}
2316
2317/*
2318 * ":colder [count]": Up in the quickfix stack.
2319 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002320 * ":lolder [count]": Up in the location list stack.
2321 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 */
2323 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002324qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002326 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 int count;
2328
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002329 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2330 {
2331 qi = GET_LOC_LIST(curwin);
2332 if (qi == NULL)
2333 {
2334 EMSG(_(e_loclist));
2335 return;
2336 }
2337 }
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (eap->addr_count != 0)
2340 count = eap->line2;
2341 else
2342 count = 1;
2343 while (count--)
2344 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002345 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002347 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002350 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002352 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354 else
2355 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002356 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002359 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002361 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
2363 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002364 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365}
2366
2367 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002368qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002371 qi->qf_curlist + 1, qi->qf_listcount,
2372 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002374 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375#endif
2376}
2377
2378/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002379 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 */
2381 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002382qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002384 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002385 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002386 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002388 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002390 qfp = qi->qf_lists[idx].qf_start;
2391 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002392 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002393 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002394 vim_free(qfp->qf_text);
2395 stop = (qfp == qfpnext);
2396 vim_free(qfp->qf_pattern);
2397 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002398 if (stop)
2399 /* Somehow qf_count may have an incorrect value, set it to 1
2400 * to avoid crashing when it's wrong.
2401 * TODO: Avoid qf_count being incorrect. */
2402 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002403 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002404 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002405 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002407 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002408 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002409 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410}
2411
2412/*
2413 * qf_mark_adjust: adjust marks
2414 */
2415 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002416qf_mark_adjust(
2417 win_T *wp,
2418 linenr_T line1,
2419 linenr_T line2,
2420 long amount,
2421 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002423 int i;
2424 qfline_T *qfp;
2425 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002426 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002427 int found_one = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002429 if (!curbuf->b_has_qf_entry)
2430 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002431 if (wp != NULL)
2432 {
2433 if (wp->w_llist == NULL)
2434 return;
2435 qi = wp->w_llist;
2436 }
2437
2438 for (idx = 0; idx < qi->qf_listcount; ++idx)
2439 if (qi->qf_lists[idx].qf_count)
2440 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002441 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2442 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 if (qfp->qf_fnum == curbuf->b_fnum)
2444 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002445 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2447 {
2448 if (amount == MAXLNUM)
2449 qfp->qf_cleared = TRUE;
2450 else
2451 qfp->qf_lnum += amount;
2452 }
2453 else if (amount_after && qfp->qf_lnum > line2)
2454 qfp->qf_lnum += amount_after;
2455 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002456
2457 if (!found_one)
2458 curbuf->b_has_qf_entry = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459}
2460
2461/*
2462 * Make a nice message out of the error character and the error number:
2463 * char number message
2464 * e or E 0 " error"
2465 * w or W 0 " warning"
2466 * i or I 0 " info"
2467 * 0 0 ""
2468 * other 0 " c"
2469 * e or E n " error n"
2470 * w or W n " warning n"
2471 * i or I n " info n"
2472 * 0 n " error n"
2473 * other n " c n"
2474 * 1 x "" :helpgrep
2475 */
2476 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002477qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478{
2479 static char_u buf[20];
2480 static char_u cc[3];
2481 char_u *p;
2482
2483 if (c == 'W' || c == 'w')
2484 p = (char_u *)" warning";
2485 else if (c == 'I' || c == 'i')
2486 p = (char_u *)" info";
2487 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2488 p = (char_u *)" error";
2489 else if (c == 0 || c == 1)
2490 p = (char_u *)"";
2491 else
2492 {
2493 cc[0] = ' ';
2494 cc[1] = c;
2495 cc[2] = NUL;
2496 p = cc;
2497 }
2498
2499 if (nr <= 0)
2500 return p;
2501
2502 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2503 return buf;
2504}
2505
2506#if defined(FEAT_WINDOWS) || defined(PROTO)
2507/*
2508 * ":cwindow": open the quickfix window if we have errors to display,
2509 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002510 * ":lwindow": open the location list window if we have locations to display,
2511 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 */
2513 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002514ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002516 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 win_T *win;
2518
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002519 if (eap->cmdidx == CMD_lwindow)
2520 {
2521 qi = GET_LOC_LIST(curwin);
2522 if (qi == NULL)
2523 return;
2524 }
2525
2526 /* Look for an existing quickfix window. */
2527 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529 /*
2530 * If a quickfix window is open but we have no errors to display,
2531 * close the window. If a quickfix window is not open, then open
2532 * it if we have errors; otherwise, leave it closed.
2533 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002534 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002535 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002536 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
2538 if (win != NULL)
2539 ex_cclose(eap);
2540 }
2541 else if (win == NULL)
2542 ex_copen(eap);
2543}
2544
2545/*
2546 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002547 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002550ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002552 win_T *win = NULL;
2553 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002555 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2556 {
2557 qi = GET_LOC_LIST(curwin);
2558 if (qi == NULL)
2559 return;
2560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002562 /* Find existing quickfix window and close it. */
2563 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 if (win != NULL)
2565 win_close(win, FALSE);
2566}
2567
2568/*
2569 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002570 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 */
2572 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002573ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002575 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002578 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002579 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002580 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002582 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2583 {
2584 qi = GET_LOC_LIST(curwin);
2585 if (qi == NULL)
2586 {
2587 EMSG(_(e_loclist));
2588 return;
2589 }
2590 }
2591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (eap->addr_count != 0)
2593 height = eap->line2;
2594 else
2595 height = QF_WINHEIGHT;
2596
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598#ifdef FEAT_GUI
2599 need_mouse_correct = TRUE;
2600#endif
2601
2602 /*
2603 * Find existing quickfix window, or open a new one.
2604 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002605 win = qf_find_win(qi);
2606
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002607 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002610 if (eap->addr_count != 0)
2611 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002612 if (cmdmod.split & WSP_VERT)
2613 {
2614 if (height != W_WIDTH(win))
2615 win_setwidth(height);
2616 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002617 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002618 win_setheight(height);
2619 }
2620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 else
2622 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002623 qf_buf = qf_find_buf(qi);
2624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 /* The current window becomes the previous window afterwards. */
2626 win = curwin;
2627
Bram Moolenaar77642c02012-11-20 17:55:10 +01002628 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2629 && cmdmod.split == 0)
2630 /* Create the new window at the very bottom, except when
2631 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002632 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002633 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002635 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002637 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002639 /*
2640 * For the location list window, create a reference to the
2641 * location list from the window 'win'.
2642 */
2643 curwin->w_llist_ref = win->w_llist;
2644 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002646
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002647 if (oldwin != curwin)
2648 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002649 if (qf_buf != NULL)
2650 /* Use the existing quickfix buffer */
2651 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002652 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002653 else
2654 {
2655 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002656 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002657 /* switch off 'swapfile' */
2658 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2659 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002660 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002661 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002662 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002663#ifdef FEAT_DIFF
2664 curwin->w_p_diff = FALSE;
2665#endif
2666#ifdef FEAT_FOLDING
2667 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2668 OPT_LOCAL);
2669#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002672 /* Only set the height when still in the same tab page and there is no
2673 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002674 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 win_setheight(height);
2676 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2677 if (win_valid(win))
2678 prevwin = win;
2679 }
2680
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002681 qf_set_title_var(qi);
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 /*
2684 * Fill the buffer with the quickfix list.
2685 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002686 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002688 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 curwin->w_cursor.col = 0;
2690 check_cursor();
2691 update_topline(); /* scroll to show the line */
2692}
2693
2694/*
2695 * Return the number of the current entry (line number in the quickfix
2696 * window).
2697 */
2698 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002699qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002701 qf_info_T *qi = &ql_info;
2702
2703 if (IS_LL_WINDOW(wp))
2704 /* In the location list window, use the referenced location list */
2705 qi = wp->w_llist_ref;
2706
2707 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708}
2709
2710/*
2711 * Update the cursor position in the quickfix window to the current error.
2712 * Return TRUE if there is a quickfix window.
2713 */
2714 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002715qf_win_pos_update(
2716 qf_info_T *qi,
2717 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002720 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
2722 /*
2723 * Put the cursor on the current error in the quickfix window, so that
2724 * it's viewable.
2725 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002726 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (win != NULL
2728 && qf_index <= win->w_buffer->b_ml.ml_line_count
2729 && old_qf_index != qf_index)
2730 {
2731 win_T *old_curwin = curwin;
2732
2733 curwin = win;
2734 curbuf = win->w_buffer;
2735 if (qf_index > old_qf_index)
2736 {
2737 curwin->w_redraw_top = old_qf_index;
2738 curwin->w_redraw_bot = qf_index;
2739 }
2740 else
2741 {
2742 curwin->w_redraw_top = qf_index;
2743 curwin->w_redraw_bot = old_qf_index;
2744 }
2745 curwin->w_cursor.lnum = qf_index;
2746 curwin->w_cursor.col = 0;
2747 update_topline(); /* scroll to show the line */
2748 redraw_later(VALID);
2749 curwin->w_redr_status = TRUE; /* update ruler */
2750 curwin = old_curwin;
2751 curbuf = curwin->w_buffer;
2752 }
2753 return win != NULL;
2754}
2755
2756/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002757 * Check whether the given window is displaying the specified quickfix/location
2758 * list buffer
2759 */
2760 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002761is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002762{
2763 /*
2764 * A window displaying the quickfix buffer will have the w_llist_ref field
2765 * set to NULL.
2766 * A window displaying a location list buffer will have the w_llist_ref
2767 * pointing to the location list.
2768 */
2769 if (bt_quickfix(win->w_buffer))
2770 if ((qi == &ql_info && win->w_llist_ref == NULL)
2771 || (qi != &ql_info && win->w_llist_ref == qi))
2772 return TRUE;
2773
2774 return FALSE;
2775}
2776
2777/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002778 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002779 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002780 */
2781 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002782qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783{
2784 win_T *win;
2785
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002786 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002787 if (is_qf_win(win, qi))
2788 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002789
2790 return win;
2791}
2792
2793/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002794 * Find a quickfix buffer.
2795 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 */
2797 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002798qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002800 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002801 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
Bram Moolenaar9c102382006-05-03 21:26:49 +00002803 FOR_ALL_TAB_WINDOWS(tp, win)
2804 if (is_qf_win(win, qi))
2805 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002806
Bram Moolenaar9c102382006-05-03 21:26:49 +00002807 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808}
2809
2810/*
2811 * Find the quickfix buffer. If it exists, update the contents.
2812 */
2813 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002814qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
2816 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002817 win_T *win;
2818 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
2821 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002822 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 if (buf != NULL)
2824 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02002825 linenr_T old_line_count = buf->b_ml.ml_line_count;
2826
2827 if (old_last == NULL)
2828 /* set curwin/curbuf to buf and save a few things */
2829 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002831 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002832 {
2833 curwin_save = curwin;
2834 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002835 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002836 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002837 }
2838
Bram Moolenaar864293a2016-06-02 13:40:04 +02002839 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01002840
Bram Moolenaar864293a2016-06-02 13:40:04 +02002841 if (old_last == NULL)
2842 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002843 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002844
2845 /* restore curwin/curbuf and a few other things */
2846 aucmd_restbuf(&aco);
2847 }
2848
2849 /* Only redraw when added lines are visible. This avoids flickering
2850 * when the added lines are not visible. */
2851 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
2852 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
2854}
2855
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002856/*
2857 * Set "w:quickfix_title" if "qi" has a title.
2858 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002859 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002860qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002861{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002862 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2863 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002864 qi->qf_lists[qi->qf_curlist].qf_title);
2865}
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867/*
2868 * Fill current buffer with quickfix errors, replacing any previous contents.
2869 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02002870 * If "old_last" is not NULL append the items after this one.
2871 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
2872 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 */
2874 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002875qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002877 linenr_T lnum;
2878 qfline_T *qfp;
2879 buf_T *errbuf;
2880 int len;
2881 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
Bram Moolenaar864293a2016-06-02 13:40:04 +02002883 if (old_last == NULL)
2884 {
2885 if (buf != curbuf)
2886 {
2887 EMSG2(_(e_intern2), "qf_fill_buffer()");
2888 return;
2889 }
2890
2891 /* delete all existing lines */
2892 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2893 (void)ml_delete((linenr_T)1, FALSE);
2894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
2896 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002897 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
2899 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002900 if (old_last == NULL)
2901 {
2902 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2903 lnum = 0;
2904 }
2905 else
2906 {
2907 qfp = old_last->qf_next;
2908 lnum = buf->b_ml.ml_line_count;
2909 }
2910 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
2912 if (qfp->qf_fnum != 0
2913 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2914 && errbuf->b_fname != NULL)
2915 {
2916 if (qfp->qf_type == 1) /* :helpgrep */
2917 STRCPY(IObuff, gettail(errbuf->b_fname));
2918 else
2919 STRCPY(IObuff, errbuf->b_fname);
2920 len = (int)STRLEN(IObuff);
2921 }
2922 else
2923 len = 0;
2924 IObuff[len++] = '|';
2925
2926 if (qfp->qf_lnum > 0)
2927 {
2928 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2929 len += (int)STRLEN(IObuff + len);
2930
2931 if (qfp->qf_col > 0)
2932 {
2933 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2934 len += (int)STRLEN(IObuff + len);
2935 }
2936
2937 sprintf((char *)IObuff + len, "%s",
2938 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2939 len += (int)STRLEN(IObuff + len);
2940 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002941 else if (qfp->qf_pattern != NULL)
2942 {
2943 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2944 len += (int)STRLEN(IObuff + len);
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 IObuff[len++] = '|';
2947 IObuff[len++] = ' ';
2948
2949 /* Remove newlines and leading whitespace from the text.
2950 * For an unrecognized line keep the indent, the compiler may
2951 * mark a word with ^^^^. */
2952 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2953 IObuff + len, IOSIZE - len);
2954
Bram Moolenaar864293a2016-06-02 13:40:04 +02002955 if (ml_append_buf(buf, lnum, IObuff,
2956 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02002958 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002960 if (qfp == NULL)
2961 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02002963
2964 if (old_last == NULL)
2965 /* Delete the empty line which is now at the end */
2966 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
2968
2969 /* correct cursor position */
2970 check_lnums(TRUE);
2971
Bram Moolenaar864293a2016-06-02 13:40:04 +02002972 if (old_last == NULL)
2973 {
2974 /* Set the 'filetype' to "qf" each time after filling the buffer.
2975 * This resembles reading a file into a buffer, it's more logical when
2976 * using autocommands. */
2977 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2978 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
2980#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02002981 keep_filetype = TRUE; /* don't detect 'filetype' */
2982 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002984 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002986 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02002988 /* make sure it will be redrawn */
2989 redraw_curbuf_later(NOT_VALID);
2990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991
2992 /* Restore KeyTyped, setting 'filetype' may reset it. */
2993 KeyTyped = old_KeyTyped;
2994}
2995
2996#endif /* FEAT_WINDOWS */
2997
2998/*
2999 * Return TRUE if "buf" is the quickfix buffer.
3000 */
3001 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003002bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003004 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005}
3006
3007/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003008 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3009 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 */
3011 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003012bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003014 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3015 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016}
3017
3018/*
3019 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3020 */
3021 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003022bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003024 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025}
3026
3027 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003028bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 if (bt_dontwrite(buf))
3031 {
3032 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3033 return TRUE;
3034 }
3035 return FALSE;
3036}
3037
3038/*
3039 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3040 * and 'bufhidden'.
3041 */
3042 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003043buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3046 switch (buf->b_p_bh[0])
3047 {
3048 case 'u': /* "unload" */
3049 case 'w': /* "wipe" */
3050 case 'd': return FALSE; /* "delete" */
3051 case 'h': return TRUE; /* "hide" */
3052 }
3053 return (p_hid || cmdmod.hide);
3054}
3055
3056/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003057 * Return TRUE when using ":vimgrep" for ":grep".
3058 */
3059 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003060grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003061{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003062 return ((cmdidx == CMD_grep
3063 || cmdidx == CMD_lgrep
3064 || cmdidx == CMD_grepadd
3065 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003066 && STRCMP("internal",
3067 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3068}
3069
3070/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003071 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 */
3073 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003074ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003076 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 char_u *cmd;
3078 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003079 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003080 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003081 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003082#ifdef FEAT_AUTOCMD
3083 char_u *au_name = NULL;
3084
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003085 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3086 if (grep_internal(eap->cmdidx))
3087 {
3088 ex_vimgrep(eap);
3089 return;
3090 }
3091
Bram Moolenaar7c626922005-02-07 22:01:03 +00003092 switch (eap->cmdidx)
3093 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003094 case CMD_make: au_name = (char_u *)"make"; break;
3095 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3096 case CMD_grep: au_name = (char_u *)"grep"; break;
3097 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3098 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3099 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003100 default: break;
3101 }
3102 if (au_name != NULL)
3103 {
3104 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3105 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003106# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003107 if (did_throw || force_abort)
3108 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003109# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003110 }
3111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112
Bram Moolenaara6557602006-02-04 22:43:20 +00003113 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3114 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003115 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003118 fname = get_mef_name();
3119 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003121 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122
3123 /*
3124 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3125 */
3126 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3127 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003128 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 cmd = alloc(len);
3130 if (cmd == NULL)
3131 return;
3132 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3133 (char *)p_shq);
3134 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003135 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 /*
3137 * Output a newline if there's something else than the :make command that
3138 * was typed (in which case the cursor is in column 0).
3139 */
3140 if (msg_col == 0)
3141 msg_didout = FALSE;
3142 msg_start();
3143 MSG_PUTS(":!");
3144 msg_outtrans(cmd); /* show what we are doing */
3145
3146 /* let the shell know if we are redirecting output or not */
3147 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3148
3149#ifdef AMIGA
3150 out_flush();
3151 /* read window status report and redraw before message */
3152 (void)char_avail();
3153#endif
3154
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003155 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003156 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3157 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003158 && eap->cmdidx != CMD_lgrepadd),
3159 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003160 if (wp != NULL)
3161 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003162#ifdef FEAT_AUTOCMD
3163 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003164 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003165 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3166 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003167 if (qi->qf_curlist < qi->qf_listcount)
3168 res = qi->qf_lists[qi->qf_curlist].qf_count;
3169 else
3170 res = 0;
3171 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003172#endif
3173 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003174 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
Bram Moolenaar7c626922005-02-07 22:01:03 +00003176 mch_remove(fname);
3177 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 vim_free(cmd);
3179}
3180
3181/*
3182 * Return the name for the errorfile, in allocated memory.
3183 * Find a new unique name when 'makeef' contains "##".
3184 * Returns NULL for error.
3185 */
3186 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003187get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189 char_u *p;
3190 char_u *name;
3191 static int start = -1;
3192 static int off = 0;
3193#ifdef HAVE_LSTAT
3194 struct stat sb;
3195#endif
3196
3197 if (*p_mef == NUL)
3198 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003199 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (name == NULL)
3201 EMSG(_(e_notmp));
3202 return name;
3203 }
3204
3205 for (p = p_mef; *p; ++p)
3206 if (p[0] == '#' && p[1] == '#')
3207 break;
3208
3209 if (*p == NUL)
3210 return vim_strsave(p_mef);
3211
3212 /* Keep trying until the name doesn't exist yet. */
3213 for (;;)
3214 {
3215 if (start == -1)
3216 start = mch_get_pid();
3217 else
3218 off += 19;
3219
3220 name = alloc((unsigned)STRLEN(p_mef) + 30);
3221 if (name == NULL)
3222 break;
3223 STRCPY(name, p_mef);
3224 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3225 STRCAT(name, p + 2);
3226 if (mch_getperm(name) < 0
3227#ifdef HAVE_LSTAT
3228 /* Don't accept a symbolic link, its a security risk. */
3229 && mch_lstat((char *)name, &sb) < 0
3230#endif
3231 )
3232 break;
3233 vim_free(name);
3234 }
3235 return name;
3236}
3237
3238/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003239 * Returns the number of valid entries in the current quickfix/location list.
3240 */
3241 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003242qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003243{
3244 qf_info_T *qi = &ql_info;
3245 qfline_T *qfp;
3246 int i, sz = 0;
3247 int prev_fnum = 0;
3248
3249 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3250 {
3251 /* Location list */
3252 qi = GET_LOC_LIST(curwin);
3253 if (qi == NULL)
3254 return 0;
3255 }
3256
3257 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003258 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003259 ++i, qfp = qfp->qf_next)
3260 {
3261 if (qfp->qf_valid)
3262 {
3263 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3264 sz++; /* Count all valid entries */
3265 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3266 {
3267 /* Count the number of files */
3268 sz++;
3269 prev_fnum = qfp->qf_fnum;
3270 }
3271 }
3272 }
3273
3274 return sz;
3275}
3276
3277/*
3278 * Returns the current index of the quickfix/location list.
3279 * Returns 0 if there is an error.
3280 */
3281 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003282qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003283{
3284 qf_info_T *qi = &ql_info;
3285
3286 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3287 {
3288 /* Location list */
3289 qi = GET_LOC_LIST(curwin);
3290 if (qi == NULL)
3291 return 0;
3292 }
3293
3294 return qi->qf_lists[qi->qf_curlist].qf_index;
3295}
3296
3297/*
3298 * Returns the current index in the quickfix/location list (counting only valid
3299 * entries). If no valid entries are in the list, then returns 1.
3300 */
3301 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003302qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003303{
3304 qf_info_T *qi = &ql_info;
3305 qf_list_T *qfl;
3306 qfline_T *qfp;
3307 int i, eidx = 0;
3308 int prev_fnum = 0;
3309
3310 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3311 {
3312 /* Location list */
3313 qi = GET_LOC_LIST(curwin);
3314 if (qi == NULL)
3315 return 1;
3316 }
3317
3318 qfl = &qi->qf_lists[qi->qf_curlist];
3319 qfp = qfl->qf_start;
3320
3321 /* check if the list has valid errors */
3322 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3323 return 1;
3324
3325 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3326 {
3327 if (qfp->qf_valid)
3328 {
3329 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3330 {
3331 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3332 {
3333 /* Count the number of files */
3334 eidx++;
3335 prev_fnum = qfp->qf_fnum;
3336 }
3337 }
3338 else
3339 eidx++;
3340 }
3341 }
3342
3343 return eidx ? eidx : 1;
3344}
3345
3346/*
3347 * Get the 'n'th valid error entry in the quickfix or location list.
3348 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3349 * For :cdo and :ldo returns the 'n'th valid error entry.
3350 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3351 */
3352 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003353qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003354{
3355 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3356 qfline_T *qfp = qfl->qf_start;
3357 int i, eidx;
3358 int prev_fnum = 0;
3359
3360 /* check if the list has valid errors */
3361 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3362 return 1;
3363
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003364 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003365 i++, qfp = qfp->qf_next)
3366 {
3367 if (qfp->qf_valid)
3368 {
3369 if (fdo)
3370 {
3371 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3372 {
3373 /* Count the number of files */
3374 eidx++;
3375 prev_fnum = qfp->qf_fnum;
3376 }
3377 }
3378 else
3379 eidx++;
3380 }
3381
3382 if (eidx == n)
3383 break;
3384 }
3385
3386 if (i <= qfl->qf_count)
3387 return i;
3388 else
3389 return 1;
3390}
3391
3392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003394 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003395 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 */
3397 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003398ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003400 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003401 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003402
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003403 if (eap->cmdidx == CMD_ll
3404 || eap->cmdidx == CMD_lrewind
3405 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003406 || eap->cmdidx == CMD_llast
3407 || eap->cmdidx == CMD_ldo
3408 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003409 {
3410 qi = GET_LOC_LIST(curwin);
3411 if (qi == NULL)
3412 {
3413 EMSG(_(e_loclist));
3414 return;
3415 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003416 }
3417
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003418 if (eap->addr_count > 0)
3419 errornr = (int)eap->line2;
3420 else
3421 {
3422 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3423 errornr = 0;
3424 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3425 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3426 errornr = 1;
3427 else
3428 errornr = 32767;
3429 }
3430
3431 /* For cdo and ldo commands, jump to the nth valid error.
3432 * For cfdo and lfdo commands, jump to the nth valid file entry.
3433 */
3434 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3435 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3436 errornr = qf_get_nth_valid_entry(qi,
3437 eap->addr_count > 0 ? (int)eap->line1 : 1,
3438 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3439
3440 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441}
3442
3443/*
3444 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003445 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003446 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 */
3448 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003449ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003451 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003452 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003453
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003454 if (eap->cmdidx == CMD_lnext
3455 || eap->cmdidx == CMD_lNext
3456 || eap->cmdidx == CMD_lprevious
3457 || eap->cmdidx == CMD_lnfile
3458 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003459 || eap->cmdidx == CMD_lpfile
3460 || eap->cmdidx == CMD_ldo
3461 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003462 {
3463 qi = GET_LOC_LIST(curwin);
3464 if (qi == NULL)
3465 {
3466 EMSG(_(e_loclist));
3467 return;
3468 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003469 }
3470
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003471 if (eap->addr_count > 0 &&
3472 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3473 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3474 errornr = (int)eap->line2;
3475 else
3476 errornr = 1;
3477
3478 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3479 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003481 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3482 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003484 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3485 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 ? BACKWARD_FILE
3487 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003488 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489}
3490
3491/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003492 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003493 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 */
3495 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003496ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003498 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003499 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003500#ifdef FEAT_AUTOCMD
3501 char_u *au_name = NULL;
3502#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003503
3504 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003505 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003506 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003507
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003508#ifdef FEAT_AUTOCMD
3509 switch (eap->cmdidx)
3510 {
3511 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3512 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3513 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3514 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3515 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3516 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3517 default: break;
3518 }
3519 if (au_name != NULL)
3520 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3521#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003522#ifdef FEAT_BROWSE
3523 if (cmdmod.browse)
3524 {
3525 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3526 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3527 if (browse_file == NULL)
3528 return;
3529 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3530 vim_free(browse_file);
3531 }
3532 else
3533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003535 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003536
3537 /*
3538 * This function is used by the :cfile, :cgetfile and :caddfile
3539 * commands.
3540 * :cfile always creates a new quickfix list and jumps to the
3541 * first error.
3542 * :cgetfile creates a new quickfix list but doesn't jump to the
3543 * first error.
3544 * :caddfile adds to an existing quickfix list. If there is no
3545 * quickfix list then a new list is created.
3546 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003547 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003548 && eap->cmdidx != CMD_laddfile),
3549 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003550 && (eap->cmdidx == CMD_cfile
3551 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003552 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003553#ifdef FEAT_AUTOCMD
3554 if (au_name != NULL)
3555 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3556#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003557 if (wp != NULL)
3558 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003559 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003560 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003561
3562 else
3563 {
3564#ifdef FEAT_AUTOCMD
3565 if (au_name != NULL)
3566 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3567#endif
3568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569}
3570
3571/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003572 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003573 * ":vimgrepadd {pattern} file(s)"
3574 * ":lvimgrep {pattern} file(s)"
3575 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003576 */
3577 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003578ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003579{
Bram Moolenaar81695252004-12-29 20:58:21 +00003580 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003581 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003582 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003583 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003584 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003585 char_u *s;
3586 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003587 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003588 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003589#ifdef FEAT_AUTOCMD
3590 qfline_T *cur_qf_start;
3591#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003592 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003593 buf_T *buf;
3594 int duplicate_name = FALSE;
3595 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003596 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003597 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003598 buf_T *first_match_buf = NULL;
3599 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003600 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003601#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3602 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003603#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003604 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003605 int flags = 0;
3606 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003607 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003608 char_u *dirname_start = NULL;
3609 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003610 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003611#ifdef FEAT_AUTOCMD
3612 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003613
3614 switch (eap->cmdidx)
3615 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003616 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3617 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3618 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003619 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003620 case CMD_grep: au_name = (char_u *)"grep"; break;
3621 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3622 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3623 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003624 default: break;
3625 }
3626 if (au_name != NULL)
3627 {
3628 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3629 curbuf->b_fname, TRUE, curbuf);
3630 if (did_throw || force_abort)
3631 return;
3632 }
3633#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003634
Bram Moolenaar754b5602006-02-09 23:53:20 +00003635 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003636 || eap->cmdidx == CMD_lvimgrep
3637 || eap->cmdidx == CMD_lgrepadd
3638 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003639 {
3640 qi = ll_get_or_alloc_list(curwin);
3641 if (qi == NULL)
3642 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003643 }
3644
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003645 if (eap->addr_count > 0)
3646 tomatch = eap->line2;
3647 else
3648 tomatch = MAXLNUM;
3649
Bram Moolenaar81695252004-12-29 20:58:21 +00003650 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003651 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003652 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003653 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003654 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003655 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003656 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003657 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003658 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003659
3660 if (s != NULL && *s == NUL)
3661 {
3662 /* Pattern is empty, use last search pattern. */
3663 if (last_search_pat() == NULL)
3664 {
3665 EMSG(_(e_noprevre));
3666 goto theend;
3667 }
3668 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3669 }
3670 else
3671 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3672
Bram Moolenaar86b68352004-12-27 21:59:20 +00003673 if (regmatch.regprog == NULL)
3674 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003675 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003676 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003677
3678 p = skipwhite(p);
3679 if (*p == NUL)
3680 {
3681 EMSG(_("E683: File name missing or invalid pattern"));
3682 goto theend;
3683 }
3684
Bram Moolenaar754b5602006-02-09 23:53:20 +00003685 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003686 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003687 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003688 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003689 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003690
3691 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003692 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003693 goto theend;
3694 if (fcount == 0)
3695 {
3696 EMSG(_(e_nomatch));
3697 goto theend;
3698 }
3699
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003700 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3701 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003702 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003703 {
3704 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003705 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003706 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003707
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003708 /* Remember the current directory, because a BufRead autocommand that does
3709 * ":lcd %:p:h" changes the meaning of short path names. */
3710 mch_dirname(dirname_start, MAXPATHL);
3711
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003712#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003713 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003714 * changing the current quickfix list. */
3715 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3716#endif
3717
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003718 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003719 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003720 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003721 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003722 if (time(NULL) > seconds)
3723 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003724 /* Display the file name every second or so, show the user we are
3725 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003726 seconds = time(NULL);
3727 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003728 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003729 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003730 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003731 else
3732 {
3733 msg_outtrans(p);
3734 vim_free(p);
3735 }
3736 msg_clr_eos();
3737 msg_didout = FALSE; /* overwrite this message */
3738 msg_nowait = TRUE; /* don't wait for this message */
3739 msg_col = 0;
3740 out_flush();
3741 }
3742
Bram Moolenaar81695252004-12-29 20:58:21 +00003743 buf = buflist_findname_exp(fnames[fi]);
3744 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3745 {
3746 /* Remember that a buffer with this name already exists. */
3747 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003748 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003749 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003750
3751#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3752 /* Don't do Filetype autocommands to avoid loading syntax and
3753 * indent scripts, a great speed improvement. */
3754 save_ei = au_event_disable(",Filetype");
3755#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003756 /* Don't use modelines here, it's useless. */
3757 save_mls = p_mls;
3758 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003759
3760 /* Load file into a buffer, so that 'fileencoding' is detected,
3761 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003762 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003763
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003764 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003765#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3766 au_event_restore(save_ei);
3767#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003768 }
3769 else
3770 /* Use existing, loaded buffer. */
3771 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003772
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003773#ifdef FEAT_AUTOCMD
3774 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3775 {
3776 int idx;
3777
3778 /* Autocommands changed the quickfix list. Find the one we were
3779 * using and restore it. */
3780 for (idx = 0; idx < LISTCOUNT; ++idx)
3781 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3782 {
3783 qi->qf_curlist = idx;
3784 break;
3785 }
3786 if (idx == LISTCOUNT)
3787 {
3788 /* List cannot be found, create a new one. */
3789 qf_new_list(qi, *eap->cmdlinep);
3790 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3791 }
3792 }
3793#endif
3794
Bram Moolenaar81695252004-12-29 20:58:21 +00003795 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003796 {
3797 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003798 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003799 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003800 else
3801 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003802 /* Try for a match in all lines of the buffer.
3803 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003804 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003805 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3806 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003807 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003808 col = 0;
3809 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003810 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003811 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003812 ;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003813 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003814 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003815 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003816 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003817 ml_get_buf(buf,
3818 regmatch.startpos[0].lnum + lnum, FALSE),
3819 regmatch.startpos[0].lnum + lnum,
3820 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003821 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003822 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003823 0, /* nr */
3824 0, /* type */
3825 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003826 ) == FAIL)
3827 {
3828 got_int = TRUE;
3829 break;
3830 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003831 found_match = TRUE;
3832 if (--tomatch == 0)
3833 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003834 if ((flags & VGR_GLOBAL) == 0
3835 || regmatch.endpos[0].lnum > 0)
3836 break;
3837 col = regmatch.endpos[0].col
3838 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003839 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003840 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003841 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003842 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003843 if (got_int)
3844 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003845 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003846#ifdef FEAT_AUTOCMD
3847 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3848#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003849
3850 if (using_dummy)
3851 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003852 if (found_match && first_match_buf == NULL)
3853 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003854 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003855 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003856 /* Never keep a dummy buffer if there is another buffer
3857 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003858 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003859 buf = NULL;
3860 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003861 else if (!cmdmod.hide
3862 || buf->b_p_bh[0] == 'u' /* "unload" */
3863 || buf->b_p_bh[0] == 'w' /* "wipe" */
3864 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003865 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003866 /* When no match was found we don't need to remember the
3867 * buffer, wipe it out. If there was a match and it
3868 * wasn't the first one or we won't jump there: only
3869 * unload the buffer.
3870 * Ignore 'hidden' here, because it may lead to having too
3871 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003872 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003873 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003874 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003875 buf = NULL;
3876 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003877 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003878 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003879 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003880 buf = NULL;
3881 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003882 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003883
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003884 if (buf != NULL)
3885 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003886 /* If the buffer is still loaded we need to use the
3887 * directory we jumped to below. */
3888 if (buf == first_match_buf
3889 && target_dir == NULL
3890 && STRCMP(dirname_start, dirname_now) != 0)
3891 target_dir = vim_strsave(dirname_now);
3892
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003893 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003894 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003895 * need to be done (again). But not the window-local
3896 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003897 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003898#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003899 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3900 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003901#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003902 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003903 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003904 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003905 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003906 }
3907 }
3908
3909 FreeWild(fcount, fnames);
3910
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003911 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3912 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3913 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003914
3915#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02003916 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003917#endif
3918
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003919#ifdef FEAT_AUTOCMD
3920 if (au_name != NULL)
3921 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3922 curbuf->b_fname, TRUE, curbuf);
3923#endif
3924
Bram Moolenaar86b68352004-12-27 21:59:20 +00003925 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003926 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003927 {
3928 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003929 {
3930 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003931 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003932 if (buf != curbuf)
3933 /* If we jumped to another buffer redrawing will already be
3934 * taken care of. */
3935 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003936
3937 /* Jump to the directory used after loading the buffer. */
3938 if (curbuf == first_match_buf && target_dir != NULL)
3939 {
3940 exarg_T ea;
3941
3942 ea.arg = target_dir;
3943 ea.cmdidx = CMD_lcd;
3944 ex_cd(&ea);
3945 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003946 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003947 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003948 else
3949 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003950
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003951 /* If we loaded a dummy buffer into the current window, the autocommands
3952 * may have messed up things, need to redraw and recompute folds. */
3953 if (redraw_for_dummy)
3954 {
3955#ifdef FEAT_FOLDING
3956 foldUpdateAll(curwin);
3957#else
3958 redraw_later(NOT_VALID);
3959#endif
3960 }
3961
Bram Moolenaar86b68352004-12-27 21:59:20 +00003962theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01003963 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003964 vim_free(dirname_now);
3965 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003966 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003967 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003968}
3969
3970/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003971 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003972 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003973 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3974 * If "s" is not NULL terminate the pattern with a NUL.
3975 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003976 */
3977 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003978skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003979{
3980 int c;
3981
3982 if (vim_isIDc(*p))
3983 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003984 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003985 if (s != NULL)
3986 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003987 p = skiptowhite(p);
3988 if (s != NULL && *p != NUL)
3989 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003990 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003991 else
3992 {
3993 /* ":vimgrep /pattern/[g][j] fname" */
3994 if (s != NULL)
3995 *s = p + 1;
3996 c = *p;
3997 p = skip_regexp(p + 1, c, TRUE, NULL);
3998 if (*p != c)
3999 return NULL;
4000
4001 /* Truncate the pattern. */
4002 if (s != NULL)
4003 *p = NUL;
4004 ++p;
4005
4006 /* Find the flags */
4007 while (*p == 'g' || *p == 'j')
4008 {
4009 if (flags != NULL)
4010 {
4011 if (*p == 'g')
4012 *flags |= VGR_GLOBAL;
4013 else
4014 *flags |= VGR_NOJUMP;
4015 }
4016 ++p;
4017 }
4018 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004019 return p;
4020}
4021
4022/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004023 * Restore current working directory to "dirname_start" if they differ, taking
4024 * into account whether it is set locally or globally.
4025 */
4026 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004027restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004028{
4029 char_u *dirname_now = alloc(MAXPATHL);
4030
4031 if (NULL != dirname_now)
4032 {
4033 mch_dirname(dirname_now, MAXPATHL);
4034 if (STRCMP(dirname_start, dirname_now) != 0)
4035 {
4036 /* If the directory has changed, change it back by building up an
4037 * appropriate ex command and executing it. */
4038 exarg_T ea;
4039
4040 ea.arg = dirname_start;
4041 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4042 ex_cd(&ea);
4043 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004044 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004045 }
4046}
4047
4048/*
4049 * Load file "fname" into a dummy buffer and return the buffer pointer,
4050 * placing the directory resulting from the buffer load into the
4051 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4052 * prior to calling this function. Restores directory to "dirname_start" prior
4053 * to returning, if autocmds or the 'autochdir' option have changed it.
4054 *
4055 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4056 * or wipe_dummy_buffer() later!
4057 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004058 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004059 */
4060 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004061load_dummy_buffer(
4062 char_u *fname,
4063 char_u *dirname_start, /* in: old directory */
4064 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004065{
4066 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004067 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004068 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004069 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004070
4071 /* Allocate a buffer without putting it in the buffer list. */
4072 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4073 if (newbuf == NULL)
4074 return NULL;
4075
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004076 /* Init the options. */
4077 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4078
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004079 /* need to open the memfile before putting the buffer in a window */
4080 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004081 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004082 /* set curwin/curbuf to buf and save a few things */
4083 aucmd_prepbuf(&aco, newbuf);
4084
4085 /* Need to set the filename for autocommands. */
4086 (void)setfname(curbuf, fname, NULL, FALSE);
4087
Bram Moolenaar81695252004-12-29 20:58:21 +00004088 /* Create swap file now to avoid the ATTENTION message. */
4089 check_need_swap(TRUE);
4090
4091 /* Remove the "dummy" flag, otherwise autocommands may not
4092 * work. */
4093 curbuf->b_flags &= ~BF_DUMMY;
4094
4095 if (readfile(fname, NULL,
4096 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4097 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004098 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004099 && !(curbuf->b_flags & BF_NEW))
4100 {
4101 failed = FALSE;
4102 if (curbuf != newbuf)
4103 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004104 /* Bloody autocommands changed the buffer! Can happen when
4105 * using netrw and editing a remote file. Use the current
4106 * buffer instead, delete the dummy one after restoring the
4107 * window stuff. */
4108 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004109 newbuf = curbuf;
4110 }
4111 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004112
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004113 /* restore curwin/curbuf and a few other things */
4114 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004115 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
4116 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004117 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004118
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004119 /*
4120 * When autocommands/'autochdir' option changed directory: go back.
4121 * Let the caller know what the resulting dir was first, in case it is
4122 * important.
4123 */
4124 mch_dirname(resulting_dir, MAXPATHL);
4125 restore_start_dir(dirname_start);
4126
Bram Moolenaar81695252004-12-29 20:58:21 +00004127 if (!buf_valid(newbuf))
4128 return NULL;
4129 if (failed)
4130 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004131 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004132 return NULL;
4133 }
4134 return newbuf;
4135}
4136
4137/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004138 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4139 * directory to "dirname_start" prior to returning, if autocmds or the
4140 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004141 */
4142 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004143wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004144{
4145 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004146 {
4147#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4148 cleanup_T cs;
4149
4150 /* Reset the error/interrupt/exception state here so that aborting()
4151 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4152 * work when got_int is set. */
4153 enter_cleanup(&cs);
4154#endif
4155
Bram Moolenaar81695252004-12-29 20:58:21 +00004156 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004157
4158#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4159 /* Restore the error/interrupt/exception state if not discarded by a
4160 * new aborting error, interrupt, or uncaught exception. */
4161 leave_cleanup(&cs);
4162#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004163 /* When autocommands/'autochdir' option changed directory: go back. */
4164 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004165 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004166}
4167
4168/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004169 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4170 * directory to "dirname_start" prior to returning, if autocmds or the
4171 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004172 */
4173 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004174unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004175{
4176 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004177 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004178 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004179
4180 /* When autocommands/'autochdir' option changed directory: go back. */
4181 restore_start_dir(dirname_start);
4182 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004183}
4184
Bram Moolenaar05159a02005-02-26 23:04:13 +00004185#if defined(FEAT_EVAL) || defined(PROTO)
4186/*
4187 * Add each quickfix error to list "list" as a dictionary.
4188 */
4189 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004190get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004191{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004192 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004193 dict_T *dict;
4194 char_u buf[2];
4195 qfline_T *qfp;
4196 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004197 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004198
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004199 if (wp != NULL)
4200 {
4201 qi = GET_LOC_LIST(wp);
4202 if (qi == NULL)
4203 return FAIL;
4204 }
4205
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004206 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004207 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004208 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004209
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004210 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4211 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004212 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004213 /* Handle entries with a non-existing buffer number. */
4214 bufnum = qfp->qf_fnum;
4215 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4216 bufnum = 0;
4217
Bram Moolenaar05159a02005-02-26 23:04:13 +00004218 if ((dict = dict_alloc()) == NULL)
4219 return FAIL;
4220 if (list_append_dict(list, dict) == FAIL)
4221 return FAIL;
4222
4223 buf[0] = qfp->qf_type;
4224 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004225 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004226 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4227 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4228 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4229 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004230 || dict_add_nr_str(dict, "pattern", 0L,
4231 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4232 || dict_add_nr_str(dict, "text", 0L,
4233 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004234 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4235 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4236 return FAIL;
4237
4238 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004239 if (qfp == NULL)
4240 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004241 }
4242 return OK;
4243}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004244
4245/*
4246 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004247 * of dictionaries. "title" will be copied to w:quickfix_title.
4248 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004249 */
4250 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004251set_errorlist(
4252 win_T *wp,
4253 list_T *list,
4254 int action,
4255 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004256{
4257 listitem_T *li;
4258 dict_T *d;
4259 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004260 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004261 long lnum;
4262 int col, nr;
4263 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004264#ifdef FEAT_WINDOWS
4265 qfline_T *old_last = NULL;
4266#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004267 int valid, status;
4268 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004269 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004270 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004271
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004272 if (wp != NULL)
4273 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004274 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004275 if (qi == NULL)
4276 return FAIL;
4277 }
4278
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004279 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004280 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004281 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004282#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004283 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4284 /* Adding to existing list, use last entry. */
4285 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004286#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004287 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004288 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004289 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004290 qf_store_title(qi, title);
4291 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004292
4293 for (li = list->lv_first; li != NULL; li = li->li_next)
4294 {
4295 if (li->li_tv.v_type != VAR_DICT)
4296 continue; /* Skip non-dict items */
4297
4298 d = li->li_tv.vval.v_dict;
4299 if (d == NULL)
4300 continue;
4301
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004302 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004303 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004304 lnum = get_dict_number(d, (char_u *)"lnum");
4305 col = get_dict_number(d, (char_u *)"col");
4306 vcol = get_dict_number(d, (char_u *)"vcol");
4307 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004308 type = get_dict_string(d, (char_u *)"type", TRUE);
4309 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4310 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004311 if (text == NULL)
4312 text = vim_strsave((char_u *)"");
4313
4314 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004315 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004316 valid = FALSE;
4317
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004318 /* Mark entries with non-existing buffer number as not valid. Give the
4319 * error message only once. */
4320 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4321 {
4322 if (!did_bufnr_emsg)
4323 {
4324 did_bufnr_emsg = TRUE;
4325 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4326 }
4327 valid = FALSE;
4328 bufnum = 0;
4329 }
4330
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004331 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004332 NULL, /* dir */
4333 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004334 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004335 text,
4336 lnum,
4337 col,
4338 vcol, /* vis_col */
4339 pattern, /* search pattern */
4340 nr,
4341 type == NULL ? NUL : *type,
4342 valid);
4343
4344 vim_free(filename);
4345 vim_free(pattern);
4346 vim_free(text);
4347 vim_free(type);
4348
4349 if (status == FAIL)
4350 {
4351 retval = FAIL;
4352 break;
4353 }
4354 }
4355
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004356 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004357 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004358 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4359 else
4360 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004361 if (action != 'a') {
4362 qi->qf_lists[qi->qf_curlist].qf_ptr =
4363 qi->qf_lists[qi->qf_curlist].qf_start;
4364 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4365 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4366 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004367
4368#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004369 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004370 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004371#endif
4372
4373 return retval;
4374}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004375#endif
4376
Bram Moolenaar81695252004-12-29 20:58:21 +00004377/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004378 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004379 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004380 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004381 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004382 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004383 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004384 */
4385 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004386ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004387{
4388 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004389 qf_info_T *qi = &ql_info;
4390
Bram Moolenaardb552d602006-03-23 22:59:57 +00004391 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4392 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004393 {
4394 qi = ll_get_or_alloc_list(curwin);
4395 if (qi == NULL)
4396 return;
4397 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004398
4399 if (*eap->arg == NUL)
4400 buf = curbuf;
4401 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4402 buf = buflist_findnr(atoi((char *)eap->arg));
4403 if (buf == NULL)
4404 EMSG(_(e_invarg));
4405 else if (buf->b_ml.ml_mfp == NULL)
4406 EMSG(_("E681: Buffer is not loaded"));
4407 else
4408 {
4409 if (eap->addr_count == 0)
4410 {
4411 eap->line1 = 1;
4412 eap->line2 = buf->b_ml.ml_line_count;
4413 }
4414 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4415 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4416 EMSG(_(e_invrange));
4417 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004418 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004419 char_u *qf_title = *eap->cmdlinep;
4420
4421 if (buf->b_sfname)
4422 {
4423 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4424 (char *)qf_title, (char *)buf->b_sfname);
4425 qf_title = IObuff;
4426 }
4427
Bram Moolenaardb552d602006-03-23 22:59:57 +00004428 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4429 (eap->cmdidx != CMD_caddbuffer
4430 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004431 eap->line1, eap->line2,
4432 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004433 && (eap->cmdidx == CMD_cbuffer
4434 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004435 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4436 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004437 }
4438}
4439
Bram Moolenaar1e015462005-09-25 22:16:38 +00004440#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004441/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004442 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4443 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004444 */
4445 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004446ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004447{
4448 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004449 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004450
Bram Moolenaardb552d602006-03-23 22:59:57 +00004451 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4452 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004453 {
4454 qi = ll_get_or_alloc_list(curwin);
4455 if (qi == NULL)
4456 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004457 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004458
Bram Moolenaar4770d092006-01-12 23:22:24 +00004459 /* Evaluate the expression. When the result is a string or a list we can
4460 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004461 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004462 if (tv != NULL)
4463 {
4464 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4465 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4466 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004467 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004468 (eap->cmdidx != CMD_caddexpr
4469 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004470 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004471 && (eap->cmdidx == CMD_cexpr
4472 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004473 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004474 }
4475 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004476 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004477 free_tv(tv);
4478 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004479}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004480#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004481
4482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 * ":helpgrep {pattern}"
4484 */
4485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004486ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
4488 regmatch_T regmatch;
4489 char_u *save_cpo;
4490 char_u *p;
4491 int fcount;
4492 char_u **fnames;
4493 FILE *fd;
4494 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004496#ifdef FEAT_MULTI_LANG
4497 char_u *lang;
4498#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004499 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004500 int new_qi = FALSE;
4501 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004502#ifdef FEAT_AUTOCMD
4503 char_u *au_name = NULL;
4504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004506#ifdef FEAT_MULTI_LANG
4507 /* Check for a specified language */
4508 lang = check_help_lang(eap->arg);
4509#endif
4510
Bram Moolenaar73633f82012-01-20 13:39:07 +01004511#ifdef FEAT_AUTOCMD
4512 switch (eap->cmdidx)
4513 {
4514 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4515 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4516 default: break;
4517 }
4518 if (au_name != NULL)
4519 {
4520 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4521 curbuf->b_fname, TRUE, curbuf);
4522 if (did_throw || force_abort)
4523 return;
4524 }
4525#endif
4526
4527 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4528 save_cpo = p_cpo;
4529 p_cpo = empty_option;
4530
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004531 if (eap->cmdidx == CMD_lhelpgrep)
4532 {
4533 /* Find an existing help window */
4534 FOR_ALL_WINDOWS(wp)
4535 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4536 break;
4537
4538 if (wp == NULL) /* Help window not found */
4539 qi = NULL;
4540 else
4541 qi = wp->w_llist;
4542
4543 if (qi == NULL)
4544 {
4545 /* Allocate a new location list for help text matches */
4546 if ((qi = ll_new_list()) == NULL)
4547 return;
4548 new_qi = TRUE;
4549 }
4550 }
4551
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4553 regmatch.rm_ic = FALSE;
4554 if (regmatch.regprog != NULL)
4555 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004556#ifdef FEAT_MBYTE
4557 vimconv_T vc;
4558
4559 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4560 * differs. */
4561 vc.vc_type = CONV_NONE;
4562 if (!enc_utf8)
4563 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4564#endif
4565
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004567 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569 /* Go through all directories in 'runtimepath' */
4570 p = p_rtp;
4571 while (*p != NUL && !got_int)
4572 {
4573 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4574
4575 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4576 add_pathsep(NameBuff);
4577 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4578 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4579 &fnames, EW_FILE|EW_SILENT) == OK
4580 && fcount > 0)
4581 {
4582 for (fi = 0; fi < fcount && !got_int; ++fi)
4583 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004584#ifdef FEAT_MULTI_LANG
4585 /* Skip files for a different language. */
4586 if (lang != NULL
4587 && STRNICMP(lang, fnames[fi]
4588 + STRLEN(fnames[fi]) - 3, 2) != 0
4589 && !(STRNICMP(lang, "en", 2) == 0
4590 && STRNICMP("txt", fnames[fi]
4591 + STRLEN(fnames[fi]) - 3, 3) == 0))
4592 continue;
4593#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004594 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 if (fd != NULL)
4596 {
4597 lnum = 1;
4598 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4599 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004600 char_u *line = IObuff;
4601#ifdef FEAT_MBYTE
4602 /* Convert a line if 'encoding' is not utf-8 and
4603 * the line contains a non-ASCII character. */
4604 if (vc.vc_type != CONV_NONE
4605 && has_non_ascii(IObuff)) {
4606 line = string_convert(&vc, IObuff, NULL);
4607 if (line == NULL)
4608 line = IObuff;
4609 }
4610#endif
4611
4612 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004614 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004617 while (l > 0 && line[l - 1] <= ' ')
4618 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004620 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 NULL, /* dir */
4622 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004623 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004624 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004626 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004627 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004628 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004629 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 0, /* nr */
4631 1, /* type */
4632 TRUE /* valid */
4633 ) == FAIL)
4634 {
4635 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004636#ifdef FEAT_MBYTE
4637 if (line != IObuff)
4638 vim_free(line);
4639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 break;
4641 }
4642 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004643#ifdef FEAT_MBYTE
4644 if (line != IObuff)
4645 vim_free(line);
4646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 ++lnum;
4648 line_breakcheck();
4649 }
4650 fclose(fd);
4651 }
4652 }
4653 FreeWild(fcount, fnames);
4654 }
4655 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004656
Bram Moolenaar473de612013-06-08 18:19:48 +02004657 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004658#ifdef FEAT_MBYTE
4659 if (vc.vc_type != CONV_NONE)
4660 convert_setup(&vc, NULL, NULL);
4661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004663 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4664 qi->qf_lists[qi->qf_curlist].qf_ptr =
4665 qi->qf_lists[qi->qf_curlist].qf_start;
4666 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
4668
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004669 if (p_cpo == empty_option)
4670 p_cpo = save_cpo;
4671 else
4672 /* Darn, some plugin changed the value. */
4673 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
4675#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004676 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677#endif
4678
Bram Moolenaar73633f82012-01-20 13:39:07 +01004679#ifdef FEAT_AUTOCMD
4680 if (au_name != NULL)
4681 {
4682 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4683 curbuf->b_fname, TRUE, curbuf);
4684 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4685 /* autocommands made "qi" invalid */
4686 return;
4687 }
4688#endif
4689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004691 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004692 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004693 else
4694 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004695
4696 if (eap->cmdidx == CMD_lhelpgrep)
4697 {
4698 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004699 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004700 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4701 {
4702 if (new_qi)
4703 ll_free_all(&qi);
4704 }
4705 else if (curwin->w_llist == NULL)
4706 curwin->w_llist = qi;
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708}
4709
4710#endif /* FEAT_QUICKFIX */