blob: a719e4fe0a75b446ed6f5e4c5de1ae8146817dba [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
24static struct dir_stack_T *dir_stack = NULL;
25
26/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000029typedef struct qfline_S qfline_T;
30struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000031{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000032 qfline_T *qf_next; /* pointer to next error in the list */
33 qfline_T *qf_prev; /* pointer to previous error in the list */
34 linenr_T qf_lnum; /* line number where the error occurred */
35 int qf_fnum; /* file number for the line */
36 int qf_col; /* column where the error occurred */
37 int qf_nr; /* error number */
38 char_u *qf_pattern; /* search pattern for the error */
39 char_u *qf_text; /* description of the error */
40 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
41 char_u qf_cleared; /* set to TRUE if line has been deleted */
42 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000044 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
47/*
48 * There is a stack of error lists.
49 */
50#define LISTCOUNT 10
51
Bram Moolenaard12f5c12006-01-25 22:10:52 +000052typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_start; /* pointer to the first error */
55 qfline_T *qf_ptr; /* pointer to the current error */
56 int qf_count; /* number of errors (0 means no error list) */
57 int qf_index; /* current index in the error list */
58 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059 char_u *qf_title; /* title derived from the command that created
60 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaard12f5c12006-01-25 22:10:52 +000063struct qf_info_S
64{
65 /*
66 * Count of references to this list. Used only for location lists.
67 * When a location list window reference this list, qf_refcount
68 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
69 * reaches 0, the list is freed.
70 */
71 int qf_refcount;
72 int qf_listcount; /* current number of lists */
73 int qf_curlist; /* current error list */
74 qf_list_T qf_lists[LISTCOUNT];
75};
76
77static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar68b76a62005-03-25 21:53:48 +000079#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/*
82 * Structure used to hold the info of one part of 'errorformat'
83 */
Bram Moolenaar01265852006-03-20 21:50:15 +000084typedef struct efm_S efm_T;
85struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000086{
87 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000088 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
90 char_u prefix; /* prefix of this format line: */
91 /* 'D' enter directory */
92 /* 'X' leave directory */
93 /* 'A' start of multi-line message */
94 /* 'E' error message */
95 /* 'W' warning message */
96 /* 'I' informational message */
97 /* 'C' continuation line */
98 /* 'Z' end of multi-line message */
99 /* 'G' general, unspecific message */
100 /* 'P' push file (partial) message */
101 /* 'Q' pop/quit file (partial) message */
102 /* 'O' overread (partial) message */
103 char_u flags; /* additional flags given in prefix */
104 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000105 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000106 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107};
108
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100109static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title);
110static void qf_store_title(qf_info_T *qi, char_u *title);
111static void qf_new_list(qf_info_T *qi, char_u *qf_title);
112static void ll_free_all(qf_info_T **pqi);
113static int qf_add_entry(qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
114static qf_info_T *ll_new_list(void);
115static void qf_msg(qf_info_T *qi);
116static void qf_free(qf_info_T *qi, int idx);
117static char_u *qf_types(int, int);
118static int qf_get_fnum(char_u *, char_u *);
119static char_u *qf_push_dir(char_u *, struct dir_stack_T **);
120static char_u *qf_pop_dir(struct dir_stack_T **);
121static char_u *qf_guess_filepath(char_u *);
122static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
123static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100125static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
126static int is_qf_win(win_T *win, qf_info_T *qi);
127static win_T *qf_find_win(qf_info_T *qi);
128static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200129static void qf_update_buffer(qf_info_T *qi, int update_cursor);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void qf_set_title_var(qf_info_T *qi);
131static void qf_fill_buffer(qf_info_T *qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static char_u *get_mef_name(void);
134static void restore_start_dir(char_u *dirname_start);
135static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
136static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
137static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
138static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000140/* Quickfix window check helper macro */
141#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
142/* Location list window check helper macro */
143#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
144/*
145 * Return location list for window 'wp'
146 * For location list window, return the referenced location list
147 */
148#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
149
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000151 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200152 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 * Return -1 for error, number of errors for success.
154 */
155 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100156qf_init(
157 win_T *wp,
158 char_u *efile,
159 char_u *errorformat,
160 int newlist, /* TRUE: start a new error list */
161 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163 qf_info_T *qi = &ql_info;
164
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000165 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000166 {
167 qi = ll_get_or_alloc_list(wp);
168 if (qi == NULL)
169 return FAIL;
170 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000171
172 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200173 (linenr_T)0, (linenr_T)0,
174 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000175}
176
177/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200178 * Maximum number of bytes allowed per line while reading a errorfile.
179 */
180#define LINE_MAXLEN 4096
181
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200182 static char_u *
183qf_grow_linebuf(char_u **growbuf, int *growbufsiz, int newsz, int *allocsz)
184{
185 /*
186 * If the line exceeds LINE_MAXLEN exclude the last
187 * byte since it's not a NL character.
188 */
189 *allocsz = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
190 if (*growbuf == NULL)
191 {
192 *growbuf = alloc(*allocsz + 1);
193 if (*growbuf == NULL)
194 return NULL;
195 *growbufsiz = *allocsz;
196 }
197 else if (*allocsz > *growbufsiz)
198 {
199 *growbuf = vim_realloc(*growbuf, *allocsz + 1);
200 if (*growbuf == NULL)
201 return NULL;
202 *growbufsiz = *allocsz;
203 }
204 return *growbuf;
205}
206
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200207/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000208 * Read the errorfile "efile" into memory, line by line, building the error
209 * list.
210 * Alternative: when "efile" is null read errors from buffer "buf".
211 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200212 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
213 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000214 * Return -1 for error, number of errors for success.
215 */
216 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100217qf_init_ext(
218 qf_info_T *qi,
219 char_u *efile,
220 buf_T *buf,
221 typval_T *tv,
222 char_u *errorformat,
223 int newlist, /* TRUE: start a new error list */
224 linenr_T lnumfirst, /* first line number to use */
225 linenr_T lnumlast, /* last line number to use */
226 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000227{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 char_u *namebuf;
229 char_u *errmsg;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200230 int errmsglen;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000231 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 char_u *fmtstr = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200233 char_u *growbuf = NULL;
234 int growbuflen;
Bram Moolenaar9a3b3312016-05-01 20:20:49 +0200235 int growbufsiz = 0;
236 char_u *linebuf = NULL;
237 int linelen = 0;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200238 int discard;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000240 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 int type = 0;
242 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000243 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 long lnum = 0L;
245 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000246 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000247 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000249 efm_T *fmt_first = NULL;
250 efm_T *fmt_last = NULL;
251 efm_T *fmt_ptr;
252 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 char_u *efm;
254 char_u *ptr;
255 char_u *srcptr;
256 int len;
257 int i;
258 int round;
259 int idx = 0;
260 int multiline = FALSE;
261 int multiignore = FALSE;
262 int multiscan = FALSE;
263 int retval = -1; /* default: return error flag */
264 char_u *directory = NULL;
265 char_u *currfile = NULL;
266 char_u *tail = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200267 char_u *p_buf = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000268 char_u *p_str = NULL;
269 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 struct dir_stack_T *file_stack = NULL;
271 regmatch_T regmatch;
272 static struct fmtpattern
273 {
274 char_u convchar;
275 char *pattern;
276 } fmt_pat[FMT_PATTERNS] =
277 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000278 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 {'n', "\\d\\+"},
280 {'l', "\\d\\+"},
281 {'c', "\\d\\+"},
282 {'t', "."},
283 {'m', ".\\+"},
284 {'r', ".*"},
Bram Moolenaarf13de072012-06-01 18:34:41 +0200285 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000286 {'v', "\\d\\+"},
287 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 };
289
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100290 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200291 errmsglen = CMDBUFFSIZE + 1;
292 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100293 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000294 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 goto qf_init_end;
296
Bram Moolenaar86b68352004-12-27 21:59:20 +0000297 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {
299 EMSG2(_(e_openerrf), efile);
300 goto qf_init_end;
301 }
302
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000303 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100305 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000306 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000308 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200309 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 ;
311
312/*
313 * Each part of the format string is copied and modified from errorformat to
314 * regex prog. Only a few % characters are allowed.
315 */
316 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000317 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000318 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 else
320 efm = errorformat;
321 /*
322 * Get some space to modify the format string into.
323 */
324 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
325 for (round = FMT_PATTERNS; round > 0; )
326 i += (int)STRLEN(fmt_pat[--round].pattern);
327#ifdef COLON_IN_FILENAME
328 i += 12; /* "%f" can become twelve chars longer */
329#else
330 i += 2; /* "%f" can become two chars longer */
331#endif
332 if ((fmtstr = alloc(i)) == NULL)
333 goto error2;
334
Bram Moolenaar01265852006-03-20 21:50:15 +0000335 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 {
337 /*
338 * Allocate a new eformat structure and put it at the end of the list
339 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000340 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 if (fmt_ptr == NULL)
342 goto error2;
343 if (fmt_first == NULL) /* first one */
344 fmt_first = fmt_ptr;
345 else
346 fmt_last->next = fmt_ptr;
347 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
349 /*
350 * Isolate one part in the 'errorformat' option
351 */
352 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
353 if (efm[len] == '\\' && efm[len + 1] != NUL)
354 ++len;
355
356 /*
357 * Build regexp pattern from current 'errorformat' option
358 */
359 ptr = fmtstr;
360 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000361 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 for (efmp = efm; efmp < efm + len; ++efmp)
363 {
364 if (*efmp == '%')
365 {
366 ++efmp;
367 for (idx = 0; idx < FMT_PATTERNS; ++idx)
368 if (fmt_pat[idx].convchar == *efmp)
369 break;
370 if (idx < FMT_PATTERNS)
371 {
372 if (fmt_ptr->addr[idx])
373 {
374 sprintf((char *)errmsg,
375 _("E372: Too many %%%c in format string"), *efmp);
376 EMSG(errmsg);
377 goto error2;
378 }
379 if ((idx
380 && idx < 6
381 && vim_strchr((char_u *)"DXOPQ",
382 fmt_ptr->prefix) != NULL)
383 || (idx == 6
384 && vim_strchr((char_u *)"OPQ",
385 fmt_ptr->prefix) == NULL))
386 {
387 sprintf((char *)errmsg,
388 _("E373: Unexpected %%%c in format string"), *efmp);
389 EMSG(errmsg);
390 goto error2;
391 }
392 fmt_ptr->addr[idx] = (char_u)++round;
393 *ptr++ = '\\';
394 *ptr++ = '(';
395#ifdef BACKSLASH_IN_FILENAME
396 if (*efmp == 'f')
397 {
398 /* Also match "c:" in the file name, even when
399 * checking for a colon next: "%f:".
400 * "\%(\a:\)\=" */
401 STRCPY(ptr, "\\%(\\a:\\)\\=");
402 ptr += 10;
403 }
404#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000405 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000407 if (efmp[1] != '\\' && efmp[1] != '%')
408 {
409 /* A file name may contain spaces, but this isn't
410 * in "\f". For "%f:%l:%m" there may be a ":" in
411 * the file name. Use ".\{-1,}x" instead (x is
412 * the next character), the requirement that :999:
413 * follows should work. */
414 STRCPY(ptr, ".\\{-1,}");
415 ptr += 7;
416 }
417 else
418 {
419 /* File name followed by '\\' or '%': include as
420 * many file name chars as possible. */
421 STRCPY(ptr, "\\f\\+");
422 ptr += 4;
423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 else
426 {
427 srcptr = (char_u *)fmt_pat[idx].pattern;
428 while ((*ptr = *srcptr++) != NUL)
429 ++ptr;
430 }
431 *ptr++ = '\\';
432 *ptr++ = ')';
433 }
434 else if (*efmp == '*')
435 {
436 if (*++efmp == '[' || *efmp == '\\')
437 {
438 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
439 {
440 if (efmp[1] == '^')
441 *ptr++ = *++efmp;
442 if (efmp < efm + len)
443 {
444 *ptr++ = *++efmp; /* could be ']' */
445 while (efmp < efm + len
446 && (*ptr++ = *++efmp) != ']')
447 /* skip */;
448 if (efmp == efm + len)
449 {
450 EMSG(_("E374: Missing ] in format string"));
451 goto error2;
452 }
453 }
454 }
455 else if (efmp < efm + len) /* %*\D, %*\s etc. */
456 *ptr++ = *++efmp;
457 *ptr++ = '\\';
458 *ptr++ = '+';
459 }
460 else
461 {
462 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
463 sprintf((char *)errmsg,
464 _("E375: Unsupported %%%c in format string"), *efmp);
465 EMSG(errmsg);
466 goto error2;
467 }
468 }
469 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
470 *ptr++ = *efmp; /* regexp magic characters */
471 else if (*efmp == '#')
472 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000473 else if (*efmp == '>')
474 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 else if (efmp == efm + 1) /* analyse prefix */
476 {
477 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
478 fmt_ptr->flags = *efmp++;
479 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
480 fmt_ptr->prefix = *efmp;
481 else
482 {
483 sprintf((char *)errmsg,
484 _("E376: Invalid %%%c in format string prefix"), *efmp);
485 EMSG(errmsg);
486 goto error2;
487 }
488 }
489 else
490 {
491 sprintf((char *)errmsg,
492 _("E377: Invalid %%%c in format string"), *efmp);
493 EMSG(errmsg);
494 goto error2;
495 }
496 }
497 else /* copy normal character */
498 {
499 if (*efmp == '\\' && efmp + 1 < efm + len)
500 ++efmp;
501 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
502 *ptr++ = '\\'; /* escape regexp atoms */
503 if (*efmp)
504 *ptr++ = *efmp;
505 }
506 }
507 *ptr++ = '$';
508 *ptr = NUL;
509 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
510 goto error2;
511 /*
512 * Advance to next part
513 */
514 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
515 }
516 if (fmt_first == NULL) /* nothing found */
517 {
518 EMSG(_("E378: 'errorformat' contains no pattern"));
519 goto error2;
520 }
521
522 /*
523 * got_int is reset here, because it was probably set when killing the
524 * ":make" command, but we still want to read the errorfile then.
525 */
526 got_int = FALSE;
527
528 /* Always ignore case when looking for a matching error. */
529 regmatch.rm_ic = TRUE;
530
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000531 if (tv != NULL)
532 {
533 if (tv->v_type == VAR_STRING)
534 p_str = tv->vval.v_string;
535 else if (tv->v_type == VAR_LIST)
536 p_li = tv->vval.v_list->lv_first;
537 }
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 /*
540 * Read the lines in the error file one by one.
541 * Try to recognize one of the error formats in each line.
542 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000543 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000545 /* Get the next line. */
546 if (fd == NULL)
547 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000548 if (tv != NULL)
549 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000550 if (tv->v_type == VAR_STRING)
551 {
552 /* Get the next line from the supplied string */
553 char_u *p;
554
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200555 if (*p_str == NUL) /* Reached the end of the string */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000556 break;
557
558 p = vim_strchr(p_str, '\n');
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200559 if (p != NULL)
560 len = (int)(p - p_str) + 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000561 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000562 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000563
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200564 if (len > IOSIZE - 2)
565 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200566 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
567 &linelen);
568 if (linebuf == NULL)
569 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200570 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000571 else
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200572 {
573 linebuf = IObuff;
574 linelen = len;
575 }
576 vim_strncpy(linebuf, p_str, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000577
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200578 /*
579 * Increment using len in order to discard the rest of the
580 * line if it exceeds LINE_MAXLEN.
581 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000582 p_str += len;
583 }
584 else if (tv->v_type == VAR_LIST)
585 {
586 /* Get the next line from the supplied list */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200587 while (p_li != NULL
588 && (p_li->li_tv.v_type != VAR_STRING
589 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000590 p_li = p_li->li_next; /* Skip non-string items */
591
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200592 if (p_li == NULL) /* End of the list */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000593 break;
594
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000595 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200596 if (len > IOSIZE - 2)
597 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200598 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
599 &linelen);
600 if (linebuf == NULL)
601 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200602 }
603 else
604 {
605 linebuf = IObuff;
606 linelen = len;
607 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000608
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200609 vim_strncpy(linebuf, p_li->li_tv.vval.v_string, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000610
611 p_li = p_li->li_next; /* next item */
612 }
613 }
614 else
615 {
616 /* Get the next line from the supplied buffer */
617 if (buflnum > lnumlast)
618 break;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200619 p_buf = ml_get_buf(buf, buflnum++, FALSE);
620 linelen = (int)STRLEN(p_buf);
621 if (linelen > IOSIZE - 2)
622 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200623 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
624 &linelen);
625 if (linebuf == NULL)
626 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200627 }
628 else
629 linebuf = IObuff;
630 vim_strncpy(linebuf, p_buf, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000631 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000632 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200633 else
634 {
635 if (fgets((char *)IObuff, IOSIZE, fd) == NULL)
636 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000637
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200638 discard = FALSE;
639 linelen = (int)STRLEN(IObuff);
640 if (linelen == IOSIZE - 1 && (IObuff[linelen - 1] != '\n'
641#ifdef USE_CRNL
642 || IObuff[linelen - 1] != '\r'
643#endif
644 ))
645 {
646 /*
647 * The current line exceeds IObuff, continue reading using
648 * growbuf until EOL or LINE_MAXLEN bytes is read.
649 */
650 if (growbuf == NULL)
651 {
652 growbufsiz = 2 * (IOSIZE - 1);
653 growbuf = alloc(growbufsiz);
654 if (growbuf == NULL)
655 goto qf_init_end;
656 }
657
658 /* Copy the read part of the line, excluding null-terminator */
659 memcpy(growbuf, IObuff, IOSIZE - 1);
660 growbuflen = linelen;
661
662 for (;;)
663 {
664 if (fgets((char *)growbuf + growbuflen,
665 growbufsiz - growbuflen, fd) == NULL)
666 break;
Bram Moolenaard9db8b42016-05-08 12:52:05 +0200667 linelen = (int)STRLEN(growbuf + growbuflen);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200668 growbuflen += linelen;
669 if (growbuf[growbuflen - 1] == '\n'
670#ifdef USE_CRNL
671 || growbuf[growbuflen - 1] == '\r'
672#endif
673 )
674 break;
675 if (growbufsiz == LINE_MAXLEN)
676 {
677 discard = TRUE;
678 break;
679 }
680
681 growbufsiz = 2 * growbufsiz < LINE_MAXLEN
682 ? 2 * growbufsiz : LINE_MAXLEN;
683 growbuf = vim_realloc(growbuf, 2 * growbufsiz);
684 if (growbuf == NULL)
685 goto qf_init_end;
686 }
687
688 while (discard)
689 {
690 /*
691 * The current line is longer than LINE_MAXLEN, continue
692 * reading but discard everything until EOL or EOF is
693 * reached.
694 */
695 if (fgets((char *)IObuff, IOSIZE, fd) == NULL
696 || (int)STRLEN(IObuff) < IOSIZE - 1
697 || IObuff[IOSIZE - 1] == '\n'
698#ifdef USE_CRNL
699 || IObuff[IOSIZE - 1] == '\r'
700#endif
701 )
702 break;
703 }
704
705 linebuf = growbuf;
706 linelen = growbuflen;
707 }
708 else
709 linebuf = IObuff;
710 }
711
712 if (linelen > 0 && linebuf[linelen - 1] == '\n')
713 linebuf[linelen - 1] = NUL;
714#ifdef USE_CRNL
715 if (linelen > 0 && linebuf[linelen - 1] == '\r')
716 linebuf[linelen - 1] = NUL;
Bram Moolenaar836082d2011-08-10 13:21:46 +0200717#endif
718
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200719#ifdef FEAT_MBYTE
720 remove_bom(linebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#endif
722
Bram Moolenaar01265852006-03-20 21:50:15 +0000723 /* If there was no %> item start at the first pattern */
724 if (fmt_start == NULL)
725 fmt_ptr = fmt_first;
726 else
727 {
728 fmt_ptr = fmt_start;
729 fmt_start = NULL;
730 }
731
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 /*
733 * Try to match each part of 'errorformat' until we find a complete
734 * match or no match.
735 */
736 valid = TRUE;
737restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000738 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100740 int r;
741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 idx = fmt_ptr->prefix;
743 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
744 continue;
745 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000746 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 if (!multiscan)
748 errmsg[0] = NUL;
749 lnum = 0;
750 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000751 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 enr = -1;
753 type = 0;
754 tail = NULL;
755
756 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200757 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100758 fmt_ptr->prog = regmatch.regprog;
759 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
761 if ((idx == 'C' || idx == 'Z') && !multiline)
762 continue;
763 if (vim_strchr((char_u *)"EWI", idx) != NULL)
764 type = idx;
765 else
766 type = 0;
767 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000768 * Extract error message data from matched line.
769 * We check for an actual submatch, because "\[" and "\]" in
770 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
772 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
773 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000774 int c;
775
776 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
777 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000778
779 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000780 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000781 *regmatch.endp[i] = NUL;
782 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
783 *regmatch.endp[i] = c;
784
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000786 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 continue;
788 }
789 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000790 {
791 if (regmatch.startp[i] == NULL)
792 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000796 {
797 if (regmatch.startp[i] == NULL)
798 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 lnum = 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[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000802 {
803 if (regmatch.startp[i] == NULL)
804 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 col = (int)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[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000808 {
809 if (regmatch.startp[i] == NULL)
810 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000812 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000813 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200814 {
815 if (linelen > errmsglen) {
816 /* linelen + null terminator */
817 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
818 goto qf_init_end;
819 errmsglen = linelen + 1;
820 }
821 vim_strncpy(errmsg, linebuf, linelen);
822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
824 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000825 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
826 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200828 if (len > errmsglen) {
829 /* len + null terminator */
830 if ((errmsg = vim_realloc(errmsg, len + 1))
831 == NULL)
832 goto qf_init_end;
833 errmsglen = len + 1;
834 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000835 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
837 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000838 {
839 if (regmatch.startp[i] == NULL)
840 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
844 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200845 char_u *match_ptr;
846
Bram Moolenaar4169da72006-06-20 18:49:32 +0000847 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
848 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200849 col = 0;
850 for (match_ptr = regmatch.startp[i];
851 match_ptr != regmatch.endp[i]; ++match_ptr)
852 {
853 ++col;
854 if (*match_ptr == TAB)
855 {
856 col += 7;
857 col -= col % 8;
858 }
859 }
860 ++col;
861 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
864 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000865 if (regmatch.startp[i] == NULL)
866 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000870 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
871 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000872 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
873 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000874 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
875 if (len > CMDBUFFSIZE - 5)
876 len = CMDBUFFSIZE - 5;
877 STRCPY(pattern, "^\\V");
878 STRNCAT(pattern, regmatch.startp[i], len);
879 pattern[len + 3] = '\\';
880 pattern[len + 4] = '$';
881 pattern[len + 5] = NUL;
882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 break;
884 }
885 }
886 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000887
Bram Moolenaar4770d092006-01-12 23:22:24 +0000888 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000890 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
892 if (idx == 'D') /* enter directory */
893 {
894 if (*namebuf == NUL)
895 {
896 EMSG(_("E379: Missing or empty directory name"));
897 goto error2;
898 }
899 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
900 goto error2;
901 }
902 else if (idx == 'X') /* leave directory */
903 directory = qf_pop_dir(&dir_stack);
904 }
905 namebuf[0] = NUL; /* no match found, remove file name */
906 lnum = 0; /* don't jump to this line */
907 valid = FALSE;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200908 if (linelen > errmsglen) {
909 /* linelen + null terminator */
910 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
911 goto qf_init_end;
912 errmsglen = linelen + 1;
913 }
914 /* copy whole line to error message */
915 vim_strncpy(errmsg, linebuf, linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000916 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 multiline = multiignore = FALSE;
918 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000919 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000921 /* honor %> item */
922 if (fmt_ptr->conthere)
923 fmt_start = fmt_ptr;
924
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100928 multiignore = FALSE; /* reset continuation */
929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
931 { /* continuation of multi-line msg */
932 if (qfprev == NULL)
933 goto error2;
934 if (*errmsg && !multiignore)
935 {
936 len = (int)STRLEN(qfprev->qf_text);
937 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
938 == NULL)
939 goto error2;
940 STRCPY(ptr, qfprev->qf_text);
941 vim_free(qfprev->qf_text);
942 qfprev->qf_text = ptr;
943 *(ptr += len) = '\n';
944 STRCPY(++ptr, errmsg);
945 }
946 if (qfprev->qf_nr == -1)
947 qfprev->qf_nr = enr;
948 if (vim_isprintc(type) && !qfprev->qf_type)
949 qfprev->qf_type = type; /* only printable chars allowed */
950 if (!qfprev->qf_lnum)
951 qfprev->qf_lnum = lnum;
952 if (!qfprev->qf_col)
953 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000954 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (!qfprev->qf_fnum)
956 qfprev->qf_fnum = qf_get_fnum(directory,
957 *namebuf || directory ? namebuf
958 : currfile && valid ? currfile : 0);
959 if (idx == 'Z')
960 multiline = multiignore = FALSE;
961 line_breakcheck();
962 continue;
963 }
964 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
965 {
966 /* global file names */
967 valid = FALSE;
968 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
969 {
970 if (*namebuf && idx == 'P')
971 currfile = qf_push_dir(namebuf, &file_stack);
972 else if (idx == 'Q')
973 currfile = qf_pop_dir(&file_stack);
974 *namebuf = NUL;
975 if (tail && *tail)
976 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000977 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 multiscan = TRUE;
979 goto restofline;
980 }
981 }
982 }
983 if (fmt_ptr->flags == '-') /* generally exclude this line */
984 {
985 if (multiline)
986 multiignore = TRUE; /* also exclude continuation lines */
987 continue;
988 }
989 }
990
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000991 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000993 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000995 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000996 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 errmsg,
998 lnum,
999 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001000 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001001 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 enr,
1003 type,
1004 valid) == FAIL)
1005 goto error2;
1006 line_breakcheck();
1007 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00001008 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001010 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001012 /* no valid entry found */
1013 qi->qf_lists[qi->qf_curlist].qf_ptr =
1014 qi->qf_lists[qi->qf_curlist].qf_start;
1015 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1016 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 }
1018 else
1019 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001020 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1021 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1022 qi->qf_lists[qi->qf_curlist].qf_ptr =
1023 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001025 /* return number of matches */
1026 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 goto qf_init_ok;
1028 }
1029 EMSG(_(e_readerrf));
1030error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001031 qf_free(qi, qi->qf_curlist);
1032 qi->qf_listcount--;
1033 if (qi->qf_curlist > 0)
1034 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +00001036 if (fd != NULL)
1037 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
1039 {
1040 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02001041 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 vim_free(fmt_ptr);
1043 }
1044 qf_clean_dir_stack(&dir_stack);
1045 qf_clean_dir_stack(&file_stack);
1046qf_init_end:
1047 vim_free(namebuf);
1048 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001049 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 vim_free(fmtstr);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001051 vim_free(growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
1053#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001054 qf_update_buffer(qi, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055#endif
1056
1057 return retval;
1058}
1059
Bram Moolenaarfb604092014-07-23 15:55:00 +02001060 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001061qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001062{
1063 if (title != NULL)
1064 {
1065 char_u *p = alloc((int)STRLEN(title) + 2);
1066
1067 qi->qf_lists[qi->qf_curlist].qf_title = p;
1068 if (p != NULL)
1069 sprintf((char *)p, ":%s", (char *)title);
1070 }
1071}
1072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073/*
1074 * Prepare for adding a new quickfix list.
1075 */
1076 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001077qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
1079 int i;
1080
1081 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001082 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 * the current entry. This makes it possible to browse in a tree-like
1084 * way with ":grep'.
1085 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001086 while (qi->qf_listcount > qi->qf_curlist + 1)
1087 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088
1089 /*
1090 * When the stack is full, remove to oldest entry
1091 * Otherwise, add a new entry.
1092 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001093 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001095 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001097 qi->qf_lists[i - 1] = qi->qf_lists[i];
1098 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
1100 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001101 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001102 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001103 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104}
1105
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001106/*
1107 * Free a location list
1108 */
1109 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001110ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001111{
1112 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001113 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001114
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001115 qi = *pqi;
1116 if (qi == NULL)
1117 return;
1118 *pqi = NULL; /* Remove reference to this list */
1119
1120 qi->qf_refcount--;
1121 if (qi->qf_refcount < 1)
1122 {
1123 /* No references to this location list */
1124 for (i = 0; i < qi->qf_listcount; ++i)
1125 qf_free(qi, i);
1126 vim_free(qi);
1127 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001128}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001129
1130 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001131qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001132{
1133 int i;
1134 qf_info_T *qi = &ql_info;
1135
1136 if (wp != NULL)
1137 {
1138 /* location list */
1139 ll_free_all(&wp->w_llist);
1140 ll_free_all(&wp->w_llist_ref);
1141 }
1142 else
1143 /* quickfix list */
1144 for (i = 0; i < qi->qf_listcount; ++i)
1145 qf_free(qi, i);
1146}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148/*
1149 * Add an entry to the end of the list of errors.
1150 * Returns OK or FAIL.
1151 */
1152 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001153qf_add_entry(
1154 qf_info_T *qi, /* quickfix list */
1155 qfline_T **prevp, /* pointer to previously added entry or NULL */
1156 char_u *dir, /* optional directory name */
1157 char_u *fname, /* file name or NULL */
1158 int bufnum, /* buffer number or zero */
1159 char_u *mesg, /* message */
1160 long lnum, /* line number */
1161 int col, /* column */
1162 int vis_col, /* using visual column */
1163 char_u *pattern, /* search pattern */
1164 int nr, /* error number */
1165 int type, /* type character */
1166 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001168 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001170 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001172 if (bufnum != 0)
1173 qfp->qf_fnum = bufnum;
1174 else
1175 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1177 {
1178 vim_free(qfp);
1179 return FAIL;
1180 }
1181 qfp->qf_lnum = lnum;
1182 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001183 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001184 if (pattern == NULL || *pattern == NUL)
1185 qfp->qf_pattern = NULL;
1186 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1187 {
1188 vim_free(qfp->qf_text);
1189 vim_free(qfp);
1190 return FAIL;
1191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 qfp->qf_nr = nr;
1193 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1194 type = 0;
1195 qfp->qf_type = type;
1196 qfp->qf_valid = valid;
1197
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001198 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1199 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001201 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001202 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1203 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 qfp->qf_prev = qfp; /* first element points to itself */
1205 }
1206 else
1207 {
1208 qfp->qf_prev = *prevp;
1209 (*prevp)->qf_next = qfp;
1210 }
1211 qfp->qf_next = qfp; /* last element points to itself */
1212 qfp->qf_cleared = FALSE;
1213 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001214 ++qi->qf_lists[qi->qf_curlist].qf_count;
1215 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1216 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001218 qi->qf_lists[qi->qf_curlist].qf_index =
1219 qi->qf_lists[qi->qf_curlist].qf_count;
1220 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222
1223 return OK;
1224}
1225
1226/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001227 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001228 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001229 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001230ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001231{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001232 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001233
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001234 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1235 if (qi != NULL)
1236 {
1237 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1238 qi->qf_refcount++;
1239 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001240
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001241 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001242}
1243
1244/*
1245 * Return the location list for window 'wp'.
1246 * If not present, allocate a location list
1247 */
1248 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001249ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001250{
1251 if (IS_LL_WINDOW(wp))
1252 /* For a location list window, use the referenced location list */
1253 return wp->w_llist_ref;
1254
1255 /*
1256 * For a non-location list window, w_llist_ref should not point to a
1257 * location list.
1258 */
1259 ll_free_all(&wp->w_llist_ref);
1260
1261 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001262 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001263 return wp->w_llist;
1264}
1265
1266/*
1267 * Copy the location list from window "from" to window "to".
1268 */
1269 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001270copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001271{
1272 qf_info_T *qi;
1273 int idx;
1274 int i;
1275
1276 /*
1277 * When copying from a location list window, copy the referenced
1278 * location list. For other windows, copy the location list for
1279 * that window.
1280 */
1281 if (IS_LL_WINDOW(from))
1282 qi = from->w_llist_ref;
1283 else
1284 qi = from->w_llist;
1285
1286 if (qi == NULL) /* no location list to copy */
1287 return;
1288
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001289 /* allocate a new location list */
1290 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001291 return;
1292
1293 to->w_llist->qf_listcount = qi->qf_listcount;
1294
1295 /* Copy the location lists one at a time */
1296 for (idx = 0; idx < qi->qf_listcount; idx++)
1297 {
1298 qf_list_T *from_qfl;
1299 qf_list_T *to_qfl;
1300
1301 to->w_llist->qf_curlist = idx;
1302
1303 from_qfl = &qi->qf_lists[idx];
1304 to_qfl = &to->w_llist->qf_lists[idx];
1305
1306 /* Some of the fields are populated by qf_add_entry() */
1307 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1308 to_qfl->qf_count = 0;
1309 to_qfl->qf_index = 0;
1310 to_qfl->qf_start = NULL;
1311 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001312 if (from_qfl->qf_title != NULL)
1313 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1314 else
1315 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001316
1317 if (from_qfl->qf_count)
1318 {
1319 qfline_T *from_qfp;
1320 qfline_T *prevp = NULL;
1321
1322 /* copy all the location entries in this list */
1323 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1324 ++i, from_qfp = from_qfp->qf_next)
1325 {
1326 if (qf_add_entry(to->w_llist, &prevp,
1327 NULL,
1328 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001329 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001330 from_qfp->qf_text,
1331 from_qfp->qf_lnum,
1332 from_qfp->qf_col,
1333 from_qfp->qf_viscol,
1334 from_qfp->qf_pattern,
1335 from_qfp->qf_nr,
1336 0,
1337 from_qfp->qf_valid) == FAIL)
1338 {
1339 qf_free_all(to);
1340 return;
1341 }
1342 /*
1343 * qf_add_entry() will not set the qf_num field, as the
1344 * directory and file names are not supplied. So the qf_fnum
1345 * field is copied here.
1346 */
1347 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1348 prevp->qf_type = from_qfp->qf_type; /* error type */
1349 if (from_qfl->qf_ptr == from_qfp)
1350 to_qfl->qf_ptr = prevp; /* current location */
1351 }
1352 }
1353
1354 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1355
1356 /* When no valid entries are present in the list, qf_ptr points to
1357 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001358 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001359 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001360 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001361 to_qfl->qf_index = 1;
1362 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001363 }
1364
1365 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1366}
1367
1368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * get buffer number for file "dir.name"
1370 */
1371 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001372qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
1374 if (fname == NULL || *fname == NUL) /* no file name */
1375 return 0;
1376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 char_u *ptr;
1378 int fnum;
1379
Bram Moolenaare60acc12011-05-10 16:41:25 +02001380#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001382#endif
1383#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 if (directory != NULL)
1385 slash_adjust(directory);
1386 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (directory != NULL && !vim_isAbsName(fname)
1389 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1390 {
1391 /*
1392 * Here we check if the file really exists.
1393 * This should normally be true, but if make works without
1394 * "leaving directory"-messages we might have missed a
1395 * directory change.
1396 */
1397 if (mch_getperm(ptr) < 0)
1398 {
1399 vim_free(ptr);
1400 directory = qf_guess_filepath(fname);
1401 if (directory)
1402 ptr = concat_fnames(directory, fname, TRUE);
1403 else
1404 ptr = vim_strsave(fname);
1405 }
1406 /* Use concatenated directory name and file name */
1407 fnum = buflist_add(ptr, 0);
1408 vim_free(ptr);
1409 return fnum;
1410 }
1411 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 }
1413}
1414
1415/*
1416 * push dirbuf onto the directory stack and return pointer to actual dir or
1417 * NULL on error
1418 */
1419 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001420qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 struct dir_stack_T *ds_new;
1423 struct dir_stack_T *ds_ptr;
1424
1425 /* allocate new stack element and hook it in */
1426 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1427 if (ds_new == NULL)
1428 return NULL;
1429
1430 ds_new->next = *stackptr;
1431 *stackptr = ds_new;
1432
1433 /* store directory on the stack */
1434 if (vim_isAbsName(dirbuf)
1435 || (*stackptr)->next == NULL
1436 || (*stackptr && dir_stack != *stackptr))
1437 (*stackptr)->dirname = vim_strsave(dirbuf);
1438 else
1439 {
1440 /* Okay we don't have an absolute path.
1441 * dirbuf must be a subdir of one of the directories on the stack.
1442 * Let's search...
1443 */
1444 ds_new = (*stackptr)->next;
1445 (*stackptr)->dirname = NULL;
1446 while (ds_new)
1447 {
1448 vim_free((*stackptr)->dirname);
1449 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1450 TRUE);
1451 if (mch_isdir((*stackptr)->dirname) == TRUE)
1452 break;
1453
1454 ds_new = ds_new->next;
1455 }
1456
1457 /* clean up all dirs we already left */
1458 while ((*stackptr)->next != ds_new)
1459 {
1460 ds_ptr = (*stackptr)->next;
1461 (*stackptr)->next = (*stackptr)->next->next;
1462 vim_free(ds_ptr->dirname);
1463 vim_free(ds_ptr);
1464 }
1465
1466 /* Nothing found -> it must be on top level */
1467 if (ds_new == NULL)
1468 {
1469 vim_free((*stackptr)->dirname);
1470 (*stackptr)->dirname = vim_strsave(dirbuf);
1471 }
1472 }
1473
1474 if ((*stackptr)->dirname != NULL)
1475 return (*stackptr)->dirname;
1476 else
1477 {
1478 ds_ptr = *stackptr;
1479 *stackptr = (*stackptr)->next;
1480 vim_free(ds_ptr);
1481 return NULL;
1482 }
1483}
1484
1485
1486/*
1487 * pop dirbuf from the directory stack and return previous directory or NULL if
1488 * stack is empty
1489 */
1490 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001491qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
1493 struct dir_stack_T *ds_ptr;
1494
1495 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1496 * What to do if it isn't? */
1497
1498 /* pop top element and free it */
1499 if (*stackptr != NULL)
1500 {
1501 ds_ptr = *stackptr;
1502 *stackptr = (*stackptr)->next;
1503 vim_free(ds_ptr->dirname);
1504 vim_free(ds_ptr);
1505 }
1506
1507 /* return NEW top element as current dir or NULL if stack is empty*/
1508 return *stackptr ? (*stackptr)->dirname : NULL;
1509}
1510
1511/*
1512 * clean up directory stack
1513 */
1514 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001515qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 struct dir_stack_T *ds_ptr;
1518
1519 while ((ds_ptr = *stackptr) != NULL)
1520 {
1521 *stackptr = (*stackptr)->next;
1522 vim_free(ds_ptr->dirname);
1523 vim_free(ds_ptr);
1524 }
1525}
1526
1527/*
1528 * Check in which directory of the directory stack the given file can be
1529 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001530 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 * Cleans up intermediate directory entries.
1532 *
1533 * TODO: How to solve the following problem?
1534 * If we have the this directory tree:
1535 * ./
1536 * ./aa
1537 * ./aa/bb
1538 * ./bb
1539 * ./bb/x.c
1540 * and make says:
1541 * making all in aa
1542 * making all in bb
1543 * x.c:9: Error
1544 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1545 * qf_guess_filepath will return NULL.
1546 */
1547 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001548qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 struct dir_stack_T *ds_ptr;
1551 struct dir_stack_T *ds_tmp;
1552 char_u *fullname;
1553
1554 /* no dirs on the stack - there's nothing we can do */
1555 if (dir_stack == NULL)
1556 return NULL;
1557
1558 ds_ptr = dir_stack->next;
1559 fullname = NULL;
1560 while (ds_ptr)
1561 {
1562 vim_free(fullname);
1563 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1564
1565 /* If concat_fnames failed, just go on. The worst thing that can happen
1566 * is that we delete the entire stack.
1567 */
1568 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1569 break;
1570
1571 ds_ptr = ds_ptr->next;
1572 }
1573
1574 vim_free(fullname);
1575
1576 /* clean up all dirs we already left */
1577 while (dir_stack->next != ds_ptr)
1578 {
1579 ds_tmp = dir_stack->next;
1580 dir_stack->next = dir_stack->next->next;
1581 vim_free(ds_tmp->dirname);
1582 vim_free(ds_tmp);
1583 }
1584
1585 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1586
1587}
1588
1589/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001590 * When loading a file from the quickfix, the auto commands may modify it.
1591 * This may invalidate the current quickfix entry. This function checks
1592 * whether a entry is still present in the quickfix.
1593 * Similar to location list.
1594 */
1595 static int
1596is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1597{
1598 qf_list_T *qfl;
1599 qfline_T *qfp;
1600 int i;
1601
1602 qfl = &qi->qf_lists[qi->qf_curlist];
1603
1604 /* Search for the entry in the current list */
1605 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1606 ++i, qfp = qfp->qf_next)
1607 if (qfp == qf_ptr)
1608 break;
1609
1610 if (i == qfl->qf_count) /* Entry is not found */
1611 return FALSE;
1612
1613 return TRUE;
1614}
1615
1616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 * jump to a quickfix line
1618 * if dir == FORWARD go "errornr" valid entries forward
1619 * if dir == BACKWARD go "errornr" valid entries backward
1620 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1621 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1622 * else if "errornr" is zero, redisplay the same line
1623 * else go to entry "errornr"
1624 */
1625 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001626qf_jump(
1627 qf_info_T *qi,
1628 int dir,
1629 int errornr,
1630 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001632 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001633 qfline_T *qf_ptr;
1634 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 int qf_index;
1636 int old_qf_fnum;
1637 int old_qf_index;
1638 int prev_index;
1639 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1640 char_u *err = e_no_more_items;
1641 linenr_T i;
1642 buf_T *old_curbuf;
1643 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 colnr_T screen_col;
1645 colnr_T char_col;
1646 char_u *line;
1647#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001648 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001649 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 int opened_window = FALSE;
1651 win_T *win;
1652 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001653 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001655 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 int print_message = TRUE;
1657 int len;
1658#ifdef FEAT_FOLDING
1659 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1660#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001661 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001662 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001664 if (qi == NULL)
1665 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001666
1667 if (qi->qf_curlist >= qi->qf_listcount
1668 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
1670 EMSG(_(e_quickfix));
1671 return;
1672 }
1673
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001674 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001676 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 old_qf_index = qf_index;
1678 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1679 {
1680 while (errornr--)
1681 {
1682 old_qf_ptr = qf_ptr;
1683 prev_index = qf_index;
1684 old_qf_fnum = qf_ptr->qf_fnum;
1685 do
1686 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001687 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 || qf_ptr->qf_next == NULL)
1689 {
1690 qf_ptr = old_qf_ptr;
1691 qf_index = prev_index;
1692 if (err != NULL)
1693 {
1694 EMSG(_(err));
1695 goto theend;
1696 }
1697 errornr = 0;
1698 break;
1699 }
1700 ++qf_index;
1701 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001702 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1703 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1705 err = NULL;
1706 }
1707 }
1708 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1709 {
1710 while (errornr--)
1711 {
1712 old_qf_ptr = qf_ptr;
1713 prev_index = qf_index;
1714 old_qf_fnum = qf_ptr->qf_fnum;
1715 do
1716 {
1717 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1718 {
1719 qf_ptr = old_qf_ptr;
1720 qf_index = prev_index;
1721 if (err != NULL)
1722 {
1723 EMSG(_(err));
1724 goto theend;
1725 }
1726 errornr = 0;
1727 break;
1728 }
1729 --qf_index;
1730 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001731 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1732 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1734 err = NULL;
1735 }
1736 }
1737 else if (errornr != 0) /* go to specified number */
1738 {
1739 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1740 {
1741 --qf_index;
1742 qf_ptr = qf_ptr->qf_prev;
1743 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001744 while (errornr > qf_index && qf_index <
1745 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 && qf_ptr->qf_next != NULL)
1747 {
1748 ++qf_index;
1749 qf_ptr = qf_ptr->qf_next;
1750 }
1751 }
1752
1753#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001754 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1755 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 /* No need to print the error message if it's visible in the error
1757 * window */
1758 print_message = FALSE;
1759
1760 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001761 * For ":helpgrep" find a help window or open one.
1762 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001763 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001764 {
1765 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001766
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001767 if (cmdmod.tab != 0)
1768 wp = NULL;
1769 else
1770 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1771 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1772 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001773 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1774 win_enter(wp, TRUE);
1775 else
1776 {
1777 /*
1778 * Split off help window; put it at far top if no position
1779 * specified, the current window is vertically split and narrow.
1780 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001781 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001782 if (cmdmod.split == 0 && curwin->w_width != Columns
1783 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001784 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001785 if (qi != &ql_info)
1786 flags |= WSP_NEWLOC; /* don't copy the location list */
1787
1788 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001789 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001790 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001791
1792 if (curwin->w_height < p_hh)
1793 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001794
1795 if (qi != &ql_info) /* not a quickfix list */
1796 {
1797 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001798 curwin->w_llist = qi;
1799 qi->qf_refcount++;
1800 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001801 }
1802
1803 if (!p_im)
1804 restart_edit = 0; /* don't want insert mode in help file */
1805 }
1806
1807 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 * If currently in the quickfix window, find another window to show the
1809 * file in.
1810 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001811 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001813 win_T *usable_win_ptr = NULL;
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 /*
1816 * If there is no file specified, we don't know where to go.
1817 * But do advance, otherwise ":cn" gets stuck.
1818 */
1819 if (qf_ptr->qf_fnum == 0)
1820 goto theend;
1821
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001822 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001823
1824 ll_ref = curwin->w_llist_ref;
1825 if (ll_ref != NULL)
1826 {
1827 /* Find a window using the same location list that is not a
1828 * quickfix window. */
1829 FOR_ALL_WINDOWS(usable_win_ptr)
1830 if (usable_win_ptr->w_llist == ll_ref
1831 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001832 {
1833 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001834 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001835 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001836 }
1837
1838 if (!usable_win)
1839 {
1840 /* Locate a window showing a normal buffer */
1841 FOR_ALL_WINDOWS(win)
1842 if (win->w_buffer->b_p_bt[0] == NUL)
1843 {
1844 usable_win = 1;
1845 break;
1846 }
1847 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001850 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001851 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001853 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001854 {
1855 tabpage_T *tp;
1856 win_T *wp;
1857
1858 FOR_ALL_TAB_WINDOWS(tp, wp)
1859 {
1860 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1861 {
1862 goto_tabpage_win(tp, wp);
1863 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001864 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001865 }
1866 }
1867 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001868win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001869
1870 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001871 * If there is only one window and it is the quickfix window, create a
1872 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001873 */
1874 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001876 flags = WSP_ABOVE;
1877 if (ll_ref != NULL)
1878 flags |= WSP_NEWLOC;
1879 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 goto failed; /* not enough room for window */
1881 opened_window = TRUE; /* close it when fail */
1882 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001883 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001884 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001885 if (ll_ref != NULL)
1886 {
1887 /* The new window should use the location list from the
1888 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001889 curwin->w_llist = ll_ref;
1890 ll_ref->qf_refcount++;
1891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
1893 else
1894 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001895 if (curwin->w_llist_ref != NULL)
1896 {
1897 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001898 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001899 if (win == NULL)
1900 {
1901 /* Find the window showing the selected file */
1902 FOR_ALL_WINDOWS(win)
1903 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1904 break;
1905 if (win == NULL)
1906 {
1907 /* Find a previous usable window */
1908 win = curwin;
1909 do
1910 {
1911 if (win->w_buffer->b_p_bt[0] == NUL)
1912 break;
1913 if (win->w_prev == NULL)
1914 win = lastwin; /* wrap around the top */
1915 else
1916 win = win->w_prev; /* go to previous window */
1917 } while (win != curwin);
1918 }
1919 }
1920 win_goto(win);
1921
1922 /* If the location list for the window is not set, then set it
1923 * to the location list from the location window */
1924 if (win->w_llist == NULL)
1925 {
1926 win->w_llist = ll_ref;
1927 ll_ref->qf_refcount++;
1928 }
1929 }
1930 else
1931 {
1932
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 /*
1934 * Try to find a window that shows the right buffer.
1935 * Default to the window just above the quickfix buffer.
1936 */
1937 win = curwin;
1938 altwin = NULL;
1939 for (;;)
1940 {
1941 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1942 break;
1943 if (win->w_prev == NULL)
1944 win = lastwin; /* wrap around the top */
1945 else
1946 win = win->w_prev; /* go to previous window */
1947
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001948 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
1950 /* Didn't find it, go to the window before the quickfix
1951 * window. */
1952 if (altwin != NULL)
1953 win = altwin;
1954 else if (curwin->w_prev != NULL)
1955 win = curwin->w_prev;
1956 else
1957 win = curwin->w_next;
1958 break;
1959 }
1960
1961 /* Remember a usable window. */
1962 if (altwin == NULL && !win->w_p_pvw
1963 && win->w_buffer->b_p_bt[0] == NUL)
1964 altwin = win;
1965 }
1966
1967 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970 }
1971#endif
1972
1973 /*
1974 * If there is a file name,
1975 * read the wanted file if needed, and check autowrite etc.
1976 */
1977 old_curbuf = curbuf;
1978 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001979
1980 if (qf_ptr->qf_fnum != 0)
1981 {
1982 if (qf_ptr->qf_type == 1)
1983 {
1984 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1985 * set b_p_ro flag). */
1986 if (!can_abandon(curbuf, forceit))
1987 {
1988 EMSG(_(e_nowrtmsg));
1989 ok = FALSE;
1990 }
1991 else
1992 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001993 ECMD_HIDE + ECMD_SET_HELP,
1994 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001995 }
1996 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01001997 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001998 int old_qf_curlist = qi->qf_curlist;
1999 int is_abort = FALSE;
2000
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002001 ok = buflist_getfile(qf_ptr->qf_fnum,
2002 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002003 if (qi != &ql_info && !win_valid(oldwin))
2004 {
2005 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002006 is_abort = TRUE;
2007 opened_window = FALSE;
2008 }
2009 else if (old_qf_curlist != qi->qf_curlist
2010 || !is_qf_entry_present(qi, qf_ptr))
2011 {
2012 if (qi == &ql_info)
2013 EMSG(_("E925: Current quickfix was changed"));
2014 else
2015 EMSG(_("E926: Current location list was changed"));
2016 is_abort = TRUE;
2017 }
2018
2019 if (is_abort)
2020 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002021 ok = FALSE;
2022 qi = NULL;
2023 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002024 }
2025 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002026 }
2027
2028 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
2030 /* When not switched to another buffer, still need to set pc mark */
2031 if (curbuf == old_curbuf)
2032 setpcmark();
2033
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002034 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002036 /*
2037 * Go to line with error, unless qf_lnum is 0.
2038 */
2039 i = qf_ptr->qf_lnum;
2040 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002042 if (i > curbuf->b_ml.ml_line_count)
2043 i = curbuf->b_ml.ml_line_count;
2044 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002046 if (qf_ptr->qf_col > 0)
2047 {
2048 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002049#ifdef FEAT_VIRTUALEDIT
2050 curwin->w_cursor.coladd = 0;
2051#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002052 if (qf_ptr->qf_viscol == TRUE)
2053 {
2054 /*
2055 * Check each character from the beginning of the error
2056 * line up to the error column. For each tab character
2057 * found, reduce the error column value by the length of
2058 * a tab character.
2059 */
2060 line = ml_get_curline();
2061 screen_col = 0;
2062 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2063 {
2064 if (*line == NUL)
2065 break;
2066 if (*line++ == '\t')
2067 {
2068 curwin->w_cursor.col -= 7 - (screen_col % 8);
2069 screen_col += 8 - (screen_col % 8);
2070 }
2071 else
2072 ++screen_col;
2073 }
2074 }
2075 check_cursor();
2076 }
2077 else
2078 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002081 {
2082 pos_T save_cursor;
2083
2084 /* Move the cursor to the first line in the buffer */
2085 save_cursor = curwin->w_cursor;
2086 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002087 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2088 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002089 curwin->w_cursor = save_cursor;
2090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
2092#ifdef FEAT_FOLDING
2093 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2094 foldOpenCursor();
2095#endif
2096 if (print_message)
2097 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002098 /* Update the screen before showing the message, unless the screen
2099 * scrolled up. */
2100 if (!msg_scrolled)
2101 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002103 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2105 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2106 /* Add the message, skipping leading whitespace and newlines. */
2107 len = (int)STRLEN(IObuff);
2108 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2109
2110 /* Output the message. Overwrite to avoid scrolling when the 'O'
2111 * flag is present in 'shortmess'; But when not jumping, print the
2112 * whole message. */
2113 i = msg_scroll;
2114 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2115 msg_scroll = TRUE;
2116 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2117 msg_scroll = FALSE;
2118 msg_attr_keep(IObuff, 0, TRUE);
2119 msg_scroll = i;
2120 }
2121 }
2122 else
2123 {
2124#ifdef FEAT_WINDOWS
2125 if (opened_window)
2126 win_close(curwin, TRUE); /* Close opened window */
2127#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002128 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
2130 /*
2131 * Couldn't open file, so put index back where it was. This could
2132 * happen if the file was readonly and we changed something.
2133 */
2134#ifdef FEAT_WINDOWS
2135failed:
2136#endif
2137 qf_ptr = old_qf_ptr;
2138 qf_index = old_qf_index;
2139 }
2140 }
2141theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002142 if (qi != NULL)
2143 {
2144 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2145 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147#ifdef FEAT_WINDOWS
2148 if (p_swb != old_swb && opened_window)
2149 {
2150 /* Restore old 'switchbuf' value, but not when an autocommand or
2151 * modeline has changed the value. */
2152 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002155 swb_flags = old_swb_flags;
2156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 else
2158 free_string_option(old_swb);
2159 }
2160#endif
2161}
2162
2163/*
2164 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002165 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 */
2167 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002168qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002170 buf_T *buf;
2171 char_u *fname;
2172 qfline_T *qfp;
2173 int i;
2174 int idx1 = 1;
2175 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002176 char_u *arg = eap->arg;
2177 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002179 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002181 if (eap->cmdidx == CMD_llist)
2182 {
2183 qi = GET_LOC_LIST(curwin);
2184 if (qi == NULL)
2185 {
2186 EMSG(_(e_loclist));
2187 return;
2188 }
2189 }
2190
2191 if (qi->qf_curlist >= qi->qf_listcount
2192 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 EMSG(_(e_quickfix));
2195 return;
2196 }
2197 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2198 {
2199 EMSG(_(e_trailing));
2200 return;
2201 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002202 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (idx1 < 0)
2204 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2205 if (idx2 < 0)
2206 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2207
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002208 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002210 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2211 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2214 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002215 msg_putchar('\n');
2216 if (got_int)
2217 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002218
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002219 fname = NULL;
2220 if (qfp->qf_fnum != 0
2221 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2222 {
2223 fname = buf->b_fname;
2224 if (qfp->qf_type == 1) /* :helpgrep */
2225 fname = gettail(fname);
2226 }
2227 if (fname == NULL)
2228 sprintf((char *)IObuff, "%2d", i);
2229 else
2230 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2231 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002232 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002233 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2234 if (qfp->qf_lnum == 0)
2235 IObuff[0] = NUL;
2236 else if (qfp->qf_col == 0)
2237 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2238 else
2239 sprintf((char *)IObuff, ":%ld col %d",
2240 qfp->qf_lnum, qfp->qf_col);
2241 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2242 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2243 msg_puts_attr(IObuff, hl_attr(HLF_N));
2244 if (qfp->qf_pattern != NULL)
2245 {
2246 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2247 STRCAT(IObuff, ":");
2248 msg_puts(IObuff);
2249 }
2250 msg_puts((char_u *)" ");
2251
2252 /* Remove newlines and leading whitespace from the text. For an
2253 * unrecognized line keep the indent, the compiler may mark a word
2254 * with ^^^^. */
2255 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2257 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002258 msg_prt_line(IObuff, FALSE);
2259 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002261
2262 qfp = qfp->qf_next;
2263 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 ui_breakcheck();
2265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266}
2267
2268/*
2269 * Remove newlines and leading whitespace from an error message.
2270 * Put the result in "buf[bufsize]".
2271 */
2272 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002273qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 int i;
2276 char_u *p = text;
2277
2278 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2279 {
2280 if (*p == '\n')
2281 {
2282 buf[i] = ' ';
2283 while (*++p != NUL)
2284 if (!vim_iswhite(*p) && *p != '\n')
2285 break;
2286 }
2287 else
2288 buf[i] = *p++;
2289 }
2290 buf[i] = NUL;
2291}
2292
2293/*
2294 * ":colder [count]": Up in the quickfix stack.
2295 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002296 * ":lolder [count]": Up in the location list stack.
2297 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 */
2299 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002300qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002302 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 int count;
2304
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002305 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2306 {
2307 qi = GET_LOC_LIST(curwin);
2308 if (qi == NULL)
2309 {
2310 EMSG(_(e_loclist));
2311 return;
2312 }
2313 }
2314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (eap->addr_count != 0)
2316 count = eap->line2;
2317 else
2318 count = 1;
2319 while (count--)
2320 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002321 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002323 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002326 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002328 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
2330 else
2331 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002332 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
2334 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002335 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002337 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002340 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341}
2342
2343 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002344qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002347 qi->qf_curlist + 1, qi->qf_listcount,
2348 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002350 qf_update_buffer(qi, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#endif
2352}
2353
2354/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002355 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 */
2357 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002358qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002360 qfline_T *qfp;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002361 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002363 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002365 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002366 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002367 {
2368 vim_free(qi->qf_lists[idx].qf_start->qf_text);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002369 stop = (qi->qf_lists[idx].qf_start == qfp);
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002370 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2371 vim_free(qi->qf_lists[idx].qf_start);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002372 if (stop)
2373 /* Somehow qf_count may have an incorrect value, set it to 1
2374 * to avoid crashing when it's wrong.
2375 * TODO: Avoid qf_count being incorrect. */
2376 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002377 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002378 qi->qf_lists[idx].qf_start = qfp;
2379 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002381 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002382 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002383 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384}
2385
2386/*
2387 * qf_mark_adjust: adjust marks
2388 */
2389 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002390qf_mark_adjust(
2391 win_T *wp,
2392 linenr_T line1,
2393 linenr_T line2,
2394 long amount,
2395 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002397 int i;
2398 qfline_T *qfp;
2399 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002400 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402 if (wp != NULL)
2403 {
2404 if (wp->w_llist == NULL)
2405 return;
2406 qi = wp->w_llist;
2407 }
2408
2409 for (idx = 0; idx < qi->qf_listcount; ++idx)
2410 if (qi->qf_lists[idx].qf_count)
2411 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2412 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (qfp->qf_fnum == curbuf->b_fnum)
2414 {
2415 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2416 {
2417 if (amount == MAXLNUM)
2418 qfp->qf_cleared = TRUE;
2419 else
2420 qfp->qf_lnum += amount;
2421 }
2422 else if (amount_after && qfp->qf_lnum > line2)
2423 qfp->qf_lnum += amount_after;
2424 }
2425}
2426
2427/*
2428 * Make a nice message out of the error character and the error number:
2429 * char number message
2430 * e or E 0 " error"
2431 * w or W 0 " warning"
2432 * i or I 0 " info"
2433 * 0 0 ""
2434 * other 0 " c"
2435 * e or E n " error n"
2436 * w or W n " warning n"
2437 * i or I n " info n"
2438 * 0 n " error n"
2439 * other n " c n"
2440 * 1 x "" :helpgrep
2441 */
2442 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002443qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445 static char_u buf[20];
2446 static char_u cc[3];
2447 char_u *p;
2448
2449 if (c == 'W' || c == 'w')
2450 p = (char_u *)" warning";
2451 else if (c == 'I' || c == 'i')
2452 p = (char_u *)" info";
2453 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2454 p = (char_u *)" error";
2455 else if (c == 0 || c == 1)
2456 p = (char_u *)"";
2457 else
2458 {
2459 cc[0] = ' ';
2460 cc[1] = c;
2461 cc[2] = NUL;
2462 p = cc;
2463 }
2464
2465 if (nr <= 0)
2466 return p;
2467
2468 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2469 return buf;
2470}
2471
2472#if defined(FEAT_WINDOWS) || defined(PROTO)
2473/*
2474 * ":cwindow": open the quickfix window if we have errors to display,
2475 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002476 * ":lwindow": open the location list window if we have locations to display,
2477 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 */
2479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002480ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002482 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 win_T *win;
2484
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002485 if (eap->cmdidx == CMD_lwindow)
2486 {
2487 qi = GET_LOC_LIST(curwin);
2488 if (qi == NULL)
2489 return;
2490 }
2491
2492 /* Look for an existing quickfix window. */
2493 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 /*
2496 * If a quickfix window is open but we have no errors to display,
2497 * close the window. If a quickfix window is not open, then open
2498 * it if we have errors; otherwise, leave it closed.
2499 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002500 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002501 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002502 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
2504 if (win != NULL)
2505 ex_cclose(eap);
2506 }
2507 else if (win == NULL)
2508 ex_copen(eap);
2509}
2510
2511/*
2512 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002513 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002516ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002518 win_T *win = NULL;
2519 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002521 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2522 {
2523 qi = GET_LOC_LIST(curwin);
2524 if (qi == NULL)
2525 return;
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002528 /* Find existing quickfix window and close it. */
2529 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 if (win != NULL)
2531 win_close(win, FALSE);
2532}
2533
2534/*
2535 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002536 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 */
2538 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002539ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002541 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002544 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002545 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002546 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002548 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2549 {
2550 qi = GET_LOC_LIST(curwin);
2551 if (qi == NULL)
2552 {
2553 EMSG(_(e_loclist));
2554 return;
2555 }
2556 }
2557
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (eap->addr_count != 0)
2559 height = eap->line2;
2560 else
2561 height = QF_WINHEIGHT;
2562
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564#ifdef FEAT_GUI
2565 need_mouse_correct = TRUE;
2566#endif
2567
2568 /*
2569 * Find existing quickfix window, or open a new one.
2570 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002571 win = qf_find_win(qi);
2572
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002573 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002576 if (eap->addr_count != 0)
2577 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002578 if (cmdmod.split & WSP_VERT)
2579 {
2580 if (height != W_WIDTH(win))
2581 win_setwidth(height);
2582 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002583 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002584 win_setheight(height);
2585 }
2586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 else
2588 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002589 qf_buf = qf_find_buf(qi);
2590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 /* The current window becomes the previous window afterwards. */
2592 win = curwin;
2593
Bram Moolenaar77642c02012-11-20 17:55:10 +01002594 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2595 && cmdmod.split == 0)
2596 /* Create the new window at the very bottom, except when
2597 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002598 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002599 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002601 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002603 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002605 /*
2606 * For the location list window, create a reference to the
2607 * location list from the window 'win'.
2608 */
2609 curwin->w_llist_ref = win->w_llist;
2610 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002612
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002613 if (oldwin != curwin)
2614 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002615 if (qf_buf != NULL)
2616 /* Use the existing quickfix buffer */
2617 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002618 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002619 else
2620 {
2621 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002622 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002623 /* switch off 'swapfile' */
2624 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2625 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002626 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002627 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002628 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002629#ifdef FEAT_DIFF
2630 curwin->w_p_diff = FALSE;
2631#endif
2632#ifdef FEAT_FOLDING
2633 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2634 OPT_LOCAL);
2635#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002638 /* Only set the height when still in the same tab page and there is no
2639 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002640 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 win_setheight(height);
2642 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2643 if (win_valid(win))
2644 prevwin = win;
2645 }
2646
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002647 qf_set_title_var(qi);
2648
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 /*
2650 * Fill the buffer with the quickfix list.
2651 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002652 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002654 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 curwin->w_cursor.col = 0;
2656 check_cursor();
2657 update_topline(); /* scroll to show the line */
2658}
2659
2660/*
2661 * Return the number of the current entry (line number in the quickfix
2662 * window).
2663 */
2664 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002665qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002667 qf_info_T *qi = &ql_info;
2668
2669 if (IS_LL_WINDOW(wp))
2670 /* In the location list window, use the referenced location list */
2671 qi = wp->w_llist_ref;
2672
2673 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674}
2675
2676/*
2677 * Update the cursor position in the quickfix window to the current error.
2678 * Return TRUE if there is a quickfix window.
2679 */
2680 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002681qf_win_pos_update(
2682 qf_info_T *qi,
2683 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
2685 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002686 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 /*
2689 * Put the cursor on the current error in the quickfix window, so that
2690 * it's viewable.
2691 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002692 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (win != NULL
2694 && qf_index <= win->w_buffer->b_ml.ml_line_count
2695 && old_qf_index != qf_index)
2696 {
2697 win_T *old_curwin = curwin;
2698
2699 curwin = win;
2700 curbuf = win->w_buffer;
2701 if (qf_index > old_qf_index)
2702 {
2703 curwin->w_redraw_top = old_qf_index;
2704 curwin->w_redraw_bot = qf_index;
2705 }
2706 else
2707 {
2708 curwin->w_redraw_top = qf_index;
2709 curwin->w_redraw_bot = old_qf_index;
2710 }
2711 curwin->w_cursor.lnum = qf_index;
2712 curwin->w_cursor.col = 0;
2713 update_topline(); /* scroll to show the line */
2714 redraw_later(VALID);
2715 curwin->w_redr_status = TRUE; /* update ruler */
2716 curwin = old_curwin;
2717 curbuf = curwin->w_buffer;
2718 }
2719 return win != NULL;
2720}
2721
2722/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002723 * Check whether the given window is displaying the specified quickfix/location
2724 * list buffer
2725 */
2726 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002727is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002728{
2729 /*
2730 * A window displaying the quickfix buffer will have the w_llist_ref field
2731 * set to NULL.
2732 * A window displaying a location list buffer will have the w_llist_ref
2733 * pointing to the location list.
2734 */
2735 if (bt_quickfix(win->w_buffer))
2736 if ((qi == &ql_info && win->w_llist_ref == NULL)
2737 || (qi != &ql_info && win->w_llist_ref == qi))
2738 return TRUE;
2739
2740 return FALSE;
2741}
2742
2743/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002744 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002745 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002746 */
2747 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002748qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002749{
2750 win_T *win;
2751
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002752 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002753 if (is_qf_win(win, qi))
2754 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002755
2756 return win;
2757}
2758
2759/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002760 * Find a quickfix buffer.
2761 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 */
2763 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002764qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002766 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002767 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaar9c102382006-05-03 21:26:49 +00002769 FOR_ALL_TAB_WINDOWS(tp, win)
2770 if (is_qf_win(win, qi))
2771 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002772
Bram Moolenaar9c102382006-05-03 21:26:49 +00002773 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774}
2775
2776/*
2777 * Find the quickfix buffer. If it exists, update the contents.
2778 */
2779 static void
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002780qf_update_buffer(qf_info_T *qi, int update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002783 win_T *win;
2784 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002788 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 if (buf != NULL)
2790 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 /* set curwin/curbuf to buf and save a few things */
2792 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002794 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002795 {
2796 curwin_save = curwin;
2797 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002798 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002799 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002800 }
2801
Bram Moolenaar6920c722016-01-22 22:44:10 +01002802 qf_fill_buffer(qi);
2803
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 /* restore curwin/curbuf and a few other things */
2805 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002807 if (update_cursor)
2808 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810}
2811
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002812/*
2813 * Set "w:quickfix_title" if "qi" has a title.
2814 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002815 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002816qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002817{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002818 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2819 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002820 qi->qf_lists[qi->qf_curlist].qf_title);
2821}
2822
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823/*
2824 * Fill current buffer with quickfix errors, replacing any previous contents.
2825 * curbuf must be the quickfix buffer!
2826 */
2827 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002828qf_fill_buffer(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002830 linenr_T lnum;
2831 qfline_T *qfp;
2832 buf_T *errbuf;
2833 int len;
2834 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835
2836 /* delete all existing lines */
2837 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2838 (void)ml_delete((linenr_T)1, FALSE);
2839
2840 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002841 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
2843 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002844 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2845 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
2847 if (qfp->qf_fnum != 0
2848 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2849 && errbuf->b_fname != NULL)
2850 {
2851 if (qfp->qf_type == 1) /* :helpgrep */
2852 STRCPY(IObuff, gettail(errbuf->b_fname));
2853 else
2854 STRCPY(IObuff, errbuf->b_fname);
2855 len = (int)STRLEN(IObuff);
2856 }
2857 else
2858 len = 0;
2859 IObuff[len++] = '|';
2860
2861 if (qfp->qf_lnum > 0)
2862 {
2863 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2864 len += (int)STRLEN(IObuff + len);
2865
2866 if (qfp->qf_col > 0)
2867 {
2868 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2869 len += (int)STRLEN(IObuff + len);
2870 }
2871
2872 sprintf((char *)IObuff + len, "%s",
2873 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2874 len += (int)STRLEN(IObuff + len);
2875 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002876 else if (qfp->qf_pattern != NULL)
2877 {
2878 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2879 len += (int)STRLEN(IObuff + len);
2880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 IObuff[len++] = '|';
2882 IObuff[len++] = ' ';
2883
2884 /* Remove newlines and leading whitespace from the text.
2885 * For an unrecognized line keep the indent, the compiler may
2886 * mark a word with ^^^^. */
2887 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2888 IObuff + len, IOSIZE - len);
2889
2890 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2891 == FAIL)
2892 break;
2893 qfp = qfp->qf_next;
2894 }
2895 /* Delete the empty line which is now at the end */
2896 (void)ml_delete(lnum + 1, FALSE);
2897 }
2898
2899 /* correct cursor position */
2900 check_lnums(TRUE);
2901
2902 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2903 * resembles reading a file into a buffer, it's more logical when using
2904 * autocommands. */
2905 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2906 curbuf->b_p_ma = FALSE;
2907
2908#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002909 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2911 FALSE, curbuf);
2912 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2913 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002914 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915#endif
2916
2917 /* make sure it will be redrawn */
2918 redraw_curbuf_later(NOT_VALID);
2919
2920 /* Restore KeyTyped, setting 'filetype' may reset it. */
2921 KeyTyped = old_KeyTyped;
2922}
2923
2924#endif /* FEAT_WINDOWS */
2925
2926/*
2927 * Return TRUE if "buf" is the quickfix buffer.
2928 */
2929 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002930bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002932 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933}
2934
2935/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002936 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2937 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 */
2939 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002940bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002942 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2943 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944}
2945
2946/*
2947 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2948 */
2949 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002950bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002952 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953}
2954
2955 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002956bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957{
2958 if (bt_dontwrite(buf))
2959 {
2960 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2961 return TRUE;
2962 }
2963 return FALSE;
2964}
2965
2966/*
2967 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2968 * and 'bufhidden'.
2969 */
2970 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002971buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972{
2973 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2974 switch (buf->b_p_bh[0])
2975 {
2976 case 'u': /* "unload" */
2977 case 'w': /* "wipe" */
2978 case 'd': return FALSE; /* "delete" */
2979 case 'h': return TRUE; /* "hide" */
2980 }
2981 return (p_hid || cmdmod.hide);
2982}
2983
2984/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002985 * Return TRUE when using ":vimgrep" for ":grep".
2986 */
2987 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002988grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002989{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002990 return ((cmdidx == CMD_grep
2991 || cmdidx == CMD_lgrep
2992 || cmdidx == CMD_grepadd
2993 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002994 && STRCMP("internal",
2995 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2996}
2997
2998/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002999 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 */
3001 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003002ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003004 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 char_u *cmd;
3006 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003007 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003008 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003009 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003010#ifdef FEAT_AUTOCMD
3011 char_u *au_name = NULL;
3012
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003013 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3014 if (grep_internal(eap->cmdidx))
3015 {
3016 ex_vimgrep(eap);
3017 return;
3018 }
3019
Bram Moolenaar7c626922005-02-07 22:01:03 +00003020 switch (eap->cmdidx)
3021 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003022 case CMD_make: au_name = (char_u *)"make"; break;
3023 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3024 case CMD_grep: au_name = (char_u *)"grep"; break;
3025 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3026 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3027 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003028 default: break;
3029 }
3030 if (au_name != NULL)
3031 {
3032 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3033 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003034# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003035 if (did_throw || force_abort)
3036 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003037# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003038 }
3039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
Bram Moolenaara6557602006-02-04 22:43:20 +00003041 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3042 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003043 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003044
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003046 fname = get_mef_name();
3047 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003049 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050
3051 /*
3052 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3053 */
3054 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3055 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003056 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 cmd = alloc(len);
3058 if (cmd == NULL)
3059 return;
3060 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3061 (char *)p_shq);
3062 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003063 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 /*
3065 * Output a newline if there's something else than the :make command that
3066 * was typed (in which case the cursor is in column 0).
3067 */
3068 if (msg_col == 0)
3069 msg_didout = FALSE;
3070 msg_start();
3071 MSG_PUTS(":!");
3072 msg_outtrans(cmd); /* show what we are doing */
3073
3074 /* let the shell know if we are redirecting output or not */
3075 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3076
3077#ifdef AMIGA
3078 out_flush();
3079 /* read window status report and redraw before message */
3080 (void)char_avail();
3081#endif
3082
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003083 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003084 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3085 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003086 && eap->cmdidx != CMD_lgrepadd),
3087 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003088 if (wp != NULL)
3089 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003090#ifdef FEAT_AUTOCMD
3091 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003092 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003093 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3094 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003095 if (qi->qf_curlist < qi->qf_listcount)
3096 res = qi->qf_lists[qi->qf_curlist].qf_count;
3097 else
3098 res = 0;
3099 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003100#endif
3101 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003102 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Bram Moolenaar7c626922005-02-07 22:01:03 +00003104 mch_remove(fname);
3105 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 vim_free(cmd);
3107}
3108
3109/*
3110 * Return the name for the errorfile, in allocated memory.
3111 * Find a new unique name when 'makeef' contains "##".
3112 * Returns NULL for error.
3113 */
3114 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003115get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 char_u *p;
3118 char_u *name;
3119 static int start = -1;
3120 static int off = 0;
3121#ifdef HAVE_LSTAT
3122 struct stat sb;
3123#endif
3124
3125 if (*p_mef == NUL)
3126 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003127 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (name == NULL)
3129 EMSG(_(e_notmp));
3130 return name;
3131 }
3132
3133 for (p = p_mef; *p; ++p)
3134 if (p[0] == '#' && p[1] == '#')
3135 break;
3136
3137 if (*p == NUL)
3138 return vim_strsave(p_mef);
3139
3140 /* Keep trying until the name doesn't exist yet. */
3141 for (;;)
3142 {
3143 if (start == -1)
3144 start = mch_get_pid();
3145 else
3146 off += 19;
3147
3148 name = alloc((unsigned)STRLEN(p_mef) + 30);
3149 if (name == NULL)
3150 break;
3151 STRCPY(name, p_mef);
3152 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3153 STRCAT(name, p + 2);
3154 if (mch_getperm(name) < 0
3155#ifdef HAVE_LSTAT
3156 /* Don't accept a symbolic link, its a security risk. */
3157 && mch_lstat((char *)name, &sb) < 0
3158#endif
3159 )
3160 break;
3161 vim_free(name);
3162 }
3163 return name;
3164}
3165
3166/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003167 * Returns the number of valid entries in the current quickfix/location list.
3168 */
3169 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003170qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003171{
3172 qf_info_T *qi = &ql_info;
3173 qfline_T *qfp;
3174 int i, sz = 0;
3175 int prev_fnum = 0;
3176
3177 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3178 {
3179 /* Location list */
3180 qi = GET_LOC_LIST(curwin);
3181 if (qi == NULL)
3182 return 0;
3183 }
3184
3185 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3186 (i < qi->qf_lists[qi->qf_curlist].qf_count) && (qfp != NULL);
3187 ++i, qfp = qfp->qf_next)
3188 {
3189 if (qfp->qf_valid)
3190 {
3191 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3192 sz++; /* Count all valid entries */
3193 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3194 {
3195 /* Count the number of files */
3196 sz++;
3197 prev_fnum = qfp->qf_fnum;
3198 }
3199 }
3200 }
3201
3202 return sz;
3203}
3204
3205/*
3206 * Returns the current index of the quickfix/location list.
3207 * Returns 0 if there is an error.
3208 */
3209 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003210qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003211{
3212 qf_info_T *qi = &ql_info;
3213
3214 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3215 {
3216 /* Location list */
3217 qi = GET_LOC_LIST(curwin);
3218 if (qi == NULL)
3219 return 0;
3220 }
3221
3222 return qi->qf_lists[qi->qf_curlist].qf_index;
3223}
3224
3225/*
3226 * Returns the current index in the quickfix/location list (counting only valid
3227 * entries). If no valid entries are in the list, then returns 1.
3228 */
3229 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003230qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003231{
3232 qf_info_T *qi = &ql_info;
3233 qf_list_T *qfl;
3234 qfline_T *qfp;
3235 int i, eidx = 0;
3236 int prev_fnum = 0;
3237
3238 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3239 {
3240 /* Location list */
3241 qi = GET_LOC_LIST(curwin);
3242 if (qi == NULL)
3243 return 1;
3244 }
3245
3246 qfl = &qi->qf_lists[qi->qf_curlist];
3247 qfp = qfl->qf_start;
3248
3249 /* check if the list has valid errors */
3250 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3251 return 1;
3252
3253 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3254 {
3255 if (qfp->qf_valid)
3256 {
3257 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3258 {
3259 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3260 {
3261 /* Count the number of files */
3262 eidx++;
3263 prev_fnum = qfp->qf_fnum;
3264 }
3265 }
3266 else
3267 eidx++;
3268 }
3269 }
3270
3271 return eidx ? eidx : 1;
3272}
3273
3274/*
3275 * Get the 'n'th valid error entry in the quickfix or location list.
3276 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3277 * For :cdo and :ldo returns the 'n'th valid error entry.
3278 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3279 */
3280 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003281qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003282{
3283 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3284 qfline_T *qfp = qfl->qf_start;
3285 int i, eidx;
3286 int prev_fnum = 0;
3287
3288 /* check if the list has valid errors */
3289 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3290 return 1;
3291
3292 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp!= NULL;
3293 i++, qfp = qfp->qf_next)
3294 {
3295 if (qfp->qf_valid)
3296 {
3297 if (fdo)
3298 {
3299 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3300 {
3301 /* Count the number of files */
3302 eidx++;
3303 prev_fnum = qfp->qf_fnum;
3304 }
3305 }
3306 else
3307 eidx++;
3308 }
3309
3310 if (eidx == n)
3311 break;
3312 }
3313
3314 if (i <= qfl->qf_count)
3315 return i;
3316 else
3317 return 1;
3318}
3319
3320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003322 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003323 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 */
3325 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003326ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003328 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003329 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003330
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003331 if (eap->cmdidx == CMD_ll
3332 || eap->cmdidx == CMD_lrewind
3333 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003334 || eap->cmdidx == CMD_llast
3335 || eap->cmdidx == CMD_ldo
3336 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003337 {
3338 qi = GET_LOC_LIST(curwin);
3339 if (qi == NULL)
3340 {
3341 EMSG(_(e_loclist));
3342 return;
3343 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003344 }
3345
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003346 if (eap->addr_count > 0)
3347 errornr = (int)eap->line2;
3348 else
3349 {
3350 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3351 errornr = 0;
3352 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3353 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3354 errornr = 1;
3355 else
3356 errornr = 32767;
3357 }
3358
3359 /* For cdo and ldo commands, jump to the nth valid error.
3360 * For cfdo and lfdo commands, jump to the nth valid file entry.
3361 */
3362 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3363 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3364 errornr = qf_get_nth_valid_entry(qi,
3365 eap->addr_count > 0 ? (int)eap->line1 : 1,
3366 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3367
3368 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369}
3370
3371/*
3372 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003373 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003374 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 */
3376 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003377ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003379 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003380 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003381
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003382 if (eap->cmdidx == CMD_lnext
3383 || eap->cmdidx == CMD_lNext
3384 || eap->cmdidx == CMD_lprevious
3385 || eap->cmdidx == CMD_lnfile
3386 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003387 || eap->cmdidx == CMD_lpfile
3388 || eap->cmdidx == CMD_ldo
3389 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003390 {
3391 qi = GET_LOC_LIST(curwin);
3392 if (qi == NULL)
3393 {
3394 EMSG(_(e_loclist));
3395 return;
3396 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003397 }
3398
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003399 if (eap->addr_count > 0 &&
3400 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3401 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3402 errornr = (int)eap->line2;
3403 else
3404 errornr = 1;
3405
3406 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3407 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003409 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3410 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003412 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3413 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 ? BACKWARD_FILE
3415 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003416 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417}
3418
3419/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003420 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003421 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 */
3423 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003424ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003426 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003427 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003428#ifdef FEAT_AUTOCMD
3429 char_u *au_name = NULL;
3430#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003431
3432 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003433 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003434 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003435
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003436#ifdef FEAT_AUTOCMD
3437 switch (eap->cmdidx)
3438 {
3439 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3440 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3441 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3442 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3443 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3444 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3445 default: break;
3446 }
3447 if (au_name != NULL)
3448 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3449#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003450#ifdef FEAT_BROWSE
3451 if (cmdmod.browse)
3452 {
3453 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3454 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3455 if (browse_file == NULL)
3456 return;
3457 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3458 vim_free(browse_file);
3459 }
3460 else
3461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003463 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003464
3465 /*
3466 * This function is used by the :cfile, :cgetfile and :caddfile
3467 * commands.
3468 * :cfile always creates a new quickfix list and jumps to the
3469 * first error.
3470 * :cgetfile creates a new quickfix list but doesn't jump to the
3471 * first error.
3472 * :caddfile adds to an existing quickfix list. If there is no
3473 * quickfix list then a new list is created.
3474 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003475 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003476 && eap->cmdidx != CMD_laddfile),
3477 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003478 && (eap->cmdidx == CMD_cfile
3479 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003480 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003481#ifdef FEAT_AUTOCMD
3482 if (au_name != NULL)
3483 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3484#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003485 if (wp != NULL)
3486 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003487 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003488 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003489
3490 else
3491 {
3492#ifdef FEAT_AUTOCMD
3493 if (au_name != NULL)
3494 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3495#endif
3496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497}
3498
3499/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003500 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003501 * ":vimgrepadd {pattern} file(s)"
3502 * ":lvimgrep {pattern} file(s)"
3503 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003504 */
3505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003506ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003507{
Bram Moolenaar81695252004-12-29 20:58:21 +00003508 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003509 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003510 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003511 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003512 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003513 char_u *s;
3514 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003515 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003516 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003517#ifdef FEAT_AUTOCMD
3518 qfline_T *cur_qf_start;
3519#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003520 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003521 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003522 buf_T *buf;
3523 int duplicate_name = FALSE;
3524 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003525 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003526 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003527 buf_T *first_match_buf = NULL;
3528 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003529 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003530#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3531 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003532#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003533 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003534 int flags = 0;
3535 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003536 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003537 char_u *dirname_start = NULL;
3538 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003539 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003540#ifdef FEAT_AUTOCMD
3541 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003542
3543 switch (eap->cmdidx)
3544 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003545 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3546 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3547 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003548 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003549 case CMD_grep: au_name = (char_u *)"grep"; break;
3550 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3551 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3552 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003553 default: break;
3554 }
3555 if (au_name != NULL)
3556 {
3557 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3558 curbuf->b_fname, TRUE, curbuf);
3559 if (did_throw || force_abort)
3560 return;
3561 }
3562#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003563
Bram Moolenaar754b5602006-02-09 23:53:20 +00003564 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003565 || eap->cmdidx == CMD_lvimgrep
3566 || eap->cmdidx == CMD_lgrepadd
3567 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003568 {
3569 qi = ll_get_or_alloc_list(curwin);
3570 if (qi == NULL)
3571 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003572 }
3573
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003574 if (eap->addr_count > 0)
3575 tomatch = eap->line2;
3576 else
3577 tomatch = MAXLNUM;
3578
Bram Moolenaar81695252004-12-29 20:58:21 +00003579 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003580 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003581 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003582 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003583 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003584 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003585 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003586 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003587 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003588
3589 if (s != NULL && *s == NUL)
3590 {
3591 /* Pattern is empty, use last search pattern. */
3592 if (last_search_pat() == NULL)
3593 {
3594 EMSG(_(e_noprevre));
3595 goto theend;
3596 }
3597 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3598 }
3599 else
3600 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3601
Bram Moolenaar86b68352004-12-27 21:59:20 +00003602 if (regmatch.regprog == NULL)
3603 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003604 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003605 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003606
3607 p = skipwhite(p);
3608 if (*p == NUL)
3609 {
3610 EMSG(_("E683: File name missing or invalid pattern"));
3611 goto theend;
3612 }
3613
Bram Moolenaar754b5602006-02-09 23:53:20 +00003614 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003615 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003616 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003617 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003618 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003619 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003620 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003621 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003622 prevp->qf_next != prevp; prevp = prevp->qf_next)
3623 ;
3624
3625 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003626 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003627 goto theend;
3628 if (fcount == 0)
3629 {
3630 EMSG(_(e_nomatch));
3631 goto theend;
3632 }
3633
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003634 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3635 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003636 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003637 {
3638 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003639 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003640 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003641
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003642 /* Remember the current directory, because a BufRead autocommand that does
3643 * ":lcd %:p:h" changes the meaning of short path names. */
3644 mch_dirname(dirname_start, MAXPATHL);
3645
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003646#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003647 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003648 * changing the current quickfix list. */
3649 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3650#endif
3651
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003652 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003653 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003654 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003655 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003656 if (time(NULL) > seconds)
3657 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003658 /* Display the file name every second or so, show the user we are
3659 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003660 seconds = time(NULL);
3661 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003662 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003663 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003664 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003665 else
3666 {
3667 msg_outtrans(p);
3668 vim_free(p);
3669 }
3670 msg_clr_eos();
3671 msg_didout = FALSE; /* overwrite this message */
3672 msg_nowait = TRUE; /* don't wait for this message */
3673 msg_col = 0;
3674 out_flush();
3675 }
3676
Bram Moolenaar81695252004-12-29 20:58:21 +00003677 buf = buflist_findname_exp(fnames[fi]);
3678 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3679 {
3680 /* Remember that a buffer with this name already exists. */
3681 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003682 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003683 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003684
3685#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3686 /* Don't do Filetype autocommands to avoid loading syntax and
3687 * indent scripts, a great speed improvement. */
3688 save_ei = au_event_disable(",Filetype");
3689#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003690 /* Don't use modelines here, it's useless. */
3691 save_mls = p_mls;
3692 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003693
3694 /* Load file into a buffer, so that 'fileencoding' is detected,
3695 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003696 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003697
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003698 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003699#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3700 au_event_restore(save_ei);
3701#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003702 }
3703 else
3704 /* Use existing, loaded buffer. */
3705 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003706
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003707#ifdef FEAT_AUTOCMD
3708 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3709 {
3710 int idx;
3711
3712 /* Autocommands changed the quickfix list. Find the one we were
3713 * using and restore it. */
3714 for (idx = 0; idx < LISTCOUNT; ++idx)
3715 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3716 {
3717 qi->qf_curlist = idx;
3718 break;
3719 }
3720 if (idx == LISTCOUNT)
3721 {
3722 /* List cannot be found, create a new one. */
3723 qf_new_list(qi, *eap->cmdlinep);
3724 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3725 }
3726 }
3727#endif
3728
Bram Moolenaar81695252004-12-29 20:58:21 +00003729 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003730 {
3731 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003732 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003733 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003734 else
3735 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003736 /* Try for a match in all lines of the buffer.
3737 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003738 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003739 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3740 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003741 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003742 col = 0;
3743 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003744 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003745 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003746 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003747 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003748 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003749 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003750 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003751 ml_get_buf(buf,
3752 regmatch.startpos[0].lnum + lnum, FALSE),
3753 regmatch.startpos[0].lnum + lnum,
3754 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003755 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003756 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003757 0, /* nr */
3758 0, /* type */
3759 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003760 ) == FAIL)
3761 {
3762 got_int = TRUE;
3763 break;
3764 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003765 found_match = TRUE;
3766 if (--tomatch == 0)
3767 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003768 if ((flags & VGR_GLOBAL) == 0
3769 || regmatch.endpos[0].lnum > 0)
3770 break;
3771 col = regmatch.endpos[0].col
3772 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003773 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003774 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003775 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003776 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003777 if (got_int)
3778 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003779 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003780#ifdef FEAT_AUTOCMD
3781 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3782#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003783
3784 if (using_dummy)
3785 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003786 if (found_match && first_match_buf == NULL)
3787 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003788 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003789 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003790 /* Never keep a dummy buffer if there is another buffer
3791 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003792 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003793 buf = NULL;
3794 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003795 else if (!cmdmod.hide
3796 || buf->b_p_bh[0] == 'u' /* "unload" */
3797 || buf->b_p_bh[0] == 'w' /* "wipe" */
3798 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003799 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003800 /* When no match was found we don't need to remember the
3801 * buffer, wipe it out. If there was a match and it
3802 * wasn't the first one or we won't jump there: only
3803 * unload the buffer.
3804 * Ignore 'hidden' here, because it may lead to having too
3805 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003806 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003807 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003808 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003809 buf = NULL;
3810 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003811 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003812 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003813 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003814 buf = NULL;
3815 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003816 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003817
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003818 if (buf != NULL)
3819 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003820 /* If the buffer is still loaded we need to use the
3821 * directory we jumped to below. */
3822 if (buf == first_match_buf
3823 && target_dir == NULL
3824 && STRCMP(dirname_start, dirname_now) != 0)
3825 target_dir = vim_strsave(dirname_now);
3826
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003827 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003828 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003829 * need to be done (again). But not the window-local
3830 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003831 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003832#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003833 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3834 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003835#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003836 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003837 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003838 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003839 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003840 }
3841 }
3842
3843 FreeWild(fcount, fnames);
3844
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003845 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3846 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3847 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003848
3849#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003850 qf_update_buffer(qi, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003851#endif
3852
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003853#ifdef FEAT_AUTOCMD
3854 if (au_name != NULL)
3855 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3856 curbuf->b_fname, TRUE, curbuf);
3857#endif
3858
Bram Moolenaar86b68352004-12-27 21:59:20 +00003859 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003860 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003861 {
3862 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003863 {
3864 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003865 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003866 if (buf != curbuf)
3867 /* If we jumped to another buffer redrawing will already be
3868 * taken care of. */
3869 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003870
3871 /* Jump to the directory used after loading the buffer. */
3872 if (curbuf == first_match_buf && target_dir != NULL)
3873 {
3874 exarg_T ea;
3875
3876 ea.arg = target_dir;
3877 ea.cmdidx = CMD_lcd;
3878 ex_cd(&ea);
3879 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003880 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003881 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003882 else
3883 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003884
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003885 /* If we loaded a dummy buffer into the current window, the autocommands
3886 * may have messed up things, need to redraw and recompute folds. */
3887 if (redraw_for_dummy)
3888 {
3889#ifdef FEAT_FOLDING
3890 foldUpdateAll(curwin);
3891#else
3892 redraw_later(NOT_VALID);
3893#endif
3894 }
3895
Bram Moolenaar86b68352004-12-27 21:59:20 +00003896theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01003897 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003898 vim_free(dirname_now);
3899 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003900 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003901 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003902}
3903
3904/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003905 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003906 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003907 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3908 * If "s" is not NULL terminate the pattern with a NUL.
3909 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003910 */
3911 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003912skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003913{
3914 int c;
3915
3916 if (vim_isIDc(*p))
3917 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003918 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003919 if (s != NULL)
3920 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003921 p = skiptowhite(p);
3922 if (s != NULL && *p != NUL)
3923 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003924 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003925 else
3926 {
3927 /* ":vimgrep /pattern/[g][j] fname" */
3928 if (s != NULL)
3929 *s = p + 1;
3930 c = *p;
3931 p = skip_regexp(p + 1, c, TRUE, NULL);
3932 if (*p != c)
3933 return NULL;
3934
3935 /* Truncate the pattern. */
3936 if (s != NULL)
3937 *p = NUL;
3938 ++p;
3939
3940 /* Find the flags */
3941 while (*p == 'g' || *p == 'j')
3942 {
3943 if (flags != NULL)
3944 {
3945 if (*p == 'g')
3946 *flags |= VGR_GLOBAL;
3947 else
3948 *flags |= VGR_NOJUMP;
3949 }
3950 ++p;
3951 }
3952 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003953 return p;
3954}
3955
3956/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003957 * Restore current working directory to "dirname_start" if they differ, taking
3958 * into account whether it is set locally or globally.
3959 */
3960 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003961restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003962{
3963 char_u *dirname_now = alloc(MAXPATHL);
3964
3965 if (NULL != dirname_now)
3966 {
3967 mch_dirname(dirname_now, MAXPATHL);
3968 if (STRCMP(dirname_start, dirname_now) != 0)
3969 {
3970 /* If the directory has changed, change it back by building up an
3971 * appropriate ex command and executing it. */
3972 exarg_T ea;
3973
3974 ea.arg = dirname_start;
3975 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3976 ex_cd(&ea);
3977 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01003978 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003979 }
3980}
3981
3982/*
3983 * Load file "fname" into a dummy buffer and return the buffer pointer,
3984 * placing the directory resulting from the buffer load into the
3985 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3986 * prior to calling this function. Restores directory to "dirname_start" prior
3987 * to returning, if autocmds or the 'autochdir' option have changed it.
3988 *
3989 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3990 * or wipe_dummy_buffer() later!
3991 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003992 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003993 */
3994 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003995load_dummy_buffer(
3996 char_u *fname,
3997 char_u *dirname_start, /* in: old directory */
3998 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003999{
4000 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004001 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004002 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004003 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004004
4005 /* Allocate a buffer without putting it in the buffer list. */
4006 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4007 if (newbuf == NULL)
4008 return NULL;
4009
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004010 /* Init the options. */
4011 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4012
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004013 /* need to open the memfile before putting the buffer in a window */
4014 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004015 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004016 /* set curwin/curbuf to buf and save a few things */
4017 aucmd_prepbuf(&aco, newbuf);
4018
4019 /* Need to set the filename for autocommands. */
4020 (void)setfname(curbuf, fname, NULL, FALSE);
4021
Bram Moolenaar81695252004-12-29 20:58:21 +00004022 /* Create swap file now to avoid the ATTENTION message. */
4023 check_need_swap(TRUE);
4024
4025 /* Remove the "dummy" flag, otherwise autocommands may not
4026 * work. */
4027 curbuf->b_flags &= ~BF_DUMMY;
4028
4029 if (readfile(fname, NULL,
4030 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4031 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004032 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004033 && !(curbuf->b_flags & BF_NEW))
4034 {
4035 failed = FALSE;
4036 if (curbuf != newbuf)
4037 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004038 /* Bloody autocommands changed the buffer! Can happen when
4039 * using netrw and editing a remote file. Use the current
4040 * buffer instead, delete the dummy one after restoring the
4041 * window stuff. */
4042 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004043 newbuf = curbuf;
4044 }
4045 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004046
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004047 /* restore curwin/curbuf and a few other things */
4048 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004049 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
4050 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004051 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004052
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004053 /*
4054 * When autocommands/'autochdir' option changed directory: go back.
4055 * Let the caller know what the resulting dir was first, in case it is
4056 * important.
4057 */
4058 mch_dirname(resulting_dir, MAXPATHL);
4059 restore_start_dir(dirname_start);
4060
Bram Moolenaar81695252004-12-29 20:58:21 +00004061 if (!buf_valid(newbuf))
4062 return NULL;
4063 if (failed)
4064 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004065 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004066 return NULL;
4067 }
4068 return newbuf;
4069}
4070
4071/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004072 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4073 * directory to "dirname_start" prior to returning, if autocmds or the
4074 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004075 */
4076 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004077wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004078{
4079 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004080 {
4081#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4082 cleanup_T cs;
4083
4084 /* Reset the error/interrupt/exception state here so that aborting()
4085 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4086 * work when got_int is set. */
4087 enter_cleanup(&cs);
4088#endif
4089
Bram Moolenaar81695252004-12-29 20:58:21 +00004090 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004091
4092#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4093 /* Restore the error/interrupt/exception state if not discarded by a
4094 * new aborting error, interrupt, or uncaught exception. */
4095 leave_cleanup(&cs);
4096#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004097 /* When autocommands/'autochdir' option changed directory: go back. */
4098 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004099 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004100}
4101
4102/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004103 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4104 * directory to "dirname_start" prior to returning, if autocmds or the
4105 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004106 */
4107 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004108unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004109{
4110 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004111 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004112 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004113
4114 /* When autocommands/'autochdir' option changed directory: go back. */
4115 restore_start_dir(dirname_start);
4116 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004117}
4118
Bram Moolenaar05159a02005-02-26 23:04:13 +00004119#if defined(FEAT_EVAL) || defined(PROTO)
4120/*
4121 * Add each quickfix error to list "list" as a dictionary.
4122 */
4123 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004124get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004125{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004126 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004127 dict_T *dict;
4128 char_u buf[2];
4129 qfline_T *qfp;
4130 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004131 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004132
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004133 if (wp != NULL)
4134 {
4135 qi = GET_LOC_LIST(wp);
4136 if (qi == NULL)
4137 return FAIL;
4138 }
4139
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004140 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004141 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004142 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004143
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004144 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4145 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004146 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004147 /* Handle entries with a non-existing buffer number. */
4148 bufnum = qfp->qf_fnum;
4149 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4150 bufnum = 0;
4151
Bram Moolenaar05159a02005-02-26 23:04:13 +00004152 if ((dict = dict_alloc()) == NULL)
4153 return FAIL;
4154 if (list_append_dict(list, dict) == FAIL)
4155 return FAIL;
4156
4157 buf[0] = qfp->qf_type;
4158 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004159 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004160 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4161 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4162 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4163 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004164 || dict_add_nr_str(dict, "pattern", 0L,
4165 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4166 || dict_add_nr_str(dict, "text", 0L,
4167 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004168 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4169 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4170 return FAIL;
4171
4172 qfp = qfp->qf_next;
4173 }
4174 return OK;
4175}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004176
4177/*
4178 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02004179 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004180 */
4181 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004182set_errorlist(
4183 win_T *wp,
4184 list_T *list,
4185 int action,
4186 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004187{
4188 listitem_T *li;
4189 dict_T *d;
4190 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004191 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004192 long lnum;
4193 int col, nr;
4194 int vcol;
4195 qfline_T *prevp = NULL;
4196 int valid, status;
4197 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004198 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004199 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004200
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004201 if (wp != NULL)
4202 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004203 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004204 if (qi == NULL)
4205 return FAIL;
4206 }
4207
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004208 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004209 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004210 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004211 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004212 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004213 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004214 prevp->qf_next != prevp; prevp = prevp->qf_next)
4215 ;
4216 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004217 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004218 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004219 qf_store_title(qi, title);
4220 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004221
4222 for (li = list->lv_first; li != NULL; li = li->li_next)
4223 {
4224 if (li->li_tv.v_type != VAR_DICT)
4225 continue; /* Skip non-dict items */
4226
4227 d = li->li_tv.vval.v_dict;
4228 if (d == NULL)
4229 continue;
4230
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004231 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004232 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004233 lnum = get_dict_number(d, (char_u *)"lnum");
4234 col = get_dict_number(d, (char_u *)"col");
4235 vcol = get_dict_number(d, (char_u *)"vcol");
4236 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004237 type = get_dict_string(d, (char_u *)"type", TRUE);
4238 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4239 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004240 if (text == NULL)
4241 text = vim_strsave((char_u *)"");
4242
4243 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004244 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004245 valid = FALSE;
4246
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004247 /* Mark entries with non-existing buffer number as not valid. Give the
4248 * error message only once. */
4249 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4250 {
4251 if (!did_bufnr_emsg)
4252 {
4253 did_bufnr_emsg = TRUE;
4254 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4255 }
4256 valid = FALSE;
4257 bufnum = 0;
4258 }
4259
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004260 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004261 NULL, /* dir */
4262 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004263 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004264 text,
4265 lnum,
4266 col,
4267 vcol, /* vis_col */
4268 pattern, /* search pattern */
4269 nr,
4270 type == NULL ? NUL : *type,
4271 valid);
4272
4273 vim_free(filename);
4274 vim_free(pattern);
4275 vim_free(text);
4276 vim_free(type);
4277
4278 if (status == FAIL)
4279 {
4280 retval = FAIL;
4281 break;
4282 }
4283 }
4284
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004285 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004286 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004287 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4288 else
4289 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004290 if (action != 'a') {
4291 qi->qf_lists[qi->qf_curlist].qf_ptr =
4292 qi->qf_lists[qi->qf_curlist].qf_start;
4293 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4294 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4295 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004296
4297#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004298 /* Don't update the cursor in quickfix window when appending entries */
4299 qf_update_buffer(qi, (action != 'a'));
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004300#endif
4301
4302 return retval;
4303}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004304#endif
4305
Bram Moolenaar81695252004-12-29 20:58:21 +00004306/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004307 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004308 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004309 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004310 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004311 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004312 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004313 */
4314 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004315ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004316{
4317 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004318 qf_info_T *qi = &ql_info;
4319
Bram Moolenaardb552d602006-03-23 22:59:57 +00004320 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4321 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004322 {
4323 qi = ll_get_or_alloc_list(curwin);
4324 if (qi == NULL)
4325 return;
4326 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004327
4328 if (*eap->arg == NUL)
4329 buf = curbuf;
4330 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4331 buf = buflist_findnr(atoi((char *)eap->arg));
4332 if (buf == NULL)
4333 EMSG(_(e_invarg));
4334 else if (buf->b_ml.ml_mfp == NULL)
4335 EMSG(_("E681: Buffer is not loaded"));
4336 else
4337 {
4338 if (eap->addr_count == 0)
4339 {
4340 eap->line1 = 1;
4341 eap->line2 = buf->b_ml.ml_line_count;
4342 }
4343 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4344 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4345 EMSG(_(e_invrange));
4346 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004347 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004348 char_u *qf_title = *eap->cmdlinep;
4349
4350 if (buf->b_sfname)
4351 {
4352 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4353 (char *)qf_title, (char *)buf->b_sfname);
4354 qf_title = IObuff;
4355 }
4356
Bram Moolenaardb552d602006-03-23 22:59:57 +00004357 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4358 (eap->cmdidx != CMD_caddbuffer
4359 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004360 eap->line1, eap->line2,
4361 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004362 && (eap->cmdidx == CMD_cbuffer
4363 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004364 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4365 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004366 }
4367}
4368
Bram Moolenaar1e015462005-09-25 22:16:38 +00004369#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004370/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004371 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4372 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004373 */
4374 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004375ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004376{
4377 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004378 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004379
Bram Moolenaardb552d602006-03-23 22:59:57 +00004380 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4381 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004382 {
4383 qi = ll_get_or_alloc_list(curwin);
4384 if (qi == NULL)
4385 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004386 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004387
Bram Moolenaar4770d092006-01-12 23:22:24 +00004388 /* Evaluate the expression. When the result is a string or a list we can
4389 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004390 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004391 if (tv != NULL)
4392 {
4393 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4394 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4395 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004396 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004397 (eap->cmdidx != CMD_caddexpr
4398 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004399 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004400 && (eap->cmdidx == CMD_cexpr
4401 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004402 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004403 }
4404 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004405 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004406 free_tv(tv);
4407 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004408}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004409#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004410
4411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 * ":helpgrep {pattern}"
4413 */
4414 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004415ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
4417 regmatch_T regmatch;
4418 char_u *save_cpo;
4419 char_u *p;
4420 int fcount;
4421 char_u **fnames;
4422 FILE *fd;
4423 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004424 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004426#ifdef FEAT_MULTI_LANG
4427 char_u *lang;
4428#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004429 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004430 int new_qi = FALSE;
4431 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004432#ifdef FEAT_AUTOCMD
4433 char_u *au_name = NULL;
4434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004436#ifdef FEAT_MULTI_LANG
4437 /* Check for a specified language */
4438 lang = check_help_lang(eap->arg);
4439#endif
4440
Bram Moolenaar73633f82012-01-20 13:39:07 +01004441#ifdef FEAT_AUTOCMD
4442 switch (eap->cmdidx)
4443 {
4444 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4445 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4446 default: break;
4447 }
4448 if (au_name != NULL)
4449 {
4450 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4451 curbuf->b_fname, TRUE, curbuf);
4452 if (did_throw || force_abort)
4453 return;
4454 }
4455#endif
4456
4457 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4458 save_cpo = p_cpo;
4459 p_cpo = empty_option;
4460
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004461 if (eap->cmdidx == CMD_lhelpgrep)
4462 {
4463 /* Find an existing help window */
4464 FOR_ALL_WINDOWS(wp)
4465 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4466 break;
4467
4468 if (wp == NULL) /* Help window not found */
4469 qi = NULL;
4470 else
4471 qi = wp->w_llist;
4472
4473 if (qi == NULL)
4474 {
4475 /* Allocate a new location list for help text matches */
4476 if ((qi = ll_new_list()) == NULL)
4477 return;
4478 new_qi = TRUE;
4479 }
4480 }
4481
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4483 regmatch.rm_ic = FALSE;
4484 if (regmatch.regprog != NULL)
4485 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004486#ifdef FEAT_MBYTE
4487 vimconv_T vc;
4488
4489 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4490 * differs. */
4491 vc.vc_type = CONV_NONE;
4492 if (!enc_utf8)
4493 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4494#endif
4495
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004497 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 /* Go through all directories in 'runtimepath' */
4500 p = p_rtp;
4501 while (*p != NUL && !got_int)
4502 {
4503 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4504
4505 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4506 add_pathsep(NameBuff);
4507 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4508 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4509 &fnames, EW_FILE|EW_SILENT) == OK
4510 && fcount > 0)
4511 {
4512 for (fi = 0; fi < fcount && !got_int; ++fi)
4513 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004514#ifdef FEAT_MULTI_LANG
4515 /* Skip files for a different language. */
4516 if (lang != NULL
4517 && STRNICMP(lang, fnames[fi]
4518 + STRLEN(fnames[fi]) - 3, 2) != 0
4519 && !(STRNICMP(lang, "en", 2) == 0
4520 && STRNICMP("txt", fnames[fi]
4521 + STRLEN(fnames[fi]) - 3, 3) == 0))
4522 continue;
4523#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004524 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 if (fd != NULL)
4526 {
4527 lnum = 1;
4528 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4529 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004530 char_u *line = IObuff;
4531#ifdef FEAT_MBYTE
4532 /* Convert a line if 'encoding' is not utf-8 and
4533 * the line contains a non-ASCII character. */
4534 if (vc.vc_type != CONV_NONE
4535 && has_non_ascii(IObuff)) {
4536 line = string_convert(&vc, IObuff, NULL);
4537 if (line == NULL)
4538 line = IObuff;
4539 }
4540#endif
4541
4542 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004544 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004547 while (l > 0 && line[l - 1] <= ' ')
4548 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004550 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 NULL, /* dir */
4552 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004553 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004554 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004556 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004557 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004558 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004559 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 0, /* nr */
4561 1, /* type */
4562 TRUE /* valid */
4563 ) == FAIL)
4564 {
4565 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004566#ifdef FEAT_MBYTE
4567 if (line != IObuff)
4568 vim_free(line);
4569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 break;
4571 }
4572 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004573#ifdef FEAT_MBYTE
4574 if (line != IObuff)
4575 vim_free(line);
4576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 ++lnum;
4578 line_breakcheck();
4579 }
4580 fclose(fd);
4581 }
4582 }
4583 FreeWild(fcount, fnames);
4584 }
4585 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004586
Bram Moolenaar473de612013-06-08 18:19:48 +02004587 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004588#ifdef FEAT_MBYTE
4589 if (vc.vc_type != CONV_NONE)
4590 convert_setup(&vc, NULL, NULL);
4591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004593 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4594 qi->qf_lists[qi->qf_curlist].qf_ptr =
4595 qi->qf_lists[qi->qf_curlist].qf_start;
4596 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004599 if (p_cpo == empty_option)
4600 p_cpo = save_cpo;
4601 else
4602 /* Darn, some plugin changed the value. */
4603 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
4605#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004606 qf_update_buffer(qi, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#endif
4608
Bram Moolenaar73633f82012-01-20 13:39:07 +01004609#ifdef FEAT_AUTOCMD
4610 if (au_name != NULL)
4611 {
4612 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4613 curbuf->b_fname, TRUE, curbuf);
4614 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4615 /* autocommands made "qi" invalid */
4616 return;
4617 }
4618#endif
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004621 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004622 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004623 else
4624 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004625
4626 if (eap->cmdidx == CMD_lhelpgrep)
4627 {
4628 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004629 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004630 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4631 {
4632 if (new_qi)
4633 ll_free_all(&qi);
4634 }
4635 else if (curwin->w_llist == NULL)
4636 curwin->w_llist = qi;
4637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638}
4639
4640#endif /* FEAT_QUICKFIX */