blob: 8c55a16c7f59eb1f2c0b6e077af51f9b70e792ae [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020050/*
51 * Quickfix/Location list definition
52 * Contains a list of entries (qfline_T). qf_start points to the first entry
53 * and qf_last points to the last entry. qf_count contains the list size.
54 *
55 * Usually the list contains one or more entries. But an empty list can be
56 * created using setqflist()/setloclist() with a title and/or user context
57 * information and entries can be added later using setqflist()/setloclist().
58 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000059typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000060{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000061 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020062 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000063 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020064 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000065 int qf_index; /* current index in the error list */
66 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020067 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020068 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020069 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020070
71 struct dir_stack_T *qf_dir_stack;
72 char_u *qf_directory;
73 struct dir_stack_T *qf_file_stack;
74 char_u *qf_currfile;
75 int qf_multiline;
76 int qf_multiignore;
77 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000078} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020080/*
81 * Quickfix/Location list stack definition
82 * Contains a list of quickfix/location lists (qf_list_T)
83 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000084struct qf_info_S
85{
86 /*
87 * Count of references to this list. Used only for location lists.
88 * When a location list window reference this list, qf_refcount
89 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
90 * reaches 0, the list is freed.
91 */
92 int qf_refcount;
93 int qf_listcount; /* current number of lists */
94 int qf_curlist; /* current error list */
95 qf_list_T qf_lists[LISTCOUNT];
96};
97
98static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000100#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
102/*
103 * Structure used to hold the info of one part of 'errorformat'
104 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000105typedef struct efm_S efm_T;
106struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107{
108 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000109 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
111 char_u prefix; /* prefix of this format line: */
112 /* 'D' enter directory */
113 /* 'X' leave directory */
114 /* 'A' start of multi-line message */
115 /* 'E' error message */
116 /* 'W' warning message */
117 /* 'I' informational message */
118 /* 'C' continuation line */
119 /* 'Z' end of multi-line message */
120 /* 'G' general, unspecific message */
121 /* 'P' push file (partial) message */
122 /* 'Q' pop/quit file (partial) message */
123 /* 'O' overread (partial) message */
124 char_u flags; /* additional flags given in prefix */
125 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000126 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000127 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128};
129
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100130static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
131
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200132static int qf_init_ext(qf_info_T *qi, int qf_idx, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title, char_u *enc);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200133static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static void qf_new_list(qf_info_T *qi, char_u *qf_title);
135static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200136static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void qf_free(qf_info_T *qi, int idx);
139static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200140static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200141static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200143static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
145static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
148static int is_qf_win(win_T *win, qf_info_T *qi);
149static win_T *qf_find_win(qf_info_T *qi);
150static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200151static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200153static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static char_u *get_mef_name(void);
156static void restore_start_dir(char_u *dirname_start);
157static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
158static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
159static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
160static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162/* Quickfix window check helper macro */
163#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
164/* Location list window check helper macro */
165#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
166/*
167 * Return location list for window 'wp'
168 * For location list window, return the referenced location list
169 */
170#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200173 * Looking up a buffer can be slow if there are many. Remember the last one
174 * to make this a lot faster if there are multiple matches in the same file.
175 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200176static char_u *qf_last_bufname = NULL;
177static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178
179/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000180 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200181 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 * Return -1 for error, number of errors for success.
183 */
184 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100185qf_init(
186 win_T *wp,
187 char_u *efile,
188 char_u *errorformat,
189 int newlist, /* TRUE: start a new error list */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100190 char_u *qf_title,
191 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000193 qf_info_T *qi = &ql_info;
194
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000195 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000196 {
197 qi = ll_get_or_alloc_list(wp);
198 if (qi == NULL)
199 return FAIL;
200 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000201
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200202 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
203 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000204}
205
206/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200207 * Maximum number of bytes allowed per line while reading a errorfile.
208 */
209#define LINE_MAXLEN 4096
210
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200211static struct fmtpattern
212{
213 char_u convchar;
214 char *pattern;
215} fmt_pat[FMT_PATTERNS] =
216 {
217 {'f', ".\\+"}, /* only used when at end */
218 {'n', "\\d\\+"},
219 {'l', "\\d\\+"},
220 {'c', "\\d\\+"},
221 {'t', "."},
222 {'m', ".\\+"},
223 {'r', ".*"},
224 {'p', "[- .]*"},
225 {'v', "\\d\\+"},
226 {'s', ".\\+"}
227 };
228
229/*
230 * Converts a 'errorformat' string to regular expression pattern
231 */
232 static int
233efm_to_regpat(
234 char_u *efm,
235 int len,
236 efm_T *fmt_ptr,
237 char_u *regpat,
238 char_u *errmsg)
239{
240 char_u *ptr;
241 char_u *efmp;
242 char_u *srcptr;
243 int round;
244 int idx = 0;
245
246 /*
247 * Build regexp pattern from current 'errorformat' option
248 */
249 ptr = regpat;
250 *ptr++ = '^';
251 round = 0;
252 for (efmp = efm; efmp < efm + len; ++efmp)
253 {
254 if (*efmp == '%')
255 {
256 ++efmp;
257 for (idx = 0; idx < FMT_PATTERNS; ++idx)
258 if (fmt_pat[idx].convchar == *efmp)
259 break;
260 if (idx < FMT_PATTERNS)
261 {
262 if (fmt_ptr->addr[idx])
263 {
264 sprintf((char *)errmsg,
265 _("E372: Too many %%%c in format string"), *efmp);
266 EMSG(errmsg);
267 return -1;
268 }
269 if ((idx
270 && idx < 6
271 && vim_strchr((char_u *)"DXOPQ",
272 fmt_ptr->prefix) != NULL)
273 || (idx == 6
274 && vim_strchr((char_u *)"OPQ",
275 fmt_ptr->prefix) == NULL))
276 {
277 sprintf((char *)errmsg,
278 _("E373: Unexpected %%%c in format string"), *efmp);
279 EMSG(errmsg);
280 return -1;
281 }
282 fmt_ptr->addr[idx] = (char_u)++round;
283 *ptr++ = '\\';
284 *ptr++ = '(';
285#ifdef BACKSLASH_IN_FILENAME
286 if (*efmp == 'f')
287 {
288 /* Also match "c:" in the file name, even when
289 * checking for a colon next: "%f:".
290 * "\%(\a:\)\=" */
291 STRCPY(ptr, "\\%(\\a:\\)\\=");
292 ptr += 10;
293 }
294#endif
295 if (*efmp == 'f' && efmp[1] != NUL)
296 {
297 if (efmp[1] != '\\' && efmp[1] != '%')
298 {
299 /* A file name may contain spaces, but this isn't
300 * in "\f". For "%f:%l:%m" there may be a ":" in
301 * the file name. Use ".\{-1,}x" instead (x is
302 * the next character), the requirement that :999:
303 * follows should work. */
304 STRCPY(ptr, ".\\{-1,}");
305 ptr += 7;
306 }
307 else
308 {
309 /* File name followed by '\\' or '%': include as
310 * many file name chars as possible. */
311 STRCPY(ptr, "\\f\\+");
312 ptr += 4;
313 }
314 }
315 else
316 {
317 srcptr = (char_u *)fmt_pat[idx].pattern;
318 while ((*ptr = *srcptr++) != NUL)
319 ++ptr;
320 }
321 *ptr++ = '\\';
322 *ptr++ = ')';
323 }
324 else if (*efmp == '*')
325 {
326 if (*++efmp == '[' || *efmp == '\\')
327 {
328 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
329 {
330 if (efmp[1] == '^')
331 *ptr++ = *++efmp;
332 if (efmp < efm + len)
333 {
334 *ptr++ = *++efmp; /* could be ']' */
335 while (efmp < efm + len
336 && (*ptr++ = *++efmp) != ']')
337 /* skip */;
338 if (efmp == efm + len)
339 {
340 EMSG(_("E374: Missing ] in format string"));
341 return -1;
342 }
343 }
344 }
345 else if (efmp < efm + len) /* %*\D, %*\s etc. */
346 *ptr++ = *++efmp;
347 *ptr++ = '\\';
348 *ptr++ = '+';
349 }
350 else
351 {
352 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
353 sprintf((char *)errmsg,
354 _("E375: Unsupported %%%c in format string"), *efmp);
355 EMSG(errmsg);
356 return -1;
357 }
358 }
359 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
360 *ptr++ = *efmp; /* regexp magic characters */
361 else if (*efmp == '#')
362 *ptr++ = '*';
363 else if (*efmp == '>')
364 fmt_ptr->conthere = TRUE;
365 else if (efmp == efm + 1) /* analyse prefix */
366 {
367 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
368 fmt_ptr->flags = *efmp++;
369 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
370 fmt_ptr->prefix = *efmp;
371 else
372 {
373 sprintf((char *)errmsg,
374 _("E376: Invalid %%%c in format string prefix"), *efmp);
375 EMSG(errmsg);
376 return -1;
377 }
378 }
379 else
380 {
381 sprintf((char *)errmsg,
382 _("E377: Invalid %%%c in format string"), *efmp);
383 EMSG(errmsg);
384 return -1;
385 }
386 }
387 else /* copy normal character */
388 {
389 if (*efmp == '\\' && efmp + 1 < efm + len)
390 ++efmp;
391 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
392 *ptr++ = '\\'; /* escape regexp atoms */
393 if (*efmp)
394 *ptr++ = *efmp;
395 }
396 }
397 *ptr++ = '$';
398 *ptr = NUL;
399
400 return 0;
401}
402
403 static void
404free_efm_list(efm_T **efm_first)
405{
406 efm_T *efm_ptr;
407
408 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
409 {
410 *efm_first = efm_ptr->next;
411 vim_regfree(efm_ptr->prog);
412 vim_free(efm_ptr);
413 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100414 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200415}
416
417/* Parse 'errorformat' option */
418 static efm_T *
419parse_efm_option(char_u *efm)
420{
421 char_u *errmsg = NULL;
422 int errmsglen;
423 efm_T *fmt_ptr = NULL;
424 efm_T *fmt_first = NULL;
425 efm_T *fmt_last = NULL;
426 char_u *fmtstr = NULL;
427 int len;
428 int i;
429 int round;
430
431 errmsglen = CMDBUFFSIZE + 1;
432 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
433 if (errmsg == NULL)
434 goto parse_efm_end;
435
436 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200437 * Each part of the format string is copied and modified from errorformat
438 * to regex prog. Only a few % characters are allowed.
439 */
440
441 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 * Get some space to modify the format string into.
443 */
444 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
445 for (round = FMT_PATTERNS; round > 0; )
446 i += (int)STRLEN(fmt_pat[--round].pattern);
447#ifdef COLON_IN_FILENAME
448 i += 12; /* "%f" can become twelve chars longer */
449#else
450 i += 2; /* "%f" can become two chars longer */
451#endif
452 if ((fmtstr = alloc(i)) == NULL)
453 goto parse_efm_error;
454
455 while (efm[0] != NUL)
456 {
457 /*
458 * Allocate a new eformat structure and put it at the end of the list
459 */
460 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
461 if (fmt_ptr == NULL)
462 goto parse_efm_error;
463 if (fmt_first == NULL) /* first one */
464 fmt_first = fmt_ptr;
465 else
466 fmt_last->next = fmt_ptr;
467 fmt_last = fmt_ptr;
468
469 /*
470 * Isolate one part in the 'errorformat' option
471 */
472 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
473 if (efm[len] == '\\' && efm[len + 1] != NUL)
474 ++len;
475
476 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
477 goto parse_efm_error;
478 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
479 goto parse_efm_error;
480 /*
481 * Advance to next part
482 */
483 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
484 }
485
486 if (fmt_first == NULL) /* nothing found */
487 EMSG(_("E378: 'errorformat' contains no pattern"));
488
489 goto parse_efm_end;
490
491parse_efm_error:
492 free_efm_list(&fmt_first);
493
494parse_efm_end:
495 vim_free(fmtstr);
496 vim_free(errmsg);
497
498 return fmt_first;
499}
500
Bram Moolenaare0d37972016-07-15 22:36:01 +0200501enum {
502 QF_FAIL = 0,
503 QF_OK = 1,
504 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200505 QF_NOMEM = 3,
506 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200507};
508
509typedef struct {
510 char_u *linebuf;
511 int linelen;
512 char_u *growbuf;
513 int growbufsiz;
514 FILE *fd;
515 typval_T *tv;
516 char_u *p_str;
517 listitem_T *p_li;
518 buf_T *buf;
519 linenr_T buflnum;
520 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100521 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200522} qfstate_T;
523
524 static char_u *
525qf_grow_linebuf(qfstate_T *state, int newsz)
526{
527 /*
528 * If the line exceeds LINE_MAXLEN exclude the last
529 * byte since it's not a NL character.
530 */
531 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
532 if (state->growbuf == NULL)
533 {
534 state->growbuf = alloc(state->linelen + 1);
535 if (state->growbuf == NULL)
536 return NULL;
537 state->growbufsiz = state->linelen;
538 }
539 else if (state->linelen > state->growbufsiz)
540 {
541 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
542 if (state->growbuf == NULL)
543 return NULL;
544 state->growbufsiz = state->linelen;
545 }
546 return state->growbuf;
547}
548
549/*
550 * Get the next string (separated by newline) from state->p_str.
551 */
552 static int
553qf_get_next_str_line(qfstate_T *state)
554{
555 /* Get the next line from the supplied string */
556 char_u *p_str = state->p_str;
557 char_u *p;
558 int len;
559
560 if (*p_str == NUL) /* Reached the end of the string */
561 return QF_END_OF_INPUT;
562
563 p = vim_strchr(p_str, '\n');
564 if (p != NULL)
565 len = (int)(p - p_str) + 1;
566 else
567 len = (int)STRLEN(p_str);
568
569 if (len > IOSIZE - 2)
570 {
571 state->linebuf = qf_grow_linebuf(state, len);
572 if (state->linebuf == NULL)
573 return QF_NOMEM;
574 }
575 else
576 {
577 state->linebuf = IObuff;
578 state->linelen = len;
579 }
580 vim_strncpy(state->linebuf, p_str, state->linelen);
581
582 /*
583 * Increment using len in order to discard the rest of the
584 * line if it exceeds LINE_MAXLEN.
585 */
586 p_str += len;
587 state->p_str = p_str;
588
589 return QF_OK;
590}
591
592/*
593 * Get the next string from state->p_Li.
594 */
595 static int
596qf_get_next_list_line(qfstate_T *state)
597{
598 listitem_T *p_li = state->p_li;
599 int len;
600
601 while (p_li != NULL
602 && (p_li->li_tv.v_type != VAR_STRING
603 || p_li->li_tv.vval.v_string == NULL))
604 p_li = p_li->li_next; /* Skip non-string items */
605
606 if (p_li == NULL) /* End of the list */
607 {
608 state->p_li = NULL;
609 return QF_END_OF_INPUT;
610 }
611
612 len = (int)STRLEN(p_li->li_tv.vval.v_string);
613 if (len > IOSIZE - 2)
614 {
615 state->linebuf = qf_grow_linebuf(state, len);
616 if (state->linebuf == NULL)
617 return QF_NOMEM;
618 }
619 else
620 {
621 state->linebuf = IObuff;
622 state->linelen = len;
623 }
624
625 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
626
627 state->p_li = p_li->li_next; /* next item */
628 return QF_OK;
629}
630
631/*
632 * Get the next string from state->buf.
633 */
634 static int
635qf_get_next_buf_line(qfstate_T *state)
636{
637 char_u *p_buf = NULL;
638 int len;
639
640 /* Get the next line from the supplied buffer */
641 if (state->buflnum > state->lnumlast)
642 return QF_END_OF_INPUT;
643
644 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
645 state->buflnum += 1;
646
647 len = (int)STRLEN(p_buf);
648 if (len > IOSIZE - 2)
649 {
650 state->linebuf = qf_grow_linebuf(state, len);
651 if (state->linebuf == NULL)
652 return QF_NOMEM;
653 }
654 else
655 {
656 state->linebuf = IObuff;
657 state->linelen = len;
658 }
659 vim_strncpy(state->linebuf, p_buf, state->linelen);
660
661 return QF_OK;
662}
663
664/*
665 * Get the next string from file state->fd.
666 */
667 static int
668qf_get_next_file_line(qfstate_T *state)
669{
670 int discard;
671 int growbuflen;
672
673 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
674 return QF_END_OF_INPUT;
675
676 discard = FALSE;
677 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200678 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200679 {
680 /*
681 * The current line exceeds IObuff, continue reading using
682 * growbuf until EOL or LINE_MAXLEN bytes is read.
683 */
684 if (state->growbuf == NULL)
685 {
686 state->growbufsiz = 2 * (IOSIZE - 1);
687 state->growbuf = alloc(state->growbufsiz);
688 if (state->growbuf == NULL)
689 return QF_NOMEM;
690 }
691
692 /* Copy the read part of the line, excluding null-terminator */
693 memcpy(state->growbuf, IObuff, IOSIZE - 1);
694 growbuflen = state->linelen;
695
696 for (;;)
697 {
698 if (fgets((char *)state->growbuf + growbuflen,
699 state->growbufsiz - growbuflen, state->fd) == NULL)
700 break;
701 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
702 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200703 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200704 break;
705 if (state->growbufsiz == LINE_MAXLEN)
706 {
707 discard = TRUE;
708 break;
709 }
710
711 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
712 ? 2 * state->growbufsiz : LINE_MAXLEN;
713 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
714 if (state->growbuf == NULL)
715 return QF_NOMEM;
716 }
717
718 while (discard)
719 {
720 /*
721 * The current line is longer than LINE_MAXLEN, continue
722 * reading but discard everything until EOL or EOF is
723 * reached.
724 */
725 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
726 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200727 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200728 break;
729 }
730
731 state->linebuf = state->growbuf;
732 state->linelen = growbuflen;
733 }
734 else
735 state->linebuf = IObuff;
736
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100737#ifdef FEAT_MBYTE
738 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200739 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
740 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100741 char_u *line;
742
743 line = string_convert(&state->vc, state->linebuf, &state->linelen);
744 if (line != NULL)
745 {
746 if (state->linelen < IOSIZE)
747 {
748 STRCPY(state->linebuf, line);
749 vim_free(line);
750 }
751 else
752 {
753 vim_free(state->growbuf);
754 state->linebuf = state->growbuf = line;
755 state->growbufsiz = state->linelen < LINE_MAXLEN
756 ? state->linelen : LINE_MAXLEN;
757 }
758 }
759 }
760#endif
761
Bram Moolenaare0d37972016-07-15 22:36:01 +0200762 return QF_OK;
763}
764
765/*
766 * Get the next string from a file/buffer/list/string.
767 */
768 static int
769qf_get_nextline(qfstate_T *state)
770{
771 int status = QF_FAIL;
772
773 if (state->fd == NULL)
774 {
775 if (state->tv != NULL)
776 {
777 if (state->tv->v_type == VAR_STRING)
778 /* Get the next line from the supplied string */
779 status = qf_get_next_str_line(state);
780 else if (state->tv->v_type == VAR_LIST)
781 /* Get the next line from the supplied list */
782 status = qf_get_next_list_line(state);
783 }
784 else
785 /* Get the next line from the supplied buffer */
786 status = qf_get_next_buf_line(state);
787 }
788 else
789 /* Get the next line from the supplied file */
790 status = qf_get_next_file_line(state);
791
792 if (status != QF_OK)
793 return status;
794
795 /* remove newline/CR from the line */
796 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200797 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 state->linebuf[state->linelen - 1] = NUL;
799#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200800 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
801 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200803 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804
805#ifdef FEAT_MBYTE
806 remove_bom(state->linebuf);
807#endif
808
809 return QF_OK;
810}
811
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200812typedef struct {
813 char_u *namebuf;
814 char_u *errmsg;
815 int errmsglen;
816 long lnum;
817 int col;
818 char_u use_viscol;
819 char_u *pattern;
820 int enr;
821 int type;
822 int valid;
823} qffields_T;
824
825/*
826 * Parse a line and get the quickfix fields.
827 * Return the QF_ status.
828 */
829 static int
830qf_parse_line(
831 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200832 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200833 char_u *linebuf,
834 int linelen,
835 efm_T *fmt_first,
836 qffields_T *fields)
837{
838 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200839 char_u *ptr;
840 int len;
841 int i;
842 int idx = 0;
843 char_u *tail = NULL;
844 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200845 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200846
847 /* Always ignore case when looking for a matching error. */
848 regmatch.rm_ic = TRUE;
849
850 /* If there was no %> item start at the first pattern */
851 if (fmt_start == NULL)
852 fmt_ptr = fmt_first;
853 else
854 {
855 fmt_ptr = fmt_start;
856 fmt_start = NULL;
857 }
858
859 /*
860 * Try to match each part of 'errorformat' until we find a complete
861 * match or no match.
862 */
863 fields->valid = TRUE;
864restofline:
865 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
866 {
867 int r;
868
869 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200870 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200871 continue;
872 fields->namebuf[0] = NUL;
873 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200874 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200875 fields->errmsg[0] = NUL;
876 fields->lnum = 0;
877 fields->col = 0;
878 fields->use_viscol = FALSE;
879 fields->enr = -1;
880 fields->type = 0;
881 tail = NULL;
882
883 regmatch.regprog = fmt_ptr->prog;
884 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
885 fmt_ptr->prog = regmatch.regprog;
886 if (r)
887 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200888 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200889 continue;
890 if (vim_strchr((char_u *)"EWI", idx) != NULL)
891 fields->type = idx;
892 else
893 fields->type = 0;
894 /*
895 * Extract error message data from matched line.
896 * We check for an actual submatch, because "\[" and "\]" in
897 * the 'errorformat' may cause the wrong submatch to be used.
898 */
899 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
900 {
901 int c;
902
903 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
904 continue;
905
906 /* Expand ~/file and $HOME/file to full path. */
907 c = *regmatch.endp[i];
908 *regmatch.endp[i] = NUL;
909 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
910 *regmatch.endp[i] = c;
911
912 if (vim_strchr((char_u *)"OPQ", idx) != NULL
913 && mch_getperm(fields->namebuf) == -1)
914 continue;
915 }
916 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
917 {
918 if (regmatch.startp[i] == NULL)
919 continue;
920 fields->enr = (int)atol((char *)regmatch.startp[i]);
921 }
922 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
923 {
924 if (regmatch.startp[i] == NULL)
925 continue;
926 fields->lnum = atol((char *)regmatch.startp[i]);
927 }
928 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
929 {
930 if (regmatch.startp[i] == NULL)
931 continue;
932 fields->col = (int)atol((char *)regmatch.startp[i]);
933 }
934 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
935 {
936 if (regmatch.startp[i] == NULL)
937 continue;
938 fields->type = *regmatch.startp[i];
939 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200940 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200941 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200942 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200943 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200944 /* linelen + null terminator */
945 if ((fields->errmsg = vim_realloc(fields->errmsg,
946 linelen + 1)) == NULL)
947 return QF_NOMEM;
948 fields->errmsglen = linelen + 1;
949 }
950 vim_strncpy(fields->errmsg, linebuf, linelen);
951 }
952 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
953 {
954 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
955 continue;
956 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200957 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200958 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200959 /* len + null terminator */
960 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
961 == NULL)
962 return QF_NOMEM;
963 fields->errmsglen = len + 1;
964 }
965 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
966 }
967 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
968 {
969 if (regmatch.startp[i] == NULL)
970 continue;
971 tail = regmatch.startp[i];
972 }
973 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
974 {
975 char_u *match_ptr;
976
977 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
978 continue;
979 fields->col = 0;
980 for (match_ptr = regmatch.startp[i];
981 match_ptr != regmatch.endp[i]; ++match_ptr)
982 {
983 ++fields->col;
984 if (*match_ptr == TAB)
985 {
986 fields->col += 7;
987 fields->col -= fields->col % 8;
988 }
989 }
990 ++fields->col;
991 fields->use_viscol = TRUE;
992 }
993 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
994 {
995 if (regmatch.startp[i] == NULL)
996 continue;
997 fields->col = (int)atol((char *)regmatch.startp[i]);
998 fields->use_viscol = TRUE;
999 }
1000 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1001 {
1002 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1003 continue;
1004 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1005 if (len > CMDBUFFSIZE - 5)
1006 len = CMDBUFFSIZE - 5;
1007 STRCPY(fields->pattern, "^\\V");
1008 STRNCAT(fields->pattern, regmatch.startp[i], len);
1009 fields->pattern[len + 3] = '\\';
1010 fields->pattern[len + 4] = '$';
1011 fields->pattern[len + 5] = NUL;
1012 }
1013 break;
1014 }
1015 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001016 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001017
1018 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1019 {
1020 if (fmt_ptr != NULL)
1021 {
1022 if (idx == 'D') /* enter directory */
1023 {
1024 if (*fields->namebuf == NUL)
1025 {
1026 EMSG(_("E379: Missing or empty directory name"));
1027 return QF_FAIL;
1028 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001029 qfl->qf_directory =
1030 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1031 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001032 return QF_FAIL;
1033 }
1034 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001035 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001036 }
1037 fields->namebuf[0] = NUL; /* no match found, remove file name */
1038 fields->lnum = 0; /* don't jump to this line */
1039 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001040 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001041 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001042 /* linelen + null terminator */
1043 if ((fields->errmsg = vim_realloc(fields->errmsg,
1044 linelen + 1)) == NULL)
1045 return QF_NOMEM;
1046 fields->errmsglen = linelen + 1;
1047 }
1048 /* copy whole line to error message */
1049 vim_strncpy(fields->errmsg, linebuf, linelen);
1050 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001051 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001052 }
1053 else if (fmt_ptr != NULL)
1054 {
1055 /* honor %> item */
1056 if (fmt_ptr->conthere)
1057 fmt_start = fmt_ptr;
1058
1059 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1060 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001061 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1062 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001063 }
1064 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1065 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001066 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001067 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001068 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001069
Bram Moolenaar9b457942016-10-09 16:10:05 +02001070 if (qfprev == NULL)
1071 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001072 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001073 {
1074 len = (int)STRLEN(qfprev->qf_text);
1075 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1076 == NULL)
1077 return QF_FAIL;
1078 STRCPY(ptr, qfprev->qf_text);
1079 vim_free(qfprev->qf_text);
1080 qfprev->qf_text = ptr;
1081 *(ptr += len) = '\n';
1082 STRCPY(++ptr, fields->errmsg);
1083 }
1084 if (qfprev->qf_nr == -1)
1085 qfprev->qf_nr = fields->enr;
1086 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1087 /* only printable chars allowed */
1088 qfprev->qf_type = fields->type;
1089
1090 if (!qfprev->qf_lnum)
1091 qfprev->qf_lnum = fields->lnum;
1092 if (!qfprev->qf_col)
1093 qfprev->qf_col = fields->col;
1094 qfprev->qf_viscol = fields->use_viscol;
1095 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001096 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1097 qfl->qf_directory,
1098 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001099 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001100 : qfl->qf_currfile != NULL && fields->valid
1101 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001102 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001103 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001104 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001105 line_breakcheck();
1106 return QF_IGNORE_LINE;
1107 }
1108 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1109 {
1110 /* global file names */
1111 fields->valid = FALSE;
1112 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1113 {
1114 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001115 qfl->qf_currfile =
1116 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001117 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001118 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001119 *fields->namebuf = NUL;
1120 if (tail && *tail)
1121 {
1122 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001123 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001124 goto restofline;
1125 }
1126 }
1127 }
1128 if (fmt_ptr->flags == '-') /* generally exclude this line */
1129 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001130 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001131 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001132 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001133 return QF_IGNORE_LINE;
1134 }
1135 }
1136
1137 return QF_OK;
1138}
1139
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001140/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001141 * Read the errorfile "efile" into memory, line by line, building the error
1142 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001143 * Alternative: when "efile" is NULL read errors from buffer "buf".
1144 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001145 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001146 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1147 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001148 * Return -1 for error, number of errors for success.
1149 */
1150 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001151qf_init_ext(
1152 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001153 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001154 char_u *efile,
1155 buf_T *buf,
1156 typval_T *tv,
1157 char_u *errorformat,
1158 int newlist, /* TRUE: start a new error list */
1159 linenr_T lnumfirst, /* first line number to use */
1160 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001161 char_u *qf_title,
1162 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001163{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001164 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001165 qfstate_T state;
1166 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001167#ifdef FEAT_WINDOWS
1168 qfline_T *old_last = NULL;
1169#endif
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001170 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001171 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001173 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001175 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001177 /* Do not used the cached buffer, it may have been wiped out. */
1178 vim_free(qf_last_bufname);
1179 qf_last_bufname = NULL;
1180
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001181 vim_memset(&state, 0, sizeof(state));
1182 vim_memset(&fields, 0, sizeof(fields));
1183#ifdef FEAT_MBYTE
1184 state.vc.vc_type = CONV_NONE;
1185 if (enc != NULL && *enc != NUL)
1186 convert_setup(&state.vc, enc, p_enc);
1187#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001188 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1189 fields.errmsglen = CMDBUFFSIZE + 1;
1190 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1191 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001192 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 goto qf_init_end;
1194
Bram Moolenaare0d37972016-07-15 22:36:01 +02001195 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 {
1197 EMSG2(_(e_openerrf), efile);
1198 goto qf_init_end;
1199 }
1200
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001201 if (newlist || qf_idx == qi->qf_listcount)
1202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001204 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001205 qf_idx = qi->qf_curlist;
1206 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001207 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001208 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001209 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001210 adding = TRUE;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001211#ifdef FEAT_WINDOWS
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001212 if (qi->qf_lists[qf_idx].qf_count > 0)
1213 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001214#endif
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001217 qfl = &qi->qf_lists[qf_idx];
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001220 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001221 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 else
1223 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001225 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001226 * If the errorformat didn't change between calls, then reuse the
1227 * previously parsed values.
1228 */
1229 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1230 {
1231 /* free the previously parsed data */
1232 vim_free(last_efm);
1233 last_efm = NULL;
1234 free_efm_list(&fmt_first);
1235
1236 /* parse the current 'efm' */
1237 fmt_first = parse_efm_option(efm);
1238 if (fmt_first != NULL)
1239 last_efm = vim_strsave(efm);
1240 }
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /*
1246 * got_int is reset here, because it was probably set when killing the
1247 * ":make" command, but we still want to read the errorfile then.
1248 */
1249 got_int = FALSE;
1250
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001251 if (tv != NULL)
1252 {
1253 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001254 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001255 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001256 state.p_li = tv->vval.v_list->lv_first;
1257 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001258 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001259 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001260 state.buflnum = lnumfirst;
1261 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /*
1264 * Read the lines in the error file one by one.
1265 * Try to recognize one of the error formats in each line.
1266 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001267 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001269 /* Get the next line from a file/buffer/list/string */
1270 status = qf_get_nextline(&state);
1271 if (status == QF_NOMEM) /* memory alloc failure */
1272 goto qf_init_end;
1273 if (status == QF_END_OF_INPUT) /* end of input */
1274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001276 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1277 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001278 if (status == QF_FAIL)
1279 goto error2;
1280 if (status == QF_NOMEM)
1281 goto qf_init_end;
1282 if (status == QF_IGNORE_LINE)
1283 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001285 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001286 qf_idx,
1287 qfl->qf_directory,
1288 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001289 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001290 : ((qfl->qf_currfile != NULL && fields.valid)
1291 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001292 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001293 fields.errmsg,
1294 fields.lnum,
1295 fields.col,
1296 fields.use_viscol,
1297 fields.pattern,
1298 fields.enr,
1299 fields.type,
1300 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 goto error2;
1302 line_breakcheck();
1303 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001304 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001306 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001308 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001309 qfl->qf_ptr = qfl->qf_start;
1310 qfl->qf_index = 1;
1311 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 else
1314 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001315 qfl->qf_nonevalid = FALSE;
1316 if (qfl->qf_ptr == NULL)
1317 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001319 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001320 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001321 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 EMSG(_(e_readerrf));
1324error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001325 if (!adding)
1326 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001327 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001328 qf_free(qi, qi->qf_curlist);
1329 qi->qf_listcount--;
1330 if (qi->qf_curlist > 0)
1331 --qi->qf_curlist;
1332 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001333qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001334 if (state.fd != NULL)
1335 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001336 vim_free(fields.namebuf);
1337 vim_free(fields.errmsg);
1338 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001339 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341#ifdef FEAT_WINDOWS
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001342 if (qf_idx == qi->qf_curlist)
1343 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001345#ifdef FEAT_MBYTE
1346 if (state.vc.vc_type != CONV_NONE)
1347 convert_setup(&state.vc, NULL, NULL);
1348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
1350 return retval;
1351}
1352
Bram Moolenaarfb604092014-07-23 15:55:00 +02001353 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001354qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001355{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001356 vim_free(qi->qf_lists[qf_idx].qf_title);
1357 qi->qf_lists[qf_idx].qf_title = NULL;
1358
Bram Moolenaarfb604092014-07-23 15:55:00 +02001359 if (title != NULL)
1360 {
1361 char_u *p = alloc((int)STRLEN(title) + 2);
1362
Bram Moolenaara3921f42017-06-04 15:30:34 +02001363 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001364 if (p != NULL)
1365 sprintf((char *)p, ":%s", (char *)title);
1366 }
1367}
1368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001370 * Prepare for adding a new quickfix list. If the current list is in the
1371 * middle of the stack, then all the following lists are freed and then
1372 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 */
1374 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001375qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 int i;
1378
1379 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001380 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 * the current entry. This makes it possible to browse in a tree-like
1382 * way with ":grep'.
1383 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001384 while (qi->qf_listcount > qi->qf_curlist + 1)
1385 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
1387 /*
1388 * When the stack is full, remove to oldest entry
1389 * Otherwise, add a new entry.
1390 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001391 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001393 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001395 qi->qf_lists[i - 1] = qi->qf_lists[i];
1396 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001399 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001400 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001401 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402}
1403
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001404/*
1405 * Free a location list
1406 */
1407 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001408ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001409{
1410 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001411 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001412
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001413 qi = *pqi;
1414 if (qi == NULL)
1415 return;
1416 *pqi = NULL; /* Remove reference to this list */
1417
1418 qi->qf_refcount--;
1419 if (qi->qf_refcount < 1)
1420 {
1421 /* No references to this location list */
1422 for (i = 0; i < qi->qf_listcount; ++i)
1423 qf_free(qi, i);
1424 vim_free(qi);
1425 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001426}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001427
1428 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001429qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001430{
1431 int i;
1432 qf_info_T *qi = &ql_info;
1433
1434 if (wp != NULL)
1435 {
1436 /* location list */
1437 ll_free_all(&wp->w_llist);
1438 ll_free_all(&wp->w_llist_ref);
1439 }
1440 else
1441 /* quickfix list */
1442 for (i = 0; i < qi->qf_listcount; ++i)
1443 qf_free(qi, i);
1444}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001445
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446/*
1447 * Add an entry to the end of the list of errors.
1448 * Returns OK or FAIL.
1449 */
1450 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001451qf_add_entry(
1452 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001453 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001454 char_u *dir, /* optional directory name */
1455 char_u *fname, /* file name or NULL */
1456 int bufnum, /* buffer number or zero */
1457 char_u *mesg, /* message */
1458 long lnum, /* line number */
1459 int col, /* column */
1460 int vis_col, /* using visual column */
1461 char_u *pattern, /* search pattern */
1462 int nr, /* error number */
1463 int type, /* type character */
1464 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001466 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001467 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001469 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001471 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001472 {
1473 buf_T *buf = buflist_findnr(bufnum);
1474
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001475 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001476 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001477 buf->b_has_qf_entry |=
1478 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001479 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001480 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001481 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1483 {
1484 vim_free(qfp);
1485 return FAIL;
1486 }
1487 qfp->qf_lnum = lnum;
1488 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001489 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001490 if (pattern == NULL || *pattern == NUL)
1491 qfp->qf_pattern = NULL;
1492 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1493 {
1494 vim_free(qfp->qf_text);
1495 vim_free(qfp);
1496 return FAIL;
1497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 qfp->qf_nr = nr;
1499 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1500 type = 0;
1501 qfp->qf_type = type;
1502 qfp->qf_valid = valid;
1503
Bram Moolenaara3921f42017-06-04 15:30:34 +02001504 lastp = &qi->qf_lists[qf_idx].qf_last;
1505 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001506 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001508 qi->qf_lists[qf_idx].qf_start = qfp;
1509 qi->qf_lists[qf_idx].qf_ptr = qfp;
1510 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001511 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 else
1514 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001515 qfp->qf_prev = *lastp;
1516 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001518 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001520 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001521 ++qi->qf_lists[qf_idx].qf_count;
1522 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001523 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001525 qi->qf_lists[qf_idx].qf_index =
1526 qi->qf_lists[qf_idx].qf_count;
1527 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
1529
1530 return OK;
1531}
1532
1533/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001534 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001535 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001536 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001537ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001538{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001539 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001540
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001541 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1542 if (qi != NULL)
1543 {
1544 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1545 qi->qf_refcount++;
1546 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001547
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001548 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001549}
1550
1551/*
1552 * Return the location list for window 'wp'.
1553 * If not present, allocate a location list
1554 */
1555 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001556ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001557{
1558 if (IS_LL_WINDOW(wp))
1559 /* For a location list window, use the referenced location list */
1560 return wp->w_llist_ref;
1561
1562 /*
1563 * For a non-location list window, w_llist_ref should not point to a
1564 * location list.
1565 */
1566 ll_free_all(&wp->w_llist_ref);
1567
1568 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001569 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001570 return wp->w_llist;
1571}
1572
1573/*
1574 * Copy the location list from window "from" to window "to".
1575 */
1576 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001577copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001578{
1579 qf_info_T *qi;
1580 int idx;
1581 int i;
1582
1583 /*
1584 * When copying from a location list window, copy the referenced
1585 * location list. For other windows, copy the location list for
1586 * that window.
1587 */
1588 if (IS_LL_WINDOW(from))
1589 qi = from->w_llist_ref;
1590 else
1591 qi = from->w_llist;
1592
1593 if (qi == NULL) /* no location list to copy */
1594 return;
1595
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001596 /* allocate a new location list */
1597 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001598 return;
1599
1600 to->w_llist->qf_listcount = qi->qf_listcount;
1601
1602 /* Copy the location lists one at a time */
1603 for (idx = 0; idx < qi->qf_listcount; idx++)
1604 {
1605 qf_list_T *from_qfl;
1606 qf_list_T *to_qfl;
1607
1608 to->w_llist->qf_curlist = idx;
1609
1610 from_qfl = &qi->qf_lists[idx];
1611 to_qfl = &to->w_llist->qf_lists[idx];
1612
1613 /* Some of the fields are populated by qf_add_entry() */
1614 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1615 to_qfl->qf_count = 0;
1616 to_qfl->qf_index = 0;
1617 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001618 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001619 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001620 if (from_qfl->qf_title != NULL)
1621 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1622 else
1623 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001624 if (from_qfl->qf_ctx != NULL)
1625 {
1626 to_qfl->qf_ctx = alloc_tv();
1627 if (to_qfl->qf_ctx != NULL)
1628 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1629 }
1630 else
1631 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001632
1633 if (from_qfl->qf_count)
1634 {
1635 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001636 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001637
1638 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001639 for (i = 0, from_qfp = from_qfl->qf_start;
1640 i < from_qfl->qf_count && from_qfp != NULL;
1641 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001642 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001643 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001644 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001645 NULL,
1646 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001647 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001648 from_qfp->qf_text,
1649 from_qfp->qf_lnum,
1650 from_qfp->qf_col,
1651 from_qfp->qf_viscol,
1652 from_qfp->qf_pattern,
1653 from_qfp->qf_nr,
1654 0,
1655 from_qfp->qf_valid) == FAIL)
1656 {
1657 qf_free_all(to);
1658 return;
1659 }
1660 /*
1661 * qf_add_entry() will not set the qf_num field, as the
1662 * directory and file names are not supplied. So the qf_fnum
1663 * field is copied here.
1664 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001665 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001666 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1667 prevp->qf_type = from_qfp->qf_type; /* error type */
1668 if (from_qfl->qf_ptr == from_qfp)
1669 to_qfl->qf_ptr = prevp; /* current location */
1670 }
1671 }
1672
1673 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1674
1675 /* When no valid entries are present in the list, qf_ptr points to
1676 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001677 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001678 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001679 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001680 to_qfl->qf_index = 1;
1681 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001682 }
1683
1684 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1685}
1686
1687/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001688 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001689 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 */
1691 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001692qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
Bram Moolenaar82404332016-07-10 17:00:38 +02001694 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001695 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001696 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001697
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 if (fname == NULL || *fname == NUL) /* no file name */
1699 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaare60acc12011-05-10 16:41:25 +02001701#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001702 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001703#endif
1704#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001705 if (directory != NULL)
1706 slash_adjust(directory);
1707 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001708#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001709 if (directory != NULL && !vim_isAbsName(fname)
1710 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1711 {
1712 /*
1713 * Here we check if the file really exists.
1714 * This should normally be true, but if make works without
1715 * "leaving directory"-messages we might have missed a
1716 * directory change.
1717 */
1718 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001721 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001722 if (directory)
1723 ptr = concat_fnames(directory, fname, TRUE);
1724 else
1725 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001727 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001728 bufname = ptr;
1729 }
1730 else
1731 bufname = fname;
1732
1733 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001734 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001735 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001736 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001737 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001739 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001740 {
1741 vim_free(qf_last_bufname);
1742 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1743 if (bufname == ptr)
1744 qf_last_bufname = bufname;
1745 else
1746 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001747 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001748 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001749 if (buf == NULL)
1750 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001751
Bram Moolenaarc1542742016-07-20 21:44:37 +02001752 buf->b_has_qf_entry =
1753 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001754 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755}
1756
1757/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001758 * Push dirbuf onto the directory stack and return pointer to actual dir or
1759 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 */
1761 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001762qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 struct dir_stack_T *ds_new;
1765 struct dir_stack_T *ds_ptr;
1766
1767 /* allocate new stack element and hook it in */
1768 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1769 if (ds_new == NULL)
1770 return NULL;
1771
1772 ds_new->next = *stackptr;
1773 *stackptr = ds_new;
1774
1775 /* store directory on the stack */
1776 if (vim_isAbsName(dirbuf)
1777 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001778 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 (*stackptr)->dirname = vim_strsave(dirbuf);
1780 else
1781 {
1782 /* Okay we don't have an absolute path.
1783 * dirbuf must be a subdir of one of the directories on the stack.
1784 * Let's search...
1785 */
1786 ds_new = (*stackptr)->next;
1787 (*stackptr)->dirname = NULL;
1788 while (ds_new)
1789 {
1790 vim_free((*stackptr)->dirname);
1791 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1792 TRUE);
1793 if (mch_isdir((*stackptr)->dirname) == TRUE)
1794 break;
1795
1796 ds_new = ds_new->next;
1797 }
1798
1799 /* clean up all dirs we already left */
1800 while ((*stackptr)->next != ds_new)
1801 {
1802 ds_ptr = (*stackptr)->next;
1803 (*stackptr)->next = (*stackptr)->next->next;
1804 vim_free(ds_ptr->dirname);
1805 vim_free(ds_ptr);
1806 }
1807
1808 /* Nothing found -> it must be on top level */
1809 if (ds_new == NULL)
1810 {
1811 vim_free((*stackptr)->dirname);
1812 (*stackptr)->dirname = vim_strsave(dirbuf);
1813 }
1814 }
1815
1816 if ((*stackptr)->dirname != NULL)
1817 return (*stackptr)->dirname;
1818 else
1819 {
1820 ds_ptr = *stackptr;
1821 *stackptr = (*stackptr)->next;
1822 vim_free(ds_ptr);
1823 return NULL;
1824 }
1825}
1826
1827
1828/*
1829 * pop dirbuf from the directory stack and return previous directory or NULL if
1830 * stack is empty
1831 */
1832 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001833qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 struct dir_stack_T *ds_ptr;
1836
1837 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1838 * What to do if it isn't? */
1839
1840 /* pop top element and free it */
1841 if (*stackptr != NULL)
1842 {
1843 ds_ptr = *stackptr;
1844 *stackptr = (*stackptr)->next;
1845 vim_free(ds_ptr->dirname);
1846 vim_free(ds_ptr);
1847 }
1848
1849 /* return NEW top element as current dir or NULL if stack is empty*/
1850 return *stackptr ? (*stackptr)->dirname : NULL;
1851}
1852
1853/*
1854 * clean up directory stack
1855 */
1856 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001857qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
1859 struct dir_stack_T *ds_ptr;
1860
1861 while ((ds_ptr = *stackptr) != NULL)
1862 {
1863 *stackptr = (*stackptr)->next;
1864 vim_free(ds_ptr->dirname);
1865 vim_free(ds_ptr);
1866 }
1867}
1868
1869/*
1870 * Check in which directory of the directory stack the given file can be
1871 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001872 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 * Cleans up intermediate directory entries.
1874 *
1875 * TODO: How to solve the following problem?
1876 * If we have the this directory tree:
1877 * ./
1878 * ./aa
1879 * ./aa/bb
1880 * ./bb
1881 * ./bb/x.c
1882 * and make says:
1883 * making all in aa
1884 * making all in bb
1885 * x.c:9: Error
1886 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1887 * qf_guess_filepath will return NULL.
1888 */
1889 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001890qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 struct dir_stack_T *ds_ptr;
1893 struct dir_stack_T *ds_tmp;
1894 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001895 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001898 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 return NULL;
1900
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001901 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 fullname = NULL;
1903 while (ds_ptr)
1904 {
1905 vim_free(fullname);
1906 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1907
1908 /* If concat_fnames failed, just go on. The worst thing that can happen
1909 * is that we delete the entire stack.
1910 */
1911 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1912 break;
1913
1914 ds_ptr = ds_ptr->next;
1915 }
1916
1917 vim_free(fullname);
1918
1919 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001920 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001922 ds_tmp = qfl->qf_dir_stack->next;
1923 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 vim_free(ds_tmp->dirname);
1925 vim_free(ds_tmp);
1926 }
1927
1928 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929}
1930
1931/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001932 * When loading a file from the quickfix, the auto commands may modify it.
1933 * This may invalidate the current quickfix entry. This function checks
1934 * whether a entry is still present in the quickfix.
1935 * Similar to location list.
1936 */
1937 static int
1938is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1939{
1940 qf_list_T *qfl;
1941 qfline_T *qfp;
1942 int i;
1943
1944 qfl = &qi->qf_lists[qi->qf_curlist];
1945
1946 /* Search for the entry in the current list */
1947 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1948 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001949 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001950 break;
1951
1952 if (i == qfl->qf_count) /* Entry is not found */
1953 return FALSE;
1954
1955 return TRUE;
1956}
1957
1958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 * jump to a quickfix line
1960 * if dir == FORWARD go "errornr" valid entries forward
1961 * if dir == BACKWARD go "errornr" valid entries backward
1962 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1963 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1964 * else if "errornr" is zero, redisplay the same line
1965 * else go to entry "errornr"
1966 */
1967 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001968qf_jump(
1969 qf_info_T *qi,
1970 int dir,
1971 int errornr,
1972 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001974 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001975 qfline_T *qf_ptr;
1976 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 int qf_index;
1978 int old_qf_fnum;
1979 int old_qf_index;
1980 int prev_index;
1981 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1982 char_u *err = e_no_more_items;
1983 linenr_T i;
1984 buf_T *old_curbuf;
1985 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 colnr_T screen_col;
1987 colnr_T char_col;
1988 char_u *line;
1989#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001990 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001991 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 int opened_window = FALSE;
1993 win_T *win;
1994 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001995 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001997 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 int print_message = TRUE;
1999 int len;
2000#ifdef FEAT_FOLDING
2001 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2002#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002003 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002004 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002006 if (qi == NULL)
2007 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002008
2009 if (qi->qf_curlist >= qi->qf_listcount
2010 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
2012 EMSG(_(e_quickfix));
2013 return;
2014 }
2015
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002016 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002018 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 old_qf_index = qf_index;
2020 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
2021 {
2022 while (errornr--)
2023 {
2024 old_qf_ptr = qf_ptr;
2025 prev_index = qf_index;
2026 old_qf_fnum = qf_ptr->qf_fnum;
2027 do
2028 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002029 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 || qf_ptr->qf_next == NULL)
2031 {
2032 qf_ptr = old_qf_ptr;
2033 qf_index = prev_index;
2034 if (err != NULL)
2035 {
2036 EMSG(_(err));
2037 goto theend;
2038 }
2039 errornr = 0;
2040 break;
2041 }
2042 ++qf_index;
2043 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002044 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2045 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2047 err = NULL;
2048 }
2049 }
2050 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
2051 {
2052 while (errornr--)
2053 {
2054 old_qf_ptr = qf_ptr;
2055 prev_index = qf_index;
2056 old_qf_fnum = qf_ptr->qf_fnum;
2057 do
2058 {
2059 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
2060 {
2061 qf_ptr = old_qf_ptr;
2062 qf_index = prev_index;
2063 if (err != NULL)
2064 {
2065 EMSG(_(err));
2066 goto theend;
2067 }
2068 errornr = 0;
2069 break;
2070 }
2071 --qf_index;
2072 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002073 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2074 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2076 err = NULL;
2077 }
2078 }
2079 else if (errornr != 0) /* go to specified number */
2080 {
2081 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2082 {
2083 --qf_index;
2084 qf_ptr = qf_ptr->qf_prev;
2085 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002086 while (errornr > qf_index && qf_index <
2087 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 && qf_ptr->qf_next != NULL)
2089 {
2090 ++qf_index;
2091 qf_ptr = qf_ptr->qf_next;
2092 }
2093 }
2094
2095#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002096 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2097 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 /* No need to print the error message if it's visible in the error
2099 * window */
2100 print_message = FALSE;
2101
2102 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002103 * For ":helpgrep" find a help window or open one.
2104 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002105 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002106 {
2107 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002108
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002109 if (cmdmod.tab != 0)
2110 wp = NULL;
2111 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002112 FOR_ALL_WINDOWS(wp)
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002113 if (bt_help(wp->w_buffer))
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002114 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002115 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2116 win_enter(wp, TRUE);
2117 else
2118 {
2119 /*
2120 * Split off help window; put it at far top if no position
2121 * specified, the current window is vertically split and narrow.
2122 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002123 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002124 if (cmdmod.split == 0 && curwin->w_width != Columns
2125 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002126 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002127 if (qi != &ql_info)
2128 flags |= WSP_NEWLOC; /* don't copy the location list */
2129
2130 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002131 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002132 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002133
2134 if (curwin->w_height < p_hh)
2135 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002136
2137 if (qi != &ql_info) /* not a quickfix list */
2138 {
2139 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002140 curwin->w_llist = qi;
2141 qi->qf_refcount++;
2142 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002143 }
2144
2145 if (!p_im)
2146 restart_edit = 0; /* don't want insert mode in help file */
2147 }
2148
2149 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 * If currently in the quickfix window, find another window to show the
2151 * file in.
2152 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002153 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002155 win_T *usable_win_ptr = NULL;
2156
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 /*
2158 * If there is no file specified, we don't know where to go.
2159 * But do advance, otherwise ":cn" gets stuck.
2160 */
2161 if (qf_ptr->qf_fnum == 0)
2162 goto theend;
2163
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002164 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002165
2166 ll_ref = curwin->w_llist_ref;
2167 if (ll_ref != NULL)
2168 {
2169 /* Find a window using the same location list that is not a
2170 * quickfix window. */
2171 FOR_ALL_WINDOWS(usable_win_ptr)
2172 if (usable_win_ptr->w_llist == ll_ref
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02002173 && !bt_quickfix(usable_win_ptr->w_buffer))
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002174 {
2175 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002176 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002177 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002178 }
2179
2180 if (!usable_win)
2181 {
2182 /* Locate a window showing a normal buffer */
2183 FOR_ALL_WINDOWS(win)
2184 if (win->w_buffer->b_p_bt[0] == NUL)
2185 {
2186 usable_win = 1;
2187 break;
2188 }
2189 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002192 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002193 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002195 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002196 {
2197 tabpage_T *tp;
2198 win_T *wp;
2199
2200 FOR_ALL_TAB_WINDOWS(tp, wp)
2201 {
2202 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2203 {
2204 goto_tabpage_win(tp, wp);
2205 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002206 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002207 }
2208 }
2209 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002210win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002211
2212 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002213 * If there is only one window and it is the quickfix window, create a
2214 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002215 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002216 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002218 flags = WSP_ABOVE;
2219 if (ll_ref != NULL)
2220 flags |= WSP_NEWLOC;
2221 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 goto failed; /* not enough room for window */
2223 opened_window = TRUE; /* close it when fail */
2224 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002225 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002226 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 if (ll_ref != NULL)
2228 {
2229 /* The new window should use the location list from the
2230 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002231 curwin->w_llist = ll_ref;
2232 ll_ref->qf_refcount++;
2233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
2235 else
2236 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002237 if (curwin->w_llist_ref != NULL)
2238 {
2239 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002240 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002241 if (win == NULL)
2242 {
2243 /* Find the window showing the selected file */
2244 FOR_ALL_WINDOWS(win)
2245 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2246 break;
2247 if (win == NULL)
2248 {
2249 /* Find a previous usable window */
2250 win = curwin;
2251 do
2252 {
2253 if (win->w_buffer->b_p_bt[0] == NUL)
2254 break;
2255 if (win->w_prev == NULL)
2256 win = lastwin; /* wrap around the top */
2257 else
2258 win = win->w_prev; /* go to previous window */
2259 } while (win != curwin);
2260 }
2261 }
2262 win_goto(win);
2263
2264 /* If the location list for the window is not set, then set it
2265 * to the location list from the location window */
2266 if (win->w_llist == NULL)
2267 {
2268 win->w_llist = ll_ref;
2269 ll_ref->qf_refcount++;
2270 }
2271 }
2272 else
2273 {
2274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 /*
2276 * Try to find a window that shows the right buffer.
2277 * Default to the window just above the quickfix buffer.
2278 */
2279 win = curwin;
2280 altwin = NULL;
2281 for (;;)
2282 {
2283 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2284 break;
2285 if (win->w_prev == NULL)
2286 win = lastwin; /* wrap around the top */
2287 else
2288 win = win->w_prev; /* go to previous window */
2289
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002290 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 /* Didn't find it, go to the window before the quickfix
2293 * window. */
2294 if (altwin != NULL)
2295 win = altwin;
2296 else if (curwin->w_prev != NULL)
2297 win = curwin->w_prev;
2298 else
2299 win = curwin->w_next;
2300 break;
2301 }
2302
2303 /* Remember a usable window. */
2304 if (altwin == NULL && !win->w_p_pvw
2305 && win->w_buffer->b_p_bt[0] == NUL)
2306 altwin = win;
2307 }
2308
2309 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312 }
2313#endif
2314
2315 /*
2316 * If there is a file name,
2317 * read the wanted file if needed, and check autowrite etc.
2318 */
2319 old_curbuf = curbuf;
2320 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002321
2322 if (qf_ptr->qf_fnum != 0)
2323 {
2324 if (qf_ptr->qf_type == 1)
2325 {
2326 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2327 * set b_p_ro flag). */
2328 if (!can_abandon(curbuf, forceit))
2329 {
2330 EMSG(_(e_nowrtmsg));
2331 ok = FALSE;
2332 }
2333 else
2334 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002335 ECMD_HIDE + ECMD_SET_HELP,
2336 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002337 }
2338 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002339 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002340 int old_qf_curlist = qi->qf_curlist;
2341 int is_abort = FALSE;
2342
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002343 ok = buflist_getfile(qf_ptr->qf_fnum,
2344 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002345 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002346 {
2347 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002348 is_abort = TRUE;
2349 opened_window = FALSE;
2350 }
2351 else if (old_qf_curlist != qi->qf_curlist
2352 || !is_qf_entry_present(qi, qf_ptr))
2353 {
2354 if (qi == &ql_info)
2355 EMSG(_("E925: Current quickfix was changed"));
2356 else
2357 EMSG(_("E926: Current location list was changed"));
2358 is_abort = TRUE;
2359 }
2360
2361 if (is_abort)
2362 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002363 ok = FALSE;
2364 qi = NULL;
2365 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002366 }
2367 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002368 }
2369
2370 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 /* When not switched to another buffer, still need to set pc mark */
2373 if (curbuf == old_curbuf)
2374 setpcmark();
2375
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002376 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002378 /*
2379 * Go to line with error, unless qf_lnum is 0.
2380 */
2381 i = qf_ptr->qf_lnum;
2382 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002384 if (i > curbuf->b_ml.ml_line_count)
2385 i = curbuf->b_ml.ml_line_count;
2386 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002388 if (qf_ptr->qf_col > 0)
2389 {
2390 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002391#ifdef FEAT_VIRTUALEDIT
2392 curwin->w_cursor.coladd = 0;
2393#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002394 if (qf_ptr->qf_viscol == TRUE)
2395 {
2396 /*
2397 * Check each character from the beginning of the error
2398 * line up to the error column. For each tab character
2399 * found, reduce the error column value by the length of
2400 * a tab character.
2401 */
2402 line = ml_get_curline();
2403 screen_col = 0;
2404 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2405 {
2406 if (*line == NUL)
2407 break;
2408 if (*line++ == '\t')
2409 {
2410 curwin->w_cursor.col -= 7 - (screen_col % 8);
2411 screen_col += 8 - (screen_col % 8);
2412 }
2413 else
2414 ++screen_col;
2415 }
2416 }
2417 check_cursor();
2418 }
2419 else
2420 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002423 {
2424 pos_T save_cursor;
2425
2426 /* Move the cursor to the first line in the buffer */
2427 save_cursor = curwin->w_cursor;
2428 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002429 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002430 SEARCH_KEEP, NULL, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002431 curwin->w_cursor = save_cursor;
2432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434#ifdef FEAT_FOLDING
2435 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2436 foldOpenCursor();
2437#endif
2438 if (print_message)
2439 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002440 /* Update the screen before showing the message, unless the screen
2441 * scrolled up. */
2442 if (!msg_scrolled)
2443 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002445 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2447 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2448 /* Add the message, skipping leading whitespace and newlines. */
2449 len = (int)STRLEN(IObuff);
2450 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2451
2452 /* Output the message. Overwrite to avoid scrolling when the 'O'
2453 * flag is present in 'shortmess'; But when not jumping, print the
2454 * whole message. */
2455 i = msg_scroll;
2456 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2457 msg_scroll = TRUE;
2458 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2459 msg_scroll = FALSE;
2460 msg_attr_keep(IObuff, 0, TRUE);
2461 msg_scroll = i;
2462 }
2463 }
2464 else
2465 {
2466#ifdef FEAT_WINDOWS
2467 if (opened_window)
2468 win_close(curwin, TRUE); /* Close opened window */
2469#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002470 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 /*
2473 * Couldn't open file, so put index back where it was. This could
2474 * happen if the file was readonly and we changed something.
2475 */
2476#ifdef FEAT_WINDOWS
2477failed:
2478#endif
2479 qf_ptr = old_qf_ptr;
2480 qf_index = old_qf_index;
2481 }
2482 }
2483theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002484 if (qi != NULL)
2485 {
2486 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2487 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489#ifdef FEAT_WINDOWS
2490 if (p_swb != old_swb && opened_window)
2491 {
2492 /* Restore old 'switchbuf' value, but not when an autocommand or
2493 * modeline has changed the value. */
2494 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002497 swb_flags = old_swb_flags;
2498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 else
2500 free_string_option(old_swb);
2501 }
2502#endif
2503}
2504
2505/*
2506 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002507 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
2509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002510qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002512 buf_T *buf;
2513 char_u *fname;
2514 qfline_T *qfp;
2515 int i;
2516 int idx1 = 1;
2517 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002518 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002519 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002520 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002522 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002524 if (eap->cmdidx == CMD_llist)
2525 {
2526 qi = GET_LOC_LIST(curwin);
2527 if (qi == NULL)
2528 {
2529 EMSG(_(e_loclist));
2530 return;
2531 }
2532 }
2533
2534 if (qi->qf_curlist >= qi->qf_listcount
2535 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
2537 EMSG(_(e_quickfix));
2538 return;
2539 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002540 if (*arg == '+')
2541 {
2542 ++arg;
2543 plus = TRUE;
2544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2546 {
2547 EMSG(_(e_trailing));
2548 return;
2549 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002550 if (plus)
2551 {
2552 i = qi->qf_lists[qi->qf_curlist].qf_index;
2553 idx2 = i + idx1;
2554 idx1 = i;
2555 }
2556 else
2557 {
2558 i = qi->qf_lists[qi->qf_curlist].qf_count;
2559 if (idx1 < 0)
2560 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2561 if (idx2 < 0)
2562 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002565 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002567 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2568 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
2570 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2571 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002572 msg_putchar('\n');
2573 if (got_int)
2574 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002575
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002576 fname = NULL;
2577 if (qfp->qf_fnum != 0
2578 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2579 {
2580 fname = buf->b_fname;
2581 if (qfp->qf_type == 1) /* :helpgrep */
2582 fname = gettail(fname);
2583 }
2584 if (fname == NULL)
2585 sprintf((char *)IObuff, "%2d", i);
2586 else
2587 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2588 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002589 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar21020352017-06-13 17:21:04 +02002590 ? HL_ATTR(HLF_QFL) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002591 if (qfp->qf_lnum == 0)
2592 IObuff[0] = NUL;
2593 else if (qfp->qf_col == 0)
2594 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2595 else
2596 sprintf((char *)IObuff, ":%ld col %d",
2597 qfp->qf_lnum, qfp->qf_col);
2598 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2599 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002600 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002601 if (qfp->qf_pattern != NULL)
2602 {
2603 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2604 STRCAT(IObuff, ":");
2605 msg_puts(IObuff);
2606 }
2607 msg_puts((char_u *)" ");
2608
2609 /* Remove newlines and leading whitespace from the text. For an
2610 * unrecognized line keep the indent, the compiler may mark a word
2611 * with ^^^^. */
2612 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2614 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002615 msg_prt_line(IObuff, FALSE);
2616 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002618
2619 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002620 if (qfp == NULL)
2621 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002622 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 ui_breakcheck();
2624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625}
2626
2627/*
2628 * Remove newlines and leading whitespace from an error message.
2629 * Put the result in "buf[bufsize]".
2630 */
2631 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002632qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 int i;
2635 char_u *p = text;
2636
2637 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2638 {
2639 if (*p == '\n')
2640 {
2641 buf[i] = ' ';
2642 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002643 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 break;
2645 }
2646 else
2647 buf[i] = *p++;
2648 }
2649 buf[i] = NUL;
2650}
2651
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002652 static void
2653qf_msg(qf_info_T *qi, int which, char *lead)
2654{
2655 char *title = (char *)qi->qf_lists[which].qf_title;
2656 int count = qi->qf_lists[which].qf_count;
2657 char_u buf[IOSIZE];
2658
2659 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2660 lead,
2661 which + 1,
2662 qi->qf_listcount,
2663 count);
2664
2665 if (title != NULL)
2666 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002667 size_t len = STRLEN(buf);
2668
2669 if (len < 34)
2670 {
2671 vim_memset(buf + len, ' ', 34 - len);
2672 buf[34] = NUL;
2673 }
2674 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002675 }
2676 trunc_string(buf, buf, Columns - 1, IOSIZE);
2677 msg(buf);
2678}
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680/*
2681 * ":colder [count]": Up in the quickfix stack.
2682 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002683 * ":lolder [count]": Up in the location list stack.
2684 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 */
2686 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002687qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002689 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 int count;
2691
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002692 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2693 {
2694 qi = GET_LOC_LIST(curwin);
2695 if (qi == NULL)
2696 {
2697 EMSG(_(e_loclist));
2698 return;
2699 }
2700 }
2701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (eap->addr_count != 0)
2703 count = eap->line2;
2704 else
2705 count = 1;
2706 while (count--)
2707 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002708 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002710 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
2712 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002713 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002715 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
2717 else
2718 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002719 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
2721 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002722 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002724 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 }
2726 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002727 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002729 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730#endif
2731}
2732
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002733 void
2734qf_history(exarg_T *eap)
2735{
2736 qf_info_T *qi = &ql_info;
2737 int i;
2738
2739 if (eap->cmdidx == CMD_lhistory)
2740 qi = GET_LOC_LIST(curwin);
2741 if (qi == NULL || (qi->qf_listcount == 0
2742 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2743 MSG(_("No entries"));
2744 else
2745 for (i = 0; i < qi->qf_listcount; ++i)
2746 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2747}
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002750 * Free all the entries in the error list "idx". Note that other information
2751 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
2753 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002754qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002756 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002757 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002758 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002759 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002761 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002763 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002764 qfpnext = qfp->qf_next;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002765 if (qfl->qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002766 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002767 vim_free(qfp->qf_text);
2768 stop = (qfp == qfpnext);
2769 vim_free(qfp->qf_pattern);
2770 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002771 if (stop)
2772 /* Somehow qf_count may have an incorrect value, set it to 1
2773 * to avoid crashing when it's wrong.
2774 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002775 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002776 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002777 qfl->qf_start = qfpnext;
2778 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002780
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002781 qfl->qf_index = 0;
2782 qfl->qf_start = NULL;
2783 qfl->qf_last = NULL;
2784 qfl->qf_ptr = NULL;
2785 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002786
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002787 qf_clean_dir_stack(&qfl->qf_dir_stack);
2788 qfl->qf_directory = NULL;
2789 qf_clean_dir_stack(&qfl->qf_file_stack);
2790 qfl->qf_currfile = NULL;
2791 qfl->qf_multiline = FALSE;
2792 qfl->qf_multiignore = FALSE;
2793 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794}
2795
2796/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002797 * Free error list "idx". Frees all the entries in the quickfix list,
2798 * associated context information and the title.
2799 */
2800 static void
2801qf_free(qf_info_T *qi, int idx)
2802{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002803 qf_list_T *qfl = &qi->qf_lists[idx];
2804
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002805 qf_free_items(qi, idx);
2806
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002807 vim_free(qfl->qf_title);
2808 qfl->qf_title = NULL;
2809 free_tv(qfl->qf_ctx);
2810 qfl->qf_ctx = NULL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002811}
2812
2813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 * qf_mark_adjust: adjust marks
2815 */
2816 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002817qf_mark_adjust(
2818 win_T *wp,
2819 linenr_T line1,
2820 linenr_T line2,
2821 long amount,
2822 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002824 int i;
2825 qfline_T *qfp;
2826 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002827 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002828 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002829 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
Bram Moolenaarc1542742016-07-20 21:44:37 +02002831 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002832 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002833 if (wp != NULL)
2834 {
2835 if (wp->w_llist == NULL)
2836 return;
2837 qi = wp->w_llist;
2838 }
2839
2840 for (idx = 0; idx < qi->qf_listcount; ++idx)
2841 if (qi->qf_lists[idx].qf_count)
2842 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002843 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2844 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (qfp->qf_fnum == curbuf->b_fnum)
2846 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002847 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2849 {
2850 if (amount == MAXLNUM)
2851 qfp->qf_cleared = TRUE;
2852 else
2853 qfp->qf_lnum += amount;
2854 }
2855 else if (amount_after && qfp->qf_lnum > line2)
2856 qfp->qf_lnum += amount_after;
2857 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002858
2859 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002860 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861}
2862
2863/*
2864 * Make a nice message out of the error character and the error number:
2865 * char number message
2866 * e or E 0 " error"
2867 * w or W 0 " warning"
2868 * i or I 0 " info"
2869 * 0 0 ""
2870 * other 0 " c"
2871 * e or E n " error n"
2872 * w or W n " warning n"
2873 * i or I n " info n"
2874 * 0 n " error n"
2875 * other n " c n"
2876 * 1 x "" :helpgrep
2877 */
2878 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002879qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 static char_u buf[20];
2882 static char_u cc[3];
2883 char_u *p;
2884
2885 if (c == 'W' || c == 'w')
2886 p = (char_u *)" warning";
2887 else if (c == 'I' || c == 'i')
2888 p = (char_u *)" info";
2889 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2890 p = (char_u *)" error";
2891 else if (c == 0 || c == 1)
2892 p = (char_u *)"";
2893 else
2894 {
2895 cc[0] = ' ';
2896 cc[1] = c;
2897 cc[2] = NUL;
2898 p = cc;
2899 }
2900
2901 if (nr <= 0)
2902 return p;
2903
2904 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2905 return buf;
2906}
2907
2908#if defined(FEAT_WINDOWS) || defined(PROTO)
2909/*
2910 * ":cwindow": open the quickfix window if we have errors to display,
2911 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002912 * ":lwindow": open the location list window if we have locations to display,
2913 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 */
2915 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002916ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002918 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 win_T *win;
2920
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002921 if (eap->cmdidx == CMD_lwindow)
2922 {
2923 qi = GET_LOC_LIST(curwin);
2924 if (qi == NULL)
2925 return;
2926 }
2927
2928 /* Look for an existing quickfix window. */
2929 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
2931 /*
2932 * If a quickfix window is open but we have no errors to display,
2933 * close the window. If a quickfix window is not open, then open
2934 * it if we have errors; otherwise, leave it closed.
2935 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002936 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002937 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002938 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 {
2940 if (win != NULL)
2941 ex_cclose(eap);
2942 }
2943 else if (win == NULL)
2944 ex_copen(eap);
2945}
2946
2947/*
2948 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002949 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002952ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002954 win_T *win = NULL;
2955 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002957 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2958 {
2959 qi = GET_LOC_LIST(curwin);
2960 if (qi == NULL)
2961 return;
2962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002964 /* Find existing quickfix window and close it. */
2965 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 if (win != NULL)
2967 win_close(win, FALSE);
2968}
2969
2970/*
2971 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002972 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 */
2974 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002975ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002977 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002980 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002981 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002982 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002984 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2985 {
2986 qi = GET_LOC_LIST(curwin);
2987 if (qi == NULL)
2988 {
2989 EMSG(_(e_loclist));
2990 return;
2991 }
2992 }
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 if (eap->addr_count != 0)
2995 height = eap->line2;
2996 else
2997 height = QF_WINHEIGHT;
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000#ifdef FEAT_GUI
3001 need_mouse_correct = TRUE;
3002#endif
3003
3004 /*
3005 * Find existing quickfix window, or open a new one.
3006 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003007 win = qf_find_win(qi);
3008
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003009 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003012 if (eap->addr_count != 0)
3013 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003014 if (cmdmod.split & WSP_VERT)
3015 {
3016 if (height != W_WIDTH(win))
3017 win_setwidth(height);
3018 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003019 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003020 win_setheight(height);
3021 }
3022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 else
3024 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00003025 qf_buf = qf_find_buf(qi);
3026
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 /* The current window becomes the previous window afterwards. */
3028 win = curwin;
3029
Bram Moolenaar77642c02012-11-20 17:55:10 +01003030 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3031 && cmdmod.split == 0)
3032 /* Create the new window at the very bottom, except when
3033 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003034 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003035 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003037 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003039 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003041 /*
3042 * For the location list window, create a reference to the
3043 * location list from the window 'win'.
3044 */
3045 curwin->w_llist_ref = win->w_llist;
3046 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003048
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003049 if (oldwin != curwin)
3050 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003051 if (qf_buf != NULL)
3052 /* Use the existing quickfix buffer */
3053 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003054 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003055 else
3056 {
3057 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003058 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003059 /* switch off 'swapfile' */
3060 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3061 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003062 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003063 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003064 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003065#ifdef FEAT_DIFF
3066 curwin->w_p_diff = FALSE;
3067#endif
3068#ifdef FEAT_FOLDING
3069 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3070 OPT_LOCAL);
3071#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003074 /* Only set the height when still in the same tab page and there is no
3075 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003076 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 win_setheight(height);
3078 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3079 if (win_valid(win))
3080 prevwin = win;
3081 }
3082
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003083 qf_set_title_var(qi);
3084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 /*
3086 * Fill the buffer with the quickfix list.
3087 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003088 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003090 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 curwin->w_cursor.col = 0;
3092 check_cursor();
3093 update_topline(); /* scroll to show the line */
3094}
3095
3096/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003097 * Move the cursor in the quickfix window to "lnum".
3098 */
3099 static void
3100qf_win_goto(win_T *win, linenr_T lnum)
3101{
3102 win_T *old_curwin = curwin;
3103
3104 curwin = win;
3105 curbuf = win->w_buffer;
3106 curwin->w_cursor.lnum = lnum;
3107 curwin->w_cursor.col = 0;
3108#ifdef FEAT_VIRTUALEDIT
3109 curwin->w_cursor.coladd = 0;
3110#endif
3111 curwin->w_curswant = 0;
3112 update_topline(); /* scroll to show the line */
3113 redraw_later(VALID);
3114 curwin->w_redr_status = TRUE; /* update ruler */
3115 curwin = old_curwin;
3116 curbuf = curwin->w_buffer;
3117}
3118
3119/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003120 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003121 */
3122 void
3123ex_cbottom(exarg_T *eap UNUSED)
3124{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003125 qf_info_T *qi = &ql_info;
3126 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003127
Bram Moolenaar537ef082016-07-09 17:56:19 +02003128 if (eap->cmdidx == CMD_lbottom)
3129 {
3130 qi = GET_LOC_LIST(curwin);
3131 if (qi == NULL)
3132 {
3133 EMSG(_(e_loclist));
3134 return;
3135 }
3136 }
3137
3138 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003139 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3140 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3141}
3142
3143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * Return the number of the current entry (line number in the quickfix
3145 * window).
3146 */
3147 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003148qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003150 qf_info_T *qi = &ql_info;
3151
3152 if (IS_LL_WINDOW(wp))
3153 /* In the location list window, use the referenced location list */
3154 qi = wp->w_llist_ref;
3155
3156 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157}
3158
3159/*
3160 * Update the cursor position in the quickfix window to the current error.
3161 * Return TRUE if there is a quickfix window.
3162 */
3163 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003164qf_win_pos_update(
3165 qf_info_T *qi,
3166 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167{
3168 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003169 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 /*
3172 * Put the cursor on the current error in the quickfix window, so that
3173 * it's viewable.
3174 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003175 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 if (win != NULL
3177 && qf_index <= win->w_buffer->b_ml.ml_line_count
3178 && old_qf_index != qf_index)
3179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (qf_index > old_qf_index)
3181 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003182 win->w_redraw_top = old_qf_index;
3183 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
3185 else
3186 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003187 win->w_redraw_top = qf_index;
3188 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003190 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
3192 return win != NULL;
3193}
3194
3195/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003196 * Check whether the given window is displaying the specified quickfix/location
3197 * list buffer
3198 */
3199 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003200is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003201{
3202 /*
3203 * A window displaying the quickfix buffer will have the w_llist_ref field
3204 * set to NULL.
3205 * A window displaying a location list buffer will have the w_llist_ref
3206 * pointing to the location list.
3207 */
3208 if (bt_quickfix(win->w_buffer))
3209 if ((qi == &ql_info && win->w_llist_ref == NULL)
3210 || (qi != &ql_info && win->w_llist_ref == qi))
3211 return TRUE;
3212
3213 return FALSE;
3214}
3215
3216/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003217 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003218 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003219 */
3220 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003221qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003222{
3223 win_T *win;
3224
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003225 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003226 if (is_qf_win(win, qi))
3227 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003228
3229 return win;
3230}
3231
3232/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003233 * Find a quickfix buffer.
3234 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 */
3236 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003237qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003239 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003240 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
Bram Moolenaar9c102382006-05-03 21:26:49 +00003242 FOR_ALL_TAB_WINDOWS(tp, win)
3243 if (is_qf_win(win, qi))
3244 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003245
Bram Moolenaar9c102382006-05-03 21:26:49 +00003246 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247}
3248
3249/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003250 * Update the w:quickfix_title variable in the quickfix/location list window
3251 */
3252 static void
3253qf_update_win_titlevar(qf_info_T *qi)
3254{
3255 win_T *win;
3256 win_T *curwin_save;
3257
3258 if ((win = qf_find_win(qi)) != NULL)
3259 {
3260 curwin_save = curwin;
3261 curwin = win;
3262 qf_set_title_var(qi);
3263 curwin = curwin_save;
3264 }
3265}
3266
3267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 * Find the quickfix buffer. If it exists, update the contents.
3269 */
3270 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003271qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003274 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
3277 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003278 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 if (buf != NULL)
3280 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003281 linenr_T old_line_count = buf->b_ml.ml_line_count;
3282
3283 if (old_last == NULL)
3284 /* set curwin/curbuf to buf and save a few things */
3285 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaard823fa92016-08-12 16:29:27 +02003287 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003288
Bram Moolenaar864293a2016-06-02 13:40:04 +02003289 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003290 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003291
Bram Moolenaar864293a2016-06-02 13:40:04 +02003292 if (old_last == NULL)
3293 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003294 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003295
3296 /* restore curwin/curbuf and a few other things */
3297 aucmd_restbuf(&aco);
3298 }
3299
3300 /* Only redraw when added lines are visible. This avoids flickering
3301 * when the added lines are not visible. */
3302 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3303 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
3305}
3306
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003307/*
3308 * Set "w:quickfix_title" if "qi" has a title.
3309 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003310 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003311qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003312{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003313 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3314 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003315 qi->qf_lists[qi->qf_curlist].qf_title);
3316}
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318/*
3319 * Fill current buffer with quickfix errors, replacing any previous contents.
3320 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003321 * If "old_last" is not NULL append the items after this one.
3322 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3323 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 */
3325 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003326qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003328 linenr_T lnum;
3329 qfline_T *qfp;
3330 buf_T *errbuf;
3331 int len;
3332 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar864293a2016-06-02 13:40:04 +02003334 if (old_last == NULL)
3335 {
3336 if (buf != curbuf)
3337 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003338 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003339 return;
3340 }
3341
3342 /* delete all existing lines */
3343 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3344 (void)ml_delete((linenr_T)1, FALSE);
3345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
3347 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003348 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003351 if (old_last == NULL)
3352 {
3353 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3354 lnum = 0;
3355 }
3356 else
3357 {
3358 qfp = old_last->qf_next;
3359 lnum = buf->b_ml.ml_line_count;
3360 }
3361 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
3363 if (qfp->qf_fnum != 0
3364 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3365 && errbuf->b_fname != NULL)
3366 {
3367 if (qfp->qf_type == 1) /* :helpgrep */
3368 STRCPY(IObuff, gettail(errbuf->b_fname));
3369 else
3370 STRCPY(IObuff, errbuf->b_fname);
3371 len = (int)STRLEN(IObuff);
3372 }
3373 else
3374 len = 0;
3375 IObuff[len++] = '|';
3376
3377 if (qfp->qf_lnum > 0)
3378 {
3379 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3380 len += (int)STRLEN(IObuff + len);
3381
3382 if (qfp->qf_col > 0)
3383 {
3384 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3385 len += (int)STRLEN(IObuff + len);
3386 }
3387
3388 sprintf((char *)IObuff + len, "%s",
3389 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3390 len += (int)STRLEN(IObuff + len);
3391 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003392 else if (qfp->qf_pattern != NULL)
3393 {
3394 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3395 len += (int)STRLEN(IObuff + len);
3396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 IObuff[len++] = '|';
3398 IObuff[len++] = ' ';
3399
3400 /* Remove newlines and leading whitespace from the text.
3401 * For an unrecognized line keep the indent, the compiler may
3402 * mark a word with ^^^^. */
3403 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3404 IObuff + len, IOSIZE - len);
3405
Bram Moolenaar864293a2016-06-02 13:40:04 +02003406 if (ml_append_buf(buf, lnum, IObuff,
3407 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003409 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003411 if (qfp == NULL)
3412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003414
3415 if (old_last == NULL)
3416 /* Delete the empty line which is now at the end */
3417 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
3419
3420 /* correct cursor position */
3421 check_lnums(TRUE);
3422
Bram Moolenaar864293a2016-06-02 13:40:04 +02003423 if (old_last == NULL)
3424 {
3425 /* Set the 'filetype' to "qf" each time after filling the buffer.
3426 * This resembles reading a file into a buffer, it's more logical when
3427 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003428#ifdef FEAT_AUTOCMD
3429 ++curbuf_lock;
3430#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003431 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3432 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003435 keep_filetype = TRUE; /* don't detect 'filetype' */
3436 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003438 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003440 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003441 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003443 /* make sure it will be redrawn */
3444 redraw_curbuf_later(NOT_VALID);
3445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 /* Restore KeyTyped, setting 'filetype' may reset it. */
3448 KeyTyped = old_KeyTyped;
3449}
3450
3451#endif /* FEAT_WINDOWS */
3452
3453/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003454 * Return TRUE when using ":vimgrep" for ":grep".
3455 */
3456 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003457grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003458{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003459 return ((cmdidx == CMD_grep
3460 || cmdidx == CMD_lgrep
3461 || cmdidx == CMD_grepadd
3462 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003463 && STRCMP("internal",
3464 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3465}
3466
3467/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003468 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 */
3470 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003471ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003473 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003475 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003477 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003478 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003479 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003480#ifdef FEAT_AUTOCMD
3481 char_u *au_name = NULL;
3482
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003483 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3484 if (grep_internal(eap->cmdidx))
3485 {
3486 ex_vimgrep(eap);
3487 return;
3488 }
3489
Bram Moolenaar7c626922005-02-07 22:01:03 +00003490 switch (eap->cmdidx)
3491 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003492 case CMD_make: au_name = (char_u *)"make"; break;
3493 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3494 case CMD_grep: au_name = (char_u *)"grep"; break;
3495 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3496 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3497 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003498 default: break;
3499 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003500 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3501 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003502 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003503# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003504 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003505 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003506# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003507 }
3508#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003509#ifdef FEAT_MBYTE
3510 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaara6557602006-02-04 22:43:20 +00003513 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3514 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003515 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003518 fname = get_mef_name();
3519 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003521 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523 /*
3524 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3525 */
3526 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3527 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003528 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 cmd = alloc(len);
3530 if (cmd == NULL)
3531 return;
3532 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3533 (char *)p_shq);
3534 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003535 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 /*
3537 * Output a newline if there's something else than the :make command that
3538 * was typed (in which case the cursor is in column 0).
3539 */
3540 if (msg_col == 0)
3541 msg_didout = FALSE;
3542 msg_start();
3543 MSG_PUTS(":!");
3544 msg_outtrans(cmd); /* show what we are doing */
3545
3546 /* let the shell know if we are redirecting output or not */
3547 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3548
3549#ifdef AMIGA
3550 out_flush();
3551 /* read window status report and redraw before message */
3552 (void)char_avail();
3553#endif
3554
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003555 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003556 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3557 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003558 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003559 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003560 if (wp != NULL)
3561 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003562#ifdef FEAT_AUTOCMD
3563 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003564 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003565 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3566 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003567 if (qi->qf_curlist < qi->qf_listcount)
3568 res = qi->qf_lists[qi->qf_curlist].qf_count;
3569 else
3570 res = 0;
3571 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003572#endif
3573 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003574 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar7c626922005-02-07 22:01:03 +00003576 mch_remove(fname);
3577 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 vim_free(cmd);
3579}
3580
3581/*
3582 * Return the name for the errorfile, in allocated memory.
3583 * Find a new unique name when 'makeef' contains "##".
3584 * Returns NULL for error.
3585 */
3586 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003587get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
3589 char_u *p;
3590 char_u *name;
3591 static int start = -1;
3592 static int off = 0;
3593#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003594 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595#endif
3596
3597 if (*p_mef == NUL)
3598 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003599 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (name == NULL)
3601 EMSG(_(e_notmp));
3602 return name;
3603 }
3604
3605 for (p = p_mef; *p; ++p)
3606 if (p[0] == '#' && p[1] == '#')
3607 break;
3608
3609 if (*p == NUL)
3610 return vim_strsave(p_mef);
3611
3612 /* Keep trying until the name doesn't exist yet. */
3613 for (;;)
3614 {
3615 if (start == -1)
3616 start = mch_get_pid();
3617 else
3618 off += 19;
3619
3620 name = alloc((unsigned)STRLEN(p_mef) + 30);
3621 if (name == NULL)
3622 break;
3623 STRCPY(name, p_mef);
3624 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3625 STRCAT(name, p + 2);
3626 if (mch_getperm(name) < 0
3627#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003628 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 && mch_lstat((char *)name, &sb) < 0
3630#endif
3631 )
3632 break;
3633 vim_free(name);
3634 }
3635 return name;
3636}
3637
3638/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003639 * Returns the number of valid entries in the current quickfix/location list.
3640 */
3641 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003642qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003643{
3644 qf_info_T *qi = &ql_info;
3645 qfline_T *qfp;
3646 int i, sz = 0;
3647 int prev_fnum = 0;
3648
3649 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3650 {
3651 /* Location list */
3652 qi = GET_LOC_LIST(curwin);
3653 if (qi == NULL)
3654 return 0;
3655 }
3656
3657 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003658 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003659 ++i, qfp = qfp->qf_next)
3660 {
3661 if (qfp->qf_valid)
3662 {
3663 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3664 sz++; /* Count all valid entries */
3665 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3666 {
3667 /* Count the number of files */
3668 sz++;
3669 prev_fnum = qfp->qf_fnum;
3670 }
3671 }
3672 }
3673
3674 return sz;
3675}
3676
3677/*
3678 * Returns the current index of the quickfix/location list.
3679 * Returns 0 if there is an error.
3680 */
3681 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003682qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003683{
3684 qf_info_T *qi = &ql_info;
3685
3686 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3687 {
3688 /* Location list */
3689 qi = GET_LOC_LIST(curwin);
3690 if (qi == NULL)
3691 return 0;
3692 }
3693
3694 return qi->qf_lists[qi->qf_curlist].qf_index;
3695}
3696
3697/*
3698 * Returns the current index in the quickfix/location list (counting only valid
3699 * entries). If no valid entries are in the list, then returns 1.
3700 */
3701 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003702qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003703{
3704 qf_info_T *qi = &ql_info;
3705 qf_list_T *qfl;
3706 qfline_T *qfp;
3707 int i, eidx = 0;
3708 int prev_fnum = 0;
3709
3710 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3711 {
3712 /* Location list */
3713 qi = GET_LOC_LIST(curwin);
3714 if (qi == NULL)
3715 return 1;
3716 }
3717
3718 qfl = &qi->qf_lists[qi->qf_curlist];
3719 qfp = qfl->qf_start;
3720
3721 /* check if the list has valid errors */
3722 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3723 return 1;
3724
3725 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3726 {
3727 if (qfp->qf_valid)
3728 {
3729 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3730 {
3731 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3732 {
3733 /* Count the number of files */
3734 eidx++;
3735 prev_fnum = qfp->qf_fnum;
3736 }
3737 }
3738 else
3739 eidx++;
3740 }
3741 }
3742
3743 return eidx ? eidx : 1;
3744}
3745
3746/*
3747 * Get the 'n'th valid error entry in the quickfix or location list.
3748 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3749 * For :cdo and :ldo returns the 'n'th valid error entry.
3750 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3751 */
3752 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003753qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003754{
3755 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3756 qfline_T *qfp = qfl->qf_start;
3757 int i, eidx;
3758 int prev_fnum = 0;
3759
3760 /* check if the list has valid errors */
3761 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3762 return 1;
3763
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003764 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003765 i++, qfp = qfp->qf_next)
3766 {
3767 if (qfp->qf_valid)
3768 {
3769 if (fdo)
3770 {
3771 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3772 {
3773 /* Count the number of files */
3774 eidx++;
3775 prev_fnum = qfp->qf_fnum;
3776 }
3777 }
3778 else
3779 eidx++;
3780 }
3781
3782 if (eidx == n)
3783 break;
3784 }
3785
3786 if (i <= qfl->qf_count)
3787 return i;
3788 else
3789 return 1;
3790}
3791
3792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003794 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003795 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 */
3797 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003798ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003800 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003801 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003802
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003803 if (eap->cmdidx == CMD_ll
3804 || eap->cmdidx == CMD_lrewind
3805 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003806 || eap->cmdidx == CMD_llast
3807 || eap->cmdidx == CMD_ldo
3808 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003809 {
3810 qi = GET_LOC_LIST(curwin);
3811 if (qi == NULL)
3812 {
3813 EMSG(_(e_loclist));
3814 return;
3815 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003816 }
3817
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003818 if (eap->addr_count > 0)
3819 errornr = (int)eap->line2;
3820 else
3821 {
3822 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3823 errornr = 0;
3824 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3825 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3826 errornr = 1;
3827 else
3828 errornr = 32767;
3829 }
3830
3831 /* For cdo and ldo commands, jump to the nth valid error.
3832 * For cfdo and lfdo commands, jump to the nth valid file entry.
3833 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02003834 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
3835 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003836 errornr = qf_get_nth_valid_entry(qi,
3837 eap->addr_count > 0 ? (int)eap->line1 : 1,
3838 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3839
3840 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841}
3842
3843/*
3844 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003845 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003846 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 */
3848 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003849ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003851 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003852 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003853
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003854 if (eap->cmdidx == CMD_lnext
3855 || eap->cmdidx == CMD_lNext
3856 || eap->cmdidx == CMD_lprevious
3857 || eap->cmdidx == CMD_lnfile
3858 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003859 || eap->cmdidx == CMD_lpfile
3860 || eap->cmdidx == CMD_ldo
3861 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003862 {
3863 qi = GET_LOC_LIST(curwin);
3864 if (qi == NULL)
3865 {
3866 EMSG(_(e_loclist));
3867 return;
3868 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003869 }
3870
Bram Moolenaar55b69262017-08-13 13:42:01 +02003871 if (eap->addr_count > 0
3872 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
3873 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003874 errornr = (int)eap->line2;
3875 else
3876 errornr = 1;
3877
3878 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3879 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003881 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3882 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003884 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3885 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 ? BACKWARD_FILE
3887 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003888 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889}
3890
3891/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003892 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003893 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 */
3895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003896ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003898 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003899 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003900 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003901#ifdef FEAT_AUTOCMD
3902 char_u *au_name = NULL;
3903#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003904
3905 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003906 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003907 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003908
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003909#ifdef FEAT_AUTOCMD
3910 switch (eap->cmdidx)
3911 {
3912 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3913 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3914 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3915 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3916 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3917 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3918 default: break;
3919 }
3920 if (au_name != NULL)
3921 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3922#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003923#ifdef FEAT_MBYTE
3924 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3925#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003926#ifdef FEAT_BROWSE
3927 if (cmdmod.browse)
3928 {
3929 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3930 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3931 if (browse_file == NULL)
3932 return;
3933 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3934 vim_free(browse_file);
3935 }
3936 else
3937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003939 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003940
3941 /*
3942 * This function is used by the :cfile, :cgetfile and :caddfile
3943 * commands.
3944 * :cfile always creates a new quickfix list and jumps to the
3945 * first error.
3946 * :cgetfile creates a new quickfix list but doesn't jump to the
3947 * first error.
3948 * :caddfile adds to an existing quickfix list. If there is no
3949 * quickfix list then a new list is created.
3950 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003951 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003952 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003953 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003954 && (eap->cmdidx == CMD_cfile
3955 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003956 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003957#ifdef FEAT_AUTOCMD
3958 if (au_name != NULL)
3959 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3960#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003961 if (wp != NULL)
3962 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003963 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003964 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003965
3966 else
3967 {
3968#ifdef FEAT_AUTOCMD
3969 if (au_name != NULL)
3970 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3971#endif
3972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973}
3974
3975/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003976 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003977 * ":vimgrepadd {pattern} file(s)"
3978 * ":lvimgrep {pattern} file(s)"
3979 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003980 */
3981 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003982ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003983{
Bram Moolenaar81695252004-12-29 20:58:21 +00003984 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003985 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003986 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003987 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003988 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003989 char_u *s;
3990 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003991 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003992 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003993#ifdef FEAT_AUTOCMD
3994 qfline_T *cur_qf_start;
3995#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003996 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003997 buf_T *buf;
3998 int duplicate_name = FALSE;
3999 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004000 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004001 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004002 buf_T *first_match_buf = NULL;
4003 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004004 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004005#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4006 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004007#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004008 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004009 int flags = 0;
4010 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004011 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004012 char_u *dirname_start = NULL;
4013 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004014 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004015#ifdef FEAT_AUTOCMD
4016 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004017
4018 switch (eap->cmdidx)
4019 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004020 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4021 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4022 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004023 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004024 case CMD_grep: au_name = (char_u *)"grep"; break;
4025 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4026 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4027 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004028 default: break;
4029 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004030 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4031 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004032 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004033# ifdef FEAT_EVAL
4034 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004035 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004036# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004037 }
4038#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004039
Bram Moolenaar754b5602006-02-09 23:53:20 +00004040 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004041 || eap->cmdidx == CMD_lvimgrep
4042 || eap->cmdidx == CMD_lgrepadd
4043 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004044 {
4045 qi = ll_get_or_alloc_list(curwin);
4046 if (qi == NULL)
4047 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004048 }
4049
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004050 if (eap->addr_count > 0)
4051 tomatch = eap->line2;
4052 else
4053 tomatch = MAXLNUM;
4054
Bram Moolenaar81695252004-12-29 20:58:21 +00004055 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004056 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004057 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004058 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004059 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004060 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004061 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004062 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004063 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004064
4065 if (s != NULL && *s == NUL)
4066 {
4067 /* Pattern is empty, use last search pattern. */
4068 if (last_search_pat() == NULL)
4069 {
4070 EMSG(_(e_noprevre));
4071 goto theend;
4072 }
4073 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4074 }
4075 else
4076 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4077
Bram Moolenaar86b68352004-12-27 21:59:20 +00004078 if (regmatch.regprog == NULL)
4079 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004080 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004081 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004082
4083 p = skipwhite(p);
4084 if (*p == NUL)
4085 {
4086 EMSG(_("E683: File name missing or invalid pattern"));
4087 goto theend;
4088 }
4089
Bram Moolenaar55b69262017-08-13 13:42:01 +02004090 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4091 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004092 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004093 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004094 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004095
4096 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004097 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004098 goto theend;
4099 if (fcount == 0)
4100 {
4101 EMSG(_(e_nomatch));
4102 goto theend;
4103 }
4104
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004105 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4106 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004107 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004108 {
4109 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004110 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004111 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004112
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004113 /* Remember the current directory, because a BufRead autocommand that does
4114 * ":lcd %:p:h" changes the meaning of short path names. */
4115 mch_dirname(dirname_start, MAXPATHL);
4116
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004117#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004118 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004119 * changing the current quickfix list. */
4120 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4121#endif
4122
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004123 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004124 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004125 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004126 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004127 if (time(NULL) > seconds)
4128 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004129 /* Display the file name every second or so, show the user we are
4130 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004131 seconds = time(NULL);
4132 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004133 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004134 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004135 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004136 else
4137 {
4138 msg_outtrans(p);
4139 vim_free(p);
4140 }
4141 msg_clr_eos();
4142 msg_didout = FALSE; /* overwrite this message */
4143 msg_nowait = TRUE; /* don't wait for this message */
4144 msg_col = 0;
4145 out_flush();
4146 }
4147
Bram Moolenaar81695252004-12-29 20:58:21 +00004148 buf = buflist_findname_exp(fnames[fi]);
4149 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4150 {
4151 /* Remember that a buffer with this name already exists. */
4152 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004153 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004154 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004155
4156#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4157 /* Don't do Filetype autocommands to avoid loading syntax and
4158 * indent scripts, a great speed improvement. */
4159 save_ei = au_event_disable(",Filetype");
4160#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004161 /* Don't use modelines here, it's useless. */
4162 save_mls = p_mls;
4163 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004164
4165 /* Load file into a buffer, so that 'fileencoding' is detected,
4166 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004167 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004168
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004169 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004170#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4171 au_event_restore(save_ei);
4172#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004173 }
4174 else
4175 /* Use existing, loaded buffer. */
4176 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004177
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004178#ifdef FEAT_AUTOCMD
4179 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4180 {
4181 int idx;
4182
4183 /* Autocommands changed the quickfix list. Find the one we were
4184 * using and restore it. */
4185 for (idx = 0; idx < LISTCOUNT; ++idx)
4186 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4187 {
4188 qi->qf_curlist = idx;
4189 break;
4190 }
4191 if (idx == LISTCOUNT)
4192 {
4193 /* List cannot be found, create a new one. */
4194 qf_new_list(qi, *eap->cmdlinep);
4195 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4196 }
4197 }
4198#endif
4199
Bram Moolenaar81695252004-12-29 20:58:21 +00004200 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004201 {
4202 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004203 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004204 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004205 else
4206 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004207 /* Try for a match in all lines of the buffer.
4208 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004209 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004210 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4211 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004212 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004213 col = 0;
4214 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004215 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004216 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004217 /* Pass the buffer number so that it gets used even for a
4218 * dummy buffer, unless duplicate_name is set, then the
4219 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004220 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004221 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004222 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004223 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004224 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004225 ml_get_buf(buf,
4226 regmatch.startpos[0].lnum + lnum, FALSE),
4227 regmatch.startpos[0].lnum + lnum,
4228 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004229 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004230 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004231 0, /* nr */
4232 0, /* type */
4233 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004234 ) == FAIL)
4235 {
4236 got_int = TRUE;
4237 break;
4238 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004239 found_match = TRUE;
4240 if (--tomatch == 0)
4241 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004242 if ((flags & VGR_GLOBAL) == 0
4243 || regmatch.endpos[0].lnum > 0)
4244 break;
4245 col = regmatch.endpos[0].col
4246 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004247 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004248 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004249 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004250 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004251 if (got_int)
4252 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004253 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004254#ifdef FEAT_AUTOCMD
4255 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4256#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004257
4258 if (using_dummy)
4259 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004260 if (found_match && first_match_buf == NULL)
4261 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004262 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004263 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004264 /* Never keep a dummy buffer if there is another buffer
4265 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004266 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004267 buf = NULL;
4268 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004269 else if (!cmdmod.hide
4270 || buf->b_p_bh[0] == 'u' /* "unload" */
4271 || buf->b_p_bh[0] == 'w' /* "wipe" */
4272 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004273 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004274 /* When no match was found we don't need to remember the
4275 * buffer, wipe it out. If there was a match and it
4276 * wasn't the first one or we won't jump there: only
4277 * unload the buffer.
4278 * Ignore 'hidden' here, because it may lead to having too
4279 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004280 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004281 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004282 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004283 buf = NULL;
4284 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004285 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004286 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004287 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004288 /* Keeping the buffer, remove the dummy flag. */
4289 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004290 buf = NULL;
4291 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004292 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004293
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004294 if (buf != NULL)
4295 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004296 /* Keeping the buffer, remove the dummy flag. */
4297 buf->b_flags &= ~BF_DUMMY;
4298
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004299 /* If the buffer is still loaded we need to use the
4300 * directory we jumped to below. */
4301 if (buf == first_match_buf
4302 && target_dir == NULL
4303 && STRCMP(dirname_start, dirname_now) != 0)
4304 target_dir = vim_strsave(dirname_now);
4305
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004306 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004307 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004308 * need to be done (again). But not the window-local
4309 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004310 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004311#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004312 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4313 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004314#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004315 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004316 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004317 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004318 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004319 }
4320 }
4321
4322 FreeWild(fcount, fnames);
4323
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004324 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4325 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4326 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004327
4328#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004329 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004330#endif
4331
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004332#ifdef FEAT_AUTOCMD
4333 if (au_name != NULL)
4334 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4335 curbuf->b_fname, TRUE, curbuf);
4336#endif
4337
Bram Moolenaar86b68352004-12-27 21:59:20 +00004338 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004339 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004340 {
4341 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004342 {
4343 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004344 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004345 if (buf != curbuf)
4346 /* If we jumped to another buffer redrawing will already be
4347 * taken care of. */
4348 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004349
4350 /* Jump to the directory used after loading the buffer. */
4351 if (curbuf == first_match_buf && target_dir != NULL)
4352 {
4353 exarg_T ea;
4354
4355 ea.arg = target_dir;
4356 ea.cmdidx = CMD_lcd;
4357 ex_cd(&ea);
4358 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004359 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004360 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004361 else
4362 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004363
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004364 /* If we loaded a dummy buffer into the current window, the autocommands
4365 * may have messed up things, need to redraw and recompute folds. */
4366 if (redraw_for_dummy)
4367 {
4368#ifdef FEAT_FOLDING
4369 foldUpdateAll(curwin);
4370#else
4371 redraw_later(NOT_VALID);
4372#endif
4373 }
4374
Bram Moolenaar86b68352004-12-27 21:59:20 +00004375theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004376 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004377 vim_free(dirname_now);
4378 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004379 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004380 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004381}
4382
4383/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004384 * Restore current working directory to "dirname_start" if they differ, taking
4385 * into account whether it is set locally or globally.
4386 */
4387 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004388restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004389{
4390 char_u *dirname_now = alloc(MAXPATHL);
4391
4392 if (NULL != dirname_now)
4393 {
4394 mch_dirname(dirname_now, MAXPATHL);
4395 if (STRCMP(dirname_start, dirname_now) != 0)
4396 {
4397 /* If the directory has changed, change it back by building up an
4398 * appropriate ex command and executing it. */
4399 exarg_T ea;
4400
4401 ea.arg = dirname_start;
4402 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4403 ex_cd(&ea);
4404 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004405 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004406 }
4407}
4408
4409/*
4410 * Load file "fname" into a dummy buffer and return the buffer pointer,
4411 * placing the directory resulting from the buffer load into the
4412 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4413 * prior to calling this function. Restores directory to "dirname_start" prior
4414 * to returning, if autocmds or the 'autochdir' option have changed it.
4415 *
4416 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4417 * or wipe_dummy_buffer() later!
4418 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004419 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004420 */
4421 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004422load_dummy_buffer(
4423 char_u *fname,
4424 char_u *dirname_start, /* in: old directory */
4425 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004426{
4427 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004428 bufref_T newbufref;
4429 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004430 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004431 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004432
4433 /* Allocate a buffer without putting it in the buffer list. */
4434 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4435 if (newbuf == NULL)
4436 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004437 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004438
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004439 /* Init the options. */
4440 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4441
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004442 /* need to open the memfile before putting the buffer in a window */
4443 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004444 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004445 /* set curwin/curbuf to buf and save a few things */
4446 aucmd_prepbuf(&aco, newbuf);
4447
4448 /* Need to set the filename for autocommands. */
4449 (void)setfname(curbuf, fname, NULL, FALSE);
4450
Bram Moolenaar81695252004-12-29 20:58:21 +00004451 /* Create swap file now to avoid the ATTENTION message. */
4452 check_need_swap(TRUE);
4453
4454 /* Remove the "dummy" flag, otherwise autocommands may not
4455 * work. */
4456 curbuf->b_flags &= ~BF_DUMMY;
4457
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004458 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004459 if (readfile(fname, NULL,
4460 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4461 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004462 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004463 && !(curbuf->b_flags & BF_NEW))
4464 {
4465 failed = FALSE;
4466 if (curbuf != newbuf)
4467 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004468 /* Bloody autocommands changed the buffer! Can happen when
4469 * using netrw and editing a remote file. Use the current
4470 * buffer instead, delete the dummy one after restoring the
4471 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004472 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004473 newbuf = curbuf;
4474 }
4475 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004476
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004477 /* restore curwin/curbuf and a few other things */
4478 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004479 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4480 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004481
4482 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4483 * skip it. */
4484 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004485 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004486
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004487 /*
4488 * When autocommands/'autochdir' option changed directory: go back.
4489 * Let the caller know what the resulting dir was first, in case it is
4490 * important.
4491 */
4492 mch_dirname(resulting_dir, MAXPATHL);
4493 restore_start_dir(dirname_start);
4494
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004495 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004496 return NULL;
4497 if (failed)
4498 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004499 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004500 return NULL;
4501 }
4502 return newbuf;
4503}
4504
4505/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004506 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4507 * directory to "dirname_start" prior to returning, if autocmds or the
4508 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004509 */
4510 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004511wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004512{
4513 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004514 {
4515#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4516 cleanup_T cs;
4517
4518 /* Reset the error/interrupt/exception state here so that aborting()
4519 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4520 * work when got_int is set. */
4521 enter_cleanup(&cs);
4522#endif
4523
Bram Moolenaar81695252004-12-29 20:58:21 +00004524 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004525
4526#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4527 /* Restore the error/interrupt/exception state if not discarded by a
4528 * new aborting error, interrupt, or uncaught exception. */
4529 leave_cleanup(&cs);
4530#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004531 /* When autocommands/'autochdir' option changed directory: go back. */
4532 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004533 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004534}
4535
4536/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004537 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4538 * directory to "dirname_start" prior to returning, if autocmds or the
4539 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004540 */
4541 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004542unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004543{
4544 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004545 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004546 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004547
4548 /* When autocommands/'autochdir' option changed directory: go back. */
4549 restore_start_dir(dirname_start);
4550 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004551}
4552
Bram Moolenaar05159a02005-02-26 23:04:13 +00004553#if defined(FEAT_EVAL) || defined(PROTO)
4554/*
4555 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004556 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004557 */
4558 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004559get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004560{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004561 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004562 dict_T *dict;
4563 char_u buf[2];
4564 qfline_T *qfp;
4565 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004566 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004567
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004568 if (wp != NULL)
4569 {
4570 qi = GET_LOC_LIST(wp);
4571 if (qi == NULL)
4572 return FAIL;
4573 }
4574
Bram Moolenaard823fa92016-08-12 16:29:27 +02004575 if (qf_idx == -1)
4576 qf_idx = qi->qf_curlist;
4577
4578 if (qf_idx >= qi->qf_listcount
4579 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004580 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004581
Bram Moolenaard823fa92016-08-12 16:29:27 +02004582 qfp = qi->qf_lists[qf_idx].qf_start;
4583 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004584 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004585 /* Handle entries with a non-existing buffer number. */
4586 bufnum = qfp->qf_fnum;
4587 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4588 bufnum = 0;
4589
Bram Moolenaar05159a02005-02-26 23:04:13 +00004590 if ((dict = dict_alloc()) == NULL)
4591 return FAIL;
4592 if (list_append_dict(list, dict) == FAIL)
4593 return FAIL;
4594
4595 buf[0] = qfp->qf_type;
4596 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004597 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004598 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4599 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4600 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4601 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004602 || dict_add_nr_str(dict, "pattern", 0L,
4603 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4604 || dict_add_nr_str(dict, "text", 0L,
4605 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004606 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4607 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4608 return FAIL;
4609
4610 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004611 if (qfp == NULL)
4612 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004613 }
4614 return OK;
4615}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004616
4617/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004618 * Flags used by getqflist()/getloclist() to determine which fields to return.
4619 */
4620enum {
4621 QF_GETLIST_NONE = 0x0,
4622 QF_GETLIST_TITLE = 0x1,
4623 QF_GETLIST_ITEMS = 0x2,
4624 QF_GETLIST_NR = 0x4,
4625 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004626 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004627 QF_GETLIST_ALL = 0xFF
4628};
4629
4630/*
4631 * Return quickfix/location list details (title) as a
4632 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4633 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004634 */
4635 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004636get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4637{
4638 qf_info_T *qi = &ql_info;
4639 int status = OK;
4640 int qf_idx;
4641 dictitem_T *di;
4642 int flags = QF_GETLIST_NONE;
4643
4644 if (wp != NULL)
4645 {
4646 qi = GET_LOC_LIST(wp);
4647 if (qi == NULL)
Bram Moolenaar875feea2017-06-11 16:07:51 +02004648 {
4649 /* If querying for the size of the location list, return 0 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004650 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4651 && (di->di_tv.v_type == VAR_STRING)
4652 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4653 return dict_add_nr_str(retdict, "nr", 0, NULL);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004654 return FAIL;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004655 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004656 }
4657
4658 qf_idx = qi->qf_curlist; /* default is the current list */
4659 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4660 {
4661 /* Use the specified quickfix/location list */
4662 if (di->di_tv.v_type == VAR_NUMBER)
4663 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004664 /* for zero use the current list */
4665 if (di->di_tv.vval.v_number != 0)
4666 {
4667 qf_idx = di->di_tv.vval.v_number - 1;
4668 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4669 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004670 }
4671 else if (qi->qf_listcount == 0) /* stack is empty */
Bram Moolenaar875feea2017-06-11 16:07:51 +02004672 return FAIL;
4673 flags |= QF_GETLIST_NR;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004674 }
4675 else if ((di->di_tv.v_type == VAR_STRING)
4676 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
Bram Moolenaar875feea2017-06-11 16:07:51 +02004677 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004678 /* Get the last quickfix list number */
4679 if (qi->qf_listcount > 0)
4680 qf_idx = qi->qf_listcount - 1;
4681 else
4682 qf_idx = -1; /* Quickfix stack is empty */
Bram Moolenaard823fa92016-08-12 16:29:27 +02004683 flags |= QF_GETLIST_NR;
4684 }
4685 else
4686 return FAIL;
4687 }
4688
Bram Moolenaar875feea2017-06-11 16:07:51 +02004689 if (qf_idx != -1)
4690 {
4691 if (dict_find(what, (char_u *)"all", -1) != NULL)
4692 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004693
Bram Moolenaar875feea2017-06-11 16:07:51 +02004694 if (dict_find(what, (char_u *)"title", -1) != NULL)
4695 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004696
Bram Moolenaar875feea2017-06-11 16:07:51 +02004697 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4698 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004699
Bram Moolenaar875feea2017-06-11 16:07:51 +02004700 if (dict_find(what, (char_u *)"context", -1) != NULL)
4701 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004702
4703 if (dict_find(what, (char_u *)"items", -1) != NULL)
4704 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004705 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004706
Bram Moolenaard823fa92016-08-12 16:29:27 +02004707 if (flags & QF_GETLIST_TITLE)
4708 {
4709 char_u *t;
4710 t = qi->qf_lists[qf_idx].qf_title;
4711 if (t == NULL)
4712 t = (char_u *)"";
4713 status = dict_add_nr_str(retdict, "title", 0L, t);
4714 }
4715 if ((status == OK) && (flags & QF_GETLIST_NR))
4716 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4717 if ((status == OK) && (flags & QF_GETLIST_WINID))
4718 {
4719 win_T *win;
4720 win = qf_find_win(qi);
4721 if (win != NULL)
4722 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4723 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004724 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4725 {
4726 list_T *l = list_alloc();
4727 if (l != NULL)
4728 {
4729 (void)get_errorlist(wp, qf_idx, l);
4730 dict_add_list(retdict, "items", l);
4731 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004732 else
4733 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004734 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004735
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004736 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4737 {
4738 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4739 {
4740 di = dictitem_alloc((char_u *)"context");
4741 if (di != NULL)
4742 {
4743 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004744 status = dict_add(retdict, di);
4745 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004746 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004747 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004748 else
4749 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004750 }
4751 else
4752 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4753 }
4754
Bram Moolenaard823fa92016-08-12 16:29:27 +02004755 return status;
4756}
4757
4758/*
4759 * Add list of entries to quickfix/location list. Each list entry is
4760 * a dictionary with item information.
4761 */
4762 static int
4763qf_add_entries(
4764 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004765 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004766 list_T *list,
4767 char_u *title,
4768 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004769{
4770 listitem_T *li;
4771 dict_T *d;
4772 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004773 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004774 long lnum;
4775 int col, nr;
4776 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004777#ifdef FEAT_WINDOWS
4778 qfline_T *old_last = NULL;
4779#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004780 int valid, status;
4781 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004782 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004783
Bram Moolenaara3921f42017-06-04 15:30:34 +02004784 if (action == ' ' || qf_idx == qi->qf_listcount)
4785 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004786 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004787 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004788 qf_idx = qi->qf_curlist;
4789 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004790#ifdef FEAT_WINDOWS
Bram Moolenaara3921f42017-06-04 15:30:34 +02004791 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004792 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004793 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004794#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004795 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004796 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004797 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004798 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004799 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004800
4801 for (li = list->lv_first; li != NULL; li = li->li_next)
4802 {
4803 if (li->li_tv.v_type != VAR_DICT)
4804 continue; /* Skip non-dict items */
4805
4806 d = li->li_tv.vval.v_dict;
4807 if (d == NULL)
4808 continue;
4809
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004810 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004811 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4812 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4813 col = (int)get_dict_number(d, (char_u *)"col");
4814 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4815 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004816 type = get_dict_string(d, (char_u *)"type", TRUE);
4817 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4818 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004819 if (text == NULL)
4820 text = vim_strsave((char_u *)"");
4821
4822 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004823 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004824 valid = FALSE;
4825
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004826 /* Mark entries with non-existing buffer number as not valid. Give the
4827 * error message only once. */
4828 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4829 {
4830 if (!did_bufnr_emsg)
4831 {
4832 did_bufnr_emsg = TRUE;
4833 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4834 }
4835 valid = FALSE;
4836 bufnum = 0;
4837 }
4838
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02004839 /* If the 'valid' field is present it overrules the detected value. */
4840 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
4841 valid = (int)get_dict_number(d, (char_u *)"valid");
4842
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004843 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004844 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004845 NULL, /* dir */
4846 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004847 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004848 text,
4849 lnum,
4850 col,
4851 vcol, /* vis_col */
4852 pattern, /* search pattern */
4853 nr,
4854 type == NULL ? NUL : *type,
4855 valid);
4856
4857 vim_free(filename);
4858 vim_free(pattern);
4859 vim_free(text);
4860 vim_free(type);
4861
4862 if (status == FAIL)
4863 {
4864 retval = FAIL;
4865 break;
4866 }
4867 }
4868
Bram Moolenaara3921f42017-06-04 15:30:34 +02004869 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004870 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004871 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004872 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02004873 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004874 if (action != 'a')
4875 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02004876 qi->qf_lists[qf_idx].qf_ptr =
4877 qi->qf_lists[qf_idx].qf_start;
4878 if (qi->qf_lists[qf_idx].qf_count > 0)
4879 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004880 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004881
4882#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004883 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004884 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004885#endif
4886
4887 return retval;
4888}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004889
4890 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02004891qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004892{
4893 dictitem_T *di;
4894 int retval = FAIL;
4895 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004896 int newlist = FALSE;
4897
4898 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4899 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004900
4901 qf_idx = qi->qf_curlist; /* default is the current list */
4902 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4903 {
4904 /* Use the specified quickfix/location list */
4905 if (di->di_tv.v_type == VAR_NUMBER)
4906 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004907 /* for zero use the current list */
4908 if (di->di_tv.vval.v_number != 0)
4909 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004910
Bram Moolenaar55b69262017-08-13 13:42:01 +02004911 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
4912 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004913 /*
4914 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02004915 * non-available list and add the new list at the end of the
4916 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004917 */
4918 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004919 qf_idx = qi->qf_listcount - 1;
4920 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004921 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004922 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004923 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004924 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004925 }
4926 else if (di->di_tv.v_type == VAR_STRING
4927 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004928 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02004929 if (qi->qf_listcount > 0)
4930 qf_idx = qi->qf_listcount - 1;
4931 else if (newlist)
4932 qf_idx = 0;
4933 else
4934 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004935 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004936 else
4937 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004938 }
4939
4940 if (newlist)
4941 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02004942 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02004943 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004944 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004945 }
4946
4947 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4948 {
4949 if (di->di_tv.v_type == VAR_STRING)
4950 {
4951 vim_free(qi->qf_lists[qf_idx].qf_title);
4952 qi->qf_lists[qf_idx].qf_title =
4953 get_dict_string(what, (char_u *)"title", TRUE);
4954 if (qf_idx == qi->qf_curlist)
4955 qf_update_win_titlevar(qi);
4956 retval = OK;
4957 }
4958 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004959 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
4960 {
4961 if (di->di_tv.v_type == VAR_LIST)
4962 {
4963 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
4964
4965 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
4966 title_save, action == ' ' ? 'a' : action);
4967 vim_free(title_save);
4968 }
4969 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004970
Bram Moolenaarae338332017-08-11 20:25:26 +02004971 if ((di = dict_find(what, (char_u *)"text", -1)) != NULL)
4972 {
4973 /* Only string and list values are supported */
4974 if ((di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
4975 || (di->di_tv.v_type == VAR_LIST
4976 && di->di_tv.vval.v_list != NULL))
4977 {
4978 if (action == 'r')
4979 qf_free_items(qi, qf_idx);
4980 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, p_efm,
4981 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4982 retval = OK;
4983 }
4984 else
4985 return FAIL;
4986 }
4987
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004988 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
4989 {
4990 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004991
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004992 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004993 ctx = alloc_tv();
4994 if (ctx != NULL)
4995 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004996 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004997 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004998 }
4999
Bram Moolenaard823fa92016-08-12 16:29:27 +02005000 return retval;
5001}
5002
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005003/*
5004 * Find the non-location list window with the specified location list.
5005 */
5006 static win_T *
5007find_win_with_ll(qf_info_T *qi)
5008{
5009 win_T *wp = NULL;
5010
5011 FOR_ALL_WINDOWS(wp)
5012 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5013 return wp;
5014
5015 return NULL;
5016}
5017
5018/*
5019 * Free the entire quickfix/location list stack.
5020 * If the quickfix/location list window is open, then clear it.
5021 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005022 static void
5023qf_free_stack(win_T *wp, qf_info_T *qi)
5024{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005025 win_T *qfwin = qf_find_win(qi);
5026 win_T *llwin = NULL;
5027 win_T *orig_wp = wp;
5028
5029 if (qfwin != NULL)
5030 {
5031 /* If the quickfix/location list window is open, then clear it */
5032 if (qi->qf_curlist < qi->qf_listcount)
5033 qf_free(qi, qi->qf_curlist);
5034 qf_update_buffer(qi, NULL);
5035 }
5036
5037 if (wp != NULL && IS_LL_WINDOW(wp))
5038 {
5039 /* If in the location list window, then use the non-location list
5040 * window with this location list (if present)
5041 */
5042 llwin = find_win_with_ll(qi);
5043 if (llwin != NULL)
5044 wp = llwin;
5045 }
5046
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005047 qf_free_all(wp);
5048 if (wp == NULL)
5049 {
5050 /* quickfix list */
5051 qi->qf_curlist = 0;
5052 qi->qf_listcount = 0;
5053 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005054 else if (IS_LL_WINDOW(orig_wp))
5055 {
5056 /* If the location list window is open, then create a new empty
5057 * location list */
5058 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005059
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005060 /* first free the list reference in the location list window */
5061 ll_free_all(&orig_wp->w_llist_ref);
5062
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005063 orig_wp->w_llist_ref = new_ll;
5064 if (llwin != NULL)
5065 {
5066 llwin->w_llist = new_ll;
5067 new_ll->qf_refcount++;
5068 }
5069 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005070}
5071
Bram Moolenaard823fa92016-08-12 16:29:27 +02005072/*
5073 * Populate the quickfix list with the items supplied in the list
5074 * of dictionaries. "title" will be copied to w:quickfix_title.
5075 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5076 */
5077 int
5078set_errorlist(
5079 win_T *wp,
5080 list_T *list,
5081 int action,
5082 char_u *title,
5083 dict_T *what)
5084{
5085 qf_info_T *qi = &ql_info;
5086 int retval = OK;
5087
5088 if (wp != NULL)
5089 {
5090 qi = ll_get_or_alloc_list(wp);
5091 if (qi == NULL)
5092 return FAIL;
5093 }
5094
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005095 if (action == 'f')
5096 {
5097 /* Free the entire quickfix or location list stack */
5098 qf_free_stack(wp, qi);
5099 }
5100 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005101 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005102 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005103 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005104
5105 return retval;
5106}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005107
5108 static int
5109mark_quickfix_ctx(qf_info_T *qi, int copyID)
5110{
5111 int i;
5112 int abort = FALSE;
5113 typval_T *ctx;
5114
5115 for (i = 0; i < LISTCOUNT && !abort; ++i)
5116 {
5117 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005118 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5119 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005120 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5121 }
5122
5123 return abort;
5124}
5125
5126/*
5127 * Mark the context of the quickfix list and the location lists (if present) as
5128 * "in use". So that garabage collection doesn't free the context.
5129 */
5130 int
5131set_ref_in_quickfix(int copyID)
5132{
5133 int abort = FALSE;
5134 tabpage_T *tp;
5135 win_T *win;
5136
5137 abort = mark_quickfix_ctx(&ql_info, copyID);
5138 if (abort)
5139 return abort;
5140
5141 FOR_ALL_TAB_WINDOWS(tp, win)
5142 {
5143 if (win->w_llist != NULL)
5144 {
5145 abort = mark_quickfix_ctx(win->w_llist, copyID);
5146 if (abort)
5147 return abort;
5148 }
5149 }
5150
5151 return abort;
5152}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005153#endif
5154
Bram Moolenaar81695252004-12-29 20:58:21 +00005155/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005156 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005157 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005158 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005159 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005160 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005161 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005162 */
5163 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005164ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005165{
5166 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005167 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005168#ifdef FEAT_AUTOCMD
5169 char_u *au_name = NULL;
5170#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005171
Bram Moolenaardb552d602006-03-23 22:59:57 +00005172 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5173 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005174 {
5175 qi = ll_get_or_alloc_list(curwin);
5176 if (qi == NULL)
5177 return;
5178 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005179
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005180#ifdef FEAT_AUTOCMD
5181 switch (eap->cmdidx)
5182 {
5183 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5184 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5185 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5186 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5187 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5188 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5189 default: break;
5190 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005191 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5192 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005193 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005194# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005195 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005196 return;
5197# endif
5198 }
5199#endif
5200
Bram Moolenaar86b68352004-12-27 21:59:20 +00005201 if (*eap->arg == NUL)
5202 buf = curbuf;
5203 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5204 buf = buflist_findnr(atoi((char *)eap->arg));
5205 if (buf == NULL)
5206 EMSG(_(e_invarg));
5207 else if (buf->b_ml.ml_mfp == NULL)
5208 EMSG(_("E681: Buffer is not loaded"));
5209 else
5210 {
5211 if (eap->addr_count == 0)
5212 {
5213 eap->line1 = 1;
5214 eap->line2 = buf->b_ml.ml_line_count;
5215 }
5216 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5217 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5218 EMSG(_(e_invrange));
5219 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005220 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005221 char_u *qf_title = *eap->cmdlinep;
5222
5223 if (buf->b_sfname)
5224 {
5225 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5226 (char *)qf_title, (char *)buf->b_sfname);
5227 qf_title = IObuff;
5228 }
5229
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005230 if (qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005231 (eap->cmdidx != CMD_caddbuffer
5232 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005233 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005234 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005235 {
5236#ifdef FEAT_AUTOCMD
5237 if (au_name != NULL)
5238 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5239 curbuf->b_fname, TRUE, curbuf);
5240#endif
5241 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5242 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5243 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005244 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005245 }
5246}
5247
Bram Moolenaar1e015462005-09-25 22:16:38 +00005248#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005249/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005250 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5251 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005252 */
5253 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005254ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005255{
5256 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005257 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005258#ifdef FEAT_AUTOCMD
5259 char_u *au_name = NULL;
5260#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005261
Bram Moolenaardb552d602006-03-23 22:59:57 +00005262 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5263 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005264 {
5265 qi = ll_get_or_alloc_list(curwin);
5266 if (qi == NULL)
5267 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005268 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005269
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005270#ifdef FEAT_AUTOCMD
5271 switch (eap->cmdidx)
5272 {
5273 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5274 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5275 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5276 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5277 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5278 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5279 default: break;
5280 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005281 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5282 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005283 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005284# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005285 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005286 return;
5287# endif
5288 }
5289#endif
5290
Bram Moolenaar4770d092006-01-12 23:22:24 +00005291 /* Evaluate the expression. When the result is a string or a list we can
5292 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005293 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005294 if (tv != NULL)
5295 {
5296 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5297 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5298 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005299 if (qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005300 (eap->cmdidx != CMD_caddexpr
5301 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005302 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5303 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005304 {
5305#ifdef FEAT_AUTOCMD
5306 if (au_name != NULL)
5307 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5308 curbuf->b_fname, TRUE, curbuf);
5309#endif
5310 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5311 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5312 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005313 }
5314 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005315 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005316 free_tv(tv);
5317 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005318}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005319#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005320
5321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 * ":helpgrep {pattern}"
5323 */
5324 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005325ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326{
5327 regmatch_T regmatch;
5328 char_u *save_cpo;
5329 char_u *p;
5330 int fcount;
5331 char_u **fnames;
5332 FILE *fd;
5333 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005335#ifdef FEAT_MULTI_LANG
5336 char_u *lang;
5337#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005338 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005339 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005340 int new_qi = FALSE;
5341 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005342#ifdef FEAT_AUTOCMD
5343 char_u *au_name = NULL;
5344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005346#ifdef FEAT_MULTI_LANG
5347 /* Check for a specified language */
5348 lang = check_help_lang(eap->arg);
5349#endif
5350
Bram Moolenaar73633f82012-01-20 13:39:07 +01005351#ifdef FEAT_AUTOCMD
5352 switch (eap->cmdidx)
5353 {
5354 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5355 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5356 default: break;
5357 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005358 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5359 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005360 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005361# ifdef FEAT_EVAL
5362 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005363 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005364# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005365 }
5366#endif
5367
5368 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5369 save_cpo = p_cpo;
5370 p_cpo = empty_option;
5371
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005372 if (eap->cmdidx == CMD_lhelpgrep)
5373 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005374 /* If the current window is a help window, then use it */
5375 if (bt_help(curwin->w_buffer))
5376 wp = curwin;
5377 else
5378 /* Find an existing help window */
5379 FOR_ALL_WINDOWS(wp)
5380 if (bt_help(wp->w_buffer))
5381 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005382
5383 if (wp == NULL) /* Help window not found */
5384 qi = NULL;
5385 else
5386 qi = wp->w_llist;
5387
5388 if (qi == NULL)
5389 {
5390 /* Allocate a new location list for help text matches */
5391 if ((qi = ll_new_list()) == NULL)
5392 return;
5393 new_qi = TRUE;
5394 }
5395 }
5396
Bram Moolenaaree85df32017-03-19 14:19:50 +01005397 /* Autocommands may change the list. Save it for later comparison */
5398 save_qi = qi;
5399
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5401 regmatch.rm_ic = FALSE;
5402 if (regmatch.regprog != NULL)
5403 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005404#ifdef FEAT_MBYTE
5405 vimconv_T vc;
5406
5407 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5408 * differs. */
5409 vc.vc_type = CONV_NONE;
5410 if (!enc_utf8)
5411 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5412#endif
5413
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005415 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
5417 /* Go through all directories in 'runtimepath' */
5418 p = p_rtp;
5419 while (*p != NUL && !got_int)
5420 {
5421 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5422
5423 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5424 add_pathsep(NameBuff);
5425 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5426 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5427 &fnames, EW_FILE|EW_SILENT) == OK
5428 && fcount > 0)
5429 {
5430 for (fi = 0; fi < fcount && !got_int; ++fi)
5431 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005432#ifdef FEAT_MULTI_LANG
5433 /* Skip files for a different language. */
5434 if (lang != NULL
5435 && STRNICMP(lang, fnames[fi]
5436 + STRLEN(fnames[fi]) - 3, 2) != 0
5437 && !(STRNICMP(lang, "en", 2) == 0
5438 && STRNICMP("txt", fnames[fi]
5439 + STRLEN(fnames[fi]) - 3, 3) == 0))
5440 continue;
5441#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005442 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 if (fd != NULL)
5444 {
5445 lnum = 1;
5446 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5447 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005448 char_u *line = IObuff;
5449#ifdef FEAT_MBYTE
5450 /* Convert a line if 'encoding' is not utf-8 and
5451 * the line contains a non-ASCII character. */
5452 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005453 && has_non_ascii(IObuff))
5454 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005455 line = string_convert(&vc, IObuff, NULL);
5456 if (line == NULL)
5457 line = IObuff;
5458 }
5459#endif
5460
5461 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005463 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
5465 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005466 while (l > 0 && line[l - 1] <= ' ')
5467 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005469 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005470 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 NULL, /* dir */
5472 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005473 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005474 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005476 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005477 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005478 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005479 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 0, /* nr */
5481 1, /* type */
5482 TRUE /* valid */
5483 ) == FAIL)
5484 {
5485 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005486#ifdef FEAT_MBYTE
5487 if (line != IObuff)
5488 vim_free(line);
5489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 break;
5491 }
5492 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005493#ifdef FEAT_MBYTE
5494 if (line != IObuff)
5495 vim_free(line);
5496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 ++lnum;
5498 line_breakcheck();
5499 }
5500 fclose(fd);
5501 }
5502 }
5503 FreeWild(fcount, fnames);
5504 }
5505 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005506
Bram Moolenaar473de612013-06-08 18:19:48 +02005507 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005508#ifdef FEAT_MBYTE
5509 if (vc.vc_type != CONV_NONE)
5510 convert_setup(&vc, NULL, NULL);
5511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005513 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5514 qi->qf_lists[qi->qf_curlist].qf_ptr =
5515 qi->qf_lists[qi->qf_curlist].qf_start;
5516 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 }
5518
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005519 if (p_cpo == empty_option)
5520 p_cpo = save_cpo;
5521 else
5522 /* Darn, some plugin changed the value. */
5523 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
5525#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005526 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527#endif
5528
Bram Moolenaar73633f82012-01-20 13:39:07 +01005529#ifdef FEAT_AUTOCMD
5530 if (au_name != NULL)
5531 {
5532 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5533 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005534 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005535 /* autocommands made "qi" invalid */
5536 return;
5537 }
5538#endif
5539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005541 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005542 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005543 else
5544 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005545
5546 if (eap->cmdidx == CMD_lhelpgrep)
5547 {
5548 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005549 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005550 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005551 {
5552 if (new_qi)
5553 ll_free_all(&qi);
5554 }
5555 else if (curwin->w_llist == NULL)
5556 curwin->w_llist = qi;
5557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558}
5559
5560#endif /* FEAT_QUICKFIX */