blob: dbd42ee4a1e61288b95644af21b1f5b487609edb [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] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200216 {
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\\+"},
226 {'s', ".\\+"},
227 {'o', ".\\+"}
228 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200229
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,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200507 QF_IGNORE_LINE = 4,
508 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200509};
510
511typedef struct {
512 char_u *linebuf;
513 int linelen;
514 char_u *growbuf;
515 int growbufsiz;
516 FILE *fd;
517 typval_T *tv;
518 char_u *p_str;
519 listitem_T *p_li;
520 buf_T *buf;
521 linenr_T buflnum;
522 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100523 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200524} qfstate_T;
525
526 static char_u *
527qf_grow_linebuf(qfstate_T *state, int newsz)
528{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200529 char_u *p;
530
Bram Moolenaare0d37972016-07-15 22:36:01 +0200531 /*
532 * If the line exceeds LINE_MAXLEN exclude the last
533 * byte since it's not a NL character.
534 */
535 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
536 if (state->growbuf == NULL)
537 {
538 state->growbuf = alloc(state->linelen + 1);
539 if (state->growbuf == NULL)
540 return NULL;
541 state->growbufsiz = state->linelen;
542 }
543 else if (state->linelen > state->growbufsiz)
544 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200545 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200546 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200547 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200548 state->growbufsiz = state->linelen;
549 }
550 return state->growbuf;
551}
552
553/*
554 * Get the next string (separated by newline) from state->p_str.
555 */
556 static int
557qf_get_next_str_line(qfstate_T *state)
558{
559 /* Get the next line from the supplied string */
560 char_u *p_str = state->p_str;
561 char_u *p;
562 int len;
563
564 if (*p_str == NUL) /* Reached the end of the string */
565 return QF_END_OF_INPUT;
566
567 p = vim_strchr(p_str, '\n');
568 if (p != NULL)
569 len = (int)(p - p_str) + 1;
570 else
571 len = (int)STRLEN(p_str);
572
573 if (len > IOSIZE - 2)
574 {
575 state->linebuf = qf_grow_linebuf(state, len);
576 if (state->linebuf == NULL)
577 return QF_NOMEM;
578 }
579 else
580 {
581 state->linebuf = IObuff;
582 state->linelen = len;
583 }
584 vim_strncpy(state->linebuf, p_str, state->linelen);
585
586 /*
587 * Increment using len in order to discard the rest of the
588 * line if it exceeds LINE_MAXLEN.
589 */
590 p_str += len;
591 state->p_str = p_str;
592
593 return QF_OK;
594}
595
596/*
597 * Get the next string from state->p_Li.
598 */
599 static int
600qf_get_next_list_line(qfstate_T *state)
601{
602 listitem_T *p_li = state->p_li;
603 int len;
604
605 while (p_li != NULL
606 && (p_li->li_tv.v_type != VAR_STRING
607 || p_li->li_tv.vval.v_string == NULL))
608 p_li = p_li->li_next; /* Skip non-string items */
609
610 if (p_li == NULL) /* End of the list */
611 {
612 state->p_li = NULL;
613 return QF_END_OF_INPUT;
614 }
615
616 len = (int)STRLEN(p_li->li_tv.vval.v_string);
617 if (len > IOSIZE - 2)
618 {
619 state->linebuf = qf_grow_linebuf(state, len);
620 if (state->linebuf == NULL)
621 return QF_NOMEM;
622 }
623 else
624 {
625 state->linebuf = IObuff;
626 state->linelen = len;
627 }
628
629 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
630
631 state->p_li = p_li->li_next; /* next item */
632 return QF_OK;
633}
634
635/*
636 * Get the next string from state->buf.
637 */
638 static int
639qf_get_next_buf_line(qfstate_T *state)
640{
641 char_u *p_buf = NULL;
642 int len;
643
644 /* Get the next line from the supplied buffer */
645 if (state->buflnum > state->lnumlast)
646 return QF_END_OF_INPUT;
647
648 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
649 state->buflnum += 1;
650
651 len = (int)STRLEN(p_buf);
652 if (len > IOSIZE - 2)
653 {
654 state->linebuf = qf_grow_linebuf(state, len);
655 if (state->linebuf == NULL)
656 return QF_NOMEM;
657 }
658 else
659 {
660 state->linebuf = IObuff;
661 state->linelen = len;
662 }
663 vim_strncpy(state->linebuf, p_buf, state->linelen);
664
665 return QF_OK;
666}
667
668/*
669 * Get the next string from file state->fd.
670 */
671 static int
672qf_get_next_file_line(qfstate_T *state)
673{
674 int discard;
675 int growbuflen;
676
677 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
678 return QF_END_OF_INPUT;
679
680 discard = FALSE;
681 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200682 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200683 {
684 /*
685 * The current line exceeds IObuff, continue reading using
686 * growbuf until EOL or LINE_MAXLEN bytes is read.
687 */
688 if (state->growbuf == NULL)
689 {
690 state->growbufsiz = 2 * (IOSIZE - 1);
691 state->growbuf = alloc(state->growbufsiz);
692 if (state->growbuf == NULL)
693 return QF_NOMEM;
694 }
695
696 /* Copy the read part of the line, excluding null-terminator */
697 memcpy(state->growbuf, IObuff, IOSIZE - 1);
698 growbuflen = state->linelen;
699
700 for (;;)
701 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200702 char_u *p;
703
Bram Moolenaare0d37972016-07-15 22:36:01 +0200704 if (fgets((char *)state->growbuf + growbuflen,
705 state->growbufsiz - growbuflen, state->fd) == NULL)
706 break;
707 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
708 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200709 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200710 break;
711 if (state->growbufsiz == LINE_MAXLEN)
712 {
713 discard = TRUE;
714 break;
715 }
716
717 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
718 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200719 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200720 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200721 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200722 }
723
724 while (discard)
725 {
726 /*
727 * The current line is longer than LINE_MAXLEN, continue
728 * reading but discard everything until EOL or EOF is
729 * reached.
730 */
731 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
732 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200733 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200734 break;
735 }
736
737 state->linebuf = state->growbuf;
738 state->linelen = growbuflen;
739 }
740 else
741 state->linebuf = IObuff;
742
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100743#ifdef FEAT_MBYTE
744 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200745 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
746 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100747 char_u *line;
748
749 line = string_convert(&state->vc, state->linebuf, &state->linelen);
750 if (line != NULL)
751 {
752 if (state->linelen < IOSIZE)
753 {
754 STRCPY(state->linebuf, line);
755 vim_free(line);
756 }
757 else
758 {
759 vim_free(state->growbuf);
760 state->linebuf = state->growbuf = line;
761 state->growbufsiz = state->linelen < LINE_MAXLEN
762 ? state->linelen : LINE_MAXLEN;
763 }
764 }
765 }
766#endif
767
Bram Moolenaare0d37972016-07-15 22:36:01 +0200768 return QF_OK;
769}
770
771/*
772 * Get the next string from a file/buffer/list/string.
773 */
774 static int
775qf_get_nextline(qfstate_T *state)
776{
777 int status = QF_FAIL;
778
779 if (state->fd == NULL)
780 {
781 if (state->tv != NULL)
782 {
783 if (state->tv->v_type == VAR_STRING)
784 /* Get the next line from the supplied string */
785 status = qf_get_next_str_line(state);
786 else if (state->tv->v_type == VAR_LIST)
787 /* Get the next line from the supplied list */
788 status = qf_get_next_list_line(state);
789 }
790 else
791 /* Get the next line from the supplied buffer */
792 status = qf_get_next_buf_line(state);
793 }
794 else
795 /* Get the next line from the supplied file */
796 status = qf_get_next_file_line(state);
797
798 if (status != QF_OK)
799 return status;
800
801 /* remove newline/CR from the line */
802 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200803 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804 state->linebuf[state->linelen - 1] = NUL;
805#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200806 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
807 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200809 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200810
811#ifdef FEAT_MBYTE
812 remove_bom(state->linebuf);
813#endif
814
815 return QF_OK;
816}
817
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200818typedef struct {
819 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200820 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200821 char_u *errmsg;
822 int errmsglen;
823 long lnum;
824 int col;
825 char_u use_viscol;
826 char_u *pattern;
827 int enr;
828 int type;
829 int valid;
830} qffields_T;
831
832/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200833 * Parse the error format matches in 'regmatch' and set the values in 'fields'.
834 * fmt_ptr contains the 'efm' format specifiers/prefixes that have a match.
835 * Returns QF_OK if all the matches are successfully parsed. On failure,
836 * returns QF_FAIL or QF_NOMEM.
837 */
838 static int
839qf_parse_match(
840 char_u *linebuf,
841 int linelen,
842 efm_T *fmt_ptr,
843 regmatch_T *regmatch,
844 qffields_T *fields,
845 int qf_multiline,
846 int qf_multiscan,
847 char_u **tail)
848{
849 char_u *p;
850 int idx = fmt_ptr->prefix;
851 int i;
852 int len;
853
854 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
855 return QF_FAIL;
856 if (vim_strchr((char_u *)"EWI", idx) != NULL)
857 fields->type = idx;
858 else
859 fields->type = 0;
860 /*
861 * Extract error message data from matched line.
862 * We check for an actual submatch, because "\[" and "\]" in
863 * the 'errorformat' may cause the wrong submatch to be used.
864 */
865 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
866 {
867 int c;
868
869 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
870 return QF_FAIL;
871
872 /* Expand ~/file and $HOME/file to full path. */
873 c = *regmatch->endp[i];
874 *regmatch->endp[i] = NUL;
875 expand_env(regmatch->startp[i], fields->namebuf, CMDBUFFSIZE);
876 *regmatch->endp[i] = c;
877
878 if (vim_strchr((char_u *)"OPQ", idx) != NULL
879 && mch_getperm(fields->namebuf) == -1)
880 return QF_FAIL;
881 }
882 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
883 {
884 if (regmatch->startp[i] == NULL)
885 return QF_FAIL;
886 fields->enr = (int)atol((char *)regmatch->startp[i]);
887 }
888 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
889 {
890 if (regmatch->startp[i] == NULL)
891 return QF_FAIL;
892 fields->lnum = atol((char *)regmatch->startp[i]);
893 }
894 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
895 {
896 if (regmatch->startp[i] == NULL)
897 return QF_FAIL;
898 fields->col = (int)atol((char *)regmatch->startp[i]);
899 }
900 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
901 {
902 if (regmatch->startp[i] == NULL)
903 return QF_FAIL;
904 fields->type = *regmatch->startp[i];
905 }
906 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
907 {
908 if (linelen >= fields->errmsglen)
909 {
910 /* linelen + null terminator */
911 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
912 return QF_NOMEM;
913 fields->errmsg = p;
914 fields->errmsglen = linelen + 1;
915 }
916 vim_strncpy(fields->errmsg, linebuf, linelen);
917 }
918 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
919 {
920 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
921 return QF_FAIL;
922 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
923 if (len >= fields->errmsglen)
924 {
925 /* len + null terminator */
926 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
927 return QF_NOMEM;
928 fields->errmsg = p;
929 fields->errmsglen = len + 1;
930 }
931 vim_strncpy(fields->errmsg, regmatch->startp[i], len);
932 }
933 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
934 {
935 if (regmatch->startp[i] == NULL)
936 return QF_FAIL;
937 *tail = regmatch->startp[i];
938 }
939 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
940 {
941 char_u *match_ptr;
942
943 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
944 return QF_FAIL;
945 fields->col = 0;
946 for (match_ptr = regmatch->startp[i];
947 match_ptr != regmatch->endp[i]; ++match_ptr)
948 {
949 ++fields->col;
950 if (*match_ptr == TAB)
951 {
952 fields->col += 7;
953 fields->col -= fields->col % 8;
954 }
955 }
956 ++fields->col;
957 fields->use_viscol = TRUE;
958 }
959 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
960 {
961 if (regmatch->startp[i] == NULL)
962 return QF_FAIL;
963 fields->col = (int)atol((char *)regmatch->startp[i]);
964 fields->use_viscol = TRUE;
965 }
966 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
967 {
968 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
969 return QF_FAIL;
970 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
971 if (len > CMDBUFFSIZE - 5)
972 len = CMDBUFFSIZE - 5;
973 STRCPY(fields->pattern, "^\\V");
974 STRNCAT(fields->pattern, regmatch->startp[i], len);
975 fields->pattern[len + 3] = '\\';
976 fields->pattern[len + 4] = '$';
977 fields->pattern[len + 5] = NUL;
978 }
979 if ((i = (int)fmt_ptr->addr[10]) > 0) /* %o */
980 {
981 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
982 return QF_FAIL;
983 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
984 if (len > CMDBUFFSIZE)
985 len = CMDBUFFSIZE;
986 STRNCAT(fields->module, regmatch->startp[i], len);
987 }
988
989 return QF_OK;
990}
991
992/*
993 * Parse an error line in 'linebuf' using a single error format string in
994 * 'fmt_ptr->prog' and return the matching values in 'fields'.
995 * Returns QF_OK if the efm format matches completely and the fields are
996 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
997 */
998 static int
999qf_parse_get_fields(
1000 char_u *linebuf,
1001 int linelen,
1002 efm_T *fmt_ptr,
1003 qffields_T *fields,
1004 int qf_multiline,
1005 int qf_multiscan,
1006 char_u **tail)
1007{
1008 regmatch_T regmatch;
1009 int status = QF_FAIL;
1010 int r;
1011
1012 if (qf_multiscan &&
1013 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1014 return QF_FAIL;
1015
1016 fields->namebuf[0] = NUL;
1017 fields->module[0] = NUL;
1018 fields->pattern[0] = NUL;
1019 if (!qf_multiscan)
1020 fields->errmsg[0] = NUL;
1021 fields->lnum = 0;
1022 fields->col = 0;
1023 fields->use_viscol = FALSE;
1024 fields->enr = -1;
1025 fields->type = 0;
1026 *tail = NULL;
1027
1028 regmatch.regprog = fmt_ptr->prog;
1029 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1030 fmt_ptr->prog = regmatch.regprog;
1031 if (r)
1032 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1033 fields, qf_multiline, qf_multiscan, tail);
1034
1035 return status;
1036}
1037
1038/*
1039 * Parse directory error format prefixes (%D and %X).
1040 * Push and pop directories from the directory stack when scanning directory
1041 * names.
1042 */
1043 static int
1044qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1045{
1046 if (idx == 'D') /* enter directory */
1047 {
1048 if (*fields->namebuf == NUL)
1049 {
1050 EMSG(_("E379: Missing or empty directory name"));
1051 return QF_FAIL;
1052 }
1053 qfl->qf_directory =
1054 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1055 if (qfl->qf_directory == NULL)
1056 return QF_FAIL;
1057 }
1058 else if (idx == 'X') /* leave directory */
1059 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1060
1061 return QF_OK;
1062}
1063
1064/*
1065 * Parse global file name error format prefixes (%O, %P and %Q).
1066 */
1067 static int
1068qf_parse_file_pfx(
1069 int idx,
1070 qffields_T *fields,
1071 qf_list_T *qfl,
1072 char_u *tail)
1073{
1074 fields->valid = FALSE;
1075 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1076 {
1077 if (*fields->namebuf && idx == 'P')
1078 qfl->qf_currfile =
1079 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1080 else if (idx == 'Q')
1081 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1082 *fields->namebuf = NUL;
1083 if (tail && *tail)
1084 {
1085 STRMOVE(IObuff, skipwhite(tail));
1086 qfl->qf_multiscan = TRUE;
1087 return QF_MULTISCAN;
1088 }
1089 }
1090
1091 return QF_OK;
1092}
1093
1094/*
1095 * Parse a non-error line (a line which doesn't match any of the error
1096 * format in 'efm').
1097 */
1098 static int
1099qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1100{
1101 char_u *p;
1102
1103 fields->namebuf[0] = NUL; /* no match found, remove file name */
1104 fields->lnum = 0; /* don't jump to this line */
1105 fields->valid = FALSE;
1106 if (linelen >= fields->errmsglen)
1107 {
1108 /* linelen + null terminator */
1109 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1110 return QF_NOMEM;
1111 fields->errmsg = p;
1112 fields->errmsglen = linelen + 1;
1113 }
1114 /* copy whole line to error message */
1115 vim_strncpy(fields->errmsg, linebuf, linelen);
1116
1117 return QF_OK;
1118}
1119
1120/*
1121 * Parse multi-line error format prefixes (%C and %Z)
1122 */
1123 static int
1124qf_parse_multiline_pfx(
1125 qf_info_T *qi,
1126 int qf_idx,
1127 int idx,
1128 qf_list_T *qfl,
1129 qffields_T *fields)
1130{
1131 char_u *ptr;
1132 int len;
1133
1134 if (!qfl->qf_multiignore)
1135 {
1136 qfline_T *qfprev = qfl->qf_last;
1137
1138 if (qfprev == NULL)
1139 return QF_FAIL;
1140 if (*fields->errmsg && !qfl->qf_multiignore)
1141 {
1142 len = (int)STRLEN(qfprev->qf_text);
1143 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1144 == NULL)
1145 return QF_FAIL;
1146 STRCPY(ptr, qfprev->qf_text);
1147 vim_free(qfprev->qf_text);
1148 qfprev->qf_text = ptr;
1149 *(ptr += len) = '\n';
1150 STRCPY(++ptr, fields->errmsg);
1151 }
1152 if (qfprev->qf_nr == -1)
1153 qfprev->qf_nr = fields->enr;
1154 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1155 /* only printable chars allowed */
1156 qfprev->qf_type = fields->type;
1157
1158 if (!qfprev->qf_lnum)
1159 qfprev->qf_lnum = fields->lnum;
1160 if (!qfprev->qf_col)
1161 qfprev->qf_col = fields->col;
1162 qfprev->qf_viscol = fields->use_viscol;
1163 if (!qfprev->qf_fnum)
1164 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1165 qfl->qf_directory,
1166 *fields->namebuf || qfl->qf_directory != NULL
1167 ? fields->namebuf
1168 : qfl->qf_currfile != NULL && fields->valid
1169 ? qfl->qf_currfile : 0);
1170 }
1171 if (idx == 'Z')
1172 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1173 line_breakcheck();
1174
1175 return QF_IGNORE_LINE;
1176}
1177
1178/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001179 * Parse a line and get the quickfix fields.
1180 * Return the QF_ status.
1181 */
1182 static int
1183qf_parse_line(
1184 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001185 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001186 char_u *linebuf,
1187 int linelen,
1188 efm_T *fmt_first,
1189 qffields_T *fields)
1190{
1191 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001192 int idx = 0;
1193 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001194 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001195 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001196
Bram Moolenaare333e792018-04-08 13:27:39 +02001197restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001198 /* If there was no %> item start at the first pattern */
1199 if (fmt_start == NULL)
1200 fmt_ptr = fmt_first;
1201 else
1202 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001203 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001204 fmt_ptr = fmt_start;
1205 fmt_start = NULL;
1206 }
1207
1208 /*
1209 * Try to match each part of 'errorformat' until we find a complete
1210 * match or no match.
1211 */
1212 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001213 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1214 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001215 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001216 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1217 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1218 if (status == QF_NOMEM)
1219 return status;
1220 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001221 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001222 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001223 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001224
1225 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1226 {
1227 if (fmt_ptr != NULL)
1228 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001229 /* 'D' and 'X' directory specifiers */
1230 status = qf_parse_dir_pfx(idx, fields, qfl);
1231 if (status != QF_OK)
1232 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001233 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001234
1235 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1236 if (status != QF_OK)
1237 return status;
1238
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001239 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001240 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001241 }
1242 else if (fmt_ptr != NULL)
1243 {
1244 /* honor %> item */
1245 if (fmt_ptr->conthere)
1246 fmt_start = fmt_ptr;
1247
1248 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1249 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001250 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1251 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001252 }
1253 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1254 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001255 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1256 if (status != QF_OK)
1257 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001258 }
1259 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001260 { /* global file names */
1261 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1262 if (status == QF_MULTISCAN)
1263 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001264 }
1265 if (fmt_ptr->flags == '-') /* generally exclude this line */
1266 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001267 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001268 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001269 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001270 return QF_IGNORE_LINE;
1271 }
1272 }
1273
1274 return QF_OK;
1275}
1276
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001277/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001278 * Read the errorfile "efile" into memory, line by line, building the error
1279 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001280 * Alternative: when "efile" is NULL read errors from buffer "buf".
1281 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001282 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001283 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1284 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001285 * Return -1 for error, number of errors for success.
1286 */
1287 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001288qf_init_ext(
1289 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001290 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001291 char_u *efile,
1292 buf_T *buf,
1293 typval_T *tv,
1294 char_u *errorformat,
1295 int newlist, /* TRUE: start a new error list */
1296 linenr_T lnumfirst, /* first line number to use */
1297 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001298 char_u *qf_title,
1299 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001300{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001301 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001302 qfstate_T state;
1303 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001304 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001305 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001306 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001308 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001310 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001312 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001313 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001314
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001315 vim_memset(&state, 0, sizeof(state));
1316 vim_memset(&fields, 0, sizeof(fields));
1317#ifdef FEAT_MBYTE
1318 state.vc.vc_type = CONV_NONE;
1319 if (enc != NULL && *enc != NUL)
1320 convert_setup(&state.vc, enc, p_enc);
1321#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001322 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaard76ce852018-05-01 15:02:04 +02001323 fields.module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001324 fields.errmsglen = CMDBUFFSIZE + 1;
1325 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1326 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02001327 if (fields.namebuf == NULL || fields.errmsg == NULL
Bram Moolenaard76ce852018-05-01 15:02:04 +02001328 || fields.pattern == NULL || fields.module == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 goto qf_init_end;
1330
Bram Moolenaare0d37972016-07-15 22:36:01 +02001331 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
1333 EMSG2(_(e_openerrf), efile);
1334 goto qf_init_end;
1335 }
1336
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001337 if (newlist || qf_idx == qi->qf_listcount)
1338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001340 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001341 qf_idx = qi->qf_curlist;
1342 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001343 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001344 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001345 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001346 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001347 if (qi->qf_lists[qf_idx].qf_count > 0)
1348 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001351 qfl = &qi->qf_lists[qf_idx];
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001354 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001355 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 else
1357 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001359 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001360 * If the errorformat didn't change between calls, then reuse the
1361 * previously parsed values.
1362 */
1363 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1364 {
1365 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001366 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001367 free_efm_list(&fmt_first);
1368
1369 /* parse the current 'efm' */
1370 fmt_first = parse_efm_option(efm);
1371 if (fmt_first != NULL)
1372 last_efm = vim_strsave(efm);
1373 }
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
1378 /*
1379 * got_int is reset here, because it was probably set when killing the
1380 * ":make" command, but we still want to read the errorfile then.
1381 */
1382 got_int = FALSE;
1383
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001384 if (tv != NULL)
1385 {
1386 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001387 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001388 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001389 state.p_li = tv->vval.v_list->lv_first;
1390 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001391 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001392 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001393 state.buflnum = lnumfirst;
1394 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 /*
1397 * Read the lines in the error file one by one.
1398 * Try to recognize one of the error formats in each line.
1399 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001400 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001402 /* Get the next line from a file/buffer/list/string */
1403 status = qf_get_nextline(&state);
1404 if (status == QF_NOMEM) /* memory alloc failure */
1405 goto qf_init_end;
1406 if (status == QF_END_OF_INPUT) /* end of input */
1407 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001409 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1410 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001411 if (status == QF_FAIL)
1412 goto error2;
1413 if (status == QF_NOMEM)
1414 goto qf_init_end;
1415 if (status == QF_IGNORE_LINE)
1416 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001418 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001419 qf_idx,
1420 qfl->qf_directory,
1421 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001422 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001423 : ((qfl->qf_currfile != NULL && fields.valid)
1424 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001425 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001426 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001427 fields.errmsg,
1428 fields.lnum,
1429 fields.col,
1430 fields.use_viscol,
1431 fields.pattern,
1432 fields.enr,
1433 fields.type,
1434 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 goto error2;
1436 line_breakcheck();
1437 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001438 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001440 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001442 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001443 qfl->qf_ptr = qfl->qf_start;
1444 qfl->qf_index = 1;
1445 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447 else
1448 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001449 qfl->qf_nonevalid = FALSE;
1450 if (qfl->qf_ptr == NULL)
1451 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001453 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001454 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001455 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457 EMSG(_(e_readerrf));
1458error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001459 if (!adding)
1460 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001461 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001462 qf_free(qi, qi->qf_curlist);
1463 qi->qf_listcount--;
1464 if (qi->qf_curlist > 0)
1465 --qi->qf_curlist;
1466 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001467qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001468 if (state.fd != NULL)
1469 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001470 vim_free(fields.namebuf);
Bram Moolenaard76ce852018-05-01 15:02:04 +02001471 vim_free(fields.module);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001472 vim_free(fields.errmsg);
1473 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001474 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001476 if (qf_idx == qi->qf_curlist)
1477 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001478#ifdef FEAT_MBYTE
1479 if (state.vc.vc_type != CONV_NONE)
1480 convert_setup(&state.vc, NULL, NULL);
1481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
1483 return retval;
1484}
1485
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001486/*
1487 * Set the title of the specified quickfix list. Frees the previous title.
1488 * Prepends ':' to the title.
1489 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001490 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001491qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001492{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001493 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001494
Bram Moolenaarfb604092014-07-23 15:55:00 +02001495 if (title != NULL)
1496 {
1497 char_u *p = alloc((int)STRLEN(title) + 2);
1498
Bram Moolenaara3921f42017-06-04 15:30:34 +02001499 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001500 if (p != NULL)
1501 sprintf((char *)p, ":%s", (char *)title);
1502 }
1503}
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001506 * Prepare for adding a new quickfix list. If the current list is in the
1507 * middle of the stack, then all the following lists are freed and then
1508 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 */
1510 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001511qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 int i;
1514
1515 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001516 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 * the current entry. This makes it possible to browse in a tree-like
1518 * way with ":grep'.
1519 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001520 while (qi->qf_listcount > qi->qf_curlist + 1)
1521 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
1523 /*
1524 * When the stack is full, remove to oldest entry
1525 * Otherwise, add a new entry.
1526 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001527 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001529 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001531 qi->qf_lists[i - 1] = qi->qf_lists[i];
1532 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001535 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001536 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001537 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001538 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539}
1540
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001541/*
1542 * Free a location list
1543 */
1544 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001545ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001546{
1547 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001549
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001550 qi = *pqi;
1551 if (qi == NULL)
1552 return;
1553 *pqi = NULL; /* Remove reference to this list */
1554
1555 qi->qf_refcount--;
1556 if (qi->qf_refcount < 1)
1557 {
1558 /* No references to this location list */
1559 for (i = 0; i < qi->qf_listcount; ++i)
1560 qf_free(qi, i);
1561 vim_free(qi);
1562 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001563}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001564
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001565/*
1566 * Free all the quickfix/location lists in the stack.
1567 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001568 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001569qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001570{
1571 int i;
1572 qf_info_T *qi = &ql_info;
1573
1574 if (wp != NULL)
1575 {
1576 /* location list */
1577 ll_free_all(&wp->w_llist);
1578 ll_free_all(&wp->w_llist_ref);
1579 }
1580 else
1581 /* quickfix list */
1582 for (i = 0; i < qi->qf_listcount; ++i)
1583 qf_free(qi, i);
1584}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586/*
1587 * Add an entry to the end of the list of errors.
1588 * Returns OK or FAIL.
1589 */
1590 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001591qf_add_entry(
1592 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001593 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001594 char_u *dir, /* optional directory name */
1595 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001596 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001597 int bufnum, /* buffer number or zero */
1598 char_u *mesg, /* message */
1599 long lnum, /* line number */
1600 int col, /* column */
1601 int vis_col, /* using visual column */
1602 char_u *pattern, /* search pattern */
1603 int nr, /* error number */
1604 int type, /* type character */
1605 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001607 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001608 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001610 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001612 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001613 {
1614 buf_T *buf = buflist_findnr(bufnum);
1615
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001616 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001617 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001618 buf->b_has_qf_entry |=
1619 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001620 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001621 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001622 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1624 {
1625 vim_free(qfp);
1626 return FAIL;
1627 }
1628 qfp->qf_lnum = lnum;
1629 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001630 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001631 if (pattern == NULL || *pattern == NUL)
1632 qfp->qf_pattern = NULL;
1633 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1634 {
1635 vim_free(qfp->qf_text);
1636 vim_free(qfp);
1637 return FAIL;
1638 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001639 if (module == NULL || *module == NUL)
1640 qfp->qf_module = NULL;
1641 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1642 {
1643 vim_free(qfp->qf_text);
1644 vim_free(qfp->qf_pattern);
1645 vim_free(qfp);
1646 return FAIL;
1647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 qfp->qf_nr = nr;
1649 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1650 type = 0;
1651 qfp->qf_type = type;
1652 qfp->qf_valid = valid;
1653
Bram Moolenaara3921f42017-06-04 15:30:34 +02001654 lastp = &qi->qf_lists[qf_idx].qf_last;
1655 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001656 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001658 qi->qf_lists[qf_idx].qf_start = qfp;
1659 qi->qf_lists[qf_idx].qf_ptr = qfp;
1660 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001661 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 else
1664 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001665 qfp->qf_prev = *lastp;
1666 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001668 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001670 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001671 ++qi->qf_lists[qf_idx].qf_count;
1672 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001673 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001675 qi->qf_lists[qf_idx].qf_index =
1676 qi->qf_lists[qf_idx].qf_count;
1677 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679
1680 return OK;
1681}
1682
1683/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001684 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001685 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001686 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001687ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001688{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001689 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001690
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001691 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1692 if (qi != NULL)
1693 {
1694 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1695 qi->qf_refcount++;
1696 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001697
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001698 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001699}
1700
1701/*
1702 * Return the location list for window 'wp'.
1703 * If not present, allocate a location list
1704 */
1705 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001706ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001707{
1708 if (IS_LL_WINDOW(wp))
1709 /* For a location list window, use the referenced location list */
1710 return wp->w_llist_ref;
1711
1712 /*
1713 * For a non-location list window, w_llist_ref should not point to a
1714 * location list.
1715 */
1716 ll_free_all(&wp->w_llist_ref);
1717
1718 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001719 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001720 return wp->w_llist;
1721}
1722
1723/*
1724 * Copy the location list from window "from" to window "to".
1725 */
1726 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001727copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001728{
1729 qf_info_T *qi;
1730 int idx;
1731 int i;
1732
1733 /*
1734 * When copying from a location list window, copy the referenced
1735 * location list. For other windows, copy the location list for
1736 * that window.
1737 */
1738 if (IS_LL_WINDOW(from))
1739 qi = from->w_llist_ref;
1740 else
1741 qi = from->w_llist;
1742
1743 if (qi == NULL) /* no location list to copy */
1744 return;
1745
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001746 /* allocate a new location list */
1747 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001748 return;
1749
1750 to->w_llist->qf_listcount = qi->qf_listcount;
1751
1752 /* Copy the location lists one at a time */
1753 for (idx = 0; idx < qi->qf_listcount; idx++)
1754 {
1755 qf_list_T *from_qfl;
1756 qf_list_T *to_qfl;
1757
1758 to->w_llist->qf_curlist = idx;
1759
1760 from_qfl = &qi->qf_lists[idx];
1761 to_qfl = &to->w_llist->qf_lists[idx];
1762
1763 /* Some of the fields are populated by qf_add_entry() */
1764 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1765 to_qfl->qf_count = 0;
1766 to_qfl->qf_index = 0;
1767 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001768 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001769 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001770 if (from_qfl->qf_title != NULL)
1771 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1772 else
1773 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001774 if (from_qfl->qf_ctx != NULL)
1775 {
1776 to_qfl->qf_ctx = alloc_tv();
1777 if (to_qfl->qf_ctx != NULL)
1778 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1779 }
1780 else
1781 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001782
1783 if (from_qfl->qf_count)
1784 {
1785 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001786 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001787
1788 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001789 for (i = 0, from_qfp = from_qfl->qf_start;
1790 i < from_qfl->qf_count && from_qfp != NULL;
1791 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001792 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001793 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001794 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001795 NULL,
1796 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02001797 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001798 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001799 from_qfp->qf_text,
1800 from_qfp->qf_lnum,
1801 from_qfp->qf_col,
1802 from_qfp->qf_viscol,
1803 from_qfp->qf_pattern,
1804 from_qfp->qf_nr,
1805 0,
1806 from_qfp->qf_valid) == FAIL)
1807 {
1808 qf_free_all(to);
1809 return;
1810 }
1811 /*
1812 * qf_add_entry() will not set the qf_num field, as the
1813 * directory and file names are not supplied. So the qf_fnum
1814 * field is copied here.
1815 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001816 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001817 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1818 prevp->qf_type = from_qfp->qf_type; /* error type */
1819 if (from_qfl->qf_ptr == from_qfp)
1820 to_qfl->qf_ptr = prevp; /* current location */
1821 }
1822 }
1823
1824 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1825
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001826 /* Assign a new ID for the location list */
1827 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001828 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001829
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001830 /* When no valid entries are present in the list, qf_ptr points to
1831 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001832 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001833 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001834 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001835 to_qfl->qf_index = 1;
1836 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001837 }
1838
1839 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1840}
1841
1842/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001843 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001844 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001847qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
Bram Moolenaar82404332016-07-10 17:00:38 +02001849 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001850 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001851 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (fname == NULL || *fname == NUL) /* no file name */
1854 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaare60acc12011-05-10 16:41:25 +02001856#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001857 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001858#endif
1859#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001860 if (directory != NULL)
1861 slash_adjust(directory);
1862 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001863#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001864 if (directory != NULL && !vim_isAbsName(fname)
1865 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1866 {
1867 /*
1868 * Here we check if the file really exists.
1869 * This should normally be true, but if make works without
1870 * "leaving directory"-messages we might have missed a
1871 * directory change.
1872 */
1873 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001876 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001877 if (directory)
1878 ptr = concat_fnames(directory, fname, TRUE);
1879 else
1880 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001882 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001883 bufname = ptr;
1884 }
1885 else
1886 bufname = fname;
1887
1888 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001889 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001890 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001891 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001892 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001894 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001895 {
1896 vim_free(qf_last_bufname);
1897 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1898 if (bufname == ptr)
1899 qf_last_bufname = bufname;
1900 else
1901 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001902 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001903 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001904 if (buf == NULL)
1905 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001906
Bram Moolenaarc1542742016-07-20 21:44:37 +02001907 buf->b_has_qf_entry =
1908 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001909 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910}
1911
1912/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001913 * Push dirbuf onto the directory stack and return pointer to actual dir or
1914 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 */
1916 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001917qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918{
1919 struct dir_stack_T *ds_new;
1920 struct dir_stack_T *ds_ptr;
1921
1922 /* allocate new stack element and hook it in */
1923 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1924 if (ds_new == NULL)
1925 return NULL;
1926
1927 ds_new->next = *stackptr;
1928 *stackptr = ds_new;
1929
1930 /* store directory on the stack */
1931 if (vim_isAbsName(dirbuf)
1932 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001933 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 (*stackptr)->dirname = vim_strsave(dirbuf);
1935 else
1936 {
1937 /* Okay we don't have an absolute path.
1938 * dirbuf must be a subdir of one of the directories on the stack.
1939 * Let's search...
1940 */
1941 ds_new = (*stackptr)->next;
1942 (*stackptr)->dirname = NULL;
1943 while (ds_new)
1944 {
1945 vim_free((*stackptr)->dirname);
1946 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1947 TRUE);
1948 if (mch_isdir((*stackptr)->dirname) == TRUE)
1949 break;
1950
1951 ds_new = ds_new->next;
1952 }
1953
1954 /* clean up all dirs we already left */
1955 while ((*stackptr)->next != ds_new)
1956 {
1957 ds_ptr = (*stackptr)->next;
1958 (*stackptr)->next = (*stackptr)->next->next;
1959 vim_free(ds_ptr->dirname);
1960 vim_free(ds_ptr);
1961 }
1962
1963 /* Nothing found -> it must be on top level */
1964 if (ds_new == NULL)
1965 {
1966 vim_free((*stackptr)->dirname);
1967 (*stackptr)->dirname = vim_strsave(dirbuf);
1968 }
1969 }
1970
1971 if ((*stackptr)->dirname != NULL)
1972 return (*stackptr)->dirname;
1973 else
1974 {
1975 ds_ptr = *stackptr;
1976 *stackptr = (*stackptr)->next;
1977 vim_free(ds_ptr);
1978 return NULL;
1979 }
1980}
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/*
1983 * pop dirbuf from the directory stack and return previous directory or NULL if
1984 * stack is empty
1985 */
1986 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001987qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 struct dir_stack_T *ds_ptr;
1990
1991 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1992 * What to do if it isn't? */
1993
1994 /* pop top element and free it */
1995 if (*stackptr != NULL)
1996 {
1997 ds_ptr = *stackptr;
1998 *stackptr = (*stackptr)->next;
1999 vim_free(ds_ptr->dirname);
2000 vim_free(ds_ptr);
2001 }
2002
2003 /* return NEW top element as current dir or NULL if stack is empty*/
2004 return *stackptr ? (*stackptr)->dirname : NULL;
2005}
2006
2007/*
2008 * clean up directory stack
2009 */
2010 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002011qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
2013 struct dir_stack_T *ds_ptr;
2014
2015 while ((ds_ptr = *stackptr) != NULL)
2016 {
2017 *stackptr = (*stackptr)->next;
2018 vim_free(ds_ptr->dirname);
2019 vim_free(ds_ptr);
2020 }
2021}
2022
2023/*
2024 * Check in which directory of the directory stack the given file can be
2025 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002026 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 * Cleans up intermediate directory entries.
2028 *
2029 * TODO: How to solve the following problem?
2030 * If we have the this directory tree:
2031 * ./
2032 * ./aa
2033 * ./aa/bb
2034 * ./bb
2035 * ./bb/x.c
2036 * and make says:
2037 * making all in aa
2038 * making all in bb
2039 * x.c:9: Error
2040 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2041 * qf_guess_filepath will return NULL.
2042 */
2043 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002044qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 struct dir_stack_T *ds_ptr;
2047 struct dir_stack_T *ds_tmp;
2048 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002049 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002052 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 return NULL;
2054
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002055 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 fullname = NULL;
2057 while (ds_ptr)
2058 {
2059 vim_free(fullname);
2060 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2061
2062 /* If concat_fnames failed, just go on. The worst thing that can happen
2063 * is that we delete the entire stack.
2064 */
2065 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2066 break;
2067
2068 ds_ptr = ds_ptr->next;
2069 }
2070
2071 vim_free(fullname);
2072
2073 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002074 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002076 ds_tmp = qfl->qf_dir_stack->next;
2077 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 vim_free(ds_tmp->dirname);
2079 vim_free(ds_tmp);
2080 }
2081
2082 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083}
2084
2085/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002086 * Returns TRUE if a quickfix/location list with the given identifier exists.
2087 */
2088 static int
2089qflist_valid (win_T *wp, int_u qf_id)
2090{
2091 qf_info_T *qi = &ql_info;
2092 int i;
2093
2094 if (wp != NULL)
2095 {
2096 qi = GET_LOC_LIST(wp); /* Location list */
2097 if (qi == NULL)
2098 return FALSE;
2099 }
2100
2101 for (i = 0; i < qi->qf_listcount; ++i)
2102 if (qi->qf_lists[i].qf_id == qf_id)
2103 return TRUE;
2104
2105 return FALSE;
2106}
2107
2108/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002109 * When loading a file from the quickfix, the auto commands may modify it.
2110 * This may invalidate the current quickfix entry. This function checks
2111 * whether a entry is still present in the quickfix.
2112 * Similar to location list.
2113 */
2114 static int
2115is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
2116{
2117 qf_list_T *qfl;
2118 qfline_T *qfp;
2119 int i;
2120
2121 qfl = &qi->qf_lists[qi->qf_curlist];
2122
2123 /* Search for the entry in the current list */
2124 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2125 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002126 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002127 break;
2128
2129 if (i == qfl->qf_count) /* Entry is not found */
2130 return FALSE;
2131
2132 return TRUE;
2133}
2134
2135/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002136 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002137 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002138 */
2139 static qfline_T *
2140get_next_valid_entry(
2141 qf_info_T *qi,
2142 qfline_T *qf_ptr,
2143 int *qf_index,
2144 int dir)
2145{
2146 int idx;
2147 int old_qf_fnum;
2148
2149 idx = *qf_index;
2150 old_qf_fnum = qf_ptr->qf_fnum;
2151
2152 do
2153 {
2154 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2155 || qf_ptr->qf_next == NULL)
2156 return NULL;
2157 ++idx;
2158 qf_ptr = qf_ptr->qf_next;
2159 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2160 && !qf_ptr->qf_valid)
2161 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2162
2163 *qf_index = idx;
2164 return qf_ptr;
2165}
2166
2167/*
2168 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002169 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002170 */
2171 static qfline_T *
2172get_prev_valid_entry(
2173 qf_info_T *qi,
2174 qfline_T *qf_ptr,
2175 int *qf_index,
2176 int dir)
2177{
2178 int idx;
2179 int old_qf_fnum;
2180
2181 idx = *qf_index;
2182 old_qf_fnum = qf_ptr->qf_fnum;
2183
2184 do
2185 {
2186 if (idx == 1 || qf_ptr->qf_prev == NULL)
2187 return NULL;
2188 --idx;
2189 qf_ptr = qf_ptr->qf_prev;
2190 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2191 && !qf_ptr->qf_valid)
2192 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2193
2194 *qf_index = idx;
2195 return qf_ptr;
2196}
2197
2198/*
2199 * Get the n'th (errornr) previous/next valid entry from the current entry in
2200 * the quickfix list.
2201 * dir == FORWARD or FORWARD_FILE: next valid entry
2202 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2203 */
2204 static qfline_T *
2205get_nth_valid_entry(
2206 qf_info_T *qi,
2207 int errornr,
2208 qfline_T *qf_ptr,
2209 int *qf_index,
2210 int dir)
2211{
2212 qfline_T *prev_qf_ptr;
2213 int prev_index;
2214 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2215 char_u *err = e_no_more_items;
2216
2217 while (errornr--)
2218 {
2219 prev_qf_ptr = qf_ptr;
2220 prev_index = *qf_index;
2221
2222 if (dir == FORWARD || dir == FORWARD_FILE)
2223 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2224 else
2225 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2226 if (qf_ptr == NULL)
2227 {
2228 qf_ptr = prev_qf_ptr;
2229 *qf_index = prev_index;
2230 if (err != NULL)
2231 {
2232 EMSG(_(err));
2233 return NULL;
2234 }
2235 break;
2236 }
2237
2238 err = NULL;
2239 }
2240
2241 return qf_ptr;
2242}
2243
2244/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002245 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002246 */
2247 static qfline_T *
2248get_nth_entry(
2249 qf_info_T *qi,
2250 int errornr,
2251 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002252 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002253{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002254 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002255
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002256 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002257 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2258 {
2259 --qf_idx;
2260 qf_ptr = qf_ptr->qf_prev;
2261 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002262 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002263 while (errornr > qf_idx &&
2264 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2265 qf_ptr->qf_next != NULL)
2266 {
2267 ++qf_idx;
2268 qf_ptr = qf_ptr->qf_next;
2269 }
2270
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002271 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002272 return qf_ptr;
2273}
2274
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002275/*
2276 * Find a help window or open one.
2277 */
2278 static int
2279jump_to_help_window(qf_info_T *qi, int *opened_window)
2280{
2281 win_T *wp;
2282 int flags;
2283
2284 if (cmdmod.tab != 0)
2285 wp = NULL;
2286 else
2287 FOR_ALL_WINDOWS(wp)
2288 if (bt_help(wp->w_buffer))
2289 break;
2290 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2291 win_enter(wp, TRUE);
2292 else
2293 {
2294 /*
2295 * Split off help window; put it at far top if no position
2296 * specified, the current window is vertically split and narrow.
2297 */
2298 flags = WSP_HELP;
2299 if (cmdmod.split == 0 && curwin->w_width != Columns
2300 && curwin->w_width < 80)
2301 flags |= WSP_TOP;
2302 if (qi != &ql_info)
2303 flags |= WSP_NEWLOC; /* don't copy the location list */
2304
2305 if (win_split(0, flags) == FAIL)
2306 return FAIL;
2307
2308 *opened_window = TRUE;
2309
2310 if (curwin->w_height < p_hh)
2311 win_setheight((int)p_hh);
2312
2313 if (qi != &ql_info) /* not a quickfix list */
2314 {
2315 /* The new window should use the supplied location list */
2316 curwin->w_llist = qi;
2317 qi->qf_refcount++;
2318 }
2319 }
2320
2321 if (!p_im)
2322 restart_edit = 0; /* don't want insert mode in help file */
2323
2324 return OK;
2325}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002326
2327/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002328 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2329 * If the file is already opened in a window, jump to it.
2330 */
2331 static int
2332qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2333{
2334 win_T *usable_win_ptr = NULL;
2335 int usable_win;
2336 qf_info_T *ll_ref;
2337 int flags;
2338 win_T *win;
2339 win_T *altwin;
2340
2341 usable_win = 0;
2342
2343 ll_ref = curwin->w_llist_ref;
2344 if (ll_ref != NULL)
2345 {
2346 /* Find a window using the same location list that is not a
2347 * quickfix window. */
2348 FOR_ALL_WINDOWS(usable_win_ptr)
2349 if (usable_win_ptr->w_llist == ll_ref
2350 && !bt_quickfix(usable_win_ptr->w_buffer))
2351 {
2352 usable_win = 1;
2353 break;
2354 }
2355 }
2356
2357 if (!usable_win)
2358 {
2359 /* Locate a window showing a normal buffer */
2360 FOR_ALL_WINDOWS(win)
2361 if (win->w_buffer->b_p_bt[0] == NUL)
2362 {
2363 usable_win = 1;
2364 break;
2365 }
2366 }
2367
2368 /*
2369 * If no usable window is found and 'switchbuf' contains "usetab"
2370 * then search in other tabs.
2371 */
2372 if (!usable_win && (swb_flags & SWB_USETAB))
2373 {
2374 tabpage_T *tp;
2375 win_T *wp;
2376
2377 FOR_ALL_TAB_WINDOWS(tp, wp)
2378 {
2379 if (wp->w_buffer->b_fnum == qf_fnum)
2380 {
2381 goto_tabpage_win(tp, wp);
2382 usable_win = 1;
2383 goto win_found;
2384 }
2385 }
2386 }
2387win_found:
2388
2389 /*
2390 * If there is only one window and it is the quickfix window, create a
2391 * new one above the quickfix window.
2392 */
2393 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2394 {
2395 flags = WSP_ABOVE;
2396 if (ll_ref != NULL)
2397 flags |= WSP_NEWLOC;
2398 if (win_split(0, flags) == FAIL)
2399 return FAIL; /* not enough room for window */
2400 *opened_window = TRUE; /* close it when fail */
2401 p_swb = empty_option; /* don't split again */
2402 swb_flags = 0;
2403 RESET_BINDING(curwin);
2404 if (ll_ref != NULL)
2405 {
2406 /* The new window should use the location list from the
2407 * location list window */
2408 curwin->w_llist = ll_ref;
2409 ll_ref->qf_refcount++;
2410 }
2411 }
2412 else
2413 {
2414 if (curwin->w_llist_ref != NULL)
2415 {
2416 /* In a location window */
2417 win = usable_win_ptr;
2418 if (win == NULL)
2419 {
2420 /* Find the window showing the selected file */
2421 FOR_ALL_WINDOWS(win)
2422 if (win->w_buffer->b_fnum == qf_fnum)
2423 break;
2424 if (win == NULL)
2425 {
2426 /* Find a previous usable window */
2427 win = curwin;
2428 do
2429 {
2430 if (win->w_buffer->b_p_bt[0] == NUL)
2431 break;
2432 if (win->w_prev == NULL)
2433 win = lastwin; /* wrap around the top */
2434 else
2435 win = win->w_prev; /* go to previous window */
2436 } while (win != curwin);
2437 }
2438 }
2439 win_goto(win);
2440
2441 /* If the location list for the window is not set, then set it
2442 * to the location list from the location window */
2443 if (win->w_llist == NULL)
2444 {
2445 win->w_llist = ll_ref;
2446 ll_ref->qf_refcount++;
2447 }
2448 }
2449 else
2450 {
2451
2452 /*
2453 * Try to find a window that shows the right buffer.
2454 * Default to the window just above the quickfix buffer.
2455 */
2456 win = curwin;
2457 altwin = NULL;
2458 for (;;)
2459 {
2460 if (win->w_buffer->b_fnum == qf_fnum)
2461 break;
2462 if (win->w_prev == NULL)
2463 win = lastwin; /* wrap around the top */
2464 else
2465 win = win->w_prev; /* go to previous window */
2466
2467 if (IS_QF_WINDOW(win))
2468 {
2469 /* Didn't find it, go to the window before the quickfix
2470 * window. */
2471 if (altwin != NULL)
2472 win = altwin;
2473 else if (curwin->w_prev != NULL)
2474 win = curwin->w_prev;
2475 else
2476 win = curwin->w_next;
2477 break;
2478 }
2479
2480 /* Remember a usable window. */
2481 if (altwin == NULL && !win->w_p_pvw
2482 && win->w_buffer->b_p_bt[0] == NUL)
2483 altwin = win;
2484 }
2485
2486 win_goto(win);
2487 }
2488 }
2489
2490 return OK;
2491}
2492
2493/*
2494 * Edit the selected file or help file.
2495 */
2496 static int
2497qf_jump_edit_buffer(
2498 qf_info_T *qi,
2499 qfline_T *qf_ptr,
2500 int forceit,
2501 win_T *oldwin,
2502 int *opened_window,
2503 int *abort)
2504{
2505 int retval = OK;
2506
2507 if (qf_ptr->qf_type == 1)
2508 {
2509 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2510 * set b_p_ro flag). */
2511 if (!can_abandon(curbuf, forceit))
2512 {
2513 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002514 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002515 }
2516 else
2517 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2518 ECMD_HIDE + ECMD_SET_HELP,
2519 oldwin == curwin ? curwin : NULL);
2520 }
2521 else
2522 {
2523 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002524 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002525
2526 retval = buflist_getfile(qf_ptr->qf_fnum,
2527 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002528
2529 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002530 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002531 /*
2532 * Location list. Check whether the associated window is still
2533 * present and the list is still valid.
2534 */
2535 if (!win_valid_any_tab(oldwin))
2536 {
2537 EMSG(_("E924: Current window was closed"));
2538 *abort = TRUE;
2539 *opened_window = FALSE;
2540 }
2541 else if (!qflist_valid(oldwin, save_qfid))
2542 {
2543 EMSG(_(e_loc_list_changed));
2544 *abort = TRUE;
2545 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002546 }
2547 else if (old_qf_curlist != qi->qf_curlist
2548 || !is_qf_entry_present(qi, qf_ptr))
2549 {
2550 if (qi == &ql_info)
2551 EMSG(_("E925: Current quickfix was changed"));
2552 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002553 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002554 *abort = TRUE;
2555 }
2556
2557 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002558 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002559 }
2560
2561 return retval;
2562}
2563
2564/*
2565 * Goto the error line in the current file using either line/column number or a
2566 * search pattern.
2567 */
2568 static void
2569qf_jump_goto_line(
2570 linenr_T qf_lnum,
2571 int qf_col,
2572 char_u qf_viscol,
2573 char_u *qf_pattern)
2574{
2575 linenr_T i;
2576 char_u *line;
2577 colnr_T screen_col;
2578 colnr_T char_col;
2579
2580 if (qf_pattern == NULL)
2581 {
2582 /*
2583 * Go to line with error, unless qf_lnum is 0.
2584 */
2585 i = qf_lnum;
2586 if (i > 0)
2587 {
2588 if (i > curbuf->b_ml.ml_line_count)
2589 i = curbuf->b_ml.ml_line_count;
2590 curwin->w_cursor.lnum = i;
2591 }
2592 if (qf_col > 0)
2593 {
2594 curwin->w_cursor.col = qf_col - 1;
2595#ifdef FEAT_VIRTUALEDIT
2596 curwin->w_cursor.coladd = 0;
2597#endif
2598 if (qf_viscol == TRUE)
2599 {
2600 /*
2601 * Check each character from the beginning of the error
2602 * line up to the error column. For each tab character
2603 * found, reduce the error column value by the length of
2604 * a tab character.
2605 */
2606 line = ml_get_curline();
2607 screen_col = 0;
2608 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2609 {
2610 if (*line == NUL)
2611 break;
2612 if (*line++ == '\t')
2613 {
2614 curwin->w_cursor.col -= 7 - (screen_col % 8);
2615 screen_col += 8 - (screen_col % 8);
2616 }
2617 else
2618 ++screen_col;
2619 }
2620 }
2621 check_cursor();
2622 }
2623 else
2624 beginline(BL_WHITE | BL_FIX);
2625 }
2626 else
2627 {
2628 pos_T save_cursor;
2629
2630 /* Move the cursor to the first line in the buffer */
2631 save_cursor = curwin->w_cursor;
2632 curwin->w_cursor.lnum = 0;
2633 if (!do_search(NULL, '/', qf_pattern, (long)1,
2634 SEARCH_KEEP, NULL, NULL))
2635 curwin->w_cursor = save_cursor;
2636 }
2637}
2638
2639/*
2640 * Display quickfix list index and size message
2641 */
2642 static void
2643qf_jump_print_msg(
2644 qf_info_T *qi,
2645 int qf_index,
2646 qfline_T *qf_ptr,
2647 buf_T *old_curbuf,
2648 linenr_T old_lnum)
2649{
2650 linenr_T i;
2651 int len;
2652
2653 /* Update the screen before showing the message, unless the screen
2654 * scrolled up. */
2655 if (!msg_scrolled)
2656 update_topline_redraw();
2657 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2658 qi->qf_lists[qi->qf_curlist].qf_count,
2659 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2660 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2661 /* Add the message, skipping leading whitespace and newlines. */
2662 len = (int)STRLEN(IObuff);
2663 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2664
2665 /* Output the message. Overwrite to avoid scrolling when the 'O'
2666 * flag is present in 'shortmess'; But when not jumping, print the
2667 * whole message. */
2668 i = msg_scroll;
2669 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2670 msg_scroll = TRUE;
2671 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2672 msg_scroll = FALSE;
2673 msg_attr_keep(IObuff, 0, TRUE);
2674 msg_scroll = i;
2675}
2676
2677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 * jump to a quickfix line
2679 * if dir == FORWARD go "errornr" valid entries forward
2680 * if dir == BACKWARD go "errornr" valid entries backward
2681 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2682 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2683 * else if "errornr" is zero, redisplay the same line
2684 * else go to entry "errornr"
2685 */
2686 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002687qf_jump(qf_info_T *qi,
2688 int dir,
2689 int errornr,
2690 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002692 qfline_T *qf_ptr;
2693 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 buf_T *old_curbuf;
2697 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002698 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002699 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002701 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703#ifdef FEAT_FOLDING
2704 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2705#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002706 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002708 if (qi == NULL)
2709 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002710
2711 if (qi->qf_curlist >= qi->qf_listcount
2712 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
2714 EMSG(_(e_quickfix));
2715 return;
2716 }
2717
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002718 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002720 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002722 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002724 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2725 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002727 qf_ptr = old_qf_ptr;
2728 qf_index = old_qf_index;
2729 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731 }
2732 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002733 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002735 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2736 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 /* No need to print the error message if it's visible in the error
2738 * window */
2739 print_message = FALSE;
2740
2741 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002742 * For ":helpgrep" find a help window or open one.
2743 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002744 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002745 if (jump_to_help_window(qi, &opened_window) == FAIL)
2746 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002747
2748 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 * If currently in the quickfix window, find another window to show the
2750 * file in.
2751 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002752 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
2754 /*
2755 * If there is no file specified, we don't know where to go.
2756 * But do advance, otherwise ":cn" gets stuck.
2757 */
2758 if (qf_ptr->qf_fnum == 0)
2759 goto theend;
2760
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002761 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2762 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
2765 /*
2766 * If there is a file name,
2767 * read the wanted file if needed, and check autowrite etc.
2768 */
2769 old_curbuf = curbuf;
2770 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002771
2772 if (qf_ptr->qf_fnum != 0)
2773 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002774 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002775
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002776 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2777 &opened_window, &abort);
2778 if (abort)
2779 {
2780 qi = NULL;
2781 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002782 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002783 }
2784
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002785 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
2787 /* When not switched to another buffer, still need to set pc mark */
2788 if (curbuf == old_curbuf)
2789 setpcmark();
2790
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002791 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2792 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794#ifdef FEAT_FOLDING
2795 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2796 foldOpenCursor();
2797#endif
2798 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002799 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 else
2802 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (opened_window)
2804 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002805 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
2807 /*
2808 * Couldn't open file, so put index back where it was. This could
2809 * happen if the file was readonly and we changed something.
2810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 qf_ptr = old_qf_ptr;
2813 qf_index = old_qf_index;
2814 }
2815 }
2816theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002817 if (qi != NULL)
2818 {
2819 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2820 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 if (p_swb != old_swb && opened_window)
2823 {
2824 /* Restore old 'switchbuf' value, but not when an autocommand or
2825 * modeline has changed the value. */
2826 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002829 swb_flags = old_swb_flags;
2830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 else
2832 free_string_option(old_swb);
2833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834}
2835
2836/*
2837 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002838 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 */
2840 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002841qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002843 buf_T *buf;
2844 char_u *fname;
2845 qfline_T *qfp;
2846 int i;
2847 int idx1 = 1;
2848 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002849 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002850 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002851 int qfFileAttr;
2852 int qfSepAttr;
2853 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002854 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002856 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002858 if (eap->cmdidx == CMD_llist)
2859 {
2860 qi = GET_LOC_LIST(curwin);
2861 if (qi == NULL)
2862 {
2863 EMSG(_(e_loclist));
2864 return;
2865 }
2866 }
2867
2868 if (qi->qf_curlist >= qi->qf_listcount
2869 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
2871 EMSG(_(e_quickfix));
2872 return;
2873 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002874 if (*arg == '+')
2875 {
2876 ++arg;
2877 plus = TRUE;
2878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2880 {
2881 EMSG(_(e_trailing));
2882 return;
2883 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002884 if (plus)
2885 {
2886 i = qi->qf_lists[qi->qf_curlist].qf_index;
2887 idx2 = i + idx1;
2888 idx1 = i;
2889 }
2890 else
2891 {
2892 i = qi->qf_lists[qi->qf_curlist].qf_count;
2893 if (idx1 < 0)
2894 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2895 if (idx2 < 0)
2896 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
Bram Moolenaara796d462018-05-01 14:30:36 +02002899 /* Shorten all the file names, so that it is easy to read */
2900 shorten_fnames(FALSE);
2901
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002902 /*
2903 * Get the attributes for the different quickfix highlight items. Note
2904 * that this depends on syntax items defined in the qf.vim syntax file
2905 */
2906 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2907 if (qfFileAttr == 0)
2908 qfFileAttr = HL_ATTR(HLF_D);
2909 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2910 if (qfSepAttr == 0)
2911 qfSepAttr = HL_ATTR(HLF_D);
2912 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2913 if (qfLineAttr == 0)
2914 qfLineAttr = HL_ATTR(HLF_N);
2915
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002916 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002918 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2919 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
2921 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2922 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002923 msg_putchar('\n');
2924 if (got_int)
2925 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002926
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002927 fname = NULL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002928 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
2929 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", i, (char *)qfp->qf_module);
2930 else {
2931 if (qfp->qf_fnum != 0
2932 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2933 {
2934 fname = buf->b_fname;
2935 if (qfp->qf_type == 1) /* :helpgrep */
2936 fname = gettail(fname);
2937 }
2938 if (fname == NULL)
2939 sprintf((char *)IObuff, "%2d", i);
2940 else
2941 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2942 i, (char *)fname);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002943 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002944 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002945 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2946
2947 if (qfp->qf_lnum != 0)
2948 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002949 if (qfp->qf_lnum == 0)
2950 IObuff[0] = NUL;
2951 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002952 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002953 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002954 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002955 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002956 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002957 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002958 msg_puts_attr(IObuff, qfLineAttr);
2959 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002960 if (qfp->qf_pattern != NULL)
2961 {
2962 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002963 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002964 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002965 }
2966 msg_puts((char_u *)" ");
2967
2968 /* Remove newlines and leading whitespace from the text. For an
2969 * unrecognized line keep the indent, the compiler may mark a word
2970 * with ^^^^. */
2971 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2973 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002974 msg_prt_line(IObuff, FALSE);
2975 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002977
2978 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002979 if (qfp == NULL)
2980 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002981 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 ui_breakcheck();
2983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984}
2985
2986/*
2987 * Remove newlines and leading whitespace from an error message.
2988 * Put the result in "buf[bufsize]".
2989 */
2990 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002991qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 int i;
2994 char_u *p = text;
2995
2996 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2997 {
2998 if (*p == '\n')
2999 {
3000 buf[i] = ' ';
3001 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003002 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 break;
3004 }
3005 else
3006 buf[i] = *p++;
3007 }
3008 buf[i] = NUL;
3009}
3010
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003011/*
3012 * Display information (list number, list size and the title) about a
3013 * quickfix/location list.
3014 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003015 static void
3016qf_msg(qf_info_T *qi, int which, char *lead)
3017{
3018 char *title = (char *)qi->qf_lists[which].qf_title;
3019 int count = qi->qf_lists[which].qf_count;
3020 char_u buf[IOSIZE];
3021
3022 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3023 lead,
3024 which + 1,
3025 qi->qf_listcount,
3026 count);
3027
3028 if (title != NULL)
3029 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003030 size_t len = STRLEN(buf);
3031
3032 if (len < 34)
3033 {
3034 vim_memset(buf + len, ' ', 34 - len);
3035 buf[34] = NUL;
3036 }
3037 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003038 }
3039 trunc_string(buf, buf, Columns - 1, IOSIZE);
3040 msg(buf);
3041}
3042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043/*
3044 * ":colder [count]": Up in the quickfix stack.
3045 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003046 * ":lolder [count]": Up in the location list stack.
3047 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 */
3049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003050qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003052 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 int count;
3054
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003055 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
3056 {
3057 qi = GET_LOC_LIST(curwin);
3058 if (qi == NULL)
3059 {
3060 EMSG(_(e_loclist));
3061 return;
3062 }
3063 }
3064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 if (eap->addr_count != 0)
3066 count = eap->line2;
3067 else
3068 count = 1;
3069 while (count--)
3070 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003071 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003073 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 {
3075 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003078 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 }
3080 else
3081 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003082 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 {
3084 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003085 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003087 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 }
3089 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003090 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003091 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092}
3093
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003094/*
3095 * Display the information about all the quickfix/location lists in the stack
3096 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003097 void
3098qf_history(exarg_T *eap)
3099{
3100 qf_info_T *qi = &ql_info;
3101 int i;
3102
3103 if (eap->cmdidx == CMD_lhistory)
3104 qi = GET_LOC_LIST(curwin);
3105 if (qi == NULL || (qi->qf_listcount == 0
3106 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
3107 MSG(_("No entries"));
3108 else
3109 for (i = 0; i < qi->qf_listcount; ++i)
3110 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3111}
3112
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003114 * Free all the entries in the error list "idx". Note that other information
3115 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 */
3117 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003118qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003120 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003121 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003122 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003123 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003125 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003127 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003128 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003129 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003130 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003131 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003132 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003133 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003134 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003135 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003136 if (stop)
3137 /* Somehow qf_count may have an incorrect value, set it to 1
3138 * to avoid crashing when it's wrong.
3139 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003140 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003141 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003142 qfl->qf_start = qfpnext;
3143 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003145
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003146 qfl->qf_index = 0;
3147 qfl->qf_start = NULL;
3148 qfl->qf_last = NULL;
3149 qfl->qf_ptr = NULL;
3150 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003151
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003152 qf_clean_dir_stack(&qfl->qf_dir_stack);
3153 qfl->qf_directory = NULL;
3154 qf_clean_dir_stack(&qfl->qf_file_stack);
3155 qfl->qf_currfile = NULL;
3156 qfl->qf_multiline = FALSE;
3157 qfl->qf_multiignore = FALSE;
3158 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159}
3160
3161/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003162 * Free error list "idx". Frees all the entries in the quickfix list,
3163 * associated context information and the title.
3164 */
3165 static void
3166qf_free(qf_info_T *qi, int idx)
3167{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003168 qf_list_T *qfl = &qi->qf_lists[idx];
3169
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003170 qf_free_items(qi, idx);
3171
Bram Moolenaard23a8232018-02-10 18:45:26 +01003172 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003173 free_tv(qfl->qf_ctx);
3174 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003175 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003176 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003177}
3178
3179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 * qf_mark_adjust: adjust marks
3181 */
3182 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003183qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003184 win_T *wp,
3185 linenr_T line1,
3186 linenr_T line2,
3187 long amount,
3188 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003190 int i;
3191 qfline_T *qfp;
3192 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003193 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003194 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003195 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
Bram Moolenaarc1542742016-07-20 21:44:37 +02003197 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003198 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003199 if (wp != NULL)
3200 {
3201 if (wp->w_llist == NULL)
3202 return;
3203 qi = wp->w_llist;
3204 }
3205
3206 for (idx = 0; idx < qi->qf_listcount; ++idx)
3207 if (qi->qf_lists[idx].qf_count)
3208 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003209 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3210 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (qfp->qf_fnum == curbuf->b_fnum)
3212 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003213 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3215 {
3216 if (amount == MAXLNUM)
3217 qfp->qf_cleared = TRUE;
3218 else
3219 qfp->qf_lnum += amount;
3220 }
3221 else if (amount_after && qfp->qf_lnum > line2)
3222 qfp->qf_lnum += amount_after;
3223 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003224
3225 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003226 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227}
3228
3229/*
3230 * Make a nice message out of the error character and the error number:
3231 * char number message
3232 * e or E 0 " error"
3233 * w or W 0 " warning"
3234 * i or I 0 " info"
3235 * 0 0 ""
3236 * other 0 " c"
3237 * e or E n " error n"
3238 * w or W n " warning n"
3239 * i or I n " info n"
3240 * 0 n " error n"
3241 * other n " c n"
3242 * 1 x "" :helpgrep
3243 */
3244 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003245qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 static char_u buf[20];
3248 static char_u cc[3];
3249 char_u *p;
3250
3251 if (c == 'W' || c == 'w')
3252 p = (char_u *)" warning";
3253 else if (c == 'I' || c == 'i')
3254 p = (char_u *)" info";
3255 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3256 p = (char_u *)" error";
3257 else if (c == 0 || c == 1)
3258 p = (char_u *)"";
3259 else
3260 {
3261 cc[0] = ' ';
3262 cc[1] = c;
3263 cc[2] = NUL;
3264 p = cc;
3265 }
3266
3267 if (nr <= 0)
3268 return p;
3269
3270 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3271 return buf;
3272}
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274/*
3275 * ":cwindow": open the quickfix window if we have errors to display,
3276 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003277 * ":lwindow": open the location list window if we have locations to display,
3278 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 */
3280 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003281ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003283 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 win_T *win;
3285
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003286 if (eap->cmdidx == CMD_lwindow)
3287 {
3288 qi = GET_LOC_LIST(curwin);
3289 if (qi == NULL)
3290 return;
3291 }
3292
3293 /* Look for an existing quickfix window. */
3294 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
3296 /*
3297 * If a quickfix window is open but we have no errors to display,
3298 * close the window. If a quickfix window is not open, then open
3299 * it if we have errors; otherwise, leave it closed.
3300 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003301 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003302 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003303 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 if (win != NULL)
3306 ex_cclose(eap);
3307 }
3308 else if (win == NULL)
3309 ex_copen(eap);
3310}
3311
3312/*
3313 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003314 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003317ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003319 win_T *win = NULL;
3320 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003322 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3323 {
3324 qi = GET_LOC_LIST(curwin);
3325 if (qi == NULL)
3326 return;
3327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003329 /* Find existing quickfix window and close it. */
3330 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (win != NULL)
3332 win_close(win, FALSE);
3333}
3334
3335/*
3336 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003337 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 */
3339 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003340ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003342 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003345 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003346 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003347 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003349 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3350 {
3351 qi = GET_LOC_LIST(curwin);
3352 if (qi == NULL)
3353 {
3354 EMSG(_(e_loclist));
3355 return;
3356 }
3357 }
3358
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (eap->addr_count != 0)
3360 height = eap->line2;
3361 else
3362 height = QF_WINHEIGHT;
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#ifdef FEAT_GUI
3366 need_mouse_correct = TRUE;
3367#endif
3368
3369 /*
3370 * Find existing quickfix window, or open a new one.
3371 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003372 win = qf_find_win(qi);
3373
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003374 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003377 if (eap->addr_count != 0)
3378 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003379 if (cmdmod.split & WSP_VERT)
3380 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003381 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003382 win_setwidth(height);
3383 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003384 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003385 win_setheight(height);
3386 }
3387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 else
3389 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003390 int flags = 0;
3391
Bram Moolenaar9c102382006-05-03 21:26:49 +00003392 qf_buf = qf_find_buf(qi);
3393
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 /* The current window becomes the previous window afterwards. */
3395 win = curwin;
3396
Bram Moolenaar77642c02012-11-20 17:55:10 +01003397 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3398 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003399 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003400 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003401 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003402 /* Default is to open the window below the current window */
3403 if (cmdmod.split == 0)
3404 flags = WSP_BELOW;
3405 flags |= WSP_NEWLOC;
3406 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003408 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003410 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003412 /*
3413 * For the location list window, create a reference to the
3414 * location list from the window 'win'.
3415 */
3416 curwin->w_llist_ref = win->w_llist;
3417 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003419
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003420 if (oldwin != curwin)
3421 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003422 if (qf_buf != NULL)
3423 /* Use the existing quickfix buffer */
3424 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003425 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003426 else
3427 {
3428 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003429 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003430 /* switch off 'swapfile' */
3431 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3432 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003433 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003434 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003435 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003436#ifdef FEAT_DIFF
3437 curwin->w_p_diff = FALSE;
3438#endif
3439#ifdef FEAT_FOLDING
3440 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3441 OPT_LOCAL);
3442#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003445 /* Only set the height when still in the same tab page and there is no
3446 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003447 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 win_setheight(height);
3449 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3450 if (win_valid(win))
3451 prevwin = win;
3452 }
3453
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003454 qf_set_title_var(qi);
3455
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 /*
3457 * Fill the buffer with the quickfix list.
3458 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003459 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003461 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 curwin->w_cursor.col = 0;
3463 check_cursor();
3464 update_topline(); /* scroll to show the line */
3465}
3466
3467/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003468 * Move the cursor in the quickfix window to "lnum".
3469 */
3470 static void
3471qf_win_goto(win_T *win, linenr_T lnum)
3472{
3473 win_T *old_curwin = curwin;
3474
3475 curwin = win;
3476 curbuf = win->w_buffer;
3477 curwin->w_cursor.lnum = lnum;
3478 curwin->w_cursor.col = 0;
3479#ifdef FEAT_VIRTUALEDIT
3480 curwin->w_cursor.coladd = 0;
3481#endif
3482 curwin->w_curswant = 0;
3483 update_topline(); /* scroll to show the line */
3484 redraw_later(VALID);
3485 curwin->w_redr_status = TRUE; /* update ruler */
3486 curwin = old_curwin;
3487 curbuf = curwin->w_buffer;
3488}
3489
3490/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003491 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003492 */
3493 void
3494ex_cbottom(exarg_T *eap UNUSED)
3495{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003496 qf_info_T *qi = &ql_info;
3497 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003498
Bram Moolenaar537ef082016-07-09 17:56:19 +02003499 if (eap->cmdidx == CMD_lbottom)
3500 {
3501 qi = GET_LOC_LIST(curwin);
3502 if (qi == NULL)
3503 {
3504 EMSG(_(e_loclist));
3505 return;
3506 }
3507 }
3508
3509 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003510 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3511 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3512}
3513
3514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 * Return the number of the current entry (line number in the quickfix
3516 * window).
3517 */
3518 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003519qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003521 qf_info_T *qi = &ql_info;
3522
3523 if (IS_LL_WINDOW(wp))
3524 /* In the location list window, use the referenced location list */
3525 qi = wp->w_llist_ref;
3526
3527 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528}
3529
3530/*
3531 * Update the cursor position in the quickfix window to the current error.
3532 * Return TRUE if there is a quickfix window.
3533 */
3534 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003535qf_win_pos_update(
3536 qf_info_T *qi,
3537 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
3539 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003540 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
3542 /*
3543 * Put the cursor on the current error in the quickfix window, so that
3544 * it's viewable.
3545 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003546 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (win != NULL
3548 && qf_index <= win->w_buffer->b_ml.ml_line_count
3549 && old_qf_index != qf_index)
3550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (qf_index > old_qf_index)
3552 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003553 win->w_redraw_top = old_qf_index;
3554 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556 else
3557 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003558 win->w_redraw_top = qf_index;
3559 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003561 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
3563 return win != NULL;
3564}
3565
3566/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003567 * Check whether the given window is displaying the specified quickfix/location
3568 * list buffer
3569 */
3570 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003571is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003572{
3573 /*
3574 * A window displaying the quickfix buffer will have the w_llist_ref field
3575 * set to NULL.
3576 * A window displaying a location list buffer will have the w_llist_ref
3577 * pointing to the location list.
3578 */
3579 if (bt_quickfix(win->w_buffer))
3580 if ((qi == &ql_info && win->w_llist_ref == NULL)
3581 || (qi != &ql_info && win->w_llist_ref == qi))
3582 return TRUE;
3583
3584 return FALSE;
3585}
3586
3587/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003588 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003589 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003590 */
3591 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003592qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003593{
3594 win_T *win;
3595
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003596 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003597 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003598 return win;
3599 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003600}
3601
3602/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003603 * Find a quickfix buffer.
3604 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 */
3606 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003607qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003609 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003610 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
Bram Moolenaar9c102382006-05-03 21:26:49 +00003612 FOR_ALL_TAB_WINDOWS(tp, win)
3613 if (is_qf_win(win, qi))
3614 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003615
Bram Moolenaar9c102382006-05-03 21:26:49 +00003616 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617}
3618
3619/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003620 * Update the w:quickfix_title variable in the quickfix/location list window
3621 */
3622 static void
3623qf_update_win_titlevar(qf_info_T *qi)
3624{
3625 win_T *win;
3626 win_T *curwin_save;
3627
3628 if ((win = qf_find_win(qi)) != NULL)
3629 {
3630 curwin_save = curwin;
3631 curwin = win;
3632 qf_set_title_var(qi);
3633 curwin = curwin_save;
3634 }
3635}
3636
3637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 * Find the quickfix buffer. If it exists, update the contents.
3639 */
3640 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003641qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
3643 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003644 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003648 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (buf != NULL)
3650 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003651 linenr_T old_line_count = buf->b_ml.ml_line_count;
3652
3653 if (old_last == NULL)
3654 /* set curwin/curbuf to buf and save a few things */
3655 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaard823fa92016-08-12 16:29:27 +02003657 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003658
Bram Moolenaar864293a2016-06-02 13:40:04 +02003659 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003660 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003661
Bram Moolenaar864293a2016-06-02 13:40:04 +02003662 if (old_last == NULL)
3663 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003664 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003665
3666 /* restore curwin/curbuf and a few other things */
3667 aucmd_restbuf(&aco);
3668 }
3669
3670 /* Only redraw when added lines are visible. This avoids flickering
3671 * when the added lines are not visible. */
3672 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3673 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675}
3676
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003677/*
3678 * Set "w:quickfix_title" if "qi" has a title.
3679 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003681qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003682{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003683 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3684 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003685 qi->qf_lists[qi->qf_curlist].qf_title);
3686}
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688/*
3689 * Fill current buffer with quickfix errors, replacing any previous contents.
3690 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003691 * If "old_last" is not NULL append the items after this one.
3692 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3693 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 */
3695 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003696qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003698 linenr_T lnum;
3699 qfline_T *qfp;
3700 buf_T *errbuf;
3701 int len;
3702 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
Bram Moolenaar864293a2016-06-02 13:40:04 +02003704 if (old_last == NULL)
3705 {
3706 if (buf != curbuf)
3707 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003708 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003709 return;
3710 }
3711
3712 /* delete all existing lines */
3713 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3714 (void)ml_delete((linenr_T)1, FALSE);
3715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003718 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003720 char_u dirname[MAXPATHL];
3721
3722 *dirname = NUL;
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003725 if (old_last == NULL)
3726 {
3727 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3728 lnum = 0;
3729 }
3730 else
3731 {
3732 qfp = old_last->qf_next;
3733 lnum = buf->b_ml.ml_line_count;
3734 }
3735 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003737 if (qfp->qf_module != NULL)
3738 {
3739 STRCPY(IObuff, qfp->qf_module);
3740 len = (int)STRLEN(IObuff);
3741 }
3742 else if (qfp->qf_fnum != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3744 && errbuf->b_fname != NULL)
3745 {
3746 if (qfp->qf_type == 1) /* :helpgrep */
3747 STRCPY(IObuff, gettail(errbuf->b_fname));
3748 else
Bram Moolenaara796d462018-05-01 14:30:36 +02003749 {
3750 /* shorten the file name if not done already */
3751 if (errbuf->b_sfname == NULL
3752 || mch_isFullName(errbuf->b_sfname))
3753 {
3754 if (*dirname == NUL)
3755 mch_dirname(dirname, MAXPATHL);
3756 shorten_buf_fname(errbuf, dirname, FALSE);
3757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 STRCPY(IObuff, errbuf->b_fname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 len = (int)STRLEN(IObuff);
3761 }
3762 else
3763 len = 0;
3764 IObuff[len++] = '|';
3765
3766 if (qfp->qf_lnum > 0)
3767 {
3768 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3769 len += (int)STRLEN(IObuff + len);
3770
3771 if (qfp->qf_col > 0)
3772 {
3773 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3774 len += (int)STRLEN(IObuff + len);
3775 }
3776
3777 sprintf((char *)IObuff + len, "%s",
3778 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3779 len += (int)STRLEN(IObuff + len);
3780 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003781 else if (qfp->qf_pattern != NULL)
3782 {
3783 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3784 len += (int)STRLEN(IObuff + len);
3785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 IObuff[len++] = '|';
3787 IObuff[len++] = ' ';
3788
3789 /* Remove newlines and leading whitespace from the text.
3790 * For an unrecognized line keep the indent, the compiler may
3791 * mark a word with ^^^^. */
3792 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3793 IObuff + len, IOSIZE - len);
3794
Bram Moolenaar864293a2016-06-02 13:40:04 +02003795 if (ml_append_buf(buf, lnum, IObuff,
3796 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003798 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003800 if (qfp == NULL)
3801 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003803
3804 if (old_last == NULL)
3805 /* Delete the empty line which is now at the end */
3806 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808
3809 /* correct cursor position */
3810 check_lnums(TRUE);
3811
Bram Moolenaar864293a2016-06-02 13:40:04 +02003812 if (old_last == NULL)
3813 {
3814 /* Set the 'filetype' to "qf" each time after filling the buffer.
3815 * This resembles reading a file into a buffer, it's more logical when
3816 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003817 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003818 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3819 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820
Bram Moolenaar864293a2016-06-02 13:40:04 +02003821 keep_filetype = TRUE; /* don't detect 'filetype' */
3822 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003824 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003826 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003827 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003828
Bram Moolenaar864293a2016-06-02 13:40:04 +02003829 /* make sure it will be redrawn */
3830 redraw_curbuf_later(NOT_VALID);
3831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833 /* Restore KeyTyped, setting 'filetype' may reset it. */
3834 KeyTyped = old_KeyTyped;
3835}
3836
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003837/*
3838 * For every change made to the quickfix list, update the changed tick.
3839 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01003840 static void
3841qf_list_changed(qf_info_T *qi, int qf_idx)
3842{
3843 qi->qf_lists[qf_idx].qf_changedtick++;
3844}
3845
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003847 * Return TRUE when using ":vimgrep" for ":grep".
3848 */
3849 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003850grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003851{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003852 return ((cmdidx == CMD_grep
3853 || cmdidx == CMD_lgrep
3854 || cmdidx == CMD_grepadd
3855 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003856 && STRCMP("internal",
3857 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3858}
3859
3860/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003861 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 */
3863 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003864ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003866 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003868 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003870 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003871 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003872 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003873 char_u *au_name = NULL;
3874
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003875 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3876 if (grep_internal(eap->cmdidx))
3877 {
3878 ex_vimgrep(eap);
3879 return;
3880 }
3881
Bram Moolenaar7c626922005-02-07 22:01:03 +00003882 switch (eap->cmdidx)
3883 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003884 case CMD_make: au_name = (char_u *)"make"; break;
3885 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3886 case CMD_grep: au_name = (char_u *)"grep"; break;
3887 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3888 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3889 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003890 default: break;
3891 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003892 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3893 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003894 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003895#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003896 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003897 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003898#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003899 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003900#ifdef FEAT_MBYTE
3901 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
Bram Moolenaara6557602006-02-04 22:43:20 +00003904 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3905 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003906 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003909 fname = get_mef_name();
3910 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003912 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
3914 /*
3915 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3916 */
3917 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3918 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003919 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 cmd = alloc(len);
3921 if (cmd == NULL)
3922 return;
3923 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3924 (char *)p_shq);
3925 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003926 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 /*
3928 * Output a newline if there's something else than the :make command that
3929 * was typed (in which case the cursor is in column 0).
3930 */
3931 if (msg_col == 0)
3932 msg_didout = FALSE;
3933 msg_start();
3934 MSG_PUTS(":!");
3935 msg_outtrans(cmd); /* show what we are doing */
3936
3937 /* let the shell know if we are redirecting output or not */
3938 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3939
3940#ifdef AMIGA
3941 out_flush();
3942 /* read window status report and redraw before message */
3943 (void)char_avail();
3944#endif
3945
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003946 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003947 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3948 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003949 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003950 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003951 if (wp != NULL)
3952 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003953 if (res >= 0 && qi != NULL)
3954 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003955 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003956 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003957 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3958 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01003959 if (qi != NULL && qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003960 res = qi->qf_lists[qi->qf_curlist].qf_count;
3961 else
3962 res = 0;
3963 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003964 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003965 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
Bram Moolenaar7c626922005-02-07 22:01:03 +00003967 mch_remove(fname);
3968 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 vim_free(cmd);
3970}
3971
3972/*
3973 * Return the name for the errorfile, in allocated memory.
3974 * Find a new unique name when 'makeef' contains "##".
3975 * Returns NULL for error.
3976 */
3977 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003978get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979{
3980 char_u *p;
3981 char_u *name;
3982 static int start = -1;
3983 static int off = 0;
3984#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003985 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986#endif
3987
3988 if (*p_mef == NUL)
3989 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003990 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 if (name == NULL)
3992 EMSG(_(e_notmp));
3993 return name;
3994 }
3995
3996 for (p = p_mef; *p; ++p)
3997 if (p[0] == '#' && p[1] == '#')
3998 break;
3999
4000 if (*p == NUL)
4001 return vim_strsave(p_mef);
4002
4003 /* Keep trying until the name doesn't exist yet. */
4004 for (;;)
4005 {
4006 if (start == -1)
4007 start = mch_get_pid();
4008 else
4009 off += 19;
4010
4011 name = alloc((unsigned)STRLEN(p_mef) + 30);
4012 if (name == NULL)
4013 break;
4014 STRCPY(name, p_mef);
4015 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4016 STRCAT(name, p + 2);
4017 if (mch_getperm(name) < 0
4018#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004019 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 && mch_lstat((char *)name, &sb) < 0
4021#endif
4022 )
4023 break;
4024 vim_free(name);
4025 }
4026 return name;
4027}
4028
4029/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004030 * Returns the number of valid entries in the current quickfix/location list.
4031 */
4032 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004033qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004034{
4035 qf_info_T *qi = &ql_info;
4036 qfline_T *qfp;
4037 int i, sz = 0;
4038 int prev_fnum = 0;
4039
4040 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4041 {
4042 /* Location list */
4043 qi = GET_LOC_LIST(curwin);
4044 if (qi == NULL)
4045 return 0;
4046 }
4047
4048 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004049 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004050 ++i, qfp = qfp->qf_next)
4051 {
4052 if (qfp->qf_valid)
4053 {
4054 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4055 sz++; /* Count all valid entries */
4056 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4057 {
4058 /* Count the number of files */
4059 sz++;
4060 prev_fnum = qfp->qf_fnum;
4061 }
4062 }
4063 }
4064
4065 return sz;
4066}
4067
4068/*
4069 * Returns the current index of the quickfix/location list.
4070 * Returns 0 if there is an error.
4071 */
4072 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004073qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004074{
4075 qf_info_T *qi = &ql_info;
4076
4077 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4078 {
4079 /* Location list */
4080 qi = GET_LOC_LIST(curwin);
4081 if (qi == NULL)
4082 return 0;
4083 }
4084
4085 return qi->qf_lists[qi->qf_curlist].qf_index;
4086}
4087
4088/*
4089 * Returns the current index in the quickfix/location list (counting only valid
4090 * entries). If no valid entries are in the list, then returns 1.
4091 */
4092 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004093qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004094{
4095 qf_info_T *qi = &ql_info;
4096 qf_list_T *qfl;
4097 qfline_T *qfp;
4098 int i, eidx = 0;
4099 int prev_fnum = 0;
4100
4101 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4102 {
4103 /* Location list */
4104 qi = GET_LOC_LIST(curwin);
4105 if (qi == NULL)
4106 return 1;
4107 }
4108
4109 qfl = &qi->qf_lists[qi->qf_curlist];
4110 qfp = qfl->qf_start;
4111
4112 /* check if the list has valid errors */
4113 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4114 return 1;
4115
4116 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4117 {
4118 if (qfp->qf_valid)
4119 {
4120 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4121 {
4122 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4123 {
4124 /* Count the number of files */
4125 eidx++;
4126 prev_fnum = qfp->qf_fnum;
4127 }
4128 }
4129 else
4130 eidx++;
4131 }
4132 }
4133
4134 return eidx ? eidx : 1;
4135}
4136
4137/*
4138 * Get the 'n'th valid error entry in the quickfix or location list.
4139 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4140 * For :cdo and :ldo returns the 'n'th valid error entry.
4141 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4142 */
4143 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004144qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004145{
4146 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4147 qfline_T *qfp = qfl->qf_start;
4148 int i, eidx;
4149 int prev_fnum = 0;
4150
4151 /* check if the list has valid errors */
4152 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4153 return 1;
4154
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004155 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004156 i++, qfp = qfp->qf_next)
4157 {
4158 if (qfp->qf_valid)
4159 {
4160 if (fdo)
4161 {
4162 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4163 {
4164 /* Count the number of files */
4165 eidx++;
4166 prev_fnum = qfp->qf_fnum;
4167 }
4168 }
4169 else
4170 eidx++;
4171 }
4172
4173 if (eidx == n)
4174 break;
4175 }
4176
4177 if (i <= qfl->qf_count)
4178 return i;
4179 else
4180 return 1;
4181}
4182
4183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004185 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004186 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 */
4188 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004189ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004191 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004192 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004193
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004194 if (eap->cmdidx == CMD_ll
4195 || eap->cmdidx == CMD_lrewind
4196 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004197 || eap->cmdidx == CMD_llast
4198 || eap->cmdidx == CMD_ldo
4199 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004200 {
4201 qi = GET_LOC_LIST(curwin);
4202 if (qi == NULL)
4203 {
4204 EMSG(_(e_loclist));
4205 return;
4206 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004207 }
4208
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004209 if (eap->addr_count > 0)
4210 errornr = (int)eap->line2;
4211 else
4212 {
4213 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4214 errornr = 0;
4215 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4216 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4217 errornr = 1;
4218 else
4219 errornr = 32767;
4220 }
4221
4222 /* For cdo and ldo commands, jump to the nth valid error.
4223 * For cfdo and lfdo commands, jump to the nth valid file entry.
4224 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004225 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4226 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004227 errornr = qf_get_nth_valid_entry(qi,
4228 eap->addr_count > 0 ? (int)eap->line1 : 1,
4229 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4230
4231 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232}
4233
4234/*
4235 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004236 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004237 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 */
4239 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004240ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004242 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004243 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004244
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004245 if (eap->cmdidx == CMD_lnext
4246 || eap->cmdidx == CMD_lNext
4247 || eap->cmdidx == CMD_lprevious
4248 || eap->cmdidx == CMD_lnfile
4249 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004250 || eap->cmdidx == CMD_lpfile
4251 || eap->cmdidx == CMD_ldo
4252 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004253 {
4254 qi = GET_LOC_LIST(curwin);
4255 if (qi == NULL)
4256 {
4257 EMSG(_(e_loclist));
4258 return;
4259 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004260 }
4261
Bram Moolenaar55b69262017-08-13 13:42:01 +02004262 if (eap->addr_count > 0
4263 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4264 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004265 errornr = (int)eap->line2;
4266 else
4267 errornr = 1;
4268
4269 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4270 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004272 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4273 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004275 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4276 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 ? BACKWARD_FILE
4278 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004279 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280}
4281
4282/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004283 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004284 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 */
4286 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004287ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004289 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004290 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004291 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004292 char_u *au_name = NULL;
Bram Moolenaarfc6f16b2018-03-06 17:43:22 +01004293 int save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004294 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004295
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004296 switch (eap->cmdidx)
4297 {
4298 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4299 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4300 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4301 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4302 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4303 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4304 default: break;
4305 }
4306 if (au_name != NULL)
4307 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004308#ifdef FEAT_MBYTE
4309 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4310#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004311#ifdef FEAT_BROWSE
4312 if (cmdmod.browse)
4313 {
4314 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004315 NULL, NULL,
4316 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004317 if (browse_file == NULL)
4318 return;
4319 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4320 vim_free(browse_file);
4321 }
4322 else
4323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004325 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004326
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004327 if (eap->cmdidx == CMD_lfile
4328 || eap->cmdidx == CMD_lgetfile
4329 || eap->cmdidx == CMD_laddfile)
4330 wp = curwin;
4331
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004332 /*
4333 * This function is used by the :cfile, :cgetfile and :caddfile
4334 * commands.
4335 * :cfile always creates a new quickfix list and jumps to the
4336 * first error.
4337 * :cgetfile creates a new quickfix list but doesn't jump to the
4338 * first error.
4339 * :caddfile adds to an existing quickfix list. If there is no
4340 * quickfix list then a new list is created.
4341 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004342 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4343 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004344 if (wp != NULL)
4345 qi = GET_LOC_LIST(wp);
4346 if (res >= 0 && qi != NULL)
4347 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004348 if (qi != NULL)
4349 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004350 if (au_name != NULL)
4351 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004352
4353 /* An autocmd might have freed the quickfix/location list. Check whether it
4354 * is still valid. */
4355 if (qi != NULL && !qflist_valid(wp, save_qfid))
Bram Moolenaar3c097222017-12-21 20:54:49 +01004356 return;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004357 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004358 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359}
4360
4361/*
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004362 * Return the quickfix/location list number with the given identifier.
4363 * Returns -1 if list is not found.
4364 */
4365 static int
4366qf_id2nr(qf_info_T *qi, int_u qfid)
4367{
4368 int qf_idx;
4369
4370 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4371 if (qi->qf_lists[qf_idx].qf_id == qfid)
4372 return qf_idx;
Bram Moolenaar29ce4092018-04-28 21:56:44 +02004373 return INVALID_QFIDX;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004374}
4375
4376/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004377 * Return the vimgrep autocmd name.
4378 */
4379 static char_u *
4380vgr_get_auname(cmdidx_T cmdidx)
4381{
4382 switch (cmdidx)
4383 {
4384 case CMD_vimgrep: return (char_u *)"vimgrep";
4385 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4386 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4387 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4388 case CMD_grep: return (char_u *)"grep";
4389 case CMD_lgrep: return (char_u *)"lgrep";
4390 case CMD_grepadd: return (char_u *)"grepadd";
4391 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4392 default: return NULL;
4393 }
4394}
4395
4396/*
4397 * Initialize the regmatch used by vimgrep for pattern "s".
4398 */
4399 static void
4400vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4401{
4402 /* Get the search pattern: either white-separated or enclosed in // */
4403 regmatch->regprog = NULL;
4404
4405 if (s == NULL || *s == NUL)
4406 {
4407 /* Pattern is empty, use last search pattern. */
4408 if (last_search_pat() == NULL)
4409 {
4410 EMSG(_(e_noprevre));
4411 return;
4412 }
4413 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4414 }
4415 else
4416 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4417
4418 regmatch->rmm_ic = p_ic;
4419 regmatch->rmm_maxcol = 0;
4420}
4421
4422/*
4423 * Display a file name when vimgrep is running.
4424 */
4425 static void
4426vgr_display_fname(char_u *fname)
4427{
4428 char_u *p;
4429
4430 msg_start();
4431 p = msg_strtrunc(fname, TRUE);
4432 if (p == NULL)
4433 msg_outtrans(fname);
4434 else
4435 {
4436 msg_outtrans(p);
4437 vim_free(p);
4438 }
4439 msg_clr_eos();
4440 msg_didout = FALSE; /* overwrite this message */
4441 msg_nowait = TRUE; /* don't wait for this message */
4442 msg_col = 0;
4443 out_flush();
4444}
4445
4446/*
4447 * Load a dummy buffer to search for a pattern using vimgrep.
4448 */
4449 static buf_T *
4450vgr_load_dummy_buf(
4451 char_u *fname,
4452 char_u *dirname_start,
4453 char_u *dirname_now)
4454{
4455 int save_mls;
4456#if defined(FEAT_SYN_HL)
4457 char_u *save_ei = NULL;
4458#endif
4459 buf_T *buf;
4460
4461#if defined(FEAT_SYN_HL)
4462 /* Don't do Filetype autocommands to avoid loading syntax and
4463 * indent scripts, a great speed improvement. */
4464 save_ei = au_event_disable(",Filetype");
4465#endif
4466 /* Don't use modelines here, it's useless. */
4467 save_mls = p_mls;
4468 p_mls = 0;
4469
4470 /* Load file into a buffer, so that 'fileencoding' is detected,
4471 * autocommands applied, etc. */
4472 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4473
4474 p_mls = save_mls;
4475#if defined(FEAT_SYN_HL)
4476 au_event_restore(save_ei);
4477#endif
4478
4479 return buf;
4480}
4481
4482/*
4483 * Check whether a quickfix/location list valid. Autocmds may remove or change
4484 * a quickfix list when vimgrep is running. If the list is not found, create a
4485 * new list.
4486 */
4487 static int
4488vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004489 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004490 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004491 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004492 char_u *title)
4493{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004494 /* Verify that the quickfix/location list was not freed by an autocmd */
4495 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004496 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004497 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004498 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004499 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004500 EMSG(_(e_loc_list_changed));
4501 return FALSE;
4502 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004503 else
4504 {
4505 /* Quickfix list is not found, create a new one. */
4506 qf_new_list(qi, title);
4507 return TRUE;
4508 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004509 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004510
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004511 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004512 /* Autocommands changed the quickfix list. Find the one we were
4513 * using and restore it. */
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004514 qi->qf_curlist = qf_id2nr(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004515
4516 return TRUE;
4517}
4518
4519/*
4520 * Search for a pattern in all the lines in a buffer and add the matching lines
4521 * to a quickfix list.
4522 */
4523 static int
4524vgr_match_buflines(
4525 qf_info_T *qi,
4526 char_u *fname,
4527 buf_T *buf,
4528 regmmatch_T *regmatch,
4529 long tomatch,
4530 int duplicate_name,
4531 int flags)
4532{
4533 int found_match = FALSE;
4534 long lnum;
4535 colnr_T col;
4536
4537 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
4538 {
4539 col = 0;
4540 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
4541 col, NULL, NULL) > 0)
4542 {
4543 /* Pass the buffer number so that it gets used even for a
4544 * dummy buffer, unless duplicate_name is set, then the
4545 * buffer will be wiped out below. */
4546 if (qf_add_entry(qi,
4547 qi->qf_curlist,
4548 NULL, /* dir */
4549 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02004550 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004551 duplicate_name ? 0 : buf->b_fnum,
4552 ml_get_buf(buf,
4553 regmatch->startpos[0].lnum + lnum, FALSE),
4554 regmatch->startpos[0].lnum + lnum,
4555 regmatch->startpos[0].col + 1,
4556 FALSE, /* vis_col */
4557 NULL, /* search pattern */
4558 0, /* nr */
4559 0, /* type */
4560 TRUE /* valid */
4561 ) == FAIL)
4562 {
4563 got_int = TRUE;
4564 break;
4565 }
4566 found_match = TRUE;
4567 if (--tomatch == 0)
4568 break;
4569 if ((flags & VGR_GLOBAL) == 0
4570 || regmatch->endpos[0].lnum > 0)
4571 break;
4572 col = regmatch->endpos[0].col
4573 + (col == regmatch->endpos[0].col);
4574 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
4575 break;
4576 }
4577 line_breakcheck();
4578 if (got_int)
4579 break;
4580 }
4581
4582 return found_match;
4583}
4584
4585/*
4586 * Jump to the first match and update the directory.
4587 */
4588 static void
4589vgr_jump_to_match(
4590 qf_info_T *qi,
4591 int forceit,
4592 int *redraw_for_dummy,
4593 buf_T *first_match_buf,
4594 char_u *target_dir)
4595{
4596 buf_T *buf;
4597
4598 buf = curbuf;
4599 qf_jump(qi, 0, 0, forceit);
4600 if (buf != curbuf)
4601 /* If we jumped to another buffer redrawing will already be
4602 * taken care of. */
4603 *redraw_for_dummy = FALSE;
4604
4605 /* Jump to the directory used after loading the buffer. */
4606 if (curbuf == first_match_buf && target_dir != NULL)
4607 {
4608 exarg_T ea;
4609
4610 ea.arg = target_dir;
4611 ea.cmdidx = CMD_lcd;
4612 ex_cd(&ea);
4613 }
4614}
4615
4616/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004617 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004618 * ":vimgrepadd {pattern} file(s)"
4619 * ":lvimgrep {pattern} file(s)"
4620 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004621 */
4622 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004623ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004624{
Bram Moolenaar81695252004-12-29 20:58:21 +00004625 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004626 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004627 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004628 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004629 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004630 char_u *s;
4631 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004632 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004633 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004634 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004635 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004636 buf_T *buf;
4637 int duplicate_name = FALSE;
4638 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004639 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004640 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004641 buf_T *first_match_buf = NULL;
4642 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004643 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004644 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004645 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004646 char_u *dirname_start = NULL;
4647 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004648 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004649 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004650
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004651 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01004652 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4653 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004654 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004655#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004656 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004657 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004658#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004659 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004660
Bram Moolenaar754b5602006-02-09 23:53:20 +00004661 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004662 || eap->cmdidx == CMD_lvimgrep
4663 || eap->cmdidx == CMD_lgrepadd
4664 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004665 {
4666 qi = ll_get_or_alloc_list(curwin);
4667 if (qi == NULL)
4668 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004669 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004670 }
4671
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004672 if (eap->addr_count > 0)
4673 tomatch = eap->line2;
4674 else
4675 tomatch = MAXLNUM;
4676
Bram Moolenaar81695252004-12-29 20:58:21 +00004677 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004678 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004679 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004680 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004681 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004682 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004683 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004684 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004685 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004686
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004687 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004688 if (regmatch.regprog == NULL)
4689 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004690
4691 p = skipwhite(p);
4692 if (*p == NUL)
4693 {
4694 EMSG(_("E683: File name missing or invalid pattern"));
4695 goto theend;
4696 }
4697
Bram Moolenaar55b69262017-08-13 13:42:01 +02004698 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004699 && eap->cmdidx != CMD_vimgrepadd
4700 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004701 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004702 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004703 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004704
4705 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004706 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004707 goto theend;
4708 if (fcount == 0)
4709 {
4710 EMSG(_(e_nomatch));
4711 goto theend;
4712 }
4713
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004714 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4715 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004716 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004717 {
4718 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004719 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004720 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004721
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004722 /* Remember the current directory, because a BufRead autocommand that does
4723 * ":lcd %:p:h" changes the meaning of short path names. */
4724 mch_dirname(dirname_start, MAXPATHL);
4725
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004726 /* Remember the current quickfix list identifier, so that we can check for
4727 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004728 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004729
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004730 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004731 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004732 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004733 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004734 if (time(NULL) > seconds)
4735 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004736 /* Display the file name every second or so, show the user we are
4737 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004738 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004739 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004740 }
4741
Bram Moolenaar81695252004-12-29 20:58:21 +00004742 buf = buflist_findname_exp(fnames[fi]);
4743 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4744 {
4745 /* Remember that a buffer with this name already exists. */
4746 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004747 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004748 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004749
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004750 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00004751 }
4752 else
4753 /* Use existing, loaded buffer. */
4754 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004755
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004756 /* Check whether the quickfix list is still valid. When loading a
4757 * buffer above, autocommands might have changed the quickfix list. */
4758 if (!vgr_qflist_valid(wp, qi, save_qfid, *eap->cmdlinep))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004759 {
4760 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004761 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004762 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004763 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004764
Bram Moolenaar81695252004-12-29 20:58:21 +00004765 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004766 {
4767 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004768 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004769 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004770 else
4771 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004772 /* Try for a match in all lines of the buffer.
4773 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004774 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
4775 tomatch, duplicate_name, flags);
4776
Bram Moolenaar81695252004-12-29 20:58:21 +00004777 if (using_dummy)
4778 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004779 if (found_match && first_match_buf == NULL)
4780 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004781 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004782 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004783 /* Never keep a dummy buffer if there is another buffer
4784 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004785 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004786 buf = NULL;
4787 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004788 else if (!cmdmod.hide
4789 || buf->b_p_bh[0] == 'u' /* "unload" */
4790 || buf->b_p_bh[0] == 'w' /* "wipe" */
4791 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004792 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004793 /* When no match was found we don't need to remember the
4794 * buffer, wipe it out. If there was a match and it
4795 * wasn't the first one or we won't jump there: only
4796 * unload the buffer.
4797 * Ignore 'hidden' here, because it may lead to having too
4798 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004799 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004800 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004801 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004802 buf = NULL;
4803 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004804 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004805 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004806 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004807 /* Keeping the buffer, remove the dummy flag. */
4808 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004809 buf = NULL;
4810 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004811 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004812
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004813 if (buf != NULL)
4814 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004815 /* Keeping the buffer, remove the dummy flag. */
4816 buf->b_flags &= ~BF_DUMMY;
4817
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004818 /* If the buffer is still loaded we need to use the
4819 * directory we jumped to below. */
4820 if (buf == first_match_buf
4821 && target_dir == NULL
4822 && STRCMP(dirname_start, dirname_now) != 0)
4823 target_dir = vim_strsave(dirname_now);
4824
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004825 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004826 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004827 * need to be done (again). But not the window-local
4828 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004829 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004830#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004831 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4832 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004833#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004834 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004835 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004836 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004837 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004838 }
4839 }
4840
4841 FreeWild(fcount, fnames);
4842
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004843 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4844 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4845 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004846 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004847
Bram Moolenaar864293a2016-06-02 13:40:04 +02004848 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004849
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004850 if (au_name != NULL)
4851 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4852 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004853 /*
4854 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4855 * is still valid.
4856 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004857 if (!qflist_valid(wp, save_qfid))
4858 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004859
Bram Moolenaar86b68352004-12-27 21:59:20 +00004860 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004861 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004862 {
4863 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004864 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
4865 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004866 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004867 else
4868 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004869
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004870 /* If we loaded a dummy buffer into the current window, the autocommands
4871 * may have messed up things, need to redraw and recompute folds. */
4872 if (redraw_for_dummy)
4873 {
4874#ifdef FEAT_FOLDING
4875 foldUpdateAll(curwin);
4876#else
4877 redraw_later(NOT_VALID);
4878#endif
4879 }
4880
Bram Moolenaar86b68352004-12-27 21:59:20 +00004881theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004882 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004883 vim_free(dirname_now);
4884 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004885 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004886 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004887}
4888
4889/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004890 * Restore current working directory to "dirname_start" if they differ, taking
4891 * into account whether it is set locally or globally.
4892 */
4893 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004894restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004895{
4896 char_u *dirname_now = alloc(MAXPATHL);
4897
4898 if (NULL != dirname_now)
4899 {
4900 mch_dirname(dirname_now, MAXPATHL);
4901 if (STRCMP(dirname_start, dirname_now) != 0)
4902 {
4903 /* If the directory has changed, change it back by building up an
4904 * appropriate ex command and executing it. */
4905 exarg_T ea;
4906
4907 ea.arg = dirname_start;
4908 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4909 ex_cd(&ea);
4910 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004911 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004912 }
4913}
4914
4915/*
4916 * Load file "fname" into a dummy buffer and return the buffer pointer,
4917 * placing the directory resulting from the buffer load into the
4918 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4919 * prior to calling this function. Restores directory to "dirname_start" prior
4920 * to returning, if autocmds or the 'autochdir' option have changed it.
4921 *
4922 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4923 * or wipe_dummy_buffer() later!
4924 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004925 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004926 */
4927 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004928load_dummy_buffer(
4929 char_u *fname,
4930 char_u *dirname_start, /* in: old directory */
4931 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004932{
4933 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004934 bufref_T newbufref;
4935 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004936 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004937 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004938 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004939
4940 /* Allocate a buffer without putting it in the buffer list. */
4941 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4942 if (newbuf == NULL)
4943 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004944 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004945
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004946 /* Init the options. */
4947 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4948
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004949 /* need to open the memfile before putting the buffer in a window */
4950 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004951 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004952 /* Make sure this buffer isn't wiped out by auto commands. */
4953 ++newbuf->b_locked;
4954
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004955 /* set curwin/curbuf to buf and save a few things */
4956 aucmd_prepbuf(&aco, newbuf);
4957
4958 /* Need to set the filename for autocommands. */
4959 (void)setfname(curbuf, fname, NULL, FALSE);
4960
Bram Moolenaar81695252004-12-29 20:58:21 +00004961 /* Create swap file now to avoid the ATTENTION message. */
4962 check_need_swap(TRUE);
4963
4964 /* Remove the "dummy" flag, otherwise autocommands may not
4965 * work. */
4966 curbuf->b_flags &= ~BF_DUMMY;
4967
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004968 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004969 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004970 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004971 NULL, READ_NEW | READ_DUMMY);
4972 --newbuf->b_locked;
4973 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004974 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004975 && !(curbuf->b_flags & BF_NEW))
4976 {
4977 failed = FALSE;
4978 if (curbuf != newbuf)
4979 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004980 /* Bloody autocommands changed the buffer! Can happen when
4981 * using netrw and editing a remote file. Use the current
4982 * buffer instead, delete the dummy one after restoring the
4983 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004984 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004985 newbuf = curbuf;
4986 }
4987 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004988
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004989 /* restore curwin/curbuf and a few other things */
4990 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004991 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4992 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004993
4994 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4995 * skip it. */
4996 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004997 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004998
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004999 /*
5000 * When autocommands/'autochdir' option changed directory: go back.
5001 * Let the caller know what the resulting dir was first, in case it is
5002 * important.
5003 */
5004 mch_dirname(resulting_dir, MAXPATHL);
5005 restore_start_dir(dirname_start);
5006
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005007 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005008 return NULL;
5009 if (failed)
5010 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005011 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005012 return NULL;
5013 }
5014 return newbuf;
5015}
5016
5017/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005018 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5019 * directory to "dirname_start" prior to returning, if autocmds or the
5020 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005021 */
5022 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005023wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005024{
5025 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005026 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005027#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005028 cleanup_T cs;
5029
5030 /* Reset the error/interrupt/exception state here so that aborting()
5031 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5032 * work when got_int is set. */
5033 enter_cleanup(&cs);
5034#endif
5035
Bram Moolenaar81695252004-12-29 20:58:21 +00005036 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005037
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005038#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005039 /* Restore the error/interrupt/exception state if not discarded by a
5040 * new aborting error, interrupt, or uncaught exception. */
5041 leave_cleanup(&cs);
5042#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005043 /* When autocommands/'autochdir' option changed directory: go back. */
5044 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005045 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005046}
5047
5048/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005049 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5050 * directory to "dirname_start" prior to returning, if autocmds or the
5051 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005052 */
5053 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005054unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005055{
5056 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005057 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005058 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005059
5060 /* When autocommands/'autochdir' option changed directory: go back. */
5061 restore_start_dir(dirname_start);
5062 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005063}
5064
Bram Moolenaar05159a02005-02-26 23:04:13 +00005065#if defined(FEAT_EVAL) || defined(PROTO)
5066/*
5067 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005068 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005069 */
5070 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005071get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005072{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005073 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005074 dict_T *dict;
5075 char_u buf[2];
5076 qfline_T *qfp;
5077 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005078 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005079
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005080 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005081 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005082 qi = &ql_info;
5083 if (wp != NULL)
5084 {
5085 qi = GET_LOC_LIST(wp);
5086 if (qi == NULL)
5087 return FAIL;
5088 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005089 }
5090
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005091 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005092 qf_idx = qi->qf_curlist;
5093
5094 if (qf_idx >= qi->qf_listcount
5095 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005096 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005097
Bram Moolenaard823fa92016-08-12 16:29:27 +02005098 qfp = qi->qf_lists[qf_idx].qf_start;
5099 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005100 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005101 /* Handle entries with a non-existing buffer number. */
5102 bufnum = qfp->qf_fnum;
5103 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5104 bufnum = 0;
5105
Bram Moolenaar05159a02005-02-26 23:04:13 +00005106 if ((dict = dict_alloc()) == NULL)
5107 return FAIL;
5108 if (list_append_dict(list, dict) == FAIL)
5109 return FAIL;
5110
5111 buf[0] = qfp->qf_type;
5112 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005113 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00005114 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
5115 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
5116 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
5117 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaard76ce852018-05-01 15:02:04 +02005118 || dict_add_nr_str(dict, "module", 0L,
5119 qfp->qf_module == NULL ? (char_u *)"" : qfp->qf_module) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00005120 || dict_add_nr_str(dict, "pattern", 0L,
5121 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
5122 || dict_add_nr_str(dict, "text", 0L,
5123 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00005124 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
5125 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
5126 return FAIL;
5127
5128 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005129 if (qfp == NULL)
5130 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005131 }
5132 return OK;
5133}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005134
5135/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005136 * Flags used by getqflist()/getloclist() to determine which fields to return.
5137 */
5138enum {
5139 QF_GETLIST_NONE = 0x0,
5140 QF_GETLIST_TITLE = 0x1,
5141 QF_GETLIST_ITEMS = 0x2,
5142 QF_GETLIST_NR = 0x4,
5143 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005144 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005145 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005146 QF_GETLIST_IDX = 0x40,
5147 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005148 QF_GETLIST_TICK = 0x100,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005149 QF_GETLIST_ALL = 0x1FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005150};
5151
5152/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005153 * Parse text from 'di' and return the quickfix list items.
5154 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005155 */
5156 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005157qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005158{
5159 int status = FAIL;
5160 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005161 char_u *errorformat = p_efm;
5162 dictitem_T *efm_di;
5163 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005164
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005165 /* Only a List value is supported */
5166 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005167 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005168 /* If errorformat is supplied then use it, otherwise use the 'efm'
5169 * option setting
5170 */
5171 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5172 {
5173 if (efm_di->di_tv.v_type != VAR_STRING ||
5174 efm_di->di_tv.vval.v_string == NULL)
5175 return FAIL;
5176 errorformat = efm_di->di_tv.vval.v_string;
5177 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005178
Bram Moolenaar36538222017-09-02 19:51:44 +02005179 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005180 if (l == NULL)
5181 return FAIL;
5182
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005183 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
5184 if (qi != NULL)
5185 {
5186 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
5187 qi->qf_refcount++;
5188
Bram Moolenaar36538222017-09-02 19:51:44 +02005189 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005190 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5191 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005192 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005193 qf_free(qi, 0);
5194 }
5195 free(qi);
5196 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005197 dict_add_list(retdict, "items", l);
5198 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005199 }
5200
5201 return status;
5202}
5203
5204/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005205 * Return the quickfix/location list window identifier in the current tabpage.
5206 */
5207 static int
5208qf_winid(qf_info_T *qi)
5209{
5210 win_T *win;
5211
5212 /* The quickfix window can be opened even if the quickfix list is not set
5213 * using ":copen". This is not true for location lists. */
5214 if (qi == NULL)
5215 return 0;
5216 win = qf_find_win(qi);
5217 if (win != NULL)
5218 return win->w_id;
5219 return 0;
5220}
5221
5222/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005223 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005224 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005225 static int
5226qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005227{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005228 int flags = QF_GETLIST_NONE;
5229
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005230 if (dict_find(what, (char_u *)"all", -1) != NULL)
5231 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005232
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005233 if (dict_find(what, (char_u *)"title", -1) != NULL)
5234 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005235
Bram Moolenaara6d48492017-12-12 22:45:31 +01005236 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5237 flags |= QF_GETLIST_NR;
5238
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005239 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5240 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005241
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005242 if (dict_find(what, (char_u *)"context", -1) != NULL)
5243 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005244
Bram Moolenaara6d48492017-12-12 22:45:31 +01005245 if (dict_find(what, (char_u *)"id", -1) != NULL)
5246 flags |= QF_GETLIST_ID;
5247
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005248 if (dict_find(what, (char_u *)"items", -1) != NULL)
5249 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005250
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005251 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5252 flags |= QF_GETLIST_IDX;
5253
5254 if (dict_find(what, (char_u *)"size", -1) != NULL)
5255 flags |= QF_GETLIST_SIZE;
5256
Bram Moolenaarb254af32017-12-18 19:48:58 +01005257 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5258 flags |= QF_GETLIST_TICK;
5259
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005260 return flags;
5261}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005262
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005263/*
5264 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5265 * If 'nr' and 'id' are not present in 'what' then return the current
5266 * quickfix list index.
5267 * If 'nr' is zero then return the current quickfix list index.
5268 * If 'nr' is '$' then return the last quickfix list index.
5269 * If 'id' is present then return the index of the quickfix list with that id.
5270 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5271 * Return -1, if quickfix list is not present or if the stack is empty.
5272 */
5273 static int
5274qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5275{
5276 int qf_idx;
5277 dictitem_T *di;
5278
5279 qf_idx = qi->qf_curlist; /* default is the current list */
5280 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5281 {
5282 /* Use the specified quickfix/location list */
5283 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005284 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005285 /* for zero use the current list */
5286 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005287 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005288 qf_idx = di->di_tv.vval.v_number - 1;
5289 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005290 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005291 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005292 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005293 else if (di->di_tv.v_type == VAR_STRING
5294 && di->di_tv.vval.v_string != NULL
5295 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5296 /* Get the last quickfix list number */
5297 qf_idx = qi->qf_listcount - 1;
5298 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005299 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005300 }
5301
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005302 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5303 {
5304 /* Look for a list with the specified id */
5305 if (di->di_tv.v_type == VAR_NUMBER)
5306 {
5307 /*
5308 * For zero, use the current list or the list specified by 'nr'
5309 */
5310 if (di->di_tv.vval.v_number != 0)
5311 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5312 }
5313 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005314 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005315 }
5316
5317 return qf_idx;
5318}
5319
5320/*
5321 * Return default values for quickfix list properties in retdict.
5322 */
5323 static int
5324qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5325{
5326 int status = OK;
5327
5328 if (flags & QF_GETLIST_TITLE)
5329 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5330 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5331 {
5332 list_T *l = list_alloc();
5333 if (l != NULL)
5334 status = dict_add_list(retdict, "items", l);
5335 else
5336 status = FAIL;
5337 }
5338 if ((status == OK) && (flags & QF_GETLIST_NR))
5339 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5340 if ((status == OK) && (flags & QF_GETLIST_WINID))
5341 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
5342 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5343 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5344 if ((status == OK) && (flags & QF_GETLIST_ID))
5345 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5346 if ((status == OK) && (flags & QF_GETLIST_IDX))
5347 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5348 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5349 status = dict_add_nr_str(retdict, "size", 0L, NULL);
5350 if ((status == OK) && (flags & QF_GETLIST_TICK))
5351 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
5352
5353 return status;
5354}
5355
5356/*
5357 * Return the quickfix list title as 'title' in retdict
5358 */
5359 static int
5360qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5361{
5362 char_u *t;
5363
5364 t = qi->qf_lists[qf_idx].qf_title;
5365 if (t == NULL)
5366 t = (char_u *)"";
5367 return dict_add_nr_str(retdict, "title", 0L, t);
5368}
5369
5370/*
5371 * Return the quickfix list items/entries as 'items' in retdict
5372 */
5373 static int
5374qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5375{
5376 int status = OK;
5377 list_T *l = list_alloc();
5378 if (l != NULL)
5379 {
5380 (void)get_errorlist(qi, NULL, qf_idx, l);
5381 dict_add_list(retdict, "items", l);
5382 }
5383 else
5384 status = FAIL;
5385
5386 return status;
5387}
5388
5389/*
5390 * Return the quickfix list context (if any) as 'context' in retdict.
5391 */
5392 static int
5393qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5394{
5395 int status;
5396 dictitem_T *di;
5397
5398 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5399 {
5400 di = dictitem_alloc((char_u *)"context");
5401 if (di != NULL)
5402 {
5403 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5404 status = dict_add(retdict, di);
5405 if (status == FAIL)
5406 dictitem_free(di);
5407 }
5408 else
5409 status = FAIL;
5410 }
5411 else
5412 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5413
5414 return status;
5415}
5416
5417/*
5418 * Return the quickfix list index as 'idx' in retdict
5419 */
5420 static int
5421qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5422{
5423 int idx = qi->qf_lists[qf_idx].qf_index;
5424 if (qi->qf_lists[qf_idx].qf_count == 0)
5425 /* For empty lists, qf_index is set to 1 */
5426 idx = 0;
5427 return dict_add_nr_str(retdict, "idx", idx, NULL);
5428}
5429
5430/*
5431 * Return quickfix/location list details (title) as a
5432 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5433 * then current list is used. Otherwise the specified list is used.
5434 */
5435 int
5436qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5437{
5438 qf_info_T *qi = &ql_info;
5439 int status = OK;
5440 int qf_idx;
5441 dictitem_T *di;
5442 int flags = QF_GETLIST_NONE;
5443
5444 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5445 return qf_get_list_from_lines(what, di, retdict);
5446
5447 if (wp != NULL)
5448 qi = GET_LOC_LIST(wp);
5449
5450 flags = qf_getprop_keys2flags(what);
5451
5452 if (qi != NULL && qi->qf_listcount != 0)
5453 qf_idx = qf_getprop_qfidx(qi, what);
5454
Bram Moolenaara6d48492017-12-12 22:45:31 +01005455 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005456 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005457 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005458
Bram Moolenaard823fa92016-08-12 16:29:27 +02005459 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005460 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005461 if ((status == OK) && (flags & QF_GETLIST_NR))
5462 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5463 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005464 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005465 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005466 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005467 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005468 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005469 if ((status == OK) && (flags & QF_GETLIST_ID))
5470 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5471 NULL);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005472 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005473 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005474 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5475 status = dict_add_nr_str(retdict, "size",
5476 qi->qf_lists[qf_idx].qf_count, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005477 if ((status == OK) && (flags & QF_GETLIST_TICK))
5478 status = dict_add_nr_str(retdict, "changedtick",
5479 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5480
Bram Moolenaard823fa92016-08-12 16:29:27 +02005481 return status;
5482}
5483
5484/*
5485 * Add list of entries to quickfix/location list. Each list entry is
5486 * a dictionary with item information.
5487 */
5488 static int
5489qf_add_entries(
5490 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005491 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005492 list_T *list,
5493 char_u *title,
5494 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005495{
5496 listitem_T *li;
5497 dict_T *d;
Bram Moolenaard76ce852018-05-01 15:02:04 +02005498 char_u *filename, *module, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005499 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005500 long lnum;
5501 int col, nr;
5502 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005503 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005504 int valid, status;
5505 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005506 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005507
Bram Moolenaara3921f42017-06-04 15:30:34 +02005508 if (action == ' ' || qf_idx == qi->qf_listcount)
5509 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005510 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005511 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005512 qf_idx = qi->qf_curlist;
5513 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005514 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005515 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005516 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005517 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005518 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005519 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005520 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005521 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005522
5523 for (li = list->lv_first; li != NULL; li = li->li_next)
5524 {
5525 if (li->li_tv.v_type != VAR_DICT)
5526 continue; /* Skip non-dict items */
5527
5528 d = li->li_tv.vval.v_dict;
5529 if (d == NULL)
5530 continue;
5531
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005532 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaard76ce852018-05-01 15:02:04 +02005533 module = get_dict_string(d, (char_u *)"module", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005534 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5535 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5536 col = (int)get_dict_number(d, (char_u *)"col");
5537 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5538 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005539 type = get_dict_string(d, (char_u *)"type", TRUE);
5540 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5541 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005542 if (text == NULL)
5543 text = vim_strsave((char_u *)"");
5544
5545 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005546 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005547 valid = FALSE;
5548
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005549 /* Mark entries with non-existing buffer number as not valid. Give the
5550 * error message only once. */
5551 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5552 {
5553 if (!did_bufnr_emsg)
5554 {
5555 did_bufnr_emsg = TRUE;
5556 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5557 }
5558 valid = FALSE;
5559 bufnum = 0;
5560 }
5561
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005562 /* If the 'valid' field is present it overrules the detected value. */
5563 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5564 valid = (int)get_dict_number(d, (char_u *)"valid");
5565
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005566 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005567 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005568 NULL, /* dir */
5569 filename,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005570 module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005571 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005572 text,
5573 lnum,
5574 col,
5575 vcol, /* vis_col */
5576 pattern, /* search pattern */
5577 nr,
5578 type == NULL ? NUL : *type,
5579 valid);
5580
5581 vim_free(filename);
Bram Moolenaard76ce852018-05-01 15:02:04 +02005582 vim_free(module);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005583 vim_free(pattern);
5584 vim_free(text);
5585 vim_free(type);
5586
5587 if (status == FAIL)
5588 {
5589 retval = FAIL;
5590 break;
5591 }
5592 }
5593
Bram Moolenaara3921f42017-06-04 15:30:34 +02005594 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005595 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005596 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005597 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005598 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005599 if (action != 'a')
5600 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005601 qi->qf_lists[qf_idx].qf_ptr =
5602 qi->qf_lists[qf_idx].qf_start;
5603 if (qi->qf_lists[qf_idx].qf_count > 0)
5604 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005605 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005606
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005607 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005608 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005609
5610 return retval;
5611}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005612
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005613/*
5614 * Get the quickfix list index from 'nr' or 'id'
5615 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02005616 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005617qf_setprop_get_qfidx(
5618 qf_info_T *qi,
5619 dict_T *what,
5620 int action,
5621 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005622{
5623 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005624 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005625
Bram Moolenaard823fa92016-08-12 16:29:27 +02005626 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5627 {
5628 /* Use the specified quickfix/location list */
5629 if (di->di_tv.v_type == VAR_NUMBER)
5630 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005631 /* for zero use the current list */
5632 if (di->di_tv.vval.v_number != 0)
5633 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005634
Bram Moolenaar55b69262017-08-13 13:42:01 +02005635 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5636 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005637 /*
5638 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005639 * non-available list and add the new list at the end of the
5640 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005641 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005642 *newlist = TRUE;
5643 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005644 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005645 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005646 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005647 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005648 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005649 }
5650 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005651 && di->di_tv.vval.v_string != NULL
5652 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005653 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005654 if (qi->qf_listcount > 0)
5655 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005656 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02005657 qf_idx = 0;
5658 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005659 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005660 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005661 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005662 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005663 }
5664
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005665 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005666 {
5667 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005668 if (di->di_tv.v_type != VAR_NUMBER)
5669 return INVALID_QFIDX;
5670
5671 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005672 }
5673
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005674 return qf_idx;
5675}
5676
5677/*
5678 * Set the quickfix list title.
5679 */
5680 static int
5681qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
5682{
5683 if (di->di_tv.v_type != VAR_STRING)
5684 return FAIL;
5685
5686 vim_free(qi->qf_lists[qf_idx].qf_title);
5687 qi->qf_lists[qf_idx].qf_title =
5688 get_dict_string(what, (char_u *)"title", TRUE);
5689 if (qf_idx == qi->qf_curlist)
5690 qf_update_win_titlevar(qi);
5691
5692 return OK;
5693}
5694
5695/*
5696 * Set quickfix list items/entries.
5697 */
5698 static int
5699qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
5700{
5701 int retval = FAIL;
5702 char_u *title_save;
5703
5704 if (di->di_tv.v_type != VAR_LIST)
5705 return FAIL;
5706
5707 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5708 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5709 title_save, action == ' ' ? 'a' : action);
5710 if (action == 'r')
5711 {
5712 /*
5713 * When replacing the quickfix list entries using
5714 * qf_add_entries(), the title is set with a ':' prefix.
5715 * Restore the title with the saved title.
5716 */
5717 vim_free(qi->qf_lists[qf_idx].qf_title);
5718 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5719 }
5720 vim_free(title_save);
5721
5722 return retval;
5723}
5724
5725/*
5726 * Set quickfix list items/entries from a list of lines.
5727 */
5728 static int
5729qf_setprop_items_from_lines(
5730 qf_info_T *qi,
5731 int qf_idx,
5732 dict_T *what,
5733 dictitem_T *di,
5734 int action)
5735{
5736 char_u *errorformat = p_efm;
5737 dictitem_T *efm_di;
5738 int retval = FAIL;
5739
5740 /* Use the user supplied errorformat settings (if present) */
5741 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5742 {
5743 if (efm_di->di_tv.v_type != VAR_STRING ||
5744 efm_di->di_tv.vval.v_string == NULL)
5745 return FAIL;
5746 errorformat = efm_di->di_tv.vval.v_string;
5747 }
5748
5749 /* Only a List value is supported */
5750 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
5751 return FAIL;
5752
5753 if (action == 'r')
5754 qf_free_items(qi, qf_idx);
5755 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
5756 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5757 retval = OK;
5758
5759 return retval;
5760}
5761
5762/*
5763 * Set quickfix list context.
5764 */
5765 static int
5766qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
5767{
5768 typval_T *ctx;
5769
5770 free_tv(qi->qf_lists[qf_idx].qf_ctx);
5771 ctx = alloc_tv();
5772 if (ctx != NULL)
5773 copy_tv(&di->di_tv, ctx);
5774 qi->qf_lists[qf_idx].qf_ctx = ctx;
5775
5776 return OK;
5777}
5778
5779/*
5780 * Set quickfix/location list properties (title, items, context).
5781 * Also used to add items from parsing a list of lines.
5782 * Used by the setqflist() and setloclist() VimL functions.
5783 */
5784 static int
5785qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
5786{
5787 dictitem_T *di;
5788 int retval = FAIL;
5789 int qf_idx;
5790 int newlist = FALSE;
5791
5792 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5793 newlist = TRUE;
5794
5795 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
5796 if (qf_idx == INVALID_QFIDX) /* List not found */
5797 return FAIL;
5798
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005799 if (newlist)
5800 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005801 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005802 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005803 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005804 }
5805
5806 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005807 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005808 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005809 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005810 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005811 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005812 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005813 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005814
Bram Moolenaarb254af32017-12-18 19:48:58 +01005815 if (retval == OK)
5816 qf_list_changed(qi, qf_idx);
5817
Bram Moolenaard823fa92016-08-12 16:29:27 +02005818 return retval;
5819}
5820
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005821/*
5822 * Find the non-location list window with the specified location list.
5823 */
5824 static win_T *
5825find_win_with_ll(qf_info_T *qi)
5826{
5827 win_T *wp = NULL;
5828
5829 FOR_ALL_WINDOWS(wp)
5830 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5831 return wp;
5832
5833 return NULL;
5834}
5835
5836/*
5837 * Free the entire quickfix/location list stack.
5838 * If the quickfix/location list window is open, then clear it.
5839 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005840 static void
5841qf_free_stack(win_T *wp, qf_info_T *qi)
5842{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005843 win_T *qfwin = qf_find_win(qi);
5844 win_T *llwin = NULL;
5845 win_T *orig_wp = wp;
5846
5847 if (qfwin != NULL)
5848 {
5849 /* If the quickfix/location list window is open, then clear it */
5850 if (qi->qf_curlist < qi->qf_listcount)
5851 qf_free(qi, qi->qf_curlist);
5852 qf_update_buffer(qi, NULL);
5853 }
5854
5855 if (wp != NULL && IS_LL_WINDOW(wp))
5856 {
5857 /* If in the location list window, then use the non-location list
5858 * window with this location list (if present)
5859 */
5860 llwin = find_win_with_ll(qi);
5861 if (llwin != NULL)
5862 wp = llwin;
5863 }
5864
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005865 qf_free_all(wp);
5866 if (wp == NULL)
5867 {
5868 /* quickfix list */
5869 qi->qf_curlist = 0;
5870 qi->qf_listcount = 0;
5871 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005872 else if (IS_LL_WINDOW(orig_wp))
5873 {
5874 /* If the location list window is open, then create a new empty
5875 * location list */
5876 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005877
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005878 /* first free the list reference in the location list window */
5879 ll_free_all(&orig_wp->w_llist_ref);
5880
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005881 orig_wp->w_llist_ref = new_ll;
5882 if (llwin != NULL)
5883 {
5884 llwin->w_llist = new_ll;
5885 new_ll->qf_refcount++;
5886 }
5887 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005888}
5889
Bram Moolenaard823fa92016-08-12 16:29:27 +02005890/*
5891 * Populate the quickfix list with the items supplied in the list
5892 * of dictionaries. "title" will be copied to w:quickfix_title.
5893 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5894 */
5895 int
5896set_errorlist(
5897 win_T *wp,
5898 list_T *list,
5899 int action,
5900 char_u *title,
5901 dict_T *what)
5902{
5903 qf_info_T *qi = &ql_info;
5904 int retval = OK;
5905
5906 if (wp != NULL)
5907 {
5908 qi = ll_get_or_alloc_list(wp);
5909 if (qi == NULL)
5910 return FAIL;
5911 }
5912
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005913 if (action == 'f')
5914 {
5915 /* Free the entire quickfix or location list stack */
5916 qf_free_stack(wp, qi);
5917 }
5918 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005919 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005920 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005921 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005922 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005923 if (retval == OK)
5924 qf_list_changed(qi, qi->qf_curlist);
5925 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005926
5927 return retval;
5928}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005929
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005930/*
5931 * Mark the context as in use for all the lists in a quickfix stack.
5932 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005933 static int
5934mark_quickfix_ctx(qf_info_T *qi, int copyID)
5935{
5936 int i;
5937 int abort = FALSE;
5938 typval_T *ctx;
5939
5940 for (i = 0; i < LISTCOUNT && !abort; ++i)
5941 {
5942 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005943 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5944 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005945 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5946 }
5947
5948 return abort;
5949}
5950
5951/*
5952 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005953 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005954 */
5955 int
5956set_ref_in_quickfix(int copyID)
5957{
5958 int abort = FALSE;
5959 tabpage_T *tp;
5960 win_T *win;
5961
5962 abort = mark_quickfix_ctx(&ql_info, copyID);
5963 if (abort)
5964 return abort;
5965
5966 FOR_ALL_TAB_WINDOWS(tp, win)
5967 {
5968 if (win->w_llist != NULL)
5969 {
5970 abort = mark_quickfix_ctx(win->w_llist, copyID);
5971 if (abort)
5972 return abort;
5973 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005974 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5975 {
5976 /* In a location list window and none of the other windows is
5977 * referring to this location list. Mark the location list
5978 * context as still in use.
5979 */
5980 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5981 if (abort)
5982 return abort;
5983 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005984 }
5985
5986 return abort;
5987}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005988#endif
5989
Bram Moolenaar81695252004-12-29 20:58:21 +00005990/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005991 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005992 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005993 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005994 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005995 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005996 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005997 */
5998 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005999ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006000{
6001 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006002 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006003 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006004 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006005
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006006 switch (eap->cmdidx)
6007 {
6008 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6009 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6010 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6011 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6012 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6013 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6014 default: break;
6015 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006016 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6017 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006018 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006019#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006020 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006021 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006022#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006023 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006024
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006025 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01006026 if (eap->cmdidx == CMD_lbuffer
6027 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006028 || eap->cmdidx == CMD_laddbuffer)
6029 {
6030 qi = ll_get_or_alloc_list(curwin);
6031 if (qi == NULL)
6032 return;
6033 }
6034
Bram Moolenaar86b68352004-12-27 21:59:20 +00006035 if (*eap->arg == NUL)
6036 buf = curbuf;
6037 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6038 buf = buflist_findnr(atoi((char *)eap->arg));
6039 if (buf == NULL)
6040 EMSG(_(e_invarg));
6041 else if (buf->b_ml.ml_mfp == NULL)
6042 EMSG(_("E681: Buffer is not loaded"));
6043 else
6044 {
6045 if (eap->addr_count == 0)
6046 {
6047 eap->line1 = 1;
6048 eap->line2 = buf->b_ml.ml_line_count;
6049 }
6050 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6051 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6052 EMSG(_(e_invrange));
6053 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006054 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006055 char_u *qf_title = *eap->cmdlinep;
6056
6057 if (buf->b_sfname)
6058 {
6059 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6060 (char *)qf_title, (char *)buf->b_sfname);
6061 qf_title = IObuff;
6062 }
6063
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006064 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006065 (eap->cmdidx != CMD_caddbuffer
6066 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006067 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006068 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006069 if (res >= 0)
6070 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006071 if (au_name != NULL)
6072 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6073 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006074 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
6075 eap->cmdidx == CMD_lbuffer))
6076 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006077 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006078 }
6079}
6080
Bram Moolenaar1e015462005-09-25 22:16:38 +00006081#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006082/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006083 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6084 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006085 */
6086 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006087ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006088{
6089 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006090 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006091 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006092 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006093
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006094 switch (eap->cmdidx)
6095 {
6096 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6097 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6098 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6099 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6100 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6101 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6102 default: break;
6103 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006104 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6105 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006106 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006107#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006108 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006109 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006110#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006111 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006112
Bram Moolenaar3c097222017-12-21 20:54:49 +01006113 if (eap->cmdidx == CMD_lexpr
6114 || eap->cmdidx == CMD_lgetexpr
6115 || eap->cmdidx == CMD_laddexpr)
6116 {
6117 qi = ll_get_or_alloc_list(curwin);
6118 if (qi == NULL)
6119 return;
6120 }
6121
Bram Moolenaar4770d092006-01-12 23:22:24 +00006122 /* Evaluate the expression. When the result is a string or a list we can
6123 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006124 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006125 if (tv != NULL)
6126 {
6127 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6128 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6129 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006130 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006131 (eap->cmdidx != CMD_caddexpr
6132 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006133 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006134 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006135 if (res >= 0)
6136 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006137 if (au_name != NULL)
6138 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6139 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006140 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
6141 eap->cmdidx == CMD_lexpr))
6142 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006143 }
6144 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006145 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006146 free_tv(tv);
6147 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006148}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006149#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006150
6151/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006152 * Get the location list for ":lhelpgrep"
6153 */
6154 static qf_info_T *
6155hgr_get_ll(int *new_ll)
6156{
6157 win_T *wp;
6158 qf_info_T *qi;
6159
6160 /* If the current window is a help window, then use it */
6161 if (bt_help(curwin->w_buffer))
6162 wp = curwin;
6163 else
6164 /* Find an existing help window */
6165 FOR_ALL_WINDOWS(wp)
6166 if (bt_help(wp->w_buffer))
6167 break;
6168
6169 if (wp == NULL) /* Help window not found */
6170 qi = NULL;
6171 else
6172 qi = wp->w_llist;
6173
6174 if (qi == NULL)
6175 {
6176 /* Allocate a new location list for help text matches */
6177 if ((qi = ll_new_list()) == NULL)
6178 return NULL;
6179 *new_ll = TRUE;
6180 }
6181
6182 return qi;
6183}
6184
6185/*
6186 * Search for a pattern in a help file.
6187 */
6188 static void
6189hgr_search_file(
6190 qf_info_T *qi,
6191 char_u *fname,
6192#ifdef FEAT_MBYTE
6193 vimconv_T *p_vc,
6194#endif
6195 regmatch_T *p_regmatch)
6196{
6197 FILE *fd;
6198 long lnum;
6199
6200 fd = mch_fopen((char *)fname, "r");
6201 if (fd == NULL)
6202 return;
6203
6204 lnum = 1;
6205 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6206 {
6207 char_u *line = IObuff;
6208#ifdef FEAT_MBYTE
6209 /* Convert a line if 'encoding' is not utf-8 and
6210 * the line contains a non-ASCII character. */
6211 if (p_vc->vc_type != CONV_NONE
6212 && has_non_ascii(IObuff))
6213 {
6214 line = string_convert(p_vc, IObuff, NULL);
6215 if (line == NULL)
6216 line = IObuff;
6217 }
6218#endif
6219
6220 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6221 {
6222 int l = (int)STRLEN(line);
6223
6224 /* remove trailing CR, LF, spaces, etc. */
6225 while (l > 0 && line[l - 1] <= ' ')
6226 line[--l] = NUL;
6227
6228 if (qf_add_entry(qi,
6229 qi->qf_curlist,
6230 NULL, /* dir */
6231 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006232 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006233 0,
6234 line,
6235 lnum,
6236 (int)(p_regmatch->startp[0] - line)
6237 + 1, /* col */
6238 FALSE, /* vis_col */
6239 NULL, /* search pattern */
6240 0, /* nr */
6241 1, /* type */
6242 TRUE /* valid */
6243 ) == FAIL)
6244 {
6245 got_int = TRUE;
6246#ifdef FEAT_MBYTE
6247 if (line != IObuff)
6248 vim_free(line);
6249#endif
6250 break;
6251 }
6252 }
6253#ifdef FEAT_MBYTE
6254 if (line != IObuff)
6255 vim_free(line);
6256#endif
6257 ++lnum;
6258 line_breakcheck();
6259 }
6260 fclose(fd);
6261}
6262
6263/*
6264 * Search for a pattern in all the help files in the doc directory under
6265 * the given directory.
6266 */
6267 static void
6268hgr_search_files_in_dir(
6269 qf_info_T *qi,
6270 char_u *dirname,
6271 regmatch_T *p_regmatch
6272#ifdef FEAT_MBYTE
6273 , vimconv_T *p_vc
6274#endif
6275#ifdef FEAT_MULTI_LANG
6276 , char_u *lang
6277#endif
6278 )
6279{
6280 int fcount;
6281 char_u **fnames;
6282 int fi;
6283
6284 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6285 add_pathsep(dirname);
6286 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6287 if (gen_expand_wildcards(1, &dirname, &fcount,
6288 &fnames, EW_FILE|EW_SILENT) == OK
6289 && fcount > 0)
6290 {
6291 for (fi = 0; fi < fcount && !got_int; ++fi)
6292 {
6293#ifdef FEAT_MULTI_LANG
6294 /* Skip files for a different language. */
6295 if (lang != NULL
6296 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006297 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006298 && !(STRNICMP(lang, "en", 2) == 0
6299 && STRNICMP("txt", fnames[fi]
6300 + STRLEN(fnames[fi]) - 3, 3) == 0))
6301 continue;
6302#endif
6303
6304 hgr_search_file(qi, fnames[fi],
6305#ifdef FEAT_MBYTE
6306 p_vc,
6307#endif
6308 p_regmatch);
6309 }
6310 FreeWild(fcount, fnames);
6311 }
6312}
6313
6314/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006315 * Search for a pattern in all the help files in the 'runtimepath'
6316 * and add the matches to a quickfix list.
6317 * 'arg' is the language specifier. If supplied, then only matches in the
6318 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006319 */
6320 static void
6321hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6322{
6323 char_u *p;
6324#ifdef FEAT_MULTI_LANG
6325 char_u *lang;
6326#endif
6327
6328#ifdef FEAT_MBYTE
6329 vimconv_T vc;
6330
6331 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6332 * differs. */
6333 vc.vc_type = CONV_NONE;
6334 if (!enc_utf8)
6335 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6336#endif
6337
6338#ifdef FEAT_MULTI_LANG
6339 /* Check for a specified language */
6340 lang = check_help_lang(arg);
6341#endif
6342
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006343 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006344 p = p_rtp;
6345 while (*p != NUL && !got_int)
6346 {
6347 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6348
6349 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6350#ifdef FEAT_MBYTE
6351 , &vc
6352#endif
6353#ifdef FEAT_MULTI_LANG
6354 , lang
6355#endif
6356 );
6357 }
6358
6359#ifdef FEAT_MBYTE
6360 if (vc.vc_type != CONV_NONE)
6361 convert_setup(&vc, NULL, NULL);
6362#endif
6363}
6364
6365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 * ":helpgrep {pattern}"
6367 */
6368 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006369ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 regmatch_T regmatch;
6372 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006373 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006374 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006375 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376
Bram Moolenaar73633f82012-01-20 13:39:07 +01006377 switch (eap->cmdidx)
6378 {
6379 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6380 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6381 default: break;
6382 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006383 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6384 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006385 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006386#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006387 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006388 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006389#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006390 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006391
6392 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6393 save_cpo = p_cpo;
6394 p_cpo = empty_option;
6395
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006396 if (eap->cmdidx == CMD_lhelpgrep)
6397 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006398 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006399 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006400 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006401 }
6402
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6404 regmatch.rm_ic = FALSE;
6405 if (regmatch.regprog != NULL)
6406 {
6407 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006408 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006410 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006411
Bram Moolenaar473de612013-06-08 18:19:48 +02006412 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006414 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6415 qi->qf_lists[qi->qf_curlist].qf_ptr =
6416 qi->qf_lists[qi->qf_curlist].qf_start;
6417 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006420 if (p_cpo == empty_option)
6421 p_cpo = save_cpo;
6422 else
6423 /* Darn, some plugin changed the value. */
6424 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
Bram Moolenaarb254af32017-12-18 19:48:58 +01006426 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006427 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
Bram Moolenaar73633f82012-01-20 13:39:07 +01006429 if (au_name != NULL)
6430 {
6431 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6432 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006433 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006434 /* autocommands made "qi" invalid */
6435 return;
6436 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006437
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006439 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006440 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006441 else
6442 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006443
6444 if (eap->cmdidx == CMD_lhelpgrep)
6445 {
6446 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006447 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006448 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006449 {
6450 if (new_qi)
6451 ll_free_all(&qi);
6452 }
6453 else if (curwin->w_llist == NULL)
6454 curwin->w_llist = qi;
6455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456}
6457
6458#endif /* FEAT_QUICKFIX */