blob: 80308103877ea1679a6479412e1ab93287d9ec46 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020050/*
51 * Quickfix/Location list definition
52 * Contains a list of entries (qfline_T). qf_start points to the first entry
53 * and qf_last points to the last entry. qf_count contains the list size.
54 *
55 * Usually the list contains one or more entries. But an empty list can be
56 * created using setqflist()/setloclist() with a title and/or user context
57 * information and entries can be added later using setqflist()/setloclist().
58 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000059typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000060{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020061 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000062 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020063 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020065 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 int qf_index; /* current index in the error list */
67 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020068 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020069 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020070 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020071
72 struct dir_stack_T *qf_dir_stack;
73 char_u *qf_directory;
74 struct dir_stack_T *qf_file_stack;
75 char_u *qf_currfile;
76 int qf_multiline;
77 int qf_multiignore;
78 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010079 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000080} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020082/*
83 * Quickfix/Location list stack definition
84 * Contains a list of quickfix/location lists (qf_list_T)
85 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000086struct qf_info_S
87{
88 /*
89 * Count of references to this list. Used only for location lists.
90 * When a location list window reference this list, qf_refcount
91 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
92 * reaches 0, the list is freed.
93 */
94 int qf_refcount;
95 int qf_listcount; /* current number of lists */
96 int qf_curlist; /* current error list */
97 qf_list_T qf_lists[LISTCOUNT];
98};
99
100static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200101static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000103#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/*
106 * Structure used to hold the info of one part of 'errorformat'
107 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000108typedef struct efm_S efm_T;
109struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110{
111 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000112 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
114 char_u prefix; /* prefix of this format line: */
115 /* 'D' enter directory */
116 /* 'X' leave directory */
117 /* 'A' start of multi-line message */
118 /* 'E' error message */
119 /* 'W' warning message */
120 /* 'I' informational message */
121 /* 'C' continuation line */
122 /* 'Z' end of multi-line message */
123 /* 'G' general, unspecific message */
124 /* 'P' push file (partial) message */
125 /* 'Q' pop/quit file (partial) message */
126 /* 'O' overread (partial) message */
127 char_u flags; /* additional flags given in prefix */
128 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000129 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000130 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131};
132
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100133static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
134
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200135static int qf_init_ext(qf_info_T *qi, int qf_idx, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title, char_u *enc);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200136static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_new_list(qf_info_T *qi, char_u *qf_title);
138static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200139static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static 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 Moolenaar3c097222017-12-21 20:54:49 +0100147static int qflist_valid(win_T *wp, int_u qf_id);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
149static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
151static int is_qf_win(win_T *win, qf_info_T *qi);
152static win_T *qf_find_win(qf_info_T *qi);
153static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200154static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200156static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static char_u *get_mef_name(void);
158static void restore_start_dir(char_u *dirname_start);
159static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
160static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
161static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
162static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164/* Quickfix window check helper macro */
165#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
166/* Location list window check helper macro */
167#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
168/*
169 * Return location list for window 'wp'
170 * For location list window, return the referenced location list
171 */
172#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
173
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200175 * Looking up a buffer can be slow if there are many. Remember the last one
176 * to make this a lot faster if there are multiple matches in the same file.
177 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200178static char_u *qf_last_bufname = NULL;
179static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200180
Bram Moolenaar3c097222017-12-21 20:54:49 +0100181static char *e_loc_list_changed =
182 N_("E926: Current location list was changed");
183
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200184/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000185 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200186 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 * Return -1 for error, number of errors for success.
188 */
189 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200190qf_init(win_T *wp,
191 char_u *efile,
192 char_u *errorformat,
193 int newlist, /* TRUE: start a new error list */
194 char_u *qf_title,
195 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000197 qf_info_T *qi = &ql_info;
198
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000199 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000200 {
201 qi = ll_get_or_alloc_list(wp);
202 if (qi == NULL)
203 return FAIL;
204 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000205
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200206 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
207 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000208}
209
210/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200211 * Maximum number of bytes allowed per line while reading a errorfile.
212 */
213#define LINE_MAXLEN 4096
214
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200215static struct fmtpattern
216{
217 char_u convchar;
218 char *pattern;
219} fmt_pat[FMT_PATTERNS] =
220 {
221 {'f', ".\\+"}, /* only used when at end */
222 {'n', "\\d\\+"},
223 {'l', "\\d\\+"},
224 {'c', "\\d\\+"},
225 {'t', "."},
226 {'m', ".\\+"},
227 {'r', ".*"},
228 {'p', "[- .]*"},
229 {'v', "\\d\\+"},
230 {'s', ".\\+"}
231 };
232
233/*
234 * Converts a 'errorformat' string to regular expression pattern
235 */
236 static int
237efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200238 char_u *efm,
239 int len,
240 efm_T *fmt_ptr,
241 char_u *regpat,
242 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200243{
244 char_u *ptr;
245 char_u *efmp;
246 char_u *srcptr;
247 int round;
248 int idx = 0;
249
250 /*
251 * Build regexp pattern from current 'errorformat' option
252 */
253 ptr = regpat;
254 *ptr++ = '^';
255 round = 0;
256 for (efmp = efm; efmp < efm + len; ++efmp)
257 {
258 if (*efmp == '%')
259 {
260 ++efmp;
261 for (idx = 0; idx < FMT_PATTERNS; ++idx)
262 if (fmt_pat[idx].convchar == *efmp)
263 break;
264 if (idx < FMT_PATTERNS)
265 {
266 if (fmt_ptr->addr[idx])
267 {
268 sprintf((char *)errmsg,
269 _("E372: Too many %%%c in format string"), *efmp);
270 EMSG(errmsg);
271 return -1;
272 }
273 if ((idx
274 && idx < 6
275 && vim_strchr((char_u *)"DXOPQ",
276 fmt_ptr->prefix) != NULL)
277 || (idx == 6
278 && vim_strchr((char_u *)"OPQ",
279 fmt_ptr->prefix) == NULL))
280 {
281 sprintf((char *)errmsg,
282 _("E373: Unexpected %%%c in format string"), *efmp);
283 EMSG(errmsg);
284 return -1;
285 }
286 fmt_ptr->addr[idx] = (char_u)++round;
287 *ptr++ = '\\';
288 *ptr++ = '(';
289#ifdef BACKSLASH_IN_FILENAME
290 if (*efmp == 'f')
291 {
292 /* Also match "c:" in the file name, even when
293 * checking for a colon next: "%f:".
294 * "\%(\a:\)\=" */
295 STRCPY(ptr, "\\%(\\a:\\)\\=");
296 ptr += 10;
297 }
298#endif
299 if (*efmp == 'f' && efmp[1] != NUL)
300 {
301 if (efmp[1] != '\\' && efmp[1] != '%')
302 {
303 /* A file name may contain spaces, but this isn't
304 * in "\f". For "%f:%l:%m" there may be a ":" in
305 * the file name. Use ".\{-1,}x" instead (x is
306 * the next character), the requirement that :999:
307 * follows should work. */
308 STRCPY(ptr, ".\\{-1,}");
309 ptr += 7;
310 }
311 else
312 {
313 /* File name followed by '\\' or '%': include as
314 * many file name chars as possible. */
315 STRCPY(ptr, "\\f\\+");
316 ptr += 4;
317 }
318 }
319 else
320 {
321 srcptr = (char_u *)fmt_pat[idx].pattern;
322 while ((*ptr = *srcptr++) != NUL)
323 ++ptr;
324 }
325 *ptr++ = '\\';
326 *ptr++ = ')';
327 }
328 else if (*efmp == '*')
329 {
330 if (*++efmp == '[' || *efmp == '\\')
331 {
332 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
333 {
334 if (efmp[1] == '^')
335 *ptr++ = *++efmp;
336 if (efmp < efm + len)
337 {
338 *ptr++ = *++efmp; /* could be ']' */
339 while (efmp < efm + len
340 && (*ptr++ = *++efmp) != ']')
341 /* skip */;
342 if (efmp == efm + len)
343 {
344 EMSG(_("E374: Missing ] in format string"));
345 return -1;
346 }
347 }
348 }
349 else if (efmp < efm + len) /* %*\D, %*\s etc. */
350 *ptr++ = *++efmp;
351 *ptr++ = '\\';
352 *ptr++ = '+';
353 }
354 else
355 {
356 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
357 sprintf((char *)errmsg,
358 _("E375: Unsupported %%%c in format string"), *efmp);
359 EMSG(errmsg);
360 return -1;
361 }
362 }
363 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
364 *ptr++ = *efmp; /* regexp magic characters */
365 else if (*efmp == '#')
366 *ptr++ = '*';
367 else if (*efmp == '>')
368 fmt_ptr->conthere = TRUE;
369 else if (efmp == efm + 1) /* analyse prefix */
370 {
371 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
372 fmt_ptr->flags = *efmp++;
373 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
374 fmt_ptr->prefix = *efmp;
375 else
376 {
377 sprintf((char *)errmsg,
378 _("E376: Invalid %%%c in format string prefix"), *efmp);
379 EMSG(errmsg);
380 return -1;
381 }
382 }
383 else
384 {
385 sprintf((char *)errmsg,
386 _("E377: Invalid %%%c in format string"), *efmp);
387 EMSG(errmsg);
388 return -1;
389 }
390 }
391 else /* copy normal character */
392 {
393 if (*efmp == '\\' && efmp + 1 < efm + len)
394 ++efmp;
395 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
396 *ptr++ = '\\'; /* escape regexp atoms */
397 if (*efmp)
398 *ptr++ = *efmp;
399 }
400 }
401 *ptr++ = '$';
402 *ptr = NUL;
403
404 return 0;
405}
406
407 static void
408free_efm_list(efm_T **efm_first)
409{
410 efm_T *efm_ptr;
411
412 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
413 {
414 *efm_first = efm_ptr->next;
415 vim_regfree(efm_ptr->prog);
416 vim_free(efm_ptr);
417 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100418 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419}
420
421/* Parse 'errorformat' option */
422 static efm_T *
423parse_efm_option(char_u *efm)
424{
425 char_u *errmsg = NULL;
426 int errmsglen;
427 efm_T *fmt_ptr = NULL;
428 efm_T *fmt_first = NULL;
429 efm_T *fmt_last = NULL;
430 char_u *fmtstr = NULL;
431 int len;
432 int i;
433 int round;
434
435 errmsglen = CMDBUFFSIZE + 1;
436 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
437 if (errmsg == NULL)
438 goto parse_efm_end;
439
440 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200441 * Each part of the format string is copied and modified from errorformat
442 * to regex prog. Only a few % characters are allowed.
443 */
444
445 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 * Get some space to modify the format string into.
447 */
448 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
449 for (round = FMT_PATTERNS; round > 0; )
450 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200451#ifdef BACKSLASH_IN_FILENAME
452 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453#else
454 i += 2; /* "%f" can become two chars longer */
455#endif
456 if ((fmtstr = alloc(i)) == NULL)
457 goto parse_efm_error;
458
459 while (efm[0] != NUL)
460 {
461 /*
462 * Allocate a new eformat structure and put it at the end of the list
463 */
464 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
465 if (fmt_ptr == NULL)
466 goto parse_efm_error;
467 if (fmt_first == NULL) /* first one */
468 fmt_first = fmt_ptr;
469 else
470 fmt_last->next = fmt_ptr;
471 fmt_last = fmt_ptr;
472
473 /*
474 * Isolate one part in the 'errorformat' option
475 */
476 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
477 if (efm[len] == '\\' && efm[len + 1] != NUL)
478 ++len;
479
480 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
481 goto parse_efm_error;
482 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
483 goto parse_efm_error;
484 /*
485 * Advance to next part
486 */
487 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
488 }
489
490 if (fmt_first == NULL) /* nothing found */
491 EMSG(_("E378: 'errorformat' contains no pattern"));
492
493 goto parse_efm_end;
494
495parse_efm_error:
496 free_efm_list(&fmt_first);
497
498parse_efm_end:
499 vim_free(fmtstr);
500 vim_free(errmsg);
501
502 return fmt_first;
503}
504
Bram Moolenaare0d37972016-07-15 22:36:01 +0200505enum {
506 QF_FAIL = 0,
507 QF_OK = 1,
508 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200509 QF_NOMEM = 3,
510 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200511};
512
513typedef struct {
514 char_u *linebuf;
515 int linelen;
516 char_u *growbuf;
517 int growbufsiz;
518 FILE *fd;
519 typval_T *tv;
520 char_u *p_str;
521 listitem_T *p_li;
522 buf_T *buf;
523 linenr_T buflnum;
524 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100525 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200526} qfstate_T;
527
528 static char_u *
529qf_grow_linebuf(qfstate_T *state, int newsz)
530{
531 /*
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 {
545 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
546 if (state->growbuf == NULL)
547 return NULL;
548 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 {
702 if (fgets((char *)state->growbuf + growbuflen,
703 state->growbufsiz - growbuflen, state->fd) == NULL)
704 break;
705 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
706 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200707 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200708 break;
709 if (state->growbufsiz == LINE_MAXLEN)
710 {
711 discard = TRUE;
712 break;
713 }
714
715 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
716 ? 2 * state->growbufsiz : LINE_MAXLEN;
717 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
718 if (state->growbuf == NULL)
719 return QF_NOMEM;
720 }
721
722 while (discard)
723 {
724 /*
725 * The current line is longer than LINE_MAXLEN, continue
726 * reading but discard everything until EOL or EOF is
727 * reached.
728 */
729 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
730 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200731 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200732 break;
733 }
734
735 state->linebuf = state->growbuf;
736 state->linelen = growbuflen;
737 }
738 else
739 state->linebuf = IObuff;
740
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100741#ifdef FEAT_MBYTE
742 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200743 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
744 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100745 char_u *line;
746
747 line = string_convert(&state->vc, state->linebuf, &state->linelen);
748 if (line != NULL)
749 {
750 if (state->linelen < IOSIZE)
751 {
752 STRCPY(state->linebuf, line);
753 vim_free(line);
754 }
755 else
756 {
757 vim_free(state->growbuf);
758 state->linebuf = state->growbuf = line;
759 state->growbufsiz = state->linelen < LINE_MAXLEN
760 ? state->linelen : LINE_MAXLEN;
761 }
762 }
763 }
764#endif
765
Bram Moolenaare0d37972016-07-15 22:36:01 +0200766 return QF_OK;
767}
768
769/*
770 * Get the next string from a file/buffer/list/string.
771 */
772 static int
773qf_get_nextline(qfstate_T *state)
774{
775 int status = QF_FAIL;
776
777 if (state->fd == NULL)
778 {
779 if (state->tv != NULL)
780 {
781 if (state->tv->v_type == VAR_STRING)
782 /* Get the next line from the supplied string */
783 status = qf_get_next_str_line(state);
784 else if (state->tv->v_type == VAR_LIST)
785 /* Get the next line from the supplied list */
786 status = qf_get_next_list_line(state);
787 }
788 else
789 /* Get the next line from the supplied buffer */
790 status = qf_get_next_buf_line(state);
791 }
792 else
793 /* Get the next line from the supplied file */
794 status = qf_get_next_file_line(state);
795
796 if (status != QF_OK)
797 return status;
798
799 /* remove newline/CR from the line */
800 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200801 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802 state->linebuf[state->linelen - 1] = NUL;
803#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200804 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
805 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200807 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808
809#ifdef FEAT_MBYTE
810 remove_bom(state->linebuf);
811#endif
812
813 return QF_OK;
814}
815
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200816typedef struct {
817 char_u *namebuf;
818 char_u *errmsg;
819 int errmsglen;
820 long lnum;
821 int col;
822 char_u use_viscol;
823 char_u *pattern;
824 int enr;
825 int type;
826 int valid;
827} qffields_T;
828
829/*
830 * Parse a line and get the quickfix fields.
831 * Return the QF_ status.
832 */
833 static int
834qf_parse_line(
835 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200836 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200837 char_u *linebuf,
838 int linelen,
839 efm_T *fmt_first,
840 qffields_T *fields)
841{
842 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200843 char_u *ptr;
844 int len;
845 int i;
846 int idx = 0;
847 char_u *tail = NULL;
848 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200849 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200850
851 /* Always ignore case when looking for a matching error. */
852 regmatch.rm_ic = TRUE;
853
854 /* If there was no %> item start at the first pattern */
855 if (fmt_start == NULL)
856 fmt_ptr = fmt_first;
857 else
858 {
859 fmt_ptr = fmt_start;
860 fmt_start = NULL;
861 }
862
863 /*
864 * Try to match each part of 'errorformat' until we find a complete
865 * match or no match.
866 */
867 fields->valid = TRUE;
868restofline:
869 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
870 {
871 int r;
872
873 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200874 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200875 continue;
876 fields->namebuf[0] = NUL;
877 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200878 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200879 fields->errmsg[0] = NUL;
880 fields->lnum = 0;
881 fields->col = 0;
882 fields->use_viscol = FALSE;
883 fields->enr = -1;
884 fields->type = 0;
885 tail = NULL;
886
887 regmatch.regprog = fmt_ptr->prog;
888 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
889 fmt_ptr->prog = regmatch.regprog;
890 if (r)
891 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200892 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200893 continue;
894 if (vim_strchr((char_u *)"EWI", idx) != NULL)
895 fields->type = idx;
896 else
897 fields->type = 0;
898 /*
899 * Extract error message data from matched line.
900 * We check for an actual submatch, because "\[" and "\]" in
901 * the 'errorformat' may cause the wrong submatch to be used.
902 */
903 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
904 {
905 int c;
906
907 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
908 continue;
909
910 /* Expand ~/file and $HOME/file to full path. */
911 c = *regmatch.endp[i];
912 *regmatch.endp[i] = NUL;
913 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
914 *regmatch.endp[i] = c;
915
916 if (vim_strchr((char_u *)"OPQ", idx) != NULL
917 && mch_getperm(fields->namebuf) == -1)
918 continue;
919 }
920 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
921 {
922 if (regmatch.startp[i] == NULL)
923 continue;
924 fields->enr = (int)atol((char *)regmatch.startp[i]);
925 }
926 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
927 {
928 if (regmatch.startp[i] == NULL)
929 continue;
930 fields->lnum = atol((char *)regmatch.startp[i]);
931 }
932 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
933 {
934 if (regmatch.startp[i] == NULL)
935 continue;
936 fields->col = (int)atol((char *)regmatch.startp[i]);
937 }
938 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
939 {
940 if (regmatch.startp[i] == NULL)
941 continue;
942 fields->type = *regmatch.startp[i];
943 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200944 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200945 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200946 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200947 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200948 /* linelen + null terminator */
949 if ((fields->errmsg = vim_realloc(fields->errmsg,
950 linelen + 1)) == NULL)
951 return QF_NOMEM;
952 fields->errmsglen = linelen + 1;
953 }
954 vim_strncpy(fields->errmsg, linebuf, linelen);
955 }
956 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
957 {
958 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
959 continue;
960 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200961 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200962 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200963 /* len + null terminator */
964 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
965 == NULL)
966 return QF_NOMEM;
967 fields->errmsglen = len + 1;
968 }
969 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
970 }
971 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
972 {
973 if (regmatch.startp[i] == NULL)
974 continue;
975 tail = regmatch.startp[i];
976 }
977 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
978 {
979 char_u *match_ptr;
980
981 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
982 continue;
983 fields->col = 0;
984 for (match_ptr = regmatch.startp[i];
985 match_ptr != regmatch.endp[i]; ++match_ptr)
986 {
987 ++fields->col;
988 if (*match_ptr == TAB)
989 {
990 fields->col += 7;
991 fields->col -= fields->col % 8;
992 }
993 }
994 ++fields->col;
995 fields->use_viscol = TRUE;
996 }
997 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
998 {
999 if (regmatch.startp[i] == NULL)
1000 continue;
1001 fields->col = (int)atol((char *)regmatch.startp[i]);
1002 fields->use_viscol = TRUE;
1003 }
1004 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1005 {
1006 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1007 continue;
1008 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1009 if (len > CMDBUFFSIZE - 5)
1010 len = CMDBUFFSIZE - 5;
1011 STRCPY(fields->pattern, "^\\V");
1012 STRNCAT(fields->pattern, regmatch.startp[i], len);
1013 fields->pattern[len + 3] = '\\';
1014 fields->pattern[len + 4] = '$';
1015 fields->pattern[len + 5] = NUL;
1016 }
1017 break;
1018 }
1019 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001020 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001021
1022 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1023 {
1024 if (fmt_ptr != NULL)
1025 {
1026 if (idx == 'D') /* enter directory */
1027 {
1028 if (*fields->namebuf == NUL)
1029 {
1030 EMSG(_("E379: Missing or empty directory name"));
1031 return QF_FAIL;
1032 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001033 qfl->qf_directory =
1034 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1035 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001036 return QF_FAIL;
1037 }
1038 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001039 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001040 }
1041 fields->namebuf[0] = NUL; /* no match found, remove file name */
1042 fields->lnum = 0; /* don't jump to this line */
1043 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001044 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001045 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001046 /* linelen + null terminator */
1047 if ((fields->errmsg = vim_realloc(fields->errmsg,
1048 linelen + 1)) == NULL)
1049 return QF_NOMEM;
1050 fields->errmsglen = linelen + 1;
1051 }
1052 /* copy whole line to error message */
1053 vim_strncpy(fields->errmsg, linebuf, linelen);
1054 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001055 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001056 }
1057 else if (fmt_ptr != NULL)
1058 {
1059 /* honor %> item */
1060 if (fmt_ptr->conthere)
1061 fmt_start = fmt_ptr;
1062
1063 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1064 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001065 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1066 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001067 }
1068 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1069 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001070 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001071 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001072 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001073
Bram Moolenaar9b457942016-10-09 16:10:05 +02001074 if (qfprev == NULL)
1075 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001076 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001077 {
1078 len = (int)STRLEN(qfprev->qf_text);
1079 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1080 == NULL)
1081 return QF_FAIL;
1082 STRCPY(ptr, qfprev->qf_text);
1083 vim_free(qfprev->qf_text);
1084 qfprev->qf_text = ptr;
1085 *(ptr += len) = '\n';
1086 STRCPY(++ptr, fields->errmsg);
1087 }
1088 if (qfprev->qf_nr == -1)
1089 qfprev->qf_nr = fields->enr;
1090 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1091 /* only printable chars allowed */
1092 qfprev->qf_type = fields->type;
1093
1094 if (!qfprev->qf_lnum)
1095 qfprev->qf_lnum = fields->lnum;
1096 if (!qfprev->qf_col)
1097 qfprev->qf_col = fields->col;
1098 qfprev->qf_viscol = fields->use_viscol;
1099 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001100 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1101 qfl->qf_directory,
1102 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001103 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001104 : qfl->qf_currfile != NULL && fields->valid
1105 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001106 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001107 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001108 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001109 line_breakcheck();
1110 return QF_IGNORE_LINE;
1111 }
1112 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1113 {
1114 /* global file names */
1115 fields->valid = FALSE;
1116 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1117 {
1118 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001119 qfl->qf_currfile =
1120 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001121 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001122 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001123 *fields->namebuf = NUL;
1124 if (tail && *tail)
1125 {
1126 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001127 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001128 goto restofline;
1129 }
1130 }
1131 }
1132 if (fmt_ptr->flags == '-') /* generally exclude this line */
1133 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001134 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001135 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001136 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001137 return QF_IGNORE_LINE;
1138 }
1139 }
1140
1141 return QF_OK;
1142}
1143
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001144/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001145 * Read the errorfile "efile" into memory, line by line, building the error
1146 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001147 * Alternative: when "efile" is NULL read errors from buffer "buf".
1148 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001149 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001150 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1151 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001152 * Return -1 for error, number of errors for success.
1153 */
1154 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001155qf_init_ext(
1156 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001157 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001158 char_u *efile,
1159 buf_T *buf,
1160 typval_T *tv,
1161 char_u *errorformat,
1162 int newlist, /* TRUE: start a new error list */
1163 linenr_T lnumfirst, /* first line number to use */
1164 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001165 char_u *qf_title,
1166 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001167{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001168 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001169 qfstate_T state;
1170 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001171 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001172 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001173 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001175 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001177 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001179 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001180 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001181
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001182 vim_memset(&state, 0, sizeof(state));
1183 vim_memset(&fields, 0, sizeof(fields));
1184#ifdef FEAT_MBYTE
1185 state.vc.vc_type = CONV_NONE;
1186 if (enc != NULL && *enc != NUL)
1187 convert_setup(&state.vc, enc, p_enc);
1188#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001189 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1190 fields.errmsglen = CMDBUFFSIZE + 1;
1191 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1192 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001193 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 goto qf_init_end;
1195
Bram Moolenaare0d37972016-07-15 22:36:01 +02001196 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
1198 EMSG2(_(e_openerrf), efile);
1199 goto qf_init_end;
1200 }
1201
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001202 if (newlist || qf_idx == qi->qf_listcount)
1203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001205 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001206 qf_idx = qi->qf_curlist;
1207 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001208 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001209 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001210 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001211 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001212 if (qi->qf_lists[qf_idx].qf_count > 0)
1213 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001216 qfl = &qi->qf_lists[qf_idx];
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001219 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001220 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 else
1222 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001224 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001225 * If the errorformat didn't change between calls, then reuse the
1226 * previously parsed values.
1227 */
1228 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1229 {
1230 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001231 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001232 free_efm_list(&fmt_first);
1233
1234 /* parse the current 'efm' */
1235 fmt_first = parse_efm_option(efm);
1236 if (fmt_first != NULL)
1237 last_efm = vim_strsave(efm);
1238 }
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
1243 /*
1244 * got_int is reset here, because it was probably set when killing the
1245 * ":make" command, but we still want to read the errorfile then.
1246 */
1247 got_int = FALSE;
1248
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001249 if (tv != NULL)
1250 {
1251 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001252 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001253 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001254 state.p_li = tv->vval.v_list->lv_first;
1255 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001256 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001257 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001258 state.buflnum = lnumfirst;
1259 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 /*
1262 * Read the lines in the error file one by one.
1263 * Try to recognize one of the error formats in each line.
1264 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001265 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001267 /* Get the next line from a file/buffer/list/string */
1268 status = qf_get_nextline(&state);
1269 if (status == QF_NOMEM) /* memory alloc failure */
1270 goto qf_init_end;
1271 if (status == QF_END_OF_INPUT) /* end of input */
1272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001274 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1275 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001276 if (status == QF_FAIL)
1277 goto error2;
1278 if (status == QF_NOMEM)
1279 goto qf_init_end;
1280 if (status == QF_IGNORE_LINE)
1281 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001283 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001284 qf_idx,
1285 qfl->qf_directory,
1286 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001287 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001288 : ((qfl->qf_currfile != NULL && fields.valid)
1289 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001290 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001291 fields.errmsg,
1292 fields.lnum,
1293 fields.col,
1294 fields.use_viscol,
1295 fields.pattern,
1296 fields.enr,
1297 fields.type,
1298 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 goto error2;
1300 line_breakcheck();
1301 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001302 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001304 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001306 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001307 qfl->qf_ptr = qfl->qf_start;
1308 qfl->qf_index = 1;
1309 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 else
1312 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001313 qfl->qf_nonevalid = FALSE;
1314 if (qfl->qf_ptr == NULL)
1315 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001317 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001318 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001319 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 EMSG(_(e_readerrf));
1322error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001323 if (!adding)
1324 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001325 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001326 qf_free(qi, qi->qf_curlist);
1327 qi->qf_listcount--;
1328 if (qi->qf_curlist > 0)
1329 --qi->qf_curlist;
1330 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001331qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001332 if (state.fd != NULL)
1333 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001334 vim_free(fields.namebuf);
1335 vim_free(fields.errmsg);
1336 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001337 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001339 if (qf_idx == qi->qf_curlist)
1340 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001341#ifdef FEAT_MBYTE
1342 if (state.vc.vc_type != CONV_NONE)
1343 convert_setup(&state.vc, NULL, NULL);
1344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 return retval;
1347}
1348
Bram Moolenaarfb604092014-07-23 15:55:00 +02001349 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001350qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001351{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001352 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001353
Bram Moolenaarfb604092014-07-23 15:55:00 +02001354 if (title != NULL)
1355 {
1356 char_u *p = alloc((int)STRLEN(title) + 2);
1357
Bram Moolenaara3921f42017-06-04 15:30:34 +02001358 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001359 if (p != NULL)
1360 sprintf((char *)p, ":%s", (char *)title);
1361 }
1362}
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001365 * Prepare for adding a new quickfix list. If the current list is in the
1366 * middle of the stack, then all the following lists are freed and then
1367 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 */
1369 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001370qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
1372 int i;
1373
1374 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001375 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 * the current entry. This makes it possible to browse in a tree-like
1377 * way with ":grep'.
1378 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001379 while (qi->qf_listcount > qi->qf_curlist + 1)
1380 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382 /*
1383 * When the stack is full, remove to oldest entry
1384 * Otherwise, add a new entry.
1385 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001386 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001388 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001390 qi->qf_lists[i - 1] = qi->qf_lists[i];
1391 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001394 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001395 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001396 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001397 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398}
1399
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001400/*
1401 * Free a location list
1402 */
1403 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001404ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001405{
1406 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001407 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001408
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001409 qi = *pqi;
1410 if (qi == NULL)
1411 return;
1412 *pqi = NULL; /* Remove reference to this list */
1413
1414 qi->qf_refcount--;
1415 if (qi->qf_refcount < 1)
1416 {
1417 /* No references to this location list */
1418 for (i = 0; i < qi->qf_listcount; ++i)
1419 qf_free(qi, i);
1420 vim_free(qi);
1421 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001422}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001423
1424 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001425qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001426{
1427 int i;
1428 qf_info_T *qi = &ql_info;
1429
1430 if (wp != NULL)
1431 {
1432 /* location list */
1433 ll_free_all(&wp->w_llist);
1434 ll_free_all(&wp->w_llist_ref);
1435 }
1436 else
1437 /* quickfix list */
1438 for (i = 0; i < qi->qf_listcount; ++i)
1439 qf_free(qi, i);
1440}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001441
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442/*
1443 * Add an entry to the end of the list of errors.
1444 * Returns OK or FAIL.
1445 */
1446 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001447qf_add_entry(
1448 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001449 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001450 char_u *dir, /* optional directory name */
1451 char_u *fname, /* file name or NULL */
1452 int bufnum, /* buffer number or zero */
1453 char_u *mesg, /* message */
1454 long lnum, /* line number */
1455 int col, /* column */
1456 int vis_col, /* using visual column */
1457 char_u *pattern, /* search pattern */
1458 int nr, /* error number */
1459 int type, /* type character */
1460 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001462 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001463 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001465 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001467 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001468 {
1469 buf_T *buf = buflist_findnr(bufnum);
1470
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001471 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001472 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001473 buf->b_has_qf_entry |=
1474 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001475 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001476 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001477 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1479 {
1480 vim_free(qfp);
1481 return FAIL;
1482 }
1483 qfp->qf_lnum = lnum;
1484 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001485 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001486 if (pattern == NULL || *pattern == NUL)
1487 qfp->qf_pattern = NULL;
1488 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1489 {
1490 vim_free(qfp->qf_text);
1491 vim_free(qfp);
1492 return FAIL;
1493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 qfp->qf_nr = nr;
1495 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1496 type = 0;
1497 qfp->qf_type = type;
1498 qfp->qf_valid = valid;
1499
Bram Moolenaara3921f42017-06-04 15:30:34 +02001500 lastp = &qi->qf_lists[qf_idx].qf_last;
1501 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001502 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001504 qi->qf_lists[qf_idx].qf_start = qfp;
1505 qi->qf_lists[qf_idx].qf_ptr = qfp;
1506 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001507 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509 else
1510 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001511 qfp->qf_prev = *lastp;
1512 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001514 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001516 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001517 ++qi->qf_lists[qf_idx].qf_count;
1518 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001519 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001521 qi->qf_lists[qf_idx].qf_index =
1522 qi->qf_lists[qf_idx].qf_count;
1523 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 }
1525
1526 return OK;
1527}
1528
1529/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001530 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001531 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001532 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001533ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001535 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001536
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001537 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1538 if (qi != NULL)
1539 {
1540 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1541 qi->qf_refcount++;
1542 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001544 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001545}
1546
1547/*
1548 * Return the location list for window 'wp'.
1549 * If not present, allocate a location list
1550 */
1551 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001552ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001553{
1554 if (IS_LL_WINDOW(wp))
1555 /* For a location list window, use the referenced location list */
1556 return wp->w_llist_ref;
1557
1558 /*
1559 * For a non-location list window, w_llist_ref should not point to a
1560 * location list.
1561 */
1562 ll_free_all(&wp->w_llist_ref);
1563
1564 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001565 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001566 return wp->w_llist;
1567}
1568
1569/*
1570 * Copy the location list from window "from" to window "to".
1571 */
1572 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001573copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001574{
1575 qf_info_T *qi;
1576 int idx;
1577 int i;
1578
1579 /*
1580 * When copying from a location list window, copy the referenced
1581 * location list. For other windows, copy the location list for
1582 * that window.
1583 */
1584 if (IS_LL_WINDOW(from))
1585 qi = from->w_llist_ref;
1586 else
1587 qi = from->w_llist;
1588
1589 if (qi == NULL) /* no location list to copy */
1590 return;
1591
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001592 /* allocate a new location list */
1593 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001594 return;
1595
1596 to->w_llist->qf_listcount = qi->qf_listcount;
1597
1598 /* Copy the location lists one at a time */
1599 for (idx = 0; idx < qi->qf_listcount; idx++)
1600 {
1601 qf_list_T *from_qfl;
1602 qf_list_T *to_qfl;
1603
1604 to->w_llist->qf_curlist = idx;
1605
1606 from_qfl = &qi->qf_lists[idx];
1607 to_qfl = &to->w_llist->qf_lists[idx];
1608
1609 /* Some of the fields are populated by qf_add_entry() */
1610 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1611 to_qfl->qf_count = 0;
1612 to_qfl->qf_index = 0;
1613 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001614 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001615 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001616 if (from_qfl->qf_title != NULL)
1617 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1618 else
1619 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001620 if (from_qfl->qf_ctx != NULL)
1621 {
1622 to_qfl->qf_ctx = alloc_tv();
1623 if (to_qfl->qf_ctx != NULL)
1624 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1625 }
1626 else
1627 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001628
1629 if (from_qfl->qf_count)
1630 {
1631 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001632 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001633
1634 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001635 for (i = 0, from_qfp = from_qfl->qf_start;
1636 i < from_qfl->qf_count && from_qfp != NULL;
1637 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001638 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001639 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001640 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001641 NULL,
1642 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001643 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 from_qfp->qf_text,
1645 from_qfp->qf_lnum,
1646 from_qfp->qf_col,
1647 from_qfp->qf_viscol,
1648 from_qfp->qf_pattern,
1649 from_qfp->qf_nr,
1650 0,
1651 from_qfp->qf_valid) == FAIL)
1652 {
1653 qf_free_all(to);
1654 return;
1655 }
1656 /*
1657 * qf_add_entry() will not set the qf_num field, as the
1658 * directory and file names are not supplied. So the qf_fnum
1659 * field is copied here.
1660 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001661 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001662 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1663 prevp->qf_type = from_qfp->qf_type; /* error type */
1664 if (from_qfl->qf_ptr == from_qfp)
1665 to_qfl->qf_ptr = prevp; /* current location */
1666 }
1667 }
1668
1669 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1670
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001671 /* Assign a new ID for the location list */
1672 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001673 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001674
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001675 /* When no valid entries are present in the list, qf_ptr points to
1676 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001677 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001678 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001679 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001680 to_qfl->qf_index = 1;
1681 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001682 }
1683
1684 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1685}
1686
1687/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001688 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001689 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 */
1691 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001692qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
Bram Moolenaar82404332016-07-10 17:00:38 +02001694 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001695 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001696 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001697
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 if (fname == NULL || *fname == NUL) /* no file name */
1699 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaare60acc12011-05-10 16:41:25 +02001701#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001702 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001703#endif
1704#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001705 if (directory != NULL)
1706 slash_adjust(directory);
1707 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001708#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001709 if (directory != NULL && !vim_isAbsName(fname)
1710 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1711 {
1712 /*
1713 * Here we check if the file really exists.
1714 * This should normally be true, but if make works without
1715 * "leaving directory"-messages we might have missed a
1716 * directory change.
1717 */
1718 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001721 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001722 if (directory)
1723 ptr = concat_fnames(directory, fname, TRUE);
1724 else
1725 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001727 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001728 bufname = ptr;
1729 }
1730 else
1731 bufname = fname;
1732
1733 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001734 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001735 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001736 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001737 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001739 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001740 {
1741 vim_free(qf_last_bufname);
1742 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1743 if (bufname == ptr)
1744 qf_last_bufname = bufname;
1745 else
1746 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001747 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001748 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001749 if (buf == NULL)
1750 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001751
Bram Moolenaarc1542742016-07-20 21:44:37 +02001752 buf->b_has_qf_entry =
1753 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001754 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755}
1756
1757/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001758 * Push dirbuf onto the directory stack and return pointer to actual dir or
1759 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 */
1761 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001762qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 struct dir_stack_T *ds_new;
1765 struct dir_stack_T *ds_ptr;
1766
1767 /* allocate new stack element and hook it in */
1768 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1769 if (ds_new == NULL)
1770 return NULL;
1771
1772 ds_new->next = *stackptr;
1773 *stackptr = ds_new;
1774
1775 /* store directory on the stack */
1776 if (vim_isAbsName(dirbuf)
1777 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001778 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 (*stackptr)->dirname = vim_strsave(dirbuf);
1780 else
1781 {
1782 /* Okay we don't have an absolute path.
1783 * dirbuf must be a subdir of one of the directories on the stack.
1784 * Let's search...
1785 */
1786 ds_new = (*stackptr)->next;
1787 (*stackptr)->dirname = NULL;
1788 while (ds_new)
1789 {
1790 vim_free((*stackptr)->dirname);
1791 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1792 TRUE);
1793 if (mch_isdir((*stackptr)->dirname) == TRUE)
1794 break;
1795
1796 ds_new = ds_new->next;
1797 }
1798
1799 /* clean up all dirs we already left */
1800 while ((*stackptr)->next != ds_new)
1801 {
1802 ds_ptr = (*stackptr)->next;
1803 (*stackptr)->next = (*stackptr)->next->next;
1804 vim_free(ds_ptr->dirname);
1805 vim_free(ds_ptr);
1806 }
1807
1808 /* Nothing found -> it must be on top level */
1809 if (ds_new == NULL)
1810 {
1811 vim_free((*stackptr)->dirname);
1812 (*stackptr)->dirname = vim_strsave(dirbuf);
1813 }
1814 }
1815
1816 if ((*stackptr)->dirname != NULL)
1817 return (*stackptr)->dirname;
1818 else
1819 {
1820 ds_ptr = *stackptr;
1821 *stackptr = (*stackptr)->next;
1822 vim_free(ds_ptr);
1823 return NULL;
1824 }
1825}
1826
1827
1828/*
1829 * pop dirbuf from the directory stack and return previous directory or NULL if
1830 * stack is empty
1831 */
1832 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001833qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 struct dir_stack_T *ds_ptr;
1836
1837 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1838 * What to do if it isn't? */
1839
1840 /* pop top element and free it */
1841 if (*stackptr != NULL)
1842 {
1843 ds_ptr = *stackptr;
1844 *stackptr = (*stackptr)->next;
1845 vim_free(ds_ptr->dirname);
1846 vim_free(ds_ptr);
1847 }
1848
1849 /* return NEW top element as current dir or NULL if stack is empty*/
1850 return *stackptr ? (*stackptr)->dirname : NULL;
1851}
1852
1853/*
1854 * clean up directory stack
1855 */
1856 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001857qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
1859 struct dir_stack_T *ds_ptr;
1860
1861 while ((ds_ptr = *stackptr) != NULL)
1862 {
1863 *stackptr = (*stackptr)->next;
1864 vim_free(ds_ptr->dirname);
1865 vim_free(ds_ptr);
1866 }
1867}
1868
1869/*
1870 * Check in which directory of the directory stack the given file can be
1871 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001872 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 * Cleans up intermediate directory entries.
1874 *
1875 * TODO: How to solve the following problem?
1876 * If we have the this directory tree:
1877 * ./
1878 * ./aa
1879 * ./aa/bb
1880 * ./bb
1881 * ./bb/x.c
1882 * and make says:
1883 * making all in aa
1884 * making all in bb
1885 * x.c:9: Error
1886 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1887 * qf_guess_filepath will return NULL.
1888 */
1889 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001890qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 struct dir_stack_T *ds_ptr;
1893 struct dir_stack_T *ds_tmp;
1894 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001895 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001898 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 return NULL;
1900
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001901 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 fullname = NULL;
1903 while (ds_ptr)
1904 {
1905 vim_free(fullname);
1906 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1907
1908 /* If concat_fnames failed, just go on. The worst thing that can happen
1909 * is that we delete the entire stack.
1910 */
1911 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1912 break;
1913
1914 ds_ptr = ds_ptr->next;
1915 }
1916
1917 vim_free(fullname);
1918
1919 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001920 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001922 ds_tmp = qfl->qf_dir_stack->next;
1923 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 vim_free(ds_tmp->dirname);
1925 vim_free(ds_tmp);
1926 }
1927
1928 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929}
1930
1931/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01001932 * Returns TRUE if a quickfix/location list with the given identifier exists.
1933 */
1934 static int
1935qflist_valid (win_T *wp, int_u qf_id)
1936{
1937 qf_info_T *qi = &ql_info;
1938 int i;
1939
1940 if (wp != NULL)
1941 {
1942 qi = GET_LOC_LIST(wp); /* Location list */
1943 if (qi == NULL)
1944 return FALSE;
1945 }
1946
1947 for (i = 0; i < qi->qf_listcount; ++i)
1948 if (qi->qf_lists[i].qf_id == qf_id)
1949 return TRUE;
1950
1951 return FALSE;
1952}
1953
1954/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001955 * When loading a file from the quickfix, the auto commands may modify it.
1956 * This may invalidate the current quickfix entry. This function checks
1957 * whether a entry is still present in the quickfix.
1958 * Similar to location list.
1959 */
1960 static int
1961is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1962{
1963 qf_list_T *qfl;
1964 qfline_T *qfp;
1965 int i;
1966
1967 qfl = &qi->qf_lists[qi->qf_curlist];
1968
1969 /* Search for the entry in the current list */
1970 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1971 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001972 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001973 break;
1974
1975 if (i == qfl->qf_count) /* Entry is not found */
1976 return FALSE;
1977
1978 return TRUE;
1979}
1980
1981/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001982 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001983 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001984 */
1985 static qfline_T *
1986get_next_valid_entry(
1987 qf_info_T *qi,
1988 qfline_T *qf_ptr,
1989 int *qf_index,
1990 int dir)
1991{
1992 int idx;
1993 int old_qf_fnum;
1994
1995 idx = *qf_index;
1996 old_qf_fnum = qf_ptr->qf_fnum;
1997
1998 do
1999 {
2000 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2001 || qf_ptr->qf_next == NULL)
2002 return NULL;
2003 ++idx;
2004 qf_ptr = qf_ptr->qf_next;
2005 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2006 && !qf_ptr->qf_valid)
2007 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2008
2009 *qf_index = idx;
2010 return qf_ptr;
2011}
2012
2013/*
2014 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002015 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002016 */
2017 static qfline_T *
2018get_prev_valid_entry(
2019 qf_info_T *qi,
2020 qfline_T *qf_ptr,
2021 int *qf_index,
2022 int dir)
2023{
2024 int idx;
2025 int old_qf_fnum;
2026
2027 idx = *qf_index;
2028 old_qf_fnum = qf_ptr->qf_fnum;
2029
2030 do
2031 {
2032 if (idx == 1 || qf_ptr->qf_prev == NULL)
2033 return NULL;
2034 --idx;
2035 qf_ptr = qf_ptr->qf_prev;
2036 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2037 && !qf_ptr->qf_valid)
2038 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2039
2040 *qf_index = idx;
2041 return qf_ptr;
2042}
2043
2044/*
2045 * Get the n'th (errornr) previous/next valid entry from the current entry in
2046 * the quickfix list.
2047 * dir == FORWARD or FORWARD_FILE: next valid entry
2048 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2049 */
2050 static qfline_T *
2051get_nth_valid_entry(
2052 qf_info_T *qi,
2053 int errornr,
2054 qfline_T *qf_ptr,
2055 int *qf_index,
2056 int dir)
2057{
2058 qfline_T *prev_qf_ptr;
2059 int prev_index;
2060 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2061 char_u *err = e_no_more_items;
2062
2063 while (errornr--)
2064 {
2065 prev_qf_ptr = qf_ptr;
2066 prev_index = *qf_index;
2067
2068 if (dir == FORWARD || dir == FORWARD_FILE)
2069 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2070 else
2071 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2072 if (qf_ptr == NULL)
2073 {
2074 qf_ptr = prev_qf_ptr;
2075 *qf_index = prev_index;
2076 if (err != NULL)
2077 {
2078 EMSG(_(err));
2079 return NULL;
2080 }
2081 break;
2082 }
2083
2084 err = NULL;
2085 }
2086
2087 return qf_ptr;
2088}
2089
2090/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002091 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002092 */
2093 static qfline_T *
2094get_nth_entry(
2095 qf_info_T *qi,
2096 int errornr,
2097 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002098 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002099{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002100 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002101
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002102 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002103 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2104 {
2105 --qf_idx;
2106 qf_ptr = qf_ptr->qf_prev;
2107 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002108 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002109 while (errornr > qf_idx &&
2110 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2111 qf_ptr->qf_next != NULL)
2112 {
2113 ++qf_idx;
2114 qf_ptr = qf_ptr->qf_next;
2115 }
2116
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002117 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002118 return qf_ptr;
2119}
2120
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002121/*
2122 * Find a help window or open one.
2123 */
2124 static int
2125jump_to_help_window(qf_info_T *qi, int *opened_window)
2126{
2127 win_T *wp;
2128 int flags;
2129
2130 if (cmdmod.tab != 0)
2131 wp = NULL;
2132 else
2133 FOR_ALL_WINDOWS(wp)
2134 if (bt_help(wp->w_buffer))
2135 break;
2136 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2137 win_enter(wp, TRUE);
2138 else
2139 {
2140 /*
2141 * Split off help window; put it at far top if no position
2142 * specified, the current window is vertically split and narrow.
2143 */
2144 flags = WSP_HELP;
2145 if (cmdmod.split == 0 && curwin->w_width != Columns
2146 && curwin->w_width < 80)
2147 flags |= WSP_TOP;
2148 if (qi != &ql_info)
2149 flags |= WSP_NEWLOC; /* don't copy the location list */
2150
2151 if (win_split(0, flags) == FAIL)
2152 return FAIL;
2153
2154 *opened_window = TRUE;
2155
2156 if (curwin->w_height < p_hh)
2157 win_setheight((int)p_hh);
2158
2159 if (qi != &ql_info) /* not a quickfix list */
2160 {
2161 /* The new window should use the supplied location list */
2162 curwin->w_llist = qi;
2163 qi->qf_refcount++;
2164 }
2165 }
2166
2167 if (!p_im)
2168 restart_edit = 0; /* don't want insert mode in help file */
2169
2170 return OK;
2171}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002172
2173/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002174 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2175 * If the file is already opened in a window, jump to it.
2176 */
2177 static int
2178qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2179{
2180 win_T *usable_win_ptr = NULL;
2181 int usable_win;
2182 qf_info_T *ll_ref;
2183 int flags;
2184 win_T *win;
2185 win_T *altwin;
2186
2187 usable_win = 0;
2188
2189 ll_ref = curwin->w_llist_ref;
2190 if (ll_ref != NULL)
2191 {
2192 /* Find a window using the same location list that is not a
2193 * quickfix window. */
2194 FOR_ALL_WINDOWS(usable_win_ptr)
2195 if (usable_win_ptr->w_llist == ll_ref
2196 && !bt_quickfix(usable_win_ptr->w_buffer))
2197 {
2198 usable_win = 1;
2199 break;
2200 }
2201 }
2202
2203 if (!usable_win)
2204 {
2205 /* Locate a window showing a normal buffer */
2206 FOR_ALL_WINDOWS(win)
2207 if (win->w_buffer->b_p_bt[0] == NUL)
2208 {
2209 usable_win = 1;
2210 break;
2211 }
2212 }
2213
2214 /*
2215 * If no usable window is found and 'switchbuf' contains "usetab"
2216 * then search in other tabs.
2217 */
2218 if (!usable_win && (swb_flags & SWB_USETAB))
2219 {
2220 tabpage_T *tp;
2221 win_T *wp;
2222
2223 FOR_ALL_TAB_WINDOWS(tp, wp)
2224 {
2225 if (wp->w_buffer->b_fnum == qf_fnum)
2226 {
2227 goto_tabpage_win(tp, wp);
2228 usable_win = 1;
2229 goto win_found;
2230 }
2231 }
2232 }
2233win_found:
2234
2235 /*
2236 * If there is only one window and it is the quickfix window, create a
2237 * new one above the quickfix window.
2238 */
2239 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2240 {
2241 flags = WSP_ABOVE;
2242 if (ll_ref != NULL)
2243 flags |= WSP_NEWLOC;
2244 if (win_split(0, flags) == FAIL)
2245 return FAIL; /* not enough room for window */
2246 *opened_window = TRUE; /* close it when fail */
2247 p_swb = empty_option; /* don't split again */
2248 swb_flags = 0;
2249 RESET_BINDING(curwin);
2250 if (ll_ref != NULL)
2251 {
2252 /* The new window should use the location list from the
2253 * location list window */
2254 curwin->w_llist = ll_ref;
2255 ll_ref->qf_refcount++;
2256 }
2257 }
2258 else
2259 {
2260 if (curwin->w_llist_ref != NULL)
2261 {
2262 /* In a location window */
2263 win = usable_win_ptr;
2264 if (win == NULL)
2265 {
2266 /* Find the window showing the selected file */
2267 FOR_ALL_WINDOWS(win)
2268 if (win->w_buffer->b_fnum == qf_fnum)
2269 break;
2270 if (win == NULL)
2271 {
2272 /* Find a previous usable window */
2273 win = curwin;
2274 do
2275 {
2276 if (win->w_buffer->b_p_bt[0] == NUL)
2277 break;
2278 if (win->w_prev == NULL)
2279 win = lastwin; /* wrap around the top */
2280 else
2281 win = win->w_prev; /* go to previous window */
2282 } while (win != curwin);
2283 }
2284 }
2285 win_goto(win);
2286
2287 /* If the location list for the window is not set, then set it
2288 * to the location list from the location window */
2289 if (win->w_llist == NULL)
2290 {
2291 win->w_llist = ll_ref;
2292 ll_ref->qf_refcount++;
2293 }
2294 }
2295 else
2296 {
2297
2298 /*
2299 * Try to find a window that shows the right buffer.
2300 * Default to the window just above the quickfix buffer.
2301 */
2302 win = curwin;
2303 altwin = NULL;
2304 for (;;)
2305 {
2306 if (win->w_buffer->b_fnum == qf_fnum)
2307 break;
2308 if (win->w_prev == NULL)
2309 win = lastwin; /* wrap around the top */
2310 else
2311 win = win->w_prev; /* go to previous window */
2312
2313 if (IS_QF_WINDOW(win))
2314 {
2315 /* Didn't find it, go to the window before the quickfix
2316 * window. */
2317 if (altwin != NULL)
2318 win = altwin;
2319 else if (curwin->w_prev != NULL)
2320 win = curwin->w_prev;
2321 else
2322 win = curwin->w_next;
2323 break;
2324 }
2325
2326 /* Remember a usable window. */
2327 if (altwin == NULL && !win->w_p_pvw
2328 && win->w_buffer->b_p_bt[0] == NUL)
2329 altwin = win;
2330 }
2331
2332 win_goto(win);
2333 }
2334 }
2335
2336 return OK;
2337}
2338
2339/*
2340 * Edit the selected file or help file.
2341 */
2342 static int
2343qf_jump_edit_buffer(
2344 qf_info_T *qi,
2345 qfline_T *qf_ptr,
2346 int forceit,
2347 win_T *oldwin,
2348 int *opened_window,
2349 int *abort)
2350{
2351 int retval = OK;
2352
2353 if (qf_ptr->qf_type == 1)
2354 {
2355 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2356 * set b_p_ro flag). */
2357 if (!can_abandon(curbuf, forceit))
2358 {
2359 no_write_message();
2360 retval = FALSE;
2361 }
2362 else
2363 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2364 ECMD_HIDE + ECMD_SET_HELP,
2365 oldwin == curwin ? curwin : NULL);
2366 }
2367 else
2368 {
2369 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002370 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002371
2372 retval = buflist_getfile(qf_ptr->qf_fnum,
2373 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002374
2375 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002376 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002377 /*
2378 * Location list. Check whether the associated window is still
2379 * present and the list is still valid.
2380 */
2381 if (!win_valid_any_tab(oldwin))
2382 {
2383 EMSG(_("E924: Current window was closed"));
2384 *abort = TRUE;
2385 *opened_window = FALSE;
2386 }
2387 else if (!qflist_valid(oldwin, save_qfid))
2388 {
2389 EMSG(_(e_loc_list_changed));
2390 *abort = TRUE;
2391 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002392 }
2393 else if (old_qf_curlist != qi->qf_curlist
2394 || !is_qf_entry_present(qi, qf_ptr))
2395 {
2396 if (qi == &ql_info)
2397 EMSG(_("E925: Current quickfix was changed"));
2398 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002399 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002400 *abort = TRUE;
2401 }
2402
2403 if (*abort)
2404 retval = FALSE;
2405 }
2406
2407 return retval;
2408}
2409
2410/*
2411 * Goto the error line in the current file using either line/column number or a
2412 * search pattern.
2413 */
2414 static void
2415qf_jump_goto_line(
2416 linenr_T qf_lnum,
2417 int qf_col,
2418 char_u qf_viscol,
2419 char_u *qf_pattern)
2420{
2421 linenr_T i;
2422 char_u *line;
2423 colnr_T screen_col;
2424 colnr_T char_col;
2425
2426 if (qf_pattern == NULL)
2427 {
2428 /*
2429 * Go to line with error, unless qf_lnum is 0.
2430 */
2431 i = qf_lnum;
2432 if (i > 0)
2433 {
2434 if (i > curbuf->b_ml.ml_line_count)
2435 i = curbuf->b_ml.ml_line_count;
2436 curwin->w_cursor.lnum = i;
2437 }
2438 if (qf_col > 0)
2439 {
2440 curwin->w_cursor.col = qf_col - 1;
2441#ifdef FEAT_VIRTUALEDIT
2442 curwin->w_cursor.coladd = 0;
2443#endif
2444 if (qf_viscol == TRUE)
2445 {
2446 /*
2447 * Check each character from the beginning of the error
2448 * line up to the error column. For each tab character
2449 * found, reduce the error column value by the length of
2450 * a tab character.
2451 */
2452 line = ml_get_curline();
2453 screen_col = 0;
2454 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2455 {
2456 if (*line == NUL)
2457 break;
2458 if (*line++ == '\t')
2459 {
2460 curwin->w_cursor.col -= 7 - (screen_col % 8);
2461 screen_col += 8 - (screen_col % 8);
2462 }
2463 else
2464 ++screen_col;
2465 }
2466 }
2467 check_cursor();
2468 }
2469 else
2470 beginline(BL_WHITE | BL_FIX);
2471 }
2472 else
2473 {
2474 pos_T save_cursor;
2475
2476 /* Move the cursor to the first line in the buffer */
2477 save_cursor = curwin->w_cursor;
2478 curwin->w_cursor.lnum = 0;
2479 if (!do_search(NULL, '/', qf_pattern, (long)1,
2480 SEARCH_KEEP, NULL, NULL))
2481 curwin->w_cursor = save_cursor;
2482 }
2483}
2484
2485/*
2486 * Display quickfix list index and size message
2487 */
2488 static void
2489qf_jump_print_msg(
2490 qf_info_T *qi,
2491 int qf_index,
2492 qfline_T *qf_ptr,
2493 buf_T *old_curbuf,
2494 linenr_T old_lnum)
2495{
2496 linenr_T i;
2497 int len;
2498
2499 /* Update the screen before showing the message, unless the screen
2500 * scrolled up. */
2501 if (!msg_scrolled)
2502 update_topline_redraw();
2503 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2504 qi->qf_lists[qi->qf_curlist].qf_count,
2505 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2506 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2507 /* Add the message, skipping leading whitespace and newlines. */
2508 len = (int)STRLEN(IObuff);
2509 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2510
2511 /* Output the message. Overwrite to avoid scrolling when the 'O'
2512 * flag is present in 'shortmess'; But when not jumping, print the
2513 * whole message. */
2514 i = msg_scroll;
2515 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2516 msg_scroll = TRUE;
2517 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2518 msg_scroll = FALSE;
2519 msg_attr_keep(IObuff, 0, TRUE);
2520 msg_scroll = i;
2521}
2522
2523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * jump to a quickfix line
2525 * if dir == FORWARD go "errornr" valid entries forward
2526 * if dir == BACKWARD go "errornr" valid entries backward
2527 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2528 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2529 * else if "errornr" is zero, redisplay the same line
2530 * else go to entry "errornr"
2531 */
2532 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002533qf_jump(qf_info_T *qi,
2534 int dir,
2535 int errornr,
2536 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002538 qfline_T *qf_ptr;
2539 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 buf_T *old_curbuf;
2543 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002544 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002545 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002547 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549#ifdef FEAT_FOLDING
2550 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2551#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002552 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002554 if (qi == NULL)
2555 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002556
2557 if (qi->qf_curlist >= qi->qf_listcount
2558 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 EMSG(_(e_quickfix));
2561 return;
2562 }
2563
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002564 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002566 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002568 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002570 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2571 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002573 qf_ptr = old_qf_ptr;
2574 qf_index = old_qf_index;
2575 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577 }
2578 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002579 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002581 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2582 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 /* No need to print the error message if it's visible in the error
2584 * window */
2585 print_message = FALSE;
2586
2587 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002588 * For ":helpgrep" find a help window or open one.
2589 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002590 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002591 if (jump_to_help_window(qi, &opened_window) == FAIL)
2592 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002593
2594 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 * If currently in the quickfix window, find another window to show the
2596 * file in.
2597 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002598 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
2600 /*
2601 * If there is no file specified, we don't know where to go.
2602 * But do advance, otherwise ":cn" gets stuck.
2603 */
2604 if (qf_ptr->qf_fnum == 0)
2605 goto theend;
2606
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002607 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2608 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
2611 /*
2612 * If there is a file name,
2613 * read the wanted file if needed, and check autowrite etc.
2614 */
2615 old_curbuf = curbuf;
2616 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002617
2618 if (qf_ptr->qf_fnum != 0)
2619 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002620 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002621
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002622 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2623 &opened_window, &abort);
2624 if (abort)
2625 {
2626 qi = NULL;
2627 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002628 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002629 }
2630
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002631 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
2633 /* When not switched to another buffer, still need to set pc mark */
2634 if (curbuf == old_curbuf)
2635 setpcmark();
2636
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002637 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2638 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
2640#ifdef FEAT_FOLDING
2641 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2642 foldOpenCursor();
2643#endif
2644 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002645 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
2647 else
2648 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (opened_window)
2650 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002651 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
2653 /*
2654 * Couldn't open file, so put index back where it was. This could
2655 * happen if the file was readonly and we changed something.
2656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 qf_ptr = old_qf_ptr;
2659 qf_index = old_qf_index;
2660 }
2661 }
2662theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002663 if (qi != NULL)
2664 {
2665 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2666 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (p_swb != old_swb && opened_window)
2669 {
2670 /* Restore old 'switchbuf' value, but not when an autocommand or
2671 * modeline has changed the value. */
2672 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002673 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002675 swb_flags = old_swb_flags;
2676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 else
2678 free_string_option(old_swb);
2679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680}
2681
2682/*
2683 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002684 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 */
2686 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002687qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002689 buf_T *buf;
2690 char_u *fname;
2691 qfline_T *qfp;
2692 int i;
2693 int idx1 = 1;
2694 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002695 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002696 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002697 int qfFileAttr;
2698 int qfSepAttr;
2699 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002700 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002702 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002704 if (eap->cmdidx == CMD_llist)
2705 {
2706 qi = GET_LOC_LIST(curwin);
2707 if (qi == NULL)
2708 {
2709 EMSG(_(e_loclist));
2710 return;
2711 }
2712 }
2713
2714 if (qi->qf_curlist >= qi->qf_listcount
2715 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
2717 EMSG(_(e_quickfix));
2718 return;
2719 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002720 if (*arg == '+')
2721 {
2722 ++arg;
2723 plus = TRUE;
2724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2726 {
2727 EMSG(_(e_trailing));
2728 return;
2729 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002730 if (plus)
2731 {
2732 i = qi->qf_lists[qi->qf_curlist].qf_index;
2733 idx2 = i + idx1;
2734 idx1 = i;
2735 }
2736 else
2737 {
2738 i = qi->qf_lists[qi->qf_curlist].qf_count;
2739 if (idx1 < 0)
2740 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2741 if (idx2 < 0)
2742 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002745 /*
2746 * Get the attributes for the different quickfix highlight items. Note
2747 * that this depends on syntax items defined in the qf.vim syntax file
2748 */
2749 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2750 if (qfFileAttr == 0)
2751 qfFileAttr = HL_ATTR(HLF_D);
2752 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2753 if (qfSepAttr == 0)
2754 qfSepAttr = HL_ATTR(HLF_D);
2755 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2756 if (qfLineAttr == 0)
2757 qfLineAttr = HL_ATTR(HLF_N);
2758
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002759 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002761 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2762 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
2764 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2765 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002766 msg_putchar('\n');
2767 if (got_int)
2768 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002769
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002770 fname = NULL;
2771 if (qfp->qf_fnum != 0
2772 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2773 {
2774 fname = buf->b_fname;
2775 if (qfp->qf_type == 1) /* :helpgrep */
2776 fname = gettail(fname);
2777 }
2778 if (fname == NULL)
2779 sprintf((char *)IObuff, "%2d", i);
2780 else
2781 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2782 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002784 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2785
2786 if (qfp->qf_lnum != 0)
2787 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788 if (qfp->qf_lnum == 0)
2789 IObuff[0] = NUL;
2790 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002791 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002792 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002793 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002794 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002795 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002796 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002797 msg_puts_attr(IObuff, qfLineAttr);
2798 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 if (qfp->qf_pattern != NULL)
2800 {
2801 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002802 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002803 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002804 }
2805 msg_puts((char_u *)" ");
2806
2807 /* Remove newlines and leading whitespace from the text. For an
2808 * unrecognized line keep the indent, the compiler may mark a word
2809 * with ^^^^. */
2810 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2812 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813 msg_prt_line(IObuff, FALSE);
2814 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816
2817 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002818 if (qfp == NULL)
2819 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002820 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 ui_breakcheck();
2822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823}
2824
2825/*
2826 * Remove newlines and leading whitespace from an error message.
2827 * Put the result in "buf[bufsize]".
2828 */
2829 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002830qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
2832 int i;
2833 char_u *p = text;
2834
2835 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2836 {
2837 if (*p == '\n')
2838 {
2839 buf[i] = ' ';
2840 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002841 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 break;
2843 }
2844 else
2845 buf[i] = *p++;
2846 }
2847 buf[i] = NUL;
2848}
2849
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002850 static void
2851qf_msg(qf_info_T *qi, int which, char *lead)
2852{
2853 char *title = (char *)qi->qf_lists[which].qf_title;
2854 int count = qi->qf_lists[which].qf_count;
2855 char_u buf[IOSIZE];
2856
2857 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2858 lead,
2859 which + 1,
2860 qi->qf_listcount,
2861 count);
2862
2863 if (title != NULL)
2864 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002865 size_t len = STRLEN(buf);
2866
2867 if (len < 34)
2868 {
2869 vim_memset(buf + len, ' ', 34 - len);
2870 buf[34] = NUL;
2871 }
2872 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002873 }
2874 trunc_string(buf, buf, Columns - 1, IOSIZE);
2875 msg(buf);
2876}
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878/*
2879 * ":colder [count]": Up in the quickfix stack.
2880 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002881 * ":lolder [count]": Up in the location list stack.
2882 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 */
2884 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002885qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002887 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 int count;
2889
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2891 {
2892 qi = GET_LOC_LIST(curwin);
2893 if (qi == NULL)
2894 {
2895 EMSG(_(e_loclist));
2896 return;
2897 }
2898 }
2899
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 if (eap->addr_count != 0)
2901 count = eap->line2;
2902 else
2903 count = 1;
2904 while (count--)
2905 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002906 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002908 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
2910 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002911 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002913 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 else
2916 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002917 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
2919 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002920 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002922 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002925 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002926 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927}
2928
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002929 void
2930qf_history(exarg_T *eap)
2931{
2932 qf_info_T *qi = &ql_info;
2933 int i;
2934
2935 if (eap->cmdidx == CMD_lhistory)
2936 qi = GET_LOC_LIST(curwin);
2937 if (qi == NULL || (qi->qf_listcount == 0
2938 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2939 MSG(_("No entries"));
2940 else
2941 for (i = 0; i < qi->qf_listcount; ++i)
2942 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2943}
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002946 * Free all the entries in the error list "idx". Note that other information
2947 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 */
2949 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002950qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002952 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002953 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002954 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002955 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002957 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002959 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002960 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002961 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002962 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002963 vim_free(qfp->qf_text);
2964 stop = (qfp == qfpnext);
2965 vim_free(qfp->qf_pattern);
2966 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002967 if (stop)
2968 /* Somehow qf_count may have an incorrect value, set it to 1
2969 * to avoid crashing when it's wrong.
2970 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002971 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002972 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002973 qfl->qf_start = qfpnext;
2974 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002976
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002977 qfl->qf_index = 0;
2978 qfl->qf_start = NULL;
2979 qfl->qf_last = NULL;
2980 qfl->qf_ptr = NULL;
2981 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002982
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002983 qf_clean_dir_stack(&qfl->qf_dir_stack);
2984 qfl->qf_directory = NULL;
2985 qf_clean_dir_stack(&qfl->qf_file_stack);
2986 qfl->qf_currfile = NULL;
2987 qfl->qf_multiline = FALSE;
2988 qfl->qf_multiignore = FALSE;
2989 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990}
2991
2992/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002993 * Free error list "idx". Frees all the entries in the quickfix list,
2994 * associated context information and the title.
2995 */
2996 static void
2997qf_free(qf_info_T *qi, int idx)
2998{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002999 qf_list_T *qfl = &qi->qf_lists[idx];
3000
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003001 qf_free_items(qi, idx);
3002
Bram Moolenaard23a8232018-02-10 18:45:26 +01003003 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003004 free_tv(qfl->qf_ctx);
3005 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003006 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003007 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003008}
3009
3010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 * qf_mark_adjust: adjust marks
3012 */
3013 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003014qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003015 win_T *wp,
3016 linenr_T line1,
3017 linenr_T line2,
3018 long amount,
3019 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003021 int i;
3022 qfline_T *qfp;
3023 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003024 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003025 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003026 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaarc1542742016-07-20 21:44:37 +02003028 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003029 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003030 if (wp != NULL)
3031 {
3032 if (wp->w_llist == NULL)
3033 return;
3034 qi = wp->w_llist;
3035 }
3036
3037 for (idx = 0; idx < qi->qf_listcount; ++idx)
3038 if (qi->qf_lists[idx].qf_count)
3039 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003040 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3041 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 if (qfp->qf_fnum == curbuf->b_fnum)
3043 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003044 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3046 {
3047 if (amount == MAXLNUM)
3048 qfp->qf_cleared = TRUE;
3049 else
3050 qfp->qf_lnum += amount;
3051 }
3052 else if (amount_after && qfp->qf_lnum > line2)
3053 qfp->qf_lnum += amount_after;
3054 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003055
3056 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003057 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058}
3059
3060/*
3061 * Make a nice message out of the error character and the error number:
3062 * char number message
3063 * e or E 0 " error"
3064 * w or W 0 " warning"
3065 * i or I 0 " info"
3066 * 0 0 ""
3067 * other 0 " c"
3068 * e or E n " error n"
3069 * w or W n " warning n"
3070 * i or I n " info n"
3071 * 0 n " error n"
3072 * other n " c n"
3073 * 1 x "" :helpgrep
3074 */
3075 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003076qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 static char_u buf[20];
3079 static char_u cc[3];
3080 char_u *p;
3081
3082 if (c == 'W' || c == 'w')
3083 p = (char_u *)" warning";
3084 else if (c == 'I' || c == 'i')
3085 p = (char_u *)" info";
3086 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3087 p = (char_u *)" error";
3088 else if (c == 0 || c == 1)
3089 p = (char_u *)"";
3090 else
3091 {
3092 cc[0] = ' ';
3093 cc[1] = c;
3094 cc[2] = NUL;
3095 p = cc;
3096 }
3097
3098 if (nr <= 0)
3099 return p;
3100
3101 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3102 return buf;
3103}
3104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105/*
3106 * ":cwindow": open the quickfix window if we have errors to display,
3107 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003108 * ":lwindow": open the location list window if we have locations to display,
3109 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 */
3111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003112ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003114 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 win_T *win;
3116
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003117 if (eap->cmdidx == CMD_lwindow)
3118 {
3119 qi = GET_LOC_LIST(curwin);
3120 if (qi == NULL)
3121 return;
3122 }
3123
3124 /* Look for an existing quickfix window. */
3125 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127 /*
3128 * If a quickfix window is open but we have no errors to display,
3129 * close the window. If a quickfix window is not open, then open
3130 * it if we have errors; otherwise, leave it closed.
3131 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003132 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003133 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003134 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 {
3136 if (win != NULL)
3137 ex_cclose(eap);
3138 }
3139 else if (win == NULL)
3140 ex_copen(eap);
3141}
3142
3143/*
3144 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003145 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003148ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003150 win_T *win = NULL;
3151 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003153 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3154 {
3155 qi = GET_LOC_LIST(curwin);
3156 if (qi == NULL)
3157 return;
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003160 /* Find existing quickfix window and close it. */
3161 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (win != NULL)
3163 win_close(win, FALSE);
3164}
3165
3166/*
3167 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003168 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 */
3170 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003171ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003173 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003176 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003177 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003178 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003180 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3181 {
3182 qi = GET_LOC_LIST(curwin);
3183 if (qi == NULL)
3184 {
3185 EMSG(_(e_loclist));
3186 return;
3187 }
3188 }
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (eap->addr_count != 0)
3191 height = eap->line2;
3192 else
3193 height = QF_WINHEIGHT;
3194
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#ifdef FEAT_GUI
3197 need_mouse_correct = TRUE;
3198#endif
3199
3200 /*
3201 * Find existing quickfix window, or open a new one.
3202 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003203 win = qf_find_win(qi);
3204
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003205 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003208 if (eap->addr_count != 0)
3209 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003210 if (cmdmod.split & WSP_VERT)
3211 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003212 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003213 win_setwidth(height);
3214 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003215 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003216 win_setheight(height);
3217 }
3218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 else
3220 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003221 int flags = 0;
3222
Bram Moolenaar9c102382006-05-03 21:26:49 +00003223 qf_buf = qf_find_buf(qi);
3224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 /* The current window becomes the previous window afterwards. */
3226 win = curwin;
3227
Bram Moolenaar77642c02012-11-20 17:55:10 +01003228 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3229 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003230 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003231 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003232 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003233 /* Default is to open the window below the current window */
3234 if (cmdmod.split == 0)
3235 flags = WSP_BELOW;
3236 flags |= WSP_NEWLOC;
3237 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003239 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003241 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003243 /*
3244 * For the location list window, create a reference to the
3245 * location list from the window 'win'.
3246 */
3247 curwin->w_llist_ref = win->w_llist;
3248 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003250
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003251 if (oldwin != curwin)
3252 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003253 if (qf_buf != NULL)
3254 /* Use the existing quickfix buffer */
3255 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003256 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003257 else
3258 {
3259 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003260 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003261 /* switch off 'swapfile' */
3262 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3263 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003264 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003265 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003266 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003267#ifdef FEAT_DIFF
3268 curwin->w_p_diff = FALSE;
3269#endif
3270#ifdef FEAT_FOLDING
3271 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3272 OPT_LOCAL);
3273#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003276 /* Only set the height when still in the same tab page and there is no
3277 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003278 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 win_setheight(height);
3280 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3281 if (win_valid(win))
3282 prevwin = win;
3283 }
3284
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003285 qf_set_title_var(qi);
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 /*
3288 * Fill the buffer with the quickfix list.
3289 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003290 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003292 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 curwin->w_cursor.col = 0;
3294 check_cursor();
3295 update_topline(); /* scroll to show the line */
3296}
3297
3298/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003299 * Move the cursor in the quickfix window to "lnum".
3300 */
3301 static void
3302qf_win_goto(win_T *win, linenr_T lnum)
3303{
3304 win_T *old_curwin = curwin;
3305
3306 curwin = win;
3307 curbuf = win->w_buffer;
3308 curwin->w_cursor.lnum = lnum;
3309 curwin->w_cursor.col = 0;
3310#ifdef FEAT_VIRTUALEDIT
3311 curwin->w_cursor.coladd = 0;
3312#endif
3313 curwin->w_curswant = 0;
3314 update_topline(); /* scroll to show the line */
3315 redraw_later(VALID);
3316 curwin->w_redr_status = TRUE; /* update ruler */
3317 curwin = old_curwin;
3318 curbuf = curwin->w_buffer;
3319}
3320
3321/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003322 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003323 */
3324 void
3325ex_cbottom(exarg_T *eap UNUSED)
3326{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003327 qf_info_T *qi = &ql_info;
3328 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003329
Bram Moolenaar537ef082016-07-09 17:56:19 +02003330 if (eap->cmdidx == CMD_lbottom)
3331 {
3332 qi = GET_LOC_LIST(curwin);
3333 if (qi == NULL)
3334 {
3335 EMSG(_(e_loclist));
3336 return;
3337 }
3338 }
3339
3340 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003341 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3342 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3343}
3344
3345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 * Return the number of the current entry (line number in the quickfix
3347 * window).
3348 */
3349 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003350qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003352 qf_info_T *qi = &ql_info;
3353
3354 if (IS_LL_WINDOW(wp))
3355 /* In the location list window, use the referenced location list */
3356 qi = wp->w_llist_ref;
3357
3358 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359}
3360
3361/*
3362 * Update the cursor position in the quickfix window to the current error.
3363 * Return TRUE if there is a quickfix window.
3364 */
3365 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003366qf_win_pos_update(
3367 qf_info_T *qi,
3368 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003371 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372
3373 /*
3374 * Put the cursor on the current error in the quickfix window, so that
3375 * it's viewable.
3376 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003377 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (win != NULL
3379 && qf_index <= win->w_buffer->b_ml.ml_line_count
3380 && old_qf_index != qf_index)
3381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (qf_index > old_qf_index)
3383 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003384 win->w_redraw_top = old_qf_index;
3385 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387 else
3388 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003389 win->w_redraw_top = qf_index;
3390 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003392 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 }
3394 return win != NULL;
3395}
3396
3397/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003398 * Check whether the given window is displaying the specified quickfix/location
3399 * list buffer
3400 */
3401 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003402is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003403{
3404 /*
3405 * A window displaying the quickfix buffer will have the w_llist_ref field
3406 * set to NULL.
3407 * A window displaying a location list buffer will have the w_llist_ref
3408 * pointing to the location list.
3409 */
3410 if (bt_quickfix(win->w_buffer))
3411 if ((qi == &ql_info && win->w_llist_ref == NULL)
3412 || (qi != &ql_info && win->w_llist_ref == qi))
3413 return TRUE;
3414
3415 return FALSE;
3416}
3417
3418/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003419 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003420 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003421 */
3422 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003423qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003424{
3425 win_T *win;
3426
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003427 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003428 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003429 return win;
3430 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003431}
3432
3433/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003434 * Find a quickfix buffer.
3435 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 */
3437 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003438qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003440 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003441 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar9c102382006-05-03 21:26:49 +00003443 FOR_ALL_TAB_WINDOWS(tp, win)
3444 if (is_qf_win(win, qi))
3445 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003446
Bram Moolenaar9c102382006-05-03 21:26:49 +00003447 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448}
3449
3450/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003451 * Update the w:quickfix_title variable in the quickfix/location list window
3452 */
3453 static void
3454qf_update_win_titlevar(qf_info_T *qi)
3455{
3456 win_T *win;
3457 win_T *curwin_save;
3458
3459 if ((win = qf_find_win(qi)) != NULL)
3460 {
3461 curwin_save = curwin;
3462 curwin = win;
3463 qf_set_title_var(qi);
3464 curwin = curwin_save;
3465 }
3466}
3467
3468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 * Find the quickfix buffer. If it exists, update the contents.
3470 */
3471 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003472qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
3474 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003475 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
3478 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (buf != NULL)
3481 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003482 linenr_T old_line_count = buf->b_ml.ml_line_count;
3483
3484 if (old_last == NULL)
3485 /* set curwin/curbuf to buf and save a few things */
3486 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
Bram Moolenaard823fa92016-08-12 16:29:27 +02003488 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003489
Bram Moolenaar864293a2016-06-02 13:40:04 +02003490 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003491 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003492
Bram Moolenaar864293a2016-06-02 13:40:04 +02003493 if (old_last == NULL)
3494 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003495 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003496
3497 /* restore curwin/curbuf and a few other things */
3498 aucmd_restbuf(&aco);
3499 }
3500
3501 /* Only redraw when added lines are visible. This avoids flickering
3502 * when the added lines are not visible. */
3503 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3504 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506}
3507
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003508/*
3509 * Set "w:quickfix_title" if "qi" has a title.
3510 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003511 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003512qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003513{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003514 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3515 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003516 qi->qf_lists[qi->qf_curlist].qf_title);
3517}
3518
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519/*
3520 * Fill current buffer with quickfix errors, replacing any previous contents.
3521 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003522 * If "old_last" is not NULL append the items after this one.
3523 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3524 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 */
3526 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003527qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003529 linenr_T lnum;
3530 qfline_T *qfp;
3531 buf_T *errbuf;
3532 int len;
3533 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaar864293a2016-06-02 13:40:04 +02003535 if (old_last == NULL)
3536 {
3537 if (buf != curbuf)
3538 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003539 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003540 return;
3541 }
3542
3543 /* delete all existing lines */
3544 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3545 (void)ml_delete((linenr_T)1, FALSE);
3546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
3548 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003549 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
3551 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003552 if (old_last == NULL)
3553 {
3554 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3555 lnum = 0;
3556 }
3557 else
3558 {
3559 qfp = old_last->qf_next;
3560 lnum = buf->b_ml.ml_line_count;
3561 }
3562 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
3564 if (qfp->qf_fnum != 0
3565 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3566 && errbuf->b_fname != NULL)
3567 {
3568 if (qfp->qf_type == 1) /* :helpgrep */
3569 STRCPY(IObuff, gettail(errbuf->b_fname));
3570 else
3571 STRCPY(IObuff, errbuf->b_fname);
3572 len = (int)STRLEN(IObuff);
3573 }
3574 else
3575 len = 0;
3576 IObuff[len++] = '|';
3577
3578 if (qfp->qf_lnum > 0)
3579 {
3580 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3581 len += (int)STRLEN(IObuff + len);
3582
3583 if (qfp->qf_col > 0)
3584 {
3585 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3586 len += (int)STRLEN(IObuff + len);
3587 }
3588
3589 sprintf((char *)IObuff + len, "%s",
3590 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3591 len += (int)STRLEN(IObuff + len);
3592 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003593 else if (qfp->qf_pattern != NULL)
3594 {
3595 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3596 len += (int)STRLEN(IObuff + len);
3597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 IObuff[len++] = '|';
3599 IObuff[len++] = ' ';
3600
3601 /* Remove newlines and leading whitespace from the text.
3602 * For an unrecognized line keep the indent, the compiler may
3603 * mark a word with ^^^^. */
3604 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3605 IObuff + len, IOSIZE - len);
3606
Bram Moolenaar864293a2016-06-02 13:40:04 +02003607 if (ml_append_buf(buf, lnum, IObuff,
3608 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003610 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003612 if (qfp == NULL)
3613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003615
3616 if (old_last == NULL)
3617 /* Delete the empty line which is now at the end */
3618 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
3620
3621 /* correct cursor position */
3622 check_lnums(TRUE);
3623
Bram Moolenaar864293a2016-06-02 13:40:04 +02003624 if (old_last == NULL)
3625 {
3626 /* Set the 'filetype' to "qf" each time after filling the buffer.
3627 * This resembles reading a file into a buffer, it's more logical when
3628 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003629#ifdef FEAT_AUTOCMD
3630 ++curbuf_lock;
3631#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003632 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3633 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
3635#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003636 keep_filetype = TRUE; /* don't detect 'filetype' */
3637 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003639 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003641 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003642 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003644 /* make sure it will be redrawn */
3645 redraw_curbuf_later(NOT_VALID);
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 /* Restore KeyTyped, setting 'filetype' may reset it. */
3649 KeyTyped = old_KeyTyped;
3650}
3651
Bram Moolenaarb254af32017-12-18 19:48:58 +01003652 static void
3653qf_list_changed(qf_info_T *qi, int qf_idx)
3654{
3655 qi->qf_lists[qf_idx].qf_changedtick++;
3656}
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003659 * Return TRUE when using ":vimgrep" for ":grep".
3660 */
3661 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003662grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003663{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003664 return ((cmdidx == CMD_grep
3665 || cmdidx == CMD_lgrep
3666 || cmdidx == CMD_grepadd
3667 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003668 && STRCMP("internal",
3669 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3670}
3671
3672/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003673 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
3675 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003676ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003678 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003680 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003682 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003683 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003684 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003685#ifdef FEAT_AUTOCMD
3686 char_u *au_name = NULL;
3687
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003688 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3689 if (grep_internal(eap->cmdidx))
3690 {
3691 ex_vimgrep(eap);
3692 return;
3693 }
3694
Bram Moolenaar7c626922005-02-07 22:01:03 +00003695 switch (eap->cmdidx)
3696 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003697 case CMD_make: au_name = (char_u *)"make"; break;
3698 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3699 case CMD_grep: au_name = (char_u *)"grep"; break;
3700 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3701 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3702 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003703 default: break;
3704 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003705 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3706 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003707 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003708# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003709 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003710 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003711# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003712 }
3713#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003714#ifdef FEAT_MBYTE
3715 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaara6557602006-02-04 22:43:20 +00003718 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3719 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003720 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003723 fname = get_mef_name();
3724 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003726 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727
3728 /*
3729 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3730 */
3731 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3732 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003733 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 cmd = alloc(len);
3735 if (cmd == NULL)
3736 return;
3737 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3738 (char *)p_shq);
3739 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003740 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 /*
3742 * Output a newline if there's something else than the :make command that
3743 * was typed (in which case the cursor is in column 0).
3744 */
3745 if (msg_col == 0)
3746 msg_didout = FALSE;
3747 msg_start();
3748 MSG_PUTS(":!");
3749 msg_outtrans(cmd); /* show what we are doing */
3750
3751 /* let the shell know if we are redirecting output or not */
3752 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3753
3754#ifdef AMIGA
3755 out_flush();
3756 /* read window status report and redraw before message */
3757 (void)char_avail();
3758#endif
3759
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003760 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003761 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3762 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003763 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003764 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003765 if (wp != NULL)
3766 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003767 if (res >= 0 && qi != NULL)
3768 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003769#ifdef FEAT_AUTOCMD
3770 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003771 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003772 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3773 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01003774 if (qi != NULL && qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003775 res = qi->qf_lists[qi->qf_curlist].qf_count;
3776 else
3777 res = 0;
3778 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003779#endif
3780 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003781 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
Bram Moolenaar7c626922005-02-07 22:01:03 +00003783 mch_remove(fname);
3784 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 vim_free(cmd);
3786}
3787
3788/*
3789 * Return the name for the errorfile, in allocated memory.
3790 * Find a new unique name when 'makeef' contains "##".
3791 * Returns NULL for error.
3792 */
3793 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003794get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
3796 char_u *p;
3797 char_u *name;
3798 static int start = -1;
3799 static int off = 0;
3800#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003801 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802#endif
3803
3804 if (*p_mef == NUL)
3805 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003806 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (name == NULL)
3808 EMSG(_(e_notmp));
3809 return name;
3810 }
3811
3812 for (p = p_mef; *p; ++p)
3813 if (p[0] == '#' && p[1] == '#')
3814 break;
3815
3816 if (*p == NUL)
3817 return vim_strsave(p_mef);
3818
3819 /* Keep trying until the name doesn't exist yet. */
3820 for (;;)
3821 {
3822 if (start == -1)
3823 start = mch_get_pid();
3824 else
3825 off += 19;
3826
3827 name = alloc((unsigned)STRLEN(p_mef) + 30);
3828 if (name == NULL)
3829 break;
3830 STRCPY(name, p_mef);
3831 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3832 STRCAT(name, p + 2);
3833 if (mch_getperm(name) < 0
3834#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003835 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 && mch_lstat((char *)name, &sb) < 0
3837#endif
3838 )
3839 break;
3840 vim_free(name);
3841 }
3842 return name;
3843}
3844
3845/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003846 * Returns the number of valid entries in the current quickfix/location list.
3847 */
3848 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003849qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003850{
3851 qf_info_T *qi = &ql_info;
3852 qfline_T *qfp;
3853 int i, sz = 0;
3854 int prev_fnum = 0;
3855
3856 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3857 {
3858 /* Location list */
3859 qi = GET_LOC_LIST(curwin);
3860 if (qi == NULL)
3861 return 0;
3862 }
3863
3864 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003865 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003866 ++i, qfp = qfp->qf_next)
3867 {
3868 if (qfp->qf_valid)
3869 {
3870 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3871 sz++; /* Count all valid entries */
3872 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3873 {
3874 /* Count the number of files */
3875 sz++;
3876 prev_fnum = qfp->qf_fnum;
3877 }
3878 }
3879 }
3880
3881 return sz;
3882}
3883
3884/*
3885 * Returns the current index of the quickfix/location list.
3886 * Returns 0 if there is an error.
3887 */
3888 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003889qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003890{
3891 qf_info_T *qi = &ql_info;
3892
3893 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3894 {
3895 /* Location list */
3896 qi = GET_LOC_LIST(curwin);
3897 if (qi == NULL)
3898 return 0;
3899 }
3900
3901 return qi->qf_lists[qi->qf_curlist].qf_index;
3902}
3903
3904/*
3905 * Returns the current index in the quickfix/location list (counting only valid
3906 * entries). If no valid entries are in the list, then returns 1.
3907 */
3908 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003909qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003910{
3911 qf_info_T *qi = &ql_info;
3912 qf_list_T *qfl;
3913 qfline_T *qfp;
3914 int i, eidx = 0;
3915 int prev_fnum = 0;
3916
3917 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3918 {
3919 /* Location list */
3920 qi = GET_LOC_LIST(curwin);
3921 if (qi == NULL)
3922 return 1;
3923 }
3924
3925 qfl = &qi->qf_lists[qi->qf_curlist];
3926 qfp = qfl->qf_start;
3927
3928 /* check if the list has valid errors */
3929 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3930 return 1;
3931
3932 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3933 {
3934 if (qfp->qf_valid)
3935 {
3936 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3937 {
3938 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3939 {
3940 /* Count the number of files */
3941 eidx++;
3942 prev_fnum = qfp->qf_fnum;
3943 }
3944 }
3945 else
3946 eidx++;
3947 }
3948 }
3949
3950 return eidx ? eidx : 1;
3951}
3952
3953/*
3954 * Get the 'n'th valid error entry in the quickfix or location list.
3955 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3956 * For :cdo and :ldo returns the 'n'th valid error entry.
3957 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3958 */
3959 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003960qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003961{
3962 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3963 qfline_T *qfp = qfl->qf_start;
3964 int i, eidx;
3965 int prev_fnum = 0;
3966
3967 /* check if the list has valid errors */
3968 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3969 return 1;
3970
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003971 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003972 i++, qfp = qfp->qf_next)
3973 {
3974 if (qfp->qf_valid)
3975 {
3976 if (fdo)
3977 {
3978 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3979 {
3980 /* Count the number of files */
3981 eidx++;
3982 prev_fnum = qfp->qf_fnum;
3983 }
3984 }
3985 else
3986 eidx++;
3987 }
3988
3989 if (eidx == n)
3990 break;
3991 }
3992
3993 if (i <= qfl->qf_count)
3994 return i;
3995 else
3996 return 1;
3997}
3998
3999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004001 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004002 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 */
4004 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004005ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004007 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004008 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004009
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004010 if (eap->cmdidx == CMD_ll
4011 || eap->cmdidx == CMD_lrewind
4012 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004013 || eap->cmdidx == CMD_llast
4014 || eap->cmdidx == CMD_ldo
4015 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004016 {
4017 qi = GET_LOC_LIST(curwin);
4018 if (qi == NULL)
4019 {
4020 EMSG(_(e_loclist));
4021 return;
4022 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004023 }
4024
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004025 if (eap->addr_count > 0)
4026 errornr = (int)eap->line2;
4027 else
4028 {
4029 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4030 errornr = 0;
4031 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4032 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4033 errornr = 1;
4034 else
4035 errornr = 32767;
4036 }
4037
4038 /* For cdo and ldo commands, jump to the nth valid error.
4039 * For cfdo and lfdo commands, jump to the nth valid file entry.
4040 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004041 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4042 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004043 errornr = qf_get_nth_valid_entry(qi,
4044 eap->addr_count > 0 ? (int)eap->line1 : 1,
4045 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4046
4047 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048}
4049
4050/*
4051 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004052 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004053 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 */
4055 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004056ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004058 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004059 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004060
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004061 if (eap->cmdidx == CMD_lnext
4062 || eap->cmdidx == CMD_lNext
4063 || eap->cmdidx == CMD_lprevious
4064 || eap->cmdidx == CMD_lnfile
4065 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004066 || eap->cmdidx == CMD_lpfile
4067 || eap->cmdidx == CMD_ldo
4068 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004069 {
4070 qi = GET_LOC_LIST(curwin);
4071 if (qi == NULL)
4072 {
4073 EMSG(_(e_loclist));
4074 return;
4075 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004076 }
4077
Bram Moolenaar55b69262017-08-13 13:42:01 +02004078 if (eap->addr_count > 0
4079 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4080 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004081 errornr = (int)eap->line2;
4082 else
4083 errornr = 1;
4084
4085 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4086 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004088 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4089 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004091 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4092 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 ? BACKWARD_FILE
4094 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004095 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096}
4097
4098/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004099 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004100 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 */
4102 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004103ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004105 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004106 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004107 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004108#ifdef FEAT_AUTOCMD
4109 char_u *au_name = NULL;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004110 int save_qfid;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004111#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004112 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004113
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004114#ifdef FEAT_AUTOCMD
4115 switch (eap->cmdidx)
4116 {
4117 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4118 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4119 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4120 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4121 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4122 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4123 default: break;
4124 }
4125 if (au_name != NULL)
4126 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
4127#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004128#ifdef FEAT_MBYTE
4129 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4130#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004131#ifdef FEAT_BROWSE
4132 if (cmdmod.browse)
4133 {
4134 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4135 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4136 if (browse_file == NULL)
4137 return;
4138 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4139 vim_free(browse_file);
4140 }
4141 else
4142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004144 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004145
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004146 if (eap->cmdidx == CMD_lfile
4147 || eap->cmdidx == CMD_lgetfile
4148 || eap->cmdidx == CMD_laddfile)
4149 wp = curwin;
4150
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004151 /*
4152 * This function is used by the :cfile, :cgetfile and :caddfile
4153 * commands.
4154 * :cfile always creates a new quickfix list and jumps to the
4155 * first error.
4156 * :cgetfile creates a new quickfix list but doesn't jump to the
4157 * first error.
4158 * :caddfile adds to an existing quickfix list. If there is no
4159 * quickfix list then a new list is created.
4160 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004161 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4162 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004163 if (wp != NULL)
4164 qi = GET_LOC_LIST(wp);
4165 if (res >= 0 && qi != NULL)
4166 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004167#ifdef FEAT_AUTOCMD
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004168 if (qi != NULL)
4169 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004170 if (au_name != NULL)
4171 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004172
4173 /* An autocmd might have freed the quickfix/location list. Check whether it
4174 * is still valid. */
4175 if (qi != NULL && !qflist_valid(wp, save_qfid))
Bram Moolenaar3c097222017-12-21 20:54:49 +01004176 return;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004177#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004178 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004179 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180}
4181
4182/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004183 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004184 * ":vimgrepadd {pattern} file(s)"
4185 * ":lvimgrep {pattern} file(s)"
4186 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004187 */
4188 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004189ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004190{
Bram Moolenaar81695252004-12-29 20:58:21 +00004191 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004192 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004193 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004194 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004195 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004196 char_u *s;
4197 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004198 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004199 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004200 int loclist_cmd = FALSE;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004201#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004202 int_u save_qfid;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004203 qfline_T *cur_qf_start;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004204 win_T *wp;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004205#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004206 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004207 buf_T *buf;
4208 int duplicate_name = FALSE;
4209 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004210 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004211 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004212 buf_T *first_match_buf = NULL;
4213 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004214 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004215#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4216 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004217#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004218 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004219 int flags = 0;
4220 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004221 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004222 char_u *dirname_start = NULL;
4223 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004224 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004225#ifdef FEAT_AUTOCMD
4226 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004227
4228 switch (eap->cmdidx)
4229 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004230 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4231 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4232 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004233 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004234 case CMD_grep: au_name = (char_u *)"grep"; break;
4235 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4236 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4237 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004238 default: break;
4239 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004240 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4241 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004242 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004243# ifdef FEAT_EVAL
4244 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004245 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004246# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004247 }
4248#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004249
Bram Moolenaar754b5602006-02-09 23:53:20 +00004250 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004251 || eap->cmdidx == CMD_lvimgrep
4252 || eap->cmdidx == CMD_lgrepadd
4253 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004254 {
4255 qi = ll_get_or_alloc_list(curwin);
4256 if (qi == NULL)
4257 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004258 loclist_cmd = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00004259 }
4260
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004261 if (eap->addr_count > 0)
4262 tomatch = eap->line2;
4263 else
4264 tomatch = MAXLNUM;
4265
Bram Moolenaar81695252004-12-29 20:58:21 +00004266 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004267 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004268 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004269 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004270 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004271 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004272 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004273 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004274 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004275
4276 if (s != NULL && *s == NUL)
4277 {
4278 /* Pattern is empty, use last search pattern. */
4279 if (last_search_pat() == NULL)
4280 {
4281 EMSG(_(e_noprevre));
4282 goto theend;
4283 }
4284 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4285 }
4286 else
4287 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4288
Bram Moolenaar86b68352004-12-27 21:59:20 +00004289 if (regmatch.regprog == NULL)
4290 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004291 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004292 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004293
4294 p = skipwhite(p);
4295 if (*p == NUL)
4296 {
4297 EMSG(_("E683: File name missing or invalid pattern"));
4298 goto theend;
4299 }
4300
Bram Moolenaar55b69262017-08-13 13:42:01 +02004301 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4302 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004303 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004304 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004305 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004306
4307 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004308 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004309 goto theend;
4310 if (fcount == 0)
4311 {
4312 EMSG(_(e_nomatch));
4313 goto theend;
4314 }
4315
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004316 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4317 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004318 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004319 {
4320 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004321 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004322 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004323
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004324 /* Remember the current directory, because a BufRead autocommand that does
4325 * ":lcd %:p:h" changes the meaning of short path names. */
4326 mch_dirname(dirname_start, MAXPATHL);
4327
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004328#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004329 /* Remember the current values of the quickfix list and qf_start, so that
4330 * we can check for autocommands changing the current quickfix list. */
4331 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004332 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4333#endif
4334
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004335 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004336 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004337 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004338 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004339 if (time(NULL) > seconds)
4340 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004341 /* Display the file name every second or so, show the user we are
4342 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004343 seconds = time(NULL);
4344 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004345 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004346 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004347 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004348 else
4349 {
4350 msg_outtrans(p);
4351 vim_free(p);
4352 }
4353 msg_clr_eos();
4354 msg_didout = FALSE; /* overwrite this message */
4355 msg_nowait = TRUE; /* don't wait for this message */
4356 msg_col = 0;
4357 out_flush();
4358 }
4359
Bram Moolenaar81695252004-12-29 20:58:21 +00004360 buf = buflist_findname_exp(fnames[fi]);
4361 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4362 {
4363 /* Remember that a buffer with this name already exists. */
4364 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004365 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004366 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004367
4368#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4369 /* Don't do Filetype autocommands to avoid loading syntax and
4370 * indent scripts, a great speed improvement. */
4371 save_ei = au_event_disable(",Filetype");
4372#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004373 /* Don't use modelines here, it's useless. */
4374 save_mls = p_mls;
4375 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004376
4377 /* Load file into a buffer, so that 'fileencoding' is detected,
4378 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004379 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004380
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004381 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004382#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4383 au_event_restore(save_ei);
4384#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004385 }
4386 else
4387 /* Use existing, loaded buffer. */
4388 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004389
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004390#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004391 if (loclist_cmd)
4392 {
4393 /*
4394 * Verify that the location list is still valid. An autocmd might
4395 * have freed the location list.
4396 */
4397 if (!qflist_valid(curwin, save_qfid))
4398 {
4399 EMSG(_(e_loc_list_changed));
4400 goto theend;
4401 }
4402 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004403 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4404 {
4405 int idx;
4406
4407 /* Autocommands changed the quickfix list. Find the one we were
4408 * using and restore it. */
4409 for (idx = 0; idx < LISTCOUNT; ++idx)
4410 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4411 {
4412 qi->qf_curlist = idx;
4413 break;
4414 }
4415 if (idx == LISTCOUNT)
4416 {
4417 /* List cannot be found, create a new one. */
4418 qf_new_list(qi, *eap->cmdlinep);
4419 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4420 }
4421 }
4422#endif
4423
Bram Moolenaar81695252004-12-29 20:58:21 +00004424 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004425 {
4426 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004427 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004428 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004429 else
4430 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004431 /* Try for a match in all lines of the buffer.
4432 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004433 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004434 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4435 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004436 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004437 col = 0;
4438 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004439 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004440 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004441 /* Pass the buffer number so that it gets used even for a
4442 * dummy buffer, unless duplicate_name is set, then the
4443 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004444 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004445 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004446 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004447 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004448 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004449 ml_get_buf(buf,
4450 regmatch.startpos[0].lnum + lnum, FALSE),
4451 regmatch.startpos[0].lnum + lnum,
4452 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004453 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004454 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004455 0, /* nr */
4456 0, /* type */
4457 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004458 ) == FAIL)
4459 {
4460 got_int = TRUE;
4461 break;
4462 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004463 found_match = TRUE;
4464 if (--tomatch == 0)
4465 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004466 if ((flags & VGR_GLOBAL) == 0
4467 || regmatch.endpos[0].lnum > 0)
4468 break;
4469 col = regmatch.endpos[0].col
4470 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004471 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004472 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004473 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004474 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004475 if (got_int)
4476 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004477 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004478#ifdef FEAT_AUTOCMD
4479 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4480#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004481
4482 if (using_dummy)
4483 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004484 if (found_match && first_match_buf == NULL)
4485 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004486 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004487 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004488 /* Never keep a dummy buffer if there is another buffer
4489 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004490 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004491 buf = NULL;
4492 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004493 else if (!cmdmod.hide
4494 || buf->b_p_bh[0] == 'u' /* "unload" */
4495 || buf->b_p_bh[0] == 'w' /* "wipe" */
4496 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004497 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004498 /* When no match was found we don't need to remember the
4499 * buffer, wipe it out. If there was a match and it
4500 * wasn't the first one or we won't jump there: only
4501 * unload the buffer.
4502 * Ignore 'hidden' here, because it may lead to having too
4503 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004504 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004505 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004506 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004507 buf = NULL;
4508 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004509 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004510 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004511 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004512 /* Keeping the buffer, remove the dummy flag. */
4513 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004514 buf = NULL;
4515 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004516 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004517
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004518 if (buf != NULL)
4519 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004520 /* Keeping the buffer, remove the dummy flag. */
4521 buf->b_flags &= ~BF_DUMMY;
4522
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004523 /* If the buffer is still loaded we need to use the
4524 * directory we jumped to below. */
4525 if (buf == first_match_buf
4526 && target_dir == NULL
4527 && STRCMP(dirname_start, dirname_now) != 0)
4528 target_dir = vim_strsave(dirname_now);
4529
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004530 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004531 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004532 * need to be done (again). But not the window-local
4533 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004534 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004535#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004536 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4537 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004538#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004539 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004540 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004541 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004542 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004543 }
4544 }
4545
4546 FreeWild(fcount, fnames);
4547
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004548 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4549 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4550 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004551 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004552
Bram Moolenaar864293a2016-06-02 13:40:04 +02004553 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004554
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004555#ifdef FEAT_AUTOCMD
4556 if (au_name != NULL)
4557 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4558 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004559 /*
4560 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4561 * is still valid.
4562 */
4563 wp = loclist_cmd ? curwin : NULL;
4564 if (!qflist_valid(wp, save_qfid))
4565 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004566#endif
4567
Bram Moolenaar86b68352004-12-27 21:59:20 +00004568 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004569 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004570 {
4571 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004572 {
4573 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004574 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004575 if (buf != curbuf)
4576 /* If we jumped to another buffer redrawing will already be
4577 * taken care of. */
4578 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004579
4580 /* Jump to the directory used after loading the buffer. */
4581 if (curbuf == first_match_buf && target_dir != NULL)
4582 {
4583 exarg_T ea;
4584
4585 ea.arg = target_dir;
4586 ea.cmdidx = CMD_lcd;
4587 ex_cd(&ea);
4588 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004589 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004590 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004591 else
4592 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004593
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004594 /* If we loaded a dummy buffer into the current window, the autocommands
4595 * may have messed up things, need to redraw and recompute folds. */
4596 if (redraw_for_dummy)
4597 {
4598#ifdef FEAT_FOLDING
4599 foldUpdateAll(curwin);
4600#else
4601 redraw_later(NOT_VALID);
4602#endif
4603 }
4604
Bram Moolenaar86b68352004-12-27 21:59:20 +00004605theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004606 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004607 vim_free(dirname_now);
4608 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004609 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004610 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004611}
4612
4613/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004614 * Restore current working directory to "dirname_start" if they differ, taking
4615 * into account whether it is set locally or globally.
4616 */
4617 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004618restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004619{
4620 char_u *dirname_now = alloc(MAXPATHL);
4621
4622 if (NULL != dirname_now)
4623 {
4624 mch_dirname(dirname_now, MAXPATHL);
4625 if (STRCMP(dirname_start, dirname_now) != 0)
4626 {
4627 /* If the directory has changed, change it back by building up an
4628 * appropriate ex command and executing it. */
4629 exarg_T ea;
4630
4631 ea.arg = dirname_start;
4632 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4633 ex_cd(&ea);
4634 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004635 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004636 }
4637}
4638
4639/*
4640 * Load file "fname" into a dummy buffer and return the buffer pointer,
4641 * placing the directory resulting from the buffer load into the
4642 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4643 * prior to calling this function. Restores directory to "dirname_start" prior
4644 * to returning, if autocmds or the 'autochdir' option have changed it.
4645 *
4646 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4647 * or wipe_dummy_buffer() later!
4648 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004649 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004650 */
4651 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004652load_dummy_buffer(
4653 char_u *fname,
4654 char_u *dirname_start, /* in: old directory */
4655 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004656{
4657 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004658 bufref_T newbufref;
4659 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004660 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004661 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004662 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004663
4664 /* Allocate a buffer without putting it in the buffer list. */
4665 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4666 if (newbuf == NULL)
4667 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004668 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004669
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004670 /* Init the options. */
4671 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4672
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004673 /* need to open the memfile before putting the buffer in a window */
4674 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004675 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004676 /* Make sure this buffer isn't wiped out by auto commands. */
4677 ++newbuf->b_locked;
4678
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004679 /* set curwin/curbuf to buf and save a few things */
4680 aucmd_prepbuf(&aco, newbuf);
4681
4682 /* Need to set the filename for autocommands. */
4683 (void)setfname(curbuf, fname, NULL, FALSE);
4684
Bram Moolenaar81695252004-12-29 20:58:21 +00004685 /* Create swap file now to avoid the ATTENTION message. */
4686 check_need_swap(TRUE);
4687
4688 /* Remove the "dummy" flag, otherwise autocommands may not
4689 * work. */
4690 curbuf->b_flags &= ~BF_DUMMY;
4691
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004692 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004693 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004694 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004695 NULL, READ_NEW | READ_DUMMY);
4696 --newbuf->b_locked;
4697 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004698 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004699 && !(curbuf->b_flags & BF_NEW))
4700 {
4701 failed = FALSE;
4702 if (curbuf != newbuf)
4703 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004704 /* Bloody autocommands changed the buffer! Can happen when
4705 * using netrw and editing a remote file. Use the current
4706 * buffer instead, delete the dummy one after restoring the
4707 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004708 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004709 newbuf = curbuf;
4710 }
4711 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004712
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004713 /* restore curwin/curbuf and a few other things */
4714 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004715 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4716 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004717
4718 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4719 * skip it. */
4720 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004721 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004722
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004723 /*
4724 * When autocommands/'autochdir' option changed directory: go back.
4725 * Let the caller know what the resulting dir was first, in case it is
4726 * important.
4727 */
4728 mch_dirname(resulting_dir, MAXPATHL);
4729 restore_start_dir(dirname_start);
4730
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004731 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004732 return NULL;
4733 if (failed)
4734 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004735 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004736 return NULL;
4737 }
4738 return newbuf;
4739}
4740
4741/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004742 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4743 * directory to "dirname_start" prior to returning, if autocmds or the
4744 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004745 */
4746 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004747wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004748{
4749 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004750 {
4751#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4752 cleanup_T cs;
4753
4754 /* Reset the error/interrupt/exception state here so that aborting()
4755 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4756 * work when got_int is set. */
4757 enter_cleanup(&cs);
4758#endif
4759
Bram Moolenaar81695252004-12-29 20:58:21 +00004760 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004761
4762#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4763 /* Restore the error/interrupt/exception state if not discarded by a
4764 * new aborting error, interrupt, or uncaught exception. */
4765 leave_cleanup(&cs);
4766#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004767 /* When autocommands/'autochdir' option changed directory: go back. */
4768 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004769 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004770}
4771
4772/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004773 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4774 * directory to "dirname_start" prior to returning, if autocmds or the
4775 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004776 */
4777 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004778unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004779{
4780 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004781 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004782 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004783
4784 /* When autocommands/'autochdir' option changed directory: go back. */
4785 restore_start_dir(dirname_start);
4786 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004787}
4788
Bram Moolenaar05159a02005-02-26 23:04:13 +00004789#if defined(FEAT_EVAL) || defined(PROTO)
4790/*
4791 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004792 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004793 */
4794 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004795get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004796{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004797 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004798 dict_T *dict;
4799 char_u buf[2];
4800 qfline_T *qfp;
4801 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004802 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004803
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004804 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004805 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004806 qi = &ql_info;
4807 if (wp != NULL)
4808 {
4809 qi = GET_LOC_LIST(wp);
4810 if (qi == NULL)
4811 return FAIL;
4812 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004813 }
4814
Bram Moolenaard823fa92016-08-12 16:29:27 +02004815 if (qf_idx == -1)
4816 qf_idx = qi->qf_curlist;
4817
4818 if (qf_idx >= qi->qf_listcount
4819 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004820 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004821
Bram Moolenaard823fa92016-08-12 16:29:27 +02004822 qfp = qi->qf_lists[qf_idx].qf_start;
4823 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004824 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004825 /* Handle entries with a non-existing buffer number. */
4826 bufnum = qfp->qf_fnum;
4827 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4828 bufnum = 0;
4829
Bram Moolenaar05159a02005-02-26 23:04:13 +00004830 if ((dict = dict_alloc()) == NULL)
4831 return FAIL;
4832 if (list_append_dict(list, dict) == FAIL)
4833 return FAIL;
4834
4835 buf[0] = qfp->qf_type;
4836 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004837 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004838 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4839 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4840 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4841 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004842 || dict_add_nr_str(dict, "pattern", 0L,
4843 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4844 || dict_add_nr_str(dict, "text", 0L,
4845 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004846 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4847 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4848 return FAIL;
4849
4850 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004851 if (qfp == NULL)
4852 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004853 }
4854 return OK;
4855}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004856
4857/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004858 * Flags used by getqflist()/getloclist() to determine which fields to return.
4859 */
4860enum {
4861 QF_GETLIST_NONE = 0x0,
4862 QF_GETLIST_TITLE = 0x1,
4863 QF_GETLIST_ITEMS = 0x2,
4864 QF_GETLIST_NR = 0x4,
4865 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004866 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004867 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004868 QF_GETLIST_IDX = 0x40,
4869 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01004870 QF_GETLIST_TICK = 0x100,
4871 QF_GETLIST_ALL = 0x1FF
Bram Moolenaard823fa92016-08-12 16:29:27 +02004872};
4873
4874/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004875 * Parse text from 'di' and return the quickfix list items
4876 */
4877 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004878qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004879{
4880 int status = FAIL;
4881 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004882 char_u *errorformat = p_efm;
4883 dictitem_T *efm_di;
4884 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004885
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004886 /* Only a List value is supported */
4887 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004888 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004889 /* If errorformat is supplied then use it, otherwise use the 'efm'
4890 * option setting
4891 */
4892 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4893 {
4894 if (efm_di->di_tv.v_type != VAR_STRING ||
4895 efm_di->di_tv.vval.v_string == NULL)
4896 return FAIL;
4897 errorformat = efm_di->di_tv.vval.v_string;
4898 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004899
Bram Moolenaar36538222017-09-02 19:51:44 +02004900 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004901 if (l == NULL)
4902 return FAIL;
4903
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004904 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4905 if (qi != NULL)
4906 {
4907 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4908 qi->qf_refcount++;
4909
Bram Moolenaar36538222017-09-02 19:51:44 +02004910 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004911 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4912 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004913 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004914 qf_free(qi, 0);
4915 }
4916 free(qi);
4917 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004918 dict_add_list(retdict, "items", l);
4919 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004920 }
4921
4922 return status;
4923}
4924
4925/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004926 * Return the quickfix/location list number with the given identifier.
4927 * Returns -1 if list is not found.
4928 */
4929 static int
4930qf_id2nr(qf_info_T *qi, int_u qfid)
4931{
4932 int qf_idx;
4933
4934 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4935 if (qi->qf_lists[qf_idx].qf_id == qfid)
4936 return qf_idx;
4937 return -1;
4938}
4939
4940/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004941 * Return the quickfix/location list window identifier in the current tabpage.
4942 */
4943 static int
4944qf_winid(qf_info_T *qi)
4945{
4946 win_T *win;
4947
4948 /* The quickfix window can be opened even if the quickfix list is not set
4949 * using ":copen". This is not true for location lists. */
4950 if (qi == NULL)
4951 return 0;
4952 win = qf_find_win(qi);
4953 if (win != NULL)
4954 return win->w_id;
4955 return 0;
4956}
4957
4958/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004959 * Return quickfix/location list details (title) as a
4960 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4961 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004962 */
4963 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004964qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004965{
4966 qf_info_T *qi = &ql_info;
4967 int status = OK;
4968 int qf_idx;
4969 dictitem_T *di;
4970 int flags = QF_GETLIST_NONE;
4971
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004972 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004973 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004974
Bram Moolenaard823fa92016-08-12 16:29:27 +02004975 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004976 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004977
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004978 if (dict_find(what, (char_u *)"all", -1) != NULL)
4979 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004980
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004981 if (dict_find(what, (char_u *)"title", -1) != NULL)
4982 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004983
Bram Moolenaara6d48492017-12-12 22:45:31 +01004984 if (dict_find(what, (char_u *)"nr", -1) != NULL)
4985 flags |= QF_GETLIST_NR;
4986
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004987 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4988 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004989
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004990 if (dict_find(what, (char_u *)"context", -1) != NULL)
4991 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004992
Bram Moolenaara6d48492017-12-12 22:45:31 +01004993 if (dict_find(what, (char_u *)"id", -1) != NULL)
4994 flags |= QF_GETLIST_ID;
4995
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004996 if (dict_find(what, (char_u *)"items", -1) != NULL)
4997 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004998
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004999 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5000 flags |= QF_GETLIST_IDX;
5001
5002 if (dict_find(what, (char_u *)"size", -1) != NULL)
5003 flags |= QF_GETLIST_SIZE;
5004
Bram Moolenaarb254af32017-12-18 19:48:58 +01005005 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5006 flags |= QF_GETLIST_TICK;
5007
Bram Moolenaara6d48492017-12-12 22:45:31 +01005008 if (qi != NULL && qi->qf_listcount != 0)
5009 {
5010 qf_idx = qi->qf_curlist; /* default is the current list */
5011 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5012 {
5013 /* Use the specified quickfix/location list */
5014 if (di->di_tv.v_type == VAR_NUMBER)
5015 {
5016 /* for zero use the current list */
5017 if (di->di_tv.vval.v_number != 0)
5018 {
5019 qf_idx = di->di_tv.vval.v_number - 1;
5020 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
5021 qf_idx = -1;
5022 }
5023 }
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005024 else if (di->di_tv.v_type == VAR_STRING
5025 && di->di_tv.vval.v_string != NULL
5026 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005027 /* Get the last quickfix list number */
5028 qf_idx = qi->qf_listcount - 1;
5029 else
5030 qf_idx = -1;
5031 flags |= QF_GETLIST_NR;
5032 }
5033
5034 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5035 {
5036 /* Look for a list with the specified id */
5037 if (di->di_tv.v_type == VAR_NUMBER)
5038 {
5039 /*
5040 * For zero, use the current list or the list specifed by 'nr'
5041 */
5042 if (di->di_tv.vval.v_number != 0)
5043 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5044 flags |= QF_GETLIST_ID;
5045 }
5046 else
5047 qf_idx = -1;
5048 }
5049 }
5050
5051 /* List is not present or is empty */
5052 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
5053 {
5054 if (flags & QF_GETLIST_TITLE)
5055 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5056 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5057 {
5058 list_T *l = list_alloc();
5059 if (l != NULL)
5060 status = dict_add_list(retdict, "items", l);
5061 else
5062 status = FAIL;
5063 }
5064 if ((status == OK) && (flags & QF_GETLIST_NR))
5065 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5066 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005067 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005068 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5069 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5070 if ((status == OK) && (flags & QF_GETLIST_ID))
5071 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5072 if ((status == OK) && (flags & QF_GETLIST_IDX))
5073 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5074 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5075 status = dict_add_nr_str(retdict, "size", 0L, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005076 if ((status == OK) && (flags & QF_GETLIST_TICK))
5077 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005078
5079 return status;
5080 }
5081
Bram Moolenaard823fa92016-08-12 16:29:27 +02005082 if (flags & QF_GETLIST_TITLE)
5083 {
5084 char_u *t;
5085 t = qi->qf_lists[qf_idx].qf_title;
5086 if (t == NULL)
5087 t = (char_u *)"";
5088 status = dict_add_nr_str(retdict, "title", 0L, t);
5089 }
5090 if ((status == OK) && (flags & QF_GETLIST_NR))
5091 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5092 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005093 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005094 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5095 {
5096 list_T *l = list_alloc();
5097 if (l != NULL)
5098 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005099 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005100 dict_add_list(retdict, "items", l);
5101 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005102 else
5103 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005104 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005105
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005106 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5107 {
5108 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5109 {
5110 di = dictitem_alloc((char_u *)"context");
5111 if (di != NULL)
5112 {
5113 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005114 status = dict_add(retdict, di);
5115 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02005116 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005117 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005118 else
5119 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005120 }
5121 else
5122 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5123 }
5124
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005125 if ((status == OK) && (flags & QF_GETLIST_ID))
5126 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5127 NULL);
5128
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005129 if ((status == OK) && (flags & QF_GETLIST_IDX))
5130 {
5131 int idx = qi->qf_lists[qf_idx].qf_index;
5132 if (qi->qf_lists[qf_idx].qf_count == 0)
5133 /* For empty lists, qf_index is set to 1 */
5134 idx = 0;
5135 status = dict_add_nr_str(retdict, "idx", idx, NULL);
5136 }
5137
5138 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5139 status = dict_add_nr_str(retdict, "size",
5140 qi->qf_lists[qf_idx].qf_count, NULL);
5141
Bram Moolenaarb254af32017-12-18 19:48:58 +01005142 if ((status == OK) && (flags & QF_GETLIST_TICK))
5143 status = dict_add_nr_str(retdict, "changedtick",
5144 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5145
Bram Moolenaard823fa92016-08-12 16:29:27 +02005146 return status;
5147}
5148
5149/*
5150 * Add list of entries to quickfix/location list. Each list entry is
5151 * a dictionary with item information.
5152 */
5153 static int
5154qf_add_entries(
5155 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005156 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005157 list_T *list,
5158 char_u *title,
5159 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005160{
5161 listitem_T *li;
5162 dict_T *d;
5163 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005164 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005165 long lnum;
5166 int col, nr;
5167 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005168 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005169 int valid, status;
5170 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005171 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005172
Bram Moolenaara3921f42017-06-04 15:30:34 +02005173 if (action == ' ' || qf_idx == qi->qf_listcount)
5174 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005175 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005176 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005177 qf_idx = qi->qf_curlist;
5178 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005179 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005180 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005181 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005182 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005183 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005184 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005185 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005186 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005187
5188 for (li = list->lv_first; li != NULL; li = li->li_next)
5189 {
5190 if (li->li_tv.v_type != VAR_DICT)
5191 continue; /* Skip non-dict items */
5192
5193 d = li->li_tv.vval.v_dict;
5194 if (d == NULL)
5195 continue;
5196
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005197 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005198 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5199 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5200 col = (int)get_dict_number(d, (char_u *)"col");
5201 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5202 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005203 type = get_dict_string(d, (char_u *)"type", TRUE);
5204 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5205 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005206 if (text == NULL)
5207 text = vim_strsave((char_u *)"");
5208
5209 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005210 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005211 valid = FALSE;
5212
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005213 /* Mark entries with non-existing buffer number as not valid. Give the
5214 * error message only once. */
5215 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5216 {
5217 if (!did_bufnr_emsg)
5218 {
5219 did_bufnr_emsg = TRUE;
5220 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5221 }
5222 valid = FALSE;
5223 bufnum = 0;
5224 }
5225
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005226 /* If the 'valid' field is present it overrules the detected value. */
5227 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5228 valid = (int)get_dict_number(d, (char_u *)"valid");
5229
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005230 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005231 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005232 NULL, /* dir */
5233 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005234 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005235 text,
5236 lnum,
5237 col,
5238 vcol, /* vis_col */
5239 pattern, /* search pattern */
5240 nr,
5241 type == NULL ? NUL : *type,
5242 valid);
5243
5244 vim_free(filename);
5245 vim_free(pattern);
5246 vim_free(text);
5247 vim_free(type);
5248
5249 if (status == FAIL)
5250 {
5251 retval = FAIL;
5252 break;
5253 }
5254 }
5255
Bram Moolenaara3921f42017-06-04 15:30:34 +02005256 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005257 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005258 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005259 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005260 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005261 if (action != 'a')
5262 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005263 qi->qf_lists[qf_idx].qf_ptr =
5264 qi->qf_lists[qf_idx].qf_start;
5265 if (qi->qf_lists[qf_idx].qf_count > 0)
5266 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005267 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005268
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005269 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005270 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005271
5272 return retval;
5273}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005274
5275 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005276qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005277{
5278 dictitem_T *di;
5279 int retval = FAIL;
5280 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005281 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005282 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005283
5284 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5285 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005286
5287 qf_idx = qi->qf_curlist; /* default is the current list */
5288 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5289 {
5290 /* Use the specified quickfix/location list */
5291 if (di->di_tv.v_type == VAR_NUMBER)
5292 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005293 /* for zero use the current list */
5294 if (di->di_tv.vval.v_number != 0)
5295 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005296
Bram Moolenaar55b69262017-08-13 13:42:01 +02005297 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5298 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005299 /*
5300 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005301 * non-available list and add the new list at the end of the
5302 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005303 */
5304 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005305 qf_idx = qi->qf_listcount - 1;
5306 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005307 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005308 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005309 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005310 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005311 }
5312 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005313 && di->di_tv.vval.v_string != NULL
5314 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005315 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005316 if (qi->qf_listcount > 0)
5317 qf_idx = qi->qf_listcount - 1;
5318 else if (newlist)
5319 qf_idx = 0;
5320 else
5321 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005322 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005323 else
5324 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005325 }
5326
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005327 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5328 {
5329 /* Use the quickfix/location list with the specified id */
5330 if (di->di_tv.v_type == VAR_NUMBER)
5331 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005332 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5333 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005334 return FAIL; /* List not found */
5335 }
5336 else
5337 return FAIL;
5338 }
5339
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005340 if (newlist)
5341 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005342 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005343 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005344 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005345 }
5346
5347 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5348 {
5349 if (di->di_tv.v_type == VAR_STRING)
5350 {
5351 vim_free(qi->qf_lists[qf_idx].qf_title);
5352 qi->qf_lists[qf_idx].qf_title =
5353 get_dict_string(what, (char_u *)"title", TRUE);
5354 if (qf_idx == qi->qf_curlist)
5355 qf_update_win_titlevar(qi);
5356 retval = OK;
5357 }
5358 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005359
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005360 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5361 {
5362 if (di->di_tv.v_type == VAR_LIST)
5363 {
5364 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5365
5366 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5367 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005368 if (action == 'r')
5369 {
5370 /*
5371 * When replacing the quickfix list entries using
5372 * qf_add_entries(), the title is set with a ':' prefix.
5373 * Restore the title with the saved title.
5374 */
5375 vim_free(qi->qf_lists[qf_idx].qf_title);
5376 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5377 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005378 vim_free(title_save);
5379 }
5380 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005381
Bram Moolenaar36538222017-09-02 19:51:44 +02005382 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5383 {
5384 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5385 return FAIL;
5386 errorformat = di->di_tv.vval.v_string;
5387 }
5388
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005389 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005390 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005391 /* Only a List value is supported */
5392 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005393 {
5394 if (action == 'r')
5395 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005396 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005397 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5398 retval = OK;
5399 }
5400 else
5401 return FAIL;
5402 }
5403
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005404 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5405 {
5406 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005407
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005408 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005409 ctx = alloc_tv();
5410 if (ctx != NULL)
5411 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005412 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005413 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005414 }
5415
Bram Moolenaarb254af32017-12-18 19:48:58 +01005416 if (retval == OK)
5417 qf_list_changed(qi, qf_idx);
5418
Bram Moolenaard823fa92016-08-12 16:29:27 +02005419 return retval;
5420}
5421
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005422/*
5423 * Find the non-location list window with the specified location list.
5424 */
5425 static win_T *
5426find_win_with_ll(qf_info_T *qi)
5427{
5428 win_T *wp = NULL;
5429
5430 FOR_ALL_WINDOWS(wp)
5431 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5432 return wp;
5433
5434 return NULL;
5435}
5436
5437/*
5438 * Free the entire quickfix/location list stack.
5439 * If the quickfix/location list window is open, then clear it.
5440 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005441 static void
5442qf_free_stack(win_T *wp, qf_info_T *qi)
5443{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005444 win_T *qfwin = qf_find_win(qi);
5445 win_T *llwin = NULL;
5446 win_T *orig_wp = wp;
5447
5448 if (qfwin != NULL)
5449 {
5450 /* If the quickfix/location list window is open, then clear it */
5451 if (qi->qf_curlist < qi->qf_listcount)
5452 qf_free(qi, qi->qf_curlist);
5453 qf_update_buffer(qi, NULL);
5454 }
5455
5456 if (wp != NULL && IS_LL_WINDOW(wp))
5457 {
5458 /* If in the location list window, then use the non-location list
5459 * window with this location list (if present)
5460 */
5461 llwin = find_win_with_ll(qi);
5462 if (llwin != NULL)
5463 wp = llwin;
5464 }
5465
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005466 qf_free_all(wp);
5467 if (wp == NULL)
5468 {
5469 /* quickfix list */
5470 qi->qf_curlist = 0;
5471 qi->qf_listcount = 0;
5472 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005473 else if (IS_LL_WINDOW(orig_wp))
5474 {
5475 /* If the location list window is open, then create a new empty
5476 * location list */
5477 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005478
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005479 /* first free the list reference in the location list window */
5480 ll_free_all(&orig_wp->w_llist_ref);
5481
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005482 orig_wp->w_llist_ref = new_ll;
5483 if (llwin != NULL)
5484 {
5485 llwin->w_llist = new_ll;
5486 new_ll->qf_refcount++;
5487 }
5488 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005489}
5490
Bram Moolenaard823fa92016-08-12 16:29:27 +02005491/*
5492 * Populate the quickfix list with the items supplied in the list
5493 * of dictionaries. "title" will be copied to w:quickfix_title.
5494 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5495 */
5496 int
5497set_errorlist(
5498 win_T *wp,
5499 list_T *list,
5500 int action,
5501 char_u *title,
5502 dict_T *what)
5503{
5504 qf_info_T *qi = &ql_info;
5505 int retval = OK;
5506
5507 if (wp != NULL)
5508 {
5509 qi = ll_get_or_alloc_list(wp);
5510 if (qi == NULL)
5511 return FAIL;
5512 }
5513
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005514 if (action == 'f')
5515 {
5516 /* Free the entire quickfix or location list stack */
5517 qf_free_stack(wp, qi);
5518 }
5519 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005520 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005521 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005522 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005523 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005524 if (retval == OK)
5525 qf_list_changed(qi, qi->qf_curlist);
5526 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005527
5528 return retval;
5529}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005530
5531 static int
5532mark_quickfix_ctx(qf_info_T *qi, int copyID)
5533{
5534 int i;
5535 int abort = FALSE;
5536 typval_T *ctx;
5537
5538 for (i = 0; i < LISTCOUNT && !abort; ++i)
5539 {
5540 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005541 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5542 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005543 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5544 }
5545
5546 return abort;
5547}
5548
5549/*
5550 * Mark the context of the quickfix list and the location lists (if present) as
5551 * "in use". So that garabage collection doesn't free the context.
5552 */
5553 int
5554set_ref_in_quickfix(int copyID)
5555{
5556 int abort = FALSE;
5557 tabpage_T *tp;
5558 win_T *win;
5559
5560 abort = mark_quickfix_ctx(&ql_info, copyID);
5561 if (abort)
5562 return abort;
5563
5564 FOR_ALL_TAB_WINDOWS(tp, win)
5565 {
5566 if (win->w_llist != NULL)
5567 {
5568 abort = mark_quickfix_ctx(win->w_llist, copyID);
5569 if (abort)
5570 return abort;
5571 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005572 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5573 {
5574 /* In a location list window and none of the other windows is
5575 * referring to this location list. Mark the location list
5576 * context as still in use.
5577 */
5578 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5579 if (abort)
5580 return abort;
5581 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005582 }
5583
5584 return abort;
5585}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005586#endif
5587
Bram Moolenaar81695252004-12-29 20:58:21 +00005588/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005589 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005590 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005591 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005592 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005593 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005594 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005595 */
5596 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005597ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005598{
5599 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005600 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005601#ifdef FEAT_AUTOCMD
5602 char_u *au_name = NULL;
5603#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005604 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005605
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005606#ifdef FEAT_AUTOCMD
5607 switch (eap->cmdidx)
5608 {
5609 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5610 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5611 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5612 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5613 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5614 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5615 default: break;
5616 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005617 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5618 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005619 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005620# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005621 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005622 return;
5623# endif
5624 }
5625#endif
5626
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005627 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005628 if (eap->cmdidx == CMD_lbuffer
5629 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005630 || eap->cmdidx == CMD_laddbuffer)
5631 {
5632 qi = ll_get_or_alloc_list(curwin);
5633 if (qi == NULL)
5634 return;
5635 }
5636
Bram Moolenaar86b68352004-12-27 21:59:20 +00005637 if (*eap->arg == NUL)
5638 buf = curbuf;
5639 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5640 buf = buflist_findnr(atoi((char *)eap->arg));
5641 if (buf == NULL)
5642 EMSG(_(e_invarg));
5643 else if (buf->b_ml.ml_mfp == NULL)
5644 EMSG(_("E681: Buffer is not loaded"));
5645 else
5646 {
5647 if (eap->addr_count == 0)
5648 {
5649 eap->line1 = 1;
5650 eap->line2 = buf->b_ml.ml_line_count;
5651 }
5652 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5653 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5654 EMSG(_(e_invrange));
5655 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005656 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005657 char_u *qf_title = *eap->cmdlinep;
5658
5659 if (buf->b_sfname)
5660 {
5661 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5662 (char *)qf_title, (char *)buf->b_sfname);
5663 qf_title = IObuff;
5664 }
5665
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005666 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005667 (eap->cmdidx != CMD_caddbuffer
5668 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005669 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005670 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005671 if (res >= 0)
5672 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005673#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005674 if (au_name != NULL)
5675 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5676 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005677#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005678 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5679 eap->cmdidx == CMD_lbuffer))
5680 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005681 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005682 }
5683}
5684
Bram Moolenaar1e015462005-09-25 22:16:38 +00005685#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005686/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005687 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5688 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005689 */
5690 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005691ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005692{
5693 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005694 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005695#ifdef FEAT_AUTOCMD
5696 char_u *au_name = NULL;
5697#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005698 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005699
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005700#ifdef FEAT_AUTOCMD
5701 switch (eap->cmdidx)
5702 {
5703 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5704 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5705 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5706 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5707 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5708 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5709 default: break;
5710 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005711 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5712 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005713 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005714# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005715 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005716 return;
5717# endif
5718 }
5719#endif
5720
Bram Moolenaar3c097222017-12-21 20:54:49 +01005721 if (eap->cmdidx == CMD_lexpr
5722 || eap->cmdidx == CMD_lgetexpr
5723 || eap->cmdidx == CMD_laddexpr)
5724 {
5725 qi = ll_get_or_alloc_list(curwin);
5726 if (qi == NULL)
5727 return;
5728 }
5729
Bram Moolenaar4770d092006-01-12 23:22:24 +00005730 /* Evaluate the expression. When the result is a string or a list we can
5731 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005732 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005733 if (tv != NULL)
5734 {
5735 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5736 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5737 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005738 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005739 (eap->cmdidx != CMD_caddexpr
5740 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005741 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005742 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005743 if (res >= 0)
5744 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005745#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005746 if (au_name != NULL)
5747 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5748 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005749#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005750 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5751 eap->cmdidx == CMD_lexpr))
5752 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005753 }
5754 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005755 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005756 free_tv(tv);
5757 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005758}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005759#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005760
5761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 * ":helpgrep {pattern}"
5763 */
5764 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005765ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 regmatch_T regmatch;
5768 char_u *save_cpo;
5769 char_u *p;
5770 int fcount;
5771 char_u **fnames;
5772 FILE *fd;
5773 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005775#ifdef FEAT_MULTI_LANG
5776 char_u *lang;
5777#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005778 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005779 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005780 int new_qi = FALSE;
5781 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005782#ifdef FEAT_AUTOCMD
5783 char_u *au_name = NULL;
5784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005786#ifdef FEAT_MULTI_LANG
5787 /* Check for a specified language */
5788 lang = check_help_lang(eap->arg);
5789#endif
5790
Bram Moolenaar73633f82012-01-20 13:39:07 +01005791#ifdef FEAT_AUTOCMD
5792 switch (eap->cmdidx)
5793 {
5794 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5795 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5796 default: break;
5797 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005798 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5799 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005800 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005801# ifdef FEAT_EVAL
5802 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005803 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005804# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005805 }
5806#endif
5807
5808 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5809 save_cpo = p_cpo;
5810 p_cpo = empty_option;
5811
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005812 if (eap->cmdidx == CMD_lhelpgrep)
5813 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005814 /* If the current window is a help window, then use it */
5815 if (bt_help(curwin->w_buffer))
5816 wp = curwin;
5817 else
5818 /* Find an existing help window */
5819 FOR_ALL_WINDOWS(wp)
5820 if (bt_help(wp->w_buffer))
5821 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005822
5823 if (wp == NULL) /* Help window not found */
5824 qi = NULL;
5825 else
5826 qi = wp->w_llist;
5827
5828 if (qi == NULL)
5829 {
5830 /* Allocate a new location list for help text matches */
5831 if ((qi = ll_new_list()) == NULL)
5832 return;
5833 new_qi = TRUE;
5834 }
5835 }
5836
Bram Moolenaaree85df32017-03-19 14:19:50 +01005837 /* Autocommands may change the list. Save it for later comparison */
5838 save_qi = qi;
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5841 regmatch.rm_ic = FALSE;
5842 if (regmatch.regprog != NULL)
5843 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005844#ifdef FEAT_MBYTE
5845 vimconv_T vc;
5846
5847 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5848 * differs. */
5849 vc.vc_type = CONV_NONE;
5850 if (!enc_utf8)
5851 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5852#endif
5853
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005855 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
5857 /* Go through all directories in 'runtimepath' */
5858 p = p_rtp;
5859 while (*p != NUL && !got_int)
5860 {
5861 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5862
5863 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5864 add_pathsep(NameBuff);
5865 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5866 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5867 &fnames, EW_FILE|EW_SILENT) == OK
5868 && fcount > 0)
5869 {
5870 for (fi = 0; fi < fcount && !got_int; ++fi)
5871 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005872#ifdef FEAT_MULTI_LANG
5873 /* Skip files for a different language. */
5874 if (lang != NULL
5875 && STRNICMP(lang, fnames[fi]
5876 + STRLEN(fnames[fi]) - 3, 2) != 0
5877 && !(STRNICMP(lang, "en", 2) == 0
5878 && STRNICMP("txt", fnames[fi]
5879 + STRLEN(fnames[fi]) - 3, 3) == 0))
5880 continue;
5881#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005882 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 if (fd != NULL)
5884 {
5885 lnum = 1;
5886 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5887 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005888 char_u *line = IObuff;
5889#ifdef FEAT_MBYTE
5890 /* Convert a line if 'encoding' is not utf-8 and
5891 * the line contains a non-ASCII character. */
5892 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005893 && has_non_ascii(IObuff))
5894 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005895 line = string_convert(&vc, IObuff, NULL);
5896 if (line == NULL)
5897 line = IObuff;
5898 }
5899#endif
5900
5901 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005903 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904
5905 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005906 while (l > 0 && line[l - 1] <= ' ')
5907 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005909 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005910 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 NULL, /* dir */
5912 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005913 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005914 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005916 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005917 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005918 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005919 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 0, /* nr */
5921 1, /* type */
5922 TRUE /* valid */
5923 ) == FAIL)
5924 {
5925 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005926#ifdef FEAT_MBYTE
5927 if (line != IObuff)
5928 vim_free(line);
5929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 break;
5931 }
5932 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005933#ifdef FEAT_MBYTE
5934 if (line != IObuff)
5935 vim_free(line);
5936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 ++lnum;
5938 line_breakcheck();
5939 }
5940 fclose(fd);
5941 }
5942 }
5943 FreeWild(fcount, fnames);
5944 }
5945 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005946
Bram Moolenaar473de612013-06-08 18:19:48 +02005947 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005948#ifdef FEAT_MBYTE
5949 if (vc.vc_type != CONV_NONE)
5950 convert_setup(&vc, NULL, NULL);
5951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005953 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5954 qi->qf_lists[qi->qf_curlist].qf_ptr =
5955 qi->qf_lists[qi->qf_curlist].qf_start;
5956 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 }
5958
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005959 if (p_cpo == empty_option)
5960 p_cpo = save_cpo;
5961 else
5962 /* Darn, some plugin changed the value. */
5963 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Bram Moolenaarb254af32017-12-18 19:48:58 +01005965 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005966 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967
Bram Moolenaar73633f82012-01-20 13:39:07 +01005968#ifdef FEAT_AUTOCMD
5969 if (au_name != NULL)
5970 {
5971 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5972 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005973 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005974 /* autocommands made "qi" invalid */
5975 return;
5976 }
5977#endif
5978
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005980 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005981 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005982 else
5983 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005984
5985 if (eap->cmdidx == CMD_lhelpgrep)
5986 {
5987 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005988 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005989 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005990 {
5991 if (new_qi)
5992 ll_free_all(&qi);
5993 }
5994 else if (curwin->w_llist == NULL)
5995 curwin->w_llist = qi;
5996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997}
5998
5999#endif /* FEAT_QUICKFIX */