blob: c370761bf20aba56f33c2fbdf31e78f7760b2af7 [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 Moolenaar55b69262017-08-13 13:42:01 +02001186 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 goto qf_init_end;
1188
Bram Moolenaare0d37972016-07-15 22:36:01 +02001189 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
1191 EMSG2(_(e_openerrf), efile);
1192 goto qf_init_end;
1193 }
1194
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001195 if (newlist || qf_idx == qi->qf_listcount)
1196 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001198 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001199 qf_idx = qi->qf_curlist;
1200 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001201 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001202 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001203 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001204 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001205 if (qi->qf_lists[qf_idx].qf_count > 0)
1206 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001209 qfl = &qi->qf_lists[qf_idx];
1210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001212 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001213 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 else
1215 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001217 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001218 * If the errorformat didn't change between calls, then reuse the
1219 * previously parsed values.
1220 */
1221 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1222 {
1223 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001224 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001225 free_efm_list(&fmt_first);
1226
1227 /* parse the current 'efm' */
1228 fmt_first = parse_efm_option(efm);
1229 if (fmt_first != NULL)
1230 last_efm = vim_strsave(efm);
1231 }
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235
1236 /*
1237 * got_int is reset here, because it was probably set when killing the
1238 * ":make" command, but we still want to read the errorfile then.
1239 */
1240 got_int = FALSE;
1241
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001242 if (tv != NULL)
1243 {
1244 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001245 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001246 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001247 state.p_li = tv->vval.v_list->lv_first;
1248 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001249 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001250 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001251 state.buflnum = lnumfirst;
1252 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 /*
1255 * Read the lines in the error file one by one.
1256 * Try to recognize one of the error formats in each line.
1257 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001258 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001260 /* Get the next line from a file/buffer/list/string */
1261 status = qf_get_nextline(&state);
1262 if (status == QF_NOMEM) /* memory alloc failure */
1263 goto qf_init_end;
1264 if (status == QF_END_OF_INPUT) /* end of input */
1265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001267 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1268 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001269 if (status == QF_FAIL)
1270 goto error2;
1271 if (status == QF_NOMEM)
1272 goto qf_init_end;
1273 if (status == QF_IGNORE_LINE)
1274 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001276 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001277 qf_idx,
1278 qfl->qf_directory,
1279 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001280 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001281 : ((qfl->qf_currfile != NULL && fields.valid)
1282 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001283 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001284 fields.errmsg,
1285 fields.lnum,
1286 fields.col,
1287 fields.use_viscol,
1288 fields.pattern,
1289 fields.enr,
1290 fields.type,
1291 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 goto error2;
1293 line_breakcheck();
1294 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001295 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001297 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001299 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001300 qfl->qf_ptr = qfl->qf_start;
1301 qfl->qf_index = 1;
1302 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 else
1305 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001306 qfl->qf_nonevalid = FALSE;
1307 if (qfl->qf_ptr == NULL)
1308 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001310 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001311 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001312 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 EMSG(_(e_readerrf));
1315error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001316 if (!adding)
1317 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001318 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001319 qf_free(qi, qi->qf_curlist);
1320 qi->qf_listcount--;
1321 if (qi->qf_curlist > 0)
1322 --qi->qf_curlist;
1323 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001324qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001325 if (state.fd != NULL)
1326 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001327 vim_free(fields.namebuf);
1328 vim_free(fields.errmsg);
1329 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001330 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001332 if (qf_idx == qi->qf_curlist)
1333 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001334#ifdef FEAT_MBYTE
1335 if (state.vc.vc_type != CONV_NONE)
1336 convert_setup(&state.vc, NULL, NULL);
1337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 return retval;
1340}
1341
Bram Moolenaarfb604092014-07-23 15:55:00 +02001342 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001343qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001344{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001345 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001346
Bram Moolenaarfb604092014-07-23 15:55:00 +02001347 if (title != NULL)
1348 {
1349 char_u *p = alloc((int)STRLEN(title) + 2);
1350
Bram Moolenaara3921f42017-06-04 15:30:34 +02001351 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001352 if (p != NULL)
1353 sprintf((char *)p, ":%s", (char *)title);
1354 }
1355}
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001358 * Prepare for adding a new quickfix list. If the current list is in the
1359 * middle of the stack, then all the following lists are freed and then
1360 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 */
1362 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001363qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 int i;
1366
1367 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001368 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * the current entry. This makes it possible to browse in a tree-like
1370 * way with ":grep'.
1371 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001372 while (qi->qf_listcount > qi->qf_curlist + 1)
1373 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375 /*
1376 * When the stack is full, remove to oldest entry
1377 * Otherwise, add a new entry.
1378 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001379 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001381 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001383 qi->qf_lists[i - 1] = qi->qf_lists[i];
1384 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001387 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001388 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001389 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001390 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391}
1392
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001393/*
1394 * Free a location list
1395 */
1396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001397ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001398{
1399 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001400 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001401
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001402 qi = *pqi;
1403 if (qi == NULL)
1404 return;
1405 *pqi = NULL; /* Remove reference to this list */
1406
1407 qi->qf_refcount--;
1408 if (qi->qf_refcount < 1)
1409 {
1410 /* No references to this location list */
1411 for (i = 0; i < qi->qf_listcount; ++i)
1412 qf_free(qi, i);
1413 vim_free(qi);
1414 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001415}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001416
1417 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001418qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001419{
1420 int i;
1421 qf_info_T *qi = &ql_info;
1422
1423 if (wp != NULL)
1424 {
1425 /* location list */
1426 ll_free_all(&wp->w_llist);
1427 ll_free_all(&wp->w_llist_ref);
1428 }
1429 else
1430 /* quickfix list */
1431 for (i = 0; i < qi->qf_listcount; ++i)
1432 qf_free(qi, i);
1433}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001434
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435/*
1436 * Add an entry to the end of the list of errors.
1437 * Returns OK or FAIL.
1438 */
1439 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001440qf_add_entry(
1441 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001442 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001443 char_u *dir, /* optional directory name */
1444 char_u *fname, /* file name or NULL */
1445 int bufnum, /* buffer number or zero */
1446 char_u *mesg, /* message */
1447 long lnum, /* line number */
1448 int col, /* column */
1449 int vis_col, /* using visual column */
1450 char_u *pattern, /* search pattern */
1451 int nr, /* error number */
1452 int type, /* type character */
1453 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001455 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001456 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001458 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001460 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001461 {
1462 buf_T *buf = buflist_findnr(bufnum);
1463
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001464 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001465 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001466 buf->b_has_qf_entry |=
1467 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001468 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001469 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001470 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1472 {
1473 vim_free(qfp);
1474 return FAIL;
1475 }
1476 qfp->qf_lnum = lnum;
1477 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001478 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001479 if (pattern == NULL || *pattern == NUL)
1480 qfp->qf_pattern = NULL;
1481 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1482 {
1483 vim_free(qfp->qf_text);
1484 vim_free(qfp);
1485 return FAIL;
1486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 qfp->qf_nr = nr;
1488 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1489 type = 0;
1490 qfp->qf_type = type;
1491 qfp->qf_valid = valid;
1492
Bram Moolenaara3921f42017-06-04 15:30:34 +02001493 lastp = &qi->qf_lists[qf_idx].qf_last;
1494 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001495 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001497 qi->qf_lists[qf_idx].qf_start = qfp;
1498 qi->qf_lists[qf_idx].qf_ptr = qfp;
1499 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001500 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502 else
1503 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001504 qfp->qf_prev = *lastp;
1505 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001507 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001509 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001510 ++qi->qf_lists[qf_idx].qf_count;
1511 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001512 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001514 qi->qf_lists[qf_idx].qf_index =
1515 qi->qf_lists[qf_idx].qf_count;
1516 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518
1519 return OK;
1520}
1521
1522/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001523 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001524 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001525 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001526ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001527{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001528 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001529
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001530 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1531 if (qi != NULL)
1532 {
1533 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1534 qi->qf_refcount++;
1535 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001536
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001537 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001538}
1539
1540/*
1541 * Return the location list for window 'wp'.
1542 * If not present, allocate a location list
1543 */
1544 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001545ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001546{
1547 if (IS_LL_WINDOW(wp))
1548 /* For a location list window, use the referenced location list */
1549 return wp->w_llist_ref;
1550
1551 /*
1552 * For a non-location list window, w_llist_ref should not point to a
1553 * location list.
1554 */
1555 ll_free_all(&wp->w_llist_ref);
1556
1557 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001558 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001559 return wp->w_llist;
1560}
1561
1562/*
1563 * Copy the location list from window "from" to window "to".
1564 */
1565 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001566copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001567{
1568 qf_info_T *qi;
1569 int idx;
1570 int i;
1571
1572 /*
1573 * When copying from a location list window, copy the referenced
1574 * location list. For other windows, copy the location list for
1575 * that window.
1576 */
1577 if (IS_LL_WINDOW(from))
1578 qi = from->w_llist_ref;
1579 else
1580 qi = from->w_llist;
1581
1582 if (qi == NULL) /* no location list to copy */
1583 return;
1584
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001585 /* allocate a new location list */
1586 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001587 return;
1588
1589 to->w_llist->qf_listcount = qi->qf_listcount;
1590
1591 /* Copy the location lists one at a time */
1592 for (idx = 0; idx < qi->qf_listcount; idx++)
1593 {
1594 qf_list_T *from_qfl;
1595 qf_list_T *to_qfl;
1596
1597 to->w_llist->qf_curlist = idx;
1598
1599 from_qfl = &qi->qf_lists[idx];
1600 to_qfl = &to->w_llist->qf_lists[idx];
1601
1602 /* Some of the fields are populated by qf_add_entry() */
1603 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1604 to_qfl->qf_count = 0;
1605 to_qfl->qf_index = 0;
1606 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001607 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001608 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001609 if (from_qfl->qf_title != NULL)
1610 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1611 else
1612 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001613 if (from_qfl->qf_ctx != NULL)
1614 {
1615 to_qfl->qf_ctx = alloc_tv();
1616 if (to_qfl->qf_ctx != NULL)
1617 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1618 }
1619 else
1620 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001621
1622 if (from_qfl->qf_count)
1623 {
1624 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001625 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001626
1627 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001628 for (i = 0, from_qfp = from_qfl->qf_start;
1629 i < from_qfl->qf_count && from_qfp != NULL;
1630 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001632 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001633 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001634 NULL,
1635 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001636 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001637 from_qfp->qf_text,
1638 from_qfp->qf_lnum,
1639 from_qfp->qf_col,
1640 from_qfp->qf_viscol,
1641 from_qfp->qf_pattern,
1642 from_qfp->qf_nr,
1643 0,
1644 from_qfp->qf_valid) == FAIL)
1645 {
1646 qf_free_all(to);
1647 return;
1648 }
1649 /*
1650 * qf_add_entry() will not set the qf_num field, as the
1651 * directory and file names are not supplied. So the qf_fnum
1652 * field is copied here.
1653 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001654 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001655 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1656 prevp->qf_type = from_qfp->qf_type; /* error type */
1657 if (from_qfl->qf_ptr == from_qfp)
1658 to_qfl->qf_ptr = prevp; /* current location */
1659 }
1660 }
1661
1662 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1663
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001664 /* Assign a new ID for the location list */
1665 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001666 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001667
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001668 /* When no valid entries are present in the list, qf_ptr points to
1669 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001670 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001671 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001672 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001673 to_qfl->qf_index = 1;
1674 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001675 }
1676
1677 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1678}
1679
1680/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001681 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001682 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 */
1684 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001685qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
Bram Moolenaar82404332016-07-10 17:00:38 +02001687 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001688 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001689 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001690
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if (fname == NULL || *fname == NUL) /* no file name */
1692 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
Bram Moolenaare60acc12011-05-10 16:41:25 +02001694#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001695 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001696#endif
1697#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001698 if (directory != NULL)
1699 slash_adjust(directory);
1700 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001701#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001702 if (directory != NULL && !vim_isAbsName(fname)
1703 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1704 {
1705 /*
1706 * Here we check if the file really exists.
1707 * This should normally be true, but if make works without
1708 * "leaving directory"-messages we might have missed a
1709 * directory change.
1710 */
1711 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001714 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001715 if (directory)
1716 ptr = concat_fnames(directory, fname, TRUE);
1717 else
1718 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001720 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001721 bufname = ptr;
1722 }
1723 else
1724 bufname = fname;
1725
1726 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001727 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001728 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001729 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001730 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001732 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001733 {
1734 vim_free(qf_last_bufname);
1735 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1736 if (bufname == ptr)
1737 qf_last_bufname = bufname;
1738 else
1739 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001740 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001741 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001742 if (buf == NULL)
1743 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001744
Bram Moolenaarc1542742016-07-20 21:44:37 +02001745 buf->b_has_qf_entry =
1746 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001747 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748}
1749
1750/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001751 * Push dirbuf onto the directory stack and return pointer to actual dir or
1752 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 */
1754 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001755qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 struct dir_stack_T *ds_new;
1758 struct dir_stack_T *ds_ptr;
1759
1760 /* allocate new stack element and hook it in */
1761 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1762 if (ds_new == NULL)
1763 return NULL;
1764
1765 ds_new->next = *stackptr;
1766 *stackptr = ds_new;
1767
1768 /* store directory on the stack */
1769 if (vim_isAbsName(dirbuf)
1770 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001771 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 (*stackptr)->dirname = vim_strsave(dirbuf);
1773 else
1774 {
1775 /* Okay we don't have an absolute path.
1776 * dirbuf must be a subdir of one of the directories on the stack.
1777 * Let's search...
1778 */
1779 ds_new = (*stackptr)->next;
1780 (*stackptr)->dirname = NULL;
1781 while (ds_new)
1782 {
1783 vim_free((*stackptr)->dirname);
1784 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1785 TRUE);
1786 if (mch_isdir((*stackptr)->dirname) == TRUE)
1787 break;
1788
1789 ds_new = ds_new->next;
1790 }
1791
1792 /* clean up all dirs we already left */
1793 while ((*stackptr)->next != ds_new)
1794 {
1795 ds_ptr = (*stackptr)->next;
1796 (*stackptr)->next = (*stackptr)->next->next;
1797 vim_free(ds_ptr->dirname);
1798 vim_free(ds_ptr);
1799 }
1800
1801 /* Nothing found -> it must be on top level */
1802 if (ds_new == NULL)
1803 {
1804 vim_free((*stackptr)->dirname);
1805 (*stackptr)->dirname = vim_strsave(dirbuf);
1806 }
1807 }
1808
1809 if ((*stackptr)->dirname != NULL)
1810 return (*stackptr)->dirname;
1811 else
1812 {
1813 ds_ptr = *stackptr;
1814 *stackptr = (*stackptr)->next;
1815 vim_free(ds_ptr);
1816 return NULL;
1817 }
1818}
1819
1820
1821/*
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 Moolenaar7adf06f2017-08-27 15:23:41 +02004951 * Parse text from 'di' and return the quickfix list items
4952 */
4953 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004954qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004955{
4956 int status = FAIL;
4957 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004958 char_u *errorformat = p_efm;
4959 dictitem_T *efm_di;
4960 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004961
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004962 /* Only a List value is supported */
4963 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004964 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004965 /* If errorformat is supplied then use it, otherwise use the 'efm'
4966 * option setting
4967 */
4968 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4969 {
4970 if (efm_di->di_tv.v_type != VAR_STRING ||
4971 efm_di->di_tv.vval.v_string == NULL)
4972 return FAIL;
4973 errorformat = efm_di->di_tv.vval.v_string;
4974 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004975
Bram Moolenaar36538222017-09-02 19:51:44 +02004976 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004977 if (l == NULL)
4978 return FAIL;
4979
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004980 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4981 if (qi != NULL)
4982 {
4983 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4984 qi->qf_refcount++;
4985
Bram Moolenaar36538222017-09-02 19:51:44 +02004986 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004987 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4988 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004989 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004990 qf_free(qi, 0);
4991 }
4992 free(qi);
4993 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004994 dict_add_list(retdict, "items", l);
4995 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004996 }
4997
4998 return status;
4999}
5000
5001/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005002 * Return the quickfix/location list window identifier in the current tabpage.
5003 */
5004 static int
5005qf_winid(qf_info_T *qi)
5006{
5007 win_T *win;
5008
5009 /* The quickfix window can be opened even if the quickfix list is not set
5010 * using ":copen". This is not true for location lists. */
5011 if (qi == NULL)
5012 return 0;
5013 win = qf_find_win(qi);
5014 if (win != NULL)
5015 return win->w_id;
5016 return 0;
5017}
5018
5019/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005020 * Return quickfix/location list details (title) as a
5021 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5022 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005023 */
5024 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005025qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005026{
5027 qf_info_T *qi = &ql_info;
5028 int status = OK;
5029 int qf_idx;
5030 dictitem_T *di;
5031 int flags = QF_GETLIST_NONE;
5032
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005033 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02005034 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005035
Bram Moolenaard823fa92016-08-12 16:29:27 +02005036 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005037 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005038
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005039 if (dict_find(what, (char_u *)"all", -1) != NULL)
5040 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005041
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005042 if (dict_find(what, (char_u *)"title", -1) != NULL)
5043 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005044
Bram Moolenaara6d48492017-12-12 22:45:31 +01005045 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5046 flags |= QF_GETLIST_NR;
5047
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005048 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5049 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005050
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005051 if (dict_find(what, (char_u *)"context", -1) != NULL)
5052 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005053
Bram Moolenaara6d48492017-12-12 22:45:31 +01005054 if (dict_find(what, (char_u *)"id", -1) != NULL)
5055 flags |= QF_GETLIST_ID;
5056
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005057 if (dict_find(what, (char_u *)"items", -1) != NULL)
5058 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005059
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005060 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5061 flags |= QF_GETLIST_IDX;
5062
5063 if (dict_find(what, (char_u *)"size", -1) != NULL)
5064 flags |= QF_GETLIST_SIZE;
5065
Bram Moolenaarb254af32017-12-18 19:48:58 +01005066 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5067 flags |= QF_GETLIST_TICK;
5068
Bram Moolenaara6d48492017-12-12 22:45:31 +01005069 if (qi != NULL && qi->qf_listcount != 0)
5070 {
5071 qf_idx = qi->qf_curlist; /* default is the current list */
5072 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5073 {
5074 /* Use the specified quickfix/location list */
5075 if (di->di_tv.v_type == VAR_NUMBER)
5076 {
5077 /* for zero use the current list */
5078 if (di->di_tv.vval.v_number != 0)
5079 {
5080 qf_idx = di->di_tv.vval.v_number - 1;
5081 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
5082 qf_idx = -1;
5083 }
5084 }
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005085 else if (di->di_tv.v_type == VAR_STRING
5086 && di->di_tv.vval.v_string != NULL
5087 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005088 /* Get the last quickfix list number */
5089 qf_idx = qi->qf_listcount - 1;
5090 else
5091 qf_idx = -1;
5092 flags |= QF_GETLIST_NR;
5093 }
5094
5095 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5096 {
5097 /* Look for a list with the specified id */
5098 if (di->di_tv.v_type == VAR_NUMBER)
5099 {
5100 /*
5101 * For zero, use the current list or the list specifed by 'nr'
5102 */
5103 if (di->di_tv.vval.v_number != 0)
5104 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5105 flags |= QF_GETLIST_ID;
5106 }
5107 else
5108 qf_idx = -1;
5109 }
5110 }
5111
5112 /* List is not present or is empty */
5113 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
5114 {
5115 if (flags & QF_GETLIST_TITLE)
5116 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5117 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5118 {
5119 list_T *l = list_alloc();
5120 if (l != NULL)
5121 status = dict_add_list(retdict, "items", l);
5122 else
5123 status = FAIL;
5124 }
5125 if ((status == OK) && (flags & QF_GETLIST_NR))
5126 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5127 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005128 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005129 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5130 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5131 if ((status == OK) && (flags & QF_GETLIST_ID))
5132 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5133 if ((status == OK) && (flags & QF_GETLIST_IDX))
5134 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5135 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5136 status = dict_add_nr_str(retdict, "size", 0L, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005137 if ((status == OK) && (flags & QF_GETLIST_TICK))
5138 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005139
5140 return status;
5141 }
5142
Bram Moolenaard823fa92016-08-12 16:29:27 +02005143 if (flags & QF_GETLIST_TITLE)
5144 {
5145 char_u *t;
5146 t = qi->qf_lists[qf_idx].qf_title;
5147 if (t == NULL)
5148 t = (char_u *)"";
5149 status = dict_add_nr_str(retdict, "title", 0L, t);
5150 }
5151 if ((status == OK) && (flags & QF_GETLIST_NR))
5152 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5153 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005154 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005155 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5156 {
5157 list_T *l = list_alloc();
5158 if (l != NULL)
5159 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005160 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005161 dict_add_list(retdict, "items", l);
5162 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005163 else
5164 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005165 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005166
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005167 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5168 {
5169 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5170 {
5171 di = dictitem_alloc((char_u *)"context");
5172 if (di != NULL)
5173 {
5174 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005175 status = dict_add(retdict, di);
5176 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02005177 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005178 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005179 else
5180 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005181 }
5182 else
5183 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5184 }
5185
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005186 if ((status == OK) && (flags & QF_GETLIST_ID))
5187 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5188 NULL);
5189
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005190 if ((status == OK) && (flags & QF_GETLIST_IDX))
5191 {
5192 int idx = qi->qf_lists[qf_idx].qf_index;
5193 if (qi->qf_lists[qf_idx].qf_count == 0)
5194 /* For empty lists, qf_index is set to 1 */
5195 idx = 0;
5196 status = dict_add_nr_str(retdict, "idx", idx, NULL);
5197 }
5198
5199 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5200 status = dict_add_nr_str(retdict, "size",
5201 qi->qf_lists[qf_idx].qf_count, NULL);
5202
Bram Moolenaarb254af32017-12-18 19:48:58 +01005203 if ((status == OK) && (flags & QF_GETLIST_TICK))
5204 status = dict_add_nr_str(retdict, "changedtick",
5205 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5206
Bram Moolenaard823fa92016-08-12 16:29:27 +02005207 return status;
5208}
5209
5210/*
5211 * Add list of entries to quickfix/location list. Each list entry is
5212 * a dictionary with item information.
5213 */
5214 static int
5215qf_add_entries(
5216 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005217 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005218 list_T *list,
5219 char_u *title,
5220 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005221{
5222 listitem_T *li;
5223 dict_T *d;
5224 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005225 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005226 long lnum;
5227 int col, nr;
5228 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005229 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005230 int valid, status;
5231 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005232 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005233
Bram Moolenaara3921f42017-06-04 15:30:34 +02005234 if (action == ' ' || qf_idx == qi->qf_listcount)
5235 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005236 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005237 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005238 qf_idx = qi->qf_curlist;
5239 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005240 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005241 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005242 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005243 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005244 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005245 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005246 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005247 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005248
5249 for (li = list->lv_first; li != NULL; li = li->li_next)
5250 {
5251 if (li->li_tv.v_type != VAR_DICT)
5252 continue; /* Skip non-dict items */
5253
5254 d = li->li_tv.vval.v_dict;
5255 if (d == NULL)
5256 continue;
5257
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005258 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005259 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5260 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5261 col = (int)get_dict_number(d, (char_u *)"col");
5262 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5263 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005264 type = get_dict_string(d, (char_u *)"type", TRUE);
5265 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5266 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005267 if (text == NULL)
5268 text = vim_strsave((char_u *)"");
5269
5270 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005271 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005272 valid = FALSE;
5273
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005274 /* Mark entries with non-existing buffer number as not valid. Give the
5275 * error message only once. */
5276 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5277 {
5278 if (!did_bufnr_emsg)
5279 {
5280 did_bufnr_emsg = TRUE;
5281 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5282 }
5283 valid = FALSE;
5284 bufnum = 0;
5285 }
5286
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005287 /* If the 'valid' field is present it overrules the detected value. */
5288 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5289 valid = (int)get_dict_number(d, (char_u *)"valid");
5290
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005291 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005292 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005293 NULL, /* dir */
5294 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005295 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005296 text,
5297 lnum,
5298 col,
5299 vcol, /* vis_col */
5300 pattern, /* search pattern */
5301 nr,
5302 type == NULL ? NUL : *type,
5303 valid);
5304
5305 vim_free(filename);
5306 vim_free(pattern);
5307 vim_free(text);
5308 vim_free(type);
5309
5310 if (status == FAIL)
5311 {
5312 retval = FAIL;
5313 break;
5314 }
5315 }
5316
Bram Moolenaara3921f42017-06-04 15:30:34 +02005317 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005318 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005319 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005320 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005321 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005322 if (action != 'a')
5323 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005324 qi->qf_lists[qf_idx].qf_ptr =
5325 qi->qf_lists[qf_idx].qf_start;
5326 if (qi->qf_lists[qf_idx].qf_count > 0)
5327 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005328 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005329
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005330 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005331 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005332
5333 return retval;
5334}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005335
5336 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005337qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005338{
5339 dictitem_T *di;
5340 int retval = FAIL;
5341 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005342 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005343 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005344
5345 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5346 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005347
5348 qf_idx = qi->qf_curlist; /* default is the current list */
5349 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5350 {
5351 /* Use the specified quickfix/location list */
5352 if (di->di_tv.v_type == VAR_NUMBER)
5353 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005354 /* for zero use the current list */
5355 if (di->di_tv.vval.v_number != 0)
5356 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005357
Bram Moolenaar55b69262017-08-13 13:42:01 +02005358 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5359 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005360 /*
5361 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005362 * non-available list and add the new list at the end of the
5363 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005364 */
5365 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005366 qf_idx = qi->qf_listcount - 1;
5367 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005368 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005369 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005370 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005371 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005372 }
5373 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005374 && di->di_tv.vval.v_string != NULL
5375 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005376 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005377 if (qi->qf_listcount > 0)
5378 qf_idx = qi->qf_listcount - 1;
5379 else if (newlist)
5380 qf_idx = 0;
5381 else
5382 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005383 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005384 else
5385 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005386 }
5387
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005388 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5389 {
5390 /* Use the quickfix/location list with the specified id */
5391 if (di->di_tv.v_type == VAR_NUMBER)
5392 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005393 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5394 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005395 return FAIL; /* List not found */
5396 }
5397 else
5398 return FAIL;
5399 }
5400
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005401 if (newlist)
5402 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005403 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005404 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005405 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005406 }
5407
5408 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5409 {
5410 if (di->di_tv.v_type == VAR_STRING)
5411 {
5412 vim_free(qi->qf_lists[qf_idx].qf_title);
5413 qi->qf_lists[qf_idx].qf_title =
5414 get_dict_string(what, (char_u *)"title", TRUE);
5415 if (qf_idx == qi->qf_curlist)
5416 qf_update_win_titlevar(qi);
5417 retval = OK;
5418 }
5419 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005420
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005421 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5422 {
5423 if (di->di_tv.v_type == VAR_LIST)
5424 {
5425 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5426
5427 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5428 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005429 if (action == 'r')
5430 {
5431 /*
5432 * When replacing the quickfix list entries using
5433 * qf_add_entries(), the title is set with a ':' prefix.
5434 * Restore the title with the saved title.
5435 */
5436 vim_free(qi->qf_lists[qf_idx].qf_title);
5437 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5438 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005439 vim_free(title_save);
5440 }
5441 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005442
Bram Moolenaar36538222017-09-02 19:51:44 +02005443 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5444 {
5445 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5446 return FAIL;
5447 errorformat = di->di_tv.vval.v_string;
5448 }
5449
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005450 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005451 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005452 /* Only a List value is supported */
5453 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005454 {
5455 if (action == 'r')
5456 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005457 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005458 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5459 retval = OK;
5460 }
5461 else
5462 return FAIL;
5463 }
5464
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005465 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5466 {
5467 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005468
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005469 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005470 ctx = alloc_tv();
5471 if (ctx != NULL)
5472 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005473 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005474 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005475 }
5476
Bram Moolenaarb254af32017-12-18 19:48:58 +01005477 if (retval == OK)
5478 qf_list_changed(qi, qf_idx);
5479
Bram Moolenaard823fa92016-08-12 16:29:27 +02005480 return retval;
5481}
5482
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005483/*
5484 * Find the non-location list window with the specified location list.
5485 */
5486 static win_T *
5487find_win_with_ll(qf_info_T *qi)
5488{
5489 win_T *wp = NULL;
5490
5491 FOR_ALL_WINDOWS(wp)
5492 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5493 return wp;
5494
5495 return NULL;
5496}
5497
5498/*
5499 * Free the entire quickfix/location list stack.
5500 * If the quickfix/location list window is open, then clear it.
5501 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005502 static void
5503qf_free_stack(win_T *wp, qf_info_T *qi)
5504{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005505 win_T *qfwin = qf_find_win(qi);
5506 win_T *llwin = NULL;
5507 win_T *orig_wp = wp;
5508
5509 if (qfwin != NULL)
5510 {
5511 /* If the quickfix/location list window is open, then clear it */
5512 if (qi->qf_curlist < qi->qf_listcount)
5513 qf_free(qi, qi->qf_curlist);
5514 qf_update_buffer(qi, NULL);
5515 }
5516
5517 if (wp != NULL && IS_LL_WINDOW(wp))
5518 {
5519 /* If in the location list window, then use the non-location list
5520 * window with this location list (if present)
5521 */
5522 llwin = find_win_with_ll(qi);
5523 if (llwin != NULL)
5524 wp = llwin;
5525 }
5526
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005527 qf_free_all(wp);
5528 if (wp == NULL)
5529 {
5530 /* quickfix list */
5531 qi->qf_curlist = 0;
5532 qi->qf_listcount = 0;
5533 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005534 else if (IS_LL_WINDOW(orig_wp))
5535 {
5536 /* If the location list window is open, then create a new empty
5537 * location list */
5538 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005539
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005540 /* first free the list reference in the location list window */
5541 ll_free_all(&orig_wp->w_llist_ref);
5542
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005543 orig_wp->w_llist_ref = new_ll;
5544 if (llwin != NULL)
5545 {
5546 llwin->w_llist = new_ll;
5547 new_ll->qf_refcount++;
5548 }
5549 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005550}
5551
Bram Moolenaard823fa92016-08-12 16:29:27 +02005552/*
5553 * Populate the quickfix list with the items supplied in the list
5554 * of dictionaries. "title" will be copied to w:quickfix_title.
5555 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5556 */
5557 int
5558set_errorlist(
5559 win_T *wp,
5560 list_T *list,
5561 int action,
5562 char_u *title,
5563 dict_T *what)
5564{
5565 qf_info_T *qi = &ql_info;
5566 int retval = OK;
5567
5568 if (wp != NULL)
5569 {
5570 qi = ll_get_or_alloc_list(wp);
5571 if (qi == NULL)
5572 return FAIL;
5573 }
5574
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005575 if (action == 'f')
5576 {
5577 /* Free the entire quickfix or location list stack */
5578 qf_free_stack(wp, qi);
5579 }
5580 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005581 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005582 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005583 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005584 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005585 if (retval == OK)
5586 qf_list_changed(qi, qi->qf_curlist);
5587 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005588
5589 return retval;
5590}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005591
5592 static int
5593mark_quickfix_ctx(qf_info_T *qi, int copyID)
5594{
5595 int i;
5596 int abort = FALSE;
5597 typval_T *ctx;
5598
5599 for (i = 0; i < LISTCOUNT && !abort; ++i)
5600 {
5601 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005602 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5603 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005604 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5605 }
5606
5607 return abort;
5608}
5609
5610/*
5611 * Mark the context of the quickfix list and the location lists (if present) as
5612 * "in use". So that garabage collection doesn't free the context.
5613 */
5614 int
5615set_ref_in_quickfix(int copyID)
5616{
5617 int abort = FALSE;
5618 tabpage_T *tp;
5619 win_T *win;
5620
5621 abort = mark_quickfix_ctx(&ql_info, copyID);
5622 if (abort)
5623 return abort;
5624
5625 FOR_ALL_TAB_WINDOWS(tp, win)
5626 {
5627 if (win->w_llist != NULL)
5628 {
5629 abort = mark_quickfix_ctx(win->w_llist, copyID);
5630 if (abort)
5631 return abort;
5632 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005633 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5634 {
5635 /* In a location list window and none of the other windows is
5636 * referring to this location list. Mark the location list
5637 * context as still in use.
5638 */
5639 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5640 if (abort)
5641 return abort;
5642 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005643 }
5644
5645 return abort;
5646}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005647#endif
5648
Bram Moolenaar81695252004-12-29 20:58:21 +00005649/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005650 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005651 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005652 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005653 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005654 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005655 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005656 */
5657 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005658ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005659{
5660 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005661 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005662 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005663 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005664
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005665 switch (eap->cmdidx)
5666 {
5667 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5668 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5669 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5670 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5671 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5672 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5673 default: break;
5674 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005675 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5676 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005677 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005678#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005679 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005680 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005681#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005682 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005683
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005684 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005685 if (eap->cmdidx == CMD_lbuffer
5686 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005687 || eap->cmdidx == CMD_laddbuffer)
5688 {
5689 qi = ll_get_or_alloc_list(curwin);
5690 if (qi == NULL)
5691 return;
5692 }
5693
Bram Moolenaar86b68352004-12-27 21:59:20 +00005694 if (*eap->arg == NUL)
5695 buf = curbuf;
5696 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5697 buf = buflist_findnr(atoi((char *)eap->arg));
5698 if (buf == NULL)
5699 EMSG(_(e_invarg));
5700 else if (buf->b_ml.ml_mfp == NULL)
5701 EMSG(_("E681: Buffer is not loaded"));
5702 else
5703 {
5704 if (eap->addr_count == 0)
5705 {
5706 eap->line1 = 1;
5707 eap->line2 = buf->b_ml.ml_line_count;
5708 }
5709 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5710 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5711 EMSG(_(e_invrange));
5712 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005713 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005714 char_u *qf_title = *eap->cmdlinep;
5715
5716 if (buf->b_sfname)
5717 {
5718 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5719 (char *)qf_title, (char *)buf->b_sfname);
5720 qf_title = IObuff;
5721 }
5722
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005723 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005724 (eap->cmdidx != CMD_caddbuffer
5725 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005726 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005727 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005728 if (res >= 0)
5729 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005730 if (au_name != NULL)
5731 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5732 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005733 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5734 eap->cmdidx == CMD_lbuffer))
5735 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005736 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005737 }
5738}
5739
Bram Moolenaar1e015462005-09-25 22:16:38 +00005740#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005741/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005742 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5743 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005744 */
5745 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005746ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005747{
5748 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005749 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005750 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005751 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005752
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005753 switch (eap->cmdidx)
5754 {
5755 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5756 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5757 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5758 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5759 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5760 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5761 default: break;
5762 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005763 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5764 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005765 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005766#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005767 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005768 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005769#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005770 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005771
Bram Moolenaar3c097222017-12-21 20:54:49 +01005772 if (eap->cmdidx == CMD_lexpr
5773 || eap->cmdidx == CMD_lgetexpr
5774 || eap->cmdidx == CMD_laddexpr)
5775 {
5776 qi = ll_get_or_alloc_list(curwin);
5777 if (qi == NULL)
5778 return;
5779 }
5780
Bram Moolenaar4770d092006-01-12 23:22:24 +00005781 /* Evaluate the expression. When the result is a string or a list we can
5782 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005783 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005784 if (tv != NULL)
5785 {
5786 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5787 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5788 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005789 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005790 (eap->cmdidx != CMD_caddexpr
5791 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005792 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005793 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005794 if (res >= 0)
5795 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005796 if (au_name != NULL)
5797 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5798 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005799 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5800 eap->cmdidx == CMD_lexpr))
5801 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005802 }
5803 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005804 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005805 free_tv(tv);
5806 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005807}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005808#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005809
5810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 * ":helpgrep {pattern}"
5812 */
5813 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005814ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 regmatch_T regmatch;
5817 char_u *save_cpo;
5818 char_u *p;
5819 int fcount;
5820 char_u **fnames;
5821 FILE *fd;
5822 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005824#ifdef FEAT_MULTI_LANG
5825 char_u *lang;
5826#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005827 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005828 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005829 int new_qi = FALSE;
5830 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005831 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005833#ifdef FEAT_MULTI_LANG
5834 /* Check for a specified language */
5835 lang = check_help_lang(eap->arg);
5836#endif
5837
Bram Moolenaar73633f82012-01-20 13:39:07 +01005838 switch (eap->cmdidx)
5839 {
5840 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5841 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5842 default: break;
5843 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005844 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5845 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005846 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005847#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005848 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005849 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005850#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005851 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01005852
5853 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5854 save_cpo = p_cpo;
5855 p_cpo = empty_option;
5856
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005857 if (eap->cmdidx == CMD_lhelpgrep)
5858 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005859 /* If the current window is a help window, then use it */
5860 if (bt_help(curwin->w_buffer))
5861 wp = curwin;
5862 else
5863 /* Find an existing help window */
5864 FOR_ALL_WINDOWS(wp)
5865 if (bt_help(wp->w_buffer))
5866 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005867
5868 if (wp == NULL) /* Help window not found */
5869 qi = NULL;
5870 else
5871 qi = wp->w_llist;
5872
5873 if (qi == NULL)
5874 {
5875 /* Allocate a new location list for help text matches */
5876 if ((qi = ll_new_list()) == NULL)
5877 return;
5878 new_qi = TRUE;
5879 }
5880 }
5881
Bram Moolenaaree85df32017-03-19 14:19:50 +01005882 /* Autocommands may change the list. Save it for later comparison */
5883 save_qi = qi;
5884
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5886 regmatch.rm_ic = FALSE;
5887 if (regmatch.regprog != NULL)
5888 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005889#ifdef FEAT_MBYTE
5890 vimconv_T vc;
5891
5892 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5893 * differs. */
5894 vc.vc_type = CONV_NONE;
5895 if (!enc_utf8)
5896 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5897#endif
5898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005900 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902 /* Go through all directories in 'runtimepath' */
5903 p = p_rtp;
5904 while (*p != NUL && !got_int)
5905 {
5906 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5907
5908 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5909 add_pathsep(NameBuff);
5910 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5911 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5912 &fnames, EW_FILE|EW_SILENT) == OK
5913 && fcount > 0)
5914 {
5915 for (fi = 0; fi < fcount && !got_int; ++fi)
5916 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005917#ifdef FEAT_MULTI_LANG
5918 /* Skip files for a different language. */
5919 if (lang != NULL
5920 && STRNICMP(lang, fnames[fi]
5921 + STRLEN(fnames[fi]) - 3, 2) != 0
5922 && !(STRNICMP(lang, "en", 2) == 0
5923 && STRNICMP("txt", fnames[fi]
5924 + STRLEN(fnames[fi]) - 3, 3) == 0))
5925 continue;
5926#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005927 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 if (fd != NULL)
5929 {
5930 lnum = 1;
5931 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5932 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005933 char_u *line = IObuff;
5934#ifdef FEAT_MBYTE
5935 /* Convert a line if 'encoding' is not utf-8 and
5936 * the line contains a non-ASCII character. */
5937 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005938 && has_non_ascii(IObuff))
5939 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005940 line = string_convert(&vc, IObuff, NULL);
5941 if (line == NULL)
5942 line = IObuff;
5943 }
5944#endif
5945
5946 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005948 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
5950 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005951 while (l > 0 && line[l - 1] <= ' ')
5952 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005954 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005955 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 NULL, /* dir */
5957 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005958 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005959 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005961 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005962 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005963 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005964 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 0, /* nr */
5966 1, /* type */
5967 TRUE /* valid */
5968 ) == FAIL)
5969 {
5970 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005971#ifdef FEAT_MBYTE
5972 if (line != IObuff)
5973 vim_free(line);
5974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 break;
5976 }
5977 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005978#ifdef FEAT_MBYTE
5979 if (line != IObuff)
5980 vim_free(line);
5981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 ++lnum;
5983 line_breakcheck();
5984 }
5985 fclose(fd);
5986 }
5987 }
5988 FreeWild(fcount, fnames);
5989 }
5990 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005991
Bram Moolenaar473de612013-06-08 18:19:48 +02005992 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005993#ifdef FEAT_MBYTE
5994 if (vc.vc_type != CONV_NONE)
5995 convert_setup(&vc, NULL, NULL);
5996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005998 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5999 qi->qf_lists[qi->qf_curlist].qf_ptr =
6000 qi->qf_lists[qi->qf_curlist].qf_start;
6001 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 }
6003
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006004 if (p_cpo == empty_option)
6005 p_cpo = save_cpo;
6006 else
6007 /* Darn, some plugin changed the value. */
6008 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
Bram Moolenaarb254af32017-12-18 19:48:58 +01006010 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006011 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012
Bram Moolenaar73633f82012-01-20 13:39:07 +01006013 if (au_name != NULL)
6014 {
6015 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6016 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01006017 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006018 /* autocommands made "qi" invalid */
6019 return;
6020 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006021
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006023 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006024 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006025 else
6026 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006027
6028 if (eap->cmdidx == CMD_lhelpgrep)
6029 {
6030 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006031 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006032 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006033 {
6034 if (new_qi)
6035 ll_free_all(&qi);
6036 }
6037 else if (curwin->w_llist == NULL)
6038 curwin->w_llist = qi;
6039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040}
6041
6042#endif /* FEAT_QUICKFIX */