blob: acff1ec7036f2cedaeffc5755e0b3759fc2592d5 [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
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020049#define INVALID_QFIDX (-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000050
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020051/*
52 * Quickfix/Location list definition
53 * Contains a list of entries (qfline_T). qf_start points to the first entry
54 * and qf_last points to the last entry. qf_count contains the list size.
55 *
56 * Usually the list contains one or more entries. But an empty list can be
57 * created using setqflist()/setloclist() with a title and/or user context
58 * information and entries can be added later using setqflist()/setloclist().
59 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000060typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000061{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020062 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000063 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020064 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000065 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020066 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000067 int qf_index; /* current index in the error list */
68 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020070 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020071 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020072
73 struct dir_stack_T *qf_dir_stack;
74 char_u *qf_directory;
75 struct dir_stack_T *qf_file_stack;
76 char_u *qf_currfile;
77 int qf_multiline;
78 int qf_multiignore;
79 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010080 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000081} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000082
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020083/*
84 * Quickfix/Location list stack definition
85 * Contains a list of quickfix/location lists (qf_list_T)
86 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000087struct qf_info_S
88{
89 /*
90 * Count of references to this list. Used only for location lists.
91 * When a location list window reference this list, qf_refcount
92 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
93 * reaches 0, the list is freed.
94 */
95 int qf_refcount;
96 int qf_listcount; /* current number of lists */
97 int qf_curlist; /* current error list */
98 qf_list_T qf_lists[LISTCOUNT];
99};
100
101static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200102static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000104#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105
106/*
107 * Structure used to hold the info of one part of 'errorformat'
108 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000109typedef struct efm_S efm_T;
110struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111{
112 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000113 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
115 char_u prefix; /* prefix of this format line: */
116 /* 'D' enter directory */
117 /* 'X' leave directory */
118 /* 'A' start of multi-line message */
119 /* 'E' error message */
120 /* 'W' warning message */
121 /* 'I' informational message */
122 /* 'C' continuation line */
123 /* 'Z' end of multi-line message */
124 /* 'G' general, unspecific message */
125 /* 'P' push file (partial) message */
126 /* 'Q' pop/quit file (partial) message */
127 /* 'O' overread (partial) message */
128 char_u flags; /* additional flags given in prefix */
129 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000130 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000131 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132};
133
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100134static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
135
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200136static 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 +0100137static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200138static 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 +0100139static void qf_free(qf_info_T *qi, int idx);
140static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200141static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200142static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200144static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static win_T *qf_find_win(qf_info_T *qi);
148static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200149static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200151static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
154static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
155static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
156static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000158/* Quickfix window check helper macro */
159#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
160/* Location list window check helper macro */
161#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
162/*
163 * Return location list for window 'wp'
164 * For location list window, return the referenced location list
165 */
166#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200169 * Looking up a buffer can be slow if there are many. Remember the last one
170 * to make this a lot faster if there are multiple matches in the same file.
171 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200172static char_u *qf_last_bufname = NULL;
173static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200174
Bram Moolenaar3c097222017-12-21 20:54:49 +0100175static char *e_loc_list_changed =
176 N_("E926: Current location list was changed");
177
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000179 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200180 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 * Return -1 for error, number of errors for success.
182 */
183 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200184qf_init(win_T *wp,
185 char_u *efile,
186 char_u *errorformat,
187 int newlist, /* TRUE: start a new error list */
188 char_u *qf_title,
189 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000191 qf_info_T *qi = &ql_info;
192
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000193 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000194 {
195 qi = ll_get_or_alloc_list(wp);
196 if (qi == NULL)
197 return FAIL;
198 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000199
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200200 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
201 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000202}
203
204/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200205 * Maximum number of bytes allowed per line while reading a errorfile.
206 */
207#define LINE_MAXLEN 4096
208
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200209static struct fmtpattern
210{
211 char_u convchar;
212 char *pattern;
213} fmt_pat[FMT_PATTERNS] =
214 {
215 {'f', ".\\+"}, /* only used when at end */
216 {'n', "\\d\\+"},
217 {'l', "\\d\\+"},
218 {'c', "\\d\\+"},
219 {'t', "."},
220 {'m', ".\\+"},
221 {'r', ".*"},
222 {'p', "[- .]*"},
223 {'v', "\\d\\+"},
224 {'s', ".\\+"}
225 };
226
227/*
228 * Converts a 'errorformat' string to regular expression pattern
229 */
230 static int
231efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200232 char_u *efm,
233 int len,
234 efm_T *fmt_ptr,
235 char_u *regpat,
236 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200237{
238 char_u *ptr;
239 char_u *efmp;
240 char_u *srcptr;
241 int round;
242 int idx = 0;
243
244 /*
245 * Build regexp pattern from current 'errorformat' option
246 */
247 ptr = regpat;
248 *ptr++ = '^';
249 round = 0;
250 for (efmp = efm; efmp < efm + len; ++efmp)
251 {
252 if (*efmp == '%')
253 {
254 ++efmp;
255 for (idx = 0; idx < FMT_PATTERNS; ++idx)
256 if (fmt_pat[idx].convchar == *efmp)
257 break;
258 if (idx < FMT_PATTERNS)
259 {
260 if (fmt_ptr->addr[idx])
261 {
262 sprintf((char *)errmsg,
263 _("E372: Too many %%%c in format string"), *efmp);
264 EMSG(errmsg);
265 return -1;
266 }
267 if ((idx
268 && idx < 6
269 && vim_strchr((char_u *)"DXOPQ",
270 fmt_ptr->prefix) != NULL)
271 || (idx == 6
272 && vim_strchr((char_u *)"OPQ",
273 fmt_ptr->prefix) == NULL))
274 {
275 sprintf((char *)errmsg,
276 _("E373: Unexpected %%%c in format string"), *efmp);
277 EMSG(errmsg);
278 return -1;
279 }
280 fmt_ptr->addr[idx] = (char_u)++round;
281 *ptr++ = '\\';
282 *ptr++ = '(';
283#ifdef BACKSLASH_IN_FILENAME
284 if (*efmp == 'f')
285 {
286 /* Also match "c:" in the file name, even when
287 * checking for a colon next: "%f:".
288 * "\%(\a:\)\=" */
289 STRCPY(ptr, "\\%(\\a:\\)\\=");
290 ptr += 10;
291 }
292#endif
293 if (*efmp == 'f' && efmp[1] != NUL)
294 {
295 if (efmp[1] != '\\' && efmp[1] != '%')
296 {
297 /* A file name may contain spaces, but this isn't
298 * in "\f". For "%f:%l:%m" there may be a ":" in
299 * the file name. Use ".\{-1,}x" instead (x is
300 * the next character), the requirement that :999:
301 * follows should work. */
302 STRCPY(ptr, ".\\{-1,}");
303 ptr += 7;
304 }
305 else
306 {
307 /* File name followed by '\\' or '%': include as
308 * many file name chars as possible. */
309 STRCPY(ptr, "\\f\\+");
310 ptr += 4;
311 }
312 }
313 else
314 {
315 srcptr = (char_u *)fmt_pat[idx].pattern;
316 while ((*ptr = *srcptr++) != NUL)
317 ++ptr;
318 }
319 *ptr++ = '\\';
320 *ptr++ = ')';
321 }
322 else if (*efmp == '*')
323 {
324 if (*++efmp == '[' || *efmp == '\\')
325 {
326 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
327 {
328 if (efmp[1] == '^')
329 *ptr++ = *++efmp;
330 if (efmp < efm + len)
331 {
332 *ptr++ = *++efmp; /* could be ']' */
333 while (efmp < efm + len
334 && (*ptr++ = *++efmp) != ']')
335 /* skip */;
336 if (efmp == efm + len)
337 {
338 EMSG(_("E374: Missing ] in format string"));
339 return -1;
340 }
341 }
342 }
343 else if (efmp < efm + len) /* %*\D, %*\s etc. */
344 *ptr++ = *++efmp;
345 *ptr++ = '\\';
346 *ptr++ = '+';
347 }
348 else
349 {
350 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
351 sprintf((char *)errmsg,
352 _("E375: Unsupported %%%c in format string"), *efmp);
353 EMSG(errmsg);
354 return -1;
355 }
356 }
357 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
358 *ptr++ = *efmp; /* regexp magic characters */
359 else if (*efmp == '#')
360 *ptr++ = '*';
361 else if (*efmp == '>')
362 fmt_ptr->conthere = TRUE;
363 else if (efmp == efm + 1) /* analyse prefix */
364 {
365 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
366 fmt_ptr->flags = *efmp++;
367 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
368 fmt_ptr->prefix = *efmp;
369 else
370 {
371 sprintf((char *)errmsg,
372 _("E376: Invalid %%%c in format string prefix"), *efmp);
373 EMSG(errmsg);
374 return -1;
375 }
376 }
377 else
378 {
379 sprintf((char *)errmsg,
380 _("E377: Invalid %%%c in format string"), *efmp);
381 EMSG(errmsg);
382 return -1;
383 }
384 }
385 else /* copy normal character */
386 {
387 if (*efmp == '\\' && efmp + 1 < efm + len)
388 ++efmp;
389 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
390 *ptr++ = '\\'; /* escape regexp atoms */
391 if (*efmp)
392 *ptr++ = *efmp;
393 }
394 }
395 *ptr++ = '$';
396 *ptr = NUL;
397
398 return 0;
399}
400
401 static void
402free_efm_list(efm_T **efm_first)
403{
404 efm_T *efm_ptr;
405
406 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
407 {
408 *efm_first = efm_ptr->next;
409 vim_regfree(efm_ptr->prog);
410 vim_free(efm_ptr);
411 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100412 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200413}
414
415/* Parse 'errorformat' option */
416 static efm_T *
417parse_efm_option(char_u *efm)
418{
419 char_u *errmsg = NULL;
420 int errmsglen;
421 efm_T *fmt_ptr = NULL;
422 efm_T *fmt_first = NULL;
423 efm_T *fmt_last = NULL;
424 char_u *fmtstr = NULL;
425 int len;
426 int i;
427 int round;
428
429 errmsglen = CMDBUFFSIZE + 1;
430 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
431 if (errmsg == NULL)
432 goto parse_efm_end;
433
434 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200435 * Each part of the format string is copied and modified from errorformat
436 * to regex prog. Only a few % characters are allowed.
437 */
438
439 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200440 * Get some space to modify the format string into.
441 */
442 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
443 for (round = FMT_PATTERNS; round > 0; )
444 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200445#ifdef BACKSLASH_IN_FILENAME
446 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200447#else
448 i += 2; /* "%f" can become two chars longer */
449#endif
450 if ((fmtstr = alloc(i)) == NULL)
451 goto parse_efm_error;
452
453 while (efm[0] != NUL)
454 {
455 /*
456 * Allocate a new eformat structure and put it at the end of the list
457 */
458 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
459 if (fmt_ptr == NULL)
460 goto parse_efm_error;
461 if (fmt_first == NULL) /* first one */
462 fmt_first = fmt_ptr;
463 else
464 fmt_last->next = fmt_ptr;
465 fmt_last = fmt_ptr;
466
467 /*
468 * Isolate one part in the 'errorformat' option
469 */
470 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
471 if (efm[len] == '\\' && efm[len + 1] != NUL)
472 ++len;
473
474 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
475 goto parse_efm_error;
476 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
477 goto parse_efm_error;
478 /*
479 * Advance to next part
480 */
481 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
482 }
483
484 if (fmt_first == NULL) /* nothing found */
485 EMSG(_("E378: 'errorformat' contains no pattern"));
486
487 goto parse_efm_end;
488
489parse_efm_error:
490 free_efm_list(&fmt_first);
491
492parse_efm_end:
493 vim_free(fmtstr);
494 vim_free(errmsg);
495
496 return fmt_first;
497}
498
Bram Moolenaare0d37972016-07-15 22:36:01 +0200499enum {
500 QF_FAIL = 0,
501 QF_OK = 1,
502 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200503 QF_NOMEM = 3,
504 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200505};
506
507typedef struct {
508 char_u *linebuf;
509 int linelen;
510 char_u *growbuf;
511 int growbufsiz;
512 FILE *fd;
513 typval_T *tv;
514 char_u *p_str;
515 listitem_T *p_li;
516 buf_T *buf;
517 linenr_T buflnum;
518 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100519 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200520} qfstate_T;
521
522 static char_u *
523qf_grow_linebuf(qfstate_T *state, int newsz)
524{
525 /*
526 * If the line exceeds LINE_MAXLEN exclude the last
527 * byte since it's not a NL character.
528 */
529 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
530 if (state->growbuf == NULL)
531 {
532 state->growbuf = alloc(state->linelen + 1);
533 if (state->growbuf == NULL)
534 return NULL;
535 state->growbufsiz = state->linelen;
536 }
537 else if (state->linelen > state->growbufsiz)
538 {
539 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
540 if (state->growbuf == NULL)
541 return NULL;
542 state->growbufsiz = state->linelen;
543 }
544 return state->growbuf;
545}
546
547/*
548 * Get the next string (separated by newline) from state->p_str.
549 */
550 static int
551qf_get_next_str_line(qfstate_T *state)
552{
553 /* Get the next line from the supplied string */
554 char_u *p_str = state->p_str;
555 char_u *p;
556 int len;
557
558 if (*p_str == NUL) /* Reached the end of the string */
559 return QF_END_OF_INPUT;
560
561 p = vim_strchr(p_str, '\n');
562 if (p != NULL)
563 len = (int)(p - p_str) + 1;
564 else
565 len = (int)STRLEN(p_str);
566
567 if (len > IOSIZE - 2)
568 {
569 state->linebuf = qf_grow_linebuf(state, len);
570 if (state->linebuf == NULL)
571 return QF_NOMEM;
572 }
573 else
574 {
575 state->linebuf = IObuff;
576 state->linelen = len;
577 }
578 vim_strncpy(state->linebuf, p_str, state->linelen);
579
580 /*
581 * Increment using len in order to discard the rest of the
582 * line if it exceeds LINE_MAXLEN.
583 */
584 p_str += len;
585 state->p_str = p_str;
586
587 return QF_OK;
588}
589
590/*
591 * Get the next string from state->p_Li.
592 */
593 static int
594qf_get_next_list_line(qfstate_T *state)
595{
596 listitem_T *p_li = state->p_li;
597 int len;
598
599 while (p_li != NULL
600 && (p_li->li_tv.v_type != VAR_STRING
601 || p_li->li_tv.vval.v_string == NULL))
602 p_li = p_li->li_next; /* Skip non-string items */
603
604 if (p_li == NULL) /* End of the list */
605 {
606 state->p_li = NULL;
607 return QF_END_OF_INPUT;
608 }
609
610 len = (int)STRLEN(p_li->li_tv.vval.v_string);
611 if (len > IOSIZE - 2)
612 {
613 state->linebuf = qf_grow_linebuf(state, len);
614 if (state->linebuf == NULL)
615 return QF_NOMEM;
616 }
617 else
618 {
619 state->linebuf = IObuff;
620 state->linelen = len;
621 }
622
623 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
624
625 state->p_li = p_li->li_next; /* next item */
626 return QF_OK;
627}
628
629/*
630 * Get the next string from state->buf.
631 */
632 static int
633qf_get_next_buf_line(qfstate_T *state)
634{
635 char_u *p_buf = NULL;
636 int len;
637
638 /* Get the next line from the supplied buffer */
639 if (state->buflnum > state->lnumlast)
640 return QF_END_OF_INPUT;
641
642 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
643 state->buflnum += 1;
644
645 len = (int)STRLEN(p_buf);
646 if (len > IOSIZE - 2)
647 {
648 state->linebuf = qf_grow_linebuf(state, len);
649 if (state->linebuf == NULL)
650 return QF_NOMEM;
651 }
652 else
653 {
654 state->linebuf = IObuff;
655 state->linelen = len;
656 }
657 vim_strncpy(state->linebuf, p_buf, state->linelen);
658
659 return QF_OK;
660}
661
662/*
663 * Get the next string from file state->fd.
664 */
665 static int
666qf_get_next_file_line(qfstate_T *state)
667{
668 int discard;
669 int growbuflen;
670
671 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
672 return QF_END_OF_INPUT;
673
674 discard = FALSE;
675 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200676 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200677 {
678 /*
679 * The current line exceeds IObuff, continue reading using
680 * growbuf until EOL or LINE_MAXLEN bytes is read.
681 */
682 if (state->growbuf == NULL)
683 {
684 state->growbufsiz = 2 * (IOSIZE - 1);
685 state->growbuf = alloc(state->growbufsiz);
686 if (state->growbuf == NULL)
687 return QF_NOMEM;
688 }
689
690 /* Copy the read part of the line, excluding null-terminator */
691 memcpy(state->growbuf, IObuff, IOSIZE - 1);
692 growbuflen = state->linelen;
693
694 for (;;)
695 {
696 if (fgets((char *)state->growbuf + growbuflen,
697 state->growbufsiz - growbuflen, state->fd) == NULL)
698 break;
699 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
700 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200701 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200702 break;
703 if (state->growbufsiz == LINE_MAXLEN)
704 {
705 discard = TRUE;
706 break;
707 }
708
709 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
710 ? 2 * state->growbufsiz : LINE_MAXLEN;
711 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
712 if (state->growbuf == NULL)
713 return QF_NOMEM;
714 }
715
716 while (discard)
717 {
718 /*
719 * The current line is longer than LINE_MAXLEN, continue
720 * reading but discard everything until EOL or EOF is
721 * reached.
722 */
723 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
724 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200725 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200726 break;
727 }
728
729 state->linebuf = state->growbuf;
730 state->linelen = growbuflen;
731 }
732 else
733 state->linebuf = IObuff;
734
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100735#ifdef FEAT_MBYTE
736 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200737 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
738 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100739 char_u *line;
740
741 line = string_convert(&state->vc, state->linebuf, &state->linelen);
742 if (line != NULL)
743 {
744 if (state->linelen < IOSIZE)
745 {
746 STRCPY(state->linebuf, line);
747 vim_free(line);
748 }
749 else
750 {
751 vim_free(state->growbuf);
752 state->linebuf = state->growbuf = line;
753 state->growbufsiz = state->linelen < LINE_MAXLEN
754 ? state->linelen : LINE_MAXLEN;
755 }
756 }
757 }
758#endif
759
Bram Moolenaare0d37972016-07-15 22:36:01 +0200760 return QF_OK;
761}
762
763/*
764 * Get the next string from a file/buffer/list/string.
765 */
766 static int
767qf_get_nextline(qfstate_T *state)
768{
769 int status = QF_FAIL;
770
771 if (state->fd == NULL)
772 {
773 if (state->tv != NULL)
774 {
775 if (state->tv->v_type == VAR_STRING)
776 /* Get the next line from the supplied string */
777 status = qf_get_next_str_line(state);
778 else if (state->tv->v_type == VAR_LIST)
779 /* Get the next line from the supplied list */
780 status = qf_get_next_list_line(state);
781 }
782 else
783 /* Get the next line from the supplied buffer */
784 status = qf_get_next_buf_line(state);
785 }
786 else
787 /* Get the next line from the supplied file */
788 status = qf_get_next_file_line(state);
789
790 if (status != QF_OK)
791 return status;
792
793 /* remove newline/CR from the line */
794 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200795 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200796 state->linebuf[state->linelen - 1] = NUL;
797#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200798 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
799 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200800#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200801 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802
803#ifdef FEAT_MBYTE
804 remove_bom(state->linebuf);
805#endif
806
807 return QF_OK;
808}
809
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200810typedef struct {
811 char_u *namebuf;
812 char_u *errmsg;
813 int errmsglen;
814 long lnum;
815 int col;
816 char_u use_viscol;
817 char_u *pattern;
818 int enr;
819 int type;
820 int valid;
821} qffields_T;
822
823/*
824 * Parse a line and get the quickfix fields.
825 * Return the QF_ status.
826 */
827 static int
828qf_parse_line(
829 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200830 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200831 char_u *linebuf,
832 int linelen,
833 efm_T *fmt_first,
834 qffields_T *fields)
835{
836 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200837 char_u *ptr;
838 int len;
839 int i;
840 int idx = 0;
841 char_u *tail = NULL;
842 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200843 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200844
845 /* Always ignore case when looking for a matching error. */
846 regmatch.rm_ic = TRUE;
847
Bram Moolenaare333e792018-04-08 13:27:39 +0200848restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200849 /* If there was no %> item start at the first pattern */
850 if (fmt_start == NULL)
851 fmt_ptr = fmt_first;
852 else
853 {
854 fmt_ptr = fmt_start;
855 fmt_start = NULL;
856 }
857
858 /*
859 * Try to match each part of 'errorformat' until we find a complete
860 * match or no match.
861 */
862 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200863 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
864 {
865 int r;
866
867 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200868 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200869 continue;
870 fields->namebuf[0] = NUL;
871 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200872 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200873 fields->errmsg[0] = NUL;
874 fields->lnum = 0;
875 fields->col = 0;
876 fields->use_viscol = FALSE;
877 fields->enr = -1;
878 fields->type = 0;
879 tail = NULL;
880
881 regmatch.regprog = fmt_ptr->prog;
882 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
883 fmt_ptr->prog = regmatch.regprog;
884 if (r)
885 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200886 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200887 continue;
888 if (vim_strchr((char_u *)"EWI", idx) != NULL)
889 fields->type = idx;
890 else
891 fields->type = 0;
892 /*
893 * Extract error message data from matched line.
894 * We check for an actual submatch, because "\[" and "\]" in
895 * the 'errorformat' may cause the wrong submatch to be used.
896 */
897 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
898 {
899 int c;
900
901 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
902 continue;
903
904 /* Expand ~/file and $HOME/file to full path. */
905 c = *regmatch.endp[i];
906 *regmatch.endp[i] = NUL;
907 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
908 *regmatch.endp[i] = c;
909
910 if (vim_strchr((char_u *)"OPQ", idx) != NULL
911 && mch_getperm(fields->namebuf) == -1)
912 continue;
913 }
914 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
915 {
916 if (regmatch.startp[i] == NULL)
917 continue;
918 fields->enr = (int)atol((char *)regmatch.startp[i]);
919 }
920 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
921 {
922 if (regmatch.startp[i] == NULL)
923 continue;
924 fields->lnum = atol((char *)regmatch.startp[i]);
925 }
926 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
927 {
928 if (regmatch.startp[i] == NULL)
929 continue;
930 fields->col = (int)atol((char *)regmatch.startp[i]);
931 }
932 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
933 {
934 if (regmatch.startp[i] == NULL)
935 continue;
936 fields->type = *regmatch.startp[i];
937 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200938 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200939 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200940 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200941 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200942 /* linelen + null terminator */
943 if ((fields->errmsg = vim_realloc(fields->errmsg,
944 linelen + 1)) == NULL)
945 return QF_NOMEM;
946 fields->errmsglen = linelen + 1;
947 }
948 vim_strncpy(fields->errmsg, linebuf, linelen);
949 }
950 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
951 {
952 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
953 continue;
954 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200955 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200956 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200957 /* len + null terminator */
958 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
959 == NULL)
960 return QF_NOMEM;
961 fields->errmsglen = len + 1;
962 }
963 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
964 }
965 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
966 {
967 if (regmatch.startp[i] == NULL)
968 continue;
969 tail = regmatch.startp[i];
970 }
971 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
972 {
973 char_u *match_ptr;
974
975 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
976 continue;
977 fields->col = 0;
978 for (match_ptr = regmatch.startp[i];
979 match_ptr != regmatch.endp[i]; ++match_ptr)
980 {
981 ++fields->col;
982 if (*match_ptr == TAB)
983 {
984 fields->col += 7;
985 fields->col -= fields->col % 8;
986 }
987 }
988 ++fields->col;
989 fields->use_viscol = TRUE;
990 }
991 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
992 {
993 if (regmatch.startp[i] == NULL)
994 continue;
995 fields->col = (int)atol((char *)regmatch.startp[i]);
996 fields->use_viscol = TRUE;
997 }
998 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
999 {
1000 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1001 continue;
1002 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1003 if (len > CMDBUFFSIZE - 5)
1004 len = CMDBUFFSIZE - 5;
1005 STRCPY(fields->pattern, "^\\V");
1006 STRNCAT(fields->pattern, regmatch.startp[i], len);
1007 fields->pattern[len + 3] = '\\';
1008 fields->pattern[len + 4] = '$';
1009 fields->pattern[len + 5] = NUL;
1010 }
1011 break;
1012 }
1013 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001014 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001015
1016 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1017 {
1018 if (fmt_ptr != NULL)
1019 {
1020 if (idx == 'D') /* enter directory */
1021 {
1022 if (*fields->namebuf == NUL)
1023 {
1024 EMSG(_("E379: Missing or empty directory name"));
1025 return QF_FAIL;
1026 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001027 qfl->qf_directory =
1028 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1029 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001030 return QF_FAIL;
1031 }
1032 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001033 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001034 }
1035 fields->namebuf[0] = NUL; /* no match found, remove file name */
1036 fields->lnum = 0; /* don't jump to this line */
1037 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001038 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001039 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001040 /* linelen + null terminator */
1041 if ((fields->errmsg = vim_realloc(fields->errmsg,
1042 linelen + 1)) == NULL)
1043 return QF_NOMEM;
1044 fields->errmsglen = linelen + 1;
1045 }
1046 /* copy whole line to error message */
1047 vim_strncpy(fields->errmsg, linebuf, linelen);
1048 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001049 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001050 }
1051 else if (fmt_ptr != NULL)
1052 {
1053 /* honor %> item */
1054 if (fmt_ptr->conthere)
1055 fmt_start = fmt_ptr;
1056
1057 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1058 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001059 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1060 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001061 }
1062 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1063 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001064 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001065 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001066 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001067
Bram Moolenaar9b457942016-10-09 16:10:05 +02001068 if (qfprev == NULL)
1069 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001070 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001071 {
1072 len = (int)STRLEN(qfprev->qf_text);
1073 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1074 == NULL)
1075 return QF_FAIL;
1076 STRCPY(ptr, qfprev->qf_text);
1077 vim_free(qfprev->qf_text);
1078 qfprev->qf_text = ptr;
1079 *(ptr += len) = '\n';
1080 STRCPY(++ptr, fields->errmsg);
1081 }
1082 if (qfprev->qf_nr == -1)
1083 qfprev->qf_nr = fields->enr;
1084 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1085 /* only printable chars allowed */
1086 qfprev->qf_type = fields->type;
1087
1088 if (!qfprev->qf_lnum)
1089 qfprev->qf_lnum = fields->lnum;
1090 if (!qfprev->qf_col)
1091 qfprev->qf_col = fields->col;
1092 qfprev->qf_viscol = fields->use_viscol;
1093 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001094 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1095 qfl->qf_directory,
1096 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001097 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001098 : qfl->qf_currfile != NULL && fields->valid
1099 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001100 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001101 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001102 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001103 line_breakcheck();
1104 return QF_IGNORE_LINE;
1105 }
1106 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1107 {
1108 /* global file names */
1109 fields->valid = FALSE;
1110 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1111 {
1112 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001113 qfl->qf_currfile =
1114 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001115 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001116 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001117 *fields->namebuf = NUL;
1118 if (tail && *tail)
1119 {
1120 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001121 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001122 goto restofline;
1123 }
1124 }
1125 }
1126 if (fmt_ptr->flags == '-') /* generally exclude this line */
1127 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001128 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001129 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001130 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001131 return QF_IGNORE_LINE;
1132 }
1133 }
1134
1135 return QF_OK;
1136}
1137
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001138/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001139 * Read the errorfile "efile" into memory, line by line, building the error
1140 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001141 * Alternative: when "efile" is NULL read errors from buffer "buf".
1142 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001143 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001144 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1145 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001146 * Return -1 for error, number of errors for success.
1147 */
1148 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001149qf_init_ext(
1150 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001151 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001152 char_u *efile,
1153 buf_T *buf,
1154 typval_T *tv,
1155 char_u *errorformat,
1156 int newlist, /* TRUE: start a new error list */
1157 linenr_T lnumfirst, /* first line number to use */
1158 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001159 char_u *qf_title,
1160 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001161{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001162 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001163 qfstate_T state;
1164 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001165 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001166 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001167 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001169 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001171 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001173 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001174 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001175
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001176 vim_memset(&state, 0, sizeof(state));
1177 vim_memset(&fields, 0, sizeof(fields));
1178#ifdef FEAT_MBYTE
1179 state.vc.vc_type = CONV_NONE;
1180 if (enc != NULL && *enc != NUL)
1181 convert_setup(&state.vc, enc, p_enc);
1182#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001183 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1184 fields.errmsglen = CMDBUFFSIZE + 1;
1185 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1186 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02001187 if (fields.namebuf == NULL || fields.errmsg == NULL
1188 || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 goto qf_init_end;
1190
Bram Moolenaare0d37972016-07-15 22:36:01 +02001191 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
1193 EMSG2(_(e_openerrf), efile);
1194 goto qf_init_end;
1195 }
1196
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001197 if (newlist || qf_idx == qi->qf_listcount)
1198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001200 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001201 qf_idx = qi->qf_curlist;
1202 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001203 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001204 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001205 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001206 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001207 if (qi->qf_lists[qf_idx].qf_count > 0)
1208 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001211 qfl = &qi->qf_lists[qf_idx];
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001214 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001215 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else
1217 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001219 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001220 * If the errorformat didn't change between calls, then reuse the
1221 * previously parsed values.
1222 */
1223 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1224 {
1225 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001226 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001227 free_efm_list(&fmt_first);
1228
1229 /* parse the current 'efm' */
1230 fmt_first = parse_efm_option(efm);
1231 if (fmt_first != NULL)
1232 last_efm = vim_strsave(efm);
1233 }
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
1238 /*
1239 * got_int is reset here, because it was probably set when killing the
1240 * ":make" command, but we still want to read the errorfile then.
1241 */
1242 got_int = FALSE;
1243
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001244 if (tv != NULL)
1245 {
1246 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001247 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001248 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001249 state.p_li = tv->vval.v_list->lv_first;
1250 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001251 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001252 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001253 state.buflnum = lnumfirst;
1254 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 /*
1257 * Read the lines in the error file one by one.
1258 * Try to recognize one of the error formats in each line.
1259 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001260 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001262 /* Get the next line from a file/buffer/list/string */
1263 status = qf_get_nextline(&state);
1264 if (status == QF_NOMEM) /* memory alloc failure */
1265 goto qf_init_end;
1266 if (status == QF_END_OF_INPUT) /* end of input */
1267 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001269 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1270 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001271 if (status == QF_FAIL)
1272 goto error2;
1273 if (status == QF_NOMEM)
1274 goto qf_init_end;
1275 if (status == QF_IGNORE_LINE)
1276 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001278 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001279 qf_idx,
1280 qfl->qf_directory,
1281 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001282 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001283 : ((qfl->qf_currfile != NULL && fields.valid)
1284 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001285 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001286 fields.errmsg,
1287 fields.lnum,
1288 fields.col,
1289 fields.use_viscol,
1290 fields.pattern,
1291 fields.enr,
1292 fields.type,
1293 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 goto error2;
1295 line_breakcheck();
1296 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001297 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001299 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001301 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001302 qfl->qf_ptr = qfl->qf_start;
1303 qfl->qf_index = 1;
1304 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 else
1307 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001308 qfl->qf_nonevalid = FALSE;
1309 if (qfl->qf_ptr == NULL)
1310 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001312 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001313 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001314 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 EMSG(_(e_readerrf));
1317error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001318 if (!adding)
1319 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001320 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001321 qf_free(qi, qi->qf_curlist);
1322 qi->qf_listcount--;
1323 if (qi->qf_curlist > 0)
1324 --qi->qf_curlist;
1325 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001326qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001327 if (state.fd != NULL)
1328 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001329 vim_free(fields.namebuf);
1330 vim_free(fields.errmsg);
1331 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001332 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001334 if (qf_idx == qi->qf_curlist)
1335 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001336#ifdef FEAT_MBYTE
1337 if (state.vc.vc_type != CONV_NONE)
1338 convert_setup(&state.vc, NULL, NULL);
1339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341 return retval;
1342}
1343
Bram Moolenaarfb604092014-07-23 15:55:00 +02001344 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001345qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001346{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001347 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001348
Bram Moolenaarfb604092014-07-23 15:55:00 +02001349 if (title != NULL)
1350 {
1351 char_u *p = alloc((int)STRLEN(title) + 2);
1352
Bram Moolenaara3921f42017-06-04 15:30:34 +02001353 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001354 if (p != NULL)
1355 sprintf((char *)p, ":%s", (char *)title);
1356 }
1357}
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001360 * Prepare for adding a new quickfix list. If the current list is in the
1361 * middle of the stack, then all the following lists are freed and then
1362 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 */
1364 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001365qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
1367 int i;
1368
1369 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001370 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 * the current entry. This makes it possible to browse in a tree-like
1372 * way with ":grep'.
1373 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001374 while (qi->qf_listcount > qi->qf_curlist + 1)
1375 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377 /*
1378 * When the stack is full, remove to oldest entry
1379 * Otherwise, add a new entry.
1380 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001381 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001383 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001385 qi->qf_lists[i - 1] = qi->qf_lists[i];
1386 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001389 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001390 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001391 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001392 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393}
1394
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001395/*
1396 * Free a location list
1397 */
1398 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001399ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001400{
1401 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001402 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001403
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001404 qi = *pqi;
1405 if (qi == NULL)
1406 return;
1407 *pqi = NULL; /* Remove reference to this list */
1408
1409 qi->qf_refcount--;
1410 if (qi->qf_refcount < 1)
1411 {
1412 /* No references to this location list */
1413 for (i = 0; i < qi->qf_listcount; ++i)
1414 qf_free(qi, i);
1415 vim_free(qi);
1416 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001417}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001418
1419 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001420qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001421{
1422 int i;
1423 qf_info_T *qi = &ql_info;
1424
1425 if (wp != NULL)
1426 {
1427 /* location list */
1428 ll_free_all(&wp->w_llist);
1429 ll_free_all(&wp->w_llist_ref);
1430 }
1431 else
1432 /* quickfix list */
1433 for (i = 0; i < qi->qf_listcount; ++i)
1434 qf_free(qi, i);
1435}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001436
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437/*
1438 * Add an entry to the end of the list of errors.
1439 * Returns OK or FAIL.
1440 */
1441 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001442qf_add_entry(
1443 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001444 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001445 char_u *dir, /* optional directory name */
1446 char_u *fname, /* file name or NULL */
1447 int bufnum, /* buffer number or zero */
1448 char_u *mesg, /* message */
1449 long lnum, /* line number */
1450 int col, /* column */
1451 int vis_col, /* using visual column */
1452 char_u *pattern, /* search pattern */
1453 int nr, /* error number */
1454 int type, /* type character */
1455 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001457 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001458 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001460 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001462 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001463 {
1464 buf_T *buf = buflist_findnr(bufnum);
1465
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001466 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001467 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001468 buf->b_has_qf_entry |=
1469 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001470 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001471 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001472 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1474 {
1475 vim_free(qfp);
1476 return FAIL;
1477 }
1478 qfp->qf_lnum = lnum;
1479 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001480 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001481 if (pattern == NULL || *pattern == NUL)
1482 qfp->qf_pattern = NULL;
1483 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1484 {
1485 vim_free(qfp->qf_text);
1486 vim_free(qfp);
1487 return FAIL;
1488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 qfp->qf_nr = nr;
1490 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1491 type = 0;
1492 qfp->qf_type = type;
1493 qfp->qf_valid = valid;
1494
Bram Moolenaara3921f42017-06-04 15:30:34 +02001495 lastp = &qi->qf_lists[qf_idx].qf_last;
1496 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001497 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001499 qi->qf_lists[qf_idx].qf_start = qfp;
1500 qi->qf_lists[qf_idx].qf_ptr = qfp;
1501 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001502 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 else
1505 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001506 qfp->qf_prev = *lastp;
1507 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001509 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001511 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001512 ++qi->qf_lists[qf_idx].qf_count;
1513 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001514 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001516 qi->qf_lists[qf_idx].qf_index =
1517 qi->qf_lists[qf_idx].qf_count;
1518 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
1520
1521 return OK;
1522}
1523
1524/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001525 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001526 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001527 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001528ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001529{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001530 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001531
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001532 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1533 if (qi != NULL)
1534 {
1535 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1536 qi->qf_refcount++;
1537 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001538
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001539 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001540}
1541
1542/*
1543 * Return the location list for window 'wp'.
1544 * If not present, allocate a location list
1545 */
1546 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001547ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548{
1549 if (IS_LL_WINDOW(wp))
1550 /* For a location list window, use the referenced location list */
1551 return wp->w_llist_ref;
1552
1553 /*
1554 * For a non-location list window, w_llist_ref should not point to a
1555 * location list.
1556 */
1557 ll_free_all(&wp->w_llist_ref);
1558
1559 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001560 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001561 return wp->w_llist;
1562}
1563
1564/*
1565 * Copy the location list from window "from" to window "to".
1566 */
1567 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001568copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001569{
1570 qf_info_T *qi;
1571 int idx;
1572 int i;
1573
1574 /*
1575 * When copying from a location list window, copy the referenced
1576 * location list. For other windows, copy the location list for
1577 * that window.
1578 */
1579 if (IS_LL_WINDOW(from))
1580 qi = from->w_llist_ref;
1581 else
1582 qi = from->w_llist;
1583
1584 if (qi == NULL) /* no location list to copy */
1585 return;
1586
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001587 /* allocate a new location list */
1588 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001589 return;
1590
1591 to->w_llist->qf_listcount = qi->qf_listcount;
1592
1593 /* Copy the location lists one at a time */
1594 for (idx = 0; idx < qi->qf_listcount; idx++)
1595 {
1596 qf_list_T *from_qfl;
1597 qf_list_T *to_qfl;
1598
1599 to->w_llist->qf_curlist = idx;
1600
1601 from_qfl = &qi->qf_lists[idx];
1602 to_qfl = &to->w_llist->qf_lists[idx];
1603
1604 /* Some of the fields are populated by qf_add_entry() */
1605 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1606 to_qfl->qf_count = 0;
1607 to_qfl->qf_index = 0;
1608 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001609 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001610 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001611 if (from_qfl->qf_title != NULL)
1612 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1613 else
1614 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001615 if (from_qfl->qf_ctx != NULL)
1616 {
1617 to_qfl->qf_ctx = alloc_tv();
1618 if (to_qfl->qf_ctx != NULL)
1619 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1620 }
1621 else
1622 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001623
1624 if (from_qfl->qf_count)
1625 {
1626 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001627 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001628
1629 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001630 for (i = 0, from_qfp = from_qfl->qf_start;
1631 i < from_qfl->qf_count && from_qfp != NULL;
1632 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001633 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001634 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001635 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001636 NULL,
1637 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001638 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001639 from_qfp->qf_text,
1640 from_qfp->qf_lnum,
1641 from_qfp->qf_col,
1642 from_qfp->qf_viscol,
1643 from_qfp->qf_pattern,
1644 from_qfp->qf_nr,
1645 0,
1646 from_qfp->qf_valid) == FAIL)
1647 {
1648 qf_free_all(to);
1649 return;
1650 }
1651 /*
1652 * qf_add_entry() will not set the qf_num field, as the
1653 * directory and file names are not supplied. So the qf_fnum
1654 * field is copied here.
1655 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001656 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001657 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1658 prevp->qf_type = from_qfp->qf_type; /* error type */
1659 if (from_qfl->qf_ptr == from_qfp)
1660 to_qfl->qf_ptr = prevp; /* current location */
1661 }
1662 }
1663
1664 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1665
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001666 /* Assign a new ID for the location list */
1667 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001668 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001669
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001670 /* When no valid entries are present in the list, qf_ptr points to
1671 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001672 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001673 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001674 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001675 to_qfl->qf_index = 1;
1676 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001677 }
1678
1679 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1680}
1681
1682/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001683 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001684 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 */
1686 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001687qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
Bram Moolenaar82404332016-07-10 17:00:38 +02001689 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001690 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001691 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (fname == NULL || *fname == NUL) /* no file name */
1694 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
Bram Moolenaare60acc12011-05-10 16:41:25 +02001696#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001697 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001698#endif
1699#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001700 if (directory != NULL)
1701 slash_adjust(directory);
1702 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001703#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001704 if (directory != NULL && !vim_isAbsName(fname)
1705 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1706 {
1707 /*
1708 * Here we check if the file really exists.
1709 * This should normally be true, but if make works without
1710 * "leaving directory"-messages we might have missed a
1711 * directory change.
1712 */
1713 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001717 if (directory)
1718 ptr = concat_fnames(directory, fname, TRUE);
1719 else
1720 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001722 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001723 bufname = ptr;
1724 }
1725 else
1726 bufname = fname;
1727
1728 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001729 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001730 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001731 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001732 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001734 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001735 {
1736 vim_free(qf_last_bufname);
1737 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1738 if (bufname == ptr)
1739 qf_last_bufname = bufname;
1740 else
1741 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001742 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001743 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001744 if (buf == NULL)
1745 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001746
Bram Moolenaarc1542742016-07-20 21:44:37 +02001747 buf->b_has_qf_entry =
1748 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001749 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750}
1751
1752/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001753 * Push dirbuf onto the directory stack and return pointer to actual dir or
1754 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 */
1756 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001757qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
1759 struct dir_stack_T *ds_new;
1760 struct dir_stack_T *ds_ptr;
1761
1762 /* allocate new stack element and hook it in */
1763 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1764 if (ds_new == NULL)
1765 return NULL;
1766
1767 ds_new->next = *stackptr;
1768 *stackptr = ds_new;
1769
1770 /* store directory on the stack */
1771 if (vim_isAbsName(dirbuf)
1772 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001773 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 (*stackptr)->dirname = vim_strsave(dirbuf);
1775 else
1776 {
1777 /* Okay we don't have an absolute path.
1778 * dirbuf must be a subdir of one of the directories on the stack.
1779 * Let's search...
1780 */
1781 ds_new = (*stackptr)->next;
1782 (*stackptr)->dirname = NULL;
1783 while (ds_new)
1784 {
1785 vim_free((*stackptr)->dirname);
1786 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1787 TRUE);
1788 if (mch_isdir((*stackptr)->dirname) == TRUE)
1789 break;
1790
1791 ds_new = ds_new->next;
1792 }
1793
1794 /* clean up all dirs we already left */
1795 while ((*stackptr)->next != ds_new)
1796 {
1797 ds_ptr = (*stackptr)->next;
1798 (*stackptr)->next = (*stackptr)->next->next;
1799 vim_free(ds_ptr->dirname);
1800 vim_free(ds_ptr);
1801 }
1802
1803 /* Nothing found -> it must be on top level */
1804 if (ds_new == NULL)
1805 {
1806 vim_free((*stackptr)->dirname);
1807 (*stackptr)->dirname = vim_strsave(dirbuf);
1808 }
1809 }
1810
1811 if ((*stackptr)->dirname != NULL)
1812 return (*stackptr)->dirname;
1813 else
1814 {
1815 ds_ptr = *stackptr;
1816 *stackptr = (*stackptr)->next;
1817 vim_free(ds_ptr);
1818 return NULL;
1819 }
1820}
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
1823 * pop dirbuf from the directory stack and return previous directory or NULL if
1824 * stack is empty
1825 */
1826 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001827qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 struct dir_stack_T *ds_ptr;
1830
1831 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1832 * What to do if it isn't? */
1833
1834 /* pop top element and free it */
1835 if (*stackptr != NULL)
1836 {
1837 ds_ptr = *stackptr;
1838 *stackptr = (*stackptr)->next;
1839 vim_free(ds_ptr->dirname);
1840 vim_free(ds_ptr);
1841 }
1842
1843 /* return NEW top element as current dir or NULL if stack is empty*/
1844 return *stackptr ? (*stackptr)->dirname : NULL;
1845}
1846
1847/*
1848 * clean up directory stack
1849 */
1850 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001851qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
1853 struct dir_stack_T *ds_ptr;
1854
1855 while ((ds_ptr = *stackptr) != NULL)
1856 {
1857 *stackptr = (*stackptr)->next;
1858 vim_free(ds_ptr->dirname);
1859 vim_free(ds_ptr);
1860 }
1861}
1862
1863/*
1864 * Check in which directory of the directory stack the given file can be
1865 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001866 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 * Cleans up intermediate directory entries.
1868 *
1869 * TODO: How to solve the following problem?
1870 * If we have the this directory tree:
1871 * ./
1872 * ./aa
1873 * ./aa/bb
1874 * ./bb
1875 * ./bb/x.c
1876 * and make says:
1877 * making all in aa
1878 * making all in bb
1879 * x.c:9: Error
1880 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1881 * qf_guess_filepath will return NULL.
1882 */
1883 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001884qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885{
1886 struct dir_stack_T *ds_ptr;
1887 struct dir_stack_T *ds_tmp;
1888 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001889 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001892 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 return NULL;
1894
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001895 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 fullname = NULL;
1897 while (ds_ptr)
1898 {
1899 vim_free(fullname);
1900 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1901
1902 /* If concat_fnames failed, just go on. The worst thing that can happen
1903 * is that we delete the entire stack.
1904 */
1905 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1906 break;
1907
1908 ds_ptr = ds_ptr->next;
1909 }
1910
1911 vim_free(fullname);
1912
1913 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001914 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001916 ds_tmp = qfl->qf_dir_stack->next;
1917 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 vim_free(ds_tmp->dirname);
1919 vim_free(ds_tmp);
1920 }
1921
1922 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923}
1924
1925/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01001926 * Returns TRUE if a quickfix/location list with the given identifier exists.
1927 */
1928 static int
1929qflist_valid (win_T *wp, int_u qf_id)
1930{
1931 qf_info_T *qi = &ql_info;
1932 int i;
1933
1934 if (wp != NULL)
1935 {
1936 qi = GET_LOC_LIST(wp); /* Location list */
1937 if (qi == NULL)
1938 return FALSE;
1939 }
1940
1941 for (i = 0; i < qi->qf_listcount; ++i)
1942 if (qi->qf_lists[i].qf_id == qf_id)
1943 return TRUE;
1944
1945 return FALSE;
1946}
1947
1948/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001949 * When loading a file from the quickfix, the auto commands may modify it.
1950 * This may invalidate the current quickfix entry. This function checks
1951 * whether a entry is still present in the quickfix.
1952 * Similar to location list.
1953 */
1954 static int
1955is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1956{
1957 qf_list_T *qfl;
1958 qfline_T *qfp;
1959 int i;
1960
1961 qfl = &qi->qf_lists[qi->qf_curlist];
1962
1963 /* Search for the entry in the current list */
1964 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1965 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001966 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001967 break;
1968
1969 if (i == qfl->qf_count) /* Entry is not found */
1970 return FALSE;
1971
1972 return TRUE;
1973}
1974
1975/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001976 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001977 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001978 */
1979 static qfline_T *
1980get_next_valid_entry(
1981 qf_info_T *qi,
1982 qfline_T *qf_ptr,
1983 int *qf_index,
1984 int dir)
1985{
1986 int idx;
1987 int old_qf_fnum;
1988
1989 idx = *qf_index;
1990 old_qf_fnum = qf_ptr->qf_fnum;
1991
1992 do
1993 {
1994 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
1995 || qf_ptr->qf_next == NULL)
1996 return NULL;
1997 ++idx;
1998 qf_ptr = qf_ptr->qf_next;
1999 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2000 && !qf_ptr->qf_valid)
2001 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2002
2003 *qf_index = idx;
2004 return qf_ptr;
2005}
2006
2007/*
2008 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002009 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002010 */
2011 static qfline_T *
2012get_prev_valid_entry(
2013 qf_info_T *qi,
2014 qfline_T *qf_ptr,
2015 int *qf_index,
2016 int dir)
2017{
2018 int idx;
2019 int old_qf_fnum;
2020
2021 idx = *qf_index;
2022 old_qf_fnum = qf_ptr->qf_fnum;
2023
2024 do
2025 {
2026 if (idx == 1 || qf_ptr->qf_prev == NULL)
2027 return NULL;
2028 --idx;
2029 qf_ptr = qf_ptr->qf_prev;
2030 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2031 && !qf_ptr->qf_valid)
2032 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2033
2034 *qf_index = idx;
2035 return qf_ptr;
2036}
2037
2038/*
2039 * Get the n'th (errornr) previous/next valid entry from the current entry in
2040 * the quickfix list.
2041 * dir == FORWARD or FORWARD_FILE: next valid entry
2042 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2043 */
2044 static qfline_T *
2045get_nth_valid_entry(
2046 qf_info_T *qi,
2047 int errornr,
2048 qfline_T *qf_ptr,
2049 int *qf_index,
2050 int dir)
2051{
2052 qfline_T *prev_qf_ptr;
2053 int prev_index;
2054 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2055 char_u *err = e_no_more_items;
2056
2057 while (errornr--)
2058 {
2059 prev_qf_ptr = qf_ptr;
2060 prev_index = *qf_index;
2061
2062 if (dir == FORWARD || dir == FORWARD_FILE)
2063 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2064 else
2065 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2066 if (qf_ptr == NULL)
2067 {
2068 qf_ptr = prev_qf_ptr;
2069 *qf_index = prev_index;
2070 if (err != NULL)
2071 {
2072 EMSG(_(err));
2073 return NULL;
2074 }
2075 break;
2076 }
2077
2078 err = NULL;
2079 }
2080
2081 return qf_ptr;
2082}
2083
2084/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002085 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002086 */
2087 static qfline_T *
2088get_nth_entry(
2089 qf_info_T *qi,
2090 int errornr,
2091 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002092 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002093{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002094 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002095
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002096 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002097 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2098 {
2099 --qf_idx;
2100 qf_ptr = qf_ptr->qf_prev;
2101 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002102 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002103 while (errornr > qf_idx &&
2104 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2105 qf_ptr->qf_next != NULL)
2106 {
2107 ++qf_idx;
2108 qf_ptr = qf_ptr->qf_next;
2109 }
2110
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002111 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002112 return qf_ptr;
2113}
2114
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002115/*
2116 * Find a help window or open one.
2117 */
2118 static int
2119jump_to_help_window(qf_info_T *qi, int *opened_window)
2120{
2121 win_T *wp;
2122 int flags;
2123
2124 if (cmdmod.tab != 0)
2125 wp = NULL;
2126 else
2127 FOR_ALL_WINDOWS(wp)
2128 if (bt_help(wp->w_buffer))
2129 break;
2130 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2131 win_enter(wp, TRUE);
2132 else
2133 {
2134 /*
2135 * Split off help window; put it at far top if no position
2136 * specified, the current window is vertically split and narrow.
2137 */
2138 flags = WSP_HELP;
2139 if (cmdmod.split == 0 && curwin->w_width != Columns
2140 && curwin->w_width < 80)
2141 flags |= WSP_TOP;
2142 if (qi != &ql_info)
2143 flags |= WSP_NEWLOC; /* don't copy the location list */
2144
2145 if (win_split(0, flags) == FAIL)
2146 return FAIL;
2147
2148 *opened_window = TRUE;
2149
2150 if (curwin->w_height < p_hh)
2151 win_setheight((int)p_hh);
2152
2153 if (qi != &ql_info) /* not a quickfix list */
2154 {
2155 /* The new window should use the supplied location list */
2156 curwin->w_llist = qi;
2157 qi->qf_refcount++;
2158 }
2159 }
2160
2161 if (!p_im)
2162 restart_edit = 0; /* don't want insert mode in help file */
2163
2164 return OK;
2165}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002166
2167/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002168 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2169 * If the file is already opened in a window, jump to it.
2170 */
2171 static int
2172qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2173{
2174 win_T *usable_win_ptr = NULL;
2175 int usable_win;
2176 qf_info_T *ll_ref;
2177 int flags;
2178 win_T *win;
2179 win_T *altwin;
2180
2181 usable_win = 0;
2182
2183 ll_ref = curwin->w_llist_ref;
2184 if (ll_ref != NULL)
2185 {
2186 /* Find a window using the same location list that is not a
2187 * quickfix window. */
2188 FOR_ALL_WINDOWS(usable_win_ptr)
2189 if (usable_win_ptr->w_llist == ll_ref
2190 && !bt_quickfix(usable_win_ptr->w_buffer))
2191 {
2192 usable_win = 1;
2193 break;
2194 }
2195 }
2196
2197 if (!usable_win)
2198 {
2199 /* Locate a window showing a normal buffer */
2200 FOR_ALL_WINDOWS(win)
2201 if (win->w_buffer->b_p_bt[0] == NUL)
2202 {
2203 usable_win = 1;
2204 break;
2205 }
2206 }
2207
2208 /*
2209 * If no usable window is found and 'switchbuf' contains "usetab"
2210 * then search in other tabs.
2211 */
2212 if (!usable_win && (swb_flags & SWB_USETAB))
2213 {
2214 tabpage_T *tp;
2215 win_T *wp;
2216
2217 FOR_ALL_TAB_WINDOWS(tp, wp)
2218 {
2219 if (wp->w_buffer->b_fnum == qf_fnum)
2220 {
2221 goto_tabpage_win(tp, wp);
2222 usable_win = 1;
2223 goto win_found;
2224 }
2225 }
2226 }
2227win_found:
2228
2229 /*
2230 * If there is only one window and it is the quickfix window, create a
2231 * new one above the quickfix window.
2232 */
2233 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2234 {
2235 flags = WSP_ABOVE;
2236 if (ll_ref != NULL)
2237 flags |= WSP_NEWLOC;
2238 if (win_split(0, flags) == FAIL)
2239 return FAIL; /* not enough room for window */
2240 *opened_window = TRUE; /* close it when fail */
2241 p_swb = empty_option; /* don't split again */
2242 swb_flags = 0;
2243 RESET_BINDING(curwin);
2244 if (ll_ref != NULL)
2245 {
2246 /* The new window should use the location list from the
2247 * location list window */
2248 curwin->w_llist = ll_ref;
2249 ll_ref->qf_refcount++;
2250 }
2251 }
2252 else
2253 {
2254 if (curwin->w_llist_ref != NULL)
2255 {
2256 /* In a location window */
2257 win = usable_win_ptr;
2258 if (win == NULL)
2259 {
2260 /* Find the window showing the selected file */
2261 FOR_ALL_WINDOWS(win)
2262 if (win->w_buffer->b_fnum == qf_fnum)
2263 break;
2264 if (win == NULL)
2265 {
2266 /* Find a previous usable window */
2267 win = curwin;
2268 do
2269 {
2270 if (win->w_buffer->b_p_bt[0] == NUL)
2271 break;
2272 if (win->w_prev == NULL)
2273 win = lastwin; /* wrap around the top */
2274 else
2275 win = win->w_prev; /* go to previous window */
2276 } while (win != curwin);
2277 }
2278 }
2279 win_goto(win);
2280
2281 /* If the location list for the window is not set, then set it
2282 * to the location list from the location window */
2283 if (win->w_llist == NULL)
2284 {
2285 win->w_llist = ll_ref;
2286 ll_ref->qf_refcount++;
2287 }
2288 }
2289 else
2290 {
2291
2292 /*
2293 * Try to find a window that shows the right buffer.
2294 * Default to the window just above the quickfix buffer.
2295 */
2296 win = curwin;
2297 altwin = NULL;
2298 for (;;)
2299 {
2300 if (win->w_buffer->b_fnum == qf_fnum)
2301 break;
2302 if (win->w_prev == NULL)
2303 win = lastwin; /* wrap around the top */
2304 else
2305 win = win->w_prev; /* go to previous window */
2306
2307 if (IS_QF_WINDOW(win))
2308 {
2309 /* Didn't find it, go to the window before the quickfix
2310 * window. */
2311 if (altwin != NULL)
2312 win = altwin;
2313 else if (curwin->w_prev != NULL)
2314 win = curwin->w_prev;
2315 else
2316 win = curwin->w_next;
2317 break;
2318 }
2319
2320 /* Remember a usable window. */
2321 if (altwin == NULL && !win->w_p_pvw
2322 && win->w_buffer->b_p_bt[0] == NUL)
2323 altwin = win;
2324 }
2325
2326 win_goto(win);
2327 }
2328 }
2329
2330 return OK;
2331}
2332
2333/*
2334 * Edit the selected file or help file.
2335 */
2336 static int
2337qf_jump_edit_buffer(
2338 qf_info_T *qi,
2339 qfline_T *qf_ptr,
2340 int forceit,
2341 win_T *oldwin,
2342 int *opened_window,
2343 int *abort)
2344{
2345 int retval = OK;
2346
2347 if (qf_ptr->qf_type == 1)
2348 {
2349 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2350 * set b_p_ro flag). */
2351 if (!can_abandon(curbuf, forceit))
2352 {
2353 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002354 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002355 }
2356 else
2357 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2358 ECMD_HIDE + ECMD_SET_HELP,
2359 oldwin == curwin ? curwin : NULL);
2360 }
2361 else
2362 {
2363 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002364 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002365
2366 retval = buflist_getfile(qf_ptr->qf_fnum,
2367 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002368
2369 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002370 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002371 /*
2372 * Location list. Check whether the associated window is still
2373 * present and the list is still valid.
2374 */
2375 if (!win_valid_any_tab(oldwin))
2376 {
2377 EMSG(_("E924: Current window was closed"));
2378 *abort = TRUE;
2379 *opened_window = FALSE;
2380 }
2381 else if (!qflist_valid(oldwin, save_qfid))
2382 {
2383 EMSG(_(e_loc_list_changed));
2384 *abort = TRUE;
2385 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002386 }
2387 else if (old_qf_curlist != qi->qf_curlist
2388 || !is_qf_entry_present(qi, qf_ptr))
2389 {
2390 if (qi == &ql_info)
2391 EMSG(_("E925: Current quickfix was changed"));
2392 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002393 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002394 *abort = TRUE;
2395 }
2396
2397 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002398 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002399 }
2400
2401 return retval;
2402}
2403
2404/*
2405 * Goto the error line in the current file using either line/column number or a
2406 * search pattern.
2407 */
2408 static void
2409qf_jump_goto_line(
2410 linenr_T qf_lnum,
2411 int qf_col,
2412 char_u qf_viscol,
2413 char_u *qf_pattern)
2414{
2415 linenr_T i;
2416 char_u *line;
2417 colnr_T screen_col;
2418 colnr_T char_col;
2419
2420 if (qf_pattern == NULL)
2421 {
2422 /*
2423 * Go to line with error, unless qf_lnum is 0.
2424 */
2425 i = qf_lnum;
2426 if (i > 0)
2427 {
2428 if (i > curbuf->b_ml.ml_line_count)
2429 i = curbuf->b_ml.ml_line_count;
2430 curwin->w_cursor.lnum = i;
2431 }
2432 if (qf_col > 0)
2433 {
2434 curwin->w_cursor.col = qf_col - 1;
2435#ifdef FEAT_VIRTUALEDIT
2436 curwin->w_cursor.coladd = 0;
2437#endif
2438 if (qf_viscol == TRUE)
2439 {
2440 /*
2441 * Check each character from the beginning of the error
2442 * line up to the error column. For each tab character
2443 * found, reduce the error column value by the length of
2444 * a tab character.
2445 */
2446 line = ml_get_curline();
2447 screen_col = 0;
2448 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2449 {
2450 if (*line == NUL)
2451 break;
2452 if (*line++ == '\t')
2453 {
2454 curwin->w_cursor.col -= 7 - (screen_col % 8);
2455 screen_col += 8 - (screen_col % 8);
2456 }
2457 else
2458 ++screen_col;
2459 }
2460 }
2461 check_cursor();
2462 }
2463 else
2464 beginline(BL_WHITE | BL_FIX);
2465 }
2466 else
2467 {
2468 pos_T save_cursor;
2469
2470 /* Move the cursor to the first line in the buffer */
2471 save_cursor = curwin->w_cursor;
2472 curwin->w_cursor.lnum = 0;
2473 if (!do_search(NULL, '/', qf_pattern, (long)1,
2474 SEARCH_KEEP, NULL, NULL))
2475 curwin->w_cursor = save_cursor;
2476 }
2477}
2478
2479/*
2480 * Display quickfix list index and size message
2481 */
2482 static void
2483qf_jump_print_msg(
2484 qf_info_T *qi,
2485 int qf_index,
2486 qfline_T *qf_ptr,
2487 buf_T *old_curbuf,
2488 linenr_T old_lnum)
2489{
2490 linenr_T i;
2491 int len;
2492
2493 /* Update the screen before showing the message, unless the screen
2494 * scrolled up. */
2495 if (!msg_scrolled)
2496 update_topline_redraw();
2497 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2498 qi->qf_lists[qi->qf_curlist].qf_count,
2499 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2500 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2501 /* Add the message, skipping leading whitespace and newlines. */
2502 len = (int)STRLEN(IObuff);
2503 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2504
2505 /* Output the message. Overwrite to avoid scrolling when the 'O'
2506 * flag is present in 'shortmess'; But when not jumping, print the
2507 * whole message. */
2508 i = msg_scroll;
2509 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2510 msg_scroll = TRUE;
2511 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2512 msg_scroll = FALSE;
2513 msg_attr_keep(IObuff, 0, TRUE);
2514 msg_scroll = i;
2515}
2516
2517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 * jump to a quickfix line
2519 * if dir == FORWARD go "errornr" valid entries forward
2520 * if dir == BACKWARD go "errornr" valid entries backward
2521 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2522 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2523 * else if "errornr" is zero, redisplay the same line
2524 * else go to entry "errornr"
2525 */
2526 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002527qf_jump(qf_info_T *qi,
2528 int dir,
2529 int errornr,
2530 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002532 qfline_T *qf_ptr;
2533 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 buf_T *old_curbuf;
2537 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002538 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002539 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002541 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543#ifdef FEAT_FOLDING
2544 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2545#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002546 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002548 if (qi == NULL)
2549 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002550
2551 if (qi->qf_curlist >= qi->qf_listcount
2552 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
2554 EMSG(_(e_quickfix));
2555 return;
2556 }
2557
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002558 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002560 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002562 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002564 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2565 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002567 qf_ptr = old_qf_ptr;
2568 qf_index = old_qf_index;
2569 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571 }
2572 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002573 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002575 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2576 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 /* No need to print the error message if it's visible in the error
2578 * window */
2579 print_message = FALSE;
2580
2581 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002582 * For ":helpgrep" find a help window or open one.
2583 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002584 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002585 if (jump_to_help_window(qi, &opened_window) == FAIL)
2586 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002587
2588 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 * If currently in the quickfix window, find another window to show the
2590 * file in.
2591 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002592 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 /*
2595 * If there is no file specified, we don't know where to go.
2596 * But do advance, otherwise ":cn" gets stuck.
2597 */
2598 if (qf_ptr->qf_fnum == 0)
2599 goto theend;
2600
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002601 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2602 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
2605 /*
2606 * If there is a file name,
2607 * read the wanted file if needed, and check autowrite etc.
2608 */
2609 old_curbuf = curbuf;
2610 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002611
2612 if (qf_ptr->qf_fnum != 0)
2613 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002614 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002615
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002616 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2617 &opened_window, &abort);
2618 if (abort)
2619 {
2620 qi = NULL;
2621 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002622 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002623 }
2624
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002625 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
2627 /* When not switched to another buffer, still need to set pc mark */
2628 if (curbuf == old_curbuf)
2629 setpcmark();
2630
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002631 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2632 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
2634#ifdef FEAT_FOLDING
2635 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2636 foldOpenCursor();
2637#endif
2638 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002639 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
2641 else
2642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (opened_window)
2644 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002645 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
2647 /*
2648 * Couldn't open file, so put index back where it was. This could
2649 * happen if the file was readonly and we changed something.
2650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 qf_ptr = old_qf_ptr;
2653 qf_index = old_qf_index;
2654 }
2655 }
2656theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002657 if (qi != NULL)
2658 {
2659 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2660 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (p_swb != old_swb && opened_window)
2663 {
2664 /* Restore old 'switchbuf' value, but not when an autocommand or
2665 * modeline has changed the value. */
2666 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002667 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002669 swb_flags = old_swb_flags;
2670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 else
2672 free_string_option(old_swb);
2673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674}
2675
2676/*
2677 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002678 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 */
2680 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002681qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002683 buf_T *buf;
2684 char_u *fname;
2685 qfline_T *qfp;
2686 int i;
2687 int idx1 = 1;
2688 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002689 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002690 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002691 int qfFileAttr;
2692 int qfSepAttr;
2693 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002694 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002696 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002698 if (eap->cmdidx == CMD_llist)
2699 {
2700 qi = GET_LOC_LIST(curwin);
2701 if (qi == NULL)
2702 {
2703 EMSG(_(e_loclist));
2704 return;
2705 }
2706 }
2707
2708 if (qi->qf_curlist >= qi->qf_listcount
2709 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
2711 EMSG(_(e_quickfix));
2712 return;
2713 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002714 if (*arg == '+')
2715 {
2716 ++arg;
2717 plus = TRUE;
2718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2720 {
2721 EMSG(_(e_trailing));
2722 return;
2723 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002724 if (plus)
2725 {
2726 i = qi->qf_lists[qi->qf_curlist].qf_index;
2727 idx2 = i + idx1;
2728 idx1 = i;
2729 }
2730 else
2731 {
2732 i = qi->qf_lists[qi->qf_curlist].qf_count;
2733 if (idx1 < 0)
2734 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2735 if (idx2 < 0)
2736 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002739 /*
2740 * Get the attributes for the different quickfix highlight items. Note
2741 * that this depends on syntax items defined in the qf.vim syntax file
2742 */
2743 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2744 if (qfFileAttr == 0)
2745 qfFileAttr = HL_ATTR(HLF_D);
2746 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2747 if (qfSepAttr == 0)
2748 qfSepAttr = HL_ATTR(HLF_D);
2749 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2750 if (qfLineAttr == 0)
2751 qfLineAttr = HL_ATTR(HLF_N);
2752
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002753 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002755 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2756 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
2758 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2759 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002760 msg_putchar('\n');
2761 if (got_int)
2762 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002763
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002764 fname = NULL;
2765 if (qfp->qf_fnum != 0
2766 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2767 {
2768 fname = buf->b_fname;
2769 if (qfp->qf_type == 1) /* :helpgrep */
2770 fname = gettail(fname);
2771 }
2772 if (fname == NULL)
2773 sprintf((char *)IObuff, "%2d", i);
2774 else
2775 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2776 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002777 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002778 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2779
2780 if (qfp->qf_lnum != 0)
2781 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002782 if (qfp->qf_lnum == 0)
2783 IObuff[0] = NUL;
2784 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002785 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002786 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002787 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002789 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002790 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002791 msg_puts_attr(IObuff, qfLineAttr);
2792 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002793 if (qfp->qf_pattern != NULL)
2794 {
2795 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002796 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002797 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002798 }
2799 msg_puts((char_u *)" ");
2800
2801 /* Remove newlines and leading whitespace from the text. For an
2802 * unrecognized line keep the indent, the compiler may mark a word
2803 * with ^^^^. */
2804 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2806 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002807 msg_prt_line(IObuff, FALSE);
2808 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002810
2811 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002812 if (qfp == NULL)
2813 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002814 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 ui_breakcheck();
2816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817}
2818
2819/*
2820 * Remove newlines and leading whitespace from an error message.
2821 * Put the result in "buf[bufsize]".
2822 */
2823 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002824qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
2826 int i;
2827 char_u *p = text;
2828
2829 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2830 {
2831 if (*p == '\n')
2832 {
2833 buf[i] = ' ';
2834 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002835 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 break;
2837 }
2838 else
2839 buf[i] = *p++;
2840 }
2841 buf[i] = NUL;
2842}
2843
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002844 static void
2845qf_msg(qf_info_T *qi, int which, char *lead)
2846{
2847 char *title = (char *)qi->qf_lists[which].qf_title;
2848 int count = qi->qf_lists[which].qf_count;
2849 char_u buf[IOSIZE];
2850
2851 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2852 lead,
2853 which + 1,
2854 qi->qf_listcount,
2855 count);
2856
2857 if (title != NULL)
2858 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002859 size_t len = STRLEN(buf);
2860
2861 if (len < 34)
2862 {
2863 vim_memset(buf + len, ' ', 34 - len);
2864 buf[34] = NUL;
2865 }
2866 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002867 }
2868 trunc_string(buf, buf, Columns - 1, IOSIZE);
2869 msg(buf);
2870}
2871
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872/*
2873 * ":colder [count]": Up in the quickfix stack.
2874 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002875 * ":lolder [count]": Up in the location list stack.
2876 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 */
2878 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002879qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002881 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 int count;
2883
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002884 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2885 {
2886 qi = GET_LOC_LIST(curwin);
2887 if (qi == NULL)
2888 {
2889 EMSG(_(e_loclist));
2890 return;
2891 }
2892 }
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 if (eap->addr_count != 0)
2895 count = eap->line2;
2896 else
2897 count = 1;
2898 while (count--)
2899 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002900 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002902 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
2904 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002905 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002907 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
2909 else
2910 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
2913 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002914 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002916 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002919 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002920 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921}
2922
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002923 void
2924qf_history(exarg_T *eap)
2925{
2926 qf_info_T *qi = &ql_info;
2927 int i;
2928
2929 if (eap->cmdidx == CMD_lhistory)
2930 qi = GET_LOC_LIST(curwin);
2931 if (qi == NULL || (qi->qf_listcount == 0
2932 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2933 MSG(_("No entries"));
2934 else
2935 for (i = 0; i < qi->qf_listcount; ++i)
2936 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2937}
2938
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002940 * Free all the entries in the error list "idx". Note that other information
2941 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 */
2943 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002944qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002946 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002947 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002948 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002949 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002951 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002953 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002954 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002955 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002956 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002957 vim_free(qfp->qf_text);
2958 stop = (qfp == qfpnext);
2959 vim_free(qfp->qf_pattern);
2960 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002961 if (stop)
2962 /* Somehow qf_count may have an incorrect value, set it to 1
2963 * to avoid crashing when it's wrong.
2964 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002965 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002966 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002967 qfl->qf_start = qfpnext;
2968 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002970
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002971 qfl->qf_index = 0;
2972 qfl->qf_start = NULL;
2973 qfl->qf_last = NULL;
2974 qfl->qf_ptr = NULL;
2975 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002976
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002977 qf_clean_dir_stack(&qfl->qf_dir_stack);
2978 qfl->qf_directory = NULL;
2979 qf_clean_dir_stack(&qfl->qf_file_stack);
2980 qfl->qf_currfile = NULL;
2981 qfl->qf_multiline = FALSE;
2982 qfl->qf_multiignore = FALSE;
2983 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984}
2985
2986/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002987 * Free error list "idx". Frees all the entries in the quickfix list,
2988 * associated context information and the title.
2989 */
2990 static void
2991qf_free(qf_info_T *qi, int idx)
2992{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002993 qf_list_T *qfl = &qi->qf_lists[idx];
2994
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002995 qf_free_items(qi, idx);
2996
Bram Moolenaard23a8232018-02-10 18:45:26 +01002997 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002998 free_tv(qfl->qf_ctx);
2999 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003000 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003001 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003002}
3003
3004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 * qf_mark_adjust: adjust marks
3006 */
3007 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003008qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003009 win_T *wp,
3010 linenr_T line1,
3011 linenr_T line2,
3012 long amount,
3013 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003015 int i;
3016 qfline_T *qfp;
3017 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003018 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003019 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003020 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
Bram Moolenaarc1542742016-07-20 21:44:37 +02003022 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003023 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003024 if (wp != NULL)
3025 {
3026 if (wp->w_llist == NULL)
3027 return;
3028 qi = wp->w_llist;
3029 }
3030
3031 for (idx = 0; idx < qi->qf_listcount; ++idx)
3032 if (qi->qf_lists[idx].qf_count)
3033 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003034 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3035 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 if (qfp->qf_fnum == curbuf->b_fnum)
3037 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003038 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3040 {
3041 if (amount == MAXLNUM)
3042 qfp->qf_cleared = TRUE;
3043 else
3044 qfp->qf_lnum += amount;
3045 }
3046 else if (amount_after && qfp->qf_lnum > line2)
3047 qfp->qf_lnum += amount_after;
3048 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003049
3050 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003051 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052}
3053
3054/*
3055 * Make a nice message out of the error character and the error number:
3056 * char number message
3057 * e or E 0 " error"
3058 * w or W 0 " warning"
3059 * i or I 0 " info"
3060 * 0 0 ""
3061 * other 0 " c"
3062 * e or E n " error n"
3063 * w or W n " warning n"
3064 * i or I n " info n"
3065 * 0 n " error n"
3066 * other n " c n"
3067 * 1 x "" :helpgrep
3068 */
3069 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003070qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 static char_u buf[20];
3073 static char_u cc[3];
3074 char_u *p;
3075
3076 if (c == 'W' || c == 'w')
3077 p = (char_u *)" warning";
3078 else if (c == 'I' || c == 'i')
3079 p = (char_u *)" info";
3080 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3081 p = (char_u *)" error";
3082 else if (c == 0 || c == 1)
3083 p = (char_u *)"";
3084 else
3085 {
3086 cc[0] = ' ';
3087 cc[1] = c;
3088 cc[2] = NUL;
3089 p = cc;
3090 }
3091
3092 if (nr <= 0)
3093 return p;
3094
3095 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3096 return buf;
3097}
3098
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099/*
3100 * ":cwindow": open the quickfix window if we have errors to display,
3101 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003102 * ":lwindow": open the location list window if we have locations to display,
3103 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 */
3105 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003106ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003108 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 win_T *win;
3110
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003111 if (eap->cmdidx == CMD_lwindow)
3112 {
3113 qi = GET_LOC_LIST(curwin);
3114 if (qi == NULL)
3115 return;
3116 }
3117
3118 /* Look for an existing quickfix window. */
3119 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
3121 /*
3122 * If a quickfix window is open but we have no errors to display,
3123 * close the window. If a quickfix window is not open, then open
3124 * it if we have errors; otherwise, leave it closed.
3125 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003126 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003127 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003128 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 {
3130 if (win != NULL)
3131 ex_cclose(eap);
3132 }
3133 else if (win == NULL)
3134 ex_copen(eap);
3135}
3136
3137/*
3138 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003139 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003142ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003144 win_T *win = NULL;
3145 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003147 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3148 {
3149 qi = GET_LOC_LIST(curwin);
3150 if (qi == NULL)
3151 return;
3152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003154 /* Find existing quickfix window and close it. */
3155 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (win != NULL)
3157 win_close(win, FALSE);
3158}
3159
3160/*
3161 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003162 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 */
3164 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003165ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003167 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003170 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003171 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003172 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003174 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3175 {
3176 qi = GET_LOC_LIST(curwin);
3177 if (qi == NULL)
3178 {
3179 EMSG(_(e_loclist));
3180 return;
3181 }
3182 }
3183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (eap->addr_count != 0)
3185 height = eap->line2;
3186 else
3187 height = QF_WINHEIGHT;
3188
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190#ifdef FEAT_GUI
3191 need_mouse_correct = TRUE;
3192#endif
3193
3194 /*
3195 * Find existing quickfix window, or open a new one.
3196 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003197 win = qf_find_win(qi);
3198
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003199 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003202 if (eap->addr_count != 0)
3203 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003204 if (cmdmod.split & WSP_VERT)
3205 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003206 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003207 win_setwidth(height);
3208 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003209 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003210 win_setheight(height);
3211 }
3212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 else
3214 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003215 int flags = 0;
3216
Bram Moolenaar9c102382006-05-03 21:26:49 +00003217 qf_buf = qf_find_buf(qi);
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 /* The current window becomes the previous window afterwards. */
3220 win = curwin;
3221
Bram Moolenaar77642c02012-11-20 17:55:10 +01003222 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3223 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003224 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003225 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003226 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003227 /* Default is to open the window below the current window */
3228 if (cmdmod.split == 0)
3229 flags = WSP_BELOW;
3230 flags |= WSP_NEWLOC;
3231 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003233 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003235 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003237 /*
3238 * For the location list window, create a reference to the
3239 * location list from the window 'win'.
3240 */
3241 curwin->w_llist_ref = win->w_llist;
3242 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003244
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003245 if (oldwin != curwin)
3246 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003247 if (qf_buf != NULL)
3248 /* Use the existing quickfix buffer */
3249 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003250 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003251 else
3252 {
3253 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003254 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003255 /* switch off 'swapfile' */
3256 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3257 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003258 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003259 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003260 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003261#ifdef FEAT_DIFF
3262 curwin->w_p_diff = FALSE;
3263#endif
3264#ifdef FEAT_FOLDING
3265 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3266 OPT_LOCAL);
3267#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003270 /* Only set the height when still in the same tab page and there is no
3271 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003272 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 win_setheight(height);
3274 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3275 if (win_valid(win))
3276 prevwin = win;
3277 }
3278
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003279 qf_set_title_var(qi);
3280
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 /*
3282 * Fill the buffer with the quickfix list.
3283 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003284 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003286 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 curwin->w_cursor.col = 0;
3288 check_cursor();
3289 update_topline(); /* scroll to show the line */
3290}
3291
3292/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003293 * Move the cursor in the quickfix window to "lnum".
3294 */
3295 static void
3296qf_win_goto(win_T *win, linenr_T lnum)
3297{
3298 win_T *old_curwin = curwin;
3299
3300 curwin = win;
3301 curbuf = win->w_buffer;
3302 curwin->w_cursor.lnum = lnum;
3303 curwin->w_cursor.col = 0;
3304#ifdef FEAT_VIRTUALEDIT
3305 curwin->w_cursor.coladd = 0;
3306#endif
3307 curwin->w_curswant = 0;
3308 update_topline(); /* scroll to show the line */
3309 redraw_later(VALID);
3310 curwin->w_redr_status = TRUE; /* update ruler */
3311 curwin = old_curwin;
3312 curbuf = curwin->w_buffer;
3313}
3314
3315/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003316 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003317 */
3318 void
3319ex_cbottom(exarg_T *eap UNUSED)
3320{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003321 qf_info_T *qi = &ql_info;
3322 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003323
Bram Moolenaar537ef082016-07-09 17:56:19 +02003324 if (eap->cmdidx == CMD_lbottom)
3325 {
3326 qi = GET_LOC_LIST(curwin);
3327 if (qi == NULL)
3328 {
3329 EMSG(_(e_loclist));
3330 return;
3331 }
3332 }
3333
3334 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003335 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3336 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3337}
3338
3339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 * Return the number of the current entry (line number in the quickfix
3341 * window).
3342 */
3343 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003344qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003346 qf_info_T *qi = &ql_info;
3347
3348 if (IS_LL_WINDOW(wp))
3349 /* In the location list window, use the referenced location list */
3350 qi = wp->w_llist_ref;
3351
3352 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353}
3354
3355/*
3356 * Update the cursor position in the quickfix window to the current error.
3357 * Return TRUE if there is a quickfix window.
3358 */
3359 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003360qf_win_pos_update(
3361 qf_info_T *qi,
3362 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003365 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367 /*
3368 * Put the cursor on the current error in the quickfix window, so that
3369 * it's viewable.
3370 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003371 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 if (win != NULL
3373 && qf_index <= win->w_buffer->b_ml.ml_line_count
3374 && old_qf_index != qf_index)
3375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 if (qf_index > old_qf_index)
3377 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003378 win->w_redraw_top = old_qf_index;
3379 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 else
3382 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003383 win->w_redraw_top = qf_index;
3384 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003386 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
3388 return win != NULL;
3389}
3390
3391/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003392 * Check whether the given window is displaying the specified quickfix/location
3393 * list buffer
3394 */
3395 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003396is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003397{
3398 /*
3399 * A window displaying the quickfix buffer will have the w_llist_ref field
3400 * set to NULL.
3401 * A window displaying a location list buffer will have the w_llist_ref
3402 * pointing to the location list.
3403 */
3404 if (bt_quickfix(win->w_buffer))
3405 if ((qi == &ql_info && win->w_llist_ref == NULL)
3406 || (qi != &ql_info && win->w_llist_ref == qi))
3407 return TRUE;
3408
3409 return FALSE;
3410}
3411
3412/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003413 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003414 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003415 */
3416 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003417qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003418{
3419 win_T *win;
3420
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003421 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003422 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003423 return win;
3424 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003425}
3426
3427/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003428 * Find a quickfix buffer.
3429 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
3431 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003432qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003434 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003435 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar9c102382006-05-03 21:26:49 +00003437 FOR_ALL_TAB_WINDOWS(tp, win)
3438 if (is_qf_win(win, qi))
3439 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003440
Bram Moolenaar9c102382006-05-03 21:26:49 +00003441 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443
3444/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003445 * Update the w:quickfix_title variable in the quickfix/location list window
3446 */
3447 static void
3448qf_update_win_titlevar(qf_info_T *qi)
3449{
3450 win_T *win;
3451 win_T *curwin_save;
3452
3453 if ((win = qf_find_win(qi)) != NULL)
3454 {
3455 curwin_save = curwin;
3456 curwin = win;
3457 qf_set_title_var(qi);
3458 curwin = curwin_save;
3459 }
3460}
3461
3462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 * Find the quickfix buffer. If it exists, update the contents.
3464 */
3465 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003466qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
3468 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003469 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003473 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 if (buf != NULL)
3475 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003476 linenr_T old_line_count = buf->b_ml.ml_line_count;
3477
3478 if (old_last == NULL)
3479 /* set curwin/curbuf to buf and save a few things */
3480 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaard823fa92016-08-12 16:29:27 +02003482 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003483
Bram Moolenaar864293a2016-06-02 13:40:04 +02003484 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003485 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003486
Bram Moolenaar864293a2016-06-02 13:40:04 +02003487 if (old_last == NULL)
3488 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003489 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003490
3491 /* restore curwin/curbuf and a few other things */
3492 aucmd_restbuf(&aco);
3493 }
3494
3495 /* Only redraw when added lines are visible. This avoids flickering
3496 * when the added lines are not visible. */
3497 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3498 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 }
3500}
3501
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003502/*
3503 * Set "w:quickfix_title" if "qi" has a title.
3504 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003505 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003506qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003507{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003508 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3509 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003510 qi->qf_lists[qi->qf_curlist].qf_title);
3511}
3512
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513/*
3514 * Fill current buffer with quickfix errors, replacing any previous contents.
3515 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003516 * If "old_last" is not NULL append the items after this one.
3517 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3518 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 */
3520 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003521qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003523 linenr_T lnum;
3524 qfline_T *qfp;
3525 buf_T *errbuf;
3526 int len;
3527 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
Bram Moolenaar864293a2016-06-02 13:40:04 +02003529 if (old_last == NULL)
3530 {
3531 if (buf != curbuf)
3532 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003533 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003534 return;
3535 }
3536
3537 /* delete all existing lines */
3538 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3539 (void)ml_delete((linenr_T)1, FALSE);
3540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
3542 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003543 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
3545 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003546 if (old_last == NULL)
3547 {
3548 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3549 lnum = 0;
3550 }
3551 else
3552 {
3553 qfp = old_last->qf_next;
3554 lnum = buf->b_ml.ml_line_count;
3555 }
3556 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
3558 if (qfp->qf_fnum != 0
3559 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3560 && errbuf->b_fname != NULL)
3561 {
3562 if (qfp->qf_type == 1) /* :helpgrep */
3563 STRCPY(IObuff, gettail(errbuf->b_fname));
3564 else
3565 STRCPY(IObuff, errbuf->b_fname);
3566 len = (int)STRLEN(IObuff);
3567 }
3568 else
3569 len = 0;
3570 IObuff[len++] = '|';
3571
3572 if (qfp->qf_lnum > 0)
3573 {
3574 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3575 len += (int)STRLEN(IObuff + len);
3576
3577 if (qfp->qf_col > 0)
3578 {
3579 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3580 len += (int)STRLEN(IObuff + len);
3581 }
3582
3583 sprintf((char *)IObuff + len, "%s",
3584 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3585 len += (int)STRLEN(IObuff + len);
3586 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003587 else if (qfp->qf_pattern != NULL)
3588 {
3589 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3590 len += (int)STRLEN(IObuff + len);
3591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 IObuff[len++] = '|';
3593 IObuff[len++] = ' ';
3594
3595 /* Remove newlines and leading whitespace from the text.
3596 * For an unrecognized line keep the indent, the compiler may
3597 * mark a word with ^^^^. */
3598 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3599 IObuff + len, IOSIZE - len);
3600
Bram Moolenaar864293a2016-06-02 13:40:04 +02003601 if (ml_append_buf(buf, lnum, IObuff,
3602 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003604 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003606 if (qfp == NULL)
3607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003609
3610 if (old_last == NULL)
3611 /* Delete the empty line which is now at the end */
3612 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
3614
3615 /* correct cursor position */
3616 check_lnums(TRUE);
3617
Bram Moolenaar864293a2016-06-02 13:40:04 +02003618 if (old_last == NULL)
3619 {
3620 /* Set the 'filetype' to "qf" each time after filling the buffer.
3621 * This resembles reading a file into a buffer, it's more logical when
3622 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003623 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003624 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3625 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
Bram Moolenaar864293a2016-06-02 13:40:04 +02003627 keep_filetype = TRUE; /* don't detect 'filetype' */
3628 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003630 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003632 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003633 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003634
Bram Moolenaar864293a2016-06-02 13:40:04 +02003635 /* make sure it will be redrawn */
3636 redraw_curbuf_later(NOT_VALID);
3637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639 /* Restore KeyTyped, setting 'filetype' may reset it. */
3640 KeyTyped = old_KeyTyped;
3641}
3642
Bram Moolenaarb254af32017-12-18 19:48:58 +01003643 static void
3644qf_list_changed(qf_info_T *qi, int qf_idx)
3645{
3646 qi->qf_lists[qf_idx].qf_changedtick++;
3647}
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003650 * Return TRUE when using ":vimgrep" for ":grep".
3651 */
3652 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003653grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003654{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003655 return ((cmdidx == CMD_grep
3656 || cmdidx == CMD_lgrep
3657 || cmdidx == CMD_grepadd
3658 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003659 && STRCMP("internal",
3660 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3661}
3662
3663/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003664 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 */
3666 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003667ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003669 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003671 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003673 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003674 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003675 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003676 char_u *au_name = NULL;
3677
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003678 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3679 if (grep_internal(eap->cmdidx))
3680 {
3681 ex_vimgrep(eap);
3682 return;
3683 }
3684
Bram Moolenaar7c626922005-02-07 22:01:03 +00003685 switch (eap->cmdidx)
3686 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003687 case CMD_make: au_name = (char_u *)"make"; break;
3688 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3689 case CMD_grep: au_name = (char_u *)"grep"; break;
3690 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3691 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3692 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003693 default: break;
3694 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003695 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3696 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003697 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003698#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003699 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003700 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003701#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003702 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003703#ifdef FEAT_MBYTE
3704 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
Bram Moolenaara6557602006-02-04 22:43:20 +00003707 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3708 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003709 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003712 fname = get_mef_name();
3713 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003715 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 /*
3718 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3719 */
3720 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3721 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003722 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 cmd = alloc(len);
3724 if (cmd == NULL)
3725 return;
3726 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3727 (char *)p_shq);
3728 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003729 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 /*
3731 * Output a newline if there's something else than the :make command that
3732 * was typed (in which case the cursor is in column 0).
3733 */
3734 if (msg_col == 0)
3735 msg_didout = FALSE;
3736 msg_start();
3737 MSG_PUTS(":!");
3738 msg_outtrans(cmd); /* show what we are doing */
3739
3740 /* let the shell know if we are redirecting output or not */
3741 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3742
3743#ifdef AMIGA
3744 out_flush();
3745 /* read window status report and redraw before message */
3746 (void)char_avail();
3747#endif
3748
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003749 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003750 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3751 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003752 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003753 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003754 if (wp != NULL)
3755 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003756 if (res >= 0 && qi != NULL)
3757 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003758 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003759 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003760 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3761 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01003762 if (qi != NULL && qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003763 res = qi->qf_lists[qi->qf_curlist].qf_count;
3764 else
3765 res = 0;
3766 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003767 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003768 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
Bram Moolenaar7c626922005-02-07 22:01:03 +00003770 mch_remove(fname);
3771 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 vim_free(cmd);
3773}
3774
3775/*
3776 * Return the name for the errorfile, in allocated memory.
3777 * Find a new unique name when 'makeef' contains "##".
3778 * Returns NULL for error.
3779 */
3780 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003781get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
3783 char_u *p;
3784 char_u *name;
3785 static int start = -1;
3786 static int off = 0;
3787#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003788 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789#endif
3790
3791 if (*p_mef == NUL)
3792 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003793 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (name == NULL)
3795 EMSG(_(e_notmp));
3796 return name;
3797 }
3798
3799 for (p = p_mef; *p; ++p)
3800 if (p[0] == '#' && p[1] == '#')
3801 break;
3802
3803 if (*p == NUL)
3804 return vim_strsave(p_mef);
3805
3806 /* Keep trying until the name doesn't exist yet. */
3807 for (;;)
3808 {
3809 if (start == -1)
3810 start = mch_get_pid();
3811 else
3812 off += 19;
3813
3814 name = alloc((unsigned)STRLEN(p_mef) + 30);
3815 if (name == NULL)
3816 break;
3817 STRCPY(name, p_mef);
3818 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3819 STRCAT(name, p + 2);
3820 if (mch_getperm(name) < 0
3821#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003822 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 && mch_lstat((char *)name, &sb) < 0
3824#endif
3825 )
3826 break;
3827 vim_free(name);
3828 }
3829 return name;
3830}
3831
3832/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003833 * Returns the number of valid entries in the current quickfix/location list.
3834 */
3835 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003836qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003837{
3838 qf_info_T *qi = &ql_info;
3839 qfline_T *qfp;
3840 int i, sz = 0;
3841 int prev_fnum = 0;
3842
3843 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3844 {
3845 /* Location list */
3846 qi = GET_LOC_LIST(curwin);
3847 if (qi == NULL)
3848 return 0;
3849 }
3850
3851 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003852 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003853 ++i, qfp = qfp->qf_next)
3854 {
3855 if (qfp->qf_valid)
3856 {
3857 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3858 sz++; /* Count all valid entries */
3859 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3860 {
3861 /* Count the number of files */
3862 sz++;
3863 prev_fnum = qfp->qf_fnum;
3864 }
3865 }
3866 }
3867
3868 return sz;
3869}
3870
3871/*
3872 * Returns the current index of the quickfix/location list.
3873 * Returns 0 if there is an error.
3874 */
3875 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003876qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003877{
3878 qf_info_T *qi = &ql_info;
3879
3880 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3881 {
3882 /* Location list */
3883 qi = GET_LOC_LIST(curwin);
3884 if (qi == NULL)
3885 return 0;
3886 }
3887
3888 return qi->qf_lists[qi->qf_curlist].qf_index;
3889}
3890
3891/*
3892 * Returns the current index in the quickfix/location list (counting only valid
3893 * entries). If no valid entries are in the list, then returns 1.
3894 */
3895 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003896qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003897{
3898 qf_info_T *qi = &ql_info;
3899 qf_list_T *qfl;
3900 qfline_T *qfp;
3901 int i, eidx = 0;
3902 int prev_fnum = 0;
3903
3904 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3905 {
3906 /* Location list */
3907 qi = GET_LOC_LIST(curwin);
3908 if (qi == NULL)
3909 return 1;
3910 }
3911
3912 qfl = &qi->qf_lists[qi->qf_curlist];
3913 qfp = qfl->qf_start;
3914
3915 /* check if the list has valid errors */
3916 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3917 return 1;
3918
3919 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3920 {
3921 if (qfp->qf_valid)
3922 {
3923 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3924 {
3925 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3926 {
3927 /* Count the number of files */
3928 eidx++;
3929 prev_fnum = qfp->qf_fnum;
3930 }
3931 }
3932 else
3933 eidx++;
3934 }
3935 }
3936
3937 return eidx ? eidx : 1;
3938}
3939
3940/*
3941 * Get the 'n'th valid error entry in the quickfix or location list.
3942 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3943 * For :cdo and :ldo returns the 'n'th valid error entry.
3944 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3945 */
3946 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003947qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003948{
3949 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3950 qfline_T *qfp = qfl->qf_start;
3951 int i, eidx;
3952 int prev_fnum = 0;
3953
3954 /* check if the list has valid errors */
3955 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3956 return 1;
3957
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003958 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003959 i++, qfp = qfp->qf_next)
3960 {
3961 if (qfp->qf_valid)
3962 {
3963 if (fdo)
3964 {
3965 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3966 {
3967 /* Count the number of files */
3968 eidx++;
3969 prev_fnum = qfp->qf_fnum;
3970 }
3971 }
3972 else
3973 eidx++;
3974 }
3975
3976 if (eidx == n)
3977 break;
3978 }
3979
3980 if (i <= qfl->qf_count)
3981 return i;
3982 else
3983 return 1;
3984}
3985
3986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003988 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003989 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 */
3991 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003992ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003994 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003995 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003996
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003997 if (eap->cmdidx == CMD_ll
3998 || eap->cmdidx == CMD_lrewind
3999 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004000 || eap->cmdidx == CMD_llast
4001 || eap->cmdidx == CMD_ldo
4002 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004003 {
4004 qi = GET_LOC_LIST(curwin);
4005 if (qi == NULL)
4006 {
4007 EMSG(_(e_loclist));
4008 return;
4009 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004010 }
4011
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004012 if (eap->addr_count > 0)
4013 errornr = (int)eap->line2;
4014 else
4015 {
4016 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4017 errornr = 0;
4018 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4019 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4020 errornr = 1;
4021 else
4022 errornr = 32767;
4023 }
4024
4025 /* For cdo and ldo commands, jump to the nth valid error.
4026 * For cfdo and lfdo commands, jump to the nth valid file entry.
4027 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004028 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4029 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004030 errornr = qf_get_nth_valid_entry(qi,
4031 eap->addr_count > 0 ? (int)eap->line1 : 1,
4032 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4033
4034 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035}
4036
4037/*
4038 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004039 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004040 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 */
4042 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004043ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004045 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004046 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004047
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004048 if (eap->cmdidx == CMD_lnext
4049 || eap->cmdidx == CMD_lNext
4050 || eap->cmdidx == CMD_lprevious
4051 || eap->cmdidx == CMD_lnfile
4052 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004053 || eap->cmdidx == CMD_lpfile
4054 || eap->cmdidx == CMD_ldo
4055 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004056 {
4057 qi = GET_LOC_LIST(curwin);
4058 if (qi == NULL)
4059 {
4060 EMSG(_(e_loclist));
4061 return;
4062 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004063 }
4064
Bram Moolenaar55b69262017-08-13 13:42:01 +02004065 if (eap->addr_count > 0
4066 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4067 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004068 errornr = (int)eap->line2;
4069 else
4070 errornr = 1;
4071
4072 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4073 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004075 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4076 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004078 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4079 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 ? BACKWARD_FILE
4081 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004082 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083}
4084
4085/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004086 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004087 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 */
4089 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004090ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004092 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004093 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004094 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004095 char_u *au_name = NULL;
Bram Moolenaarfc6f16b2018-03-06 17:43:22 +01004096 int save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004097 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004098
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004099 switch (eap->cmdidx)
4100 {
4101 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4102 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4103 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4104 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4105 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4106 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4107 default: break;
4108 }
4109 if (au_name != NULL)
4110 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004111#ifdef FEAT_MBYTE
4112 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4113#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004114#ifdef FEAT_BROWSE
4115 if (cmdmod.browse)
4116 {
4117 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004118 NULL, NULL,
4119 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004120 if (browse_file == NULL)
4121 return;
4122 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4123 vim_free(browse_file);
4124 }
4125 else
4126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004128 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004129
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004130 if (eap->cmdidx == CMD_lfile
4131 || eap->cmdidx == CMD_lgetfile
4132 || eap->cmdidx == CMD_laddfile)
4133 wp = curwin;
4134
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004135 /*
4136 * This function is used by the :cfile, :cgetfile and :caddfile
4137 * commands.
4138 * :cfile always creates a new quickfix list and jumps to the
4139 * first error.
4140 * :cgetfile creates a new quickfix list but doesn't jump to the
4141 * first error.
4142 * :caddfile adds to an existing quickfix list. If there is no
4143 * quickfix list then a new list is created.
4144 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004145 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4146 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004147 if (wp != NULL)
4148 qi = GET_LOC_LIST(wp);
4149 if (res >= 0 && qi != NULL)
4150 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004151 if (qi != NULL)
4152 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004153 if (au_name != NULL)
4154 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004155
4156 /* An autocmd might have freed the quickfix/location list. Check whether it
4157 * is still valid. */
4158 if (qi != NULL && !qflist_valid(wp, save_qfid))
Bram Moolenaar3c097222017-12-21 20:54:49 +01004159 return;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004160 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004161 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162}
4163
4164/*
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004165 * Return the quickfix/location list number with the given identifier.
4166 * Returns -1 if list is not found.
4167 */
4168 static int
4169qf_id2nr(qf_info_T *qi, int_u qfid)
4170{
4171 int qf_idx;
4172
4173 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4174 if (qi->qf_lists[qf_idx].qf_id == qfid)
4175 return qf_idx;
Bram Moolenaar29ce4092018-04-28 21:56:44 +02004176 return INVALID_QFIDX;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004177}
4178
4179/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004180 * Return the vimgrep autocmd name.
4181 */
4182 static char_u *
4183vgr_get_auname(cmdidx_T cmdidx)
4184{
4185 switch (cmdidx)
4186 {
4187 case CMD_vimgrep: return (char_u *)"vimgrep";
4188 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4189 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4190 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4191 case CMD_grep: return (char_u *)"grep";
4192 case CMD_lgrep: return (char_u *)"lgrep";
4193 case CMD_grepadd: return (char_u *)"grepadd";
4194 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4195 default: return NULL;
4196 }
4197}
4198
4199/*
4200 * Initialize the regmatch used by vimgrep for pattern "s".
4201 */
4202 static void
4203vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4204{
4205 /* Get the search pattern: either white-separated or enclosed in // */
4206 regmatch->regprog = NULL;
4207
4208 if (s == NULL || *s == NUL)
4209 {
4210 /* Pattern is empty, use last search pattern. */
4211 if (last_search_pat() == NULL)
4212 {
4213 EMSG(_(e_noprevre));
4214 return;
4215 }
4216 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4217 }
4218 else
4219 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4220
4221 regmatch->rmm_ic = p_ic;
4222 regmatch->rmm_maxcol = 0;
4223}
4224
4225/*
4226 * Display a file name when vimgrep is running.
4227 */
4228 static void
4229vgr_display_fname(char_u *fname)
4230{
4231 char_u *p;
4232
4233 msg_start();
4234 p = msg_strtrunc(fname, TRUE);
4235 if (p == NULL)
4236 msg_outtrans(fname);
4237 else
4238 {
4239 msg_outtrans(p);
4240 vim_free(p);
4241 }
4242 msg_clr_eos();
4243 msg_didout = FALSE; /* overwrite this message */
4244 msg_nowait = TRUE; /* don't wait for this message */
4245 msg_col = 0;
4246 out_flush();
4247}
4248
4249/*
4250 * Load a dummy buffer to search for a pattern using vimgrep.
4251 */
4252 static buf_T *
4253vgr_load_dummy_buf(
4254 char_u *fname,
4255 char_u *dirname_start,
4256 char_u *dirname_now)
4257{
4258 int save_mls;
4259#if defined(FEAT_SYN_HL)
4260 char_u *save_ei = NULL;
4261#endif
4262 buf_T *buf;
4263
4264#if defined(FEAT_SYN_HL)
4265 /* Don't do Filetype autocommands to avoid loading syntax and
4266 * indent scripts, a great speed improvement. */
4267 save_ei = au_event_disable(",Filetype");
4268#endif
4269 /* Don't use modelines here, it's useless. */
4270 save_mls = p_mls;
4271 p_mls = 0;
4272
4273 /* Load file into a buffer, so that 'fileencoding' is detected,
4274 * autocommands applied, etc. */
4275 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4276
4277 p_mls = save_mls;
4278#if defined(FEAT_SYN_HL)
4279 au_event_restore(save_ei);
4280#endif
4281
4282 return buf;
4283}
4284
4285/*
4286 * Check whether a quickfix/location list valid. Autocmds may remove or change
4287 * a quickfix list when vimgrep is running. If the list is not found, create a
4288 * new list.
4289 */
4290 static int
4291vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004292 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004293 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004294 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004295 char_u *title)
4296{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004297 /* Verify that the quickfix/location list was not freed by an autocmd */
4298 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004299 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004300 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004301 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004302 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004303 EMSG(_(e_loc_list_changed));
4304 return FALSE;
4305 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004306 else
4307 {
4308 /* Quickfix list is not found, create a new one. */
4309 qf_new_list(qi, title);
4310 return TRUE;
4311 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004312 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004313
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004314 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004315 /* Autocommands changed the quickfix list. Find the one we were
4316 * using and restore it. */
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004317 qi->qf_curlist = qf_id2nr(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004318
4319 return TRUE;
4320}
4321
4322/*
4323 * Search for a pattern in all the lines in a buffer and add the matching lines
4324 * to a quickfix list.
4325 */
4326 static int
4327vgr_match_buflines(
4328 qf_info_T *qi,
4329 char_u *fname,
4330 buf_T *buf,
4331 regmmatch_T *regmatch,
4332 long tomatch,
4333 int duplicate_name,
4334 int flags)
4335{
4336 int found_match = FALSE;
4337 long lnum;
4338 colnr_T col;
4339
4340 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
4341 {
4342 col = 0;
4343 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
4344 col, NULL, NULL) > 0)
4345 {
4346 /* Pass the buffer number so that it gets used even for a
4347 * dummy buffer, unless duplicate_name is set, then the
4348 * buffer will be wiped out below. */
4349 if (qf_add_entry(qi,
4350 qi->qf_curlist,
4351 NULL, /* dir */
4352 fname,
4353 duplicate_name ? 0 : buf->b_fnum,
4354 ml_get_buf(buf,
4355 regmatch->startpos[0].lnum + lnum, FALSE),
4356 regmatch->startpos[0].lnum + lnum,
4357 regmatch->startpos[0].col + 1,
4358 FALSE, /* vis_col */
4359 NULL, /* search pattern */
4360 0, /* nr */
4361 0, /* type */
4362 TRUE /* valid */
4363 ) == FAIL)
4364 {
4365 got_int = TRUE;
4366 break;
4367 }
4368 found_match = TRUE;
4369 if (--tomatch == 0)
4370 break;
4371 if ((flags & VGR_GLOBAL) == 0
4372 || regmatch->endpos[0].lnum > 0)
4373 break;
4374 col = regmatch->endpos[0].col
4375 + (col == regmatch->endpos[0].col);
4376 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
4377 break;
4378 }
4379 line_breakcheck();
4380 if (got_int)
4381 break;
4382 }
4383
4384 return found_match;
4385}
4386
4387/*
4388 * Jump to the first match and update the directory.
4389 */
4390 static void
4391vgr_jump_to_match(
4392 qf_info_T *qi,
4393 int forceit,
4394 int *redraw_for_dummy,
4395 buf_T *first_match_buf,
4396 char_u *target_dir)
4397{
4398 buf_T *buf;
4399
4400 buf = curbuf;
4401 qf_jump(qi, 0, 0, forceit);
4402 if (buf != curbuf)
4403 /* If we jumped to another buffer redrawing will already be
4404 * taken care of. */
4405 *redraw_for_dummy = FALSE;
4406
4407 /* Jump to the directory used after loading the buffer. */
4408 if (curbuf == first_match_buf && target_dir != NULL)
4409 {
4410 exarg_T ea;
4411
4412 ea.arg = target_dir;
4413 ea.cmdidx = CMD_lcd;
4414 ex_cd(&ea);
4415 }
4416}
4417
4418/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004419 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004420 * ":vimgrepadd {pattern} file(s)"
4421 * ":lvimgrep {pattern} file(s)"
4422 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004423 */
4424 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004425ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004426{
Bram Moolenaar81695252004-12-29 20:58:21 +00004427 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004428 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004429 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004430 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004431 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004432 char_u *s;
4433 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004434 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004435 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004436 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004437 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004438 buf_T *buf;
4439 int duplicate_name = FALSE;
4440 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004441 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004442 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004443 buf_T *first_match_buf = NULL;
4444 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004445 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004446 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004447 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004448 char_u *dirname_start = NULL;
4449 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004450 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004451 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004452
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004453 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01004454 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4455 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004456 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004457#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004458 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004459 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004460#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004461 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004462
Bram Moolenaar754b5602006-02-09 23:53:20 +00004463 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004464 || eap->cmdidx == CMD_lvimgrep
4465 || eap->cmdidx == CMD_lgrepadd
4466 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004467 {
4468 qi = ll_get_or_alloc_list(curwin);
4469 if (qi == NULL)
4470 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004471 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004472 }
4473
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004474 if (eap->addr_count > 0)
4475 tomatch = eap->line2;
4476 else
4477 tomatch = MAXLNUM;
4478
Bram Moolenaar81695252004-12-29 20:58:21 +00004479 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004480 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004481 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004482 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004483 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004484 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004485 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004486 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004487 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004488
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004489 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004490 if (regmatch.regprog == NULL)
4491 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004492
4493 p = skipwhite(p);
4494 if (*p == NUL)
4495 {
4496 EMSG(_("E683: File name missing or invalid pattern"));
4497 goto theend;
4498 }
4499
Bram Moolenaar55b69262017-08-13 13:42:01 +02004500 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004501 && eap->cmdidx != CMD_vimgrepadd
4502 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004503 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004504 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004505 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004506
4507 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004508 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004509 goto theend;
4510 if (fcount == 0)
4511 {
4512 EMSG(_(e_nomatch));
4513 goto theend;
4514 }
4515
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004516 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4517 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004518 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004519 {
4520 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004521 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004522 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004523
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004524 /* Remember the current directory, because a BufRead autocommand that does
4525 * ":lcd %:p:h" changes the meaning of short path names. */
4526 mch_dirname(dirname_start, MAXPATHL);
4527
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004528 /* Remember the current quickfix list identifier, so that we can check for
4529 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004530 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004531
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004532 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004533 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004534 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004535 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004536 if (time(NULL) > seconds)
4537 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004538 /* Display the file name every second or so, show the user we are
4539 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004540 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004541 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004542 }
4543
Bram Moolenaar81695252004-12-29 20:58:21 +00004544 buf = buflist_findname_exp(fnames[fi]);
4545 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4546 {
4547 /* Remember that a buffer with this name already exists. */
4548 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004549 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004550 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004551
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004552 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00004553 }
4554 else
4555 /* Use existing, loaded buffer. */
4556 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004557
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004558 /* Check whether the quickfix list is still valid. When loading a
4559 * buffer above, autocommands might have changed the quickfix list. */
4560 if (!vgr_qflist_valid(wp, qi, save_qfid, *eap->cmdlinep))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004561 {
4562 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004563 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004564 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004565 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004566
Bram Moolenaar81695252004-12-29 20:58:21 +00004567 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004568 {
4569 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004570 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004571 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004572 else
4573 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004574 /* Try for a match in all lines of the buffer.
4575 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004576 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
4577 tomatch, duplicate_name, flags);
4578
Bram Moolenaar81695252004-12-29 20:58:21 +00004579 if (using_dummy)
4580 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004581 if (found_match && first_match_buf == NULL)
4582 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004583 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004584 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004585 /* Never keep a dummy buffer if there is another buffer
4586 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004587 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004588 buf = NULL;
4589 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004590 else if (!cmdmod.hide
4591 || buf->b_p_bh[0] == 'u' /* "unload" */
4592 || buf->b_p_bh[0] == 'w' /* "wipe" */
4593 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004594 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004595 /* When no match was found we don't need to remember the
4596 * buffer, wipe it out. If there was a match and it
4597 * wasn't the first one or we won't jump there: only
4598 * unload the buffer.
4599 * Ignore 'hidden' here, because it may lead to having too
4600 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004601 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004602 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004603 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004604 buf = NULL;
4605 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004606 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004607 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004608 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004609 /* Keeping the buffer, remove the dummy flag. */
4610 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004611 buf = NULL;
4612 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004613 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004614
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004615 if (buf != NULL)
4616 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004617 /* Keeping the buffer, remove the dummy flag. */
4618 buf->b_flags &= ~BF_DUMMY;
4619
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004620 /* If the buffer is still loaded we need to use the
4621 * directory we jumped to below. */
4622 if (buf == first_match_buf
4623 && target_dir == NULL
4624 && STRCMP(dirname_start, dirname_now) != 0)
4625 target_dir = vim_strsave(dirname_now);
4626
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004627 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004628 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004629 * need to be done (again). But not the window-local
4630 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004631 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004632#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004633 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4634 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004635#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004636 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004637 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004638 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004639 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004640 }
4641 }
4642
4643 FreeWild(fcount, fnames);
4644
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004645 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4646 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4647 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004648 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004649
Bram Moolenaar864293a2016-06-02 13:40:04 +02004650 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004651
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004652 if (au_name != NULL)
4653 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4654 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004655 /*
4656 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4657 * is still valid.
4658 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004659 if (!qflist_valid(wp, save_qfid))
4660 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004661
Bram Moolenaar86b68352004-12-27 21:59:20 +00004662 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004663 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004664 {
4665 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004666 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
4667 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004668 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004669 else
4670 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004671
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004672 /* If we loaded a dummy buffer into the current window, the autocommands
4673 * may have messed up things, need to redraw and recompute folds. */
4674 if (redraw_for_dummy)
4675 {
4676#ifdef FEAT_FOLDING
4677 foldUpdateAll(curwin);
4678#else
4679 redraw_later(NOT_VALID);
4680#endif
4681 }
4682
Bram Moolenaar86b68352004-12-27 21:59:20 +00004683theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004684 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004685 vim_free(dirname_now);
4686 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004687 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004688 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004689}
4690
4691/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004692 * Restore current working directory to "dirname_start" if they differ, taking
4693 * into account whether it is set locally or globally.
4694 */
4695 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004696restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004697{
4698 char_u *dirname_now = alloc(MAXPATHL);
4699
4700 if (NULL != dirname_now)
4701 {
4702 mch_dirname(dirname_now, MAXPATHL);
4703 if (STRCMP(dirname_start, dirname_now) != 0)
4704 {
4705 /* If the directory has changed, change it back by building up an
4706 * appropriate ex command and executing it. */
4707 exarg_T ea;
4708
4709 ea.arg = dirname_start;
4710 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4711 ex_cd(&ea);
4712 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004713 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004714 }
4715}
4716
4717/*
4718 * Load file "fname" into a dummy buffer and return the buffer pointer,
4719 * placing the directory resulting from the buffer load into the
4720 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4721 * prior to calling this function. Restores directory to "dirname_start" prior
4722 * to returning, if autocmds or the 'autochdir' option have changed it.
4723 *
4724 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4725 * or wipe_dummy_buffer() later!
4726 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004727 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004728 */
4729 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004730load_dummy_buffer(
4731 char_u *fname,
4732 char_u *dirname_start, /* in: old directory */
4733 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004734{
4735 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004736 bufref_T newbufref;
4737 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004738 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004739 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004740 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004741
4742 /* Allocate a buffer without putting it in the buffer list. */
4743 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4744 if (newbuf == NULL)
4745 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004746 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004747
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004748 /* Init the options. */
4749 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4750
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004751 /* need to open the memfile before putting the buffer in a window */
4752 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004753 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004754 /* Make sure this buffer isn't wiped out by auto commands. */
4755 ++newbuf->b_locked;
4756
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004757 /* set curwin/curbuf to buf and save a few things */
4758 aucmd_prepbuf(&aco, newbuf);
4759
4760 /* Need to set the filename for autocommands. */
4761 (void)setfname(curbuf, fname, NULL, FALSE);
4762
Bram Moolenaar81695252004-12-29 20:58:21 +00004763 /* Create swap file now to avoid the ATTENTION message. */
4764 check_need_swap(TRUE);
4765
4766 /* Remove the "dummy" flag, otherwise autocommands may not
4767 * work. */
4768 curbuf->b_flags &= ~BF_DUMMY;
4769
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004770 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004771 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004772 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004773 NULL, READ_NEW | READ_DUMMY);
4774 --newbuf->b_locked;
4775 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004776 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004777 && !(curbuf->b_flags & BF_NEW))
4778 {
4779 failed = FALSE;
4780 if (curbuf != newbuf)
4781 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004782 /* Bloody autocommands changed the buffer! Can happen when
4783 * using netrw and editing a remote file. Use the current
4784 * buffer instead, delete the dummy one after restoring the
4785 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004786 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004787 newbuf = curbuf;
4788 }
4789 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004790
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004791 /* restore curwin/curbuf and a few other things */
4792 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004793 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4794 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004795
4796 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4797 * skip it. */
4798 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004799 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004800
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004801 /*
4802 * When autocommands/'autochdir' option changed directory: go back.
4803 * Let the caller know what the resulting dir was first, in case it is
4804 * important.
4805 */
4806 mch_dirname(resulting_dir, MAXPATHL);
4807 restore_start_dir(dirname_start);
4808
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004809 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004810 return NULL;
4811 if (failed)
4812 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004813 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004814 return NULL;
4815 }
4816 return newbuf;
4817}
4818
4819/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004820 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4821 * directory to "dirname_start" prior to returning, if autocmds or the
4822 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004823 */
4824 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004825wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004826{
4827 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004828 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004829#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004830 cleanup_T cs;
4831
4832 /* Reset the error/interrupt/exception state here so that aborting()
4833 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4834 * work when got_int is set. */
4835 enter_cleanup(&cs);
4836#endif
4837
Bram Moolenaar81695252004-12-29 20:58:21 +00004838 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004839
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004840#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004841 /* Restore the error/interrupt/exception state if not discarded by a
4842 * new aborting error, interrupt, or uncaught exception. */
4843 leave_cleanup(&cs);
4844#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004845 /* When autocommands/'autochdir' option changed directory: go back. */
4846 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004847 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004848}
4849
4850/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004851 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4852 * directory to "dirname_start" prior to returning, if autocmds or the
4853 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004854 */
4855 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004856unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004857{
4858 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004859 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004860 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004861
4862 /* When autocommands/'autochdir' option changed directory: go back. */
4863 restore_start_dir(dirname_start);
4864 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004865}
4866
Bram Moolenaar05159a02005-02-26 23:04:13 +00004867#if defined(FEAT_EVAL) || defined(PROTO)
4868/*
4869 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004870 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004871 */
4872 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004873get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004874{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004875 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004876 dict_T *dict;
4877 char_u buf[2];
4878 qfline_T *qfp;
4879 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004880 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004881
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004882 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004883 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004884 qi = &ql_info;
4885 if (wp != NULL)
4886 {
4887 qi = GET_LOC_LIST(wp);
4888 if (qi == NULL)
4889 return FAIL;
4890 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004891 }
4892
Bram Moolenaar29ce4092018-04-28 21:56:44 +02004893 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004894 qf_idx = qi->qf_curlist;
4895
4896 if (qf_idx >= qi->qf_listcount
4897 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004898 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004899
Bram Moolenaard823fa92016-08-12 16:29:27 +02004900 qfp = qi->qf_lists[qf_idx].qf_start;
4901 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004902 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004903 /* Handle entries with a non-existing buffer number. */
4904 bufnum = qfp->qf_fnum;
4905 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4906 bufnum = 0;
4907
Bram Moolenaar05159a02005-02-26 23:04:13 +00004908 if ((dict = dict_alloc()) == NULL)
4909 return FAIL;
4910 if (list_append_dict(list, dict) == FAIL)
4911 return FAIL;
4912
4913 buf[0] = qfp->qf_type;
4914 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004915 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004916 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4917 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4918 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4919 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004920 || dict_add_nr_str(dict, "pattern", 0L,
4921 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4922 || dict_add_nr_str(dict, "text", 0L,
4923 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004924 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4925 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4926 return FAIL;
4927
4928 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004929 if (qfp == NULL)
4930 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004931 }
4932 return OK;
4933}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004934
4935/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004936 * Flags used by getqflist()/getloclist() to determine which fields to return.
4937 */
4938enum {
4939 QF_GETLIST_NONE = 0x0,
4940 QF_GETLIST_TITLE = 0x1,
4941 QF_GETLIST_ITEMS = 0x2,
4942 QF_GETLIST_NR = 0x4,
4943 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004944 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004945 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004946 QF_GETLIST_IDX = 0x40,
4947 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01004948 QF_GETLIST_TICK = 0x100,
4949 QF_GETLIST_ALL = 0x1FF
Bram Moolenaard823fa92016-08-12 16:29:27 +02004950};
4951
4952/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02004953 * Parse text from 'di' and return the quickfix list items.
4954 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004955 */
4956 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004957qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004958{
4959 int status = FAIL;
4960 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004961 char_u *errorformat = p_efm;
4962 dictitem_T *efm_di;
4963 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004964
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004965 /* Only a List value is supported */
4966 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004967 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004968 /* If errorformat is supplied then use it, otherwise use the 'efm'
4969 * option setting
4970 */
4971 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4972 {
4973 if (efm_di->di_tv.v_type != VAR_STRING ||
4974 efm_di->di_tv.vval.v_string == NULL)
4975 return FAIL;
4976 errorformat = efm_di->di_tv.vval.v_string;
4977 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004978
Bram Moolenaar36538222017-09-02 19:51:44 +02004979 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004980 if (l == NULL)
4981 return FAIL;
4982
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004983 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4984 if (qi != NULL)
4985 {
4986 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4987 qi->qf_refcount++;
4988
Bram Moolenaar36538222017-09-02 19:51:44 +02004989 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004990 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4991 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004992 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004993 qf_free(qi, 0);
4994 }
4995 free(qi);
4996 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004997 dict_add_list(retdict, "items", l);
4998 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004999 }
5000
5001 return status;
5002}
5003
5004/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005005 * Return the quickfix/location list window identifier in the current tabpage.
5006 */
5007 static int
5008qf_winid(qf_info_T *qi)
5009{
5010 win_T *win;
5011
5012 /* The quickfix window can be opened even if the quickfix list is not set
5013 * using ":copen". This is not true for location lists. */
5014 if (qi == NULL)
5015 return 0;
5016 win = qf_find_win(qi);
5017 if (win != NULL)
5018 return win->w_id;
5019 return 0;
5020}
5021
5022/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005023 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005024 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005025 static int
5026qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005027{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005028 int flags = QF_GETLIST_NONE;
5029
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005030 if (dict_find(what, (char_u *)"all", -1) != NULL)
5031 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005032
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005033 if (dict_find(what, (char_u *)"title", -1) != NULL)
5034 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005035
Bram Moolenaara6d48492017-12-12 22:45:31 +01005036 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5037 flags |= QF_GETLIST_NR;
5038
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005039 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5040 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005041
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005042 if (dict_find(what, (char_u *)"context", -1) != NULL)
5043 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005044
Bram Moolenaara6d48492017-12-12 22:45:31 +01005045 if (dict_find(what, (char_u *)"id", -1) != NULL)
5046 flags |= QF_GETLIST_ID;
5047
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005048 if (dict_find(what, (char_u *)"items", -1) != NULL)
5049 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005050
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005051 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5052 flags |= QF_GETLIST_IDX;
5053
5054 if (dict_find(what, (char_u *)"size", -1) != NULL)
5055 flags |= QF_GETLIST_SIZE;
5056
Bram Moolenaarb254af32017-12-18 19:48:58 +01005057 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5058 flags |= QF_GETLIST_TICK;
5059
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005060 return flags;
5061}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005062
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005063/*
5064 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5065 * If 'nr' and 'id' are not present in 'what' then return the current
5066 * quickfix list index.
5067 * If 'nr' is zero then return the current quickfix list index.
5068 * If 'nr' is '$' then return the last quickfix list index.
5069 * If 'id' is present then return the index of the quickfix list with that id.
5070 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5071 * Return -1, if quickfix list is not present or if the stack is empty.
5072 */
5073 static int
5074qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5075{
5076 int qf_idx;
5077 dictitem_T *di;
5078
5079 qf_idx = qi->qf_curlist; /* default is the current list */
5080 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5081 {
5082 /* Use the specified quickfix/location list */
5083 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005084 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005085 /* for zero use the current list */
5086 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005087 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005088 qf_idx = di->di_tv.vval.v_number - 1;
5089 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005090 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005091 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005092 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005093 else if (di->di_tv.v_type == VAR_STRING
5094 && di->di_tv.vval.v_string != NULL
5095 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5096 /* Get the last quickfix list number */
5097 qf_idx = qi->qf_listcount - 1;
5098 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005099 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005100 }
5101
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005102 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5103 {
5104 /* Look for a list with the specified id */
5105 if (di->di_tv.v_type == VAR_NUMBER)
5106 {
5107 /*
5108 * For zero, use the current list or the list specified by 'nr'
5109 */
5110 if (di->di_tv.vval.v_number != 0)
5111 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5112 }
5113 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005114 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005115 }
5116
5117 return qf_idx;
5118}
5119
5120/*
5121 * Return default values for quickfix list properties in retdict.
5122 */
5123 static int
5124qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5125{
5126 int status = OK;
5127
5128 if (flags & QF_GETLIST_TITLE)
5129 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5130 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5131 {
5132 list_T *l = list_alloc();
5133 if (l != NULL)
5134 status = dict_add_list(retdict, "items", l);
5135 else
5136 status = FAIL;
5137 }
5138 if ((status == OK) && (flags & QF_GETLIST_NR))
5139 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5140 if ((status == OK) && (flags & QF_GETLIST_WINID))
5141 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
5142 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5143 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5144 if ((status == OK) && (flags & QF_GETLIST_ID))
5145 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5146 if ((status == OK) && (flags & QF_GETLIST_IDX))
5147 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5148 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5149 status = dict_add_nr_str(retdict, "size", 0L, NULL);
5150 if ((status == OK) && (flags & QF_GETLIST_TICK))
5151 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
5152
5153 return status;
5154}
5155
5156/*
5157 * Return the quickfix list title as 'title' in retdict
5158 */
5159 static int
5160qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5161{
5162 char_u *t;
5163
5164 t = qi->qf_lists[qf_idx].qf_title;
5165 if (t == NULL)
5166 t = (char_u *)"";
5167 return dict_add_nr_str(retdict, "title", 0L, t);
5168}
5169
5170/*
5171 * Return the quickfix list items/entries as 'items' in retdict
5172 */
5173 static int
5174qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5175{
5176 int status = OK;
5177 list_T *l = list_alloc();
5178 if (l != NULL)
5179 {
5180 (void)get_errorlist(qi, NULL, qf_idx, l);
5181 dict_add_list(retdict, "items", l);
5182 }
5183 else
5184 status = FAIL;
5185
5186 return status;
5187}
5188
5189/*
5190 * Return the quickfix list context (if any) as 'context' in retdict.
5191 */
5192 static int
5193qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5194{
5195 int status;
5196 dictitem_T *di;
5197
5198 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5199 {
5200 di = dictitem_alloc((char_u *)"context");
5201 if (di != NULL)
5202 {
5203 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5204 status = dict_add(retdict, di);
5205 if (status == FAIL)
5206 dictitem_free(di);
5207 }
5208 else
5209 status = FAIL;
5210 }
5211 else
5212 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5213
5214 return status;
5215}
5216
5217/*
5218 * Return the quickfix list index as 'idx' in retdict
5219 */
5220 static int
5221qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5222{
5223 int idx = qi->qf_lists[qf_idx].qf_index;
5224 if (qi->qf_lists[qf_idx].qf_count == 0)
5225 /* For empty lists, qf_index is set to 1 */
5226 idx = 0;
5227 return dict_add_nr_str(retdict, "idx", idx, NULL);
5228}
5229
5230/*
5231 * Return quickfix/location list details (title) as a
5232 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5233 * then current list is used. Otherwise the specified list is used.
5234 */
5235 int
5236qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5237{
5238 qf_info_T *qi = &ql_info;
5239 int status = OK;
5240 int qf_idx;
5241 dictitem_T *di;
5242 int flags = QF_GETLIST_NONE;
5243
5244 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5245 return qf_get_list_from_lines(what, di, retdict);
5246
5247 if (wp != NULL)
5248 qi = GET_LOC_LIST(wp);
5249
5250 flags = qf_getprop_keys2flags(what);
5251
5252 if (qi != NULL && qi->qf_listcount != 0)
5253 qf_idx = qf_getprop_qfidx(qi, what);
5254
Bram Moolenaara6d48492017-12-12 22:45:31 +01005255 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005256 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005257 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005258
Bram Moolenaard823fa92016-08-12 16:29:27 +02005259 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005260 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005261 if ((status == OK) && (flags & QF_GETLIST_NR))
5262 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5263 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005264 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005265 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005266 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005267 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005268 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005269 if ((status == OK) && (flags & QF_GETLIST_ID))
5270 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5271 NULL);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005272 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005273 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005274 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5275 status = dict_add_nr_str(retdict, "size",
5276 qi->qf_lists[qf_idx].qf_count, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005277 if ((status == OK) && (flags & QF_GETLIST_TICK))
5278 status = dict_add_nr_str(retdict, "changedtick",
5279 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5280
Bram Moolenaard823fa92016-08-12 16:29:27 +02005281 return status;
5282}
5283
5284/*
5285 * Add list of entries to quickfix/location list. Each list entry is
5286 * a dictionary with item information.
5287 */
5288 static int
5289qf_add_entries(
5290 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005291 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005292 list_T *list,
5293 char_u *title,
5294 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005295{
5296 listitem_T *li;
5297 dict_T *d;
5298 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005299 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005300 long lnum;
5301 int col, nr;
5302 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005303 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005304 int valid, status;
5305 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005306 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005307
Bram Moolenaara3921f42017-06-04 15:30:34 +02005308 if (action == ' ' || qf_idx == qi->qf_listcount)
5309 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005310 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005311 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005312 qf_idx = qi->qf_curlist;
5313 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005314 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005315 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005316 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005317 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005318 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005319 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005320 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005321 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005322
5323 for (li = list->lv_first; li != NULL; li = li->li_next)
5324 {
5325 if (li->li_tv.v_type != VAR_DICT)
5326 continue; /* Skip non-dict items */
5327
5328 d = li->li_tv.vval.v_dict;
5329 if (d == NULL)
5330 continue;
5331
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005332 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005333 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5334 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5335 col = (int)get_dict_number(d, (char_u *)"col");
5336 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5337 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005338 type = get_dict_string(d, (char_u *)"type", TRUE);
5339 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5340 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005341 if (text == NULL)
5342 text = vim_strsave((char_u *)"");
5343
5344 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005345 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005346 valid = FALSE;
5347
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005348 /* Mark entries with non-existing buffer number as not valid. Give the
5349 * error message only once. */
5350 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5351 {
5352 if (!did_bufnr_emsg)
5353 {
5354 did_bufnr_emsg = TRUE;
5355 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5356 }
5357 valid = FALSE;
5358 bufnum = 0;
5359 }
5360
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005361 /* If the 'valid' field is present it overrules the detected value. */
5362 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5363 valid = (int)get_dict_number(d, (char_u *)"valid");
5364
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005365 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005366 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005367 NULL, /* dir */
5368 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005369 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005370 text,
5371 lnum,
5372 col,
5373 vcol, /* vis_col */
5374 pattern, /* search pattern */
5375 nr,
5376 type == NULL ? NUL : *type,
5377 valid);
5378
5379 vim_free(filename);
5380 vim_free(pattern);
5381 vim_free(text);
5382 vim_free(type);
5383
5384 if (status == FAIL)
5385 {
5386 retval = FAIL;
5387 break;
5388 }
5389 }
5390
Bram Moolenaara3921f42017-06-04 15:30:34 +02005391 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005392 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005393 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005394 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005395 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005396 if (action != 'a')
5397 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005398 qi->qf_lists[qf_idx].qf_ptr =
5399 qi->qf_lists[qf_idx].qf_start;
5400 if (qi->qf_lists[qf_idx].qf_count > 0)
5401 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005402 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005403
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005404 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005405 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005406
5407 return retval;
5408}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005409
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005410/*
5411 * Get the quickfix list index from 'nr' or 'id'
5412 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02005413 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005414qf_setprop_get_qfidx(
5415 qf_info_T *qi,
5416 dict_T *what,
5417 int action,
5418 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005419{
5420 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005421 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005422
Bram Moolenaard823fa92016-08-12 16:29:27 +02005423 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5424 {
5425 /* Use the specified quickfix/location list */
5426 if (di->di_tv.v_type == VAR_NUMBER)
5427 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005428 /* for zero use the current list */
5429 if (di->di_tv.vval.v_number != 0)
5430 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005431
Bram Moolenaar55b69262017-08-13 13:42:01 +02005432 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5433 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005434 /*
5435 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005436 * non-available list and add the new list at the end of the
5437 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005438 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005439 *newlist = TRUE;
5440 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005441 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005442 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005443 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005444 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005445 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005446 }
5447 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005448 && di->di_tv.vval.v_string != NULL
5449 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005450 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005451 if (qi->qf_listcount > 0)
5452 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005453 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02005454 qf_idx = 0;
5455 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005456 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005457 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005458 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005459 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005460 }
5461
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005462 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005463 {
5464 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005465 if (di->di_tv.v_type != VAR_NUMBER)
5466 return INVALID_QFIDX;
5467
5468 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005469 }
5470
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005471 return qf_idx;
5472}
5473
5474/*
5475 * Set the quickfix list title.
5476 */
5477 static int
5478qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
5479{
5480 if (di->di_tv.v_type != VAR_STRING)
5481 return FAIL;
5482
5483 vim_free(qi->qf_lists[qf_idx].qf_title);
5484 qi->qf_lists[qf_idx].qf_title =
5485 get_dict_string(what, (char_u *)"title", TRUE);
5486 if (qf_idx == qi->qf_curlist)
5487 qf_update_win_titlevar(qi);
5488
5489 return OK;
5490}
5491
5492/*
5493 * Set quickfix list items/entries.
5494 */
5495 static int
5496qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
5497{
5498 int retval = FAIL;
5499 char_u *title_save;
5500
5501 if (di->di_tv.v_type != VAR_LIST)
5502 return FAIL;
5503
5504 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5505 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5506 title_save, action == ' ' ? 'a' : action);
5507 if (action == 'r')
5508 {
5509 /*
5510 * When replacing the quickfix list entries using
5511 * qf_add_entries(), the title is set with a ':' prefix.
5512 * Restore the title with the saved title.
5513 */
5514 vim_free(qi->qf_lists[qf_idx].qf_title);
5515 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5516 }
5517 vim_free(title_save);
5518
5519 return retval;
5520}
5521
5522/*
5523 * Set quickfix list items/entries from a list of lines.
5524 */
5525 static int
5526qf_setprop_items_from_lines(
5527 qf_info_T *qi,
5528 int qf_idx,
5529 dict_T *what,
5530 dictitem_T *di,
5531 int action)
5532{
5533 char_u *errorformat = p_efm;
5534 dictitem_T *efm_di;
5535 int retval = FAIL;
5536
5537 /* Use the user supplied errorformat settings (if present) */
5538 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5539 {
5540 if (efm_di->di_tv.v_type != VAR_STRING ||
5541 efm_di->di_tv.vval.v_string == NULL)
5542 return FAIL;
5543 errorformat = efm_di->di_tv.vval.v_string;
5544 }
5545
5546 /* Only a List value is supported */
5547 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
5548 return FAIL;
5549
5550 if (action == 'r')
5551 qf_free_items(qi, qf_idx);
5552 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
5553 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5554 retval = OK;
5555
5556 return retval;
5557}
5558
5559/*
5560 * Set quickfix list context.
5561 */
5562 static int
5563qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
5564{
5565 typval_T *ctx;
5566
5567 free_tv(qi->qf_lists[qf_idx].qf_ctx);
5568 ctx = alloc_tv();
5569 if (ctx != NULL)
5570 copy_tv(&di->di_tv, ctx);
5571 qi->qf_lists[qf_idx].qf_ctx = ctx;
5572
5573 return OK;
5574}
5575
5576/*
5577 * Set quickfix/location list properties (title, items, context).
5578 * Also used to add items from parsing a list of lines.
5579 * Used by the setqflist() and setloclist() VimL functions.
5580 */
5581 static int
5582qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
5583{
5584 dictitem_T *di;
5585 int retval = FAIL;
5586 int qf_idx;
5587 int newlist = FALSE;
5588
5589 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5590 newlist = TRUE;
5591
5592 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
5593 if (qf_idx == INVALID_QFIDX) /* List not found */
5594 return FAIL;
5595
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005596 if (newlist)
5597 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005598 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005599 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005600 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005601 }
5602
5603 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005604 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005605 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005606 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005607 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005608 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005609 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005610 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005611
Bram Moolenaarb254af32017-12-18 19:48:58 +01005612 if (retval == OK)
5613 qf_list_changed(qi, qf_idx);
5614
Bram Moolenaard823fa92016-08-12 16:29:27 +02005615 return retval;
5616}
5617
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005618/*
5619 * Find the non-location list window with the specified location list.
5620 */
5621 static win_T *
5622find_win_with_ll(qf_info_T *qi)
5623{
5624 win_T *wp = NULL;
5625
5626 FOR_ALL_WINDOWS(wp)
5627 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5628 return wp;
5629
5630 return NULL;
5631}
5632
5633/*
5634 * Free the entire quickfix/location list stack.
5635 * If the quickfix/location list window is open, then clear it.
5636 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005637 static void
5638qf_free_stack(win_T *wp, qf_info_T *qi)
5639{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005640 win_T *qfwin = qf_find_win(qi);
5641 win_T *llwin = NULL;
5642 win_T *orig_wp = wp;
5643
5644 if (qfwin != NULL)
5645 {
5646 /* If the quickfix/location list window is open, then clear it */
5647 if (qi->qf_curlist < qi->qf_listcount)
5648 qf_free(qi, qi->qf_curlist);
5649 qf_update_buffer(qi, NULL);
5650 }
5651
5652 if (wp != NULL && IS_LL_WINDOW(wp))
5653 {
5654 /* If in the location list window, then use the non-location list
5655 * window with this location list (if present)
5656 */
5657 llwin = find_win_with_ll(qi);
5658 if (llwin != NULL)
5659 wp = llwin;
5660 }
5661
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005662 qf_free_all(wp);
5663 if (wp == NULL)
5664 {
5665 /* quickfix list */
5666 qi->qf_curlist = 0;
5667 qi->qf_listcount = 0;
5668 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005669 else if (IS_LL_WINDOW(orig_wp))
5670 {
5671 /* If the location list window is open, then create a new empty
5672 * location list */
5673 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005674
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005675 /* first free the list reference in the location list window */
5676 ll_free_all(&orig_wp->w_llist_ref);
5677
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005678 orig_wp->w_llist_ref = new_ll;
5679 if (llwin != NULL)
5680 {
5681 llwin->w_llist = new_ll;
5682 new_ll->qf_refcount++;
5683 }
5684 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005685}
5686
Bram Moolenaard823fa92016-08-12 16:29:27 +02005687/*
5688 * Populate the quickfix list with the items supplied in the list
5689 * of dictionaries. "title" will be copied to w:quickfix_title.
5690 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5691 */
5692 int
5693set_errorlist(
5694 win_T *wp,
5695 list_T *list,
5696 int action,
5697 char_u *title,
5698 dict_T *what)
5699{
5700 qf_info_T *qi = &ql_info;
5701 int retval = OK;
5702
5703 if (wp != NULL)
5704 {
5705 qi = ll_get_or_alloc_list(wp);
5706 if (qi == NULL)
5707 return FAIL;
5708 }
5709
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005710 if (action == 'f')
5711 {
5712 /* Free the entire quickfix or location list stack */
5713 qf_free_stack(wp, qi);
5714 }
5715 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005716 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005717 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005718 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005719 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005720 if (retval == OK)
5721 qf_list_changed(qi, qi->qf_curlist);
5722 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005723
5724 return retval;
5725}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005726
5727 static int
5728mark_quickfix_ctx(qf_info_T *qi, int copyID)
5729{
5730 int i;
5731 int abort = FALSE;
5732 typval_T *ctx;
5733
5734 for (i = 0; i < LISTCOUNT && !abort; ++i)
5735 {
5736 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005737 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5738 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005739 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5740 }
5741
5742 return abort;
5743}
5744
5745/*
5746 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005747 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005748 */
5749 int
5750set_ref_in_quickfix(int copyID)
5751{
5752 int abort = FALSE;
5753 tabpage_T *tp;
5754 win_T *win;
5755
5756 abort = mark_quickfix_ctx(&ql_info, copyID);
5757 if (abort)
5758 return abort;
5759
5760 FOR_ALL_TAB_WINDOWS(tp, win)
5761 {
5762 if (win->w_llist != NULL)
5763 {
5764 abort = mark_quickfix_ctx(win->w_llist, copyID);
5765 if (abort)
5766 return abort;
5767 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005768 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5769 {
5770 /* In a location list window and none of the other windows is
5771 * referring to this location list. Mark the location list
5772 * context as still in use.
5773 */
5774 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5775 if (abort)
5776 return abort;
5777 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005778 }
5779
5780 return abort;
5781}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005782#endif
5783
Bram Moolenaar81695252004-12-29 20:58:21 +00005784/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005785 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005786 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005787 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005788 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005789 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005790 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005791 */
5792 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005793ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005794{
5795 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005796 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005797 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005798 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005799
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005800 switch (eap->cmdidx)
5801 {
5802 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5803 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5804 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5805 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5806 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5807 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5808 default: break;
5809 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005810 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5811 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005812 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005813#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005814 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005815 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005816#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005817 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005818
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005819 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005820 if (eap->cmdidx == CMD_lbuffer
5821 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005822 || eap->cmdidx == CMD_laddbuffer)
5823 {
5824 qi = ll_get_or_alloc_list(curwin);
5825 if (qi == NULL)
5826 return;
5827 }
5828
Bram Moolenaar86b68352004-12-27 21:59:20 +00005829 if (*eap->arg == NUL)
5830 buf = curbuf;
5831 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5832 buf = buflist_findnr(atoi((char *)eap->arg));
5833 if (buf == NULL)
5834 EMSG(_(e_invarg));
5835 else if (buf->b_ml.ml_mfp == NULL)
5836 EMSG(_("E681: Buffer is not loaded"));
5837 else
5838 {
5839 if (eap->addr_count == 0)
5840 {
5841 eap->line1 = 1;
5842 eap->line2 = buf->b_ml.ml_line_count;
5843 }
5844 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5845 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5846 EMSG(_(e_invrange));
5847 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005848 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005849 char_u *qf_title = *eap->cmdlinep;
5850
5851 if (buf->b_sfname)
5852 {
5853 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5854 (char *)qf_title, (char *)buf->b_sfname);
5855 qf_title = IObuff;
5856 }
5857
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005858 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005859 (eap->cmdidx != CMD_caddbuffer
5860 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005861 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005862 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005863 if (res >= 0)
5864 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005865 if (au_name != NULL)
5866 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5867 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005868 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5869 eap->cmdidx == CMD_lbuffer))
5870 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005871 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005872 }
5873}
5874
Bram Moolenaar1e015462005-09-25 22:16:38 +00005875#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005876/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005877 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5878 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005879 */
5880 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005881ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005882{
5883 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005884 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005885 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005886 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005887
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005888 switch (eap->cmdidx)
5889 {
5890 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5891 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5892 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5893 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5894 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5895 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5896 default: break;
5897 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005898 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5899 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005900 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005901#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005902 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005903 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005904#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005905 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005906
Bram Moolenaar3c097222017-12-21 20:54:49 +01005907 if (eap->cmdidx == CMD_lexpr
5908 || eap->cmdidx == CMD_lgetexpr
5909 || eap->cmdidx == CMD_laddexpr)
5910 {
5911 qi = ll_get_or_alloc_list(curwin);
5912 if (qi == NULL)
5913 return;
5914 }
5915
Bram Moolenaar4770d092006-01-12 23:22:24 +00005916 /* Evaluate the expression. When the result is a string or a list we can
5917 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005918 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005919 if (tv != NULL)
5920 {
5921 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5922 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5923 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005924 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005925 (eap->cmdidx != CMD_caddexpr
5926 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005927 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005928 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005929 if (res >= 0)
5930 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005931 if (au_name != NULL)
5932 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5933 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005934 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5935 eap->cmdidx == CMD_lexpr))
5936 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005937 }
5938 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005939 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005940 free_tv(tv);
5941 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005942}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005943#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005944
5945/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02005946 * Get the location list for ":lhelpgrep"
5947 */
5948 static qf_info_T *
5949hgr_get_ll(int *new_ll)
5950{
5951 win_T *wp;
5952 qf_info_T *qi;
5953
5954 /* If the current window is a help window, then use it */
5955 if (bt_help(curwin->w_buffer))
5956 wp = curwin;
5957 else
5958 /* Find an existing help window */
5959 FOR_ALL_WINDOWS(wp)
5960 if (bt_help(wp->w_buffer))
5961 break;
5962
5963 if (wp == NULL) /* Help window not found */
5964 qi = NULL;
5965 else
5966 qi = wp->w_llist;
5967
5968 if (qi == NULL)
5969 {
5970 /* Allocate a new location list for help text matches */
5971 if ((qi = ll_new_list()) == NULL)
5972 return NULL;
5973 *new_ll = TRUE;
5974 }
5975
5976 return qi;
5977}
5978
5979/*
5980 * Search for a pattern in a help file.
5981 */
5982 static void
5983hgr_search_file(
5984 qf_info_T *qi,
5985 char_u *fname,
5986#ifdef FEAT_MBYTE
5987 vimconv_T *p_vc,
5988#endif
5989 regmatch_T *p_regmatch)
5990{
5991 FILE *fd;
5992 long lnum;
5993
5994 fd = mch_fopen((char *)fname, "r");
5995 if (fd == NULL)
5996 return;
5997
5998 lnum = 1;
5999 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6000 {
6001 char_u *line = IObuff;
6002#ifdef FEAT_MBYTE
6003 /* Convert a line if 'encoding' is not utf-8 and
6004 * the line contains a non-ASCII character. */
6005 if (p_vc->vc_type != CONV_NONE
6006 && has_non_ascii(IObuff))
6007 {
6008 line = string_convert(p_vc, IObuff, NULL);
6009 if (line == NULL)
6010 line = IObuff;
6011 }
6012#endif
6013
6014 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6015 {
6016 int l = (int)STRLEN(line);
6017
6018 /* remove trailing CR, LF, spaces, etc. */
6019 while (l > 0 && line[l - 1] <= ' ')
6020 line[--l] = NUL;
6021
6022 if (qf_add_entry(qi,
6023 qi->qf_curlist,
6024 NULL, /* dir */
6025 fname,
6026 0,
6027 line,
6028 lnum,
6029 (int)(p_regmatch->startp[0] - line)
6030 + 1, /* col */
6031 FALSE, /* vis_col */
6032 NULL, /* search pattern */
6033 0, /* nr */
6034 1, /* type */
6035 TRUE /* valid */
6036 ) == FAIL)
6037 {
6038 got_int = TRUE;
6039#ifdef FEAT_MBYTE
6040 if (line != IObuff)
6041 vim_free(line);
6042#endif
6043 break;
6044 }
6045 }
6046#ifdef FEAT_MBYTE
6047 if (line != IObuff)
6048 vim_free(line);
6049#endif
6050 ++lnum;
6051 line_breakcheck();
6052 }
6053 fclose(fd);
6054}
6055
6056/*
6057 * Search for a pattern in all the help files in the doc directory under
6058 * the given directory.
6059 */
6060 static void
6061hgr_search_files_in_dir(
6062 qf_info_T *qi,
6063 char_u *dirname,
6064 regmatch_T *p_regmatch
6065#ifdef FEAT_MBYTE
6066 , vimconv_T *p_vc
6067#endif
6068#ifdef FEAT_MULTI_LANG
6069 , char_u *lang
6070#endif
6071 )
6072{
6073 int fcount;
6074 char_u **fnames;
6075 int fi;
6076
6077 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6078 add_pathsep(dirname);
6079 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6080 if (gen_expand_wildcards(1, &dirname, &fcount,
6081 &fnames, EW_FILE|EW_SILENT) == OK
6082 && fcount > 0)
6083 {
6084 for (fi = 0; fi < fcount && !got_int; ++fi)
6085 {
6086#ifdef FEAT_MULTI_LANG
6087 /* Skip files for a different language. */
6088 if (lang != NULL
6089 && STRNICMP(lang, fnames[fi]
6090 + STRLEN(fnames[fi]) - 3, 2) != 0
6091 && !(STRNICMP(lang, "en", 2) == 0
6092 && STRNICMP("txt", fnames[fi]
6093 + STRLEN(fnames[fi]) - 3, 3) == 0))
6094 continue;
6095#endif
6096
6097 hgr_search_file(qi, fnames[fi],
6098#ifdef FEAT_MBYTE
6099 p_vc,
6100#endif
6101 p_regmatch);
6102 }
6103 FreeWild(fcount, fnames);
6104 }
6105}
6106
6107/*
6108 * Search for a pattern in all the help files in the 'runtimepath'.
6109 */
6110 static void
6111hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6112{
6113 char_u *p;
6114#ifdef FEAT_MULTI_LANG
6115 char_u *lang;
6116#endif
6117
6118#ifdef FEAT_MBYTE
6119 vimconv_T vc;
6120
6121 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6122 * differs. */
6123 vc.vc_type = CONV_NONE;
6124 if (!enc_utf8)
6125 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6126#endif
6127
6128#ifdef FEAT_MULTI_LANG
6129 /* Check for a specified language */
6130 lang = check_help_lang(arg);
6131#endif
6132
6133 /* Go through all directories in 'runtimepath' */
6134 p = p_rtp;
6135 while (*p != NUL && !got_int)
6136 {
6137 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6138
6139 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6140#ifdef FEAT_MBYTE
6141 , &vc
6142#endif
6143#ifdef FEAT_MULTI_LANG
6144 , lang
6145#endif
6146 );
6147 }
6148
6149#ifdef FEAT_MBYTE
6150 if (vc.vc_type != CONV_NONE)
6151 convert_setup(&vc, NULL, NULL);
6152#endif
6153}
6154
6155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 * ":helpgrep {pattern}"
6157 */
6158 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006159ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
6161 regmatch_T regmatch;
6162 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006163 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006164 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006165 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166
Bram Moolenaar73633f82012-01-20 13:39:07 +01006167 switch (eap->cmdidx)
6168 {
6169 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6170 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6171 default: break;
6172 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006173 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6174 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006175 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006176#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006177 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006178 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006179#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006180 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006181
6182 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6183 save_cpo = p_cpo;
6184 p_cpo = empty_option;
6185
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006186 if (eap->cmdidx == CMD_lhelpgrep)
6187 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006188 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006189 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006190 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006191 }
6192
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6194 regmatch.rm_ic = FALSE;
6195 if (regmatch.regprog != NULL)
6196 {
6197 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006198 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006200 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006201
Bram Moolenaar473de612013-06-08 18:19:48 +02006202 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006204 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6205 qi->qf_lists[qi->qf_curlist].qf_ptr =
6206 qi->qf_lists[qi->qf_curlist].qf_start;
6207 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 }
6209
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006210 if (p_cpo == empty_option)
6211 p_cpo = save_cpo;
6212 else
6213 /* Darn, some plugin changed the value. */
6214 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaarb254af32017-12-18 19:48:58 +01006216 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006217 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218
Bram Moolenaar73633f82012-01-20 13:39:07 +01006219 if (au_name != NULL)
6220 {
6221 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6222 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006223 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006224 /* autocommands made "qi" invalid */
6225 return;
6226 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006227
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006229 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006230 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006231 else
6232 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006233
6234 if (eap->cmdidx == CMD_lhelpgrep)
6235 {
6236 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006237 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006238 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006239 {
6240 if (new_qi)
6241 ll_free_all(&qi);
6242 }
6243 else if (curwin->w_llist == NULL)
6244 curwin->w_llist = qi;
6245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246}
6247
6248#endif /* FEAT_QUICKFIX */