blob: 385438b55fd34611ac5b647d4e7b604c4a65a732 [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 */
Bram Moolenaard76ce852018-05-01 15:02:04 +020036 char_u *qf_module; /* module name for this error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000037 char_u *qf_pattern; /* search pattern for the error */
38 char_u *qf_text; /* description of the error */
39 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
40 char_u qf_cleared; /* set to TRUE if line has been deleted */
41 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000043 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020052/*
53 * Quickfix/Location list definition
54 * Contains a list of entries (qfline_T). qf_start points to the first entry
55 * and qf_last points to the last entry. qf_count contains the list size.
56 *
57 * Usually the list contains one or more entries. But an empty list can be
58 * created using setqflist()/setloclist() with a title and/or user context
59 * information and entries can be added later using setqflist()/setloclist().
60 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000062{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020063 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020065 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000068 int qf_index; /* current index in the error list */
69 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020070 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020071 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020072 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020073
74 struct dir_stack_T *qf_dir_stack;
75 char_u *qf_directory;
76 struct dir_stack_T *qf_file_stack;
77 char_u *qf_currfile;
78 int qf_multiline;
79 int qf_multiignore;
80 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010081 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000082} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020084/*
85 * Quickfix/Location list stack definition
86 * Contains a list of quickfix/location lists (qf_list_T)
87 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000088struct qf_info_S
89{
90 /*
91 * Count of references to this list. Used only for location lists.
92 * When a location list window reference this list, qf_refcount
93 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
94 * reaches 0, the list is freed.
95 */
96 int qf_refcount;
97 int qf_listcount; /* current number of lists */
98 int qf_curlist; /* current error list */
99 qf_list_T qf_lists[LISTCOUNT];
100};
101
102static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200103static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaard76ce852018-05-01 15:02:04 +0200105#define FMT_PATTERNS 11 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107/*
108 * Structure used to hold the info of one part of 'errorformat'
109 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000110typedef struct efm_S efm_T;
111struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112{
113 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
116 char_u prefix; /* prefix of this format line: */
117 /* 'D' enter directory */
118 /* 'X' leave directory */
119 /* 'A' start of multi-line message */
120 /* 'E' error message */
121 /* 'W' warning message */
122 /* 'I' informational message */
123 /* 'C' continuation line */
124 /* 'Z' end of multi-line message */
125 /* 'G' general, unspecific message */
126 /* 'P' push file (partial) message */
127 /* 'Q' pop/quit file (partial) message */
128 /* 'O' overread (partial) message */
129 char_u flags; /* additional flags given in prefix */
130 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000131 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000132 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133};
134
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100135static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
136
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200137static 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 +0100138static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200139static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
140static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void qf_free(qf_info_T *qi, int idx);
142static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200143static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200144static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200146static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static win_T *qf_find_win(qf_info_T *qi);
150static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200151static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200153static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
156static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
157static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
158static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000160/* Quickfix window check helper macro */
161#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
162/* Location list window check helper macro */
163#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
164/*
165 * Return location list for window 'wp'
166 * For location list window, return the referenced location list
167 */
168#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200171 * Looking up a buffer can be slow if there are many. Remember the last one
172 * to make this a lot faster if there are multiple matches in the same file.
173 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200174static char_u *qf_last_bufname = NULL;
175static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200176
Bram Moolenaar3c097222017-12-21 20:54:49 +0100177static char *e_loc_list_changed =
178 N_("E926: Current location list was changed");
179
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200180/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000181 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200182 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 * Return -1 for error, number of errors for success.
184 */
185 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200186qf_init(win_T *wp,
187 char_u *efile,
188 char_u *errorformat,
189 int newlist, /* TRUE: start a new error list */
190 char_u *qf_title,
191 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000193 qf_info_T *qi = &ql_info;
194
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000195 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000196 {
197 qi = ll_get_or_alloc_list(wp);
198 if (qi == NULL)
199 return FAIL;
200 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000201
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200202 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
203 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000204}
205
206/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200207 * Maximum number of bytes allowed per line while reading a errorfile.
208 */
209#define LINE_MAXLEN 4096
210
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200211static struct fmtpattern
212{
213 char_u convchar;
214 char *pattern;
215} fmt_pat[FMT_PATTERNS] =
216 {
217 {'f', ".\\+"}, /* only used when at end */
218 {'n', "\\d\\+"},
219 {'l', "\\d\\+"},
220 {'c', "\\d\\+"},
221 {'t', "."},
222 {'m', ".\\+"},
223 {'r', ".*"},
224 {'p', "[- .]*"},
225 {'v', "\\d\\+"},
Bram Moolenaard76ce852018-05-01 15:02:04 +0200226 {'s', ".\\+"},
227 {'o', ".\\+"}
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200228 };
229
230/*
231 * Converts a 'errorformat' string to regular expression pattern
232 */
233 static int
234efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200235 char_u *efm,
236 int len,
237 efm_T *fmt_ptr,
238 char_u *regpat,
239 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200240{
241 char_u *ptr;
242 char_u *efmp;
243 char_u *srcptr;
244 int round;
245 int idx = 0;
246
247 /*
248 * Build regexp pattern from current 'errorformat' option
249 */
250 ptr = regpat;
251 *ptr++ = '^';
252 round = 0;
253 for (efmp = efm; efmp < efm + len; ++efmp)
254 {
255 if (*efmp == '%')
256 {
257 ++efmp;
258 for (idx = 0; idx < FMT_PATTERNS; ++idx)
259 if (fmt_pat[idx].convchar == *efmp)
260 break;
261 if (idx < FMT_PATTERNS)
262 {
263 if (fmt_ptr->addr[idx])
264 {
265 sprintf((char *)errmsg,
266 _("E372: Too many %%%c in format string"), *efmp);
267 EMSG(errmsg);
268 return -1;
269 }
270 if ((idx
271 && idx < 6
272 && vim_strchr((char_u *)"DXOPQ",
273 fmt_ptr->prefix) != NULL)
274 || (idx == 6
275 && vim_strchr((char_u *)"OPQ",
276 fmt_ptr->prefix) == NULL))
277 {
278 sprintf((char *)errmsg,
279 _("E373: Unexpected %%%c in format string"), *efmp);
280 EMSG(errmsg);
281 return -1;
282 }
283 fmt_ptr->addr[idx] = (char_u)++round;
284 *ptr++ = '\\';
285 *ptr++ = '(';
286#ifdef BACKSLASH_IN_FILENAME
287 if (*efmp == 'f')
288 {
289 /* Also match "c:" in the file name, even when
290 * checking for a colon next: "%f:".
291 * "\%(\a:\)\=" */
292 STRCPY(ptr, "\\%(\\a:\\)\\=");
293 ptr += 10;
294 }
295#endif
296 if (*efmp == 'f' && efmp[1] != NUL)
297 {
298 if (efmp[1] != '\\' && efmp[1] != '%')
299 {
300 /* A file name may contain spaces, but this isn't
301 * in "\f". For "%f:%l:%m" there may be a ":" in
302 * the file name. Use ".\{-1,}x" instead (x is
303 * the next character), the requirement that :999:
304 * follows should work. */
305 STRCPY(ptr, ".\\{-1,}");
306 ptr += 7;
307 }
308 else
309 {
310 /* File name followed by '\\' or '%': include as
311 * many file name chars as possible. */
312 STRCPY(ptr, "\\f\\+");
313 ptr += 4;
314 }
315 }
316 else
317 {
318 srcptr = (char_u *)fmt_pat[idx].pattern;
319 while ((*ptr = *srcptr++) != NUL)
320 ++ptr;
321 }
322 *ptr++ = '\\';
323 *ptr++ = ')';
324 }
325 else if (*efmp == '*')
326 {
327 if (*++efmp == '[' || *efmp == '\\')
328 {
329 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
330 {
331 if (efmp[1] == '^')
332 *ptr++ = *++efmp;
333 if (efmp < efm + len)
334 {
335 *ptr++ = *++efmp; /* could be ']' */
336 while (efmp < efm + len
337 && (*ptr++ = *++efmp) != ']')
338 /* skip */;
339 if (efmp == efm + len)
340 {
341 EMSG(_("E374: Missing ] in format string"));
342 return -1;
343 }
344 }
345 }
346 else if (efmp < efm + len) /* %*\D, %*\s etc. */
347 *ptr++ = *++efmp;
348 *ptr++ = '\\';
349 *ptr++ = '+';
350 }
351 else
352 {
353 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
354 sprintf((char *)errmsg,
355 _("E375: Unsupported %%%c in format string"), *efmp);
356 EMSG(errmsg);
357 return -1;
358 }
359 }
360 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
361 *ptr++ = *efmp; /* regexp magic characters */
362 else if (*efmp == '#')
363 *ptr++ = '*';
364 else if (*efmp == '>')
365 fmt_ptr->conthere = TRUE;
366 else if (efmp == efm + 1) /* analyse prefix */
367 {
368 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
369 fmt_ptr->flags = *efmp++;
370 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
371 fmt_ptr->prefix = *efmp;
372 else
373 {
374 sprintf((char *)errmsg,
375 _("E376: Invalid %%%c in format string prefix"), *efmp);
376 EMSG(errmsg);
377 return -1;
378 }
379 }
380 else
381 {
382 sprintf((char *)errmsg,
383 _("E377: Invalid %%%c in format string"), *efmp);
384 EMSG(errmsg);
385 return -1;
386 }
387 }
388 else /* copy normal character */
389 {
390 if (*efmp == '\\' && efmp + 1 < efm + len)
391 ++efmp;
392 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
393 *ptr++ = '\\'; /* escape regexp atoms */
394 if (*efmp)
395 *ptr++ = *efmp;
396 }
397 }
398 *ptr++ = '$';
399 *ptr = NUL;
400
401 return 0;
402}
403
404 static void
405free_efm_list(efm_T **efm_first)
406{
407 efm_T *efm_ptr;
408
409 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
410 {
411 *efm_first = efm_ptr->next;
412 vim_regfree(efm_ptr->prog);
413 vim_free(efm_ptr);
414 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100415 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200416}
417
418/* Parse 'errorformat' option */
419 static efm_T *
420parse_efm_option(char_u *efm)
421{
422 char_u *errmsg = NULL;
423 int errmsglen;
424 efm_T *fmt_ptr = NULL;
425 efm_T *fmt_first = NULL;
426 efm_T *fmt_last = NULL;
427 char_u *fmtstr = NULL;
428 int len;
429 int i;
430 int round;
431
432 errmsglen = CMDBUFFSIZE + 1;
433 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
434 if (errmsg == NULL)
435 goto parse_efm_end;
436
437 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200438 * Each part of the format string is copied and modified from errorformat
439 * to regex prog. Only a few % characters are allowed.
440 */
441
442 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 * Get some space to modify the format string into.
444 */
445 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
446 for (round = FMT_PATTERNS; round > 0; )
447 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200448#ifdef BACKSLASH_IN_FILENAME
449 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200450#else
451 i += 2; /* "%f" can become two chars longer */
452#endif
453 if ((fmtstr = alloc(i)) == NULL)
454 goto parse_efm_error;
455
456 while (efm[0] != NUL)
457 {
458 /*
459 * Allocate a new eformat structure and put it at the end of the list
460 */
461 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
462 if (fmt_ptr == NULL)
463 goto parse_efm_error;
464 if (fmt_first == NULL) /* first one */
465 fmt_first = fmt_ptr;
466 else
467 fmt_last->next = fmt_ptr;
468 fmt_last = fmt_ptr;
469
470 /*
471 * Isolate one part in the 'errorformat' option
472 */
473 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
474 if (efm[len] == '\\' && efm[len + 1] != NUL)
475 ++len;
476
477 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
478 goto parse_efm_error;
479 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
480 goto parse_efm_error;
481 /*
482 * Advance to next part
483 */
484 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
485 }
486
487 if (fmt_first == NULL) /* nothing found */
488 EMSG(_("E378: 'errorformat' contains no pattern"));
489
490 goto parse_efm_end;
491
492parse_efm_error:
493 free_efm_list(&fmt_first);
494
495parse_efm_end:
496 vim_free(fmtstr);
497 vim_free(errmsg);
498
499 return fmt_first;
500}
501
Bram Moolenaare0d37972016-07-15 22:36:01 +0200502enum {
503 QF_FAIL = 0,
504 QF_OK = 1,
505 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200506 QF_NOMEM = 3,
507 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200508};
509
510typedef struct {
511 char_u *linebuf;
512 int linelen;
513 char_u *growbuf;
514 int growbufsiz;
515 FILE *fd;
516 typval_T *tv;
517 char_u *p_str;
518 listitem_T *p_li;
519 buf_T *buf;
520 linenr_T buflnum;
521 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100522 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200523} qfstate_T;
524
525 static char_u *
526qf_grow_linebuf(qfstate_T *state, int newsz)
527{
528 /*
529 * If the line exceeds LINE_MAXLEN exclude the last
530 * byte since it's not a NL character.
531 */
532 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
533 if (state->growbuf == NULL)
534 {
535 state->growbuf = alloc(state->linelen + 1);
536 if (state->growbuf == NULL)
537 return NULL;
538 state->growbufsiz = state->linelen;
539 }
540 else if (state->linelen > state->growbufsiz)
541 {
542 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
543 if (state->growbuf == NULL)
544 return NULL;
545 state->growbufsiz = state->linelen;
546 }
547 return state->growbuf;
548}
549
550/*
551 * Get the next string (separated by newline) from state->p_str.
552 */
553 static int
554qf_get_next_str_line(qfstate_T *state)
555{
556 /* Get the next line from the supplied string */
557 char_u *p_str = state->p_str;
558 char_u *p;
559 int len;
560
561 if (*p_str == NUL) /* Reached the end of the string */
562 return QF_END_OF_INPUT;
563
564 p = vim_strchr(p_str, '\n');
565 if (p != NULL)
566 len = (int)(p - p_str) + 1;
567 else
568 len = (int)STRLEN(p_str);
569
570 if (len > IOSIZE - 2)
571 {
572 state->linebuf = qf_grow_linebuf(state, len);
573 if (state->linebuf == NULL)
574 return QF_NOMEM;
575 }
576 else
577 {
578 state->linebuf = IObuff;
579 state->linelen = len;
580 }
581 vim_strncpy(state->linebuf, p_str, state->linelen);
582
583 /*
584 * Increment using len in order to discard the rest of the
585 * line if it exceeds LINE_MAXLEN.
586 */
587 p_str += len;
588 state->p_str = p_str;
589
590 return QF_OK;
591}
592
593/*
594 * Get the next string from state->p_Li.
595 */
596 static int
597qf_get_next_list_line(qfstate_T *state)
598{
599 listitem_T *p_li = state->p_li;
600 int len;
601
602 while (p_li != NULL
603 && (p_li->li_tv.v_type != VAR_STRING
604 || p_li->li_tv.vval.v_string == NULL))
605 p_li = p_li->li_next; /* Skip non-string items */
606
607 if (p_li == NULL) /* End of the list */
608 {
609 state->p_li = NULL;
610 return QF_END_OF_INPUT;
611 }
612
613 len = (int)STRLEN(p_li->li_tv.vval.v_string);
614 if (len > IOSIZE - 2)
615 {
616 state->linebuf = qf_grow_linebuf(state, len);
617 if (state->linebuf == NULL)
618 return QF_NOMEM;
619 }
620 else
621 {
622 state->linebuf = IObuff;
623 state->linelen = len;
624 }
625
626 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
627
628 state->p_li = p_li->li_next; /* next item */
629 return QF_OK;
630}
631
632/*
633 * Get the next string from state->buf.
634 */
635 static int
636qf_get_next_buf_line(qfstate_T *state)
637{
638 char_u *p_buf = NULL;
639 int len;
640
641 /* Get the next line from the supplied buffer */
642 if (state->buflnum > state->lnumlast)
643 return QF_END_OF_INPUT;
644
645 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
646 state->buflnum += 1;
647
648 len = (int)STRLEN(p_buf);
649 if (len > IOSIZE - 2)
650 {
651 state->linebuf = qf_grow_linebuf(state, len);
652 if (state->linebuf == NULL)
653 return QF_NOMEM;
654 }
655 else
656 {
657 state->linebuf = IObuff;
658 state->linelen = len;
659 }
660 vim_strncpy(state->linebuf, p_buf, state->linelen);
661
662 return QF_OK;
663}
664
665/*
666 * Get the next string from file state->fd.
667 */
668 static int
669qf_get_next_file_line(qfstate_T *state)
670{
671 int discard;
672 int growbuflen;
673
674 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
675 return QF_END_OF_INPUT;
676
677 discard = FALSE;
678 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200679 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200680 {
681 /*
682 * The current line exceeds IObuff, continue reading using
683 * growbuf until EOL or LINE_MAXLEN bytes is read.
684 */
685 if (state->growbuf == NULL)
686 {
687 state->growbufsiz = 2 * (IOSIZE - 1);
688 state->growbuf = alloc(state->growbufsiz);
689 if (state->growbuf == NULL)
690 return QF_NOMEM;
691 }
692
693 /* Copy the read part of the line, excluding null-terminator */
694 memcpy(state->growbuf, IObuff, IOSIZE - 1);
695 growbuflen = state->linelen;
696
697 for (;;)
698 {
699 if (fgets((char *)state->growbuf + growbuflen,
700 state->growbufsiz - growbuflen, state->fd) == NULL)
701 break;
702 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
703 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200704 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200705 break;
706 if (state->growbufsiz == LINE_MAXLEN)
707 {
708 discard = TRUE;
709 break;
710 }
711
712 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
713 ? 2 * state->growbufsiz : LINE_MAXLEN;
714 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
715 if (state->growbuf == NULL)
716 return QF_NOMEM;
717 }
718
719 while (discard)
720 {
721 /*
722 * The current line is longer than LINE_MAXLEN, continue
723 * reading but discard everything until EOL or EOF is
724 * reached.
725 */
726 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
727 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200728 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200729 break;
730 }
731
732 state->linebuf = state->growbuf;
733 state->linelen = growbuflen;
734 }
735 else
736 state->linebuf = IObuff;
737
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100738#ifdef FEAT_MBYTE
739 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200740 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
741 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100742 char_u *line;
743
744 line = string_convert(&state->vc, state->linebuf, &state->linelen);
745 if (line != NULL)
746 {
747 if (state->linelen < IOSIZE)
748 {
749 STRCPY(state->linebuf, line);
750 vim_free(line);
751 }
752 else
753 {
754 vim_free(state->growbuf);
755 state->linebuf = state->growbuf = line;
756 state->growbufsiz = state->linelen < LINE_MAXLEN
757 ? state->linelen : LINE_MAXLEN;
758 }
759 }
760 }
761#endif
762
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 return QF_OK;
764}
765
766/*
767 * Get the next string from a file/buffer/list/string.
768 */
769 static int
770qf_get_nextline(qfstate_T *state)
771{
772 int status = QF_FAIL;
773
774 if (state->fd == NULL)
775 {
776 if (state->tv != NULL)
777 {
778 if (state->tv->v_type == VAR_STRING)
779 /* Get the next line from the supplied string */
780 status = qf_get_next_str_line(state);
781 else if (state->tv->v_type == VAR_LIST)
782 /* Get the next line from the supplied list */
783 status = qf_get_next_list_line(state);
784 }
785 else
786 /* Get the next line from the supplied buffer */
787 status = qf_get_next_buf_line(state);
788 }
789 else
790 /* Get the next line from the supplied file */
791 status = qf_get_next_file_line(state);
792
793 if (status != QF_OK)
794 return status;
795
796 /* remove newline/CR from the line */
797 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200798 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799 state->linebuf[state->linelen - 1] = NUL;
800#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200801 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
802 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200803#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200804 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200805
806#ifdef FEAT_MBYTE
807 remove_bom(state->linebuf);
808#endif
809
810 return QF_OK;
811}
812
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200813typedef struct {
814 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200815 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200816 char_u *errmsg;
817 int errmsglen;
818 long lnum;
819 int col;
820 char_u use_viscol;
821 char_u *pattern;
822 int enr;
823 int type;
824 int valid;
825} qffields_T;
826
827/*
828 * Parse a line and get the quickfix fields.
829 * Return the QF_ status.
830 */
831 static int
832qf_parse_line(
833 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200834 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200835 char_u *linebuf,
836 int linelen,
837 efm_T *fmt_first,
838 qffields_T *fields)
839{
840 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200841 char_u *ptr;
842 int len;
843 int i;
844 int idx = 0;
845 char_u *tail = NULL;
846 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200847 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200848
849 /* Always ignore case when looking for a matching error. */
850 regmatch.rm_ic = TRUE;
851
Bram Moolenaare333e792018-04-08 13:27:39 +0200852restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200853 /* If there was no %> item start at the first pattern */
854 if (fmt_start == NULL)
855 fmt_ptr = fmt_first;
856 else
857 {
858 fmt_ptr = fmt_start;
859 fmt_start = NULL;
860 }
861
862 /*
863 * Try to match each part of 'errorformat' until we find a complete
864 * match or no match.
865 */
866 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200867 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
868 {
869 int r;
870
871 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200872 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200873 continue;
874 fields->namebuf[0] = NUL;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200875 fields->module[0] = NUL;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200876 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200877 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200878 fields->errmsg[0] = NUL;
879 fields->lnum = 0;
880 fields->col = 0;
881 fields->use_viscol = FALSE;
882 fields->enr = -1;
883 fields->type = 0;
884 tail = NULL;
885
886 regmatch.regprog = fmt_ptr->prog;
887 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
888 fmt_ptr->prog = regmatch.regprog;
889 if (r)
890 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200891 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200892 continue;
893 if (vim_strchr((char_u *)"EWI", idx) != NULL)
894 fields->type = idx;
895 else
896 fields->type = 0;
897 /*
898 * Extract error message data from matched line.
899 * We check for an actual submatch, because "\[" and "\]" in
900 * the 'errorformat' may cause the wrong submatch to be used.
901 */
902 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
903 {
904 int c;
905
906 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
907 continue;
908
909 /* Expand ~/file and $HOME/file to full path. */
910 c = *regmatch.endp[i];
911 *regmatch.endp[i] = NUL;
912 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
913 *regmatch.endp[i] = c;
914
915 if (vim_strchr((char_u *)"OPQ", idx) != NULL
916 && mch_getperm(fields->namebuf) == -1)
917 continue;
918 }
919 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
920 {
921 if (regmatch.startp[i] == NULL)
922 continue;
923 fields->enr = (int)atol((char *)regmatch.startp[i]);
924 }
925 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
926 {
927 if (regmatch.startp[i] == NULL)
928 continue;
929 fields->lnum = atol((char *)regmatch.startp[i]);
930 }
931 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
932 {
933 if (regmatch.startp[i] == NULL)
934 continue;
935 fields->col = (int)atol((char *)regmatch.startp[i]);
936 }
937 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
938 {
939 if (regmatch.startp[i] == NULL)
940 continue;
941 fields->type = *regmatch.startp[i];
942 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200943 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200944 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200945 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200946 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200947 /* linelen + null terminator */
948 if ((fields->errmsg = vim_realloc(fields->errmsg,
949 linelen + 1)) == NULL)
950 return QF_NOMEM;
951 fields->errmsglen = linelen + 1;
952 }
953 vim_strncpy(fields->errmsg, linebuf, linelen);
954 }
955 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
956 {
957 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
958 continue;
959 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200960 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200961 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200962 /* len + null terminator */
963 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
964 == NULL)
965 return QF_NOMEM;
966 fields->errmsglen = len + 1;
967 }
968 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
969 }
970 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
971 {
972 if (regmatch.startp[i] == NULL)
973 continue;
974 tail = regmatch.startp[i];
975 }
976 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
977 {
978 char_u *match_ptr;
979
980 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
981 continue;
982 fields->col = 0;
983 for (match_ptr = regmatch.startp[i];
984 match_ptr != regmatch.endp[i]; ++match_ptr)
985 {
986 ++fields->col;
987 if (*match_ptr == TAB)
988 {
989 fields->col += 7;
990 fields->col -= fields->col % 8;
991 }
992 }
993 ++fields->col;
994 fields->use_viscol = TRUE;
995 }
996 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
997 {
998 if (regmatch.startp[i] == NULL)
999 continue;
1000 fields->col = (int)atol((char *)regmatch.startp[i]);
1001 fields->use_viscol = TRUE;
1002 }
1003 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1004 {
1005 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1006 continue;
1007 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1008 if (len > CMDBUFFSIZE - 5)
1009 len = CMDBUFFSIZE - 5;
1010 STRCPY(fields->pattern, "^\\V");
1011 STRNCAT(fields->pattern, regmatch.startp[i], len);
1012 fields->pattern[len + 3] = '\\';
1013 fields->pattern[len + 4] = '$';
1014 fields->pattern[len + 5] = NUL;
1015 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001016 if ((i = (int)fmt_ptr->addr[10]) > 0) /* %o */
1017 {
1018 if (regmatch.startp[i] == NULL)
1019 continue;
1020 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1021 if (len > CMDBUFFSIZE)
1022 len = CMDBUFFSIZE;
1023 STRNCAT(fields->module, regmatch.startp[i], len);
1024 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001025 break;
1026 }
1027 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001028 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001029
1030 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1031 {
1032 if (fmt_ptr != NULL)
1033 {
1034 if (idx == 'D') /* enter directory */
1035 {
1036 if (*fields->namebuf == NUL)
1037 {
1038 EMSG(_("E379: Missing or empty directory name"));
1039 return QF_FAIL;
1040 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001041 qfl->qf_directory =
1042 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1043 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001044 return QF_FAIL;
1045 }
1046 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001047 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001048 }
1049 fields->namebuf[0] = NUL; /* no match found, remove file name */
1050 fields->lnum = 0; /* don't jump to this line */
1051 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001052 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001053 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001054 /* linelen + null terminator */
1055 if ((fields->errmsg = vim_realloc(fields->errmsg,
1056 linelen + 1)) == NULL)
1057 return QF_NOMEM;
1058 fields->errmsglen = linelen + 1;
1059 }
1060 /* copy whole line to error message */
1061 vim_strncpy(fields->errmsg, linebuf, linelen);
1062 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001063 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001064 }
1065 else if (fmt_ptr != NULL)
1066 {
1067 /* honor %> item */
1068 if (fmt_ptr->conthere)
1069 fmt_start = fmt_ptr;
1070
1071 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1072 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001073 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1074 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001075 }
1076 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1077 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001078 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001079 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001080 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001081
Bram Moolenaar9b457942016-10-09 16:10:05 +02001082 if (qfprev == NULL)
1083 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001084 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001085 {
1086 len = (int)STRLEN(qfprev->qf_text);
1087 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1088 == NULL)
1089 return QF_FAIL;
1090 STRCPY(ptr, qfprev->qf_text);
1091 vim_free(qfprev->qf_text);
1092 qfprev->qf_text = ptr;
1093 *(ptr += len) = '\n';
1094 STRCPY(++ptr, fields->errmsg);
1095 }
1096 if (qfprev->qf_nr == -1)
1097 qfprev->qf_nr = fields->enr;
1098 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1099 /* only printable chars allowed */
1100 qfprev->qf_type = fields->type;
1101
1102 if (!qfprev->qf_lnum)
1103 qfprev->qf_lnum = fields->lnum;
1104 if (!qfprev->qf_col)
1105 qfprev->qf_col = fields->col;
1106 qfprev->qf_viscol = fields->use_viscol;
1107 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001108 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1109 qfl->qf_directory,
1110 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001111 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001112 : qfl->qf_currfile != NULL && fields->valid
1113 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001114 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001115 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001116 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001117 line_breakcheck();
1118 return QF_IGNORE_LINE;
1119 }
1120 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1121 {
1122 /* global file names */
1123 fields->valid = FALSE;
1124 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1125 {
1126 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001127 qfl->qf_currfile =
1128 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001129 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001130 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001131 *fields->namebuf = NUL;
1132 if (tail && *tail)
1133 {
1134 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001135 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001136 goto restofline;
1137 }
1138 }
1139 }
1140 if (fmt_ptr->flags == '-') /* generally exclude this line */
1141 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001142 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001143 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001144 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001145 return QF_IGNORE_LINE;
1146 }
1147 }
1148
1149 return QF_OK;
1150}
1151
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001152/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001153 * Read the errorfile "efile" into memory, line by line, building the error
1154 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001155 * Alternative: when "efile" is NULL read errors from buffer "buf".
1156 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001157 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001158 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1159 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001160 * Return -1 for error, number of errors for success.
1161 */
1162 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001163qf_init_ext(
1164 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001165 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001166 char_u *efile,
1167 buf_T *buf,
1168 typval_T *tv,
1169 char_u *errorformat,
1170 int newlist, /* TRUE: start a new error list */
1171 linenr_T lnumfirst, /* first line number to use */
1172 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001173 char_u *qf_title,
1174 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001175{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001176 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001177 qfstate_T state;
1178 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001179 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001180 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001181 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001183 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001185 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001187 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001188 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001189
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001190 vim_memset(&state, 0, sizeof(state));
1191 vim_memset(&fields, 0, sizeof(fields));
1192#ifdef FEAT_MBYTE
1193 state.vc.vc_type = CONV_NONE;
1194 if (enc != NULL && *enc != NUL)
1195 convert_setup(&state.vc, enc, p_enc);
1196#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001197 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaard76ce852018-05-01 15:02:04 +02001198 fields.module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001199 fields.errmsglen = CMDBUFFSIZE + 1;
1200 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1201 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02001202 if (fields.namebuf == NULL || fields.errmsg == NULL
Bram Moolenaard76ce852018-05-01 15:02:04 +02001203 || fields.pattern == NULL || fields.module == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 goto qf_init_end;
1205
Bram Moolenaare0d37972016-07-15 22:36:01 +02001206 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
1208 EMSG2(_(e_openerrf), efile);
1209 goto qf_init_end;
1210 }
1211
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001212 if (newlist || qf_idx == qi->qf_listcount)
1213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001215 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001216 qf_idx = qi->qf_curlist;
1217 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001218 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001219 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001220 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001221 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001222 if (qi->qf_lists[qf_idx].qf_count > 0)
1223 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001226 qfl = &qi->qf_lists[qf_idx];
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001229 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001230 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 else
1232 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001234 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001235 * If the errorformat didn't change between calls, then reuse the
1236 * previously parsed values.
1237 */
1238 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1239 {
1240 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001241 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001242 free_efm_list(&fmt_first);
1243
1244 /* parse the current 'efm' */
1245 fmt_first = parse_efm_option(efm);
1246 if (fmt_first != NULL)
1247 last_efm = vim_strsave(efm);
1248 }
1249
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
1253 /*
1254 * got_int is reset here, because it was probably set when killing the
1255 * ":make" command, but we still want to read the errorfile then.
1256 */
1257 got_int = FALSE;
1258
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001259 if (tv != NULL)
1260 {
1261 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001262 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001263 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001264 state.p_li = tv->vval.v_list->lv_first;
1265 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001266 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001267 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001268 state.buflnum = lnumfirst;
1269 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001270
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 /*
1272 * Read the lines in the error file one by one.
1273 * Try to recognize one of the error formats in each line.
1274 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001275 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001277 /* Get the next line from a file/buffer/list/string */
1278 status = qf_get_nextline(&state);
1279 if (status == QF_NOMEM) /* memory alloc failure */
1280 goto qf_init_end;
1281 if (status == QF_END_OF_INPUT) /* end of input */
1282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001284 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1285 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001286 if (status == QF_FAIL)
1287 goto error2;
1288 if (status == QF_NOMEM)
1289 goto qf_init_end;
1290 if (status == QF_IGNORE_LINE)
1291 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001293 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001294 qf_idx,
1295 qfl->qf_directory,
1296 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001297 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001298 : ((qfl->qf_currfile != NULL && fields.valid)
1299 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001300 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001301 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001302 fields.errmsg,
1303 fields.lnum,
1304 fields.col,
1305 fields.use_viscol,
1306 fields.pattern,
1307 fields.enr,
1308 fields.type,
1309 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 goto error2;
1311 line_breakcheck();
1312 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001313 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001315 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001317 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001318 qfl->qf_ptr = qfl->qf_start;
1319 qfl->qf_index = 1;
1320 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 else
1323 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001324 qfl->qf_nonevalid = FALSE;
1325 if (qfl->qf_ptr == NULL)
1326 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001328 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001329 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001330 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 EMSG(_(e_readerrf));
1333error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001334 if (!adding)
1335 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001336 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001337 qf_free(qi, qi->qf_curlist);
1338 qi->qf_listcount--;
1339 if (qi->qf_curlist > 0)
1340 --qi->qf_curlist;
1341 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001342qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001343 if (state.fd != NULL)
1344 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001345 vim_free(fields.namebuf);
Bram Moolenaard76ce852018-05-01 15:02:04 +02001346 vim_free(fields.module);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001347 vim_free(fields.errmsg);
1348 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001349 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001351 if (qf_idx == qi->qf_curlist)
1352 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001353#ifdef FEAT_MBYTE
1354 if (state.vc.vc_type != CONV_NONE)
1355 convert_setup(&state.vc, NULL, NULL);
1356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
1358 return retval;
1359}
1360
Bram Moolenaarfb604092014-07-23 15:55:00 +02001361 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001362qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001363{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001364 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001365
Bram Moolenaarfb604092014-07-23 15:55:00 +02001366 if (title != NULL)
1367 {
1368 char_u *p = alloc((int)STRLEN(title) + 2);
1369
Bram Moolenaara3921f42017-06-04 15:30:34 +02001370 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001371 if (p != NULL)
1372 sprintf((char *)p, ":%s", (char *)title);
1373 }
1374}
1375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001377 * Prepare for adding a new quickfix list. If the current list is in the
1378 * middle of the stack, then all the following lists are freed and then
1379 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 */
1381 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001382qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
1384 int i;
1385
1386 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001387 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 * the current entry. This makes it possible to browse in a tree-like
1389 * way with ":grep'.
1390 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001391 while (qi->qf_listcount > qi->qf_curlist + 1)
1392 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
1394 /*
1395 * When the stack is full, remove to oldest entry
1396 * Otherwise, add a new entry.
1397 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001398 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001400 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001402 qi->qf_lists[i - 1] = qi->qf_lists[i];
1403 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001406 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001407 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001408 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001409 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410}
1411
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001412/*
1413 * Free a location list
1414 */
1415 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001416ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001417{
1418 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001419 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001420
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001421 qi = *pqi;
1422 if (qi == NULL)
1423 return;
1424 *pqi = NULL; /* Remove reference to this list */
1425
1426 qi->qf_refcount--;
1427 if (qi->qf_refcount < 1)
1428 {
1429 /* No references to this location list */
1430 for (i = 0; i < qi->qf_listcount; ++i)
1431 qf_free(qi, i);
1432 vim_free(qi);
1433 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001434}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001435
1436 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001437qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001438{
1439 int i;
1440 qf_info_T *qi = &ql_info;
1441
1442 if (wp != NULL)
1443 {
1444 /* location list */
1445 ll_free_all(&wp->w_llist);
1446 ll_free_all(&wp->w_llist_ref);
1447 }
1448 else
1449 /* quickfix list */
1450 for (i = 0; i < qi->qf_listcount; ++i)
1451 qf_free(qi, i);
1452}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454/*
1455 * Add an entry to the end of the list of errors.
1456 * Returns OK or FAIL.
1457 */
1458 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001459qf_add_entry(
1460 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001461 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001462 char_u *dir, /* optional directory name */
1463 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001464 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001465 int bufnum, /* buffer number or zero */
1466 char_u *mesg, /* message */
1467 long lnum, /* line number */
1468 int col, /* column */
1469 int vis_col, /* using visual column */
1470 char_u *pattern, /* search pattern */
1471 int nr, /* error number */
1472 int type, /* type character */
1473 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001475 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001476 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001478 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001480 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001481 {
1482 buf_T *buf = buflist_findnr(bufnum);
1483
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001484 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001485 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001486 buf->b_has_qf_entry |=
1487 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001488 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001489 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001490 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1492 {
1493 vim_free(qfp);
1494 return FAIL;
1495 }
1496 qfp->qf_lnum = lnum;
1497 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001498 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001499 if (pattern == NULL || *pattern == NUL)
1500 qfp->qf_pattern = NULL;
1501 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1502 {
1503 vim_free(qfp->qf_text);
1504 vim_free(qfp);
1505 return FAIL;
1506 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001507 if (module == NULL || *module == NUL)
1508 qfp->qf_module = NULL;
1509 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1510 {
1511 vim_free(qfp->qf_text);
1512 vim_free(qfp->qf_pattern);
1513 vim_free(qfp);
1514 return FAIL;
1515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 qfp->qf_nr = nr;
1517 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1518 type = 0;
1519 qfp->qf_type = type;
1520 qfp->qf_valid = valid;
1521
Bram Moolenaara3921f42017-06-04 15:30:34 +02001522 lastp = &qi->qf_lists[qf_idx].qf_last;
1523 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001524 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001526 qi->qf_lists[qf_idx].qf_start = qfp;
1527 qi->qf_lists[qf_idx].qf_ptr = qfp;
1528 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001529 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 }
1531 else
1532 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001533 qfp->qf_prev = *lastp;
1534 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001536 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001538 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001539 ++qi->qf_lists[qf_idx].qf_count;
1540 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001541 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001543 qi->qf_lists[qf_idx].qf_index =
1544 qi->qf_lists[qf_idx].qf_count;
1545 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
1547
1548 return OK;
1549}
1550
1551/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001552 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001553 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001554 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001555ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001556{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001557 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001558
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001559 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1560 if (qi != NULL)
1561 {
1562 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1563 qi->qf_refcount++;
1564 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001565
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001566 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001567}
1568
1569/*
1570 * Return the location list for window 'wp'.
1571 * If not present, allocate a location list
1572 */
1573 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001574ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001575{
1576 if (IS_LL_WINDOW(wp))
1577 /* For a location list window, use the referenced location list */
1578 return wp->w_llist_ref;
1579
1580 /*
1581 * For a non-location list window, w_llist_ref should not point to a
1582 * location list.
1583 */
1584 ll_free_all(&wp->w_llist_ref);
1585
1586 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001587 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001588 return wp->w_llist;
1589}
1590
1591/*
1592 * Copy the location list from window "from" to window "to".
1593 */
1594 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001595copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001596{
1597 qf_info_T *qi;
1598 int idx;
1599 int i;
1600
1601 /*
1602 * When copying from a location list window, copy the referenced
1603 * location list. For other windows, copy the location list for
1604 * that window.
1605 */
1606 if (IS_LL_WINDOW(from))
1607 qi = from->w_llist_ref;
1608 else
1609 qi = from->w_llist;
1610
1611 if (qi == NULL) /* no location list to copy */
1612 return;
1613
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001614 /* allocate a new location list */
1615 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001616 return;
1617
1618 to->w_llist->qf_listcount = qi->qf_listcount;
1619
1620 /* Copy the location lists one at a time */
1621 for (idx = 0; idx < qi->qf_listcount; idx++)
1622 {
1623 qf_list_T *from_qfl;
1624 qf_list_T *to_qfl;
1625
1626 to->w_llist->qf_curlist = idx;
1627
1628 from_qfl = &qi->qf_lists[idx];
1629 to_qfl = &to->w_llist->qf_lists[idx];
1630
1631 /* Some of the fields are populated by qf_add_entry() */
1632 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1633 to_qfl->qf_count = 0;
1634 to_qfl->qf_index = 0;
1635 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001636 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001637 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001638 if (from_qfl->qf_title != NULL)
1639 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1640 else
1641 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001642 if (from_qfl->qf_ctx != NULL)
1643 {
1644 to_qfl->qf_ctx = alloc_tv();
1645 if (to_qfl->qf_ctx != NULL)
1646 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1647 }
1648 else
1649 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001650
1651 if (from_qfl->qf_count)
1652 {
1653 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001654 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001655
1656 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001657 for (i = 0, from_qfp = from_qfl->qf_start;
1658 i < from_qfl->qf_count && from_qfp != NULL;
1659 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001660 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001661 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001662 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001663 NULL,
1664 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02001665 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001666 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001667 from_qfp->qf_text,
1668 from_qfp->qf_lnum,
1669 from_qfp->qf_col,
1670 from_qfp->qf_viscol,
1671 from_qfp->qf_pattern,
1672 from_qfp->qf_nr,
1673 0,
1674 from_qfp->qf_valid) == FAIL)
1675 {
1676 qf_free_all(to);
1677 return;
1678 }
1679 /*
1680 * qf_add_entry() will not set the qf_num field, as the
1681 * directory and file names are not supplied. So the qf_fnum
1682 * field is copied here.
1683 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001684 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001685 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1686 prevp->qf_type = from_qfp->qf_type; /* error type */
1687 if (from_qfl->qf_ptr == from_qfp)
1688 to_qfl->qf_ptr = prevp; /* current location */
1689 }
1690 }
1691
1692 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1693
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001694 /* Assign a new ID for the location list */
1695 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001696 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001697
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001698 /* When no valid entries are present in the list, qf_ptr points to
1699 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001700 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001701 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001702 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001703 to_qfl->qf_index = 1;
1704 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001705 }
1706
1707 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1708}
1709
1710/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001711 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001712 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 */
1714 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001715qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
Bram Moolenaar82404332016-07-10 17:00:38 +02001717 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001718 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001719 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (fname == NULL || *fname == NUL) /* no file name */
1722 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaare60acc12011-05-10 16:41:25 +02001724#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001725 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001726#endif
1727#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001728 if (directory != NULL)
1729 slash_adjust(directory);
1730 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001731#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001732 if (directory != NULL && !vim_isAbsName(fname)
1733 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1734 {
1735 /*
1736 * Here we check if the file really exists.
1737 * This should normally be true, but if make works without
1738 * "leaving directory"-messages we might have missed a
1739 * directory change.
1740 */
1741 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001744 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001745 if (directory)
1746 ptr = concat_fnames(directory, fname, TRUE);
1747 else
1748 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001750 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001751 bufname = ptr;
1752 }
1753 else
1754 bufname = fname;
1755
1756 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001757 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001758 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001759 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001760 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001762 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001763 {
1764 vim_free(qf_last_bufname);
1765 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1766 if (bufname == ptr)
1767 qf_last_bufname = bufname;
1768 else
1769 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001770 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001771 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001772 if (buf == NULL)
1773 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001774
Bram Moolenaarc1542742016-07-20 21:44:37 +02001775 buf->b_has_qf_entry =
1776 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001777 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778}
1779
1780/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001781 * Push dirbuf onto the directory stack and return pointer to actual dir or
1782 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 */
1784 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001785qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 struct dir_stack_T *ds_new;
1788 struct dir_stack_T *ds_ptr;
1789
1790 /* allocate new stack element and hook it in */
1791 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1792 if (ds_new == NULL)
1793 return NULL;
1794
1795 ds_new->next = *stackptr;
1796 *stackptr = ds_new;
1797
1798 /* store directory on the stack */
1799 if (vim_isAbsName(dirbuf)
1800 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001801 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 (*stackptr)->dirname = vim_strsave(dirbuf);
1803 else
1804 {
1805 /* Okay we don't have an absolute path.
1806 * dirbuf must be a subdir of one of the directories on the stack.
1807 * Let's search...
1808 */
1809 ds_new = (*stackptr)->next;
1810 (*stackptr)->dirname = NULL;
1811 while (ds_new)
1812 {
1813 vim_free((*stackptr)->dirname);
1814 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1815 TRUE);
1816 if (mch_isdir((*stackptr)->dirname) == TRUE)
1817 break;
1818
1819 ds_new = ds_new->next;
1820 }
1821
1822 /* clean up all dirs we already left */
1823 while ((*stackptr)->next != ds_new)
1824 {
1825 ds_ptr = (*stackptr)->next;
1826 (*stackptr)->next = (*stackptr)->next->next;
1827 vim_free(ds_ptr->dirname);
1828 vim_free(ds_ptr);
1829 }
1830
1831 /* Nothing found -> it must be on top level */
1832 if (ds_new == NULL)
1833 {
1834 vim_free((*stackptr)->dirname);
1835 (*stackptr)->dirname = vim_strsave(dirbuf);
1836 }
1837 }
1838
1839 if ((*stackptr)->dirname != NULL)
1840 return (*stackptr)->dirname;
1841 else
1842 {
1843 ds_ptr = *stackptr;
1844 *stackptr = (*stackptr)->next;
1845 vim_free(ds_ptr);
1846 return NULL;
1847 }
1848}
1849
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850/*
1851 * pop dirbuf from the directory stack and return previous directory or NULL if
1852 * stack is empty
1853 */
1854 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001855qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856{
1857 struct dir_stack_T *ds_ptr;
1858
1859 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1860 * What to do if it isn't? */
1861
1862 /* pop top element and free it */
1863 if (*stackptr != NULL)
1864 {
1865 ds_ptr = *stackptr;
1866 *stackptr = (*stackptr)->next;
1867 vim_free(ds_ptr->dirname);
1868 vim_free(ds_ptr);
1869 }
1870
1871 /* return NEW top element as current dir or NULL if stack is empty*/
1872 return *stackptr ? (*stackptr)->dirname : NULL;
1873}
1874
1875/*
1876 * clean up directory stack
1877 */
1878 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001879qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
1881 struct dir_stack_T *ds_ptr;
1882
1883 while ((ds_ptr = *stackptr) != NULL)
1884 {
1885 *stackptr = (*stackptr)->next;
1886 vim_free(ds_ptr->dirname);
1887 vim_free(ds_ptr);
1888 }
1889}
1890
1891/*
1892 * Check in which directory of the directory stack the given file can be
1893 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001894 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 * Cleans up intermediate directory entries.
1896 *
1897 * TODO: How to solve the following problem?
1898 * If we have the this directory tree:
1899 * ./
1900 * ./aa
1901 * ./aa/bb
1902 * ./bb
1903 * ./bb/x.c
1904 * and make says:
1905 * making all in aa
1906 * making all in bb
1907 * x.c:9: Error
1908 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1909 * qf_guess_filepath will return NULL.
1910 */
1911 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001912qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913{
1914 struct dir_stack_T *ds_ptr;
1915 struct dir_stack_T *ds_tmp;
1916 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001917 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
1919 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001920 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 return NULL;
1922
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001923 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 fullname = NULL;
1925 while (ds_ptr)
1926 {
1927 vim_free(fullname);
1928 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1929
1930 /* If concat_fnames failed, just go on. The worst thing that can happen
1931 * is that we delete the entire stack.
1932 */
1933 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1934 break;
1935
1936 ds_ptr = ds_ptr->next;
1937 }
1938
1939 vim_free(fullname);
1940
1941 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001942 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001944 ds_tmp = qfl->qf_dir_stack->next;
1945 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 vim_free(ds_tmp->dirname);
1947 vim_free(ds_tmp);
1948 }
1949
1950 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951}
1952
1953/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01001954 * Returns TRUE if a quickfix/location list with the given identifier exists.
1955 */
1956 static int
1957qflist_valid (win_T *wp, int_u qf_id)
1958{
1959 qf_info_T *qi = &ql_info;
1960 int i;
1961
1962 if (wp != NULL)
1963 {
1964 qi = GET_LOC_LIST(wp); /* Location list */
1965 if (qi == NULL)
1966 return FALSE;
1967 }
1968
1969 for (i = 0; i < qi->qf_listcount; ++i)
1970 if (qi->qf_lists[i].qf_id == qf_id)
1971 return TRUE;
1972
1973 return FALSE;
1974}
1975
1976/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001977 * When loading a file from the quickfix, the auto commands may modify it.
1978 * This may invalidate the current quickfix entry. This function checks
1979 * whether a entry is still present in the quickfix.
1980 * Similar to location list.
1981 */
1982 static int
1983is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1984{
1985 qf_list_T *qfl;
1986 qfline_T *qfp;
1987 int i;
1988
1989 qfl = &qi->qf_lists[qi->qf_curlist];
1990
1991 /* Search for the entry in the current list */
1992 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1993 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001994 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001995 break;
1996
1997 if (i == qfl->qf_count) /* Entry is not found */
1998 return FALSE;
1999
2000 return TRUE;
2001}
2002
2003/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002004 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002005 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002006 */
2007 static qfline_T *
2008get_next_valid_entry(
2009 qf_info_T *qi,
2010 qfline_T *qf_ptr,
2011 int *qf_index,
2012 int dir)
2013{
2014 int idx;
2015 int old_qf_fnum;
2016
2017 idx = *qf_index;
2018 old_qf_fnum = qf_ptr->qf_fnum;
2019
2020 do
2021 {
2022 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2023 || qf_ptr->qf_next == NULL)
2024 return NULL;
2025 ++idx;
2026 qf_ptr = qf_ptr->qf_next;
2027 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2028 && !qf_ptr->qf_valid)
2029 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2030
2031 *qf_index = idx;
2032 return qf_ptr;
2033}
2034
2035/*
2036 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002037 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002038 */
2039 static qfline_T *
2040get_prev_valid_entry(
2041 qf_info_T *qi,
2042 qfline_T *qf_ptr,
2043 int *qf_index,
2044 int dir)
2045{
2046 int idx;
2047 int old_qf_fnum;
2048
2049 idx = *qf_index;
2050 old_qf_fnum = qf_ptr->qf_fnum;
2051
2052 do
2053 {
2054 if (idx == 1 || qf_ptr->qf_prev == NULL)
2055 return NULL;
2056 --idx;
2057 qf_ptr = qf_ptr->qf_prev;
2058 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2059 && !qf_ptr->qf_valid)
2060 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2061
2062 *qf_index = idx;
2063 return qf_ptr;
2064}
2065
2066/*
2067 * Get the n'th (errornr) previous/next valid entry from the current entry in
2068 * the quickfix list.
2069 * dir == FORWARD or FORWARD_FILE: next valid entry
2070 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2071 */
2072 static qfline_T *
2073get_nth_valid_entry(
2074 qf_info_T *qi,
2075 int errornr,
2076 qfline_T *qf_ptr,
2077 int *qf_index,
2078 int dir)
2079{
2080 qfline_T *prev_qf_ptr;
2081 int prev_index;
2082 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2083 char_u *err = e_no_more_items;
2084
2085 while (errornr--)
2086 {
2087 prev_qf_ptr = qf_ptr;
2088 prev_index = *qf_index;
2089
2090 if (dir == FORWARD || dir == FORWARD_FILE)
2091 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2092 else
2093 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2094 if (qf_ptr == NULL)
2095 {
2096 qf_ptr = prev_qf_ptr;
2097 *qf_index = prev_index;
2098 if (err != NULL)
2099 {
2100 EMSG(_(err));
2101 return NULL;
2102 }
2103 break;
2104 }
2105
2106 err = NULL;
2107 }
2108
2109 return qf_ptr;
2110}
2111
2112/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002113 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002114 */
2115 static qfline_T *
2116get_nth_entry(
2117 qf_info_T *qi,
2118 int errornr,
2119 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002120 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002121{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002122 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002123
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002124 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002125 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2126 {
2127 --qf_idx;
2128 qf_ptr = qf_ptr->qf_prev;
2129 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002130 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002131 while (errornr > qf_idx &&
2132 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2133 qf_ptr->qf_next != NULL)
2134 {
2135 ++qf_idx;
2136 qf_ptr = qf_ptr->qf_next;
2137 }
2138
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002139 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002140 return qf_ptr;
2141}
2142
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002143/*
2144 * Find a help window or open one.
2145 */
2146 static int
2147jump_to_help_window(qf_info_T *qi, int *opened_window)
2148{
2149 win_T *wp;
2150 int flags;
2151
2152 if (cmdmod.tab != 0)
2153 wp = NULL;
2154 else
2155 FOR_ALL_WINDOWS(wp)
2156 if (bt_help(wp->w_buffer))
2157 break;
2158 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2159 win_enter(wp, TRUE);
2160 else
2161 {
2162 /*
2163 * Split off help window; put it at far top if no position
2164 * specified, the current window is vertically split and narrow.
2165 */
2166 flags = WSP_HELP;
2167 if (cmdmod.split == 0 && curwin->w_width != Columns
2168 && curwin->w_width < 80)
2169 flags |= WSP_TOP;
2170 if (qi != &ql_info)
2171 flags |= WSP_NEWLOC; /* don't copy the location list */
2172
2173 if (win_split(0, flags) == FAIL)
2174 return FAIL;
2175
2176 *opened_window = TRUE;
2177
2178 if (curwin->w_height < p_hh)
2179 win_setheight((int)p_hh);
2180
2181 if (qi != &ql_info) /* not a quickfix list */
2182 {
2183 /* The new window should use the supplied location list */
2184 curwin->w_llist = qi;
2185 qi->qf_refcount++;
2186 }
2187 }
2188
2189 if (!p_im)
2190 restart_edit = 0; /* don't want insert mode in help file */
2191
2192 return OK;
2193}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002194
2195/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002196 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2197 * If the file is already opened in a window, jump to it.
2198 */
2199 static int
2200qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2201{
2202 win_T *usable_win_ptr = NULL;
2203 int usable_win;
2204 qf_info_T *ll_ref;
2205 int flags;
2206 win_T *win;
2207 win_T *altwin;
2208
2209 usable_win = 0;
2210
2211 ll_ref = curwin->w_llist_ref;
2212 if (ll_ref != NULL)
2213 {
2214 /* Find a window using the same location list that is not a
2215 * quickfix window. */
2216 FOR_ALL_WINDOWS(usable_win_ptr)
2217 if (usable_win_ptr->w_llist == ll_ref
2218 && !bt_quickfix(usable_win_ptr->w_buffer))
2219 {
2220 usable_win = 1;
2221 break;
2222 }
2223 }
2224
2225 if (!usable_win)
2226 {
2227 /* Locate a window showing a normal buffer */
2228 FOR_ALL_WINDOWS(win)
2229 if (win->w_buffer->b_p_bt[0] == NUL)
2230 {
2231 usable_win = 1;
2232 break;
2233 }
2234 }
2235
2236 /*
2237 * If no usable window is found and 'switchbuf' contains "usetab"
2238 * then search in other tabs.
2239 */
2240 if (!usable_win && (swb_flags & SWB_USETAB))
2241 {
2242 tabpage_T *tp;
2243 win_T *wp;
2244
2245 FOR_ALL_TAB_WINDOWS(tp, wp)
2246 {
2247 if (wp->w_buffer->b_fnum == qf_fnum)
2248 {
2249 goto_tabpage_win(tp, wp);
2250 usable_win = 1;
2251 goto win_found;
2252 }
2253 }
2254 }
2255win_found:
2256
2257 /*
2258 * If there is only one window and it is the quickfix window, create a
2259 * new one above the quickfix window.
2260 */
2261 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2262 {
2263 flags = WSP_ABOVE;
2264 if (ll_ref != NULL)
2265 flags |= WSP_NEWLOC;
2266 if (win_split(0, flags) == FAIL)
2267 return FAIL; /* not enough room for window */
2268 *opened_window = TRUE; /* close it when fail */
2269 p_swb = empty_option; /* don't split again */
2270 swb_flags = 0;
2271 RESET_BINDING(curwin);
2272 if (ll_ref != NULL)
2273 {
2274 /* The new window should use the location list from the
2275 * location list window */
2276 curwin->w_llist = ll_ref;
2277 ll_ref->qf_refcount++;
2278 }
2279 }
2280 else
2281 {
2282 if (curwin->w_llist_ref != NULL)
2283 {
2284 /* In a location window */
2285 win = usable_win_ptr;
2286 if (win == NULL)
2287 {
2288 /* Find the window showing the selected file */
2289 FOR_ALL_WINDOWS(win)
2290 if (win->w_buffer->b_fnum == qf_fnum)
2291 break;
2292 if (win == NULL)
2293 {
2294 /* Find a previous usable window */
2295 win = curwin;
2296 do
2297 {
2298 if (win->w_buffer->b_p_bt[0] == NUL)
2299 break;
2300 if (win->w_prev == NULL)
2301 win = lastwin; /* wrap around the top */
2302 else
2303 win = win->w_prev; /* go to previous window */
2304 } while (win != curwin);
2305 }
2306 }
2307 win_goto(win);
2308
2309 /* If the location list for the window is not set, then set it
2310 * to the location list from the location window */
2311 if (win->w_llist == NULL)
2312 {
2313 win->w_llist = ll_ref;
2314 ll_ref->qf_refcount++;
2315 }
2316 }
2317 else
2318 {
2319
2320 /*
2321 * Try to find a window that shows the right buffer.
2322 * Default to the window just above the quickfix buffer.
2323 */
2324 win = curwin;
2325 altwin = NULL;
2326 for (;;)
2327 {
2328 if (win->w_buffer->b_fnum == qf_fnum)
2329 break;
2330 if (win->w_prev == NULL)
2331 win = lastwin; /* wrap around the top */
2332 else
2333 win = win->w_prev; /* go to previous window */
2334
2335 if (IS_QF_WINDOW(win))
2336 {
2337 /* Didn't find it, go to the window before the quickfix
2338 * window. */
2339 if (altwin != NULL)
2340 win = altwin;
2341 else if (curwin->w_prev != NULL)
2342 win = curwin->w_prev;
2343 else
2344 win = curwin->w_next;
2345 break;
2346 }
2347
2348 /* Remember a usable window. */
2349 if (altwin == NULL && !win->w_p_pvw
2350 && win->w_buffer->b_p_bt[0] == NUL)
2351 altwin = win;
2352 }
2353
2354 win_goto(win);
2355 }
2356 }
2357
2358 return OK;
2359}
2360
2361/*
2362 * Edit the selected file or help file.
2363 */
2364 static int
2365qf_jump_edit_buffer(
2366 qf_info_T *qi,
2367 qfline_T *qf_ptr,
2368 int forceit,
2369 win_T *oldwin,
2370 int *opened_window,
2371 int *abort)
2372{
2373 int retval = OK;
2374
2375 if (qf_ptr->qf_type == 1)
2376 {
2377 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2378 * set b_p_ro flag). */
2379 if (!can_abandon(curbuf, forceit))
2380 {
2381 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002382 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002383 }
2384 else
2385 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2386 ECMD_HIDE + ECMD_SET_HELP,
2387 oldwin == curwin ? curwin : NULL);
2388 }
2389 else
2390 {
2391 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002392 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002393
2394 retval = buflist_getfile(qf_ptr->qf_fnum,
2395 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002396
2397 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002398 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002399 /*
2400 * Location list. Check whether the associated window is still
2401 * present and the list is still valid.
2402 */
2403 if (!win_valid_any_tab(oldwin))
2404 {
2405 EMSG(_("E924: Current window was closed"));
2406 *abort = TRUE;
2407 *opened_window = FALSE;
2408 }
2409 else if (!qflist_valid(oldwin, save_qfid))
2410 {
2411 EMSG(_(e_loc_list_changed));
2412 *abort = TRUE;
2413 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002414 }
2415 else if (old_qf_curlist != qi->qf_curlist
2416 || !is_qf_entry_present(qi, qf_ptr))
2417 {
2418 if (qi == &ql_info)
2419 EMSG(_("E925: Current quickfix was changed"));
2420 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002421 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002422 *abort = TRUE;
2423 }
2424
2425 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002426 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002427 }
2428
2429 return retval;
2430}
2431
2432/*
2433 * Goto the error line in the current file using either line/column number or a
2434 * search pattern.
2435 */
2436 static void
2437qf_jump_goto_line(
2438 linenr_T qf_lnum,
2439 int qf_col,
2440 char_u qf_viscol,
2441 char_u *qf_pattern)
2442{
2443 linenr_T i;
2444 char_u *line;
2445 colnr_T screen_col;
2446 colnr_T char_col;
2447
2448 if (qf_pattern == NULL)
2449 {
2450 /*
2451 * Go to line with error, unless qf_lnum is 0.
2452 */
2453 i = qf_lnum;
2454 if (i > 0)
2455 {
2456 if (i > curbuf->b_ml.ml_line_count)
2457 i = curbuf->b_ml.ml_line_count;
2458 curwin->w_cursor.lnum = i;
2459 }
2460 if (qf_col > 0)
2461 {
2462 curwin->w_cursor.col = qf_col - 1;
2463#ifdef FEAT_VIRTUALEDIT
2464 curwin->w_cursor.coladd = 0;
2465#endif
2466 if (qf_viscol == TRUE)
2467 {
2468 /*
2469 * Check each character from the beginning of the error
2470 * line up to the error column. For each tab character
2471 * found, reduce the error column value by the length of
2472 * a tab character.
2473 */
2474 line = ml_get_curline();
2475 screen_col = 0;
2476 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2477 {
2478 if (*line == NUL)
2479 break;
2480 if (*line++ == '\t')
2481 {
2482 curwin->w_cursor.col -= 7 - (screen_col % 8);
2483 screen_col += 8 - (screen_col % 8);
2484 }
2485 else
2486 ++screen_col;
2487 }
2488 }
2489 check_cursor();
2490 }
2491 else
2492 beginline(BL_WHITE | BL_FIX);
2493 }
2494 else
2495 {
2496 pos_T save_cursor;
2497
2498 /* Move the cursor to the first line in the buffer */
2499 save_cursor = curwin->w_cursor;
2500 curwin->w_cursor.lnum = 0;
2501 if (!do_search(NULL, '/', qf_pattern, (long)1,
2502 SEARCH_KEEP, NULL, NULL))
2503 curwin->w_cursor = save_cursor;
2504 }
2505}
2506
2507/*
2508 * Display quickfix list index and size message
2509 */
2510 static void
2511qf_jump_print_msg(
2512 qf_info_T *qi,
2513 int qf_index,
2514 qfline_T *qf_ptr,
2515 buf_T *old_curbuf,
2516 linenr_T old_lnum)
2517{
2518 linenr_T i;
2519 int len;
2520
2521 /* Update the screen before showing the message, unless the screen
2522 * scrolled up. */
2523 if (!msg_scrolled)
2524 update_topline_redraw();
2525 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2526 qi->qf_lists[qi->qf_curlist].qf_count,
2527 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2528 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2529 /* Add the message, skipping leading whitespace and newlines. */
2530 len = (int)STRLEN(IObuff);
2531 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2532
2533 /* Output the message. Overwrite to avoid scrolling when the 'O'
2534 * flag is present in 'shortmess'; But when not jumping, print the
2535 * whole message. */
2536 i = msg_scroll;
2537 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2538 msg_scroll = TRUE;
2539 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2540 msg_scroll = FALSE;
2541 msg_attr_keep(IObuff, 0, TRUE);
2542 msg_scroll = i;
2543}
2544
2545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 * jump to a quickfix line
2547 * if dir == FORWARD go "errornr" valid entries forward
2548 * if dir == BACKWARD go "errornr" valid entries backward
2549 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2550 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2551 * else if "errornr" is zero, redisplay the same line
2552 * else go to entry "errornr"
2553 */
2554 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002555qf_jump(qf_info_T *qi,
2556 int dir,
2557 int errornr,
2558 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002560 qfline_T *qf_ptr;
2561 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 buf_T *old_curbuf;
2565 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002566 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002567 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002569 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#ifdef FEAT_FOLDING
2572 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2573#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002574 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002576 if (qi == NULL)
2577 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002578
2579 if (qi->qf_curlist >= qi->qf_listcount
2580 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
2582 EMSG(_(e_quickfix));
2583 return;
2584 }
2585
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002586 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002588 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002590 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002592 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2593 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002595 qf_ptr = old_qf_ptr;
2596 qf_index = old_qf_index;
2597 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599 }
2600 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002601 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002603 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2604 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 /* No need to print the error message if it's visible in the error
2606 * window */
2607 print_message = FALSE;
2608
2609 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002610 * For ":helpgrep" find a help window or open one.
2611 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002612 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002613 if (jump_to_help_window(qi, &opened_window) == FAIL)
2614 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002615
2616 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 * If currently in the quickfix window, find another window to show the
2618 * file in.
2619 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002620 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {
2622 /*
2623 * If there is no file specified, we don't know where to go.
2624 * But do advance, otherwise ":cn" gets stuck.
2625 */
2626 if (qf_ptr->qf_fnum == 0)
2627 goto theend;
2628
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002629 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2630 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633 /*
2634 * If there is a file name,
2635 * read the wanted file if needed, and check autowrite etc.
2636 */
2637 old_curbuf = curbuf;
2638 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002639
2640 if (qf_ptr->qf_fnum != 0)
2641 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002642 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002643
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002644 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2645 &opened_window, &abort);
2646 if (abort)
2647 {
2648 qi = NULL;
2649 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002650 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002651 }
2652
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002653 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
2655 /* When not switched to another buffer, still need to set pc mark */
2656 if (curbuf == old_curbuf)
2657 setpcmark();
2658
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002659 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2660 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
2662#ifdef FEAT_FOLDING
2663 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2664 foldOpenCursor();
2665#endif
2666 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002667 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
2669 else
2670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (opened_window)
2672 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002673 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
2675 /*
2676 * Couldn't open file, so put index back where it was. This could
2677 * happen if the file was readonly and we changed something.
2678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 qf_ptr = old_qf_ptr;
2681 qf_index = old_qf_index;
2682 }
2683 }
2684theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002685 if (qi != NULL)
2686 {
2687 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2688 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (p_swb != old_swb && opened_window)
2691 {
2692 /* Restore old 'switchbuf' value, but not when an autocommand or
2693 * modeline has changed the value. */
2694 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002697 swb_flags = old_swb_flags;
2698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 else
2700 free_string_option(old_swb);
2701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702}
2703
2704/*
2705 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002706 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 */
2708 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002709qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002711 buf_T *buf;
2712 char_u *fname;
2713 qfline_T *qfp;
2714 int i;
2715 int idx1 = 1;
2716 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002717 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002718 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002719 int qfFileAttr;
2720 int qfSepAttr;
2721 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002722 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002724 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002726 if (eap->cmdidx == CMD_llist)
2727 {
2728 qi = GET_LOC_LIST(curwin);
2729 if (qi == NULL)
2730 {
2731 EMSG(_(e_loclist));
2732 return;
2733 }
2734 }
2735
2736 if (qi->qf_curlist >= qi->qf_listcount
2737 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
2739 EMSG(_(e_quickfix));
2740 return;
2741 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002742 if (*arg == '+')
2743 {
2744 ++arg;
2745 plus = TRUE;
2746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2748 {
2749 EMSG(_(e_trailing));
2750 return;
2751 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002752 if (plus)
2753 {
2754 i = qi->qf_lists[qi->qf_curlist].qf_index;
2755 idx2 = i + idx1;
2756 idx1 = i;
2757 }
2758 else
2759 {
2760 i = qi->qf_lists[qi->qf_curlist].qf_count;
2761 if (idx1 < 0)
2762 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2763 if (idx2 < 0)
2764 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
Bram Moolenaara796d462018-05-01 14:30:36 +02002767 /* Shorten all the file names, so that it is easy to read */
2768 shorten_fnames(FALSE);
2769
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002770 /*
2771 * Get the attributes for the different quickfix highlight items. Note
2772 * that this depends on syntax items defined in the qf.vim syntax file
2773 */
2774 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2775 if (qfFileAttr == 0)
2776 qfFileAttr = HL_ATTR(HLF_D);
2777 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2778 if (qfSepAttr == 0)
2779 qfSepAttr = HL_ATTR(HLF_D);
2780 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2781 if (qfLineAttr == 0)
2782 qfLineAttr = HL_ATTR(HLF_N);
2783
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002784 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002786 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2787 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
2789 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2790 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002791 msg_putchar('\n');
2792 if (got_int)
2793 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002794
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795 fname = NULL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002796 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
2797 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", i, (char *)qfp->qf_module);
2798 else {
2799 if (qfp->qf_fnum != 0
2800 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2801 {
2802 fname = buf->b_fname;
2803 if (qfp->qf_type == 1) /* :helpgrep */
2804 fname = gettail(fname);
2805 }
2806 if (fname == NULL)
2807 sprintf((char *)IObuff, "%2d", i);
2808 else
2809 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2810 i, (char *)fname);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002811 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002812 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002813 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2814
2815 if (qfp->qf_lnum != 0)
2816 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817 if (qfp->qf_lnum == 0)
2818 IObuff[0] = NUL;
2819 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002820 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002821 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002822 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002823 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002824 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002825 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002826 msg_puts_attr(IObuff, qfLineAttr);
2827 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002828 if (qfp->qf_pattern != NULL)
2829 {
2830 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002831 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002832 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002833 }
2834 msg_puts((char_u *)" ");
2835
2836 /* Remove newlines and leading whitespace from the text. For an
2837 * unrecognized line keep the indent, the compiler may mark a word
2838 * with ^^^^. */
2839 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2841 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002842 msg_prt_line(IObuff, FALSE);
2843 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002845
2846 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002847 if (qfp == NULL)
2848 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002849 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 ui_breakcheck();
2851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852}
2853
2854/*
2855 * Remove newlines and leading whitespace from an error message.
2856 * Put the result in "buf[bufsize]".
2857 */
2858 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002859qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860{
2861 int i;
2862 char_u *p = text;
2863
2864 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2865 {
2866 if (*p == '\n')
2867 {
2868 buf[i] = ' ';
2869 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002870 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 break;
2872 }
2873 else
2874 buf[i] = *p++;
2875 }
2876 buf[i] = NUL;
2877}
2878
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002879 static void
2880qf_msg(qf_info_T *qi, int which, char *lead)
2881{
2882 char *title = (char *)qi->qf_lists[which].qf_title;
2883 int count = qi->qf_lists[which].qf_count;
2884 char_u buf[IOSIZE];
2885
2886 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2887 lead,
2888 which + 1,
2889 qi->qf_listcount,
2890 count);
2891
2892 if (title != NULL)
2893 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002894 size_t len = STRLEN(buf);
2895
2896 if (len < 34)
2897 {
2898 vim_memset(buf + len, ' ', 34 - len);
2899 buf[34] = NUL;
2900 }
2901 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002902 }
2903 trunc_string(buf, buf, Columns - 1, IOSIZE);
2904 msg(buf);
2905}
2906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907/*
2908 * ":colder [count]": Up in the quickfix stack.
2909 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002910 * ":lolder [count]": Up in the location list stack.
2911 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 */
2913 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002914qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002916 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 int count;
2918
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2920 {
2921 qi = GET_LOC_LIST(curwin);
2922 if (qi == NULL)
2923 {
2924 EMSG(_(e_loclist));
2925 return;
2926 }
2927 }
2928
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 if (eap->addr_count != 0)
2930 count = eap->line2;
2931 else
2932 count = 1;
2933 while (count--)
2934 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002935 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002937 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
2939 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002940 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002942 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944 else
2945 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002946 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 {
2948 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002949 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002951 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002954 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002955 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956}
2957
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002958 void
2959qf_history(exarg_T *eap)
2960{
2961 qf_info_T *qi = &ql_info;
2962 int i;
2963
2964 if (eap->cmdidx == CMD_lhistory)
2965 qi = GET_LOC_LIST(curwin);
2966 if (qi == NULL || (qi->qf_listcount == 0
2967 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2968 MSG(_("No entries"));
2969 else
2970 for (i = 0; i < qi->qf_listcount; ++i)
2971 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2972}
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002975 * Free all the entries in the error list "idx". Note that other information
2976 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 */
2978 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002979qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002981 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002982 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002983 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002984 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002986 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002988 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002989 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002990 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002991 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02002992 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002993 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002994 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02002995 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002996 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002997 if (stop)
2998 /* Somehow qf_count may have an incorrect value, set it to 1
2999 * to avoid crashing when it's wrong.
3000 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003001 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003002 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003003 qfl->qf_start = qfpnext;
3004 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003006
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003007 qfl->qf_index = 0;
3008 qfl->qf_start = NULL;
3009 qfl->qf_last = NULL;
3010 qfl->qf_ptr = NULL;
3011 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003012
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003013 qf_clean_dir_stack(&qfl->qf_dir_stack);
3014 qfl->qf_directory = NULL;
3015 qf_clean_dir_stack(&qfl->qf_file_stack);
3016 qfl->qf_currfile = NULL;
3017 qfl->qf_multiline = FALSE;
3018 qfl->qf_multiignore = FALSE;
3019 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020}
3021
3022/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003023 * Free error list "idx". Frees all the entries in the quickfix list,
3024 * associated context information and the title.
3025 */
3026 static void
3027qf_free(qf_info_T *qi, int idx)
3028{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003029 qf_list_T *qfl = &qi->qf_lists[idx];
3030
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003031 qf_free_items(qi, idx);
3032
Bram Moolenaard23a8232018-02-10 18:45:26 +01003033 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003034 free_tv(qfl->qf_ctx);
3035 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003036 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003037 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003038}
3039
3040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 * qf_mark_adjust: adjust marks
3042 */
3043 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003044qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003045 win_T *wp,
3046 linenr_T line1,
3047 linenr_T line2,
3048 long amount,
3049 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003051 int i;
3052 qfline_T *qfp;
3053 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003054 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003055 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003056 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
Bram Moolenaarc1542742016-07-20 21:44:37 +02003058 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003059 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003060 if (wp != NULL)
3061 {
3062 if (wp->w_llist == NULL)
3063 return;
3064 qi = wp->w_llist;
3065 }
3066
3067 for (idx = 0; idx < qi->qf_listcount; ++idx)
3068 if (qi->qf_lists[idx].qf_count)
3069 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003070 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3071 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 if (qfp->qf_fnum == curbuf->b_fnum)
3073 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003074 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3076 {
3077 if (amount == MAXLNUM)
3078 qfp->qf_cleared = TRUE;
3079 else
3080 qfp->qf_lnum += amount;
3081 }
3082 else if (amount_after && qfp->qf_lnum > line2)
3083 qfp->qf_lnum += amount_after;
3084 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003085
3086 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003087 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088}
3089
3090/*
3091 * Make a nice message out of the error character and the error number:
3092 * char number message
3093 * e or E 0 " error"
3094 * w or W 0 " warning"
3095 * i or I 0 " info"
3096 * 0 0 ""
3097 * other 0 " c"
3098 * e or E n " error n"
3099 * w or W n " warning n"
3100 * i or I n " info n"
3101 * 0 n " error n"
3102 * other n " c n"
3103 * 1 x "" :helpgrep
3104 */
3105 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003106qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 static char_u buf[20];
3109 static char_u cc[3];
3110 char_u *p;
3111
3112 if (c == 'W' || c == 'w')
3113 p = (char_u *)" warning";
3114 else if (c == 'I' || c == 'i')
3115 p = (char_u *)" info";
3116 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3117 p = (char_u *)" error";
3118 else if (c == 0 || c == 1)
3119 p = (char_u *)"";
3120 else
3121 {
3122 cc[0] = ' ';
3123 cc[1] = c;
3124 cc[2] = NUL;
3125 p = cc;
3126 }
3127
3128 if (nr <= 0)
3129 return p;
3130
3131 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3132 return buf;
3133}
3134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135/*
3136 * ":cwindow": open the quickfix window if we have errors to display,
3137 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003138 * ":lwindow": open the location list window if we have locations to display,
3139 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 */
3141 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003142ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003144 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 win_T *win;
3146
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003147 if (eap->cmdidx == CMD_lwindow)
3148 {
3149 qi = GET_LOC_LIST(curwin);
3150 if (qi == NULL)
3151 return;
3152 }
3153
3154 /* Look for an existing quickfix window. */
3155 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157 /*
3158 * If a quickfix window is open but we have no errors to display,
3159 * close the window. If a quickfix window is not open, then open
3160 * it if we have errors; otherwise, leave it closed.
3161 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003162 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003163 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003164 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 if (win != NULL)
3167 ex_cclose(eap);
3168 }
3169 else if (win == NULL)
3170 ex_copen(eap);
3171}
3172
3173/*
3174 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003175 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003178ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003180 win_T *win = NULL;
3181 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003183 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3184 {
3185 qi = GET_LOC_LIST(curwin);
3186 if (qi == NULL)
3187 return;
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003190 /* Find existing quickfix window and close it. */
3191 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 if (win != NULL)
3193 win_close(win, FALSE);
3194}
3195
3196/*
3197 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003198 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 */
3200 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003201ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003203 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003206 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003207 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003208 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003210 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3211 {
3212 qi = GET_LOC_LIST(curwin);
3213 if (qi == NULL)
3214 {
3215 EMSG(_(e_loclist));
3216 return;
3217 }
3218 }
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (eap->addr_count != 0)
3221 height = eap->line2;
3222 else
3223 height = QF_WINHEIGHT;
3224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226#ifdef FEAT_GUI
3227 need_mouse_correct = TRUE;
3228#endif
3229
3230 /*
3231 * Find existing quickfix window, or open a new one.
3232 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003233 win = qf_find_win(qi);
3234
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003235 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003238 if (eap->addr_count != 0)
3239 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003240 if (cmdmod.split & WSP_VERT)
3241 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003242 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003243 win_setwidth(height);
3244 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003245 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003246 win_setheight(height);
3247 }
3248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 else
3250 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003251 int flags = 0;
3252
Bram Moolenaar9c102382006-05-03 21:26:49 +00003253 qf_buf = qf_find_buf(qi);
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 /* The current window becomes the previous window afterwards. */
3256 win = curwin;
3257
Bram Moolenaar77642c02012-11-20 17:55:10 +01003258 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3259 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003260 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003261 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003262 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003263 /* Default is to open the window below the current window */
3264 if (cmdmod.split == 0)
3265 flags = WSP_BELOW;
3266 flags |= WSP_NEWLOC;
3267 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003269 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003271 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003273 /*
3274 * For the location list window, create a reference to the
3275 * location list from the window 'win'.
3276 */
3277 curwin->w_llist_ref = win->w_llist;
3278 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003280
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003281 if (oldwin != curwin)
3282 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003283 if (qf_buf != NULL)
3284 /* Use the existing quickfix buffer */
3285 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003286 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003287 else
3288 {
3289 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003290 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003291 /* switch off 'swapfile' */
3292 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3293 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003294 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003295 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003296 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003297#ifdef FEAT_DIFF
3298 curwin->w_p_diff = FALSE;
3299#endif
3300#ifdef FEAT_FOLDING
3301 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3302 OPT_LOCAL);
3303#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003306 /* Only set the height when still in the same tab page and there is no
3307 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003308 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 win_setheight(height);
3310 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3311 if (win_valid(win))
3312 prevwin = win;
3313 }
3314
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003315 qf_set_title_var(qi);
3316
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 /*
3318 * Fill the buffer with the quickfix list.
3319 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003320 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003322 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 curwin->w_cursor.col = 0;
3324 check_cursor();
3325 update_topline(); /* scroll to show the line */
3326}
3327
3328/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003329 * Move the cursor in the quickfix window to "lnum".
3330 */
3331 static void
3332qf_win_goto(win_T *win, linenr_T lnum)
3333{
3334 win_T *old_curwin = curwin;
3335
3336 curwin = win;
3337 curbuf = win->w_buffer;
3338 curwin->w_cursor.lnum = lnum;
3339 curwin->w_cursor.col = 0;
3340#ifdef FEAT_VIRTUALEDIT
3341 curwin->w_cursor.coladd = 0;
3342#endif
3343 curwin->w_curswant = 0;
3344 update_topline(); /* scroll to show the line */
3345 redraw_later(VALID);
3346 curwin->w_redr_status = TRUE; /* update ruler */
3347 curwin = old_curwin;
3348 curbuf = curwin->w_buffer;
3349}
3350
3351/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003352 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003353 */
3354 void
3355ex_cbottom(exarg_T *eap UNUSED)
3356{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003357 qf_info_T *qi = &ql_info;
3358 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003359
Bram Moolenaar537ef082016-07-09 17:56:19 +02003360 if (eap->cmdidx == CMD_lbottom)
3361 {
3362 qi = GET_LOC_LIST(curwin);
3363 if (qi == NULL)
3364 {
3365 EMSG(_(e_loclist));
3366 return;
3367 }
3368 }
3369
3370 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003371 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3372 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3373}
3374
3375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 * Return the number of the current entry (line number in the quickfix
3377 * window).
3378 */
3379 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003380qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003382 qf_info_T *qi = &ql_info;
3383
3384 if (IS_LL_WINDOW(wp))
3385 /* In the location list window, use the referenced location list */
3386 qi = wp->w_llist_ref;
3387
3388 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389}
3390
3391/*
3392 * Update the cursor position in the quickfix window to the current error.
3393 * Return TRUE if there is a quickfix window.
3394 */
3395 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003396qf_win_pos_update(
3397 qf_info_T *qi,
3398 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399{
3400 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003401 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
3403 /*
3404 * Put the cursor on the current error in the quickfix window, so that
3405 * it's viewable.
3406 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003407 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (win != NULL
3409 && qf_index <= win->w_buffer->b_ml.ml_line_count
3410 && old_qf_index != qf_index)
3411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (qf_index > old_qf_index)
3413 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003414 win->w_redraw_top = old_qf_index;
3415 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417 else
3418 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003419 win->w_redraw_top = qf_index;
3420 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003422 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
3424 return win != NULL;
3425}
3426
3427/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003428 * Check whether the given window is displaying the specified quickfix/location
3429 * list buffer
3430 */
3431 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003432is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003433{
3434 /*
3435 * A window displaying the quickfix buffer will have the w_llist_ref field
3436 * set to NULL.
3437 * A window displaying a location list buffer will have the w_llist_ref
3438 * pointing to the location list.
3439 */
3440 if (bt_quickfix(win->w_buffer))
3441 if ((qi == &ql_info && win->w_llist_ref == NULL)
3442 || (qi != &ql_info && win->w_llist_ref == qi))
3443 return TRUE;
3444
3445 return FALSE;
3446}
3447
3448/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003449 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003450 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003451 */
3452 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003453qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003454{
3455 win_T *win;
3456
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003457 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003458 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003459 return win;
3460 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003461}
3462
3463/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003464 * Find a quickfix buffer.
3465 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 */
3467 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003468qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003470 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003471 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
Bram Moolenaar9c102382006-05-03 21:26:49 +00003473 FOR_ALL_TAB_WINDOWS(tp, win)
3474 if (is_qf_win(win, qi))
3475 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003476
Bram Moolenaar9c102382006-05-03 21:26:49 +00003477 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478}
3479
3480/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003481 * Update the w:quickfix_title variable in the quickfix/location list window
3482 */
3483 static void
3484qf_update_win_titlevar(qf_info_T *qi)
3485{
3486 win_T *win;
3487 win_T *curwin_save;
3488
3489 if ((win = qf_find_win(qi)) != NULL)
3490 {
3491 curwin_save = curwin;
3492 curwin = win;
3493 qf_set_title_var(qi);
3494 curwin = curwin_save;
3495 }
3496}
3497
3498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 * Find the quickfix buffer. If it exists, update the contents.
3500 */
3501 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003502qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503{
3504 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003505 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003509 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (buf != NULL)
3511 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003512 linenr_T old_line_count = buf->b_ml.ml_line_count;
3513
3514 if (old_last == NULL)
3515 /* set curwin/curbuf to buf and save a few things */
3516 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
Bram Moolenaard823fa92016-08-12 16:29:27 +02003518 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003519
Bram Moolenaar864293a2016-06-02 13:40:04 +02003520 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003521 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003522
Bram Moolenaar864293a2016-06-02 13:40:04 +02003523 if (old_last == NULL)
3524 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003525 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003526
3527 /* restore curwin/curbuf and a few other things */
3528 aucmd_restbuf(&aco);
3529 }
3530
3531 /* Only redraw when added lines are visible. This avoids flickering
3532 * when the added lines are not visible. */
3533 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3534 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536}
3537
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003538/*
3539 * Set "w:quickfix_title" if "qi" has a title.
3540 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003541 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003542qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003543{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003544 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3545 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003546 qi->qf_lists[qi->qf_curlist].qf_title);
3547}
3548
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549/*
3550 * Fill current buffer with quickfix errors, replacing any previous contents.
3551 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003552 * If "old_last" is not NULL append the items after this one.
3553 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3554 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 */
3556 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003557qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003559 linenr_T lnum;
3560 qfline_T *qfp;
3561 buf_T *errbuf;
3562 int len;
3563 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaar864293a2016-06-02 13:40:04 +02003565 if (old_last == NULL)
3566 {
3567 if (buf != curbuf)
3568 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003569 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003570 return;
3571 }
3572
3573 /* delete all existing lines */
3574 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3575 (void)ml_delete((linenr_T)1, FALSE);
3576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
3578 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003579 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003581 char_u dirname[MAXPATHL];
3582
3583 *dirname = NUL;
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003586 if (old_last == NULL)
3587 {
3588 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3589 lnum = 0;
3590 }
3591 else
3592 {
3593 qfp = old_last->qf_next;
3594 lnum = buf->b_ml.ml_line_count;
3595 }
3596 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003598 if (qfp->qf_module != NULL)
3599 {
3600 STRCPY(IObuff, qfp->qf_module);
3601 len = (int)STRLEN(IObuff);
3602 }
3603 else if (qfp->qf_fnum != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3605 && errbuf->b_fname != NULL)
3606 {
3607 if (qfp->qf_type == 1) /* :helpgrep */
3608 STRCPY(IObuff, gettail(errbuf->b_fname));
3609 else
Bram Moolenaara796d462018-05-01 14:30:36 +02003610 {
3611 /* shorten the file name if not done already */
3612 if (errbuf->b_sfname == NULL
3613 || mch_isFullName(errbuf->b_sfname))
3614 {
3615 if (*dirname == NUL)
3616 mch_dirname(dirname, MAXPATHL);
3617 shorten_buf_fname(errbuf, dirname, FALSE);
3618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 STRCPY(IObuff, errbuf->b_fname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 len = (int)STRLEN(IObuff);
3622 }
3623 else
3624 len = 0;
3625 IObuff[len++] = '|';
3626
3627 if (qfp->qf_lnum > 0)
3628 {
3629 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3630 len += (int)STRLEN(IObuff + len);
3631
3632 if (qfp->qf_col > 0)
3633 {
3634 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3635 len += (int)STRLEN(IObuff + len);
3636 }
3637
3638 sprintf((char *)IObuff + len, "%s",
3639 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3640 len += (int)STRLEN(IObuff + len);
3641 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003642 else if (qfp->qf_pattern != NULL)
3643 {
3644 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3645 len += (int)STRLEN(IObuff + len);
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 IObuff[len++] = '|';
3648 IObuff[len++] = ' ';
3649
3650 /* Remove newlines and leading whitespace from the text.
3651 * For an unrecognized line keep the indent, the compiler may
3652 * mark a word with ^^^^. */
3653 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3654 IObuff + len, IOSIZE - len);
3655
Bram Moolenaar864293a2016-06-02 13:40:04 +02003656 if (ml_append_buf(buf, lnum, IObuff,
3657 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003659 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003661 if (qfp == NULL)
3662 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003664
3665 if (old_last == NULL)
3666 /* Delete the empty line which is now at the end */
3667 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669
3670 /* correct cursor position */
3671 check_lnums(TRUE);
3672
Bram Moolenaar864293a2016-06-02 13:40:04 +02003673 if (old_last == NULL)
3674 {
3675 /* Set the 'filetype' to "qf" each time after filling the buffer.
3676 * This resembles reading a file into a buffer, it's more logical when
3677 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003678 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003679 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3680 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
Bram Moolenaar864293a2016-06-02 13:40:04 +02003682 keep_filetype = TRUE; /* don't detect 'filetype' */
3683 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003685 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003687 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003688 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003689
Bram Moolenaar864293a2016-06-02 13:40:04 +02003690 /* make sure it will be redrawn */
3691 redraw_curbuf_later(NOT_VALID);
3692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
3694 /* Restore KeyTyped, setting 'filetype' may reset it. */
3695 KeyTyped = old_KeyTyped;
3696}
3697
Bram Moolenaarb254af32017-12-18 19:48:58 +01003698 static void
3699qf_list_changed(qf_info_T *qi, int qf_idx)
3700{
3701 qi->qf_lists[qf_idx].qf_changedtick++;
3702}
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003705 * Return TRUE when using ":vimgrep" for ":grep".
3706 */
3707 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003708grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003709{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003710 return ((cmdidx == CMD_grep
3711 || cmdidx == CMD_lgrep
3712 || cmdidx == CMD_grepadd
3713 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003714 && STRCMP("internal",
3715 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3716}
3717
3718/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003719 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 */
3721 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003722ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003724 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003726 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003728 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003729 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003730 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003731 char_u *au_name = NULL;
3732
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003733 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3734 if (grep_internal(eap->cmdidx))
3735 {
3736 ex_vimgrep(eap);
3737 return;
3738 }
3739
Bram Moolenaar7c626922005-02-07 22:01:03 +00003740 switch (eap->cmdidx)
3741 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003742 case CMD_make: au_name = (char_u *)"make"; break;
3743 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3744 case CMD_grep: au_name = (char_u *)"grep"; break;
3745 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3746 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3747 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003748 default: break;
3749 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003750 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3751 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003752 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003753#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003754 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003755 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003756#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003757 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003758#ifdef FEAT_MBYTE
3759 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
Bram Moolenaara6557602006-02-04 22:43:20 +00003762 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3763 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003764 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003765
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003767 fname = get_mef_name();
3768 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003770 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772 /*
3773 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3774 */
3775 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3776 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003777 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 cmd = alloc(len);
3779 if (cmd == NULL)
3780 return;
3781 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3782 (char *)p_shq);
3783 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003784 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 /*
3786 * Output a newline if there's something else than the :make command that
3787 * was typed (in which case the cursor is in column 0).
3788 */
3789 if (msg_col == 0)
3790 msg_didout = FALSE;
3791 msg_start();
3792 MSG_PUTS(":!");
3793 msg_outtrans(cmd); /* show what we are doing */
3794
3795 /* let the shell know if we are redirecting output or not */
3796 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3797
3798#ifdef AMIGA
3799 out_flush();
3800 /* read window status report and redraw before message */
3801 (void)char_avail();
3802#endif
3803
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003804 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003805 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3806 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003807 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003808 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003809 if (wp != NULL)
3810 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003811 if (res >= 0 && qi != NULL)
3812 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003813 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003814 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003815 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3816 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01003817 if (qi != NULL && qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003818 res = qi->qf_lists[qi->qf_curlist].qf_count;
3819 else
3820 res = 0;
3821 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003822 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003823 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
Bram Moolenaar7c626922005-02-07 22:01:03 +00003825 mch_remove(fname);
3826 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 vim_free(cmd);
3828}
3829
3830/*
3831 * Return the name for the errorfile, in allocated memory.
3832 * Find a new unique name when 'makeef' contains "##".
3833 * Returns NULL for error.
3834 */
3835 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003836get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837{
3838 char_u *p;
3839 char_u *name;
3840 static int start = -1;
3841 static int off = 0;
3842#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003843 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844#endif
3845
3846 if (*p_mef == NUL)
3847 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003848 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (name == NULL)
3850 EMSG(_(e_notmp));
3851 return name;
3852 }
3853
3854 for (p = p_mef; *p; ++p)
3855 if (p[0] == '#' && p[1] == '#')
3856 break;
3857
3858 if (*p == NUL)
3859 return vim_strsave(p_mef);
3860
3861 /* Keep trying until the name doesn't exist yet. */
3862 for (;;)
3863 {
3864 if (start == -1)
3865 start = mch_get_pid();
3866 else
3867 off += 19;
3868
3869 name = alloc((unsigned)STRLEN(p_mef) + 30);
3870 if (name == NULL)
3871 break;
3872 STRCPY(name, p_mef);
3873 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3874 STRCAT(name, p + 2);
3875 if (mch_getperm(name) < 0
3876#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003877 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 && mch_lstat((char *)name, &sb) < 0
3879#endif
3880 )
3881 break;
3882 vim_free(name);
3883 }
3884 return name;
3885}
3886
3887/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003888 * Returns the number of valid entries in the current quickfix/location list.
3889 */
3890 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003891qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003892{
3893 qf_info_T *qi = &ql_info;
3894 qfline_T *qfp;
3895 int i, sz = 0;
3896 int prev_fnum = 0;
3897
3898 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3899 {
3900 /* Location list */
3901 qi = GET_LOC_LIST(curwin);
3902 if (qi == NULL)
3903 return 0;
3904 }
3905
3906 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003907 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003908 ++i, qfp = qfp->qf_next)
3909 {
3910 if (qfp->qf_valid)
3911 {
3912 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3913 sz++; /* Count all valid entries */
3914 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3915 {
3916 /* Count the number of files */
3917 sz++;
3918 prev_fnum = qfp->qf_fnum;
3919 }
3920 }
3921 }
3922
3923 return sz;
3924}
3925
3926/*
3927 * Returns the current index of the quickfix/location list.
3928 * Returns 0 if there is an error.
3929 */
3930 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003931qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003932{
3933 qf_info_T *qi = &ql_info;
3934
3935 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3936 {
3937 /* Location list */
3938 qi = GET_LOC_LIST(curwin);
3939 if (qi == NULL)
3940 return 0;
3941 }
3942
3943 return qi->qf_lists[qi->qf_curlist].qf_index;
3944}
3945
3946/*
3947 * Returns the current index in the quickfix/location list (counting only valid
3948 * entries). If no valid entries are in the list, then returns 1.
3949 */
3950 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003951qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003952{
3953 qf_info_T *qi = &ql_info;
3954 qf_list_T *qfl;
3955 qfline_T *qfp;
3956 int i, eidx = 0;
3957 int prev_fnum = 0;
3958
3959 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3960 {
3961 /* Location list */
3962 qi = GET_LOC_LIST(curwin);
3963 if (qi == NULL)
3964 return 1;
3965 }
3966
3967 qfl = &qi->qf_lists[qi->qf_curlist];
3968 qfp = qfl->qf_start;
3969
3970 /* check if the list has valid errors */
3971 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3972 return 1;
3973
3974 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3975 {
3976 if (qfp->qf_valid)
3977 {
3978 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3979 {
3980 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3981 {
3982 /* Count the number of files */
3983 eidx++;
3984 prev_fnum = qfp->qf_fnum;
3985 }
3986 }
3987 else
3988 eidx++;
3989 }
3990 }
3991
3992 return eidx ? eidx : 1;
3993}
3994
3995/*
3996 * Get the 'n'th valid error entry in the quickfix or location list.
3997 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3998 * For :cdo and :ldo returns the 'n'th valid error entry.
3999 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4000 */
4001 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004002qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004003{
4004 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4005 qfline_T *qfp = qfl->qf_start;
4006 int i, eidx;
4007 int prev_fnum = 0;
4008
4009 /* check if the list has valid errors */
4010 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4011 return 1;
4012
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004013 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004014 i++, qfp = qfp->qf_next)
4015 {
4016 if (qfp->qf_valid)
4017 {
4018 if (fdo)
4019 {
4020 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4021 {
4022 /* Count the number of files */
4023 eidx++;
4024 prev_fnum = qfp->qf_fnum;
4025 }
4026 }
4027 else
4028 eidx++;
4029 }
4030
4031 if (eidx == n)
4032 break;
4033 }
4034
4035 if (i <= qfl->qf_count)
4036 return i;
4037 else
4038 return 1;
4039}
4040
4041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004043 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004044 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 */
4046 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004047ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004049 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004050 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004051
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004052 if (eap->cmdidx == CMD_ll
4053 || eap->cmdidx == CMD_lrewind
4054 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004055 || eap->cmdidx == CMD_llast
4056 || eap->cmdidx == CMD_ldo
4057 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004058 {
4059 qi = GET_LOC_LIST(curwin);
4060 if (qi == NULL)
4061 {
4062 EMSG(_(e_loclist));
4063 return;
4064 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004065 }
4066
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004067 if (eap->addr_count > 0)
4068 errornr = (int)eap->line2;
4069 else
4070 {
4071 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4072 errornr = 0;
4073 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4074 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4075 errornr = 1;
4076 else
4077 errornr = 32767;
4078 }
4079
4080 /* For cdo and ldo commands, jump to the nth valid error.
4081 * For cfdo and lfdo commands, jump to the nth valid file entry.
4082 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004083 if (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 = qf_get_nth_valid_entry(qi,
4086 eap->addr_count > 0 ? (int)eap->line1 : 1,
4087 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4088
4089 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090}
4091
4092/*
4093 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004094 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004095 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 */
4097 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004098ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004100 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004101 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004102
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004103 if (eap->cmdidx == CMD_lnext
4104 || eap->cmdidx == CMD_lNext
4105 || eap->cmdidx == CMD_lprevious
4106 || eap->cmdidx == CMD_lnfile
4107 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004108 || eap->cmdidx == CMD_lpfile
4109 || eap->cmdidx == CMD_ldo
4110 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004111 {
4112 qi = GET_LOC_LIST(curwin);
4113 if (qi == NULL)
4114 {
4115 EMSG(_(e_loclist));
4116 return;
4117 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004118 }
4119
Bram Moolenaar55b69262017-08-13 13:42:01 +02004120 if (eap->addr_count > 0
4121 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4122 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004123 errornr = (int)eap->line2;
4124 else
4125 errornr = 1;
4126
4127 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4128 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004130 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4131 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004133 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4134 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 ? BACKWARD_FILE
4136 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004137 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138}
4139
4140/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004141 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004142 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 */
4144 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004145ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004147 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004148 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004149 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004150 char_u *au_name = NULL;
Bram Moolenaarfc6f16b2018-03-06 17:43:22 +01004151 int save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004152 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004153
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004154 switch (eap->cmdidx)
4155 {
4156 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4157 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4158 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4159 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4160 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4161 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4162 default: break;
4163 }
4164 if (au_name != NULL)
4165 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004166#ifdef FEAT_MBYTE
4167 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4168#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004169#ifdef FEAT_BROWSE
4170 if (cmdmod.browse)
4171 {
4172 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004173 NULL, NULL,
4174 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004175 if (browse_file == NULL)
4176 return;
4177 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4178 vim_free(browse_file);
4179 }
4180 else
4181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004183 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004184
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004185 if (eap->cmdidx == CMD_lfile
4186 || eap->cmdidx == CMD_lgetfile
4187 || eap->cmdidx == CMD_laddfile)
4188 wp = curwin;
4189
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004190 /*
4191 * This function is used by the :cfile, :cgetfile and :caddfile
4192 * commands.
4193 * :cfile always creates a new quickfix list and jumps to the
4194 * first error.
4195 * :cgetfile creates a new quickfix list but doesn't jump to the
4196 * first error.
4197 * :caddfile adds to an existing quickfix list. If there is no
4198 * quickfix list then a new list is created.
4199 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004200 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4201 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004202 if (wp != NULL)
4203 qi = GET_LOC_LIST(wp);
4204 if (res >= 0 && qi != NULL)
4205 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004206 if (qi != NULL)
4207 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004208 if (au_name != NULL)
4209 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004210
4211 /* An autocmd might have freed the quickfix/location list. Check whether it
4212 * is still valid. */
4213 if (qi != NULL && !qflist_valid(wp, save_qfid))
Bram Moolenaar3c097222017-12-21 20:54:49 +01004214 return;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004215 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004216 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217}
4218
4219/*
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004220 * Return the quickfix/location list number with the given identifier.
4221 * Returns -1 if list is not found.
4222 */
4223 static int
4224qf_id2nr(qf_info_T *qi, int_u qfid)
4225{
4226 int qf_idx;
4227
4228 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4229 if (qi->qf_lists[qf_idx].qf_id == qfid)
4230 return qf_idx;
Bram Moolenaar29ce4092018-04-28 21:56:44 +02004231 return INVALID_QFIDX;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004232}
4233
4234/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004235 * Return the vimgrep autocmd name.
4236 */
4237 static char_u *
4238vgr_get_auname(cmdidx_T cmdidx)
4239{
4240 switch (cmdidx)
4241 {
4242 case CMD_vimgrep: return (char_u *)"vimgrep";
4243 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4244 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4245 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4246 case CMD_grep: return (char_u *)"grep";
4247 case CMD_lgrep: return (char_u *)"lgrep";
4248 case CMD_grepadd: return (char_u *)"grepadd";
4249 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4250 default: return NULL;
4251 }
4252}
4253
4254/*
4255 * Initialize the regmatch used by vimgrep for pattern "s".
4256 */
4257 static void
4258vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4259{
4260 /* Get the search pattern: either white-separated or enclosed in // */
4261 regmatch->regprog = NULL;
4262
4263 if (s == NULL || *s == NUL)
4264 {
4265 /* Pattern is empty, use last search pattern. */
4266 if (last_search_pat() == NULL)
4267 {
4268 EMSG(_(e_noprevre));
4269 return;
4270 }
4271 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4272 }
4273 else
4274 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4275
4276 regmatch->rmm_ic = p_ic;
4277 regmatch->rmm_maxcol = 0;
4278}
4279
4280/*
4281 * Display a file name when vimgrep is running.
4282 */
4283 static void
4284vgr_display_fname(char_u *fname)
4285{
4286 char_u *p;
4287
4288 msg_start();
4289 p = msg_strtrunc(fname, TRUE);
4290 if (p == NULL)
4291 msg_outtrans(fname);
4292 else
4293 {
4294 msg_outtrans(p);
4295 vim_free(p);
4296 }
4297 msg_clr_eos();
4298 msg_didout = FALSE; /* overwrite this message */
4299 msg_nowait = TRUE; /* don't wait for this message */
4300 msg_col = 0;
4301 out_flush();
4302}
4303
4304/*
4305 * Load a dummy buffer to search for a pattern using vimgrep.
4306 */
4307 static buf_T *
4308vgr_load_dummy_buf(
4309 char_u *fname,
4310 char_u *dirname_start,
4311 char_u *dirname_now)
4312{
4313 int save_mls;
4314#if defined(FEAT_SYN_HL)
4315 char_u *save_ei = NULL;
4316#endif
4317 buf_T *buf;
4318
4319#if defined(FEAT_SYN_HL)
4320 /* Don't do Filetype autocommands to avoid loading syntax and
4321 * indent scripts, a great speed improvement. */
4322 save_ei = au_event_disable(",Filetype");
4323#endif
4324 /* Don't use modelines here, it's useless. */
4325 save_mls = p_mls;
4326 p_mls = 0;
4327
4328 /* Load file into a buffer, so that 'fileencoding' is detected,
4329 * autocommands applied, etc. */
4330 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4331
4332 p_mls = save_mls;
4333#if defined(FEAT_SYN_HL)
4334 au_event_restore(save_ei);
4335#endif
4336
4337 return buf;
4338}
4339
4340/*
4341 * Check whether a quickfix/location list valid. Autocmds may remove or change
4342 * a quickfix list when vimgrep is running. If the list is not found, create a
4343 * new list.
4344 */
4345 static int
4346vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004347 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004348 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004349 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004350 char_u *title)
4351{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004352 /* Verify that the quickfix/location list was not freed by an autocmd */
4353 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004354 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004355 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004356 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004357 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004358 EMSG(_(e_loc_list_changed));
4359 return FALSE;
4360 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004361 else
4362 {
4363 /* Quickfix list is not found, create a new one. */
4364 qf_new_list(qi, title);
4365 return TRUE;
4366 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004367 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004368
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004369 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004370 /* Autocommands changed the quickfix list. Find the one we were
4371 * using and restore it. */
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004372 qi->qf_curlist = qf_id2nr(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004373
4374 return TRUE;
4375}
4376
4377/*
4378 * Search for a pattern in all the lines in a buffer and add the matching lines
4379 * to a quickfix list.
4380 */
4381 static int
4382vgr_match_buflines(
4383 qf_info_T *qi,
4384 char_u *fname,
4385 buf_T *buf,
4386 regmmatch_T *regmatch,
4387 long tomatch,
4388 int duplicate_name,
4389 int flags)
4390{
4391 int found_match = FALSE;
4392 long lnum;
4393 colnr_T col;
4394
4395 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
4396 {
4397 col = 0;
4398 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
4399 col, NULL, NULL) > 0)
4400 {
4401 /* Pass the buffer number so that it gets used even for a
4402 * dummy buffer, unless duplicate_name is set, then the
4403 * buffer will be wiped out below. */
4404 if (qf_add_entry(qi,
4405 qi->qf_curlist,
4406 NULL, /* dir */
4407 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02004408 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004409 duplicate_name ? 0 : buf->b_fnum,
4410 ml_get_buf(buf,
4411 regmatch->startpos[0].lnum + lnum, FALSE),
4412 regmatch->startpos[0].lnum + lnum,
4413 regmatch->startpos[0].col + 1,
4414 FALSE, /* vis_col */
4415 NULL, /* search pattern */
4416 0, /* nr */
4417 0, /* type */
4418 TRUE /* valid */
4419 ) == FAIL)
4420 {
4421 got_int = TRUE;
4422 break;
4423 }
4424 found_match = TRUE;
4425 if (--tomatch == 0)
4426 break;
4427 if ((flags & VGR_GLOBAL) == 0
4428 || regmatch->endpos[0].lnum > 0)
4429 break;
4430 col = regmatch->endpos[0].col
4431 + (col == regmatch->endpos[0].col);
4432 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
4433 break;
4434 }
4435 line_breakcheck();
4436 if (got_int)
4437 break;
4438 }
4439
4440 return found_match;
4441}
4442
4443/*
4444 * Jump to the first match and update the directory.
4445 */
4446 static void
4447vgr_jump_to_match(
4448 qf_info_T *qi,
4449 int forceit,
4450 int *redraw_for_dummy,
4451 buf_T *first_match_buf,
4452 char_u *target_dir)
4453{
4454 buf_T *buf;
4455
4456 buf = curbuf;
4457 qf_jump(qi, 0, 0, forceit);
4458 if (buf != curbuf)
4459 /* If we jumped to another buffer redrawing will already be
4460 * taken care of. */
4461 *redraw_for_dummy = FALSE;
4462
4463 /* Jump to the directory used after loading the buffer. */
4464 if (curbuf == first_match_buf && target_dir != NULL)
4465 {
4466 exarg_T ea;
4467
4468 ea.arg = target_dir;
4469 ea.cmdidx = CMD_lcd;
4470 ex_cd(&ea);
4471 }
4472}
4473
4474/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004475 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004476 * ":vimgrepadd {pattern} file(s)"
4477 * ":lvimgrep {pattern} file(s)"
4478 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004479 */
4480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004481ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004482{
Bram Moolenaar81695252004-12-29 20:58:21 +00004483 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004484 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004485 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004486 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004487 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004488 char_u *s;
4489 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004490 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004491 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004492 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004493 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004494 buf_T *buf;
4495 int duplicate_name = FALSE;
4496 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004497 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004498 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004499 buf_T *first_match_buf = NULL;
4500 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004501 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004502 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004503 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004504 char_u *dirname_start = NULL;
4505 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004506 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004507 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004508
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004509 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01004510 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4511 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004512 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004513#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004514 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004515 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004516#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004517 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004518
Bram Moolenaar754b5602006-02-09 23:53:20 +00004519 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004520 || eap->cmdidx == CMD_lvimgrep
4521 || eap->cmdidx == CMD_lgrepadd
4522 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004523 {
4524 qi = ll_get_or_alloc_list(curwin);
4525 if (qi == NULL)
4526 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004527 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004528 }
4529
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004530 if (eap->addr_count > 0)
4531 tomatch = eap->line2;
4532 else
4533 tomatch = MAXLNUM;
4534
Bram Moolenaar81695252004-12-29 20:58:21 +00004535 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004536 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004537 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004538 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004539 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004540 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004541 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004542 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004543 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004544
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004545 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004546 if (regmatch.regprog == NULL)
4547 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004548
4549 p = skipwhite(p);
4550 if (*p == NUL)
4551 {
4552 EMSG(_("E683: File name missing or invalid pattern"));
4553 goto theend;
4554 }
4555
Bram Moolenaar55b69262017-08-13 13:42:01 +02004556 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004557 && eap->cmdidx != CMD_vimgrepadd
4558 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004559 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004560 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004561 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004562
4563 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004564 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004565 goto theend;
4566 if (fcount == 0)
4567 {
4568 EMSG(_(e_nomatch));
4569 goto theend;
4570 }
4571
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004572 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4573 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004574 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004575 {
4576 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004577 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004578 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004579
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004580 /* Remember the current directory, because a BufRead autocommand that does
4581 * ":lcd %:p:h" changes the meaning of short path names. */
4582 mch_dirname(dirname_start, MAXPATHL);
4583
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004584 /* Remember the current quickfix list identifier, so that we can check for
4585 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004586 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004587
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004588 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004589 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004590 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004591 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004592 if (time(NULL) > seconds)
4593 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004594 /* Display the file name every second or so, show the user we are
4595 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004596 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004597 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004598 }
4599
Bram Moolenaar81695252004-12-29 20:58:21 +00004600 buf = buflist_findname_exp(fnames[fi]);
4601 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4602 {
4603 /* Remember that a buffer with this name already exists. */
4604 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004605 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004606 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004607
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004608 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00004609 }
4610 else
4611 /* Use existing, loaded buffer. */
4612 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004613
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004614 /* Check whether the quickfix list is still valid. When loading a
4615 * buffer above, autocommands might have changed the quickfix list. */
4616 if (!vgr_qflist_valid(wp, qi, save_qfid, *eap->cmdlinep))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004617 {
4618 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004619 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004620 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004621 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004622
Bram Moolenaar81695252004-12-29 20:58:21 +00004623 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004624 {
4625 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004626 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004627 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004628 else
4629 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004630 /* Try for a match in all lines of the buffer.
4631 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004632 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
4633 tomatch, duplicate_name, flags);
4634
Bram Moolenaar81695252004-12-29 20:58:21 +00004635 if (using_dummy)
4636 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004637 if (found_match && first_match_buf == NULL)
4638 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004639 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004640 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004641 /* Never keep a dummy buffer if there is another buffer
4642 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004643 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004644 buf = NULL;
4645 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004646 else if (!cmdmod.hide
4647 || buf->b_p_bh[0] == 'u' /* "unload" */
4648 || buf->b_p_bh[0] == 'w' /* "wipe" */
4649 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004650 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004651 /* When no match was found we don't need to remember the
4652 * buffer, wipe it out. If there was a match and it
4653 * wasn't the first one or we won't jump there: only
4654 * unload the buffer.
4655 * Ignore 'hidden' here, because it may lead to having too
4656 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004657 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004658 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004659 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004660 buf = NULL;
4661 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004662 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004663 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004664 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004665 /* Keeping the buffer, remove the dummy flag. */
4666 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004667 buf = NULL;
4668 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004669 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004670
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004671 if (buf != NULL)
4672 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004673 /* Keeping the buffer, remove the dummy flag. */
4674 buf->b_flags &= ~BF_DUMMY;
4675
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004676 /* If the buffer is still loaded we need to use the
4677 * directory we jumped to below. */
4678 if (buf == first_match_buf
4679 && target_dir == NULL
4680 && STRCMP(dirname_start, dirname_now) != 0)
4681 target_dir = vim_strsave(dirname_now);
4682
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004683 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004684 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004685 * need to be done (again). But not the window-local
4686 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004687 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004688#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004689 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4690 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004691#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004692 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004693 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004694 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004695 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004696 }
4697 }
4698
4699 FreeWild(fcount, fnames);
4700
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004701 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4702 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4703 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004704 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004705
Bram Moolenaar864293a2016-06-02 13:40:04 +02004706 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004707
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004708 if (au_name != NULL)
4709 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4710 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004711 /*
4712 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4713 * is still valid.
4714 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004715 if (!qflist_valid(wp, save_qfid))
4716 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004717
Bram Moolenaar86b68352004-12-27 21:59:20 +00004718 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004719 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004720 {
4721 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004722 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
4723 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004724 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004725 else
4726 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004727
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004728 /* If we loaded a dummy buffer into the current window, the autocommands
4729 * may have messed up things, need to redraw and recompute folds. */
4730 if (redraw_for_dummy)
4731 {
4732#ifdef FEAT_FOLDING
4733 foldUpdateAll(curwin);
4734#else
4735 redraw_later(NOT_VALID);
4736#endif
4737 }
4738
Bram Moolenaar86b68352004-12-27 21:59:20 +00004739theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004740 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004741 vim_free(dirname_now);
4742 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004743 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004744 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004745}
4746
4747/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004748 * Restore current working directory to "dirname_start" if they differ, taking
4749 * into account whether it is set locally or globally.
4750 */
4751 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004752restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004753{
4754 char_u *dirname_now = alloc(MAXPATHL);
4755
4756 if (NULL != dirname_now)
4757 {
4758 mch_dirname(dirname_now, MAXPATHL);
4759 if (STRCMP(dirname_start, dirname_now) != 0)
4760 {
4761 /* If the directory has changed, change it back by building up an
4762 * appropriate ex command and executing it. */
4763 exarg_T ea;
4764
4765 ea.arg = dirname_start;
4766 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4767 ex_cd(&ea);
4768 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004769 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004770 }
4771}
4772
4773/*
4774 * Load file "fname" into a dummy buffer and return the buffer pointer,
4775 * placing the directory resulting from the buffer load into the
4776 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4777 * prior to calling this function. Restores directory to "dirname_start" prior
4778 * to returning, if autocmds or the 'autochdir' option have changed it.
4779 *
4780 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4781 * or wipe_dummy_buffer() later!
4782 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004783 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004784 */
4785 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004786load_dummy_buffer(
4787 char_u *fname,
4788 char_u *dirname_start, /* in: old directory */
4789 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004790{
4791 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004792 bufref_T newbufref;
4793 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004794 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004795 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004796 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004797
4798 /* Allocate a buffer without putting it in the buffer list. */
4799 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4800 if (newbuf == NULL)
4801 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004802 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004803
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004804 /* Init the options. */
4805 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4806
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004807 /* need to open the memfile before putting the buffer in a window */
4808 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004809 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004810 /* Make sure this buffer isn't wiped out by auto commands. */
4811 ++newbuf->b_locked;
4812
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004813 /* set curwin/curbuf to buf and save a few things */
4814 aucmd_prepbuf(&aco, newbuf);
4815
4816 /* Need to set the filename for autocommands. */
4817 (void)setfname(curbuf, fname, NULL, FALSE);
4818
Bram Moolenaar81695252004-12-29 20:58:21 +00004819 /* Create swap file now to avoid the ATTENTION message. */
4820 check_need_swap(TRUE);
4821
4822 /* Remove the "dummy" flag, otherwise autocommands may not
4823 * work. */
4824 curbuf->b_flags &= ~BF_DUMMY;
4825
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004826 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004827 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004828 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004829 NULL, READ_NEW | READ_DUMMY);
4830 --newbuf->b_locked;
4831 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004832 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004833 && !(curbuf->b_flags & BF_NEW))
4834 {
4835 failed = FALSE;
4836 if (curbuf != newbuf)
4837 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004838 /* Bloody autocommands changed the buffer! Can happen when
4839 * using netrw and editing a remote file. Use the current
4840 * buffer instead, delete the dummy one after restoring the
4841 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004842 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004843 newbuf = curbuf;
4844 }
4845 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004846
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004847 /* restore curwin/curbuf and a few other things */
4848 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004849 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4850 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004851
4852 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4853 * skip it. */
4854 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004855 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004856
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004857 /*
4858 * When autocommands/'autochdir' option changed directory: go back.
4859 * Let the caller know what the resulting dir was first, in case it is
4860 * important.
4861 */
4862 mch_dirname(resulting_dir, MAXPATHL);
4863 restore_start_dir(dirname_start);
4864
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004865 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004866 return NULL;
4867 if (failed)
4868 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004869 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004870 return NULL;
4871 }
4872 return newbuf;
4873}
4874
4875/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004876 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4877 * directory to "dirname_start" prior to returning, if autocmds or the
4878 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004879 */
4880 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004881wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004882{
4883 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004884 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004885#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004886 cleanup_T cs;
4887
4888 /* Reset the error/interrupt/exception state here so that aborting()
4889 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4890 * work when got_int is set. */
4891 enter_cleanup(&cs);
4892#endif
4893
Bram Moolenaar81695252004-12-29 20:58:21 +00004894 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004895
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004896#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004897 /* Restore the error/interrupt/exception state if not discarded by a
4898 * new aborting error, interrupt, or uncaught exception. */
4899 leave_cleanup(&cs);
4900#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004901 /* When autocommands/'autochdir' option changed directory: go back. */
4902 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004903 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004904}
4905
4906/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004907 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4908 * directory to "dirname_start" prior to returning, if autocmds or the
4909 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004910 */
4911 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004912unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004913{
4914 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004915 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004916 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004917
4918 /* When autocommands/'autochdir' option changed directory: go back. */
4919 restore_start_dir(dirname_start);
4920 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004921}
4922
Bram Moolenaar05159a02005-02-26 23:04:13 +00004923#if defined(FEAT_EVAL) || defined(PROTO)
4924/*
4925 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004926 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004927 */
4928 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004929get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004930{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004931 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004932 dict_T *dict;
4933 char_u buf[2];
4934 qfline_T *qfp;
4935 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004936 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004937
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004938 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004939 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004940 qi = &ql_info;
4941 if (wp != NULL)
4942 {
4943 qi = GET_LOC_LIST(wp);
4944 if (qi == NULL)
4945 return FAIL;
4946 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004947 }
4948
Bram Moolenaar29ce4092018-04-28 21:56:44 +02004949 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004950 qf_idx = qi->qf_curlist;
4951
4952 if (qf_idx >= qi->qf_listcount
4953 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004954 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004955
Bram Moolenaard823fa92016-08-12 16:29:27 +02004956 qfp = qi->qf_lists[qf_idx].qf_start;
4957 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004958 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004959 /* Handle entries with a non-existing buffer number. */
4960 bufnum = qfp->qf_fnum;
4961 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4962 bufnum = 0;
4963
Bram Moolenaar05159a02005-02-26 23:04:13 +00004964 if ((dict = dict_alloc()) == NULL)
4965 return FAIL;
4966 if (list_append_dict(list, dict) == FAIL)
4967 return FAIL;
4968
4969 buf[0] = qfp->qf_type;
4970 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004971 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004972 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4973 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4974 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4975 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaard76ce852018-05-01 15:02:04 +02004976 || dict_add_nr_str(dict, "module", 0L,
4977 qfp->qf_module == NULL ? (char_u *)"" : qfp->qf_module) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004978 || dict_add_nr_str(dict, "pattern", 0L,
4979 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4980 || dict_add_nr_str(dict, "text", 0L,
4981 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004982 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4983 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4984 return FAIL;
4985
4986 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004987 if (qfp == NULL)
4988 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004989 }
4990 return OK;
4991}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004992
4993/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004994 * Flags used by getqflist()/getloclist() to determine which fields to return.
4995 */
4996enum {
4997 QF_GETLIST_NONE = 0x0,
4998 QF_GETLIST_TITLE = 0x1,
4999 QF_GETLIST_ITEMS = 0x2,
5000 QF_GETLIST_NR = 0x4,
5001 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005002 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005003 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005004 QF_GETLIST_IDX = 0x40,
5005 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005006 QF_GETLIST_TICK = 0x100,
5007 QF_GETLIST_ALL = 0x1FF
Bram Moolenaard823fa92016-08-12 16:29:27 +02005008};
5009
5010/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005011 * Parse text from 'di' and return the quickfix list items.
5012 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005013 */
5014 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005015qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005016{
5017 int status = FAIL;
5018 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005019 char_u *errorformat = p_efm;
5020 dictitem_T *efm_di;
5021 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005022
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005023 /* Only a List value is supported */
5024 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005025 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005026 /* If errorformat is supplied then use it, otherwise use the 'efm'
5027 * option setting
5028 */
5029 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5030 {
5031 if (efm_di->di_tv.v_type != VAR_STRING ||
5032 efm_di->di_tv.vval.v_string == NULL)
5033 return FAIL;
5034 errorformat = efm_di->di_tv.vval.v_string;
5035 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005036
Bram Moolenaar36538222017-09-02 19:51:44 +02005037 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005038 if (l == NULL)
5039 return FAIL;
5040
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005041 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
5042 if (qi != NULL)
5043 {
5044 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
5045 qi->qf_refcount++;
5046
Bram Moolenaar36538222017-09-02 19:51:44 +02005047 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005048 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5049 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005050 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005051 qf_free(qi, 0);
5052 }
5053 free(qi);
5054 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005055 dict_add_list(retdict, "items", l);
5056 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005057 }
5058
5059 return status;
5060}
5061
5062/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005063 * Return the quickfix/location list window identifier in the current tabpage.
5064 */
5065 static int
5066qf_winid(qf_info_T *qi)
5067{
5068 win_T *win;
5069
5070 /* The quickfix window can be opened even if the quickfix list is not set
5071 * using ":copen". This is not true for location lists. */
5072 if (qi == NULL)
5073 return 0;
5074 win = qf_find_win(qi);
5075 if (win != NULL)
5076 return win->w_id;
5077 return 0;
5078}
5079
5080/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005081 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005082 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005083 static int
5084qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005085{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005086 int flags = QF_GETLIST_NONE;
5087
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005088 if (dict_find(what, (char_u *)"all", -1) != NULL)
5089 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005090
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005091 if (dict_find(what, (char_u *)"title", -1) != NULL)
5092 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005093
Bram Moolenaara6d48492017-12-12 22:45:31 +01005094 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5095 flags |= QF_GETLIST_NR;
5096
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005097 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5098 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005099
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005100 if (dict_find(what, (char_u *)"context", -1) != NULL)
5101 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005102
Bram Moolenaara6d48492017-12-12 22:45:31 +01005103 if (dict_find(what, (char_u *)"id", -1) != NULL)
5104 flags |= QF_GETLIST_ID;
5105
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005106 if (dict_find(what, (char_u *)"items", -1) != NULL)
5107 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005108
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005109 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5110 flags |= QF_GETLIST_IDX;
5111
5112 if (dict_find(what, (char_u *)"size", -1) != NULL)
5113 flags |= QF_GETLIST_SIZE;
5114
Bram Moolenaarb254af32017-12-18 19:48:58 +01005115 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5116 flags |= QF_GETLIST_TICK;
5117
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005118 return flags;
5119}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005120
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005121/*
5122 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5123 * If 'nr' and 'id' are not present in 'what' then return the current
5124 * quickfix list index.
5125 * If 'nr' is zero then return the current quickfix list index.
5126 * If 'nr' is '$' then return the last quickfix list index.
5127 * If 'id' is present then return the index of the quickfix list with that id.
5128 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5129 * Return -1, if quickfix list is not present or if the stack is empty.
5130 */
5131 static int
5132qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5133{
5134 int qf_idx;
5135 dictitem_T *di;
5136
5137 qf_idx = qi->qf_curlist; /* default is the current list */
5138 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5139 {
5140 /* Use the specified quickfix/location list */
5141 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005142 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005143 /* for zero use the current list */
5144 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005145 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005146 qf_idx = di->di_tv.vval.v_number - 1;
5147 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005148 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005149 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005150 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005151 else if (di->di_tv.v_type == VAR_STRING
5152 && di->di_tv.vval.v_string != NULL
5153 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5154 /* Get the last quickfix list number */
5155 qf_idx = qi->qf_listcount - 1;
5156 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005157 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005158 }
5159
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005160 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5161 {
5162 /* Look for a list with the specified id */
5163 if (di->di_tv.v_type == VAR_NUMBER)
5164 {
5165 /*
5166 * For zero, use the current list or the list specified by 'nr'
5167 */
5168 if (di->di_tv.vval.v_number != 0)
5169 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5170 }
5171 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005172 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005173 }
5174
5175 return qf_idx;
5176}
5177
5178/*
5179 * Return default values for quickfix list properties in retdict.
5180 */
5181 static int
5182qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5183{
5184 int status = OK;
5185
5186 if (flags & QF_GETLIST_TITLE)
5187 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5188 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5189 {
5190 list_T *l = list_alloc();
5191 if (l != NULL)
5192 status = dict_add_list(retdict, "items", l);
5193 else
5194 status = FAIL;
5195 }
5196 if ((status == OK) && (flags & QF_GETLIST_NR))
5197 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5198 if ((status == OK) && (flags & QF_GETLIST_WINID))
5199 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
5200 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5201 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5202 if ((status == OK) && (flags & QF_GETLIST_ID))
5203 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5204 if ((status == OK) && (flags & QF_GETLIST_IDX))
5205 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5206 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5207 status = dict_add_nr_str(retdict, "size", 0L, NULL);
5208 if ((status == OK) && (flags & QF_GETLIST_TICK))
5209 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
5210
5211 return status;
5212}
5213
5214/*
5215 * Return the quickfix list title as 'title' in retdict
5216 */
5217 static int
5218qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5219{
5220 char_u *t;
5221
5222 t = qi->qf_lists[qf_idx].qf_title;
5223 if (t == NULL)
5224 t = (char_u *)"";
5225 return dict_add_nr_str(retdict, "title", 0L, t);
5226}
5227
5228/*
5229 * Return the quickfix list items/entries as 'items' in retdict
5230 */
5231 static int
5232qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5233{
5234 int status = OK;
5235 list_T *l = list_alloc();
5236 if (l != NULL)
5237 {
5238 (void)get_errorlist(qi, NULL, qf_idx, l);
5239 dict_add_list(retdict, "items", l);
5240 }
5241 else
5242 status = FAIL;
5243
5244 return status;
5245}
5246
5247/*
5248 * Return the quickfix list context (if any) as 'context' in retdict.
5249 */
5250 static int
5251qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5252{
5253 int status;
5254 dictitem_T *di;
5255
5256 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5257 {
5258 di = dictitem_alloc((char_u *)"context");
5259 if (di != NULL)
5260 {
5261 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5262 status = dict_add(retdict, di);
5263 if (status == FAIL)
5264 dictitem_free(di);
5265 }
5266 else
5267 status = FAIL;
5268 }
5269 else
5270 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5271
5272 return status;
5273}
5274
5275/*
5276 * Return the quickfix list index as 'idx' in retdict
5277 */
5278 static int
5279qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5280{
5281 int idx = qi->qf_lists[qf_idx].qf_index;
5282 if (qi->qf_lists[qf_idx].qf_count == 0)
5283 /* For empty lists, qf_index is set to 1 */
5284 idx = 0;
5285 return dict_add_nr_str(retdict, "idx", idx, NULL);
5286}
5287
5288/*
5289 * Return quickfix/location list details (title) as a
5290 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5291 * then current list is used. Otherwise the specified list is used.
5292 */
5293 int
5294qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5295{
5296 qf_info_T *qi = &ql_info;
5297 int status = OK;
5298 int qf_idx;
5299 dictitem_T *di;
5300 int flags = QF_GETLIST_NONE;
5301
5302 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5303 return qf_get_list_from_lines(what, di, retdict);
5304
5305 if (wp != NULL)
5306 qi = GET_LOC_LIST(wp);
5307
5308 flags = qf_getprop_keys2flags(what);
5309
5310 if (qi != NULL && qi->qf_listcount != 0)
5311 qf_idx = qf_getprop_qfidx(qi, what);
5312
Bram Moolenaara6d48492017-12-12 22:45:31 +01005313 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005314 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005315 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005316
Bram Moolenaard823fa92016-08-12 16:29:27 +02005317 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005318 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005319 if ((status == OK) && (flags & QF_GETLIST_NR))
5320 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5321 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005322 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005323 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005324 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005325 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005326 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005327 if ((status == OK) && (flags & QF_GETLIST_ID))
5328 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5329 NULL);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005330 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005331 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005332 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5333 status = dict_add_nr_str(retdict, "size",
5334 qi->qf_lists[qf_idx].qf_count, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005335 if ((status == OK) && (flags & QF_GETLIST_TICK))
5336 status = dict_add_nr_str(retdict, "changedtick",
5337 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5338
Bram Moolenaard823fa92016-08-12 16:29:27 +02005339 return status;
5340}
5341
5342/*
5343 * Add list of entries to quickfix/location list. Each list entry is
5344 * a dictionary with item information.
5345 */
5346 static int
5347qf_add_entries(
5348 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005349 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005350 list_T *list,
5351 char_u *title,
5352 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005353{
5354 listitem_T *li;
5355 dict_T *d;
Bram Moolenaard76ce852018-05-01 15:02:04 +02005356 char_u *filename, *module, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005357 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005358 long lnum;
5359 int col, nr;
5360 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005361 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005362 int valid, status;
5363 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005364 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005365
Bram Moolenaara3921f42017-06-04 15:30:34 +02005366 if (action == ' ' || qf_idx == qi->qf_listcount)
5367 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005368 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005369 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005370 qf_idx = qi->qf_curlist;
5371 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005372 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005373 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005374 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005375 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005376 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005377 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005378 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005379 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005380
5381 for (li = list->lv_first; li != NULL; li = li->li_next)
5382 {
5383 if (li->li_tv.v_type != VAR_DICT)
5384 continue; /* Skip non-dict items */
5385
5386 d = li->li_tv.vval.v_dict;
5387 if (d == NULL)
5388 continue;
5389
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005390 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaard76ce852018-05-01 15:02:04 +02005391 module = get_dict_string(d, (char_u *)"module", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005392 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5393 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5394 col = (int)get_dict_number(d, (char_u *)"col");
5395 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5396 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005397 type = get_dict_string(d, (char_u *)"type", TRUE);
5398 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5399 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005400 if (text == NULL)
5401 text = vim_strsave((char_u *)"");
5402
5403 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005404 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005405 valid = FALSE;
5406
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005407 /* Mark entries with non-existing buffer number as not valid. Give the
5408 * error message only once. */
5409 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5410 {
5411 if (!did_bufnr_emsg)
5412 {
5413 did_bufnr_emsg = TRUE;
5414 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5415 }
5416 valid = FALSE;
5417 bufnum = 0;
5418 }
5419
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005420 /* If the 'valid' field is present it overrules the detected value. */
5421 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5422 valid = (int)get_dict_number(d, (char_u *)"valid");
5423
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005424 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005425 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005426 NULL, /* dir */
5427 filename,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005428 module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005429 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005430 text,
5431 lnum,
5432 col,
5433 vcol, /* vis_col */
5434 pattern, /* search pattern */
5435 nr,
5436 type == NULL ? NUL : *type,
5437 valid);
5438
5439 vim_free(filename);
Bram Moolenaard76ce852018-05-01 15:02:04 +02005440 vim_free(module);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005441 vim_free(pattern);
5442 vim_free(text);
5443 vim_free(type);
5444
5445 if (status == FAIL)
5446 {
5447 retval = FAIL;
5448 break;
5449 }
5450 }
5451
Bram Moolenaara3921f42017-06-04 15:30:34 +02005452 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005453 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005454 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005455 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005456 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005457 if (action != 'a')
5458 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005459 qi->qf_lists[qf_idx].qf_ptr =
5460 qi->qf_lists[qf_idx].qf_start;
5461 if (qi->qf_lists[qf_idx].qf_count > 0)
5462 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005463 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005464
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005465 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005466 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005467
5468 return retval;
5469}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005470
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005471/*
5472 * Get the quickfix list index from 'nr' or 'id'
5473 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02005474 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005475qf_setprop_get_qfidx(
5476 qf_info_T *qi,
5477 dict_T *what,
5478 int action,
5479 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005480{
5481 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005482 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005483
Bram Moolenaard823fa92016-08-12 16:29:27 +02005484 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5485 {
5486 /* Use the specified quickfix/location list */
5487 if (di->di_tv.v_type == VAR_NUMBER)
5488 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005489 /* for zero use the current list */
5490 if (di->di_tv.vval.v_number != 0)
5491 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005492
Bram Moolenaar55b69262017-08-13 13:42:01 +02005493 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5494 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005495 /*
5496 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005497 * non-available list and add the new list at the end of the
5498 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005499 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005500 *newlist = TRUE;
5501 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005502 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005503 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005504 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005505 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005506 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005507 }
5508 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005509 && di->di_tv.vval.v_string != NULL
5510 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005511 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005512 if (qi->qf_listcount > 0)
5513 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005514 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02005515 qf_idx = 0;
5516 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005517 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005518 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005519 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005520 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005521 }
5522
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005523 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005524 {
5525 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005526 if (di->di_tv.v_type != VAR_NUMBER)
5527 return INVALID_QFIDX;
5528
5529 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005530 }
5531
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005532 return qf_idx;
5533}
5534
5535/*
5536 * Set the quickfix list title.
5537 */
5538 static int
5539qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
5540{
5541 if (di->di_tv.v_type != VAR_STRING)
5542 return FAIL;
5543
5544 vim_free(qi->qf_lists[qf_idx].qf_title);
5545 qi->qf_lists[qf_idx].qf_title =
5546 get_dict_string(what, (char_u *)"title", TRUE);
5547 if (qf_idx == qi->qf_curlist)
5548 qf_update_win_titlevar(qi);
5549
5550 return OK;
5551}
5552
5553/*
5554 * Set quickfix list items/entries.
5555 */
5556 static int
5557qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
5558{
5559 int retval = FAIL;
5560 char_u *title_save;
5561
5562 if (di->di_tv.v_type != VAR_LIST)
5563 return FAIL;
5564
5565 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5566 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5567 title_save, action == ' ' ? 'a' : action);
5568 if (action == 'r')
5569 {
5570 /*
5571 * When replacing the quickfix list entries using
5572 * qf_add_entries(), the title is set with a ':' prefix.
5573 * Restore the title with the saved title.
5574 */
5575 vim_free(qi->qf_lists[qf_idx].qf_title);
5576 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5577 }
5578 vim_free(title_save);
5579
5580 return retval;
5581}
5582
5583/*
5584 * Set quickfix list items/entries from a list of lines.
5585 */
5586 static int
5587qf_setprop_items_from_lines(
5588 qf_info_T *qi,
5589 int qf_idx,
5590 dict_T *what,
5591 dictitem_T *di,
5592 int action)
5593{
5594 char_u *errorformat = p_efm;
5595 dictitem_T *efm_di;
5596 int retval = FAIL;
5597
5598 /* Use the user supplied errorformat settings (if present) */
5599 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5600 {
5601 if (efm_di->di_tv.v_type != VAR_STRING ||
5602 efm_di->di_tv.vval.v_string == NULL)
5603 return FAIL;
5604 errorformat = efm_di->di_tv.vval.v_string;
5605 }
5606
5607 /* Only a List value is supported */
5608 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
5609 return FAIL;
5610
5611 if (action == 'r')
5612 qf_free_items(qi, qf_idx);
5613 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
5614 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5615 retval = OK;
5616
5617 return retval;
5618}
5619
5620/*
5621 * Set quickfix list context.
5622 */
5623 static int
5624qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
5625{
5626 typval_T *ctx;
5627
5628 free_tv(qi->qf_lists[qf_idx].qf_ctx);
5629 ctx = alloc_tv();
5630 if (ctx != NULL)
5631 copy_tv(&di->di_tv, ctx);
5632 qi->qf_lists[qf_idx].qf_ctx = ctx;
5633
5634 return OK;
5635}
5636
5637/*
5638 * Set quickfix/location list properties (title, items, context).
5639 * Also used to add items from parsing a list of lines.
5640 * Used by the setqflist() and setloclist() VimL functions.
5641 */
5642 static int
5643qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
5644{
5645 dictitem_T *di;
5646 int retval = FAIL;
5647 int qf_idx;
5648 int newlist = FALSE;
5649
5650 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5651 newlist = TRUE;
5652
5653 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
5654 if (qf_idx == INVALID_QFIDX) /* List not found */
5655 return FAIL;
5656
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005657 if (newlist)
5658 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005659 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005660 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005661 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005662 }
5663
5664 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005665 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005666 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005667 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005668 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005669 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005670 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005671 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005672
Bram Moolenaarb254af32017-12-18 19:48:58 +01005673 if (retval == OK)
5674 qf_list_changed(qi, qf_idx);
5675
Bram Moolenaard823fa92016-08-12 16:29:27 +02005676 return retval;
5677}
5678
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005679/*
5680 * Find the non-location list window with the specified location list.
5681 */
5682 static win_T *
5683find_win_with_ll(qf_info_T *qi)
5684{
5685 win_T *wp = NULL;
5686
5687 FOR_ALL_WINDOWS(wp)
5688 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5689 return wp;
5690
5691 return NULL;
5692}
5693
5694/*
5695 * Free the entire quickfix/location list stack.
5696 * If the quickfix/location list window is open, then clear it.
5697 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005698 static void
5699qf_free_stack(win_T *wp, qf_info_T *qi)
5700{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005701 win_T *qfwin = qf_find_win(qi);
5702 win_T *llwin = NULL;
5703 win_T *orig_wp = wp;
5704
5705 if (qfwin != NULL)
5706 {
5707 /* If the quickfix/location list window is open, then clear it */
5708 if (qi->qf_curlist < qi->qf_listcount)
5709 qf_free(qi, qi->qf_curlist);
5710 qf_update_buffer(qi, NULL);
5711 }
5712
5713 if (wp != NULL && IS_LL_WINDOW(wp))
5714 {
5715 /* If in the location list window, then use the non-location list
5716 * window with this location list (if present)
5717 */
5718 llwin = find_win_with_ll(qi);
5719 if (llwin != NULL)
5720 wp = llwin;
5721 }
5722
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005723 qf_free_all(wp);
5724 if (wp == NULL)
5725 {
5726 /* quickfix list */
5727 qi->qf_curlist = 0;
5728 qi->qf_listcount = 0;
5729 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005730 else if (IS_LL_WINDOW(orig_wp))
5731 {
5732 /* If the location list window is open, then create a new empty
5733 * location list */
5734 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005735
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005736 /* first free the list reference in the location list window */
5737 ll_free_all(&orig_wp->w_llist_ref);
5738
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005739 orig_wp->w_llist_ref = new_ll;
5740 if (llwin != NULL)
5741 {
5742 llwin->w_llist = new_ll;
5743 new_ll->qf_refcount++;
5744 }
5745 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005746}
5747
Bram Moolenaard823fa92016-08-12 16:29:27 +02005748/*
5749 * Populate the quickfix list with the items supplied in the list
5750 * of dictionaries. "title" will be copied to w:quickfix_title.
5751 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5752 */
5753 int
5754set_errorlist(
5755 win_T *wp,
5756 list_T *list,
5757 int action,
5758 char_u *title,
5759 dict_T *what)
5760{
5761 qf_info_T *qi = &ql_info;
5762 int retval = OK;
5763
5764 if (wp != NULL)
5765 {
5766 qi = ll_get_or_alloc_list(wp);
5767 if (qi == NULL)
5768 return FAIL;
5769 }
5770
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005771 if (action == 'f')
5772 {
5773 /* Free the entire quickfix or location list stack */
5774 qf_free_stack(wp, qi);
5775 }
5776 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005777 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005778 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005779 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005780 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005781 if (retval == OK)
5782 qf_list_changed(qi, qi->qf_curlist);
5783 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005784
5785 return retval;
5786}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005787
5788 static int
5789mark_quickfix_ctx(qf_info_T *qi, int copyID)
5790{
5791 int i;
5792 int abort = FALSE;
5793 typval_T *ctx;
5794
5795 for (i = 0; i < LISTCOUNT && !abort; ++i)
5796 {
5797 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005798 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5799 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005800 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5801 }
5802
5803 return abort;
5804}
5805
5806/*
5807 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005808 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005809 */
5810 int
5811set_ref_in_quickfix(int copyID)
5812{
5813 int abort = FALSE;
5814 tabpage_T *tp;
5815 win_T *win;
5816
5817 abort = mark_quickfix_ctx(&ql_info, copyID);
5818 if (abort)
5819 return abort;
5820
5821 FOR_ALL_TAB_WINDOWS(tp, win)
5822 {
5823 if (win->w_llist != NULL)
5824 {
5825 abort = mark_quickfix_ctx(win->w_llist, copyID);
5826 if (abort)
5827 return abort;
5828 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005829 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5830 {
5831 /* In a location list window and none of the other windows is
5832 * referring to this location list. Mark the location list
5833 * context as still in use.
5834 */
5835 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5836 if (abort)
5837 return abort;
5838 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005839 }
5840
5841 return abort;
5842}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005843#endif
5844
Bram Moolenaar81695252004-12-29 20:58:21 +00005845/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005846 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005847 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005848 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005849 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005850 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005851 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005852 */
5853 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005854ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005855{
5856 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005857 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005858 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005859 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005860
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005861 switch (eap->cmdidx)
5862 {
5863 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5864 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5865 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5866 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5867 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5868 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5869 default: break;
5870 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005871 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5872 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005873 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005874#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005875 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005876 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005877#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005878 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005879
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005880 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005881 if (eap->cmdidx == CMD_lbuffer
5882 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005883 || eap->cmdidx == CMD_laddbuffer)
5884 {
5885 qi = ll_get_or_alloc_list(curwin);
5886 if (qi == NULL)
5887 return;
5888 }
5889
Bram Moolenaar86b68352004-12-27 21:59:20 +00005890 if (*eap->arg == NUL)
5891 buf = curbuf;
5892 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5893 buf = buflist_findnr(atoi((char *)eap->arg));
5894 if (buf == NULL)
5895 EMSG(_(e_invarg));
5896 else if (buf->b_ml.ml_mfp == NULL)
5897 EMSG(_("E681: Buffer is not loaded"));
5898 else
5899 {
5900 if (eap->addr_count == 0)
5901 {
5902 eap->line1 = 1;
5903 eap->line2 = buf->b_ml.ml_line_count;
5904 }
5905 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5906 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5907 EMSG(_(e_invrange));
5908 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005909 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005910 char_u *qf_title = *eap->cmdlinep;
5911
5912 if (buf->b_sfname)
5913 {
5914 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5915 (char *)qf_title, (char *)buf->b_sfname);
5916 qf_title = IObuff;
5917 }
5918
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005919 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005920 (eap->cmdidx != CMD_caddbuffer
5921 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005922 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005923 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005924 if (res >= 0)
5925 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005926 if (au_name != NULL)
5927 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5928 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005929 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5930 eap->cmdidx == CMD_lbuffer))
5931 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005932 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005933 }
5934}
5935
Bram Moolenaar1e015462005-09-25 22:16:38 +00005936#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005937/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005938 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5939 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005940 */
5941 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005942ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005943{
5944 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005945 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005946 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005947 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005948
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005949 switch (eap->cmdidx)
5950 {
5951 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5952 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5953 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5954 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5955 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5956 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5957 default: break;
5958 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005959 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5960 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005961 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005962#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005963 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005964 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005965#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005966 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005967
Bram Moolenaar3c097222017-12-21 20:54:49 +01005968 if (eap->cmdidx == CMD_lexpr
5969 || eap->cmdidx == CMD_lgetexpr
5970 || eap->cmdidx == CMD_laddexpr)
5971 {
5972 qi = ll_get_or_alloc_list(curwin);
5973 if (qi == NULL)
5974 return;
5975 }
5976
Bram Moolenaar4770d092006-01-12 23:22:24 +00005977 /* Evaluate the expression. When the result is a string or a list we can
5978 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005979 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005980 if (tv != NULL)
5981 {
5982 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5983 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5984 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005985 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005986 (eap->cmdidx != CMD_caddexpr
5987 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005988 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005989 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005990 if (res >= 0)
5991 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005992 if (au_name != NULL)
5993 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5994 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005995 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5996 eap->cmdidx == CMD_lexpr))
5997 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005998 }
5999 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006000 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006001 free_tv(tv);
6002 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006003}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006004#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006005
6006/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006007 * Get the location list for ":lhelpgrep"
6008 */
6009 static qf_info_T *
6010hgr_get_ll(int *new_ll)
6011{
6012 win_T *wp;
6013 qf_info_T *qi;
6014
6015 /* If the current window is a help window, then use it */
6016 if (bt_help(curwin->w_buffer))
6017 wp = curwin;
6018 else
6019 /* Find an existing help window */
6020 FOR_ALL_WINDOWS(wp)
6021 if (bt_help(wp->w_buffer))
6022 break;
6023
6024 if (wp == NULL) /* Help window not found */
6025 qi = NULL;
6026 else
6027 qi = wp->w_llist;
6028
6029 if (qi == NULL)
6030 {
6031 /* Allocate a new location list for help text matches */
6032 if ((qi = ll_new_list()) == NULL)
6033 return NULL;
6034 *new_ll = TRUE;
6035 }
6036
6037 return qi;
6038}
6039
6040/*
6041 * Search for a pattern in a help file.
6042 */
6043 static void
6044hgr_search_file(
6045 qf_info_T *qi,
6046 char_u *fname,
6047#ifdef FEAT_MBYTE
6048 vimconv_T *p_vc,
6049#endif
6050 regmatch_T *p_regmatch)
6051{
6052 FILE *fd;
6053 long lnum;
6054
6055 fd = mch_fopen((char *)fname, "r");
6056 if (fd == NULL)
6057 return;
6058
6059 lnum = 1;
6060 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6061 {
6062 char_u *line = IObuff;
6063#ifdef FEAT_MBYTE
6064 /* Convert a line if 'encoding' is not utf-8 and
6065 * the line contains a non-ASCII character. */
6066 if (p_vc->vc_type != CONV_NONE
6067 && has_non_ascii(IObuff))
6068 {
6069 line = string_convert(p_vc, IObuff, NULL);
6070 if (line == NULL)
6071 line = IObuff;
6072 }
6073#endif
6074
6075 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6076 {
6077 int l = (int)STRLEN(line);
6078
6079 /* remove trailing CR, LF, spaces, etc. */
6080 while (l > 0 && line[l - 1] <= ' ')
6081 line[--l] = NUL;
6082
6083 if (qf_add_entry(qi,
6084 qi->qf_curlist,
6085 NULL, /* dir */
6086 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006087 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006088 0,
6089 line,
6090 lnum,
6091 (int)(p_regmatch->startp[0] - line)
6092 + 1, /* col */
6093 FALSE, /* vis_col */
6094 NULL, /* search pattern */
6095 0, /* nr */
6096 1, /* type */
6097 TRUE /* valid */
6098 ) == FAIL)
6099 {
6100 got_int = TRUE;
6101#ifdef FEAT_MBYTE
6102 if (line != IObuff)
6103 vim_free(line);
6104#endif
6105 break;
6106 }
6107 }
6108#ifdef FEAT_MBYTE
6109 if (line != IObuff)
6110 vim_free(line);
6111#endif
6112 ++lnum;
6113 line_breakcheck();
6114 }
6115 fclose(fd);
6116}
6117
6118/*
6119 * Search for a pattern in all the help files in the doc directory under
6120 * the given directory.
6121 */
6122 static void
6123hgr_search_files_in_dir(
6124 qf_info_T *qi,
6125 char_u *dirname,
6126 regmatch_T *p_regmatch
6127#ifdef FEAT_MBYTE
6128 , vimconv_T *p_vc
6129#endif
6130#ifdef FEAT_MULTI_LANG
6131 , char_u *lang
6132#endif
6133 )
6134{
6135 int fcount;
6136 char_u **fnames;
6137 int fi;
6138
6139 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6140 add_pathsep(dirname);
6141 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6142 if (gen_expand_wildcards(1, &dirname, &fcount,
6143 &fnames, EW_FILE|EW_SILENT) == OK
6144 && fcount > 0)
6145 {
6146 for (fi = 0; fi < fcount && !got_int; ++fi)
6147 {
6148#ifdef FEAT_MULTI_LANG
6149 /* Skip files for a different language. */
6150 if (lang != NULL
6151 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006152 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006153 && !(STRNICMP(lang, "en", 2) == 0
6154 && STRNICMP("txt", fnames[fi]
6155 + STRLEN(fnames[fi]) - 3, 3) == 0))
6156 continue;
6157#endif
6158
6159 hgr_search_file(qi, fnames[fi],
6160#ifdef FEAT_MBYTE
6161 p_vc,
6162#endif
6163 p_regmatch);
6164 }
6165 FreeWild(fcount, fnames);
6166 }
6167}
6168
6169/*
6170 * Search for a pattern in all the help files in the 'runtimepath'.
6171 */
6172 static void
6173hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6174{
6175 char_u *p;
6176#ifdef FEAT_MULTI_LANG
6177 char_u *lang;
6178#endif
6179
6180#ifdef FEAT_MBYTE
6181 vimconv_T vc;
6182
6183 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6184 * differs. */
6185 vc.vc_type = CONV_NONE;
6186 if (!enc_utf8)
6187 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6188#endif
6189
6190#ifdef FEAT_MULTI_LANG
6191 /* Check for a specified language */
6192 lang = check_help_lang(arg);
6193#endif
6194
6195 /* Go through all directories in 'runtimepath' */
6196 p = p_rtp;
6197 while (*p != NUL && !got_int)
6198 {
6199 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6200
6201 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6202#ifdef FEAT_MBYTE
6203 , &vc
6204#endif
6205#ifdef FEAT_MULTI_LANG
6206 , lang
6207#endif
6208 );
6209 }
6210
6211#ifdef FEAT_MBYTE
6212 if (vc.vc_type != CONV_NONE)
6213 convert_setup(&vc, NULL, NULL);
6214#endif
6215}
6216
6217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 * ":helpgrep {pattern}"
6219 */
6220 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006221ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 regmatch_T regmatch;
6224 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006225 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006226 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006227 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228
Bram Moolenaar73633f82012-01-20 13:39:07 +01006229 switch (eap->cmdidx)
6230 {
6231 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6232 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6233 default: break;
6234 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006235 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6236 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006237 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006238#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006239 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006240 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006241#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006242 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006243
6244 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6245 save_cpo = p_cpo;
6246 p_cpo = empty_option;
6247
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006248 if (eap->cmdidx == CMD_lhelpgrep)
6249 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006250 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006251 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006252 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006253 }
6254
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6256 regmatch.rm_ic = FALSE;
6257 if (regmatch.regprog != NULL)
6258 {
6259 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006260 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006262 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006263
Bram Moolenaar473de612013-06-08 18:19:48 +02006264 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006266 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6267 qi->qf_lists[qi->qf_curlist].qf_ptr =
6268 qi->qf_lists[qi->qf_curlist].qf_start;
6269 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006272 if (p_cpo == empty_option)
6273 p_cpo = save_cpo;
6274 else
6275 /* Darn, some plugin changed the value. */
6276 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
Bram Moolenaarb254af32017-12-18 19:48:58 +01006278 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006279 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
Bram Moolenaar73633f82012-01-20 13:39:07 +01006281 if (au_name != NULL)
6282 {
6283 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6284 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006285 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006286 /* autocommands made "qi" invalid */
6287 return;
6288 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006289
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006291 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006292 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006293 else
6294 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006295
6296 if (eap->cmdidx == CMD_lhelpgrep)
6297 {
6298 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006299 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006300 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006301 {
6302 if (new_qi)
6303 ll_free_all(&qi);
6304 }
6305 else if (curwin->w_llist == NULL)
6306 curwin->w_llist = qi;
6307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308}
6309
6310#endif /* FEAT_QUICKFIX */