blob: dde68419a2fba5d5f02c1545738ffeaa99fafbdc [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 Moolenaara796d462018-05-01 14:30:36 +02002739 /* Shorten all the file names, so that it is easy to read */
2740 shorten_fnames(FALSE);
2741
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002742 /*
2743 * Get the attributes for the different quickfix highlight items. Note
2744 * that this depends on syntax items defined in the qf.vim syntax file
2745 */
2746 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2747 if (qfFileAttr == 0)
2748 qfFileAttr = HL_ATTR(HLF_D);
2749 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2750 if (qfSepAttr == 0)
2751 qfSepAttr = HL_ATTR(HLF_D);
2752 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2753 if (qfLineAttr == 0)
2754 qfLineAttr = HL_ATTR(HLF_N);
2755
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002756 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002758 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2759 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
2761 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2762 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002763 msg_putchar('\n');
2764 if (got_int)
2765 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002766
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002767 fname = NULL;
2768 if (qfp->qf_fnum != 0
2769 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2770 {
2771 fname = buf->b_fname;
2772 if (qfp->qf_type == 1) /* :helpgrep */
2773 fname = gettail(fname);
2774 }
2775 if (fname == NULL)
2776 sprintf((char *)IObuff, "%2d", i);
2777 else
2778 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2779 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002780 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002781 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2782
2783 if (qfp->qf_lnum != 0)
2784 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002785 if (qfp->qf_lnum == 0)
2786 IObuff[0] = NUL;
2787 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002788 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002789 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002790 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002791 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002792 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002793 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002794 msg_puts_attr(IObuff, qfLineAttr);
2795 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002796 if (qfp->qf_pattern != NULL)
2797 {
2798 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002800 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002801 }
2802 msg_puts((char_u *)" ");
2803
2804 /* Remove newlines and leading whitespace from the text. For an
2805 * unrecognized line keep the indent, the compiler may mark a word
2806 * with ^^^^. */
2807 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2809 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002810 msg_prt_line(IObuff, FALSE);
2811 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813
2814 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002815 if (qfp == NULL)
2816 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 ui_breakcheck();
2819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820}
2821
2822/*
2823 * Remove newlines and leading whitespace from an error message.
2824 * Put the result in "buf[bufsize]".
2825 */
2826 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002827qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
2829 int i;
2830 char_u *p = text;
2831
2832 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2833 {
2834 if (*p == '\n')
2835 {
2836 buf[i] = ' ';
2837 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002838 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 break;
2840 }
2841 else
2842 buf[i] = *p++;
2843 }
2844 buf[i] = NUL;
2845}
2846
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002847 static void
2848qf_msg(qf_info_T *qi, int which, char *lead)
2849{
2850 char *title = (char *)qi->qf_lists[which].qf_title;
2851 int count = qi->qf_lists[which].qf_count;
2852 char_u buf[IOSIZE];
2853
2854 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2855 lead,
2856 which + 1,
2857 qi->qf_listcount,
2858 count);
2859
2860 if (title != NULL)
2861 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002862 size_t len = STRLEN(buf);
2863
2864 if (len < 34)
2865 {
2866 vim_memset(buf + len, ' ', 34 - len);
2867 buf[34] = NUL;
2868 }
2869 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002870 }
2871 trunc_string(buf, buf, Columns - 1, IOSIZE);
2872 msg(buf);
2873}
2874
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875/*
2876 * ":colder [count]": Up in the quickfix stack.
2877 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002878 * ":lolder [count]": Up in the location list stack.
2879 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 */
2881 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002882qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002884 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 int count;
2886
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002887 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2888 {
2889 qi = GET_LOC_LIST(curwin);
2890 if (qi == NULL)
2891 {
2892 EMSG(_(e_loclist));
2893 return;
2894 }
2895 }
2896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 if (eap->addr_count != 0)
2898 count = eap->line2;
2899 else
2900 count = 1;
2901 while (count--)
2902 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002903 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002905 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
2907 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002908 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002910 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 }
2912 else
2913 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002914 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 {
2916 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002917 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
2921 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002922 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002923 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924}
2925
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002926 void
2927qf_history(exarg_T *eap)
2928{
2929 qf_info_T *qi = &ql_info;
2930 int i;
2931
2932 if (eap->cmdidx == CMD_lhistory)
2933 qi = GET_LOC_LIST(curwin);
2934 if (qi == NULL || (qi->qf_listcount == 0
2935 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2936 MSG(_("No entries"));
2937 else
2938 for (i = 0; i < qi->qf_listcount; ++i)
2939 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2940}
2941
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002943 * Free all the entries in the error list "idx". Note that other information
2944 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 */
2946 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002947qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002949 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002950 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002951 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002952 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002954 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002956 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002957 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002958 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002959 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002960 vim_free(qfp->qf_text);
2961 stop = (qfp == qfpnext);
2962 vim_free(qfp->qf_pattern);
2963 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002964 if (stop)
2965 /* Somehow qf_count may have an incorrect value, set it to 1
2966 * to avoid crashing when it's wrong.
2967 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002968 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002969 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002970 qfl->qf_start = qfpnext;
2971 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002973
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002974 qfl->qf_index = 0;
2975 qfl->qf_start = NULL;
2976 qfl->qf_last = NULL;
2977 qfl->qf_ptr = NULL;
2978 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002979
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002980 qf_clean_dir_stack(&qfl->qf_dir_stack);
2981 qfl->qf_directory = NULL;
2982 qf_clean_dir_stack(&qfl->qf_file_stack);
2983 qfl->qf_currfile = NULL;
2984 qfl->qf_multiline = FALSE;
2985 qfl->qf_multiignore = FALSE;
2986 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987}
2988
2989/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002990 * Free error list "idx". Frees all the entries in the quickfix list,
2991 * associated context information and the title.
2992 */
2993 static void
2994qf_free(qf_info_T *qi, int idx)
2995{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002996 qf_list_T *qfl = &qi->qf_lists[idx];
2997
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002998 qf_free_items(qi, idx);
2999
Bram Moolenaard23a8232018-02-10 18:45:26 +01003000 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003001 free_tv(qfl->qf_ctx);
3002 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003003 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003004 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003005}
3006
3007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 * qf_mark_adjust: adjust marks
3009 */
3010 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003011qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003012 win_T *wp,
3013 linenr_T line1,
3014 linenr_T line2,
3015 long amount,
3016 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003018 int i;
3019 qfline_T *qfp;
3020 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003021 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003022 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003023 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
Bram Moolenaarc1542742016-07-20 21:44:37 +02003025 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003026 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003027 if (wp != NULL)
3028 {
3029 if (wp->w_llist == NULL)
3030 return;
3031 qi = wp->w_llist;
3032 }
3033
3034 for (idx = 0; idx < qi->qf_listcount; ++idx)
3035 if (qi->qf_lists[idx].qf_count)
3036 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003037 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3038 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (qfp->qf_fnum == curbuf->b_fnum)
3040 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003041 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3043 {
3044 if (amount == MAXLNUM)
3045 qfp->qf_cleared = TRUE;
3046 else
3047 qfp->qf_lnum += amount;
3048 }
3049 else if (amount_after && qfp->qf_lnum > line2)
3050 qfp->qf_lnum += amount_after;
3051 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003052
3053 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003054 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055}
3056
3057/*
3058 * Make a nice message out of the error character and the error number:
3059 * char number message
3060 * e or E 0 " error"
3061 * w or W 0 " warning"
3062 * i or I 0 " info"
3063 * 0 0 ""
3064 * other 0 " c"
3065 * e or E n " error n"
3066 * w or W n " warning n"
3067 * i or I n " info n"
3068 * 0 n " error n"
3069 * other n " c n"
3070 * 1 x "" :helpgrep
3071 */
3072 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003073qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074{
3075 static char_u buf[20];
3076 static char_u cc[3];
3077 char_u *p;
3078
3079 if (c == 'W' || c == 'w')
3080 p = (char_u *)" warning";
3081 else if (c == 'I' || c == 'i')
3082 p = (char_u *)" info";
3083 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3084 p = (char_u *)" error";
3085 else if (c == 0 || c == 1)
3086 p = (char_u *)"";
3087 else
3088 {
3089 cc[0] = ' ';
3090 cc[1] = c;
3091 cc[2] = NUL;
3092 p = cc;
3093 }
3094
3095 if (nr <= 0)
3096 return p;
3097
3098 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3099 return buf;
3100}
3101
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102/*
3103 * ":cwindow": open the quickfix window if we have errors to display,
3104 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003105 * ":lwindow": open the location list window if we have locations to display,
3106 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 */
3108 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003109ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003111 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 win_T *win;
3113
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003114 if (eap->cmdidx == CMD_lwindow)
3115 {
3116 qi = GET_LOC_LIST(curwin);
3117 if (qi == NULL)
3118 return;
3119 }
3120
3121 /* Look for an existing quickfix window. */
3122 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124 /*
3125 * If a quickfix window is open but we have no errors to display,
3126 * close the window. If a quickfix window is not open, then open
3127 * it if we have errors; otherwise, leave it closed.
3128 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003129 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003130 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003131 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
3133 if (win != NULL)
3134 ex_cclose(eap);
3135 }
3136 else if (win == NULL)
3137 ex_copen(eap);
3138}
3139
3140/*
3141 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003142 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003145ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003147 win_T *win = NULL;
3148 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003150 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3151 {
3152 qi = GET_LOC_LIST(curwin);
3153 if (qi == NULL)
3154 return;
3155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003157 /* Find existing quickfix window and close it. */
3158 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 if (win != NULL)
3160 win_close(win, FALSE);
3161}
3162
3163/*
3164 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003165 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 */
3167 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003168ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003170 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003173 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003174 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003175 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003177 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3178 {
3179 qi = GET_LOC_LIST(curwin);
3180 if (qi == NULL)
3181 {
3182 EMSG(_(e_loclist));
3183 return;
3184 }
3185 }
3186
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (eap->addr_count != 0)
3188 height = eap->line2;
3189 else
3190 height = QF_WINHEIGHT;
3191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#ifdef FEAT_GUI
3194 need_mouse_correct = TRUE;
3195#endif
3196
3197 /*
3198 * Find existing quickfix window, or open a new one.
3199 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003200 win = qf_find_win(qi);
3201
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003202 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003205 if (eap->addr_count != 0)
3206 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003207 if (cmdmod.split & WSP_VERT)
3208 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003209 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003210 win_setwidth(height);
3211 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003212 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003213 win_setheight(height);
3214 }
3215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 else
3217 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003218 int flags = 0;
3219
Bram Moolenaar9c102382006-05-03 21:26:49 +00003220 qf_buf = qf_find_buf(qi);
3221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 /* The current window becomes the previous window afterwards. */
3223 win = curwin;
3224
Bram Moolenaar77642c02012-11-20 17:55:10 +01003225 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3226 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003227 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003228 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003229 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003230 /* Default is to open the window below the current window */
3231 if (cmdmod.split == 0)
3232 flags = WSP_BELOW;
3233 flags |= WSP_NEWLOC;
3234 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003236 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003238 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003240 /*
3241 * For the location list window, create a reference to the
3242 * location list from the window 'win'.
3243 */
3244 curwin->w_llist_ref = win->w_llist;
3245 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003247
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003248 if (oldwin != curwin)
3249 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003250 if (qf_buf != NULL)
3251 /* Use the existing quickfix buffer */
3252 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003253 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003254 else
3255 {
3256 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003257 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003258 /* switch off 'swapfile' */
3259 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3260 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003261 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003262 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003263 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003264#ifdef FEAT_DIFF
3265 curwin->w_p_diff = FALSE;
3266#endif
3267#ifdef FEAT_FOLDING
3268 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3269 OPT_LOCAL);
3270#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003273 /* Only set the height when still in the same tab page and there is no
3274 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003275 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 win_setheight(height);
3277 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3278 if (win_valid(win))
3279 prevwin = win;
3280 }
3281
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003282 qf_set_title_var(qi);
3283
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 /*
3285 * Fill the buffer with the quickfix list.
3286 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003287 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003289 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 curwin->w_cursor.col = 0;
3291 check_cursor();
3292 update_topline(); /* scroll to show the line */
3293}
3294
3295/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003296 * Move the cursor in the quickfix window to "lnum".
3297 */
3298 static void
3299qf_win_goto(win_T *win, linenr_T lnum)
3300{
3301 win_T *old_curwin = curwin;
3302
3303 curwin = win;
3304 curbuf = win->w_buffer;
3305 curwin->w_cursor.lnum = lnum;
3306 curwin->w_cursor.col = 0;
3307#ifdef FEAT_VIRTUALEDIT
3308 curwin->w_cursor.coladd = 0;
3309#endif
3310 curwin->w_curswant = 0;
3311 update_topline(); /* scroll to show the line */
3312 redraw_later(VALID);
3313 curwin->w_redr_status = TRUE; /* update ruler */
3314 curwin = old_curwin;
3315 curbuf = curwin->w_buffer;
3316}
3317
3318/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003319 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003320 */
3321 void
3322ex_cbottom(exarg_T *eap UNUSED)
3323{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003324 qf_info_T *qi = &ql_info;
3325 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003326
Bram Moolenaar537ef082016-07-09 17:56:19 +02003327 if (eap->cmdidx == CMD_lbottom)
3328 {
3329 qi = GET_LOC_LIST(curwin);
3330 if (qi == NULL)
3331 {
3332 EMSG(_(e_loclist));
3333 return;
3334 }
3335 }
3336
3337 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003338 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3339 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3340}
3341
3342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 * Return the number of the current entry (line number in the quickfix
3344 * window).
3345 */
3346 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003347qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003349 qf_info_T *qi = &ql_info;
3350
3351 if (IS_LL_WINDOW(wp))
3352 /* In the location list window, use the referenced location list */
3353 qi = wp->w_llist_ref;
3354
3355 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356}
3357
3358/*
3359 * Update the cursor position in the quickfix window to the current error.
3360 * Return TRUE if there is a quickfix window.
3361 */
3362 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003363qf_win_pos_update(
3364 qf_info_T *qi,
3365 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
3367 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003368 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369
3370 /*
3371 * Put the cursor on the current error in the quickfix window, so that
3372 * it's viewable.
3373 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003374 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (win != NULL
3376 && qf_index <= win->w_buffer->b_ml.ml_line_count
3377 && old_qf_index != qf_index)
3378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 if (qf_index > old_qf_index)
3380 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003381 win->w_redraw_top = old_qf_index;
3382 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 else
3385 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003386 win->w_redraw_top = qf_index;
3387 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003389 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
3391 return win != NULL;
3392}
3393
3394/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003395 * Check whether the given window is displaying the specified quickfix/location
3396 * list buffer
3397 */
3398 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003399is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003400{
3401 /*
3402 * A window displaying the quickfix buffer will have the w_llist_ref field
3403 * set to NULL.
3404 * A window displaying a location list buffer will have the w_llist_ref
3405 * pointing to the location list.
3406 */
3407 if (bt_quickfix(win->w_buffer))
3408 if ((qi == &ql_info && win->w_llist_ref == NULL)
3409 || (qi != &ql_info && win->w_llist_ref == qi))
3410 return TRUE;
3411
3412 return FALSE;
3413}
3414
3415/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003416 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003417 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003418 */
3419 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003420qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003421{
3422 win_T *win;
3423
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003424 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003425 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003426 return win;
3427 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003428}
3429
3430/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003431 * Find a quickfix buffer.
3432 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 */
3434 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003435qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003437 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003438 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
Bram Moolenaar9c102382006-05-03 21:26:49 +00003440 FOR_ALL_TAB_WINDOWS(tp, win)
3441 if (is_qf_win(win, qi))
3442 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003443
Bram Moolenaar9c102382006-05-03 21:26:49 +00003444 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445}
3446
3447/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003448 * Update the w:quickfix_title variable in the quickfix/location list window
3449 */
3450 static void
3451qf_update_win_titlevar(qf_info_T *qi)
3452{
3453 win_T *win;
3454 win_T *curwin_save;
3455
3456 if ((win = qf_find_win(qi)) != NULL)
3457 {
3458 curwin_save = curwin;
3459 curwin = win;
3460 qf_set_title_var(qi);
3461 curwin = curwin_save;
3462 }
3463}
3464
3465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 * Find the quickfix buffer. If it exists, update the contents.
3467 */
3468 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003469qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
3471 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003472 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003476 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (buf != NULL)
3478 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003479 linenr_T old_line_count = buf->b_ml.ml_line_count;
3480
3481 if (old_last == NULL)
3482 /* set curwin/curbuf to buf and save a few things */
3483 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaard823fa92016-08-12 16:29:27 +02003485 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003486
Bram Moolenaar864293a2016-06-02 13:40:04 +02003487 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003488 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003489
Bram Moolenaar864293a2016-06-02 13:40:04 +02003490 if (old_last == NULL)
3491 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003492 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003493
3494 /* restore curwin/curbuf and a few other things */
3495 aucmd_restbuf(&aco);
3496 }
3497
3498 /* Only redraw when added lines are visible. This avoids flickering
3499 * when the added lines are not visible. */
3500 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3501 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503}
3504
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003505/*
3506 * Set "w:quickfix_title" if "qi" has a title.
3507 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003508 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003509qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003510{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003511 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3512 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003513 qi->qf_lists[qi->qf_curlist].qf_title);
3514}
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516/*
3517 * Fill current buffer with quickfix errors, replacing any previous contents.
3518 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003519 * If "old_last" is not NULL append the items after this one.
3520 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3521 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 */
3523 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003524qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003526 linenr_T lnum;
3527 qfline_T *qfp;
3528 buf_T *errbuf;
3529 int len;
3530 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
Bram Moolenaar864293a2016-06-02 13:40:04 +02003532 if (old_last == NULL)
3533 {
3534 if (buf != curbuf)
3535 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003536 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003537 return;
3538 }
3539
3540 /* delete all existing lines */
3541 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3542 (void)ml_delete((linenr_T)1, FALSE);
3543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
3545 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003546 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003548 char_u dirname[MAXPATHL];
3549
3550 *dirname = NUL;
3551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003553 if (old_last == NULL)
3554 {
3555 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3556 lnum = 0;
3557 }
3558 else
3559 {
3560 qfp = old_last->qf_next;
3561 lnum = buf->b_ml.ml_line_count;
3562 }
3563 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
3565 if (qfp->qf_fnum != 0
3566 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3567 && errbuf->b_fname != NULL)
3568 {
3569 if (qfp->qf_type == 1) /* :helpgrep */
3570 STRCPY(IObuff, gettail(errbuf->b_fname));
3571 else
Bram Moolenaara796d462018-05-01 14:30:36 +02003572 {
3573 /* shorten the file name if not done already */
3574 if (errbuf->b_sfname == NULL
3575 || mch_isFullName(errbuf->b_sfname))
3576 {
3577 if (*dirname == NUL)
3578 mch_dirname(dirname, MAXPATHL);
3579 shorten_buf_fname(errbuf, dirname, FALSE);
3580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 STRCPY(IObuff, errbuf->b_fname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 len = (int)STRLEN(IObuff);
3584 }
3585 else
3586 len = 0;
3587 IObuff[len++] = '|';
3588
3589 if (qfp->qf_lnum > 0)
3590 {
3591 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3592 len += (int)STRLEN(IObuff + len);
3593
3594 if (qfp->qf_col > 0)
3595 {
3596 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3597 len += (int)STRLEN(IObuff + len);
3598 }
3599
3600 sprintf((char *)IObuff + len, "%s",
3601 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3602 len += (int)STRLEN(IObuff + len);
3603 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003604 else if (qfp->qf_pattern != NULL)
3605 {
3606 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3607 len += (int)STRLEN(IObuff + len);
3608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 IObuff[len++] = '|';
3610 IObuff[len++] = ' ';
3611
3612 /* Remove newlines and leading whitespace from the text.
3613 * For an unrecognized line keep the indent, the compiler may
3614 * mark a word with ^^^^. */
3615 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3616 IObuff + len, IOSIZE - len);
3617
Bram Moolenaar864293a2016-06-02 13:40:04 +02003618 if (ml_append_buf(buf, lnum, IObuff,
3619 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003621 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003623 if (qfp == NULL)
3624 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003626
3627 if (old_last == NULL)
3628 /* Delete the empty line which is now at the end */
3629 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631
3632 /* correct cursor position */
3633 check_lnums(TRUE);
3634
Bram Moolenaar864293a2016-06-02 13:40:04 +02003635 if (old_last == NULL)
3636 {
3637 /* Set the 'filetype' to "qf" each time after filling the buffer.
3638 * This resembles reading a file into a buffer, it's more logical when
3639 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003640 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003641 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3642 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar864293a2016-06-02 13:40:04 +02003644 keep_filetype = TRUE; /* don't detect 'filetype' */
3645 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003647 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003649 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003650 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003651
Bram Moolenaar864293a2016-06-02 13:40:04 +02003652 /* make sure it will be redrawn */
3653 redraw_curbuf_later(NOT_VALID);
3654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 /* Restore KeyTyped, setting 'filetype' may reset it. */
3657 KeyTyped = old_KeyTyped;
3658}
3659
Bram Moolenaarb254af32017-12-18 19:48:58 +01003660 static void
3661qf_list_changed(qf_info_T *qi, int qf_idx)
3662{
3663 qi->qf_lists[qf_idx].qf_changedtick++;
3664}
3665
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003667 * Return TRUE when using ":vimgrep" for ":grep".
3668 */
3669 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003670grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003671{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003672 return ((cmdidx == CMD_grep
3673 || cmdidx == CMD_lgrep
3674 || cmdidx == CMD_grepadd
3675 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003676 && STRCMP("internal",
3677 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3678}
3679
3680/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003681 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 */
3683 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003684ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003686 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003688 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003690 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003691 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003692 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003693 char_u *au_name = NULL;
3694
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003695 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3696 if (grep_internal(eap->cmdidx))
3697 {
3698 ex_vimgrep(eap);
3699 return;
3700 }
3701
Bram Moolenaar7c626922005-02-07 22:01:03 +00003702 switch (eap->cmdidx)
3703 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003704 case CMD_make: au_name = (char_u *)"make"; break;
3705 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3706 case CMD_grep: au_name = (char_u *)"grep"; break;
3707 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3708 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3709 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003710 default: break;
3711 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003712 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3713 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003714 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003715#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003716 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003717 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003718#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003719 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003720#ifdef FEAT_MBYTE
3721 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
Bram Moolenaara6557602006-02-04 22:43:20 +00003724 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3725 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003726 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003727
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003729 fname = get_mef_name();
3730 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003732 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
3734 /*
3735 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3736 */
3737 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3738 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003739 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 cmd = alloc(len);
3741 if (cmd == NULL)
3742 return;
3743 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3744 (char *)p_shq);
3745 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003746 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 /*
3748 * Output a newline if there's something else than the :make command that
3749 * was typed (in which case the cursor is in column 0).
3750 */
3751 if (msg_col == 0)
3752 msg_didout = FALSE;
3753 msg_start();
3754 MSG_PUTS(":!");
3755 msg_outtrans(cmd); /* show what we are doing */
3756
3757 /* let the shell know if we are redirecting output or not */
3758 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3759
3760#ifdef AMIGA
3761 out_flush();
3762 /* read window status report and redraw before message */
3763 (void)char_avail();
3764#endif
3765
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003766 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003767 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3768 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003769 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003770 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003771 if (wp != NULL)
3772 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003773 if (res >= 0 && qi != NULL)
3774 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003775 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003776 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003777 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3778 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01003779 if (qi != NULL && qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003780 res = qi->qf_lists[qi->qf_curlist].qf_count;
3781 else
3782 res = 0;
3783 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003784 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003785 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
Bram Moolenaar7c626922005-02-07 22:01:03 +00003787 mch_remove(fname);
3788 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 vim_free(cmd);
3790}
3791
3792/*
3793 * Return the name for the errorfile, in allocated memory.
3794 * Find a new unique name when 'makeef' contains "##".
3795 * Returns NULL for error.
3796 */
3797 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003798get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
3800 char_u *p;
3801 char_u *name;
3802 static int start = -1;
3803 static int off = 0;
3804#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003805 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806#endif
3807
3808 if (*p_mef == NUL)
3809 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003810 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (name == NULL)
3812 EMSG(_(e_notmp));
3813 return name;
3814 }
3815
3816 for (p = p_mef; *p; ++p)
3817 if (p[0] == '#' && p[1] == '#')
3818 break;
3819
3820 if (*p == NUL)
3821 return vim_strsave(p_mef);
3822
3823 /* Keep trying until the name doesn't exist yet. */
3824 for (;;)
3825 {
3826 if (start == -1)
3827 start = mch_get_pid();
3828 else
3829 off += 19;
3830
3831 name = alloc((unsigned)STRLEN(p_mef) + 30);
3832 if (name == NULL)
3833 break;
3834 STRCPY(name, p_mef);
3835 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3836 STRCAT(name, p + 2);
3837 if (mch_getperm(name) < 0
3838#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003839 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 && mch_lstat((char *)name, &sb) < 0
3841#endif
3842 )
3843 break;
3844 vim_free(name);
3845 }
3846 return name;
3847}
3848
3849/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003850 * Returns the number of valid entries in the current quickfix/location list.
3851 */
3852 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003853qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003854{
3855 qf_info_T *qi = &ql_info;
3856 qfline_T *qfp;
3857 int i, sz = 0;
3858 int prev_fnum = 0;
3859
3860 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3861 {
3862 /* Location list */
3863 qi = GET_LOC_LIST(curwin);
3864 if (qi == NULL)
3865 return 0;
3866 }
3867
3868 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003869 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003870 ++i, qfp = qfp->qf_next)
3871 {
3872 if (qfp->qf_valid)
3873 {
3874 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3875 sz++; /* Count all valid entries */
3876 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3877 {
3878 /* Count the number of files */
3879 sz++;
3880 prev_fnum = qfp->qf_fnum;
3881 }
3882 }
3883 }
3884
3885 return sz;
3886}
3887
3888/*
3889 * Returns the current index of the quickfix/location list.
3890 * Returns 0 if there is an error.
3891 */
3892 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003893qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003894{
3895 qf_info_T *qi = &ql_info;
3896
3897 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3898 {
3899 /* Location list */
3900 qi = GET_LOC_LIST(curwin);
3901 if (qi == NULL)
3902 return 0;
3903 }
3904
3905 return qi->qf_lists[qi->qf_curlist].qf_index;
3906}
3907
3908/*
3909 * Returns the current index in the quickfix/location list (counting only valid
3910 * entries). If no valid entries are in the list, then returns 1.
3911 */
3912 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003913qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003914{
3915 qf_info_T *qi = &ql_info;
3916 qf_list_T *qfl;
3917 qfline_T *qfp;
3918 int i, eidx = 0;
3919 int prev_fnum = 0;
3920
3921 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3922 {
3923 /* Location list */
3924 qi = GET_LOC_LIST(curwin);
3925 if (qi == NULL)
3926 return 1;
3927 }
3928
3929 qfl = &qi->qf_lists[qi->qf_curlist];
3930 qfp = qfl->qf_start;
3931
3932 /* check if the list has valid errors */
3933 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3934 return 1;
3935
3936 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3937 {
3938 if (qfp->qf_valid)
3939 {
3940 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3941 {
3942 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3943 {
3944 /* Count the number of files */
3945 eidx++;
3946 prev_fnum = qfp->qf_fnum;
3947 }
3948 }
3949 else
3950 eidx++;
3951 }
3952 }
3953
3954 return eidx ? eidx : 1;
3955}
3956
3957/*
3958 * Get the 'n'th valid error entry in the quickfix or location list.
3959 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3960 * For :cdo and :ldo returns the 'n'th valid error entry.
3961 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3962 */
3963 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003964qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003965{
3966 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3967 qfline_T *qfp = qfl->qf_start;
3968 int i, eidx;
3969 int prev_fnum = 0;
3970
3971 /* check if the list has valid errors */
3972 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3973 return 1;
3974
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003975 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003976 i++, qfp = qfp->qf_next)
3977 {
3978 if (qfp->qf_valid)
3979 {
3980 if (fdo)
3981 {
3982 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3983 {
3984 /* Count the number of files */
3985 eidx++;
3986 prev_fnum = qfp->qf_fnum;
3987 }
3988 }
3989 else
3990 eidx++;
3991 }
3992
3993 if (eidx == n)
3994 break;
3995 }
3996
3997 if (i <= qfl->qf_count)
3998 return i;
3999 else
4000 return 1;
4001}
4002
4003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004005 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004006 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 */
4008 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004009ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004011 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004012 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004013
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004014 if (eap->cmdidx == CMD_ll
4015 || eap->cmdidx == CMD_lrewind
4016 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004017 || eap->cmdidx == CMD_llast
4018 || eap->cmdidx == CMD_ldo
4019 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004020 {
4021 qi = GET_LOC_LIST(curwin);
4022 if (qi == NULL)
4023 {
4024 EMSG(_(e_loclist));
4025 return;
4026 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004027 }
4028
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004029 if (eap->addr_count > 0)
4030 errornr = (int)eap->line2;
4031 else
4032 {
4033 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4034 errornr = 0;
4035 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4036 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4037 errornr = 1;
4038 else
4039 errornr = 32767;
4040 }
4041
4042 /* For cdo and ldo commands, jump to the nth valid error.
4043 * For cfdo and lfdo commands, jump to the nth valid file entry.
4044 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004045 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4046 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004047 errornr = qf_get_nth_valid_entry(qi,
4048 eap->addr_count > 0 ? (int)eap->line1 : 1,
4049 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4050
4051 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052}
4053
4054/*
4055 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004056 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004057 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 */
4059 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004060ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004062 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004063 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004064
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004065 if (eap->cmdidx == CMD_lnext
4066 || eap->cmdidx == CMD_lNext
4067 || eap->cmdidx == CMD_lprevious
4068 || eap->cmdidx == CMD_lnfile
4069 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004070 || eap->cmdidx == CMD_lpfile
4071 || eap->cmdidx == CMD_ldo
4072 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004073 {
4074 qi = GET_LOC_LIST(curwin);
4075 if (qi == NULL)
4076 {
4077 EMSG(_(e_loclist));
4078 return;
4079 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004080 }
4081
Bram Moolenaar55b69262017-08-13 13:42:01 +02004082 if (eap->addr_count > 0
4083 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4084 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004085 errornr = (int)eap->line2;
4086 else
4087 errornr = 1;
4088
4089 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4090 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004092 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4093 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004095 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4096 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 ? BACKWARD_FILE
4098 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004099 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100}
4101
4102/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004103 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004104 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 */
4106 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004107ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004109 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004110 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004111 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004112 char_u *au_name = NULL;
Bram Moolenaarfc6f16b2018-03-06 17:43:22 +01004113 int save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004114 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004115
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004116 switch (eap->cmdidx)
4117 {
4118 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4119 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4120 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4121 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4122 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4123 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4124 default: break;
4125 }
4126 if (au_name != NULL)
4127 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004128#ifdef FEAT_MBYTE
4129 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4130#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004131#ifdef FEAT_BROWSE
4132 if (cmdmod.browse)
4133 {
4134 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004135 NULL, NULL,
4136 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004137 if (browse_file == NULL)
4138 return;
4139 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4140 vim_free(browse_file);
4141 }
4142 else
4143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004145 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004146
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004147 if (eap->cmdidx == CMD_lfile
4148 || eap->cmdidx == CMD_lgetfile
4149 || eap->cmdidx == CMD_laddfile)
4150 wp = curwin;
4151
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004152 /*
4153 * This function is used by the :cfile, :cgetfile and :caddfile
4154 * commands.
4155 * :cfile always creates a new quickfix list and jumps to the
4156 * first error.
4157 * :cgetfile creates a new quickfix list but doesn't jump to the
4158 * first error.
4159 * :caddfile adds to an existing quickfix list. If there is no
4160 * quickfix list then a new list is created.
4161 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004162 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4163 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004164 if (wp != NULL)
4165 qi = GET_LOC_LIST(wp);
4166 if (res >= 0 && qi != NULL)
4167 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004168 if (qi != NULL)
4169 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004170 if (au_name != NULL)
4171 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004172
4173 /* An autocmd might have freed the quickfix/location list. Check whether it
4174 * is still valid. */
4175 if (qi != NULL && !qflist_valid(wp, save_qfid))
Bram Moolenaar3c097222017-12-21 20:54:49 +01004176 return;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004177 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004178 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179}
4180
4181/*
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004182 * Return the quickfix/location list number with the given identifier.
4183 * Returns -1 if list is not found.
4184 */
4185 static int
4186qf_id2nr(qf_info_T *qi, int_u qfid)
4187{
4188 int qf_idx;
4189
4190 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4191 if (qi->qf_lists[qf_idx].qf_id == qfid)
4192 return qf_idx;
Bram Moolenaar29ce4092018-04-28 21:56:44 +02004193 return INVALID_QFIDX;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004194}
4195
4196/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004197 * Return the vimgrep autocmd name.
4198 */
4199 static char_u *
4200vgr_get_auname(cmdidx_T cmdidx)
4201{
4202 switch (cmdidx)
4203 {
4204 case CMD_vimgrep: return (char_u *)"vimgrep";
4205 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4206 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4207 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4208 case CMD_grep: return (char_u *)"grep";
4209 case CMD_lgrep: return (char_u *)"lgrep";
4210 case CMD_grepadd: return (char_u *)"grepadd";
4211 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4212 default: return NULL;
4213 }
4214}
4215
4216/*
4217 * Initialize the regmatch used by vimgrep for pattern "s".
4218 */
4219 static void
4220vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4221{
4222 /* Get the search pattern: either white-separated or enclosed in // */
4223 regmatch->regprog = NULL;
4224
4225 if (s == NULL || *s == NUL)
4226 {
4227 /* Pattern is empty, use last search pattern. */
4228 if (last_search_pat() == NULL)
4229 {
4230 EMSG(_(e_noprevre));
4231 return;
4232 }
4233 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4234 }
4235 else
4236 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4237
4238 regmatch->rmm_ic = p_ic;
4239 regmatch->rmm_maxcol = 0;
4240}
4241
4242/*
4243 * Display a file name when vimgrep is running.
4244 */
4245 static void
4246vgr_display_fname(char_u *fname)
4247{
4248 char_u *p;
4249
4250 msg_start();
4251 p = msg_strtrunc(fname, TRUE);
4252 if (p == NULL)
4253 msg_outtrans(fname);
4254 else
4255 {
4256 msg_outtrans(p);
4257 vim_free(p);
4258 }
4259 msg_clr_eos();
4260 msg_didout = FALSE; /* overwrite this message */
4261 msg_nowait = TRUE; /* don't wait for this message */
4262 msg_col = 0;
4263 out_flush();
4264}
4265
4266/*
4267 * Load a dummy buffer to search for a pattern using vimgrep.
4268 */
4269 static buf_T *
4270vgr_load_dummy_buf(
4271 char_u *fname,
4272 char_u *dirname_start,
4273 char_u *dirname_now)
4274{
4275 int save_mls;
4276#if defined(FEAT_SYN_HL)
4277 char_u *save_ei = NULL;
4278#endif
4279 buf_T *buf;
4280
4281#if defined(FEAT_SYN_HL)
4282 /* Don't do Filetype autocommands to avoid loading syntax and
4283 * indent scripts, a great speed improvement. */
4284 save_ei = au_event_disable(",Filetype");
4285#endif
4286 /* Don't use modelines here, it's useless. */
4287 save_mls = p_mls;
4288 p_mls = 0;
4289
4290 /* Load file into a buffer, so that 'fileencoding' is detected,
4291 * autocommands applied, etc. */
4292 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4293
4294 p_mls = save_mls;
4295#if defined(FEAT_SYN_HL)
4296 au_event_restore(save_ei);
4297#endif
4298
4299 return buf;
4300}
4301
4302/*
4303 * Check whether a quickfix/location list valid. Autocmds may remove or change
4304 * a quickfix list when vimgrep is running. If the list is not found, create a
4305 * new list.
4306 */
4307 static int
4308vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004309 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004310 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004311 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004312 char_u *title)
4313{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004314 /* Verify that the quickfix/location list was not freed by an autocmd */
4315 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004316 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004317 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004318 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004319 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004320 EMSG(_(e_loc_list_changed));
4321 return FALSE;
4322 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004323 else
4324 {
4325 /* Quickfix list is not found, create a new one. */
4326 qf_new_list(qi, title);
4327 return TRUE;
4328 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004329 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004330
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004331 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004332 /* Autocommands changed the quickfix list. Find the one we were
4333 * using and restore it. */
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004334 qi->qf_curlist = qf_id2nr(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004335
4336 return TRUE;
4337}
4338
4339/*
4340 * Search for a pattern in all the lines in a buffer and add the matching lines
4341 * to a quickfix list.
4342 */
4343 static int
4344vgr_match_buflines(
4345 qf_info_T *qi,
4346 char_u *fname,
4347 buf_T *buf,
4348 regmmatch_T *regmatch,
4349 long tomatch,
4350 int duplicate_name,
4351 int flags)
4352{
4353 int found_match = FALSE;
4354 long lnum;
4355 colnr_T col;
4356
4357 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
4358 {
4359 col = 0;
4360 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
4361 col, NULL, NULL) > 0)
4362 {
4363 /* Pass the buffer number so that it gets used even for a
4364 * dummy buffer, unless duplicate_name is set, then the
4365 * buffer will be wiped out below. */
4366 if (qf_add_entry(qi,
4367 qi->qf_curlist,
4368 NULL, /* dir */
4369 fname,
4370 duplicate_name ? 0 : buf->b_fnum,
4371 ml_get_buf(buf,
4372 regmatch->startpos[0].lnum + lnum, FALSE),
4373 regmatch->startpos[0].lnum + lnum,
4374 regmatch->startpos[0].col + 1,
4375 FALSE, /* vis_col */
4376 NULL, /* search pattern */
4377 0, /* nr */
4378 0, /* type */
4379 TRUE /* valid */
4380 ) == FAIL)
4381 {
4382 got_int = TRUE;
4383 break;
4384 }
4385 found_match = TRUE;
4386 if (--tomatch == 0)
4387 break;
4388 if ((flags & VGR_GLOBAL) == 0
4389 || regmatch->endpos[0].lnum > 0)
4390 break;
4391 col = regmatch->endpos[0].col
4392 + (col == regmatch->endpos[0].col);
4393 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
4394 break;
4395 }
4396 line_breakcheck();
4397 if (got_int)
4398 break;
4399 }
4400
4401 return found_match;
4402}
4403
4404/*
4405 * Jump to the first match and update the directory.
4406 */
4407 static void
4408vgr_jump_to_match(
4409 qf_info_T *qi,
4410 int forceit,
4411 int *redraw_for_dummy,
4412 buf_T *first_match_buf,
4413 char_u *target_dir)
4414{
4415 buf_T *buf;
4416
4417 buf = curbuf;
4418 qf_jump(qi, 0, 0, forceit);
4419 if (buf != curbuf)
4420 /* If we jumped to another buffer redrawing will already be
4421 * taken care of. */
4422 *redraw_for_dummy = FALSE;
4423
4424 /* Jump to the directory used after loading the buffer. */
4425 if (curbuf == first_match_buf && target_dir != NULL)
4426 {
4427 exarg_T ea;
4428
4429 ea.arg = target_dir;
4430 ea.cmdidx = CMD_lcd;
4431 ex_cd(&ea);
4432 }
4433}
4434
4435/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004436 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004437 * ":vimgrepadd {pattern} file(s)"
4438 * ":lvimgrep {pattern} file(s)"
4439 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004440 */
4441 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004442ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004443{
Bram Moolenaar81695252004-12-29 20:58:21 +00004444 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004445 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004446 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004447 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004448 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004449 char_u *s;
4450 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004451 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004452 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004453 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004454 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004455 buf_T *buf;
4456 int duplicate_name = FALSE;
4457 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004458 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004459 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004460 buf_T *first_match_buf = NULL;
4461 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004462 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004463 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004464 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004465 char_u *dirname_start = NULL;
4466 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004467 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004468 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004469
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004470 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01004471 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4472 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004473 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004474#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004475 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004476 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004477#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004478 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004479
Bram Moolenaar754b5602006-02-09 23:53:20 +00004480 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004481 || eap->cmdidx == CMD_lvimgrep
4482 || eap->cmdidx == CMD_lgrepadd
4483 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004484 {
4485 qi = ll_get_or_alloc_list(curwin);
4486 if (qi == NULL)
4487 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004488 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004489 }
4490
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004491 if (eap->addr_count > 0)
4492 tomatch = eap->line2;
4493 else
4494 tomatch = MAXLNUM;
4495
Bram Moolenaar81695252004-12-29 20:58:21 +00004496 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004497 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004498 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004499 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004500 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004501 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004502 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004503 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004504 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004505
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004506 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004507 if (regmatch.regprog == NULL)
4508 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004509
4510 p = skipwhite(p);
4511 if (*p == NUL)
4512 {
4513 EMSG(_("E683: File name missing or invalid pattern"));
4514 goto theend;
4515 }
4516
Bram Moolenaar55b69262017-08-13 13:42:01 +02004517 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004518 && eap->cmdidx != CMD_vimgrepadd
4519 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004520 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004521 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004522 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004523
4524 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004525 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004526 goto theend;
4527 if (fcount == 0)
4528 {
4529 EMSG(_(e_nomatch));
4530 goto theend;
4531 }
4532
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004533 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4534 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004535 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004536 {
4537 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004538 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004539 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004540
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004541 /* Remember the current directory, because a BufRead autocommand that does
4542 * ":lcd %:p:h" changes the meaning of short path names. */
4543 mch_dirname(dirname_start, MAXPATHL);
4544
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004545 /* Remember the current quickfix list identifier, so that we can check for
4546 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004547 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004548
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004549 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004550 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004551 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004552 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004553 if (time(NULL) > seconds)
4554 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004555 /* Display the file name every second or so, show the user we are
4556 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004557 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004558 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004559 }
4560
Bram Moolenaar81695252004-12-29 20:58:21 +00004561 buf = buflist_findname_exp(fnames[fi]);
4562 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4563 {
4564 /* Remember that a buffer with this name already exists. */
4565 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004566 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004567 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004568
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004569 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00004570 }
4571 else
4572 /* Use existing, loaded buffer. */
4573 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004574
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004575 /* Check whether the quickfix list is still valid. When loading a
4576 * buffer above, autocommands might have changed the quickfix list. */
4577 if (!vgr_qflist_valid(wp, qi, save_qfid, *eap->cmdlinep))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004578 {
4579 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004580 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004581 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004582 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004583
Bram Moolenaar81695252004-12-29 20:58:21 +00004584 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004585 {
4586 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004587 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004588 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004589 else
4590 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004591 /* Try for a match in all lines of the buffer.
4592 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004593 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
4594 tomatch, duplicate_name, flags);
4595
Bram Moolenaar81695252004-12-29 20:58:21 +00004596 if (using_dummy)
4597 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004598 if (found_match && first_match_buf == NULL)
4599 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004600 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004601 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004602 /* Never keep a dummy buffer if there is another buffer
4603 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004604 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004605 buf = NULL;
4606 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004607 else if (!cmdmod.hide
4608 || buf->b_p_bh[0] == 'u' /* "unload" */
4609 || buf->b_p_bh[0] == 'w' /* "wipe" */
4610 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004611 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004612 /* When no match was found we don't need to remember the
4613 * buffer, wipe it out. If there was a match and it
4614 * wasn't the first one or we won't jump there: only
4615 * unload the buffer.
4616 * Ignore 'hidden' here, because it may lead to having too
4617 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004618 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004619 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004620 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004621 buf = NULL;
4622 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004623 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004624 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004625 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004626 /* Keeping the buffer, remove the dummy flag. */
4627 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004628 buf = NULL;
4629 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004630 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004631
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004632 if (buf != NULL)
4633 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004634 /* Keeping the buffer, remove the dummy flag. */
4635 buf->b_flags &= ~BF_DUMMY;
4636
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004637 /* If the buffer is still loaded we need to use the
4638 * directory we jumped to below. */
4639 if (buf == first_match_buf
4640 && target_dir == NULL
4641 && STRCMP(dirname_start, dirname_now) != 0)
4642 target_dir = vim_strsave(dirname_now);
4643
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004644 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004645 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004646 * need to be done (again). But not the window-local
4647 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004648 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004649#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004650 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4651 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004652#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004653 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004654 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004655 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004656 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004657 }
4658 }
4659
4660 FreeWild(fcount, fnames);
4661
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004662 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4663 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4664 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004665 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004666
Bram Moolenaar864293a2016-06-02 13:40:04 +02004667 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004668
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004669 if (au_name != NULL)
4670 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4671 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004672 /*
4673 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4674 * is still valid.
4675 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004676 if (!qflist_valid(wp, save_qfid))
4677 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004678
Bram Moolenaar86b68352004-12-27 21:59:20 +00004679 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004680 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004681 {
4682 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004683 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
4684 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004685 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004686 else
4687 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004688
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004689 /* If we loaded a dummy buffer into the current window, the autocommands
4690 * may have messed up things, need to redraw and recompute folds. */
4691 if (redraw_for_dummy)
4692 {
4693#ifdef FEAT_FOLDING
4694 foldUpdateAll(curwin);
4695#else
4696 redraw_later(NOT_VALID);
4697#endif
4698 }
4699
Bram Moolenaar86b68352004-12-27 21:59:20 +00004700theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004701 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004702 vim_free(dirname_now);
4703 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004704 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004705 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004706}
4707
4708/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004709 * Restore current working directory to "dirname_start" if they differ, taking
4710 * into account whether it is set locally or globally.
4711 */
4712 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004713restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004714{
4715 char_u *dirname_now = alloc(MAXPATHL);
4716
4717 if (NULL != dirname_now)
4718 {
4719 mch_dirname(dirname_now, MAXPATHL);
4720 if (STRCMP(dirname_start, dirname_now) != 0)
4721 {
4722 /* If the directory has changed, change it back by building up an
4723 * appropriate ex command and executing it. */
4724 exarg_T ea;
4725
4726 ea.arg = dirname_start;
4727 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4728 ex_cd(&ea);
4729 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004730 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004731 }
4732}
4733
4734/*
4735 * Load file "fname" into a dummy buffer and return the buffer pointer,
4736 * placing the directory resulting from the buffer load into the
4737 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4738 * prior to calling this function. Restores directory to "dirname_start" prior
4739 * to returning, if autocmds or the 'autochdir' option have changed it.
4740 *
4741 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4742 * or wipe_dummy_buffer() later!
4743 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004744 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004745 */
4746 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004747load_dummy_buffer(
4748 char_u *fname,
4749 char_u *dirname_start, /* in: old directory */
4750 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004751{
4752 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004753 bufref_T newbufref;
4754 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004755 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004756 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004757 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004758
4759 /* Allocate a buffer without putting it in the buffer list. */
4760 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4761 if (newbuf == NULL)
4762 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004763 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004764
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004765 /* Init the options. */
4766 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4767
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004768 /* need to open the memfile before putting the buffer in a window */
4769 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004770 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004771 /* Make sure this buffer isn't wiped out by auto commands. */
4772 ++newbuf->b_locked;
4773
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004774 /* set curwin/curbuf to buf and save a few things */
4775 aucmd_prepbuf(&aco, newbuf);
4776
4777 /* Need to set the filename for autocommands. */
4778 (void)setfname(curbuf, fname, NULL, FALSE);
4779
Bram Moolenaar81695252004-12-29 20:58:21 +00004780 /* Create swap file now to avoid the ATTENTION message. */
4781 check_need_swap(TRUE);
4782
4783 /* Remove the "dummy" flag, otherwise autocommands may not
4784 * work. */
4785 curbuf->b_flags &= ~BF_DUMMY;
4786
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004787 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004788 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004789 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004790 NULL, READ_NEW | READ_DUMMY);
4791 --newbuf->b_locked;
4792 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004793 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004794 && !(curbuf->b_flags & BF_NEW))
4795 {
4796 failed = FALSE;
4797 if (curbuf != newbuf)
4798 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004799 /* Bloody autocommands changed the buffer! Can happen when
4800 * using netrw and editing a remote file. Use the current
4801 * buffer instead, delete the dummy one after restoring the
4802 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004803 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004804 newbuf = curbuf;
4805 }
4806 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004807
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004808 /* restore curwin/curbuf and a few other things */
4809 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004810 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4811 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004812
4813 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4814 * skip it. */
4815 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004816 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004817
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004818 /*
4819 * When autocommands/'autochdir' option changed directory: go back.
4820 * Let the caller know what the resulting dir was first, in case it is
4821 * important.
4822 */
4823 mch_dirname(resulting_dir, MAXPATHL);
4824 restore_start_dir(dirname_start);
4825
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004826 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004827 return NULL;
4828 if (failed)
4829 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004830 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004831 return NULL;
4832 }
4833 return newbuf;
4834}
4835
4836/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004837 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4838 * directory to "dirname_start" prior to returning, if autocmds or the
4839 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004840 */
4841 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004842wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004843{
4844 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004845 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004846#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004847 cleanup_T cs;
4848
4849 /* Reset the error/interrupt/exception state here so that aborting()
4850 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4851 * work when got_int is set. */
4852 enter_cleanup(&cs);
4853#endif
4854
Bram Moolenaar81695252004-12-29 20:58:21 +00004855 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004856
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004857#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004858 /* Restore the error/interrupt/exception state if not discarded by a
4859 * new aborting error, interrupt, or uncaught exception. */
4860 leave_cleanup(&cs);
4861#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004862 /* When autocommands/'autochdir' option changed directory: go back. */
4863 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004864 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004865}
4866
4867/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004868 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4869 * directory to "dirname_start" prior to returning, if autocmds or the
4870 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004871 */
4872 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004873unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004874{
4875 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004876 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004877 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004878
4879 /* When autocommands/'autochdir' option changed directory: go back. */
4880 restore_start_dir(dirname_start);
4881 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004882}
4883
Bram Moolenaar05159a02005-02-26 23:04:13 +00004884#if defined(FEAT_EVAL) || defined(PROTO)
4885/*
4886 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004887 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004888 */
4889 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004890get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004891{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004892 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004893 dict_T *dict;
4894 char_u buf[2];
4895 qfline_T *qfp;
4896 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004897 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004898
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004899 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004900 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004901 qi = &ql_info;
4902 if (wp != NULL)
4903 {
4904 qi = GET_LOC_LIST(wp);
4905 if (qi == NULL)
4906 return FAIL;
4907 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004908 }
4909
Bram Moolenaar29ce4092018-04-28 21:56:44 +02004910 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004911 qf_idx = qi->qf_curlist;
4912
4913 if (qf_idx >= qi->qf_listcount
4914 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004915 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004916
Bram Moolenaard823fa92016-08-12 16:29:27 +02004917 qfp = qi->qf_lists[qf_idx].qf_start;
4918 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004919 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004920 /* Handle entries with a non-existing buffer number. */
4921 bufnum = qfp->qf_fnum;
4922 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4923 bufnum = 0;
4924
Bram Moolenaar05159a02005-02-26 23:04:13 +00004925 if ((dict = dict_alloc()) == NULL)
4926 return FAIL;
4927 if (list_append_dict(list, dict) == FAIL)
4928 return FAIL;
4929
4930 buf[0] = qfp->qf_type;
4931 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004932 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004933 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4934 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4935 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4936 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004937 || dict_add_nr_str(dict, "pattern", 0L,
4938 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4939 || dict_add_nr_str(dict, "text", 0L,
4940 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004941 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4942 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4943 return FAIL;
4944
4945 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004946 if (qfp == NULL)
4947 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004948 }
4949 return OK;
4950}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004951
4952/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004953 * Flags used by getqflist()/getloclist() to determine which fields to return.
4954 */
4955enum {
4956 QF_GETLIST_NONE = 0x0,
4957 QF_GETLIST_TITLE = 0x1,
4958 QF_GETLIST_ITEMS = 0x2,
4959 QF_GETLIST_NR = 0x4,
4960 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004961 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004962 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004963 QF_GETLIST_IDX = 0x40,
4964 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01004965 QF_GETLIST_TICK = 0x100,
4966 QF_GETLIST_ALL = 0x1FF
Bram Moolenaard823fa92016-08-12 16:29:27 +02004967};
4968
4969/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02004970 * Parse text from 'di' and return the quickfix list items.
4971 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004972 */
4973 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004974qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004975{
4976 int status = FAIL;
4977 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004978 char_u *errorformat = p_efm;
4979 dictitem_T *efm_di;
4980 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004981
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004982 /* Only a List value is supported */
4983 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004984 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004985 /* If errorformat is supplied then use it, otherwise use the 'efm'
4986 * option setting
4987 */
4988 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4989 {
4990 if (efm_di->di_tv.v_type != VAR_STRING ||
4991 efm_di->di_tv.vval.v_string == NULL)
4992 return FAIL;
4993 errorformat = efm_di->di_tv.vval.v_string;
4994 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004995
Bram Moolenaar36538222017-09-02 19:51:44 +02004996 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004997 if (l == NULL)
4998 return FAIL;
4999
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005000 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
5001 if (qi != NULL)
5002 {
5003 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
5004 qi->qf_refcount++;
5005
Bram Moolenaar36538222017-09-02 19:51:44 +02005006 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005007 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5008 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005009 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005010 qf_free(qi, 0);
5011 }
5012 free(qi);
5013 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005014 dict_add_list(retdict, "items", l);
5015 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005016 }
5017
5018 return status;
5019}
5020
5021/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005022 * Return the quickfix/location list window identifier in the current tabpage.
5023 */
5024 static int
5025qf_winid(qf_info_T *qi)
5026{
5027 win_T *win;
5028
5029 /* The quickfix window can be opened even if the quickfix list is not set
5030 * using ":copen". This is not true for location lists. */
5031 if (qi == NULL)
5032 return 0;
5033 win = qf_find_win(qi);
5034 if (win != NULL)
5035 return win->w_id;
5036 return 0;
5037}
5038
5039/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005040 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005041 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005042 static int
5043qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005044{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005045 int flags = QF_GETLIST_NONE;
5046
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005047 if (dict_find(what, (char_u *)"all", -1) != NULL)
5048 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005049
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005050 if (dict_find(what, (char_u *)"title", -1) != NULL)
5051 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005052
Bram Moolenaara6d48492017-12-12 22:45:31 +01005053 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5054 flags |= QF_GETLIST_NR;
5055
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005056 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5057 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005058
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005059 if (dict_find(what, (char_u *)"context", -1) != NULL)
5060 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005061
Bram Moolenaara6d48492017-12-12 22:45:31 +01005062 if (dict_find(what, (char_u *)"id", -1) != NULL)
5063 flags |= QF_GETLIST_ID;
5064
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005065 if (dict_find(what, (char_u *)"items", -1) != NULL)
5066 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005067
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005068 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5069 flags |= QF_GETLIST_IDX;
5070
5071 if (dict_find(what, (char_u *)"size", -1) != NULL)
5072 flags |= QF_GETLIST_SIZE;
5073
Bram Moolenaarb254af32017-12-18 19:48:58 +01005074 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5075 flags |= QF_GETLIST_TICK;
5076
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005077 return flags;
5078}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005079
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005080/*
5081 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5082 * If 'nr' and 'id' are not present in 'what' then return the current
5083 * quickfix list index.
5084 * If 'nr' is zero then return the current quickfix list index.
5085 * If 'nr' is '$' then return the last quickfix list index.
5086 * If 'id' is present then return the index of the quickfix list with that id.
5087 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5088 * Return -1, if quickfix list is not present or if the stack is empty.
5089 */
5090 static int
5091qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5092{
5093 int qf_idx;
5094 dictitem_T *di;
5095
5096 qf_idx = qi->qf_curlist; /* default is the current list */
5097 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5098 {
5099 /* Use the specified quickfix/location list */
5100 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005101 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005102 /* for zero use the current list */
5103 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005104 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005105 qf_idx = di->di_tv.vval.v_number - 1;
5106 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005107 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005108 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005109 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005110 else if (di->di_tv.v_type == VAR_STRING
5111 && di->di_tv.vval.v_string != NULL
5112 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5113 /* Get the last quickfix list number */
5114 qf_idx = qi->qf_listcount - 1;
5115 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005116 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005117 }
5118
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005119 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5120 {
5121 /* Look for a list with the specified id */
5122 if (di->di_tv.v_type == VAR_NUMBER)
5123 {
5124 /*
5125 * For zero, use the current list or the list specified by 'nr'
5126 */
5127 if (di->di_tv.vval.v_number != 0)
5128 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5129 }
5130 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005131 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005132 }
5133
5134 return qf_idx;
5135}
5136
5137/*
5138 * Return default values for quickfix list properties in retdict.
5139 */
5140 static int
5141qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5142{
5143 int status = OK;
5144
5145 if (flags & QF_GETLIST_TITLE)
5146 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5147 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5148 {
5149 list_T *l = list_alloc();
5150 if (l != NULL)
5151 status = dict_add_list(retdict, "items", l);
5152 else
5153 status = FAIL;
5154 }
5155 if ((status == OK) && (flags & QF_GETLIST_NR))
5156 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5157 if ((status == OK) && (flags & QF_GETLIST_WINID))
5158 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
5159 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5160 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5161 if ((status == OK) && (flags & QF_GETLIST_ID))
5162 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5163 if ((status == OK) && (flags & QF_GETLIST_IDX))
5164 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5165 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5166 status = dict_add_nr_str(retdict, "size", 0L, NULL);
5167 if ((status == OK) && (flags & QF_GETLIST_TICK))
5168 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
5169
5170 return status;
5171}
5172
5173/*
5174 * Return the quickfix list title as 'title' in retdict
5175 */
5176 static int
5177qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5178{
5179 char_u *t;
5180
5181 t = qi->qf_lists[qf_idx].qf_title;
5182 if (t == NULL)
5183 t = (char_u *)"";
5184 return dict_add_nr_str(retdict, "title", 0L, t);
5185}
5186
5187/*
5188 * Return the quickfix list items/entries as 'items' in retdict
5189 */
5190 static int
5191qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5192{
5193 int status = OK;
5194 list_T *l = list_alloc();
5195 if (l != NULL)
5196 {
5197 (void)get_errorlist(qi, NULL, qf_idx, l);
5198 dict_add_list(retdict, "items", l);
5199 }
5200 else
5201 status = FAIL;
5202
5203 return status;
5204}
5205
5206/*
5207 * Return the quickfix list context (if any) as 'context' in retdict.
5208 */
5209 static int
5210qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5211{
5212 int status;
5213 dictitem_T *di;
5214
5215 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5216 {
5217 di = dictitem_alloc((char_u *)"context");
5218 if (di != NULL)
5219 {
5220 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5221 status = dict_add(retdict, di);
5222 if (status == FAIL)
5223 dictitem_free(di);
5224 }
5225 else
5226 status = FAIL;
5227 }
5228 else
5229 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5230
5231 return status;
5232}
5233
5234/*
5235 * Return the quickfix list index as 'idx' in retdict
5236 */
5237 static int
5238qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5239{
5240 int idx = qi->qf_lists[qf_idx].qf_index;
5241 if (qi->qf_lists[qf_idx].qf_count == 0)
5242 /* For empty lists, qf_index is set to 1 */
5243 idx = 0;
5244 return dict_add_nr_str(retdict, "idx", idx, NULL);
5245}
5246
5247/*
5248 * Return quickfix/location list details (title) as a
5249 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5250 * then current list is used. Otherwise the specified list is used.
5251 */
5252 int
5253qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5254{
5255 qf_info_T *qi = &ql_info;
5256 int status = OK;
5257 int qf_idx;
5258 dictitem_T *di;
5259 int flags = QF_GETLIST_NONE;
5260
5261 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5262 return qf_get_list_from_lines(what, di, retdict);
5263
5264 if (wp != NULL)
5265 qi = GET_LOC_LIST(wp);
5266
5267 flags = qf_getprop_keys2flags(what);
5268
5269 if (qi != NULL && qi->qf_listcount != 0)
5270 qf_idx = qf_getprop_qfidx(qi, what);
5271
Bram Moolenaara6d48492017-12-12 22:45:31 +01005272 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005273 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005274 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005275
Bram Moolenaard823fa92016-08-12 16:29:27 +02005276 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005277 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005278 if ((status == OK) && (flags & QF_GETLIST_NR))
5279 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5280 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005281 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005282 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005283 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005284 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005285 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005286 if ((status == OK) && (flags & QF_GETLIST_ID))
5287 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5288 NULL);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005289 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005290 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005291 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5292 status = dict_add_nr_str(retdict, "size",
5293 qi->qf_lists[qf_idx].qf_count, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005294 if ((status == OK) && (flags & QF_GETLIST_TICK))
5295 status = dict_add_nr_str(retdict, "changedtick",
5296 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5297
Bram Moolenaard823fa92016-08-12 16:29:27 +02005298 return status;
5299}
5300
5301/*
5302 * Add list of entries to quickfix/location list. Each list entry is
5303 * a dictionary with item information.
5304 */
5305 static int
5306qf_add_entries(
5307 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005308 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005309 list_T *list,
5310 char_u *title,
5311 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005312{
5313 listitem_T *li;
5314 dict_T *d;
5315 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005316 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005317 long lnum;
5318 int col, nr;
5319 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005320 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005321 int valid, status;
5322 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005323 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005324
Bram Moolenaara3921f42017-06-04 15:30:34 +02005325 if (action == ' ' || qf_idx == qi->qf_listcount)
5326 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005327 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005328 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005329 qf_idx = qi->qf_curlist;
5330 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005331 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005332 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005333 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005334 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005335 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005336 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005337 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005338 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005339
5340 for (li = list->lv_first; li != NULL; li = li->li_next)
5341 {
5342 if (li->li_tv.v_type != VAR_DICT)
5343 continue; /* Skip non-dict items */
5344
5345 d = li->li_tv.vval.v_dict;
5346 if (d == NULL)
5347 continue;
5348
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005349 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005350 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5351 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5352 col = (int)get_dict_number(d, (char_u *)"col");
5353 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5354 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005355 type = get_dict_string(d, (char_u *)"type", TRUE);
5356 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5357 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005358 if (text == NULL)
5359 text = vim_strsave((char_u *)"");
5360
5361 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005362 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005363 valid = FALSE;
5364
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005365 /* Mark entries with non-existing buffer number as not valid. Give the
5366 * error message only once. */
5367 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5368 {
5369 if (!did_bufnr_emsg)
5370 {
5371 did_bufnr_emsg = TRUE;
5372 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5373 }
5374 valid = FALSE;
5375 bufnum = 0;
5376 }
5377
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005378 /* If the 'valid' field is present it overrules the detected value. */
5379 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5380 valid = (int)get_dict_number(d, (char_u *)"valid");
5381
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005382 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005383 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005384 NULL, /* dir */
5385 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005386 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005387 text,
5388 lnum,
5389 col,
5390 vcol, /* vis_col */
5391 pattern, /* search pattern */
5392 nr,
5393 type == NULL ? NUL : *type,
5394 valid);
5395
5396 vim_free(filename);
5397 vim_free(pattern);
5398 vim_free(text);
5399 vim_free(type);
5400
5401 if (status == FAIL)
5402 {
5403 retval = FAIL;
5404 break;
5405 }
5406 }
5407
Bram Moolenaara3921f42017-06-04 15:30:34 +02005408 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005409 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005410 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005411 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005412 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005413 if (action != 'a')
5414 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005415 qi->qf_lists[qf_idx].qf_ptr =
5416 qi->qf_lists[qf_idx].qf_start;
5417 if (qi->qf_lists[qf_idx].qf_count > 0)
5418 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005419 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005420
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005421 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005422 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005423
5424 return retval;
5425}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005426
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005427/*
5428 * Get the quickfix list index from 'nr' or 'id'
5429 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02005430 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005431qf_setprop_get_qfidx(
5432 qf_info_T *qi,
5433 dict_T *what,
5434 int action,
5435 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005436{
5437 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005438 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005439
Bram Moolenaard823fa92016-08-12 16:29:27 +02005440 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5441 {
5442 /* Use the specified quickfix/location list */
5443 if (di->di_tv.v_type == VAR_NUMBER)
5444 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005445 /* for zero use the current list */
5446 if (di->di_tv.vval.v_number != 0)
5447 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005448
Bram Moolenaar55b69262017-08-13 13:42:01 +02005449 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5450 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005451 /*
5452 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005453 * non-available list and add the new list at the end of the
5454 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005455 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005456 *newlist = TRUE;
5457 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005458 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005459 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005460 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005461 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005462 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005463 }
5464 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005465 && di->di_tv.vval.v_string != NULL
5466 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005467 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005468 if (qi->qf_listcount > 0)
5469 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005470 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02005471 qf_idx = 0;
5472 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005473 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005474 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005475 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005476 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005477 }
5478
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005479 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005480 {
5481 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005482 if (di->di_tv.v_type != VAR_NUMBER)
5483 return INVALID_QFIDX;
5484
5485 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005486 }
5487
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005488 return qf_idx;
5489}
5490
5491/*
5492 * Set the quickfix list title.
5493 */
5494 static int
5495qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
5496{
5497 if (di->di_tv.v_type != VAR_STRING)
5498 return FAIL;
5499
5500 vim_free(qi->qf_lists[qf_idx].qf_title);
5501 qi->qf_lists[qf_idx].qf_title =
5502 get_dict_string(what, (char_u *)"title", TRUE);
5503 if (qf_idx == qi->qf_curlist)
5504 qf_update_win_titlevar(qi);
5505
5506 return OK;
5507}
5508
5509/*
5510 * Set quickfix list items/entries.
5511 */
5512 static int
5513qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
5514{
5515 int retval = FAIL;
5516 char_u *title_save;
5517
5518 if (di->di_tv.v_type != VAR_LIST)
5519 return FAIL;
5520
5521 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5522 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5523 title_save, action == ' ' ? 'a' : action);
5524 if (action == 'r')
5525 {
5526 /*
5527 * When replacing the quickfix list entries using
5528 * qf_add_entries(), the title is set with a ':' prefix.
5529 * Restore the title with the saved title.
5530 */
5531 vim_free(qi->qf_lists[qf_idx].qf_title);
5532 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5533 }
5534 vim_free(title_save);
5535
5536 return retval;
5537}
5538
5539/*
5540 * Set quickfix list items/entries from a list of lines.
5541 */
5542 static int
5543qf_setprop_items_from_lines(
5544 qf_info_T *qi,
5545 int qf_idx,
5546 dict_T *what,
5547 dictitem_T *di,
5548 int action)
5549{
5550 char_u *errorformat = p_efm;
5551 dictitem_T *efm_di;
5552 int retval = FAIL;
5553
5554 /* Use the user supplied errorformat settings (if present) */
5555 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5556 {
5557 if (efm_di->di_tv.v_type != VAR_STRING ||
5558 efm_di->di_tv.vval.v_string == NULL)
5559 return FAIL;
5560 errorformat = efm_di->di_tv.vval.v_string;
5561 }
5562
5563 /* Only a List value is supported */
5564 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
5565 return FAIL;
5566
5567 if (action == 'r')
5568 qf_free_items(qi, qf_idx);
5569 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
5570 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5571 retval = OK;
5572
5573 return retval;
5574}
5575
5576/*
5577 * Set quickfix list context.
5578 */
5579 static int
5580qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
5581{
5582 typval_T *ctx;
5583
5584 free_tv(qi->qf_lists[qf_idx].qf_ctx);
5585 ctx = alloc_tv();
5586 if (ctx != NULL)
5587 copy_tv(&di->di_tv, ctx);
5588 qi->qf_lists[qf_idx].qf_ctx = ctx;
5589
5590 return OK;
5591}
5592
5593/*
5594 * Set quickfix/location list properties (title, items, context).
5595 * Also used to add items from parsing a list of lines.
5596 * Used by the setqflist() and setloclist() VimL functions.
5597 */
5598 static int
5599qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
5600{
5601 dictitem_T *di;
5602 int retval = FAIL;
5603 int qf_idx;
5604 int newlist = FALSE;
5605
5606 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5607 newlist = TRUE;
5608
5609 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
5610 if (qf_idx == INVALID_QFIDX) /* List not found */
5611 return FAIL;
5612
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005613 if (newlist)
5614 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005615 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005616 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005617 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005618 }
5619
5620 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005621 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005622 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005623 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005624 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005625 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005626 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005627 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005628
Bram Moolenaarb254af32017-12-18 19:48:58 +01005629 if (retval == OK)
5630 qf_list_changed(qi, qf_idx);
5631
Bram Moolenaard823fa92016-08-12 16:29:27 +02005632 return retval;
5633}
5634
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005635/*
5636 * Find the non-location list window with the specified location list.
5637 */
5638 static win_T *
5639find_win_with_ll(qf_info_T *qi)
5640{
5641 win_T *wp = NULL;
5642
5643 FOR_ALL_WINDOWS(wp)
5644 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5645 return wp;
5646
5647 return NULL;
5648}
5649
5650/*
5651 * Free the entire quickfix/location list stack.
5652 * If the quickfix/location list window is open, then clear it.
5653 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005654 static void
5655qf_free_stack(win_T *wp, qf_info_T *qi)
5656{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005657 win_T *qfwin = qf_find_win(qi);
5658 win_T *llwin = NULL;
5659 win_T *orig_wp = wp;
5660
5661 if (qfwin != NULL)
5662 {
5663 /* If the quickfix/location list window is open, then clear it */
5664 if (qi->qf_curlist < qi->qf_listcount)
5665 qf_free(qi, qi->qf_curlist);
5666 qf_update_buffer(qi, NULL);
5667 }
5668
5669 if (wp != NULL && IS_LL_WINDOW(wp))
5670 {
5671 /* If in the location list window, then use the non-location list
5672 * window with this location list (if present)
5673 */
5674 llwin = find_win_with_ll(qi);
5675 if (llwin != NULL)
5676 wp = llwin;
5677 }
5678
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005679 qf_free_all(wp);
5680 if (wp == NULL)
5681 {
5682 /* quickfix list */
5683 qi->qf_curlist = 0;
5684 qi->qf_listcount = 0;
5685 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005686 else if (IS_LL_WINDOW(orig_wp))
5687 {
5688 /* If the location list window is open, then create a new empty
5689 * location list */
5690 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005691
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005692 /* first free the list reference in the location list window */
5693 ll_free_all(&orig_wp->w_llist_ref);
5694
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005695 orig_wp->w_llist_ref = new_ll;
5696 if (llwin != NULL)
5697 {
5698 llwin->w_llist = new_ll;
5699 new_ll->qf_refcount++;
5700 }
5701 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005702}
5703
Bram Moolenaard823fa92016-08-12 16:29:27 +02005704/*
5705 * Populate the quickfix list with the items supplied in the list
5706 * of dictionaries. "title" will be copied to w:quickfix_title.
5707 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5708 */
5709 int
5710set_errorlist(
5711 win_T *wp,
5712 list_T *list,
5713 int action,
5714 char_u *title,
5715 dict_T *what)
5716{
5717 qf_info_T *qi = &ql_info;
5718 int retval = OK;
5719
5720 if (wp != NULL)
5721 {
5722 qi = ll_get_or_alloc_list(wp);
5723 if (qi == NULL)
5724 return FAIL;
5725 }
5726
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005727 if (action == 'f')
5728 {
5729 /* Free the entire quickfix or location list stack */
5730 qf_free_stack(wp, qi);
5731 }
5732 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005733 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005734 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005735 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005736 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005737 if (retval == OK)
5738 qf_list_changed(qi, qi->qf_curlist);
5739 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005740
5741 return retval;
5742}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005743
5744 static int
5745mark_quickfix_ctx(qf_info_T *qi, int copyID)
5746{
5747 int i;
5748 int abort = FALSE;
5749 typval_T *ctx;
5750
5751 for (i = 0; i < LISTCOUNT && !abort; ++i)
5752 {
5753 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005754 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5755 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005756 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5757 }
5758
5759 return abort;
5760}
5761
5762/*
5763 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005764 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005765 */
5766 int
5767set_ref_in_quickfix(int copyID)
5768{
5769 int abort = FALSE;
5770 tabpage_T *tp;
5771 win_T *win;
5772
5773 abort = mark_quickfix_ctx(&ql_info, copyID);
5774 if (abort)
5775 return abort;
5776
5777 FOR_ALL_TAB_WINDOWS(tp, win)
5778 {
5779 if (win->w_llist != NULL)
5780 {
5781 abort = mark_quickfix_ctx(win->w_llist, copyID);
5782 if (abort)
5783 return abort;
5784 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005785 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5786 {
5787 /* In a location list window and none of the other windows is
5788 * referring to this location list. Mark the location list
5789 * context as still in use.
5790 */
5791 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5792 if (abort)
5793 return abort;
5794 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005795 }
5796
5797 return abort;
5798}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005799#endif
5800
Bram Moolenaar81695252004-12-29 20:58:21 +00005801/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005802 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005803 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005804 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005805 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005806 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005807 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005808 */
5809 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005810ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005811{
5812 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005813 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005814 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005815 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005816
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005817 switch (eap->cmdidx)
5818 {
5819 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5820 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5821 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5822 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5823 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5824 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5825 default: break;
5826 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005827 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5828 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005829 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005830#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005831 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005832 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005833#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005834 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005835
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005836 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005837 if (eap->cmdidx == CMD_lbuffer
5838 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005839 || eap->cmdidx == CMD_laddbuffer)
5840 {
5841 qi = ll_get_or_alloc_list(curwin);
5842 if (qi == NULL)
5843 return;
5844 }
5845
Bram Moolenaar86b68352004-12-27 21:59:20 +00005846 if (*eap->arg == NUL)
5847 buf = curbuf;
5848 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5849 buf = buflist_findnr(atoi((char *)eap->arg));
5850 if (buf == NULL)
5851 EMSG(_(e_invarg));
5852 else if (buf->b_ml.ml_mfp == NULL)
5853 EMSG(_("E681: Buffer is not loaded"));
5854 else
5855 {
5856 if (eap->addr_count == 0)
5857 {
5858 eap->line1 = 1;
5859 eap->line2 = buf->b_ml.ml_line_count;
5860 }
5861 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5862 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5863 EMSG(_(e_invrange));
5864 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005865 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005866 char_u *qf_title = *eap->cmdlinep;
5867
5868 if (buf->b_sfname)
5869 {
5870 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5871 (char *)qf_title, (char *)buf->b_sfname);
5872 qf_title = IObuff;
5873 }
5874
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005875 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005876 (eap->cmdidx != CMD_caddbuffer
5877 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005878 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005879 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005880 if (res >= 0)
5881 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005882 if (au_name != NULL)
5883 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5884 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005885 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5886 eap->cmdidx == CMD_lbuffer))
5887 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005888 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005889 }
5890}
5891
Bram Moolenaar1e015462005-09-25 22:16:38 +00005892#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005893/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005894 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5895 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005896 */
5897 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005898ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005899{
5900 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005901 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005902 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005903 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005904
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005905 switch (eap->cmdidx)
5906 {
5907 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5908 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5909 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5910 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5911 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5912 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5913 default: break;
5914 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005915 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5916 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005917 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005918#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005919 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005920 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005921#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005922 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005923
Bram Moolenaar3c097222017-12-21 20:54:49 +01005924 if (eap->cmdidx == CMD_lexpr
5925 || eap->cmdidx == CMD_lgetexpr
5926 || eap->cmdidx == CMD_laddexpr)
5927 {
5928 qi = ll_get_or_alloc_list(curwin);
5929 if (qi == NULL)
5930 return;
5931 }
5932
Bram Moolenaar4770d092006-01-12 23:22:24 +00005933 /* Evaluate the expression. When the result is a string or a list we can
5934 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005935 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005936 if (tv != NULL)
5937 {
5938 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5939 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5940 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005941 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005942 (eap->cmdidx != CMD_caddexpr
5943 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005944 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005945 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005946 if (res >= 0)
5947 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005948 if (au_name != NULL)
5949 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5950 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005951 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5952 eap->cmdidx == CMD_lexpr))
5953 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005954 }
5955 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005956 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005957 free_tv(tv);
5958 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005959}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005960#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005961
5962/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02005963 * Get the location list for ":lhelpgrep"
5964 */
5965 static qf_info_T *
5966hgr_get_ll(int *new_ll)
5967{
5968 win_T *wp;
5969 qf_info_T *qi;
5970
5971 /* If the current window is a help window, then use it */
5972 if (bt_help(curwin->w_buffer))
5973 wp = curwin;
5974 else
5975 /* Find an existing help window */
5976 FOR_ALL_WINDOWS(wp)
5977 if (bt_help(wp->w_buffer))
5978 break;
5979
5980 if (wp == NULL) /* Help window not found */
5981 qi = NULL;
5982 else
5983 qi = wp->w_llist;
5984
5985 if (qi == NULL)
5986 {
5987 /* Allocate a new location list for help text matches */
5988 if ((qi = ll_new_list()) == NULL)
5989 return NULL;
5990 *new_ll = TRUE;
5991 }
5992
5993 return qi;
5994}
5995
5996/*
5997 * Search for a pattern in a help file.
5998 */
5999 static void
6000hgr_search_file(
6001 qf_info_T *qi,
6002 char_u *fname,
6003#ifdef FEAT_MBYTE
6004 vimconv_T *p_vc,
6005#endif
6006 regmatch_T *p_regmatch)
6007{
6008 FILE *fd;
6009 long lnum;
6010
6011 fd = mch_fopen((char *)fname, "r");
6012 if (fd == NULL)
6013 return;
6014
6015 lnum = 1;
6016 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6017 {
6018 char_u *line = IObuff;
6019#ifdef FEAT_MBYTE
6020 /* Convert a line if 'encoding' is not utf-8 and
6021 * the line contains a non-ASCII character. */
6022 if (p_vc->vc_type != CONV_NONE
6023 && has_non_ascii(IObuff))
6024 {
6025 line = string_convert(p_vc, IObuff, NULL);
6026 if (line == NULL)
6027 line = IObuff;
6028 }
6029#endif
6030
6031 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6032 {
6033 int l = (int)STRLEN(line);
6034
6035 /* remove trailing CR, LF, spaces, etc. */
6036 while (l > 0 && line[l - 1] <= ' ')
6037 line[--l] = NUL;
6038
6039 if (qf_add_entry(qi,
6040 qi->qf_curlist,
6041 NULL, /* dir */
6042 fname,
6043 0,
6044 line,
6045 lnum,
6046 (int)(p_regmatch->startp[0] - line)
6047 + 1, /* col */
6048 FALSE, /* vis_col */
6049 NULL, /* search pattern */
6050 0, /* nr */
6051 1, /* type */
6052 TRUE /* valid */
6053 ) == FAIL)
6054 {
6055 got_int = TRUE;
6056#ifdef FEAT_MBYTE
6057 if (line != IObuff)
6058 vim_free(line);
6059#endif
6060 break;
6061 }
6062 }
6063#ifdef FEAT_MBYTE
6064 if (line != IObuff)
6065 vim_free(line);
6066#endif
6067 ++lnum;
6068 line_breakcheck();
6069 }
6070 fclose(fd);
6071}
6072
6073/*
6074 * Search for a pattern in all the help files in the doc directory under
6075 * the given directory.
6076 */
6077 static void
6078hgr_search_files_in_dir(
6079 qf_info_T *qi,
6080 char_u *dirname,
6081 regmatch_T *p_regmatch
6082#ifdef FEAT_MBYTE
6083 , vimconv_T *p_vc
6084#endif
6085#ifdef FEAT_MULTI_LANG
6086 , char_u *lang
6087#endif
6088 )
6089{
6090 int fcount;
6091 char_u **fnames;
6092 int fi;
6093
6094 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6095 add_pathsep(dirname);
6096 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6097 if (gen_expand_wildcards(1, &dirname, &fcount,
6098 &fnames, EW_FILE|EW_SILENT) == OK
6099 && fcount > 0)
6100 {
6101 for (fi = 0; fi < fcount && !got_int; ++fi)
6102 {
6103#ifdef FEAT_MULTI_LANG
6104 /* Skip files for a different language. */
6105 if (lang != NULL
6106 && STRNICMP(lang, fnames[fi]
6107 + STRLEN(fnames[fi]) - 3, 2) != 0
6108 && !(STRNICMP(lang, "en", 2) == 0
6109 && STRNICMP("txt", fnames[fi]
6110 + STRLEN(fnames[fi]) - 3, 3) == 0))
6111 continue;
6112#endif
6113
6114 hgr_search_file(qi, fnames[fi],
6115#ifdef FEAT_MBYTE
6116 p_vc,
6117#endif
6118 p_regmatch);
6119 }
6120 FreeWild(fcount, fnames);
6121 }
6122}
6123
6124/*
6125 * Search for a pattern in all the help files in the 'runtimepath'.
6126 */
6127 static void
6128hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6129{
6130 char_u *p;
6131#ifdef FEAT_MULTI_LANG
6132 char_u *lang;
6133#endif
6134
6135#ifdef FEAT_MBYTE
6136 vimconv_T vc;
6137
6138 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6139 * differs. */
6140 vc.vc_type = CONV_NONE;
6141 if (!enc_utf8)
6142 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6143#endif
6144
6145#ifdef FEAT_MULTI_LANG
6146 /* Check for a specified language */
6147 lang = check_help_lang(arg);
6148#endif
6149
6150 /* Go through all directories in 'runtimepath' */
6151 p = p_rtp;
6152 while (*p != NUL && !got_int)
6153 {
6154 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6155
6156 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6157#ifdef FEAT_MBYTE
6158 , &vc
6159#endif
6160#ifdef FEAT_MULTI_LANG
6161 , lang
6162#endif
6163 );
6164 }
6165
6166#ifdef FEAT_MBYTE
6167 if (vc.vc_type != CONV_NONE)
6168 convert_setup(&vc, NULL, NULL);
6169#endif
6170}
6171
6172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 * ":helpgrep {pattern}"
6174 */
6175 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006176ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177{
6178 regmatch_T regmatch;
6179 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006180 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006181 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006182 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
Bram Moolenaar73633f82012-01-20 13:39:07 +01006184 switch (eap->cmdidx)
6185 {
6186 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6187 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6188 default: break;
6189 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006190 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6191 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006192 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006193#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006194 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006195 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006196#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006197 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006198
6199 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6200 save_cpo = p_cpo;
6201 p_cpo = empty_option;
6202
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006203 if (eap->cmdidx == CMD_lhelpgrep)
6204 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006205 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006206 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006207 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006208 }
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6211 regmatch.rm_ic = FALSE;
6212 if (regmatch.regprog != NULL)
6213 {
6214 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006215 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006217 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006218
Bram Moolenaar473de612013-06-08 18:19:48 +02006219 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006221 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6222 qi->qf_lists[qi->qf_curlist].qf_ptr =
6223 qi->qf_lists[qi->qf_curlist].qf_start;
6224 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 }
6226
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006227 if (p_cpo == empty_option)
6228 p_cpo = save_cpo;
6229 else
6230 /* Darn, some plugin changed the value. */
6231 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
Bram Moolenaarb254af32017-12-18 19:48:58 +01006233 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006234 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
Bram Moolenaar73633f82012-01-20 13:39:07 +01006236 if (au_name != NULL)
6237 {
6238 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6239 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006240 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006241 /* autocommands made "qi" invalid */
6242 return;
6243 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006244
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006246 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006247 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006248 else
6249 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006250
6251 if (eap->cmdidx == CMD_lhelpgrep)
6252 {
6253 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006254 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006255 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006256 {
6257 if (new_qi)
6258 ll_free_all(&qi);
6259 }
6260 else if (curwin->w_llist == NULL)
6261 curwin->w_llist = qi;
6262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263}
6264
6265#endif /* FEAT_QUICKFIX */