blob: 6142825e3be09e3dbdde235e372aa865d51aed0f [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 Moolenaara539f4f2017-08-30 20:33:55 +020061 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000062 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020063 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020065 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 int qf_index; /* current index in the error list */
67 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020068 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020069 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020070 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020071
72 struct dir_stack_T *qf_dir_stack;
73 char_u *qf_directory;
74 struct dir_stack_T *qf_file_stack;
75 char_u *qf_currfile;
76 int qf_multiline;
77 int qf_multiignore;
78 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010079 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000080} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020082/*
83 * Quickfix/Location list stack definition
84 * Contains a list of quickfix/location lists (qf_list_T)
85 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000086struct qf_info_S
87{
88 /*
89 * Count of references to this list. Used only for location lists.
90 * When a location list window reference this list, qf_refcount
91 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
92 * reaches 0, the list is freed.
93 */
94 int qf_refcount;
95 int qf_listcount; /* current number of lists */
96 int qf_curlist; /* current error list */
97 qf_list_T qf_lists[LISTCOUNT];
98};
99
100static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200101static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000103#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/*
106 * Structure used to hold the info of one part of 'errorformat'
107 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000108typedef struct efm_S efm_T;
109struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110{
111 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000112 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
114 char_u prefix; /* prefix of this format line: */
115 /* 'D' enter directory */
116 /* 'X' leave directory */
117 /* 'A' start of multi-line message */
118 /* 'E' error message */
119 /* 'W' warning message */
120 /* 'I' informational message */
121 /* 'C' continuation line */
122 /* 'Z' end of multi-line message */
123 /* 'G' general, unspecific message */
124 /* 'P' push file (partial) message */
125 /* 'Q' pop/quit file (partial) message */
126 /* 'O' overread (partial) message */
127 char_u flags; /* additional flags given in prefix */
128 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000129 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000130 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131};
132
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100133static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
134
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200135static 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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200137static 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 +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);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static win_T *qf_find_win(qf_info_T *qi);
147static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200148static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200150static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
153static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
154static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
155static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000157/* Quickfix window check helper macro */
158#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
159/* Location list window check helper macro */
160#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
161/*
162 * Return location list for window 'wp'
163 * For location list window, return the referenced location list
164 */
165#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200168 * Looking up a buffer can be slow if there are many. Remember the last one
169 * to make this a lot faster if there are multiple matches in the same file.
170 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200171static char_u *qf_last_bufname = NULL;
172static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200173
Bram Moolenaar3c097222017-12-21 20:54:49 +0100174static char *e_loc_list_changed =
175 N_("E926: Current location list was changed");
176
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200177/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000178 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200179 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 * Return -1 for error, number of errors for success.
181 */
182 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200183qf_init(win_T *wp,
184 char_u *efile,
185 char_u *errorformat,
186 int newlist, /* TRUE: start a new error list */
187 char_u *qf_title,
188 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000190 qf_info_T *qi = &ql_info;
191
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000193 {
194 qi = ll_get_or_alloc_list(wp);
195 if (qi == NULL)
196 return FAIL;
197 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000198
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200199 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
200 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000201}
202
203/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200204 * Maximum number of bytes allowed per line while reading a errorfile.
205 */
206#define LINE_MAXLEN 4096
207
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200208static struct fmtpattern
209{
210 char_u convchar;
211 char *pattern;
212} fmt_pat[FMT_PATTERNS] =
213 {
214 {'f', ".\\+"}, /* only used when at end */
215 {'n', "\\d\\+"},
216 {'l', "\\d\\+"},
217 {'c', "\\d\\+"},
218 {'t', "."},
219 {'m', ".\\+"},
220 {'r', ".*"},
221 {'p', "[- .]*"},
222 {'v', "\\d\\+"},
223 {'s', ".\\+"}
224 };
225
226/*
227 * Converts a 'errorformat' string to regular expression pattern
228 */
229 static int
230efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200231 char_u *efm,
232 int len,
233 efm_T *fmt_ptr,
234 char_u *regpat,
235 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200236{
237 char_u *ptr;
238 char_u *efmp;
239 char_u *srcptr;
240 int round;
241 int idx = 0;
242
243 /*
244 * Build regexp pattern from current 'errorformat' option
245 */
246 ptr = regpat;
247 *ptr++ = '^';
248 round = 0;
249 for (efmp = efm; efmp < efm + len; ++efmp)
250 {
251 if (*efmp == '%')
252 {
253 ++efmp;
254 for (idx = 0; idx < FMT_PATTERNS; ++idx)
255 if (fmt_pat[idx].convchar == *efmp)
256 break;
257 if (idx < FMT_PATTERNS)
258 {
259 if (fmt_ptr->addr[idx])
260 {
261 sprintf((char *)errmsg,
262 _("E372: Too many %%%c in format string"), *efmp);
263 EMSG(errmsg);
264 return -1;
265 }
266 if ((idx
267 && idx < 6
268 && vim_strchr((char_u *)"DXOPQ",
269 fmt_ptr->prefix) != NULL)
270 || (idx == 6
271 && vim_strchr((char_u *)"OPQ",
272 fmt_ptr->prefix) == NULL))
273 {
274 sprintf((char *)errmsg,
275 _("E373: Unexpected %%%c in format string"), *efmp);
276 EMSG(errmsg);
277 return -1;
278 }
279 fmt_ptr->addr[idx] = (char_u)++round;
280 *ptr++ = '\\';
281 *ptr++ = '(';
282#ifdef BACKSLASH_IN_FILENAME
283 if (*efmp == 'f')
284 {
285 /* Also match "c:" in the file name, even when
286 * checking for a colon next: "%f:".
287 * "\%(\a:\)\=" */
288 STRCPY(ptr, "\\%(\\a:\\)\\=");
289 ptr += 10;
290 }
291#endif
292 if (*efmp == 'f' && efmp[1] != NUL)
293 {
294 if (efmp[1] != '\\' && efmp[1] != '%')
295 {
296 /* A file name may contain spaces, but this isn't
297 * in "\f". For "%f:%l:%m" there may be a ":" in
298 * the file name. Use ".\{-1,}x" instead (x is
299 * the next character), the requirement that :999:
300 * follows should work. */
301 STRCPY(ptr, ".\\{-1,}");
302 ptr += 7;
303 }
304 else
305 {
306 /* File name followed by '\\' or '%': include as
307 * many file name chars as possible. */
308 STRCPY(ptr, "\\f\\+");
309 ptr += 4;
310 }
311 }
312 else
313 {
314 srcptr = (char_u *)fmt_pat[idx].pattern;
315 while ((*ptr = *srcptr++) != NUL)
316 ++ptr;
317 }
318 *ptr++ = '\\';
319 *ptr++ = ')';
320 }
321 else if (*efmp == '*')
322 {
323 if (*++efmp == '[' || *efmp == '\\')
324 {
325 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
326 {
327 if (efmp[1] == '^')
328 *ptr++ = *++efmp;
329 if (efmp < efm + len)
330 {
331 *ptr++ = *++efmp; /* could be ']' */
332 while (efmp < efm + len
333 && (*ptr++ = *++efmp) != ']')
334 /* skip */;
335 if (efmp == efm + len)
336 {
337 EMSG(_("E374: Missing ] in format string"));
338 return -1;
339 }
340 }
341 }
342 else if (efmp < efm + len) /* %*\D, %*\s etc. */
343 *ptr++ = *++efmp;
344 *ptr++ = '\\';
345 *ptr++ = '+';
346 }
347 else
348 {
349 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
350 sprintf((char *)errmsg,
351 _("E375: Unsupported %%%c in format string"), *efmp);
352 EMSG(errmsg);
353 return -1;
354 }
355 }
356 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
357 *ptr++ = *efmp; /* regexp magic characters */
358 else if (*efmp == '#')
359 *ptr++ = '*';
360 else if (*efmp == '>')
361 fmt_ptr->conthere = TRUE;
362 else if (efmp == efm + 1) /* analyse prefix */
363 {
364 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
365 fmt_ptr->flags = *efmp++;
366 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
367 fmt_ptr->prefix = *efmp;
368 else
369 {
370 sprintf((char *)errmsg,
371 _("E376: Invalid %%%c in format string prefix"), *efmp);
372 EMSG(errmsg);
373 return -1;
374 }
375 }
376 else
377 {
378 sprintf((char *)errmsg,
379 _("E377: Invalid %%%c in format string"), *efmp);
380 EMSG(errmsg);
381 return -1;
382 }
383 }
384 else /* copy normal character */
385 {
386 if (*efmp == '\\' && efmp + 1 < efm + len)
387 ++efmp;
388 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
389 *ptr++ = '\\'; /* escape regexp atoms */
390 if (*efmp)
391 *ptr++ = *efmp;
392 }
393 }
394 *ptr++ = '$';
395 *ptr = NUL;
396
397 return 0;
398}
399
400 static void
401free_efm_list(efm_T **efm_first)
402{
403 efm_T *efm_ptr;
404
405 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
406 {
407 *efm_first = efm_ptr->next;
408 vim_regfree(efm_ptr->prog);
409 vim_free(efm_ptr);
410 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100411 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200412}
413
414/* Parse 'errorformat' option */
415 static efm_T *
416parse_efm_option(char_u *efm)
417{
418 char_u *errmsg = NULL;
419 int errmsglen;
420 efm_T *fmt_ptr = NULL;
421 efm_T *fmt_first = NULL;
422 efm_T *fmt_last = NULL;
423 char_u *fmtstr = NULL;
424 int len;
425 int i;
426 int round;
427
428 errmsglen = CMDBUFFSIZE + 1;
429 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
430 if (errmsg == NULL)
431 goto parse_efm_end;
432
433 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200434 * Each part of the format string is copied and modified from errorformat
435 * to regex prog. Only a few % characters are allowed.
436 */
437
438 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200439 * Get some space to modify the format string into.
440 */
441 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
442 for (round = FMT_PATTERNS; round > 0; )
443 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200444#ifdef BACKSLASH_IN_FILENAME
445 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446#else
447 i += 2; /* "%f" can become two chars longer */
448#endif
449 if ((fmtstr = alloc(i)) == NULL)
450 goto parse_efm_error;
451
452 while (efm[0] != NUL)
453 {
454 /*
455 * Allocate a new eformat structure and put it at the end of the list
456 */
457 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
458 if (fmt_ptr == NULL)
459 goto parse_efm_error;
460 if (fmt_first == NULL) /* first one */
461 fmt_first = fmt_ptr;
462 else
463 fmt_last->next = fmt_ptr;
464 fmt_last = fmt_ptr;
465
466 /*
467 * Isolate one part in the 'errorformat' option
468 */
469 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
470 if (efm[len] == '\\' && efm[len + 1] != NUL)
471 ++len;
472
473 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
474 goto parse_efm_error;
475 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
476 goto parse_efm_error;
477 /*
478 * Advance to next part
479 */
480 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
481 }
482
483 if (fmt_first == NULL) /* nothing found */
484 EMSG(_("E378: 'errorformat' contains no pattern"));
485
486 goto parse_efm_end;
487
488parse_efm_error:
489 free_efm_list(&fmt_first);
490
491parse_efm_end:
492 vim_free(fmtstr);
493 vim_free(errmsg);
494
495 return fmt_first;
496}
497
Bram Moolenaare0d37972016-07-15 22:36:01 +0200498enum {
499 QF_FAIL = 0,
500 QF_OK = 1,
501 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200502 QF_NOMEM = 3,
503 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200504};
505
506typedef struct {
507 char_u *linebuf;
508 int linelen;
509 char_u *growbuf;
510 int growbufsiz;
511 FILE *fd;
512 typval_T *tv;
513 char_u *p_str;
514 listitem_T *p_li;
515 buf_T *buf;
516 linenr_T buflnum;
517 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100518 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200519} qfstate_T;
520
521 static char_u *
522qf_grow_linebuf(qfstate_T *state, int newsz)
523{
524 /*
525 * If the line exceeds LINE_MAXLEN exclude the last
526 * byte since it's not a NL character.
527 */
528 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
529 if (state->growbuf == NULL)
530 {
531 state->growbuf = alloc(state->linelen + 1);
532 if (state->growbuf == NULL)
533 return NULL;
534 state->growbufsiz = state->linelen;
535 }
536 else if (state->linelen > state->growbufsiz)
537 {
538 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
539 if (state->growbuf == NULL)
540 return NULL;
541 state->growbufsiz = state->linelen;
542 }
543 return state->growbuf;
544}
545
546/*
547 * Get the next string (separated by newline) from state->p_str.
548 */
549 static int
550qf_get_next_str_line(qfstate_T *state)
551{
552 /* Get the next line from the supplied string */
553 char_u *p_str = state->p_str;
554 char_u *p;
555 int len;
556
557 if (*p_str == NUL) /* Reached the end of the string */
558 return QF_END_OF_INPUT;
559
560 p = vim_strchr(p_str, '\n');
561 if (p != NULL)
562 len = (int)(p - p_str) + 1;
563 else
564 len = (int)STRLEN(p_str);
565
566 if (len > IOSIZE - 2)
567 {
568 state->linebuf = qf_grow_linebuf(state, len);
569 if (state->linebuf == NULL)
570 return QF_NOMEM;
571 }
572 else
573 {
574 state->linebuf = IObuff;
575 state->linelen = len;
576 }
577 vim_strncpy(state->linebuf, p_str, state->linelen);
578
579 /*
580 * Increment using len in order to discard the rest of the
581 * line if it exceeds LINE_MAXLEN.
582 */
583 p_str += len;
584 state->p_str = p_str;
585
586 return QF_OK;
587}
588
589/*
590 * Get the next string from state->p_Li.
591 */
592 static int
593qf_get_next_list_line(qfstate_T *state)
594{
595 listitem_T *p_li = state->p_li;
596 int len;
597
598 while (p_li != NULL
599 && (p_li->li_tv.v_type != VAR_STRING
600 || p_li->li_tv.vval.v_string == NULL))
601 p_li = p_li->li_next; /* Skip non-string items */
602
603 if (p_li == NULL) /* End of the list */
604 {
605 state->p_li = NULL;
606 return QF_END_OF_INPUT;
607 }
608
609 len = (int)STRLEN(p_li->li_tv.vval.v_string);
610 if (len > IOSIZE - 2)
611 {
612 state->linebuf = qf_grow_linebuf(state, len);
613 if (state->linebuf == NULL)
614 return QF_NOMEM;
615 }
616 else
617 {
618 state->linebuf = IObuff;
619 state->linelen = len;
620 }
621
622 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
623
624 state->p_li = p_li->li_next; /* next item */
625 return QF_OK;
626}
627
628/*
629 * Get the next string from state->buf.
630 */
631 static int
632qf_get_next_buf_line(qfstate_T *state)
633{
634 char_u *p_buf = NULL;
635 int len;
636
637 /* Get the next line from the supplied buffer */
638 if (state->buflnum > state->lnumlast)
639 return QF_END_OF_INPUT;
640
641 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
642 state->buflnum += 1;
643
644 len = (int)STRLEN(p_buf);
645 if (len > IOSIZE - 2)
646 {
647 state->linebuf = qf_grow_linebuf(state, len);
648 if (state->linebuf == NULL)
649 return QF_NOMEM;
650 }
651 else
652 {
653 state->linebuf = IObuff;
654 state->linelen = len;
655 }
656 vim_strncpy(state->linebuf, p_buf, state->linelen);
657
658 return QF_OK;
659}
660
661/*
662 * Get the next string from file state->fd.
663 */
664 static int
665qf_get_next_file_line(qfstate_T *state)
666{
667 int discard;
668 int growbuflen;
669
670 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
671 return QF_END_OF_INPUT;
672
673 discard = FALSE;
674 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200675 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200676 {
677 /*
678 * The current line exceeds IObuff, continue reading using
679 * growbuf until EOL or LINE_MAXLEN bytes is read.
680 */
681 if (state->growbuf == NULL)
682 {
683 state->growbufsiz = 2 * (IOSIZE - 1);
684 state->growbuf = alloc(state->growbufsiz);
685 if (state->growbuf == NULL)
686 return QF_NOMEM;
687 }
688
689 /* Copy the read part of the line, excluding null-terminator */
690 memcpy(state->growbuf, IObuff, IOSIZE - 1);
691 growbuflen = state->linelen;
692
693 for (;;)
694 {
695 if (fgets((char *)state->growbuf + growbuflen,
696 state->growbufsiz - growbuflen, state->fd) == NULL)
697 break;
698 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
699 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200700 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200701 break;
702 if (state->growbufsiz == LINE_MAXLEN)
703 {
704 discard = TRUE;
705 break;
706 }
707
708 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
709 ? 2 * state->growbufsiz : LINE_MAXLEN;
710 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
711 if (state->growbuf == NULL)
712 return QF_NOMEM;
713 }
714
715 while (discard)
716 {
717 /*
718 * The current line is longer than LINE_MAXLEN, continue
719 * reading but discard everything until EOL or EOF is
720 * reached.
721 */
722 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
723 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200724 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200725 break;
726 }
727
728 state->linebuf = state->growbuf;
729 state->linelen = growbuflen;
730 }
731 else
732 state->linebuf = IObuff;
733
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100734#ifdef FEAT_MBYTE
735 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200736 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
737 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100738 char_u *line;
739
740 line = string_convert(&state->vc, state->linebuf, &state->linelen);
741 if (line != NULL)
742 {
743 if (state->linelen < IOSIZE)
744 {
745 STRCPY(state->linebuf, line);
746 vim_free(line);
747 }
748 else
749 {
750 vim_free(state->growbuf);
751 state->linebuf = state->growbuf = line;
752 state->growbufsiz = state->linelen < LINE_MAXLEN
753 ? state->linelen : LINE_MAXLEN;
754 }
755 }
756 }
757#endif
758
Bram Moolenaare0d37972016-07-15 22:36:01 +0200759 return QF_OK;
760}
761
762/*
763 * Get the next string from a file/buffer/list/string.
764 */
765 static int
766qf_get_nextline(qfstate_T *state)
767{
768 int status = QF_FAIL;
769
770 if (state->fd == NULL)
771 {
772 if (state->tv != NULL)
773 {
774 if (state->tv->v_type == VAR_STRING)
775 /* Get the next line from the supplied string */
776 status = qf_get_next_str_line(state);
777 else if (state->tv->v_type == VAR_LIST)
778 /* Get the next line from the supplied list */
779 status = qf_get_next_list_line(state);
780 }
781 else
782 /* Get the next line from the supplied buffer */
783 status = qf_get_next_buf_line(state);
784 }
785 else
786 /* Get the next line from the supplied file */
787 status = qf_get_next_file_line(state);
788
789 if (status != QF_OK)
790 return status;
791
792 /* remove newline/CR from the line */
793 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200794 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200795 state->linebuf[state->linelen - 1] = NUL;
796#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200797 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
798 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200800 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200801
802#ifdef FEAT_MBYTE
803 remove_bom(state->linebuf);
804#endif
805
806 return QF_OK;
807}
808
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200809typedef struct {
810 char_u *namebuf;
811 char_u *errmsg;
812 int errmsglen;
813 long lnum;
814 int col;
815 char_u use_viscol;
816 char_u *pattern;
817 int enr;
818 int type;
819 int valid;
820} qffields_T;
821
822/*
823 * Parse a line and get the quickfix fields.
824 * Return the QF_ status.
825 */
826 static int
827qf_parse_line(
828 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200829 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200830 char_u *linebuf,
831 int linelen,
832 efm_T *fmt_first,
833 qffields_T *fields)
834{
835 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200836 char_u *ptr;
837 int len;
838 int i;
839 int idx = 0;
840 char_u *tail = NULL;
841 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200842 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200843
844 /* Always ignore case when looking for a matching error. */
845 regmatch.rm_ic = TRUE;
846
Bram Moolenaare333e792018-04-08 13:27:39 +0200847restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200848 /* If there was no %> item start at the first pattern */
849 if (fmt_start == NULL)
850 fmt_ptr = fmt_first;
851 else
852 {
853 fmt_ptr = fmt_start;
854 fmt_start = NULL;
855 }
856
857 /*
858 * Try to match each part of 'errorformat' until we find a complete
859 * match or no match.
860 */
861 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200862 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
863 {
864 int r;
865
866 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200867 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200868 continue;
869 fields->namebuf[0] = NUL;
870 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200871 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200872 fields->errmsg[0] = NUL;
873 fields->lnum = 0;
874 fields->col = 0;
875 fields->use_viscol = FALSE;
876 fields->enr = -1;
877 fields->type = 0;
878 tail = NULL;
879
880 regmatch.regprog = fmt_ptr->prog;
881 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
882 fmt_ptr->prog = regmatch.regprog;
883 if (r)
884 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200885 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200886 continue;
887 if (vim_strchr((char_u *)"EWI", idx) != NULL)
888 fields->type = idx;
889 else
890 fields->type = 0;
891 /*
892 * Extract error message data from matched line.
893 * We check for an actual submatch, because "\[" and "\]" in
894 * the 'errorformat' may cause the wrong submatch to be used.
895 */
896 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
897 {
898 int c;
899
900 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
901 continue;
902
903 /* Expand ~/file and $HOME/file to full path. */
904 c = *regmatch.endp[i];
905 *regmatch.endp[i] = NUL;
906 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
907 *regmatch.endp[i] = c;
908
909 if (vim_strchr((char_u *)"OPQ", idx) != NULL
910 && mch_getperm(fields->namebuf) == -1)
911 continue;
912 }
913 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
914 {
915 if (regmatch.startp[i] == NULL)
916 continue;
917 fields->enr = (int)atol((char *)regmatch.startp[i]);
918 }
919 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
920 {
921 if (regmatch.startp[i] == NULL)
922 continue;
923 fields->lnum = atol((char *)regmatch.startp[i]);
924 }
925 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
926 {
927 if (regmatch.startp[i] == NULL)
928 continue;
929 fields->col = (int)atol((char *)regmatch.startp[i]);
930 }
931 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
932 {
933 if (regmatch.startp[i] == NULL)
934 continue;
935 fields->type = *regmatch.startp[i];
936 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200937 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200938 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200939 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200940 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200941 /* linelen + null terminator */
942 if ((fields->errmsg = vim_realloc(fields->errmsg,
943 linelen + 1)) == NULL)
944 return QF_NOMEM;
945 fields->errmsglen = linelen + 1;
946 }
947 vim_strncpy(fields->errmsg, linebuf, linelen);
948 }
949 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
950 {
951 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
952 continue;
953 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200954 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200955 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200956 /* len + null terminator */
957 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
958 == NULL)
959 return QF_NOMEM;
960 fields->errmsglen = len + 1;
961 }
962 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
963 }
964 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
965 {
966 if (regmatch.startp[i] == NULL)
967 continue;
968 tail = regmatch.startp[i];
969 }
970 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
971 {
972 char_u *match_ptr;
973
974 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
975 continue;
976 fields->col = 0;
977 for (match_ptr = regmatch.startp[i];
978 match_ptr != regmatch.endp[i]; ++match_ptr)
979 {
980 ++fields->col;
981 if (*match_ptr == TAB)
982 {
983 fields->col += 7;
984 fields->col -= fields->col % 8;
985 }
986 }
987 ++fields->col;
988 fields->use_viscol = TRUE;
989 }
990 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
991 {
992 if (regmatch.startp[i] == NULL)
993 continue;
994 fields->col = (int)atol((char *)regmatch.startp[i]);
995 fields->use_viscol = TRUE;
996 }
997 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
998 {
999 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1000 continue;
1001 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1002 if (len > CMDBUFFSIZE - 5)
1003 len = CMDBUFFSIZE - 5;
1004 STRCPY(fields->pattern, "^\\V");
1005 STRNCAT(fields->pattern, regmatch.startp[i], len);
1006 fields->pattern[len + 3] = '\\';
1007 fields->pattern[len + 4] = '$';
1008 fields->pattern[len + 5] = NUL;
1009 }
1010 break;
1011 }
1012 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001013 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001014
1015 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1016 {
1017 if (fmt_ptr != NULL)
1018 {
1019 if (idx == 'D') /* enter directory */
1020 {
1021 if (*fields->namebuf == NUL)
1022 {
1023 EMSG(_("E379: Missing or empty directory name"));
1024 return QF_FAIL;
1025 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001026 qfl->qf_directory =
1027 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1028 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001029 return QF_FAIL;
1030 }
1031 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001032 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001033 }
1034 fields->namebuf[0] = NUL; /* no match found, remove file name */
1035 fields->lnum = 0; /* don't jump to this line */
1036 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001037 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001038 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001039 /* linelen + null terminator */
1040 if ((fields->errmsg = vim_realloc(fields->errmsg,
1041 linelen + 1)) == NULL)
1042 return QF_NOMEM;
1043 fields->errmsglen = linelen + 1;
1044 }
1045 /* copy whole line to error message */
1046 vim_strncpy(fields->errmsg, linebuf, linelen);
1047 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001048 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001049 }
1050 else if (fmt_ptr != NULL)
1051 {
1052 /* honor %> item */
1053 if (fmt_ptr->conthere)
1054 fmt_start = fmt_ptr;
1055
1056 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1057 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001058 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1059 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001060 }
1061 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1062 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001063 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001064 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001065 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001066
Bram Moolenaar9b457942016-10-09 16:10:05 +02001067 if (qfprev == NULL)
1068 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001069 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001070 {
1071 len = (int)STRLEN(qfprev->qf_text);
1072 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1073 == NULL)
1074 return QF_FAIL;
1075 STRCPY(ptr, qfprev->qf_text);
1076 vim_free(qfprev->qf_text);
1077 qfprev->qf_text = ptr;
1078 *(ptr += len) = '\n';
1079 STRCPY(++ptr, fields->errmsg);
1080 }
1081 if (qfprev->qf_nr == -1)
1082 qfprev->qf_nr = fields->enr;
1083 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1084 /* only printable chars allowed */
1085 qfprev->qf_type = fields->type;
1086
1087 if (!qfprev->qf_lnum)
1088 qfprev->qf_lnum = fields->lnum;
1089 if (!qfprev->qf_col)
1090 qfprev->qf_col = fields->col;
1091 qfprev->qf_viscol = fields->use_viscol;
1092 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001093 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1094 qfl->qf_directory,
1095 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001096 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001097 : qfl->qf_currfile != NULL && fields->valid
1098 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001099 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001100 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001101 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001102 line_breakcheck();
1103 return QF_IGNORE_LINE;
1104 }
1105 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1106 {
1107 /* global file names */
1108 fields->valid = FALSE;
1109 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1110 {
1111 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001112 qfl->qf_currfile =
1113 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001114 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001115 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001116 *fields->namebuf = NUL;
1117 if (tail && *tail)
1118 {
1119 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001120 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001121 goto restofline;
1122 }
1123 }
1124 }
1125 if (fmt_ptr->flags == '-') /* generally exclude this line */
1126 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001127 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001128 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001129 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001130 return QF_IGNORE_LINE;
1131 }
1132 }
1133
1134 return QF_OK;
1135}
1136
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001137/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001138 * Read the errorfile "efile" into memory, line by line, building the error
1139 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001140 * Alternative: when "efile" is NULL read errors from buffer "buf".
1141 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001142 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001143 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1144 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001145 * Return -1 for error, number of errors for success.
1146 */
1147 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001148qf_init_ext(
1149 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001150 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001151 char_u *efile,
1152 buf_T *buf,
1153 typval_T *tv,
1154 char_u *errorformat,
1155 int newlist, /* TRUE: start a new error list */
1156 linenr_T lnumfirst, /* first line number to use */
1157 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001158 char_u *qf_title,
1159 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001160{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001161 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001162 qfstate_T state;
1163 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001164 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001165 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001166 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001168 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001170 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001172 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001173 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001174
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001175 vim_memset(&state, 0, sizeof(state));
1176 vim_memset(&fields, 0, sizeof(fields));
1177#ifdef FEAT_MBYTE
1178 state.vc.vc_type = CONV_NONE;
1179 if (enc != NULL && *enc != NUL)
1180 convert_setup(&state.vc, enc, p_enc);
1181#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001182 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1183 fields.errmsglen = CMDBUFFSIZE + 1;
1184 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1185 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02001186 if (fields.namebuf == NULL || fields.errmsg == NULL
1187 || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 goto qf_init_end;
1189
Bram Moolenaare0d37972016-07-15 22:36:01 +02001190 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
1192 EMSG2(_(e_openerrf), efile);
1193 goto qf_init_end;
1194 }
1195
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001196 if (newlist || qf_idx == qi->qf_listcount)
1197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001199 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001200 qf_idx = qi->qf_curlist;
1201 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001202 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001203 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001204 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001205 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001206 if (qi->qf_lists[qf_idx].qf_count > 0)
1207 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001210 qfl = &qi->qf_lists[qf_idx];
1211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001213 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001214 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 else
1216 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001218 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001219 * If the errorformat didn't change between calls, then reuse the
1220 * previously parsed values.
1221 */
1222 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1223 {
1224 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001225 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001226 free_efm_list(&fmt_first);
1227
1228 /* parse the current 'efm' */
1229 fmt_first = parse_efm_option(efm);
1230 if (fmt_first != NULL)
1231 last_efm = vim_strsave(efm);
1232 }
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
1237 /*
1238 * got_int is reset here, because it was probably set when killing the
1239 * ":make" command, but we still want to read the errorfile then.
1240 */
1241 got_int = FALSE;
1242
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001243 if (tv != NULL)
1244 {
1245 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001246 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001247 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001248 state.p_li = tv->vval.v_list->lv_first;
1249 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001250 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001251 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001252 state.buflnum = lnumfirst;
1253 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 /*
1256 * Read the lines in the error file one by one.
1257 * Try to recognize one of the error formats in each line.
1258 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001259 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001261 /* Get the next line from a file/buffer/list/string */
1262 status = qf_get_nextline(&state);
1263 if (status == QF_NOMEM) /* memory alloc failure */
1264 goto qf_init_end;
1265 if (status == QF_END_OF_INPUT) /* end of input */
1266 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001268 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1269 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001270 if (status == QF_FAIL)
1271 goto error2;
1272 if (status == QF_NOMEM)
1273 goto qf_init_end;
1274 if (status == QF_IGNORE_LINE)
1275 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001277 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001278 qf_idx,
1279 qfl->qf_directory,
1280 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001281 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001282 : ((qfl->qf_currfile != NULL && fields.valid)
1283 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001284 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001285 fields.errmsg,
1286 fields.lnum,
1287 fields.col,
1288 fields.use_viscol,
1289 fields.pattern,
1290 fields.enr,
1291 fields.type,
1292 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 goto error2;
1294 line_breakcheck();
1295 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001296 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001298 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001300 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001301 qfl->qf_ptr = qfl->qf_start;
1302 qfl->qf_index = 1;
1303 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
1305 else
1306 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001307 qfl->qf_nonevalid = FALSE;
1308 if (qfl->qf_ptr == NULL)
1309 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001311 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001312 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001313 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 EMSG(_(e_readerrf));
1316error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001317 if (!adding)
1318 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001319 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001320 qf_free(qi, qi->qf_curlist);
1321 qi->qf_listcount--;
1322 if (qi->qf_curlist > 0)
1323 --qi->qf_curlist;
1324 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001325qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001326 if (state.fd != NULL)
1327 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001328 vim_free(fields.namebuf);
1329 vim_free(fields.errmsg);
1330 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001331 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001333 if (qf_idx == qi->qf_curlist)
1334 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001335#ifdef FEAT_MBYTE
1336 if (state.vc.vc_type != CONV_NONE)
1337 convert_setup(&state.vc, NULL, NULL);
1338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
1340 return retval;
1341}
1342
Bram Moolenaarfb604092014-07-23 15:55:00 +02001343 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001344qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001345{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001346 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001347
Bram Moolenaarfb604092014-07-23 15:55:00 +02001348 if (title != NULL)
1349 {
1350 char_u *p = alloc((int)STRLEN(title) + 2);
1351
Bram Moolenaara3921f42017-06-04 15:30:34 +02001352 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001353 if (p != NULL)
1354 sprintf((char *)p, ":%s", (char *)title);
1355 }
1356}
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001359 * Prepare for adding a new quickfix list. If the current list is in the
1360 * middle of the stack, then all the following lists are freed and then
1361 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 */
1363 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001364qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 int i;
1367
1368 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001369 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 * the current entry. This makes it possible to browse in a tree-like
1371 * way with ":grep'.
1372 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001373 while (qi->qf_listcount > qi->qf_curlist + 1)
1374 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
1376 /*
1377 * When the stack is full, remove to oldest entry
1378 * Otherwise, add a new entry.
1379 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001380 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001382 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001384 qi->qf_lists[i - 1] = qi->qf_lists[i];
1385 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001388 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001389 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001390 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001391 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392}
1393
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001394/*
1395 * Free a location list
1396 */
1397 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001398ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001399{
1400 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001401 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001402
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001403 qi = *pqi;
1404 if (qi == NULL)
1405 return;
1406 *pqi = NULL; /* Remove reference to this list */
1407
1408 qi->qf_refcount--;
1409 if (qi->qf_refcount < 1)
1410 {
1411 /* No references to this location list */
1412 for (i = 0; i < qi->qf_listcount; ++i)
1413 qf_free(qi, i);
1414 vim_free(qi);
1415 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001416}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001417
1418 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001419qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001420{
1421 int i;
1422 qf_info_T *qi = &ql_info;
1423
1424 if (wp != NULL)
1425 {
1426 /* location list */
1427 ll_free_all(&wp->w_llist);
1428 ll_free_all(&wp->w_llist_ref);
1429 }
1430 else
1431 /* quickfix list */
1432 for (i = 0; i < qi->qf_listcount; ++i)
1433 qf_free(qi, i);
1434}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436/*
1437 * Add an entry to the end of the list of errors.
1438 * Returns OK or FAIL.
1439 */
1440 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001441qf_add_entry(
1442 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001443 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001444 char_u *dir, /* optional directory name */
1445 char_u *fname, /* file name or NULL */
1446 int bufnum, /* buffer number or zero */
1447 char_u *mesg, /* message */
1448 long lnum, /* line number */
1449 int col, /* column */
1450 int vis_col, /* using visual column */
1451 char_u *pattern, /* search pattern */
1452 int nr, /* error number */
1453 int type, /* type character */
1454 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001456 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001457 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001459 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001461 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001462 {
1463 buf_T *buf = buflist_findnr(bufnum);
1464
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001465 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001466 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001467 buf->b_has_qf_entry |=
1468 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001469 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001470 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001471 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1473 {
1474 vim_free(qfp);
1475 return FAIL;
1476 }
1477 qfp->qf_lnum = lnum;
1478 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001479 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001480 if (pattern == NULL || *pattern == NUL)
1481 qfp->qf_pattern = NULL;
1482 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1483 {
1484 vim_free(qfp->qf_text);
1485 vim_free(qfp);
1486 return FAIL;
1487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 qfp->qf_nr = nr;
1489 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1490 type = 0;
1491 qfp->qf_type = type;
1492 qfp->qf_valid = valid;
1493
Bram Moolenaara3921f42017-06-04 15:30:34 +02001494 lastp = &qi->qf_lists[qf_idx].qf_last;
1495 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001496 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001498 qi->qf_lists[qf_idx].qf_start = qfp;
1499 qi->qf_lists[qf_idx].qf_ptr = qfp;
1500 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001501 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 }
1503 else
1504 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001505 qfp->qf_prev = *lastp;
1506 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001508 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001510 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001511 ++qi->qf_lists[qf_idx].qf_count;
1512 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001513 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001515 qi->qf_lists[qf_idx].qf_index =
1516 qi->qf_lists[qf_idx].qf_count;
1517 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
1519
1520 return OK;
1521}
1522
1523/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001524 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001525 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001526 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001527ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001528{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001529 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001530
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001531 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1532 if (qi != NULL)
1533 {
1534 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1535 qi->qf_refcount++;
1536 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001537
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001538 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001539}
1540
1541/*
1542 * Return the location list for window 'wp'.
1543 * If not present, allocate a location list
1544 */
1545 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001546ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001547{
1548 if (IS_LL_WINDOW(wp))
1549 /* For a location list window, use the referenced location list */
1550 return wp->w_llist_ref;
1551
1552 /*
1553 * For a non-location list window, w_llist_ref should not point to a
1554 * location list.
1555 */
1556 ll_free_all(&wp->w_llist_ref);
1557
1558 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001559 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001560 return wp->w_llist;
1561}
1562
1563/*
1564 * Copy the location list from window "from" to window "to".
1565 */
1566 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001567copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001568{
1569 qf_info_T *qi;
1570 int idx;
1571 int i;
1572
1573 /*
1574 * When copying from a location list window, copy the referenced
1575 * location list. For other windows, copy the location list for
1576 * that window.
1577 */
1578 if (IS_LL_WINDOW(from))
1579 qi = from->w_llist_ref;
1580 else
1581 qi = from->w_llist;
1582
1583 if (qi == NULL) /* no location list to copy */
1584 return;
1585
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001586 /* allocate a new location list */
1587 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001588 return;
1589
1590 to->w_llist->qf_listcount = qi->qf_listcount;
1591
1592 /* Copy the location lists one at a time */
1593 for (idx = 0; idx < qi->qf_listcount; idx++)
1594 {
1595 qf_list_T *from_qfl;
1596 qf_list_T *to_qfl;
1597
1598 to->w_llist->qf_curlist = idx;
1599
1600 from_qfl = &qi->qf_lists[idx];
1601 to_qfl = &to->w_llist->qf_lists[idx];
1602
1603 /* Some of the fields are populated by qf_add_entry() */
1604 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1605 to_qfl->qf_count = 0;
1606 to_qfl->qf_index = 0;
1607 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001608 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001609 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001610 if (from_qfl->qf_title != NULL)
1611 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1612 else
1613 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001614 if (from_qfl->qf_ctx != NULL)
1615 {
1616 to_qfl->qf_ctx = alloc_tv();
1617 if (to_qfl->qf_ctx != NULL)
1618 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1619 }
1620 else
1621 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001622
1623 if (from_qfl->qf_count)
1624 {
1625 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001626 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001627
1628 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001629 for (i = 0, from_qfp = from_qfl->qf_start;
1630 i < from_qfl->qf_count && from_qfp != NULL;
1631 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001632 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001633 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001634 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001635 NULL,
1636 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001637 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001638 from_qfp->qf_text,
1639 from_qfp->qf_lnum,
1640 from_qfp->qf_col,
1641 from_qfp->qf_viscol,
1642 from_qfp->qf_pattern,
1643 from_qfp->qf_nr,
1644 0,
1645 from_qfp->qf_valid) == FAIL)
1646 {
1647 qf_free_all(to);
1648 return;
1649 }
1650 /*
1651 * qf_add_entry() will not set the qf_num field, as the
1652 * directory and file names are not supplied. So the qf_fnum
1653 * field is copied here.
1654 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001655 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001656 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1657 prevp->qf_type = from_qfp->qf_type; /* error type */
1658 if (from_qfl->qf_ptr == from_qfp)
1659 to_qfl->qf_ptr = prevp; /* current location */
1660 }
1661 }
1662
1663 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1664
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001665 /* Assign a new ID for the location list */
1666 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001667 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001668
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001669 /* When no valid entries are present in the list, qf_ptr points to
1670 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001671 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001672 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001673 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001674 to_qfl->qf_index = 1;
1675 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001676 }
1677
1678 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1679}
1680
1681/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001682 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001683 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 */
1685 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001686qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687{
Bram Moolenaar82404332016-07-10 17:00:38 +02001688 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001689 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001690 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (fname == NULL || *fname == NUL) /* no file name */
1693 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaare60acc12011-05-10 16:41:25 +02001695#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001696 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001697#endif
1698#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001699 if (directory != NULL)
1700 slash_adjust(directory);
1701 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001702#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001703 if (directory != NULL && !vim_isAbsName(fname)
1704 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1705 {
1706 /*
1707 * Here we check if the file really exists.
1708 * This should normally be true, but if make works without
1709 * "leaving directory"-messages we might have missed a
1710 * directory change.
1711 */
1712 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001715 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001716 if (directory)
1717 ptr = concat_fnames(directory, fname, TRUE);
1718 else
1719 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001721 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001722 bufname = ptr;
1723 }
1724 else
1725 bufname = fname;
1726
1727 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001728 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001729 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001730 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001731 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001733 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001734 {
1735 vim_free(qf_last_bufname);
1736 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1737 if (bufname == ptr)
1738 qf_last_bufname = bufname;
1739 else
1740 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001741 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001742 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001743 if (buf == NULL)
1744 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001745
Bram Moolenaarc1542742016-07-20 21:44:37 +02001746 buf->b_has_qf_entry =
1747 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001748 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749}
1750
1751/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001752 * Push dirbuf onto the directory stack and return pointer to actual dir or
1753 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 */
1755 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001756qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 struct dir_stack_T *ds_new;
1759 struct dir_stack_T *ds_ptr;
1760
1761 /* allocate new stack element and hook it in */
1762 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1763 if (ds_new == NULL)
1764 return NULL;
1765
1766 ds_new->next = *stackptr;
1767 *stackptr = ds_new;
1768
1769 /* store directory on the stack */
1770 if (vim_isAbsName(dirbuf)
1771 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001772 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 (*stackptr)->dirname = vim_strsave(dirbuf);
1774 else
1775 {
1776 /* Okay we don't have an absolute path.
1777 * dirbuf must be a subdir of one of the directories on the stack.
1778 * Let's search...
1779 */
1780 ds_new = (*stackptr)->next;
1781 (*stackptr)->dirname = NULL;
1782 while (ds_new)
1783 {
1784 vim_free((*stackptr)->dirname);
1785 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1786 TRUE);
1787 if (mch_isdir((*stackptr)->dirname) == TRUE)
1788 break;
1789
1790 ds_new = ds_new->next;
1791 }
1792
1793 /* clean up all dirs we already left */
1794 while ((*stackptr)->next != ds_new)
1795 {
1796 ds_ptr = (*stackptr)->next;
1797 (*stackptr)->next = (*stackptr)->next->next;
1798 vim_free(ds_ptr->dirname);
1799 vim_free(ds_ptr);
1800 }
1801
1802 /* Nothing found -> it must be on top level */
1803 if (ds_new == NULL)
1804 {
1805 vim_free((*stackptr)->dirname);
1806 (*stackptr)->dirname = vim_strsave(dirbuf);
1807 }
1808 }
1809
1810 if ((*stackptr)->dirname != NULL)
1811 return (*stackptr)->dirname;
1812 else
1813 {
1814 ds_ptr = *stackptr;
1815 *stackptr = (*stackptr)->next;
1816 vim_free(ds_ptr);
1817 return NULL;
1818 }
1819}
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
1822 * pop dirbuf from the directory stack and return previous directory or NULL if
1823 * stack is empty
1824 */
1825 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001826qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 struct dir_stack_T *ds_ptr;
1829
1830 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1831 * What to do if it isn't? */
1832
1833 /* pop top element and free it */
1834 if (*stackptr != NULL)
1835 {
1836 ds_ptr = *stackptr;
1837 *stackptr = (*stackptr)->next;
1838 vim_free(ds_ptr->dirname);
1839 vim_free(ds_ptr);
1840 }
1841
1842 /* return NEW top element as current dir or NULL if stack is empty*/
1843 return *stackptr ? (*stackptr)->dirname : NULL;
1844}
1845
1846/*
1847 * clean up directory stack
1848 */
1849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001850qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851{
1852 struct dir_stack_T *ds_ptr;
1853
1854 while ((ds_ptr = *stackptr) != NULL)
1855 {
1856 *stackptr = (*stackptr)->next;
1857 vim_free(ds_ptr->dirname);
1858 vim_free(ds_ptr);
1859 }
1860}
1861
1862/*
1863 * Check in which directory of the directory stack the given file can be
1864 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001865 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 * Cleans up intermediate directory entries.
1867 *
1868 * TODO: How to solve the following problem?
1869 * If we have the this directory tree:
1870 * ./
1871 * ./aa
1872 * ./aa/bb
1873 * ./bb
1874 * ./bb/x.c
1875 * and make says:
1876 * making all in aa
1877 * making all in bb
1878 * x.c:9: Error
1879 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1880 * qf_guess_filepath will return NULL.
1881 */
1882 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001883qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884{
1885 struct dir_stack_T *ds_ptr;
1886 struct dir_stack_T *ds_tmp;
1887 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001888 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001891 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 return NULL;
1893
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001894 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 fullname = NULL;
1896 while (ds_ptr)
1897 {
1898 vim_free(fullname);
1899 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1900
1901 /* If concat_fnames failed, just go on. The worst thing that can happen
1902 * is that we delete the entire stack.
1903 */
1904 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1905 break;
1906
1907 ds_ptr = ds_ptr->next;
1908 }
1909
1910 vim_free(fullname);
1911
1912 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001913 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001915 ds_tmp = qfl->qf_dir_stack->next;
1916 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 vim_free(ds_tmp->dirname);
1918 vim_free(ds_tmp);
1919 }
1920
1921 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922}
1923
1924/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01001925 * Returns TRUE if a quickfix/location list with the given identifier exists.
1926 */
1927 static int
1928qflist_valid (win_T *wp, int_u qf_id)
1929{
1930 qf_info_T *qi = &ql_info;
1931 int i;
1932
1933 if (wp != NULL)
1934 {
1935 qi = GET_LOC_LIST(wp); /* Location list */
1936 if (qi == NULL)
1937 return FALSE;
1938 }
1939
1940 for (i = 0; i < qi->qf_listcount; ++i)
1941 if (qi->qf_lists[i].qf_id == qf_id)
1942 return TRUE;
1943
1944 return FALSE;
1945}
1946
1947/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001948 * When loading a file from the quickfix, the auto commands may modify it.
1949 * This may invalidate the current quickfix entry. This function checks
1950 * whether a entry is still present in the quickfix.
1951 * Similar to location list.
1952 */
1953 static int
1954is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1955{
1956 qf_list_T *qfl;
1957 qfline_T *qfp;
1958 int i;
1959
1960 qfl = &qi->qf_lists[qi->qf_curlist];
1961
1962 /* Search for the entry in the current list */
1963 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1964 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001965 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001966 break;
1967
1968 if (i == qfl->qf_count) /* Entry is not found */
1969 return FALSE;
1970
1971 return TRUE;
1972}
1973
1974/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001975 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001976 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001977 */
1978 static qfline_T *
1979get_next_valid_entry(
1980 qf_info_T *qi,
1981 qfline_T *qf_ptr,
1982 int *qf_index,
1983 int dir)
1984{
1985 int idx;
1986 int old_qf_fnum;
1987
1988 idx = *qf_index;
1989 old_qf_fnum = qf_ptr->qf_fnum;
1990
1991 do
1992 {
1993 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
1994 || qf_ptr->qf_next == NULL)
1995 return NULL;
1996 ++idx;
1997 qf_ptr = qf_ptr->qf_next;
1998 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1999 && !qf_ptr->qf_valid)
2000 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2001
2002 *qf_index = idx;
2003 return qf_ptr;
2004}
2005
2006/*
2007 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002008 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002009 */
2010 static qfline_T *
2011get_prev_valid_entry(
2012 qf_info_T *qi,
2013 qfline_T *qf_ptr,
2014 int *qf_index,
2015 int dir)
2016{
2017 int idx;
2018 int old_qf_fnum;
2019
2020 idx = *qf_index;
2021 old_qf_fnum = qf_ptr->qf_fnum;
2022
2023 do
2024 {
2025 if (idx == 1 || qf_ptr->qf_prev == NULL)
2026 return NULL;
2027 --idx;
2028 qf_ptr = qf_ptr->qf_prev;
2029 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2030 && !qf_ptr->qf_valid)
2031 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2032
2033 *qf_index = idx;
2034 return qf_ptr;
2035}
2036
2037/*
2038 * Get the n'th (errornr) previous/next valid entry from the current entry in
2039 * the quickfix list.
2040 * dir == FORWARD or FORWARD_FILE: next valid entry
2041 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2042 */
2043 static qfline_T *
2044get_nth_valid_entry(
2045 qf_info_T *qi,
2046 int errornr,
2047 qfline_T *qf_ptr,
2048 int *qf_index,
2049 int dir)
2050{
2051 qfline_T *prev_qf_ptr;
2052 int prev_index;
2053 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2054 char_u *err = e_no_more_items;
2055
2056 while (errornr--)
2057 {
2058 prev_qf_ptr = qf_ptr;
2059 prev_index = *qf_index;
2060
2061 if (dir == FORWARD || dir == FORWARD_FILE)
2062 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2063 else
2064 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2065 if (qf_ptr == NULL)
2066 {
2067 qf_ptr = prev_qf_ptr;
2068 *qf_index = prev_index;
2069 if (err != NULL)
2070 {
2071 EMSG(_(err));
2072 return NULL;
2073 }
2074 break;
2075 }
2076
2077 err = NULL;
2078 }
2079
2080 return qf_ptr;
2081}
2082
2083/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002084 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002085 */
2086 static qfline_T *
2087get_nth_entry(
2088 qf_info_T *qi,
2089 int errornr,
2090 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002091 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002092{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002093 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002094
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002095 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002096 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2097 {
2098 --qf_idx;
2099 qf_ptr = qf_ptr->qf_prev;
2100 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002101 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002102 while (errornr > qf_idx &&
2103 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2104 qf_ptr->qf_next != NULL)
2105 {
2106 ++qf_idx;
2107 qf_ptr = qf_ptr->qf_next;
2108 }
2109
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002110 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002111 return qf_ptr;
2112}
2113
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002114/*
2115 * Find a help window or open one.
2116 */
2117 static int
2118jump_to_help_window(qf_info_T *qi, int *opened_window)
2119{
2120 win_T *wp;
2121 int flags;
2122
2123 if (cmdmod.tab != 0)
2124 wp = NULL;
2125 else
2126 FOR_ALL_WINDOWS(wp)
2127 if (bt_help(wp->w_buffer))
2128 break;
2129 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2130 win_enter(wp, TRUE);
2131 else
2132 {
2133 /*
2134 * Split off help window; put it at far top if no position
2135 * specified, the current window is vertically split and narrow.
2136 */
2137 flags = WSP_HELP;
2138 if (cmdmod.split == 0 && curwin->w_width != Columns
2139 && curwin->w_width < 80)
2140 flags |= WSP_TOP;
2141 if (qi != &ql_info)
2142 flags |= WSP_NEWLOC; /* don't copy the location list */
2143
2144 if (win_split(0, flags) == FAIL)
2145 return FAIL;
2146
2147 *opened_window = TRUE;
2148
2149 if (curwin->w_height < p_hh)
2150 win_setheight((int)p_hh);
2151
2152 if (qi != &ql_info) /* not a quickfix list */
2153 {
2154 /* The new window should use the supplied location list */
2155 curwin->w_llist = qi;
2156 qi->qf_refcount++;
2157 }
2158 }
2159
2160 if (!p_im)
2161 restart_edit = 0; /* don't want insert mode in help file */
2162
2163 return OK;
2164}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002165
2166/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002167 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2168 * If the file is already opened in a window, jump to it.
2169 */
2170 static int
2171qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2172{
2173 win_T *usable_win_ptr = NULL;
2174 int usable_win;
2175 qf_info_T *ll_ref;
2176 int flags;
2177 win_T *win;
2178 win_T *altwin;
2179
2180 usable_win = 0;
2181
2182 ll_ref = curwin->w_llist_ref;
2183 if (ll_ref != NULL)
2184 {
2185 /* Find a window using the same location list that is not a
2186 * quickfix window. */
2187 FOR_ALL_WINDOWS(usable_win_ptr)
2188 if (usable_win_ptr->w_llist == ll_ref
2189 && !bt_quickfix(usable_win_ptr->w_buffer))
2190 {
2191 usable_win = 1;
2192 break;
2193 }
2194 }
2195
2196 if (!usable_win)
2197 {
2198 /* Locate a window showing a normal buffer */
2199 FOR_ALL_WINDOWS(win)
2200 if (win->w_buffer->b_p_bt[0] == NUL)
2201 {
2202 usable_win = 1;
2203 break;
2204 }
2205 }
2206
2207 /*
2208 * If no usable window is found and 'switchbuf' contains "usetab"
2209 * then search in other tabs.
2210 */
2211 if (!usable_win && (swb_flags & SWB_USETAB))
2212 {
2213 tabpage_T *tp;
2214 win_T *wp;
2215
2216 FOR_ALL_TAB_WINDOWS(tp, wp)
2217 {
2218 if (wp->w_buffer->b_fnum == qf_fnum)
2219 {
2220 goto_tabpage_win(tp, wp);
2221 usable_win = 1;
2222 goto win_found;
2223 }
2224 }
2225 }
2226win_found:
2227
2228 /*
2229 * If there is only one window and it is the quickfix window, create a
2230 * new one above the quickfix window.
2231 */
2232 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2233 {
2234 flags = WSP_ABOVE;
2235 if (ll_ref != NULL)
2236 flags |= WSP_NEWLOC;
2237 if (win_split(0, flags) == FAIL)
2238 return FAIL; /* not enough room for window */
2239 *opened_window = TRUE; /* close it when fail */
2240 p_swb = empty_option; /* don't split again */
2241 swb_flags = 0;
2242 RESET_BINDING(curwin);
2243 if (ll_ref != NULL)
2244 {
2245 /* The new window should use the location list from the
2246 * location list window */
2247 curwin->w_llist = ll_ref;
2248 ll_ref->qf_refcount++;
2249 }
2250 }
2251 else
2252 {
2253 if (curwin->w_llist_ref != NULL)
2254 {
2255 /* In a location window */
2256 win = usable_win_ptr;
2257 if (win == NULL)
2258 {
2259 /* Find the window showing the selected file */
2260 FOR_ALL_WINDOWS(win)
2261 if (win->w_buffer->b_fnum == qf_fnum)
2262 break;
2263 if (win == NULL)
2264 {
2265 /* Find a previous usable window */
2266 win = curwin;
2267 do
2268 {
2269 if (win->w_buffer->b_p_bt[0] == NUL)
2270 break;
2271 if (win->w_prev == NULL)
2272 win = lastwin; /* wrap around the top */
2273 else
2274 win = win->w_prev; /* go to previous window */
2275 } while (win != curwin);
2276 }
2277 }
2278 win_goto(win);
2279
2280 /* If the location list for the window is not set, then set it
2281 * to the location list from the location window */
2282 if (win->w_llist == NULL)
2283 {
2284 win->w_llist = ll_ref;
2285 ll_ref->qf_refcount++;
2286 }
2287 }
2288 else
2289 {
2290
2291 /*
2292 * Try to find a window that shows the right buffer.
2293 * Default to the window just above the quickfix buffer.
2294 */
2295 win = curwin;
2296 altwin = NULL;
2297 for (;;)
2298 {
2299 if (win->w_buffer->b_fnum == qf_fnum)
2300 break;
2301 if (win->w_prev == NULL)
2302 win = lastwin; /* wrap around the top */
2303 else
2304 win = win->w_prev; /* go to previous window */
2305
2306 if (IS_QF_WINDOW(win))
2307 {
2308 /* Didn't find it, go to the window before the quickfix
2309 * window. */
2310 if (altwin != NULL)
2311 win = altwin;
2312 else if (curwin->w_prev != NULL)
2313 win = curwin->w_prev;
2314 else
2315 win = curwin->w_next;
2316 break;
2317 }
2318
2319 /* Remember a usable window. */
2320 if (altwin == NULL && !win->w_p_pvw
2321 && win->w_buffer->b_p_bt[0] == NUL)
2322 altwin = win;
2323 }
2324
2325 win_goto(win);
2326 }
2327 }
2328
2329 return OK;
2330}
2331
2332/*
2333 * Edit the selected file or help file.
2334 */
2335 static int
2336qf_jump_edit_buffer(
2337 qf_info_T *qi,
2338 qfline_T *qf_ptr,
2339 int forceit,
2340 win_T *oldwin,
2341 int *opened_window,
2342 int *abort)
2343{
2344 int retval = OK;
2345
2346 if (qf_ptr->qf_type == 1)
2347 {
2348 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2349 * set b_p_ro flag). */
2350 if (!can_abandon(curbuf, forceit))
2351 {
2352 no_write_message();
2353 retval = FALSE;
2354 }
2355 else
2356 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2357 ECMD_HIDE + ECMD_SET_HELP,
2358 oldwin == curwin ? curwin : NULL);
2359 }
2360 else
2361 {
2362 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002363 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002364
2365 retval = buflist_getfile(qf_ptr->qf_fnum,
2366 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002367
2368 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002369 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002370 /*
2371 * Location list. Check whether the associated window is still
2372 * present and the list is still valid.
2373 */
2374 if (!win_valid_any_tab(oldwin))
2375 {
2376 EMSG(_("E924: Current window was closed"));
2377 *abort = TRUE;
2378 *opened_window = FALSE;
2379 }
2380 else if (!qflist_valid(oldwin, save_qfid))
2381 {
2382 EMSG(_(e_loc_list_changed));
2383 *abort = TRUE;
2384 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002385 }
2386 else if (old_qf_curlist != qi->qf_curlist
2387 || !is_qf_entry_present(qi, qf_ptr))
2388 {
2389 if (qi == &ql_info)
2390 EMSG(_("E925: Current quickfix was changed"));
2391 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002392 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002393 *abort = TRUE;
2394 }
2395
2396 if (*abort)
2397 retval = FALSE;
2398 }
2399
2400 return retval;
2401}
2402
2403/*
2404 * Goto the error line in the current file using either line/column number or a
2405 * search pattern.
2406 */
2407 static void
2408qf_jump_goto_line(
2409 linenr_T qf_lnum,
2410 int qf_col,
2411 char_u qf_viscol,
2412 char_u *qf_pattern)
2413{
2414 linenr_T i;
2415 char_u *line;
2416 colnr_T screen_col;
2417 colnr_T char_col;
2418
2419 if (qf_pattern == NULL)
2420 {
2421 /*
2422 * Go to line with error, unless qf_lnum is 0.
2423 */
2424 i = qf_lnum;
2425 if (i > 0)
2426 {
2427 if (i > curbuf->b_ml.ml_line_count)
2428 i = curbuf->b_ml.ml_line_count;
2429 curwin->w_cursor.lnum = i;
2430 }
2431 if (qf_col > 0)
2432 {
2433 curwin->w_cursor.col = qf_col - 1;
2434#ifdef FEAT_VIRTUALEDIT
2435 curwin->w_cursor.coladd = 0;
2436#endif
2437 if (qf_viscol == TRUE)
2438 {
2439 /*
2440 * Check each character from the beginning of the error
2441 * line up to the error column. For each tab character
2442 * found, reduce the error column value by the length of
2443 * a tab character.
2444 */
2445 line = ml_get_curline();
2446 screen_col = 0;
2447 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2448 {
2449 if (*line == NUL)
2450 break;
2451 if (*line++ == '\t')
2452 {
2453 curwin->w_cursor.col -= 7 - (screen_col % 8);
2454 screen_col += 8 - (screen_col % 8);
2455 }
2456 else
2457 ++screen_col;
2458 }
2459 }
2460 check_cursor();
2461 }
2462 else
2463 beginline(BL_WHITE | BL_FIX);
2464 }
2465 else
2466 {
2467 pos_T save_cursor;
2468
2469 /* Move the cursor to the first line in the buffer */
2470 save_cursor = curwin->w_cursor;
2471 curwin->w_cursor.lnum = 0;
2472 if (!do_search(NULL, '/', qf_pattern, (long)1,
2473 SEARCH_KEEP, NULL, NULL))
2474 curwin->w_cursor = save_cursor;
2475 }
2476}
2477
2478/*
2479 * Display quickfix list index and size message
2480 */
2481 static void
2482qf_jump_print_msg(
2483 qf_info_T *qi,
2484 int qf_index,
2485 qfline_T *qf_ptr,
2486 buf_T *old_curbuf,
2487 linenr_T old_lnum)
2488{
2489 linenr_T i;
2490 int len;
2491
2492 /* Update the screen before showing the message, unless the screen
2493 * scrolled up. */
2494 if (!msg_scrolled)
2495 update_topline_redraw();
2496 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2497 qi->qf_lists[qi->qf_curlist].qf_count,
2498 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2499 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2500 /* Add the message, skipping leading whitespace and newlines. */
2501 len = (int)STRLEN(IObuff);
2502 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2503
2504 /* Output the message. Overwrite to avoid scrolling when the 'O'
2505 * flag is present in 'shortmess'; But when not jumping, print the
2506 * whole message. */
2507 i = msg_scroll;
2508 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2509 msg_scroll = TRUE;
2510 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2511 msg_scroll = FALSE;
2512 msg_attr_keep(IObuff, 0, TRUE);
2513 msg_scroll = i;
2514}
2515
2516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 * jump to a quickfix line
2518 * if dir == FORWARD go "errornr" valid entries forward
2519 * if dir == BACKWARD go "errornr" valid entries backward
2520 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2521 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2522 * else if "errornr" is zero, redisplay the same line
2523 * else go to entry "errornr"
2524 */
2525 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002526qf_jump(qf_info_T *qi,
2527 int dir,
2528 int errornr,
2529 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002531 qfline_T *qf_ptr;
2532 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 buf_T *old_curbuf;
2536 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002537 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002538 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002540 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542#ifdef FEAT_FOLDING
2543 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2544#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002545 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002547 if (qi == NULL)
2548 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002549
2550 if (qi->qf_curlist >= qi->qf_listcount
2551 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
2553 EMSG(_(e_quickfix));
2554 return;
2555 }
2556
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002557 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002559 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002561 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002563 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2564 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002566 qf_ptr = old_qf_ptr;
2567 qf_index = old_qf_index;
2568 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 }
2571 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002572 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002574 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2575 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 /* No need to print the error message if it's visible in the error
2577 * window */
2578 print_message = FALSE;
2579
2580 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002581 * For ":helpgrep" find a help window or open one.
2582 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002583 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002584 if (jump_to_help_window(qi, &opened_window) == FAIL)
2585 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002586
2587 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 * If currently in the quickfix window, find another window to show the
2589 * file in.
2590 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002591 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
2593 /*
2594 * If there is no file specified, we don't know where to go.
2595 * But do advance, otherwise ":cn" gets stuck.
2596 */
2597 if (qf_ptr->qf_fnum == 0)
2598 goto theend;
2599
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002600 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2601 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
2604 /*
2605 * If there is a file name,
2606 * read the wanted file if needed, and check autowrite etc.
2607 */
2608 old_curbuf = curbuf;
2609 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002610
2611 if (qf_ptr->qf_fnum != 0)
2612 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002613 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002614
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002615 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2616 &opened_window, &abort);
2617 if (abort)
2618 {
2619 qi = NULL;
2620 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002621 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002622 }
2623
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002624 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
2626 /* When not switched to another buffer, still need to set pc mark */
2627 if (curbuf == old_curbuf)
2628 setpcmark();
2629
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002630 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2631 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633#ifdef FEAT_FOLDING
2634 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2635 foldOpenCursor();
2636#endif
2637 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002638 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640 else
2641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 if (opened_window)
2643 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002644 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 {
2646 /*
2647 * Couldn't open file, so put index back where it was. This could
2648 * happen if the file was readonly and we changed something.
2649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 qf_ptr = old_qf_ptr;
2652 qf_index = old_qf_index;
2653 }
2654 }
2655theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002656 if (qi != NULL)
2657 {
2658 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2659 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (p_swb != old_swb && opened_window)
2662 {
2663 /* Restore old 'switchbuf' value, but not when an autocommand or
2664 * modeline has changed the value. */
2665 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002666 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002668 swb_flags = old_swb_flags;
2669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 else
2671 free_string_option(old_swb);
2672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673}
2674
2675/*
2676 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002677 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 */
2679 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002680qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002682 buf_T *buf;
2683 char_u *fname;
2684 qfline_T *qfp;
2685 int i;
2686 int idx1 = 1;
2687 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002688 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002689 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002690 int qfFileAttr;
2691 int qfSepAttr;
2692 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002693 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002695 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002697 if (eap->cmdidx == CMD_llist)
2698 {
2699 qi = GET_LOC_LIST(curwin);
2700 if (qi == NULL)
2701 {
2702 EMSG(_(e_loclist));
2703 return;
2704 }
2705 }
2706
2707 if (qi->qf_curlist >= qi->qf_listcount
2708 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
2710 EMSG(_(e_quickfix));
2711 return;
2712 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002713 if (*arg == '+')
2714 {
2715 ++arg;
2716 plus = TRUE;
2717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2719 {
2720 EMSG(_(e_trailing));
2721 return;
2722 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002723 if (plus)
2724 {
2725 i = qi->qf_lists[qi->qf_curlist].qf_index;
2726 idx2 = i + idx1;
2727 idx1 = i;
2728 }
2729 else
2730 {
2731 i = qi->qf_lists[qi->qf_curlist].qf_count;
2732 if (idx1 < 0)
2733 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2734 if (idx2 < 0)
2735 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002738 /*
2739 * Get the attributes for the different quickfix highlight items. Note
2740 * that this depends on syntax items defined in the qf.vim syntax file
2741 */
2742 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2743 if (qfFileAttr == 0)
2744 qfFileAttr = HL_ATTR(HLF_D);
2745 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2746 if (qfSepAttr == 0)
2747 qfSepAttr = HL_ATTR(HLF_D);
2748 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2749 if (qfLineAttr == 0)
2750 qfLineAttr = HL_ATTR(HLF_N);
2751
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002752 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002754 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2755 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
2757 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2758 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002759 msg_putchar('\n');
2760 if (got_int)
2761 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002762
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002763 fname = NULL;
2764 if (qfp->qf_fnum != 0
2765 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2766 {
2767 fname = buf->b_fname;
2768 if (qfp->qf_type == 1) /* :helpgrep */
2769 fname = gettail(fname);
2770 }
2771 if (fname == NULL)
2772 sprintf((char *)IObuff, "%2d", i);
2773 else
2774 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2775 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002776 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002777 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2778
2779 if (qfp->qf_lnum != 0)
2780 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002781 if (qfp->qf_lnum == 0)
2782 IObuff[0] = NUL;
2783 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002784 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002785 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002786 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002787 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002788 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002789 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002790 msg_puts_attr(IObuff, qfLineAttr);
2791 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002792 if (qfp->qf_pattern != NULL)
2793 {
2794 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002796 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002797 }
2798 msg_puts((char_u *)" ");
2799
2800 /* Remove newlines and leading whitespace from the text. For an
2801 * unrecognized line keep the indent, the compiler may mark a word
2802 * with ^^^^. */
2803 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2805 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002806 msg_prt_line(IObuff, FALSE);
2807 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002809
2810 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002811 if (qfp == NULL)
2812 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 ui_breakcheck();
2815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816}
2817
2818/*
2819 * Remove newlines and leading whitespace from an error message.
2820 * Put the result in "buf[bufsize]".
2821 */
2822 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002823qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
2825 int i;
2826 char_u *p = text;
2827
2828 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2829 {
2830 if (*p == '\n')
2831 {
2832 buf[i] = ' ';
2833 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002834 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 break;
2836 }
2837 else
2838 buf[i] = *p++;
2839 }
2840 buf[i] = NUL;
2841}
2842
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002843 static void
2844qf_msg(qf_info_T *qi, int which, char *lead)
2845{
2846 char *title = (char *)qi->qf_lists[which].qf_title;
2847 int count = qi->qf_lists[which].qf_count;
2848 char_u buf[IOSIZE];
2849
2850 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2851 lead,
2852 which + 1,
2853 qi->qf_listcount,
2854 count);
2855
2856 if (title != NULL)
2857 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002858 size_t len = STRLEN(buf);
2859
2860 if (len < 34)
2861 {
2862 vim_memset(buf + len, ' ', 34 - len);
2863 buf[34] = NUL;
2864 }
2865 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002866 }
2867 trunc_string(buf, buf, Columns - 1, IOSIZE);
2868 msg(buf);
2869}
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871/*
2872 * ":colder [count]": Up in the quickfix stack.
2873 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002874 * ":lolder [count]": Up in the location list stack.
2875 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 */
2877 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002878qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002880 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 int count;
2882
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002883 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2884 {
2885 qi = GET_LOC_LIST(curwin);
2886 if (qi == NULL)
2887 {
2888 EMSG(_(e_loclist));
2889 return;
2890 }
2891 }
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 if (eap->addr_count != 0)
2894 count = eap->line2;
2895 else
2896 count = 1;
2897 while (count--)
2898 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002899 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002901 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
2903 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002904 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002906 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
2908 else
2909 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002910 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
2912 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002913 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002915 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002918 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002919 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920}
2921
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002922 void
2923qf_history(exarg_T *eap)
2924{
2925 qf_info_T *qi = &ql_info;
2926 int i;
2927
2928 if (eap->cmdidx == CMD_lhistory)
2929 qi = GET_LOC_LIST(curwin);
2930 if (qi == NULL || (qi->qf_listcount == 0
2931 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2932 MSG(_("No entries"));
2933 else
2934 for (i = 0; i < qi->qf_listcount; ++i)
2935 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2936}
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002939 * Free all the entries in the error list "idx". Note that other information
2940 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
2942 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002943qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002945 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002946 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002947 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002948 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002950 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002952 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002953 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002954 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002955 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002956 vim_free(qfp->qf_text);
2957 stop = (qfp == qfpnext);
2958 vim_free(qfp->qf_pattern);
2959 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002960 if (stop)
2961 /* Somehow qf_count may have an incorrect value, set it to 1
2962 * to avoid crashing when it's wrong.
2963 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002964 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002965 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002966 qfl->qf_start = qfpnext;
2967 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002969
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002970 qfl->qf_index = 0;
2971 qfl->qf_start = NULL;
2972 qfl->qf_last = NULL;
2973 qfl->qf_ptr = NULL;
2974 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002975
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002976 qf_clean_dir_stack(&qfl->qf_dir_stack);
2977 qfl->qf_directory = NULL;
2978 qf_clean_dir_stack(&qfl->qf_file_stack);
2979 qfl->qf_currfile = NULL;
2980 qfl->qf_multiline = FALSE;
2981 qfl->qf_multiignore = FALSE;
2982 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983}
2984
2985/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002986 * Free error list "idx". Frees all the entries in the quickfix list,
2987 * associated context information and the title.
2988 */
2989 static void
2990qf_free(qf_info_T *qi, int idx)
2991{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002992 qf_list_T *qfl = &qi->qf_lists[idx];
2993
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002994 qf_free_items(qi, idx);
2995
Bram Moolenaard23a8232018-02-10 18:45:26 +01002996 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002997 free_tv(qfl->qf_ctx);
2998 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002999 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003000 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003001}
3002
3003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 * qf_mark_adjust: adjust marks
3005 */
3006 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003007qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003008 win_T *wp,
3009 linenr_T line1,
3010 linenr_T line2,
3011 long amount,
3012 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003014 int i;
3015 qfline_T *qfp;
3016 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003017 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003018 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003019 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
Bram Moolenaarc1542742016-07-20 21:44:37 +02003021 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003022 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003023 if (wp != NULL)
3024 {
3025 if (wp->w_llist == NULL)
3026 return;
3027 qi = wp->w_llist;
3028 }
3029
3030 for (idx = 0; idx < qi->qf_listcount; ++idx)
3031 if (qi->qf_lists[idx].qf_count)
3032 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003033 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3034 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 if (qfp->qf_fnum == curbuf->b_fnum)
3036 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003037 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3039 {
3040 if (amount == MAXLNUM)
3041 qfp->qf_cleared = TRUE;
3042 else
3043 qfp->qf_lnum += amount;
3044 }
3045 else if (amount_after && qfp->qf_lnum > line2)
3046 qfp->qf_lnum += amount_after;
3047 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003048
3049 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003050 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051}
3052
3053/*
3054 * Make a nice message out of the error character and the error number:
3055 * char number message
3056 * e or E 0 " error"
3057 * w or W 0 " warning"
3058 * i or I 0 " info"
3059 * 0 0 ""
3060 * other 0 " c"
3061 * e or E n " error n"
3062 * w or W n " warning n"
3063 * i or I n " info n"
3064 * 0 n " error n"
3065 * other n " c n"
3066 * 1 x "" :helpgrep
3067 */
3068 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003069qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 static char_u buf[20];
3072 static char_u cc[3];
3073 char_u *p;
3074
3075 if (c == 'W' || c == 'w')
3076 p = (char_u *)" warning";
3077 else if (c == 'I' || c == 'i')
3078 p = (char_u *)" info";
3079 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3080 p = (char_u *)" error";
3081 else if (c == 0 || c == 1)
3082 p = (char_u *)"";
3083 else
3084 {
3085 cc[0] = ' ';
3086 cc[1] = c;
3087 cc[2] = NUL;
3088 p = cc;
3089 }
3090
3091 if (nr <= 0)
3092 return p;
3093
3094 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3095 return buf;
3096}
3097
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098/*
3099 * ":cwindow": open the quickfix window if we have errors to display,
3100 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003101 * ":lwindow": open the location list window if we have locations to display,
3102 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 */
3104 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003105ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003107 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 win_T *win;
3109
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003110 if (eap->cmdidx == CMD_lwindow)
3111 {
3112 qi = GET_LOC_LIST(curwin);
3113 if (qi == NULL)
3114 return;
3115 }
3116
3117 /* Look for an existing quickfix window. */
3118 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119
3120 /*
3121 * If a quickfix window is open but we have no errors to display,
3122 * close the window. If a quickfix window is not open, then open
3123 * it if we have errors; otherwise, leave it closed.
3124 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003125 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003126 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003127 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
3129 if (win != NULL)
3130 ex_cclose(eap);
3131 }
3132 else if (win == NULL)
3133 ex_copen(eap);
3134}
3135
3136/*
3137 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003138 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003141ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003143 win_T *win = NULL;
3144 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003146 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3147 {
3148 qi = GET_LOC_LIST(curwin);
3149 if (qi == NULL)
3150 return;
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003153 /* Find existing quickfix window and close it. */
3154 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (win != NULL)
3156 win_close(win, FALSE);
3157}
3158
3159/*
3160 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003161 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 */
3163 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003164ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003166 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003169 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003170 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003171 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003173 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3174 {
3175 qi = GET_LOC_LIST(curwin);
3176 if (qi == NULL)
3177 {
3178 EMSG(_(e_loclist));
3179 return;
3180 }
3181 }
3182
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 if (eap->addr_count != 0)
3184 height = eap->line2;
3185 else
3186 height = QF_WINHEIGHT;
3187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189#ifdef FEAT_GUI
3190 need_mouse_correct = TRUE;
3191#endif
3192
3193 /*
3194 * Find existing quickfix window, or open a new one.
3195 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003196 win = qf_find_win(qi);
3197
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003198 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003201 if (eap->addr_count != 0)
3202 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003203 if (cmdmod.split & WSP_VERT)
3204 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003205 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003206 win_setwidth(height);
3207 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003208 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003209 win_setheight(height);
3210 }
3211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 else
3213 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003214 int flags = 0;
3215
Bram Moolenaar9c102382006-05-03 21:26:49 +00003216 qf_buf = qf_find_buf(qi);
3217
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 /* The current window becomes the previous window afterwards. */
3219 win = curwin;
3220
Bram Moolenaar77642c02012-11-20 17:55:10 +01003221 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3222 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003223 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003224 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003225 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003226 /* Default is to open the window below the current window */
3227 if (cmdmod.split == 0)
3228 flags = WSP_BELOW;
3229 flags |= WSP_NEWLOC;
3230 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003232 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003234 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003236 /*
3237 * For the location list window, create a reference to the
3238 * location list from the window 'win'.
3239 */
3240 curwin->w_llist_ref = win->w_llist;
3241 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003243
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003244 if (oldwin != curwin)
3245 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003246 if (qf_buf != NULL)
3247 /* Use the existing quickfix buffer */
3248 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003249 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003250 else
3251 {
3252 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003253 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003254 /* switch off 'swapfile' */
3255 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3256 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003257 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003258 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003259 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003260#ifdef FEAT_DIFF
3261 curwin->w_p_diff = FALSE;
3262#endif
3263#ifdef FEAT_FOLDING
3264 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3265 OPT_LOCAL);
3266#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003269 /* Only set the height when still in the same tab page and there is no
3270 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003271 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 win_setheight(height);
3273 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3274 if (win_valid(win))
3275 prevwin = win;
3276 }
3277
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003278 qf_set_title_var(qi);
3279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 /*
3281 * Fill the buffer with the quickfix list.
3282 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003283 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003285 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 curwin->w_cursor.col = 0;
3287 check_cursor();
3288 update_topline(); /* scroll to show the line */
3289}
3290
3291/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003292 * Move the cursor in the quickfix window to "lnum".
3293 */
3294 static void
3295qf_win_goto(win_T *win, linenr_T lnum)
3296{
3297 win_T *old_curwin = curwin;
3298
3299 curwin = win;
3300 curbuf = win->w_buffer;
3301 curwin->w_cursor.lnum = lnum;
3302 curwin->w_cursor.col = 0;
3303#ifdef FEAT_VIRTUALEDIT
3304 curwin->w_cursor.coladd = 0;
3305#endif
3306 curwin->w_curswant = 0;
3307 update_topline(); /* scroll to show the line */
3308 redraw_later(VALID);
3309 curwin->w_redr_status = TRUE; /* update ruler */
3310 curwin = old_curwin;
3311 curbuf = curwin->w_buffer;
3312}
3313
3314/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003315 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003316 */
3317 void
3318ex_cbottom(exarg_T *eap UNUSED)
3319{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003320 qf_info_T *qi = &ql_info;
3321 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003322
Bram Moolenaar537ef082016-07-09 17:56:19 +02003323 if (eap->cmdidx == CMD_lbottom)
3324 {
3325 qi = GET_LOC_LIST(curwin);
3326 if (qi == NULL)
3327 {
3328 EMSG(_(e_loclist));
3329 return;
3330 }
3331 }
3332
3333 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003334 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3335 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3336}
3337
3338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 * Return the number of the current entry (line number in the quickfix
3340 * window).
3341 */
3342 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003343qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003345 qf_info_T *qi = &ql_info;
3346
3347 if (IS_LL_WINDOW(wp))
3348 /* In the location list window, use the referenced location list */
3349 qi = wp->w_llist_ref;
3350
3351 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352}
3353
3354/*
3355 * Update the cursor position in the quickfix window to the current error.
3356 * Return TRUE if there is a quickfix window.
3357 */
3358 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003359qf_win_pos_update(
3360 qf_info_T *qi,
3361 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003364 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
3366 /*
3367 * Put the cursor on the current error in the quickfix window, so that
3368 * it's viewable.
3369 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003370 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (win != NULL
3372 && qf_index <= win->w_buffer->b_ml.ml_line_count
3373 && old_qf_index != qf_index)
3374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (qf_index > old_qf_index)
3376 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003377 win->w_redraw_top = old_qf_index;
3378 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
3380 else
3381 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003382 win->w_redraw_top = qf_index;
3383 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003385 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387 return win != NULL;
3388}
3389
3390/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003391 * Check whether the given window is displaying the specified quickfix/location
3392 * list buffer
3393 */
3394 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003395is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003396{
3397 /*
3398 * A window displaying the quickfix buffer will have the w_llist_ref field
3399 * set to NULL.
3400 * A window displaying a location list buffer will have the w_llist_ref
3401 * pointing to the location list.
3402 */
3403 if (bt_quickfix(win->w_buffer))
3404 if ((qi == &ql_info && win->w_llist_ref == NULL)
3405 || (qi != &ql_info && win->w_llist_ref == qi))
3406 return TRUE;
3407
3408 return FALSE;
3409}
3410
3411/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003412 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003413 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003414 */
3415 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003416qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003417{
3418 win_T *win;
3419
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003420 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003421 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003422 return win;
3423 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003424}
3425
3426/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003427 * Find a quickfix buffer.
3428 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 */
3430 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003431qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003433 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003434 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar9c102382006-05-03 21:26:49 +00003436 FOR_ALL_TAB_WINDOWS(tp, win)
3437 if (is_qf_win(win, qi))
3438 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003439
Bram Moolenaar9c102382006-05-03 21:26:49 +00003440 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441}
3442
3443/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003444 * Update the w:quickfix_title variable in the quickfix/location list window
3445 */
3446 static void
3447qf_update_win_titlevar(qf_info_T *qi)
3448{
3449 win_T *win;
3450 win_T *curwin_save;
3451
3452 if ((win = qf_find_win(qi)) != NULL)
3453 {
3454 curwin_save = curwin;
3455 curwin = win;
3456 qf_set_title_var(qi);
3457 curwin = curwin_save;
3458 }
3459}
3460
3461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 * Find the quickfix buffer. If it exists, update the contents.
3463 */
3464 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003465qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003468 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003472 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (buf != NULL)
3474 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003475 linenr_T old_line_count = buf->b_ml.ml_line_count;
3476
3477 if (old_last == NULL)
3478 /* set curwin/curbuf to buf and save a few things */
3479 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
Bram Moolenaard823fa92016-08-12 16:29:27 +02003481 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003482
Bram Moolenaar864293a2016-06-02 13:40:04 +02003483 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003484 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003485
Bram Moolenaar864293a2016-06-02 13:40:04 +02003486 if (old_last == NULL)
3487 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003488 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003489
3490 /* restore curwin/curbuf and a few other things */
3491 aucmd_restbuf(&aco);
3492 }
3493
3494 /* Only redraw when added lines are visible. This avoids flickering
3495 * when the added lines are not visible. */
3496 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3497 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499}
3500
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003501/*
3502 * Set "w:quickfix_title" if "qi" has a title.
3503 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003504 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003505qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003506{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003507 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3508 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003509 qi->qf_lists[qi->qf_curlist].qf_title);
3510}
3511
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512/*
3513 * Fill current buffer with quickfix errors, replacing any previous contents.
3514 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003515 * If "old_last" is not NULL append the items after this one.
3516 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3517 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 */
3519 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003520qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003522 linenr_T lnum;
3523 qfline_T *qfp;
3524 buf_T *errbuf;
3525 int len;
3526 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
Bram Moolenaar864293a2016-06-02 13:40:04 +02003528 if (old_last == NULL)
3529 {
3530 if (buf != curbuf)
3531 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003532 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003533 return;
3534 }
3535
3536 /* delete all existing lines */
3537 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3538 (void)ml_delete((linenr_T)1, FALSE);
3539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
3541 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003542 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
3544 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003545 if (old_last == NULL)
3546 {
3547 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3548 lnum = 0;
3549 }
3550 else
3551 {
3552 qfp = old_last->qf_next;
3553 lnum = buf->b_ml.ml_line_count;
3554 }
3555 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
3557 if (qfp->qf_fnum != 0
3558 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3559 && errbuf->b_fname != NULL)
3560 {
3561 if (qfp->qf_type == 1) /* :helpgrep */
3562 STRCPY(IObuff, gettail(errbuf->b_fname));
3563 else
3564 STRCPY(IObuff, errbuf->b_fname);
3565 len = (int)STRLEN(IObuff);
3566 }
3567 else
3568 len = 0;
3569 IObuff[len++] = '|';
3570
3571 if (qfp->qf_lnum > 0)
3572 {
3573 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3574 len += (int)STRLEN(IObuff + len);
3575
3576 if (qfp->qf_col > 0)
3577 {
3578 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3579 len += (int)STRLEN(IObuff + len);
3580 }
3581
3582 sprintf((char *)IObuff + len, "%s",
3583 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3584 len += (int)STRLEN(IObuff + len);
3585 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003586 else if (qfp->qf_pattern != NULL)
3587 {
3588 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3589 len += (int)STRLEN(IObuff + len);
3590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 IObuff[len++] = '|';
3592 IObuff[len++] = ' ';
3593
3594 /* Remove newlines and leading whitespace from the text.
3595 * For an unrecognized line keep the indent, the compiler may
3596 * mark a word with ^^^^. */
3597 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3598 IObuff + len, IOSIZE - len);
3599
Bram Moolenaar864293a2016-06-02 13:40:04 +02003600 if (ml_append_buf(buf, lnum, IObuff,
3601 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003603 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003605 if (qfp == NULL)
3606 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003608
3609 if (old_last == NULL)
3610 /* Delete the empty line which is now at the end */
3611 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613
3614 /* correct cursor position */
3615 check_lnums(TRUE);
3616
Bram Moolenaar864293a2016-06-02 13:40:04 +02003617 if (old_last == NULL)
3618 {
3619 /* Set the 'filetype' to "qf" each time after filling the buffer.
3620 * This resembles reading a file into a buffer, it's more logical when
3621 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003622 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003623 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3624 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
Bram Moolenaar864293a2016-06-02 13:40:04 +02003626 keep_filetype = TRUE; /* don't detect 'filetype' */
3627 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003629 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003631 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003632 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003633
Bram Moolenaar864293a2016-06-02 13:40:04 +02003634 /* make sure it will be redrawn */
3635 redraw_curbuf_later(NOT_VALID);
3636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 /* Restore KeyTyped, setting 'filetype' may reset it. */
3639 KeyTyped = old_KeyTyped;
3640}
3641
Bram Moolenaarb254af32017-12-18 19:48:58 +01003642 static void
3643qf_list_changed(qf_info_T *qi, int qf_idx)
3644{
3645 qi->qf_lists[qf_idx].qf_changedtick++;
3646}
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003649 * Return TRUE when using ":vimgrep" for ":grep".
3650 */
3651 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003652grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003653{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003654 return ((cmdidx == CMD_grep
3655 || cmdidx == CMD_lgrep
3656 || cmdidx == CMD_grepadd
3657 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003658 && STRCMP("internal",
3659 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3660}
3661
3662/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003663 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 */
3665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003666ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003668 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003670 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003672 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003673 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003674 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003675 char_u *au_name = NULL;
3676
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003677 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3678 if (grep_internal(eap->cmdidx))
3679 {
3680 ex_vimgrep(eap);
3681 return;
3682 }
3683
Bram Moolenaar7c626922005-02-07 22:01:03 +00003684 switch (eap->cmdidx)
3685 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003686 case CMD_make: au_name = (char_u *)"make"; break;
3687 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3688 case CMD_grep: au_name = (char_u *)"grep"; break;
3689 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3690 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3691 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003692 default: break;
3693 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003694 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3695 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003696 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003697#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003698 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003699 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003700#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003701 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003702#ifdef FEAT_MBYTE
3703 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaara6557602006-02-04 22:43:20 +00003706 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3707 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003708 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003711 fname = get_mef_name();
3712 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003714 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
3716 /*
3717 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3718 */
3719 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3720 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003721 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 cmd = alloc(len);
3723 if (cmd == NULL)
3724 return;
3725 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3726 (char *)p_shq);
3727 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003728 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 /*
3730 * Output a newline if there's something else than the :make command that
3731 * was typed (in which case the cursor is in column 0).
3732 */
3733 if (msg_col == 0)
3734 msg_didout = FALSE;
3735 msg_start();
3736 MSG_PUTS(":!");
3737 msg_outtrans(cmd); /* show what we are doing */
3738
3739 /* let the shell know if we are redirecting output or not */
3740 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3741
3742#ifdef AMIGA
3743 out_flush();
3744 /* read window status report and redraw before message */
3745 (void)char_avail();
3746#endif
3747
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003748 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003749 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3750 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003751 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003752 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003753 if (wp != NULL)
3754 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003755 if (res >= 0 && qi != NULL)
3756 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003757 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003758 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003759 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3760 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01003761 if (qi != NULL && qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003762 res = qi->qf_lists[qi->qf_curlist].qf_count;
3763 else
3764 res = 0;
3765 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003766 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003767 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
Bram Moolenaar7c626922005-02-07 22:01:03 +00003769 mch_remove(fname);
3770 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 vim_free(cmd);
3772}
3773
3774/*
3775 * Return the name for the errorfile, in allocated memory.
3776 * Find a new unique name when 'makeef' contains "##".
3777 * Returns NULL for error.
3778 */
3779 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003780get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781{
3782 char_u *p;
3783 char_u *name;
3784 static int start = -1;
3785 static int off = 0;
3786#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003787 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788#endif
3789
3790 if (*p_mef == NUL)
3791 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003792 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (name == NULL)
3794 EMSG(_(e_notmp));
3795 return name;
3796 }
3797
3798 for (p = p_mef; *p; ++p)
3799 if (p[0] == '#' && p[1] == '#')
3800 break;
3801
3802 if (*p == NUL)
3803 return vim_strsave(p_mef);
3804
3805 /* Keep trying until the name doesn't exist yet. */
3806 for (;;)
3807 {
3808 if (start == -1)
3809 start = mch_get_pid();
3810 else
3811 off += 19;
3812
3813 name = alloc((unsigned)STRLEN(p_mef) + 30);
3814 if (name == NULL)
3815 break;
3816 STRCPY(name, p_mef);
3817 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3818 STRCAT(name, p + 2);
3819 if (mch_getperm(name) < 0
3820#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003821 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 && mch_lstat((char *)name, &sb) < 0
3823#endif
3824 )
3825 break;
3826 vim_free(name);
3827 }
3828 return name;
3829}
3830
3831/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003832 * Returns the number of valid entries in the current quickfix/location list.
3833 */
3834 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003835qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003836{
3837 qf_info_T *qi = &ql_info;
3838 qfline_T *qfp;
3839 int i, sz = 0;
3840 int prev_fnum = 0;
3841
3842 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3843 {
3844 /* Location list */
3845 qi = GET_LOC_LIST(curwin);
3846 if (qi == NULL)
3847 return 0;
3848 }
3849
3850 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003851 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003852 ++i, qfp = qfp->qf_next)
3853 {
3854 if (qfp->qf_valid)
3855 {
3856 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3857 sz++; /* Count all valid entries */
3858 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3859 {
3860 /* Count the number of files */
3861 sz++;
3862 prev_fnum = qfp->qf_fnum;
3863 }
3864 }
3865 }
3866
3867 return sz;
3868}
3869
3870/*
3871 * Returns the current index of the quickfix/location list.
3872 * Returns 0 if there is an error.
3873 */
3874 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003875qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003876{
3877 qf_info_T *qi = &ql_info;
3878
3879 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3880 {
3881 /* Location list */
3882 qi = GET_LOC_LIST(curwin);
3883 if (qi == NULL)
3884 return 0;
3885 }
3886
3887 return qi->qf_lists[qi->qf_curlist].qf_index;
3888}
3889
3890/*
3891 * Returns the current index in the quickfix/location list (counting only valid
3892 * entries). If no valid entries are in the list, then returns 1.
3893 */
3894 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003895qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003896{
3897 qf_info_T *qi = &ql_info;
3898 qf_list_T *qfl;
3899 qfline_T *qfp;
3900 int i, eidx = 0;
3901 int prev_fnum = 0;
3902
3903 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3904 {
3905 /* Location list */
3906 qi = GET_LOC_LIST(curwin);
3907 if (qi == NULL)
3908 return 1;
3909 }
3910
3911 qfl = &qi->qf_lists[qi->qf_curlist];
3912 qfp = qfl->qf_start;
3913
3914 /* check if the list has valid errors */
3915 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3916 return 1;
3917
3918 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3919 {
3920 if (qfp->qf_valid)
3921 {
3922 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3923 {
3924 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3925 {
3926 /* Count the number of files */
3927 eidx++;
3928 prev_fnum = qfp->qf_fnum;
3929 }
3930 }
3931 else
3932 eidx++;
3933 }
3934 }
3935
3936 return eidx ? eidx : 1;
3937}
3938
3939/*
3940 * Get the 'n'th valid error entry in the quickfix or location list.
3941 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3942 * For :cdo and :ldo returns the 'n'th valid error entry.
3943 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3944 */
3945 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003946qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003947{
3948 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3949 qfline_T *qfp = qfl->qf_start;
3950 int i, eidx;
3951 int prev_fnum = 0;
3952
3953 /* check if the list has valid errors */
3954 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3955 return 1;
3956
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003957 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003958 i++, qfp = qfp->qf_next)
3959 {
3960 if (qfp->qf_valid)
3961 {
3962 if (fdo)
3963 {
3964 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3965 {
3966 /* Count the number of files */
3967 eidx++;
3968 prev_fnum = qfp->qf_fnum;
3969 }
3970 }
3971 else
3972 eidx++;
3973 }
3974
3975 if (eidx == n)
3976 break;
3977 }
3978
3979 if (i <= qfl->qf_count)
3980 return i;
3981 else
3982 return 1;
3983}
3984
3985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003987 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003988 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 */
3990 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003991ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003993 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003994 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003995
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003996 if (eap->cmdidx == CMD_ll
3997 || eap->cmdidx == CMD_lrewind
3998 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003999 || eap->cmdidx == CMD_llast
4000 || eap->cmdidx == CMD_ldo
4001 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004002 {
4003 qi = GET_LOC_LIST(curwin);
4004 if (qi == NULL)
4005 {
4006 EMSG(_(e_loclist));
4007 return;
4008 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004009 }
4010
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004011 if (eap->addr_count > 0)
4012 errornr = (int)eap->line2;
4013 else
4014 {
4015 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4016 errornr = 0;
4017 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4018 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4019 errornr = 1;
4020 else
4021 errornr = 32767;
4022 }
4023
4024 /* For cdo and ldo commands, jump to the nth valid error.
4025 * For cfdo and lfdo commands, jump to the nth valid file entry.
4026 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004027 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4028 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004029 errornr = qf_get_nth_valid_entry(qi,
4030 eap->addr_count > 0 ? (int)eap->line1 : 1,
4031 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4032
4033 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034}
4035
4036/*
4037 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004038 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004039 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 */
4041 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004042ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004044 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004045 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004046
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004047 if (eap->cmdidx == CMD_lnext
4048 || eap->cmdidx == CMD_lNext
4049 || eap->cmdidx == CMD_lprevious
4050 || eap->cmdidx == CMD_lnfile
4051 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004052 || eap->cmdidx == CMD_lpfile
4053 || eap->cmdidx == CMD_ldo
4054 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004055 {
4056 qi = GET_LOC_LIST(curwin);
4057 if (qi == NULL)
4058 {
4059 EMSG(_(e_loclist));
4060 return;
4061 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004062 }
4063
Bram Moolenaar55b69262017-08-13 13:42:01 +02004064 if (eap->addr_count > 0
4065 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4066 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004067 errornr = (int)eap->line2;
4068 else
4069 errornr = 1;
4070
4071 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4072 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004074 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4075 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004077 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4078 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 ? BACKWARD_FILE
4080 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004081 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082}
4083
4084/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004085 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004086 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 */
4088 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004089ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004091 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004092 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004093 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004094 char_u *au_name = NULL;
Bram Moolenaarfc6f16b2018-03-06 17:43:22 +01004095 int save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004096 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004097
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004098 switch (eap->cmdidx)
4099 {
4100 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4101 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4102 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4103 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4104 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4105 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4106 default: break;
4107 }
4108 if (au_name != NULL)
4109 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004110#ifdef FEAT_MBYTE
4111 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4112#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004113#ifdef FEAT_BROWSE
4114 if (cmdmod.browse)
4115 {
4116 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4117 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4118 if (browse_file == NULL)
4119 return;
4120 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4121 vim_free(browse_file);
4122 }
4123 else
4124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004126 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004127
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004128 if (eap->cmdidx == CMD_lfile
4129 || eap->cmdidx == CMD_lgetfile
4130 || eap->cmdidx == CMD_laddfile)
4131 wp = curwin;
4132
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004133 /*
4134 * This function is used by the :cfile, :cgetfile and :caddfile
4135 * commands.
4136 * :cfile always creates a new quickfix list and jumps to the
4137 * first error.
4138 * :cgetfile creates a new quickfix list but doesn't jump to the
4139 * first error.
4140 * :caddfile adds to an existing quickfix list. If there is no
4141 * quickfix list then a new list is created.
4142 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004143 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4144 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004145 if (wp != NULL)
4146 qi = GET_LOC_LIST(wp);
4147 if (res >= 0 && qi != NULL)
4148 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004149 if (qi != NULL)
4150 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004151 if (au_name != NULL)
4152 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004153
4154 /* An autocmd might have freed the quickfix/location list. Check whether it
4155 * is still valid. */
4156 if (qi != NULL && !qflist_valid(wp, save_qfid))
Bram Moolenaar3c097222017-12-21 20:54:49 +01004157 return;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004158 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004159 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160}
4161
4162/*
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004163 * Return the quickfix/location list number with the given identifier.
4164 * Returns -1 if list is not found.
4165 */
4166 static int
4167qf_id2nr(qf_info_T *qi, int_u qfid)
4168{
4169 int qf_idx;
4170
4171 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4172 if (qi->qf_lists[qf_idx].qf_id == qfid)
4173 return qf_idx;
4174 return -1;
4175}
4176
4177/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004178 * Return the vimgrep autocmd name.
4179 */
4180 static char_u *
4181vgr_get_auname(cmdidx_T cmdidx)
4182{
4183 switch (cmdidx)
4184 {
4185 case CMD_vimgrep: return (char_u *)"vimgrep";
4186 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4187 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4188 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4189 case CMD_grep: return (char_u *)"grep";
4190 case CMD_lgrep: return (char_u *)"lgrep";
4191 case CMD_grepadd: return (char_u *)"grepadd";
4192 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4193 default: return NULL;
4194 }
4195}
4196
4197/*
4198 * Initialize the regmatch used by vimgrep for pattern "s".
4199 */
4200 static void
4201vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4202{
4203 /* Get the search pattern: either white-separated or enclosed in // */
4204 regmatch->regprog = NULL;
4205
4206 if (s == NULL || *s == NUL)
4207 {
4208 /* Pattern is empty, use last search pattern. */
4209 if (last_search_pat() == NULL)
4210 {
4211 EMSG(_(e_noprevre));
4212 return;
4213 }
4214 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4215 }
4216 else
4217 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4218
4219 regmatch->rmm_ic = p_ic;
4220 regmatch->rmm_maxcol = 0;
4221}
4222
4223/*
4224 * Display a file name when vimgrep is running.
4225 */
4226 static void
4227vgr_display_fname(char_u *fname)
4228{
4229 char_u *p;
4230
4231 msg_start();
4232 p = msg_strtrunc(fname, TRUE);
4233 if (p == NULL)
4234 msg_outtrans(fname);
4235 else
4236 {
4237 msg_outtrans(p);
4238 vim_free(p);
4239 }
4240 msg_clr_eos();
4241 msg_didout = FALSE; /* overwrite this message */
4242 msg_nowait = TRUE; /* don't wait for this message */
4243 msg_col = 0;
4244 out_flush();
4245}
4246
4247/*
4248 * Load a dummy buffer to search for a pattern using vimgrep.
4249 */
4250 static buf_T *
4251vgr_load_dummy_buf(
4252 char_u *fname,
4253 char_u *dirname_start,
4254 char_u *dirname_now)
4255{
4256 int save_mls;
4257#if defined(FEAT_SYN_HL)
4258 char_u *save_ei = NULL;
4259#endif
4260 buf_T *buf;
4261
4262#if defined(FEAT_SYN_HL)
4263 /* Don't do Filetype autocommands to avoid loading syntax and
4264 * indent scripts, a great speed improvement. */
4265 save_ei = au_event_disable(",Filetype");
4266#endif
4267 /* Don't use modelines here, it's useless. */
4268 save_mls = p_mls;
4269 p_mls = 0;
4270
4271 /* Load file into a buffer, so that 'fileencoding' is detected,
4272 * autocommands applied, etc. */
4273 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4274
4275 p_mls = save_mls;
4276#if defined(FEAT_SYN_HL)
4277 au_event_restore(save_ei);
4278#endif
4279
4280 return buf;
4281}
4282
4283/*
4284 * Check whether a quickfix/location list valid. Autocmds may remove or change
4285 * a quickfix list when vimgrep is running. If the list is not found, create a
4286 * new list.
4287 */
4288 static int
4289vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004290 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004291 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004292 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004293 char_u *title)
4294{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004295 /* Verify that the quickfix/location list was not freed by an autocmd */
4296 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004297 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004298 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004299 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004300 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004301 EMSG(_(e_loc_list_changed));
4302 return FALSE;
4303 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004304 else
4305 {
4306 /* Quickfix list is not found, create a new one. */
4307 qf_new_list(qi, title);
4308 return TRUE;
4309 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004310 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004311
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004312 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004313 /* Autocommands changed the quickfix list. Find the one we were
4314 * using and restore it. */
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004315 qi->qf_curlist = qf_id2nr(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004316
4317 return TRUE;
4318}
4319
4320/*
4321 * Search for a pattern in all the lines in a buffer and add the matching lines
4322 * to a quickfix list.
4323 */
4324 static int
4325vgr_match_buflines(
4326 qf_info_T *qi,
4327 char_u *fname,
4328 buf_T *buf,
4329 regmmatch_T *regmatch,
4330 long tomatch,
4331 int duplicate_name,
4332 int flags)
4333{
4334 int found_match = FALSE;
4335 long lnum;
4336 colnr_T col;
4337
4338 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
4339 {
4340 col = 0;
4341 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
4342 col, NULL, NULL) > 0)
4343 {
4344 /* Pass the buffer number so that it gets used even for a
4345 * dummy buffer, unless duplicate_name is set, then the
4346 * buffer will be wiped out below. */
4347 if (qf_add_entry(qi,
4348 qi->qf_curlist,
4349 NULL, /* dir */
4350 fname,
4351 duplicate_name ? 0 : buf->b_fnum,
4352 ml_get_buf(buf,
4353 regmatch->startpos[0].lnum + lnum, FALSE),
4354 regmatch->startpos[0].lnum + lnum,
4355 regmatch->startpos[0].col + 1,
4356 FALSE, /* vis_col */
4357 NULL, /* search pattern */
4358 0, /* nr */
4359 0, /* type */
4360 TRUE /* valid */
4361 ) == FAIL)
4362 {
4363 got_int = TRUE;
4364 break;
4365 }
4366 found_match = TRUE;
4367 if (--tomatch == 0)
4368 break;
4369 if ((flags & VGR_GLOBAL) == 0
4370 || regmatch->endpos[0].lnum > 0)
4371 break;
4372 col = regmatch->endpos[0].col
4373 + (col == regmatch->endpos[0].col);
4374 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
4375 break;
4376 }
4377 line_breakcheck();
4378 if (got_int)
4379 break;
4380 }
4381
4382 return found_match;
4383}
4384
4385/*
4386 * Jump to the first match and update the directory.
4387 */
4388 static void
4389vgr_jump_to_match(
4390 qf_info_T *qi,
4391 int forceit,
4392 int *redraw_for_dummy,
4393 buf_T *first_match_buf,
4394 char_u *target_dir)
4395{
4396 buf_T *buf;
4397
4398 buf = curbuf;
4399 qf_jump(qi, 0, 0, forceit);
4400 if (buf != curbuf)
4401 /* If we jumped to another buffer redrawing will already be
4402 * taken care of. */
4403 *redraw_for_dummy = FALSE;
4404
4405 /* Jump to the directory used after loading the buffer. */
4406 if (curbuf == first_match_buf && target_dir != NULL)
4407 {
4408 exarg_T ea;
4409
4410 ea.arg = target_dir;
4411 ea.cmdidx = CMD_lcd;
4412 ex_cd(&ea);
4413 }
4414}
4415
4416/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004417 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004418 * ":vimgrepadd {pattern} file(s)"
4419 * ":lvimgrep {pattern} file(s)"
4420 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004421 */
4422 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004423ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004424{
Bram Moolenaar81695252004-12-29 20:58:21 +00004425 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004426 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004427 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004428 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004429 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004430 char_u *s;
4431 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004432 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004433 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004434 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004435 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004436 buf_T *buf;
4437 int duplicate_name = FALSE;
4438 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004439 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004440 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004441 buf_T *first_match_buf = NULL;
4442 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004443 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004444 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004445 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004446 char_u *dirname_start = NULL;
4447 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004448 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004449 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004450
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004451 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01004452 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4453 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004454 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004455#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004456 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004457 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004458#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004459 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004460
Bram Moolenaar754b5602006-02-09 23:53:20 +00004461 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004462 || eap->cmdidx == CMD_lvimgrep
4463 || eap->cmdidx == CMD_lgrepadd
4464 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004465 {
4466 qi = ll_get_or_alloc_list(curwin);
4467 if (qi == NULL)
4468 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004469 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004470 }
4471
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004472 if (eap->addr_count > 0)
4473 tomatch = eap->line2;
4474 else
4475 tomatch = MAXLNUM;
4476
Bram Moolenaar81695252004-12-29 20:58:21 +00004477 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004478 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004479 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004480 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004481 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004482 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004483 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004484 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004485 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004486
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004487 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004488 if (regmatch.regprog == NULL)
4489 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004490
4491 p = skipwhite(p);
4492 if (*p == NUL)
4493 {
4494 EMSG(_("E683: File name missing or invalid pattern"));
4495 goto theend;
4496 }
4497
Bram Moolenaar55b69262017-08-13 13:42:01 +02004498 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004499 && eap->cmdidx != CMD_vimgrepadd
4500 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004501 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004502 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004503 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004504
4505 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004506 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004507 goto theend;
4508 if (fcount == 0)
4509 {
4510 EMSG(_(e_nomatch));
4511 goto theend;
4512 }
4513
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004514 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4515 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004516 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004517 {
4518 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004519 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004520 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004521
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004522 /* Remember the current directory, because a BufRead autocommand that does
4523 * ":lcd %:p:h" changes the meaning of short path names. */
4524 mch_dirname(dirname_start, MAXPATHL);
4525
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004526 /* Remember the current quickfix list identifier, so that we can check for
4527 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004528 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004529
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004530 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004531 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004532 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004533 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004534 if (time(NULL) > seconds)
4535 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004536 /* Display the file name every second or so, show the user we are
4537 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004538 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004539 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004540 }
4541
Bram Moolenaar81695252004-12-29 20:58:21 +00004542 buf = buflist_findname_exp(fnames[fi]);
4543 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4544 {
4545 /* Remember that a buffer with this name already exists. */
4546 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004547 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004548 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004549
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004550 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00004551 }
4552 else
4553 /* Use existing, loaded buffer. */
4554 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004555
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004556 /* Check whether the quickfix list is still valid. When loading a
4557 * buffer above, autocommands might have changed the quickfix list. */
4558 if (!vgr_qflist_valid(wp, qi, save_qfid, *eap->cmdlinep))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004559 {
4560 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004561 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004562 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004563 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004564
Bram Moolenaar81695252004-12-29 20:58:21 +00004565 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004566 {
4567 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004568 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004569 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004570 else
4571 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004572 /* Try for a match in all lines of the buffer.
4573 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004574 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
4575 tomatch, duplicate_name, flags);
4576
Bram Moolenaar81695252004-12-29 20:58:21 +00004577 if (using_dummy)
4578 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004579 if (found_match && first_match_buf == NULL)
4580 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004581 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004582 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004583 /* Never keep a dummy buffer if there is another buffer
4584 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004585 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004586 buf = NULL;
4587 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004588 else if (!cmdmod.hide
4589 || buf->b_p_bh[0] == 'u' /* "unload" */
4590 || buf->b_p_bh[0] == 'w' /* "wipe" */
4591 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004592 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004593 /* When no match was found we don't need to remember the
4594 * buffer, wipe it out. If there was a match and it
4595 * wasn't the first one or we won't jump there: only
4596 * unload the buffer.
4597 * Ignore 'hidden' here, because it may lead to having too
4598 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004599 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004600 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004601 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004602 buf = NULL;
4603 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004604 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004605 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004606 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004607 /* Keeping the buffer, remove the dummy flag. */
4608 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004609 buf = NULL;
4610 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004611 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004612
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004613 if (buf != NULL)
4614 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004615 /* Keeping the buffer, remove the dummy flag. */
4616 buf->b_flags &= ~BF_DUMMY;
4617
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004618 /* If the buffer is still loaded we need to use the
4619 * directory we jumped to below. */
4620 if (buf == first_match_buf
4621 && target_dir == NULL
4622 && STRCMP(dirname_start, dirname_now) != 0)
4623 target_dir = vim_strsave(dirname_now);
4624
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004625 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004626 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004627 * need to be done (again). But not the window-local
4628 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004629 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004630#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004631 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4632 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004633#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004634 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004635 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004636 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004637 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004638 }
4639 }
4640
4641 FreeWild(fcount, fnames);
4642
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004643 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4644 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4645 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004646 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004647
Bram Moolenaar864293a2016-06-02 13:40:04 +02004648 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004649
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004650 if (au_name != NULL)
4651 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4652 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004653 /*
4654 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4655 * is still valid.
4656 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004657 if (!qflist_valid(wp, save_qfid))
4658 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004659
Bram Moolenaar86b68352004-12-27 21:59:20 +00004660 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004661 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004662 {
4663 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004664 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
4665 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004666 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004667 else
4668 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004669
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004670 /* If we loaded a dummy buffer into the current window, the autocommands
4671 * may have messed up things, need to redraw and recompute folds. */
4672 if (redraw_for_dummy)
4673 {
4674#ifdef FEAT_FOLDING
4675 foldUpdateAll(curwin);
4676#else
4677 redraw_later(NOT_VALID);
4678#endif
4679 }
4680
Bram Moolenaar86b68352004-12-27 21:59:20 +00004681theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004682 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004683 vim_free(dirname_now);
4684 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004685 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004686 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004687}
4688
4689/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004690 * Restore current working directory to "dirname_start" if they differ, taking
4691 * into account whether it is set locally or globally.
4692 */
4693 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004694restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004695{
4696 char_u *dirname_now = alloc(MAXPATHL);
4697
4698 if (NULL != dirname_now)
4699 {
4700 mch_dirname(dirname_now, MAXPATHL);
4701 if (STRCMP(dirname_start, dirname_now) != 0)
4702 {
4703 /* If the directory has changed, change it back by building up an
4704 * appropriate ex command and executing it. */
4705 exarg_T ea;
4706
4707 ea.arg = dirname_start;
4708 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4709 ex_cd(&ea);
4710 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004711 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004712 }
4713}
4714
4715/*
4716 * Load file "fname" into a dummy buffer and return the buffer pointer,
4717 * placing the directory resulting from the buffer load into the
4718 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4719 * prior to calling this function. Restores directory to "dirname_start" prior
4720 * to returning, if autocmds or the 'autochdir' option have changed it.
4721 *
4722 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4723 * or wipe_dummy_buffer() later!
4724 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004725 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004726 */
4727 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004728load_dummy_buffer(
4729 char_u *fname,
4730 char_u *dirname_start, /* in: old directory */
4731 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004732{
4733 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004734 bufref_T newbufref;
4735 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004736 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004737 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004738 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004739
4740 /* Allocate a buffer without putting it in the buffer list. */
4741 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4742 if (newbuf == NULL)
4743 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004744 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004745
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004746 /* Init the options. */
4747 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4748
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004749 /* need to open the memfile before putting the buffer in a window */
4750 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004751 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004752 /* Make sure this buffer isn't wiped out by auto commands. */
4753 ++newbuf->b_locked;
4754
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004755 /* set curwin/curbuf to buf and save a few things */
4756 aucmd_prepbuf(&aco, newbuf);
4757
4758 /* Need to set the filename for autocommands. */
4759 (void)setfname(curbuf, fname, NULL, FALSE);
4760
Bram Moolenaar81695252004-12-29 20:58:21 +00004761 /* Create swap file now to avoid the ATTENTION message. */
4762 check_need_swap(TRUE);
4763
4764 /* Remove the "dummy" flag, otherwise autocommands may not
4765 * work. */
4766 curbuf->b_flags &= ~BF_DUMMY;
4767
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004768 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004769 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004770 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004771 NULL, READ_NEW | READ_DUMMY);
4772 --newbuf->b_locked;
4773 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004774 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004775 && !(curbuf->b_flags & BF_NEW))
4776 {
4777 failed = FALSE;
4778 if (curbuf != newbuf)
4779 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004780 /* Bloody autocommands changed the buffer! Can happen when
4781 * using netrw and editing a remote file. Use the current
4782 * buffer instead, delete the dummy one after restoring the
4783 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004784 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004785 newbuf = curbuf;
4786 }
4787 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004788
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004789 /* restore curwin/curbuf and a few other things */
4790 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004791 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4792 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004793
4794 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4795 * skip it. */
4796 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004797 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004798
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004799 /*
4800 * When autocommands/'autochdir' option changed directory: go back.
4801 * Let the caller know what the resulting dir was first, in case it is
4802 * important.
4803 */
4804 mch_dirname(resulting_dir, MAXPATHL);
4805 restore_start_dir(dirname_start);
4806
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004807 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004808 return NULL;
4809 if (failed)
4810 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004811 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004812 return NULL;
4813 }
4814 return newbuf;
4815}
4816
4817/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004818 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4819 * directory to "dirname_start" prior to returning, if autocmds or the
4820 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004821 */
4822 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004823wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004824{
4825 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004826 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004827#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004828 cleanup_T cs;
4829
4830 /* Reset the error/interrupt/exception state here so that aborting()
4831 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4832 * work when got_int is set. */
4833 enter_cleanup(&cs);
4834#endif
4835
Bram Moolenaar81695252004-12-29 20:58:21 +00004836 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004837
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004838#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004839 /* Restore the error/interrupt/exception state if not discarded by a
4840 * new aborting error, interrupt, or uncaught exception. */
4841 leave_cleanup(&cs);
4842#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004843 /* When autocommands/'autochdir' option changed directory: go back. */
4844 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004845 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004846}
4847
4848/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004849 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4850 * directory to "dirname_start" prior to returning, if autocmds or the
4851 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004852 */
4853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004854unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004855{
4856 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004857 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004858 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004859
4860 /* When autocommands/'autochdir' option changed directory: go back. */
4861 restore_start_dir(dirname_start);
4862 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004863}
4864
Bram Moolenaar05159a02005-02-26 23:04:13 +00004865#if defined(FEAT_EVAL) || defined(PROTO)
4866/*
4867 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004868 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004869 */
4870 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004871get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004872{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004873 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004874 dict_T *dict;
4875 char_u buf[2];
4876 qfline_T *qfp;
4877 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004878 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004879
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004880 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004881 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004882 qi = &ql_info;
4883 if (wp != NULL)
4884 {
4885 qi = GET_LOC_LIST(wp);
4886 if (qi == NULL)
4887 return FAIL;
4888 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004889 }
4890
Bram Moolenaard823fa92016-08-12 16:29:27 +02004891 if (qf_idx == -1)
4892 qf_idx = qi->qf_curlist;
4893
4894 if (qf_idx >= qi->qf_listcount
4895 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004896 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004897
Bram Moolenaard823fa92016-08-12 16:29:27 +02004898 qfp = qi->qf_lists[qf_idx].qf_start;
4899 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004900 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004901 /* Handle entries with a non-existing buffer number. */
4902 bufnum = qfp->qf_fnum;
4903 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4904 bufnum = 0;
4905
Bram Moolenaar05159a02005-02-26 23:04:13 +00004906 if ((dict = dict_alloc()) == NULL)
4907 return FAIL;
4908 if (list_append_dict(list, dict) == FAIL)
4909 return FAIL;
4910
4911 buf[0] = qfp->qf_type;
4912 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004913 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004914 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4915 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4916 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4917 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004918 || dict_add_nr_str(dict, "pattern", 0L,
4919 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4920 || dict_add_nr_str(dict, "text", 0L,
4921 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004922 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4923 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4924 return FAIL;
4925
4926 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004927 if (qfp == NULL)
4928 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004929 }
4930 return OK;
4931}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004932
4933/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004934 * Flags used by getqflist()/getloclist() to determine which fields to return.
4935 */
4936enum {
4937 QF_GETLIST_NONE = 0x0,
4938 QF_GETLIST_TITLE = 0x1,
4939 QF_GETLIST_ITEMS = 0x2,
4940 QF_GETLIST_NR = 0x4,
4941 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004942 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004943 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004944 QF_GETLIST_IDX = 0x40,
4945 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01004946 QF_GETLIST_TICK = 0x100,
4947 QF_GETLIST_ALL = 0x1FF
Bram Moolenaard823fa92016-08-12 16:29:27 +02004948};
4949
4950/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02004951 * Parse text from 'di' and return the quickfix list items.
4952 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004953 */
4954 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004955qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004956{
4957 int status = FAIL;
4958 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004959 char_u *errorformat = p_efm;
4960 dictitem_T *efm_di;
4961 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004962
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004963 /* Only a List value is supported */
4964 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004965 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004966 /* If errorformat is supplied then use it, otherwise use the 'efm'
4967 * option setting
4968 */
4969 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4970 {
4971 if (efm_di->di_tv.v_type != VAR_STRING ||
4972 efm_di->di_tv.vval.v_string == NULL)
4973 return FAIL;
4974 errorformat = efm_di->di_tv.vval.v_string;
4975 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004976
Bram Moolenaar36538222017-09-02 19:51:44 +02004977 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004978 if (l == NULL)
4979 return FAIL;
4980
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004981 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4982 if (qi != NULL)
4983 {
4984 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4985 qi->qf_refcount++;
4986
Bram Moolenaar36538222017-09-02 19:51:44 +02004987 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004988 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4989 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004990 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004991 qf_free(qi, 0);
4992 }
4993 free(qi);
4994 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004995 dict_add_list(retdict, "items", l);
4996 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004997 }
4998
4999 return status;
5000}
5001
5002/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005003 * Return the quickfix/location list window identifier in the current tabpage.
5004 */
5005 static int
5006qf_winid(qf_info_T *qi)
5007{
5008 win_T *win;
5009
5010 /* The quickfix window can be opened even if the quickfix list is not set
5011 * using ":copen". This is not true for location lists. */
5012 if (qi == NULL)
5013 return 0;
5014 win = qf_find_win(qi);
5015 if (win != NULL)
5016 return win->w_id;
5017 return 0;
5018}
5019
5020/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005021 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005022 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005023 static int
5024qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005025{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005026 int flags = QF_GETLIST_NONE;
5027
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005028 if (dict_find(what, (char_u *)"all", -1) != NULL)
5029 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005030
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005031 if (dict_find(what, (char_u *)"title", -1) != NULL)
5032 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005033
Bram Moolenaara6d48492017-12-12 22:45:31 +01005034 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5035 flags |= QF_GETLIST_NR;
5036
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005037 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5038 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005039
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005040 if (dict_find(what, (char_u *)"context", -1) != NULL)
5041 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005042
Bram Moolenaara6d48492017-12-12 22:45:31 +01005043 if (dict_find(what, (char_u *)"id", -1) != NULL)
5044 flags |= QF_GETLIST_ID;
5045
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005046 if (dict_find(what, (char_u *)"items", -1) != NULL)
5047 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005048
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005049 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5050 flags |= QF_GETLIST_IDX;
5051
5052 if (dict_find(what, (char_u *)"size", -1) != NULL)
5053 flags |= QF_GETLIST_SIZE;
5054
Bram Moolenaarb254af32017-12-18 19:48:58 +01005055 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5056 flags |= QF_GETLIST_TICK;
5057
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005058 return flags;
5059}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005060
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005061/*
5062 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5063 * If 'nr' and 'id' are not present in 'what' then return the current
5064 * quickfix list index.
5065 * If 'nr' is zero then return the current quickfix list index.
5066 * If 'nr' is '$' then return the last quickfix list index.
5067 * If 'id' is present then return the index of the quickfix list with that id.
5068 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5069 * Return -1, if quickfix list is not present or if the stack is empty.
5070 */
5071 static int
5072qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5073{
5074 int qf_idx;
5075 dictitem_T *di;
5076
5077 qf_idx = qi->qf_curlist; /* default is the current list */
5078 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5079 {
5080 /* Use the specified quickfix/location list */
5081 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005082 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005083 /* for zero use the current list */
5084 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005085 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005086 qf_idx = di->di_tv.vval.v_number - 1;
5087 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
5088 qf_idx = -1;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005089 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005090 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005091 else if (di->di_tv.v_type == VAR_STRING
5092 && di->di_tv.vval.v_string != NULL
5093 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5094 /* Get the last quickfix list number */
5095 qf_idx = qi->qf_listcount - 1;
5096 else
5097 qf_idx = -1;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005098 }
5099
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005100 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5101 {
5102 /* Look for a list with the specified id */
5103 if (di->di_tv.v_type == VAR_NUMBER)
5104 {
5105 /*
5106 * For zero, use the current list or the list specified by 'nr'
5107 */
5108 if (di->di_tv.vval.v_number != 0)
5109 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5110 }
5111 else
5112 qf_idx = -1;
5113 }
5114
5115 return qf_idx;
5116}
5117
5118/*
5119 * Return default values for quickfix list properties in retdict.
5120 */
5121 static int
5122qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5123{
5124 int status = OK;
5125
5126 if (flags & QF_GETLIST_TITLE)
5127 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5128 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5129 {
5130 list_T *l = list_alloc();
5131 if (l != NULL)
5132 status = dict_add_list(retdict, "items", l);
5133 else
5134 status = FAIL;
5135 }
5136 if ((status == OK) && (flags & QF_GETLIST_NR))
5137 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5138 if ((status == OK) && (flags & QF_GETLIST_WINID))
5139 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
5140 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5141 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5142 if ((status == OK) && (flags & QF_GETLIST_ID))
5143 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5144 if ((status == OK) && (flags & QF_GETLIST_IDX))
5145 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5146 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5147 status = dict_add_nr_str(retdict, "size", 0L, NULL);
5148 if ((status == OK) && (flags & QF_GETLIST_TICK))
5149 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
5150
5151 return status;
5152}
5153
5154/*
5155 * Return the quickfix list title as 'title' in retdict
5156 */
5157 static int
5158qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5159{
5160 char_u *t;
5161
5162 t = qi->qf_lists[qf_idx].qf_title;
5163 if (t == NULL)
5164 t = (char_u *)"";
5165 return dict_add_nr_str(retdict, "title", 0L, t);
5166}
5167
5168/*
5169 * Return the quickfix list items/entries as 'items' in retdict
5170 */
5171 static int
5172qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5173{
5174 int status = OK;
5175 list_T *l = list_alloc();
5176 if (l != NULL)
5177 {
5178 (void)get_errorlist(qi, NULL, qf_idx, l);
5179 dict_add_list(retdict, "items", l);
5180 }
5181 else
5182 status = FAIL;
5183
5184 return status;
5185}
5186
5187/*
5188 * Return the quickfix list context (if any) as 'context' in retdict.
5189 */
5190 static int
5191qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5192{
5193 int status;
5194 dictitem_T *di;
5195
5196 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5197 {
5198 di = dictitem_alloc((char_u *)"context");
5199 if (di != NULL)
5200 {
5201 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5202 status = dict_add(retdict, di);
5203 if (status == FAIL)
5204 dictitem_free(di);
5205 }
5206 else
5207 status = FAIL;
5208 }
5209 else
5210 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5211
5212 return status;
5213}
5214
5215/*
5216 * Return the quickfix list index as 'idx' in retdict
5217 */
5218 static int
5219qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5220{
5221 int idx = qi->qf_lists[qf_idx].qf_index;
5222 if (qi->qf_lists[qf_idx].qf_count == 0)
5223 /* For empty lists, qf_index is set to 1 */
5224 idx = 0;
5225 return dict_add_nr_str(retdict, "idx", idx, NULL);
5226}
5227
5228/*
5229 * Return quickfix/location list details (title) as a
5230 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5231 * then current list is used. Otherwise the specified list is used.
5232 */
5233 int
5234qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5235{
5236 qf_info_T *qi = &ql_info;
5237 int status = OK;
5238 int qf_idx;
5239 dictitem_T *di;
5240 int flags = QF_GETLIST_NONE;
5241
5242 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5243 return qf_get_list_from_lines(what, di, retdict);
5244
5245 if (wp != NULL)
5246 qi = GET_LOC_LIST(wp);
5247
5248 flags = qf_getprop_keys2flags(what);
5249
5250 if (qi != NULL && qi->qf_listcount != 0)
5251 qf_idx = qf_getprop_qfidx(qi, what);
5252
Bram Moolenaara6d48492017-12-12 22:45:31 +01005253 /* List is not present or is empty */
5254 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005255 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005256
Bram Moolenaard823fa92016-08-12 16:29:27 +02005257 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005258 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005259 if ((status == OK) && (flags & QF_GETLIST_NR))
5260 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5261 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005262 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005263 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005264 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005265 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005266 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005267 if ((status == OK) && (flags & QF_GETLIST_ID))
5268 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5269 NULL);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005270 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005271 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005272 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5273 status = dict_add_nr_str(retdict, "size",
5274 qi->qf_lists[qf_idx].qf_count, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005275 if ((status == OK) && (flags & QF_GETLIST_TICK))
5276 status = dict_add_nr_str(retdict, "changedtick",
5277 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5278
Bram Moolenaard823fa92016-08-12 16:29:27 +02005279 return status;
5280}
5281
5282/*
5283 * Add list of entries to quickfix/location list. Each list entry is
5284 * a dictionary with item information.
5285 */
5286 static int
5287qf_add_entries(
5288 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005289 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005290 list_T *list,
5291 char_u *title,
5292 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005293{
5294 listitem_T *li;
5295 dict_T *d;
5296 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005297 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005298 long lnum;
5299 int col, nr;
5300 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005301 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005302 int valid, status;
5303 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005304 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005305
Bram Moolenaara3921f42017-06-04 15:30:34 +02005306 if (action == ' ' || qf_idx == qi->qf_listcount)
5307 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005308 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005309 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005310 qf_idx = qi->qf_curlist;
5311 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005312 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005313 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005314 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005315 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005316 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005317 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005318 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005319 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005320
5321 for (li = list->lv_first; li != NULL; li = li->li_next)
5322 {
5323 if (li->li_tv.v_type != VAR_DICT)
5324 continue; /* Skip non-dict items */
5325
5326 d = li->li_tv.vval.v_dict;
5327 if (d == NULL)
5328 continue;
5329
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005330 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005331 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5332 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5333 col = (int)get_dict_number(d, (char_u *)"col");
5334 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5335 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005336 type = get_dict_string(d, (char_u *)"type", TRUE);
5337 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5338 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005339 if (text == NULL)
5340 text = vim_strsave((char_u *)"");
5341
5342 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005343 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005344 valid = FALSE;
5345
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005346 /* Mark entries with non-existing buffer number as not valid. Give the
5347 * error message only once. */
5348 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5349 {
5350 if (!did_bufnr_emsg)
5351 {
5352 did_bufnr_emsg = TRUE;
5353 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5354 }
5355 valid = FALSE;
5356 bufnum = 0;
5357 }
5358
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005359 /* If the 'valid' field is present it overrules the detected value. */
5360 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5361 valid = (int)get_dict_number(d, (char_u *)"valid");
5362
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005363 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005364 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005365 NULL, /* dir */
5366 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005367 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005368 text,
5369 lnum,
5370 col,
5371 vcol, /* vis_col */
5372 pattern, /* search pattern */
5373 nr,
5374 type == NULL ? NUL : *type,
5375 valid);
5376
5377 vim_free(filename);
5378 vim_free(pattern);
5379 vim_free(text);
5380 vim_free(type);
5381
5382 if (status == FAIL)
5383 {
5384 retval = FAIL;
5385 break;
5386 }
5387 }
5388
Bram Moolenaara3921f42017-06-04 15:30:34 +02005389 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005390 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005391 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005392 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005393 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005394 if (action != 'a')
5395 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005396 qi->qf_lists[qf_idx].qf_ptr =
5397 qi->qf_lists[qf_idx].qf_start;
5398 if (qi->qf_lists[qf_idx].qf_count > 0)
5399 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005400 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005401
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005402 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005403 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005404
5405 return retval;
5406}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005407
5408 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005409qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005410{
5411 dictitem_T *di;
5412 int retval = FAIL;
5413 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005414 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005415 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005416
5417 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5418 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005419
5420 qf_idx = qi->qf_curlist; /* default is the current list */
5421 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5422 {
5423 /* Use the specified quickfix/location list */
5424 if (di->di_tv.v_type == VAR_NUMBER)
5425 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005426 /* for zero use the current list */
5427 if (di->di_tv.vval.v_number != 0)
5428 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005429
Bram Moolenaar55b69262017-08-13 13:42:01 +02005430 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5431 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005432 /*
5433 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005434 * non-available list and add the new list at the end of the
5435 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005436 */
5437 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005438 qf_idx = qi->qf_listcount - 1;
5439 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005440 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005441 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005442 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005443 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005444 }
5445 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005446 && di->di_tv.vval.v_string != NULL
5447 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005448 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005449 if (qi->qf_listcount > 0)
5450 qf_idx = qi->qf_listcount - 1;
5451 else if (newlist)
5452 qf_idx = 0;
5453 else
5454 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005455 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005456 else
5457 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005458 }
5459
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005460 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5461 {
5462 /* Use the quickfix/location list with the specified id */
5463 if (di->di_tv.v_type == VAR_NUMBER)
5464 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005465 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5466 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005467 return FAIL; /* List not found */
5468 }
5469 else
5470 return FAIL;
5471 }
5472
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005473 if (newlist)
5474 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005475 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005476 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005477 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005478 }
5479
5480 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5481 {
5482 if (di->di_tv.v_type == VAR_STRING)
5483 {
5484 vim_free(qi->qf_lists[qf_idx].qf_title);
5485 qi->qf_lists[qf_idx].qf_title =
5486 get_dict_string(what, (char_u *)"title", TRUE);
5487 if (qf_idx == qi->qf_curlist)
5488 qf_update_win_titlevar(qi);
5489 retval = OK;
5490 }
5491 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005492
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005493 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5494 {
5495 if (di->di_tv.v_type == VAR_LIST)
5496 {
5497 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5498
5499 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5500 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005501 if (action == 'r')
5502 {
5503 /*
5504 * When replacing the quickfix list entries using
5505 * qf_add_entries(), the title is set with a ':' prefix.
5506 * Restore the title with the saved title.
5507 */
5508 vim_free(qi->qf_lists[qf_idx].qf_title);
5509 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5510 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005511 vim_free(title_save);
5512 }
5513 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005514
Bram Moolenaar36538222017-09-02 19:51:44 +02005515 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5516 {
5517 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5518 return FAIL;
5519 errorformat = di->di_tv.vval.v_string;
5520 }
5521
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005522 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005523 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005524 /* Only a List value is supported */
5525 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005526 {
5527 if (action == 'r')
5528 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005529 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005530 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5531 retval = OK;
5532 }
5533 else
5534 return FAIL;
5535 }
5536
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005537 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5538 {
5539 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005540
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005541 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005542 ctx = alloc_tv();
5543 if (ctx != NULL)
5544 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005545 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005546 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005547 }
5548
Bram Moolenaarb254af32017-12-18 19:48:58 +01005549 if (retval == OK)
5550 qf_list_changed(qi, qf_idx);
5551
Bram Moolenaard823fa92016-08-12 16:29:27 +02005552 return retval;
5553}
5554
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005555/*
5556 * Find the non-location list window with the specified location list.
5557 */
5558 static win_T *
5559find_win_with_ll(qf_info_T *qi)
5560{
5561 win_T *wp = NULL;
5562
5563 FOR_ALL_WINDOWS(wp)
5564 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5565 return wp;
5566
5567 return NULL;
5568}
5569
5570/*
5571 * Free the entire quickfix/location list stack.
5572 * If the quickfix/location list window is open, then clear it.
5573 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005574 static void
5575qf_free_stack(win_T *wp, qf_info_T *qi)
5576{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005577 win_T *qfwin = qf_find_win(qi);
5578 win_T *llwin = NULL;
5579 win_T *orig_wp = wp;
5580
5581 if (qfwin != NULL)
5582 {
5583 /* If the quickfix/location list window is open, then clear it */
5584 if (qi->qf_curlist < qi->qf_listcount)
5585 qf_free(qi, qi->qf_curlist);
5586 qf_update_buffer(qi, NULL);
5587 }
5588
5589 if (wp != NULL && IS_LL_WINDOW(wp))
5590 {
5591 /* If in the location list window, then use the non-location list
5592 * window with this location list (if present)
5593 */
5594 llwin = find_win_with_ll(qi);
5595 if (llwin != NULL)
5596 wp = llwin;
5597 }
5598
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005599 qf_free_all(wp);
5600 if (wp == NULL)
5601 {
5602 /* quickfix list */
5603 qi->qf_curlist = 0;
5604 qi->qf_listcount = 0;
5605 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005606 else if (IS_LL_WINDOW(orig_wp))
5607 {
5608 /* If the location list window is open, then create a new empty
5609 * location list */
5610 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005611
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005612 /* first free the list reference in the location list window */
5613 ll_free_all(&orig_wp->w_llist_ref);
5614
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005615 orig_wp->w_llist_ref = new_ll;
5616 if (llwin != NULL)
5617 {
5618 llwin->w_llist = new_ll;
5619 new_ll->qf_refcount++;
5620 }
5621 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005622}
5623
Bram Moolenaard823fa92016-08-12 16:29:27 +02005624/*
5625 * Populate the quickfix list with the items supplied in the list
5626 * of dictionaries. "title" will be copied to w:quickfix_title.
5627 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5628 */
5629 int
5630set_errorlist(
5631 win_T *wp,
5632 list_T *list,
5633 int action,
5634 char_u *title,
5635 dict_T *what)
5636{
5637 qf_info_T *qi = &ql_info;
5638 int retval = OK;
5639
5640 if (wp != NULL)
5641 {
5642 qi = ll_get_or_alloc_list(wp);
5643 if (qi == NULL)
5644 return FAIL;
5645 }
5646
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005647 if (action == 'f')
5648 {
5649 /* Free the entire quickfix or location list stack */
5650 qf_free_stack(wp, qi);
5651 }
5652 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005653 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005654 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005655 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005656 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005657 if (retval == OK)
5658 qf_list_changed(qi, qi->qf_curlist);
5659 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005660
5661 return retval;
5662}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005663
5664 static int
5665mark_quickfix_ctx(qf_info_T *qi, int copyID)
5666{
5667 int i;
5668 int abort = FALSE;
5669 typval_T *ctx;
5670
5671 for (i = 0; i < LISTCOUNT && !abort; ++i)
5672 {
5673 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005674 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5675 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005676 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5677 }
5678
5679 return abort;
5680}
5681
5682/*
5683 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005684 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005685 */
5686 int
5687set_ref_in_quickfix(int copyID)
5688{
5689 int abort = FALSE;
5690 tabpage_T *tp;
5691 win_T *win;
5692
5693 abort = mark_quickfix_ctx(&ql_info, copyID);
5694 if (abort)
5695 return abort;
5696
5697 FOR_ALL_TAB_WINDOWS(tp, win)
5698 {
5699 if (win->w_llist != NULL)
5700 {
5701 abort = mark_quickfix_ctx(win->w_llist, copyID);
5702 if (abort)
5703 return abort;
5704 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005705 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5706 {
5707 /* In a location list window and none of the other windows is
5708 * referring to this location list. Mark the location list
5709 * context as still in use.
5710 */
5711 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5712 if (abort)
5713 return abort;
5714 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005715 }
5716
5717 return abort;
5718}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005719#endif
5720
Bram Moolenaar81695252004-12-29 20:58:21 +00005721/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005722 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005723 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005724 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005725 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005726 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005727 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005728 */
5729 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005730ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005731{
5732 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005733 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005734 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005735 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005736
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005737 switch (eap->cmdidx)
5738 {
5739 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5740 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5741 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5742 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5743 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5744 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5745 default: break;
5746 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005747 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5748 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005749 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005750#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005751 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005752 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005753#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005754 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005755
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005756 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005757 if (eap->cmdidx == CMD_lbuffer
5758 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005759 || eap->cmdidx == CMD_laddbuffer)
5760 {
5761 qi = ll_get_or_alloc_list(curwin);
5762 if (qi == NULL)
5763 return;
5764 }
5765
Bram Moolenaar86b68352004-12-27 21:59:20 +00005766 if (*eap->arg == NUL)
5767 buf = curbuf;
5768 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5769 buf = buflist_findnr(atoi((char *)eap->arg));
5770 if (buf == NULL)
5771 EMSG(_(e_invarg));
5772 else if (buf->b_ml.ml_mfp == NULL)
5773 EMSG(_("E681: Buffer is not loaded"));
5774 else
5775 {
5776 if (eap->addr_count == 0)
5777 {
5778 eap->line1 = 1;
5779 eap->line2 = buf->b_ml.ml_line_count;
5780 }
5781 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5782 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5783 EMSG(_(e_invrange));
5784 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005785 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005786 char_u *qf_title = *eap->cmdlinep;
5787
5788 if (buf->b_sfname)
5789 {
5790 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5791 (char *)qf_title, (char *)buf->b_sfname);
5792 qf_title = IObuff;
5793 }
5794
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005795 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005796 (eap->cmdidx != CMD_caddbuffer
5797 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005798 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005799 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005800 if (res >= 0)
5801 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005802 if (au_name != NULL)
5803 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5804 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005805 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5806 eap->cmdidx == CMD_lbuffer))
5807 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005808 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005809 }
5810}
5811
Bram Moolenaar1e015462005-09-25 22:16:38 +00005812#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005813/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005814 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5815 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005816 */
5817 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005818ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005819{
5820 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005821 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005822 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005823 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005824
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005825 switch (eap->cmdidx)
5826 {
5827 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5828 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5829 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5830 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5831 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5832 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5833 default: break;
5834 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005835 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5836 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005837 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005838#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005839 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005840 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005841#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005842 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005843
Bram Moolenaar3c097222017-12-21 20:54:49 +01005844 if (eap->cmdidx == CMD_lexpr
5845 || eap->cmdidx == CMD_lgetexpr
5846 || eap->cmdidx == CMD_laddexpr)
5847 {
5848 qi = ll_get_or_alloc_list(curwin);
5849 if (qi == NULL)
5850 return;
5851 }
5852
Bram Moolenaar4770d092006-01-12 23:22:24 +00005853 /* Evaluate the expression. When the result is a string or a list we can
5854 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005855 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005856 if (tv != NULL)
5857 {
5858 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5859 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5860 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005861 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005862 (eap->cmdidx != CMD_caddexpr
5863 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005864 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005865 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005866 if (res >= 0)
5867 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005868 if (au_name != NULL)
5869 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5870 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005871 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5872 eap->cmdidx == CMD_lexpr))
5873 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005874 }
5875 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005876 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005877 free_tv(tv);
5878 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005879}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005880#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005881
5882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 * ":helpgrep {pattern}"
5884 */
5885 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005886ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
5888 regmatch_T regmatch;
5889 char_u *save_cpo;
5890 char_u *p;
5891 int fcount;
5892 char_u **fnames;
5893 FILE *fd;
5894 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005896#ifdef FEAT_MULTI_LANG
5897 char_u *lang;
5898#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005899 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005900 int new_qi = FALSE;
5901 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005902 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005904#ifdef FEAT_MULTI_LANG
5905 /* Check for a specified language */
5906 lang = check_help_lang(eap->arg);
5907#endif
5908
Bram Moolenaar73633f82012-01-20 13:39:07 +01005909 switch (eap->cmdidx)
5910 {
5911 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5912 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5913 default: break;
5914 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005915 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5916 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005917 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005918#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005919 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005920 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005921#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005922 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01005923
5924 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5925 save_cpo = p_cpo;
5926 p_cpo = empty_option;
5927
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005928 if (eap->cmdidx == CMD_lhelpgrep)
5929 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005930 /* If the current window is a help window, then use it */
5931 if (bt_help(curwin->w_buffer))
5932 wp = curwin;
5933 else
5934 /* Find an existing help window */
5935 FOR_ALL_WINDOWS(wp)
5936 if (bt_help(wp->w_buffer))
5937 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005938
5939 if (wp == NULL) /* Help window not found */
5940 qi = NULL;
5941 else
5942 qi = wp->w_llist;
5943
5944 if (qi == NULL)
5945 {
5946 /* Allocate a new location list for help text matches */
5947 if ((qi = ll_new_list()) == NULL)
5948 return;
5949 new_qi = TRUE;
5950 }
5951 }
5952
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5954 regmatch.rm_ic = FALSE;
5955 if (regmatch.regprog != NULL)
5956 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005957#ifdef FEAT_MBYTE
5958 vimconv_T vc;
5959
5960 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5961 * differs. */
5962 vc.vc_type = CONV_NONE;
5963 if (!enc_utf8)
5964 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5965#endif
5966
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005968 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
5970 /* Go through all directories in 'runtimepath' */
5971 p = p_rtp;
5972 while (*p != NUL && !got_int)
5973 {
5974 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5975
5976 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5977 add_pathsep(NameBuff);
5978 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5979 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5980 &fnames, EW_FILE|EW_SILENT) == OK
5981 && fcount > 0)
5982 {
5983 for (fi = 0; fi < fcount && !got_int; ++fi)
5984 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005985#ifdef FEAT_MULTI_LANG
5986 /* Skip files for a different language. */
5987 if (lang != NULL
5988 && STRNICMP(lang, fnames[fi]
5989 + STRLEN(fnames[fi]) - 3, 2) != 0
5990 && !(STRNICMP(lang, "en", 2) == 0
5991 && STRNICMP("txt", fnames[fi]
5992 + STRLEN(fnames[fi]) - 3, 3) == 0))
5993 continue;
5994#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005995 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 if (fd != NULL)
5997 {
5998 lnum = 1;
5999 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6000 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006001 char_u *line = IObuff;
6002#ifdef FEAT_MBYTE
6003 /* Convert a line if 'encoding' is not utf-8 and
6004 * the line contains a non-ASCII character. */
6005 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006006 && has_non_ascii(IObuff))
6007 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006008 line = string_convert(&vc, IObuff, NULL);
6009 if (line == NULL)
6010 line = IObuff;
6011 }
6012#endif
6013
6014 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006016 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017
6018 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006019 while (l > 0 && line[l - 1] <= ' ')
6020 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006022 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006023 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 NULL, /* dir */
6025 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006026 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006027 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006029 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00006030 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006031 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006032 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 0, /* nr */
6034 1, /* type */
6035 TRUE /* valid */
6036 ) == FAIL)
6037 {
6038 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006039#ifdef FEAT_MBYTE
6040 if (line != IObuff)
6041 vim_free(line);
6042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 break;
6044 }
6045 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006046#ifdef FEAT_MBYTE
6047 if (line != IObuff)
6048 vim_free(line);
6049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 ++lnum;
6051 line_breakcheck();
6052 }
6053 fclose(fd);
6054 }
6055 }
6056 FreeWild(fcount, fnames);
6057 }
6058 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006059
Bram Moolenaar473de612013-06-08 18:19:48 +02006060 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006061#ifdef FEAT_MBYTE
6062 if (vc.vc_type != CONV_NONE)
6063 convert_setup(&vc, NULL, NULL);
6064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006066 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6067 qi->qf_lists[qi->qf_curlist].qf_ptr =
6068 qi->qf_lists[qi->qf_curlist].qf_start;
6069 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 }
6071
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006072 if (p_cpo == empty_option)
6073 p_cpo = save_cpo;
6074 else
6075 /* Darn, some plugin changed the value. */
6076 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077
Bram Moolenaarb254af32017-12-18 19:48:58 +01006078 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006079 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
Bram Moolenaar73633f82012-01-20 13:39:07 +01006081 if (au_name != NULL)
6082 {
6083 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6084 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006085 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006086 /* autocommands made "qi" invalid */
6087 return;
6088 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006091 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006092 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006093 else
6094 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006095
6096 if (eap->cmdidx == CMD_lhelpgrep)
6097 {
6098 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006099 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006100 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006101 {
6102 if (new_qi)
6103 ll_free_all(&qi);
6104 }
6105 else if (curwin->w_llist == NULL)
6106 curwin->w_llist = qi;
6107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108}
6109
6110#endif /* FEAT_QUICKFIX */