blob: b2dabaaacd0d48f4b14b95d0420451d03af64dc8 [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. */
1180 vim_free(qf_last_bufname);
1181 qf_last_bufname = NULL;
1182
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001183 vim_memset(&state, 0, sizeof(state));
1184 vim_memset(&fields, 0, sizeof(fields));
1185#ifdef FEAT_MBYTE
1186 state.vc.vc_type = CONV_NONE;
1187 if (enc != NULL && *enc != NUL)
1188 convert_setup(&state.vc, enc, p_enc);
1189#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001190 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1191 fields.errmsglen = CMDBUFFSIZE + 1;
1192 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1193 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001194 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 goto qf_init_end;
1196
Bram Moolenaare0d37972016-07-15 22:36:01 +02001197 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
1199 EMSG2(_(e_openerrf), efile);
1200 goto qf_init_end;
1201 }
1202
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001203 if (newlist || qf_idx == qi->qf_listcount)
1204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001206 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001207 qf_idx = qi->qf_curlist;
1208 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001209 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001210 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001211 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001212 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001213 if (qi->qf_lists[qf_idx].qf_count > 0)
1214 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001217 qfl = &qi->qf_lists[qf_idx];
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001220 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001221 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 else
1223 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001225 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001226 * If the errorformat didn't change between calls, then reuse the
1227 * previously parsed values.
1228 */
1229 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1230 {
1231 /* free the previously parsed data */
1232 vim_free(last_efm);
1233 last_efm = NULL;
1234 free_efm_list(&fmt_first);
1235
1236 /* parse the current 'efm' */
1237 fmt_first = parse_efm_option(efm);
1238 if (fmt_first != NULL)
1239 last_efm = vim_strsave(efm);
1240 }
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /*
1246 * got_int is reset here, because it was probably set when killing the
1247 * ":make" command, but we still want to read the errorfile then.
1248 */
1249 got_int = FALSE;
1250
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001251 if (tv != NULL)
1252 {
1253 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001254 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001255 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001256 state.p_li = tv->vval.v_list->lv_first;
1257 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001258 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001259 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001260 state.buflnum = lnumfirst;
1261 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /*
1264 * Read the lines in the error file one by one.
1265 * Try to recognize one of the error formats in each line.
1266 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001267 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001269 /* Get the next line from a file/buffer/list/string */
1270 status = qf_get_nextline(&state);
1271 if (status == QF_NOMEM) /* memory alloc failure */
1272 goto qf_init_end;
1273 if (status == QF_END_OF_INPUT) /* end of input */
1274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001276 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1277 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001278 if (status == QF_FAIL)
1279 goto error2;
1280 if (status == QF_NOMEM)
1281 goto qf_init_end;
1282 if (status == QF_IGNORE_LINE)
1283 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001285 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001286 qf_idx,
1287 qfl->qf_directory,
1288 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001289 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001290 : ((qfl->qf_currfile != NULL && fields.valid)
1291 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001292 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001293 fields.errmsg,
1294 fields.lnum,
1295 fields.col,
1296 fields.use_viscol,
1297 fields.pattern,
1298 fields.enr,
1299 fields.type,
1300 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 goto error2;
1302 line_breakcheck();
1303 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001304 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001306 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001308 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001309 qfl->qf_ptr = qfl->qf_start;
1310 qfl->qf_index = 1;
1311 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 else
1314 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001315 qfl->qf_nonevalid = FALSE;
1316 if (qfl->qf_ptr == NULL)
1317 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001319 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001320 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001321 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 EMSG(_(e_readerrf));
1324error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001325 if (!adding)
1326 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001327 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001328 qf_free(qi, qi->qf_curlist);
1329 qi->qf_listcount--;
1330 if (qi->qf_curlist > 0)
1331 --qi->qf_curlist;
1332 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001333qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001334 if (state.fd != NULL)
1335 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001336 vim_free(fields.namebuf);
1337 vim_free(fields.errmsg);
1338 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001339 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001341 if (qf_idx == qi->qf_curlist)
1342 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001343#ifdef FEAT_MBYTE
1344 if (state.vc.vc_type != CONV_NONE)
1345 convert_setup(&state.vc, NULL, NULL);
1346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 return retval;
1349}
1350
Bram Moolenaarfb604092014-07-23 15:55:00 +02001351 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001352qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001353{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001354 vim_free(qi->qf_lists[qf_idx].qf_title);
1355 qi->qf_lists[qf_idx].qf_title = NULL;
1356
Bram Moolenaarfb604092014-07-23 15:55:00 +02001357 if (title != NULL)
1358 {
1359 char_u *p = alloc((int)STRLEN(title) + 2);
1360
Bram Moolenaara3921f42017-06-04 15:30:34 +02001361 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001362 if (p != NULL)
1363 sprintf((char *)p, ":%s", (char *)title);
1364 }
1365}
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001368 * Prepare for adding a new quickfix list. If the current list is in the
1369 * middle of the stack, then all the following lists are freed and then
1370 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 */
1372 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001373qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
1375 int i;
1376
1377 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001378 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 * the current entry. This makes it possible to browse in a tree-like
1380 * way with ":grep'.
1381 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001382 while (qi->qf_listcount > qi->qf_curlist + 1)
1383 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
1385 /*
1386 * When the stack is full, remove to oldest entry
1387 * Otherwise, add a new entry.
1388 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001389 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001391 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001393 qi->qf_lists[i - 1] = qi->qf_lists[i];
1394 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001397 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001398 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001399 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001400 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401}
1402
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001403/*
1404 * Free a location list
1405 */
1406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001407ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001408{
1409 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001410 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001411
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001412 qi = *pqi;
1413 if (qi == NULL)
1414 return;
1415 *pqi = NULL; /* Remove reference to this list */
1416
1417 qi->qf_refcount--;
1418 if (qi->qf_refcount < 1)
1419 {
1420 /* No references to this location list */
1421 for (i = 0; i < qi->qf_listcount; ++i)
1422 qf_free(qi, i);
1423 vim_free(qi);
1424 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001425}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001426
1427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001428qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001429{
1430 int i;
1431 qf_info_T *qi = &ql_info;
1432
1433 if (wp != NULL)
1434 {
1435 /* location list */
1436 ll_free_all(&wp->w_llist);
1437 ll_free_all(&wp->w_llist_ref);
1438 }
1439 else
1440 /* quickfix list */
1441 for (i = 0; i < qi->qf_listcount; ++i)
1442 qf_free(qi, i);
1443}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445/*
1446 * Add an entry to the end of the list of errors.
1447 * Returns OK or FAIL.
1448 */
1449 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001450qf_add_entry(
1451 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001452 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001453 char_u *dir, /* optional directory name */
1454 char_u *fname, /* file name or NULL */
1455 int bufnum, /* buffer number or zero */
1456 char_u *mesg, /* message */
1457 long lnum, /* line number */
1458 int col, /* column */
1459 int vis_col, /* using visual column */
1460 char_u *pattern, /* search pattern */
1461 int nr, /* error number */
1462 int type, /* type character */
1463 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001465 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001466 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001468 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001470 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001471 {
1472 buf_T *buf = buflist_findnr(bufnum);
1473
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001474 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001475 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001476 buf->b_has_qf_entry |=
1477 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001478 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001479 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001480 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1482 {
1483 vim_free(qfp);
1484 return FAIL;
1485 }
1486 qfp->qf_lnum = lnum;
1487 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001488 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001489 if (pattern == NULL || *pattern == NUL)
1490 qfp->qf_pattern = NULL;
1491 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1492 {
1493 vim_free(qfp->qf_text);
1494 vim_free(qfp);
1495 return FAIL;
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 qfp->qf_nr = nr;
1498 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1499 type = 0;
1500 qfp->qf_type = type;
1501 qfp->qf_valid = valid;
1502
Bram Moolenaara3921f42017-06-04 15:30:34 +02001503 lastp = &qi->qf_lists[qf_idx].qf_last;
1504 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001505 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001507 qi->qf_lists[qf_idx].qf_start = qfp;
1508 qi->qf_lists[qf_idx].qf_ptr = qfp;
1509 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001510 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 else
1513 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001514 qfp->qf_prev = *lastp;
1515 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001517 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001519 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001520 ++qi->qf_lists[qf_idx].qf_count;
1521 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001524 qi->qf_lists[qf_idx].qf_index =
1525 qi->qf_lists[qf_idx].qf_count;
1526 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
1528
1529 return OK;
1530}
1531
1532/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001533 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001535 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001536ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001537{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001538 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001539
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001540 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1541 if (qi != NULL)
1542 {
1543 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1544 qi->qf_refcount++;
1545 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001546
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001547 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548}
1549
1550/*
1551 * Return the location list for window 'wp'.
1552 * If not present, allocate a location list
1553 */
1554 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001555ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001556{
1557 if (IS_LL_WINDOW(wp))
1558 /* For a location list window, use the referenced location list */
1559 return wp->w_llist_ref;
1560
1561 /*
1562 * For a non-location list window, w_llist_ref should not point to a
1563 * location list.
1564 */
1565 ll_free_all(&wp->w_llist_ref);
1566
1567 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001568 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001569 return wp->w_llist;
1570}
1571
1572/*
1573 * Copy the location list from window "from" to window "to".
1574 */
1575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001576copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001577{
1578 qf_info_T *qi;
1579 int idx;
1580 int i;
1581
1582 /*
1583 * When copying from a location list window, copy the referenced
1584 * location list. For other windows, copy the location list for
1585 * that window.
1586 */
1587 if (IS_LL_WINDOW(from))
1588 qi = from->w_llist_ref;
1589 else
1590 qi = from->w_llist;
1591
1592 if (qi == NULL) /* no location list to copy */
1593 return;
1594
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001595 /* allocate a new location list */
1596 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001597 return;
1598
1599 to->w_llist->qf_listcount = qi->qf_listcount;
1600
1601 /* Copy the location lists one at a time */
1602 for (idx = 0; idx < qi->qf_listcount; idx++)
1603 {
1604 qf_list_T *from_qfl;
1605 qf_list_T *to_qfl;
1606
1607 to->w_llist->qf_curlist = idx;
1608
1609 from_qfl = &qi->qf_lists[idx];
1610 to_qfl = &to->w_llist->qf_lists[idx];
1611
1612 /* Some of the fields are populated by qf_add_entry() */
1613 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1614 to_qfl->qf_count = 0;
1615 to_qfl->qf_index = 0;
1616 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001617 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001618 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001619 if (from_qfl->qf_title != NULL)
1620 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1621 else
1622 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001623 if (from_qfl->qf_ctx != NULL)
1624 {
1625 to_qfl->qf_ctx = alloc_tv();
1626 if (to_qfl->qf_ctx != NULL)
1627 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1628 }
1629 else
1630 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631
1632 if (from_qfl->qf_count)
1633 {
1634 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001635 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001636
1637 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001638 for (i = 0, from_qfp = from_qfl->qf_start;
1639 i < from_qfl->qf_count && from_qfp != NULL;
1640 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001641 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001642 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001643 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 NULL,
1645 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001646 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001647 from_qfp->qf_text,
1648 from_qfp->qf_lnum,
1649 from_qfp->qf_col,
1650 from_qfp->qf_viscol,
1651 from_qfp->qf_pattern,
1652 from_qfp->qf_nr,
1653 0,
1654 from_qfp->qf_valid) == FAIL)
1655 {
1656 qf_free_all(to);
1657 return;
1658 }
1659 /*
1660 * qf_add_entry() will not set the qf_num field, as the
1661 * directory and file names are not supplied. So the qf_fnum
1662 * field is copied here.
1663 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001664 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001665 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1666 prevp->qf_type = from_qfp->qf_type; /* error type */
1667 if (from_qfl->qf_ptr == from_qfp)
1668 to_qfl->qf_ptr = prevp; /* current location */
1669 }
1670 }
1671
1672 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1673
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001674 /* Assign a new ID for the location list */
1675 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001676 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001677
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001678 /* When no valid entries are present in the list, qf_ptr points to
1679 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001680 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001681 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001682 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001683 to_qfl->qf_index = 1;
1684 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001685 }
1686
1687 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1688}
1689
1690/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001691 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001692 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 */
1694 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001695qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
Bram Moolenaar82404332016-07-10 17:00:38 +02001697 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001698 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001699 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (fname == NULL || *fname == NUL) /* no file name */
1702 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaare60acc12011-05-10 16:41:25 +02001704#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001705 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001706#endif
1707#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001708 if (directory != NULL)
1709 slash_adjust(directory);
1710 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001711#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001712 if (directory != NULL && !vim_isAbsName(fname)
1713 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1714 {
1715 /*
1716 * Here we check if the file really exists.
1717 * This should normally be true, but if make works without
1718 * "leaving directory"-messages we might have missed a
1719 * directory change.
1720 */
1721 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001724 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001725 if (directory)
1726 ptr = concat_fnames(directory, fname, TRUE);
1727 else
1728 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001730 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001731 bufname = ptr;
1732 }
1733 else
1734 bufname = fname;
1735
1736 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001737 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001738 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001739 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001740 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001742 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001743 {
1744 vim_free(qf_last_bufname);
1745 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1746 if (bufname == ptr)
1747 qf_last_bufname = bufname;
1748 else
1749 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001750 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001751 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001752 if (buf == NULL)
1753 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001754
Bram Moolenaarc1542742016-07-20 21:44:37 +02001755 buf->b_has_qf_entry =
1756 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001757 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758}
1759
1760/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001761 * Push dirbuf onto the directory stack and return pointer to actual dir or
1762 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 */
1764 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001765qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 struct dir_stack_T *ds_new;
1768 struct dir_stack_T *ds_ptr;
1769
1770 /* allocate new stack element and hook it in */
1771 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1772 if (ds_new == NULL)
1773 return NULL;
1774
1775 ds_new->next = *stackptr;
1776 *stackptr = ds_new;
1777
1778 /* store directory on the stack */
1779 if (vim_isAbsName(dirbuf)
1780 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001781 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 (*stackptr)->dirname = vim_strsave(dirbuf);
1783 else
1784 {
1785 /* Okay we don't have an absolute path.
1786 * dirbuf must be a subdir of one of the directories on the stack.
1787 * Let's search...
1788 */
1789 ds_new = (*stackptr)->next;
1790 (*stackptr)->dirname = NULL;
1791 while (ds_new)
1792 {
1793 vim_free((*stackptr)->dirname);
1794 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1795 TRUE);
1796 if (mch_isdir((*stackptr)->dirname) == TRUE)
1797 break;
1798
1799 ds_new = ds_new->next;
1800 }
1801
1802 /* clean up all dirs we already left */
1803 while ((*stackptr)->next != ds_new)
1804 {
1805 ds_ptr = (*stackptr)->next;
1806 (*stackptr)->next = (*stackptr)->next->next;
1807 vim_free(ds_ptr->dirname);
1808 vim_free(ds_ptr);
1809 }
1810
1811 /* Nothing found -> it must be on top level */
1812 if (ds_new == NULL)
1813 {
1814 vim_free((*stackptr)->dirname);
1815 (*stackptr)->dirname = vim_strsave(dirbuf);
1816 }
1817 }
1818
1819 if ((*stackptr)->dirname != NULL)
1820 return (*stackptr)->dirname;
1821 else
1822 {
1823 ds_ptr = *stackptr;
1824 *stackptr = (*stackptr)->next;
1825 vim_free(ds_ptr);
1826 return NULL;
1827 }
1828}
1829
1830
1831/*
1832 * pop dirbuf from the directory stack and return previous directory or NULL if
1833 * stack is empty
1834 */
1835 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001836qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 struct dir_stack_T *ds_ptr;
1839
1840 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1841 * What to do if it isn't? */
1842
1843 /* pop top element and free it */
1844 if (*stackptr != NULL)
1845 {
1846 ds_ptr = *stackptr;
1847 *stackptr = (*stackptr)->next;
1848 vim_free(ds_ptr->dirname);
1849 vim_free(ds_ptr);
1850 }
1851
1852 /* return NEW top element as current dir or NULL if stack is empty*/
1853 return *stackptr ? (*stackptr)->dirname : NULL;
1854}
1855
1856/*
1857 * clean up directory stack
1858 */
1859 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001860qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
1862 struct dir_stack_T *ds_ptr;
1863
1864 while ((ds_ptr = *stackptr) != NULL)
1865 {
1866 *stackptr = (*stackptr)->next;
1867 vim_free(ds_ptr->dirname);
1868 vim_free(ds_ptr);
1869 }
1870}
1871
1872/*
1873 * Check in which directory of the directory stack the given file can be
1874 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001875 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 * Cleans up intermediate directory entries.
1877 *
1878 * TODO: How to solve the following problem?
1879 * If we have the this directory tree:
1880 * ./
1881 * ./aa
1882 * ./aa/bb
1883 * ./bb
1884 * ./bb/x.c
1885 * and make says:
1886 * making all in aa
1887 * making all in bb
1888 * x.c:9: Error
1889 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1890 * qf_guess_filepath will return NULL.
1891 */
1892 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001893qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 struct dir_stack_T *ds_ptr;
1896 struct dir_stack_T *ds_tmp;
1897 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001898 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001901 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 return NULL;
1903
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001904 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 fullname = NULL;
1906 while (ds_ptr)
1907 {
1908 vim_free(fullname);
1909 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1910
1911 /* If concat_fnames failed, just go on. The worst thing that can happen
1912 * is that we delete the entire stack.
1913 */
1914 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1915 break;
1916
1917 ds_ptr = ds_ptr->next;
1918 }
1919
1920 vim_free(fullname);
1921
1922 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001923 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001925 ds_tmp = qfl->qf_dir_stack->next;
1926 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 vim_free(ds_tmp->dirname);
1928 vim_free(ds_tmp);
1929 }
1930
1931 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932}
1933
1934/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01001935 * Returns TRUE if a quickfix/location list with the given identifier exists.
1936 */
1937 static int
1938qflist_valid (win_T *wp, int_u qf_id)
1939{
1940 qf_info_T *qi = &ql_info;
1941 int i;
1942
1943 if (wp != NULL)
1944 {
1945 qi = GET_LOC_LIST(wp); /* Location list */
1946 if (qi == NULL)
1947 return FALSE;
1948 }
1949
1950 for (i = 0; i < qi->qf_listcount; ++i)
1951 if (qi->qf_lists[i].qf_id == qf_id)
1952 return TRUE;
1953
1954 return FALSE;
1955}
1956
1957/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001958 * When loading a file from the quickfix, the auto commands may modify it.
1959 * This may invalidate the current quickfix entry. This function checks
1960 * whether a entry is still present in the quickfix.
1961 * Similar to location list.
1962 */
1963 static int
1964is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1965{
1966 qf_list_T *qfl;
1967 qfline_T *qfp;
1968 int i;
1969
1970 qfl = &qi->qf_lists[qi->qf_curlist];
1971
1972 /* Search for the entry in the current list */
1973 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1974 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001975 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001976 break;
1977
1978 if (i == qfl->qf_count) /* Entry is not found */
1979 return FALSE;
1980
1981 return TRUE;
1982}
1983
1984/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001985 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001986 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001987 */
1988 static qfline_T *
1989get_next_valid_entry(
1990 qf_info_T *qi,
1991 qfline_T *qf_ptr,
1992 int *qf_index,
1993 int dir)
1994{
1995 int idx;
1996 int old_qf_fnum;
1997
1998 idx = *qf_index;
1999 old_qf_fnum = qf_ptr->qf_fnum;
2000
2001 do
2002 {
2003 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2004 || qf_ptr->qf_next == NULL)
2005 return NULL;
2006 ++idx;
2007 qf_ptr = qf_ptr->qf_next;
2008 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2009 && !qf_ptr->qf_valid)
2010 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2011
2012 *qf_index = idx;
2013 return qf_ptr;
2014}
2015
2016/*
2017 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002018 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002019 */
2020 static qfline_T *
2021get_prev_valid_entry(
2022 qf_info_T *qi,
2023 qfline_T *qf_ptr,
2024 int *qf_index,
2025 int dir)
2026{
2027 int idx;
2028 int old_qf_fnum;
2029
2030 idx = *qf_index;
2031 old_qf_fnum = qf_ptr->qf_fnum;
2032
2033 do
2034 {
2035 if (idx == 1 || qf_ptr->qf_prev == NULL)
2036 return NULL;
2037 --idx;
2038 qf_ptr = qf_ptr->qf_prev;
2039 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2040 && !qf_ptr->qf_valid)
2041 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2042
2043 *qf_index = idx;
2044 return qf_ptr;
2045}
2046
2047/*
2048 * Get the n'th (errornr) previous/next valid entry from the current entry in
2049 * the quickfix list.
2050 * dir == FORWARD or FORWARD_FILE: next valid entry
2051 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2052 */
2053 static qfline_T *
2054get_nth_valid_entry(
2055 qf_info_T *qi,
2056 int errornr,
2057 qfline_T *qf_ptr,
2058 int *qf_index,
2059 int dir)
2060{
2061 qfline_T *prev_qf_ptr;
2062 int prev_index;
2063 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2064 char_u *err = e_no_more_items;
2065
2066 while (errornr--)
2067 {
2068 prev_qf_ptr = qf_ptr;
2069 prev_index = *qf_index;
2070
2071 if (dir == FORWARD || dir == FORWARD_FILE)
2072 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2073 else
2074 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2075 if (qf_ptr == NULL)
2076 {
2077 qf_ptr = prev_qf_ptr;
2078 *qf_index = prev_index;
2079 if (err != NULL)
2080 {
2081 EMSG(_(err));
2082 return NULL;
2083 }
2084 break;
2085 }
2086
2087 err = NULL;
2088 }
2089
2090 return qf_ptr;
2091}
2092
2093/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002094 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002095 */
2096 static qfline_T *
2097get_nth_entry(
2098 qf_info_T *qi,
2099 int errornr,
2100 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002101 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002102{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002103 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002104
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002105 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002106 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2107 {
2108 --qf_idx;
2109 qf_ptr = qf_ptr->qf_prev;
2110 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002111 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002112 while (errornr > qf_idx &&
2113 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2114 qf_ptr->qf_next != NULL)
2115 {
2116 ++qf_idx;
2117 qf_ptr = qf_ptr->qf_next;
2118 }
2119
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002120 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002121 return qf_ptr;
2122}
2123
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002124/*
2125 * Find a help window or open one.
2126 */
2127 static int
2128jump_to_help_window(qf_info_T *qi, int *opened_window)
2129{
2130 win_T *wp;
2131 int flags;
2132
2133 if (cmdmod.tab != 0)
2134 wp = NULL;
2135 else
2136 FOR_ALL_WINDOWS(wp)
2137 if (bt_help(wp->w_buffer))
2138 break;
2139 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2140 win_enter(wp, TRUE);
2141 else
2142 {
2143 /*
2144 * Split off help window; put it at far top if no position
2145 * specified, the current window is vertically split and narrow.
2146 */
2147 flags = WSP_HELP;
2148 if (cmdmod.split == 0 && curwin->w_width != Columns
2149 && curwin->w_width < 80)
2150 flags |= WSP_TOP;
2151 if (qi != &ql_info)
2152 flags |= WSP_NEWLOC; /* don't copy the location list */
2153
2154 if (win_split(0, flags) == FAIL)
2155 return FAIL;
2156
2157 *opened_window = TRUE;
2158
2159 if (curwin->w_height < p_hh)
2160 win_setheight((int)p_hh);
2161
2162 if (qi != &ql_info) /* not a quickfix list */
2163 {
2164 /* The new window should use the supplied location list */
2165 curwin->w_llist = qi;
2166 qi->qf_refcount++;
2167 }
2168 }
2169
2170 if (!p_im)
2171 restart_edit = 0; /* don't want insert mode in help file */
2172
2173 return OK;
2174}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002175
2176/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002177 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2178 * If the file is already opened in a window, jump to it.
2179 */
2180 static int
2181qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2182{
2183 win_T *usable_win_ptr = NULL;
2184 int usable_win;
2185 qf_info_T *ll_ref;
2186 int flags;
2187 win_T *win;
2188 win_T *altwin;
2189
2190 usable_win = 0;
2191
2192 ll_ref = curwin->w_llist_ref;
2193 if (ll_ref != NULL)
2194 {
2195 /* Find a window using the same location list that is not a
2196 * quickfix window. */
2197 FOR_ALL_WINDOWS(usable_win_ptr)
2198 if (usable_win_ptr->w_llist == ll_ref
2199 && !bt_quickfix(usable_win_ptr->w_buffer))
2200 {
2201 usable_win = 1;
2202 break;
2203 }
2204 }
2205
2206 if (!usable_win)
2207 {
2208 /* Locate a window showing a normal buffer */
2209 FOR_ALL_WINDOWS(win)
2210 if (win->w_buffer->b_p_bt[0] == NUL)
2211 {
2212 usable_win = 1;
2213 break;
2214 }
2215 }
2216
2217 /*
2218 * If no usable window is found and 'switchbuf' contains "usetab"
2219 * then search in other tabs.
2220 */
2221 if (!usable_win && (swb_flags & SWB_USETAB))
2222 {
2223 tabpage_T *tp;
2224 win_T *wp;
2225
2226 FOR_ALL_TAB_WINDOWS(tp, wp)
2227 {
2228 if (wp->w_buffer->b_fnum == qf_fnum)
2229 {
2230 goto_tabpage_win(tp, wp);
2231 usable_win = 1;
2232 goto win_found;
2233 }
2234 }
2235 }
2236win_found:
2237
2238 /*
2239 * If there is only one window and it is the quickfix window, create a
2240 * new one above the quickfix window.
2241 */
2242 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2243 {
2244 flags = WSP_ABOVE;
2245 if (ll_ref != NULL)
2246 flags |= WSP_NEWLOC;
2247 if (win_split(0, flags) == FAIL)
2248 return FAIL; /* not enough room for window */
2249 *opened_window = TRUE; /* close it when fail */
2250 p_swb = empty_option; /* don't split again */
2251 swb_flags = 0;
2252 RESET_BINDING(curwin);
2253 if (ll_ref != NULL)
2254 {
2255 /* The new window should use the location list from the
2256 * location list window */
2257 curwin->w_llist = ll_ref;
2258 ll_ref->qf_refcount++;
2259 }
2260 }
2261 else
2262 {
2263 if (curwin->w_llist_ref != NULL)
2264 {
2265 /* In a location window */
2266 win = usable_win_ptr;
2267 if (win == NULL)
2268 {
2269 /* Find the window showing the selected file */
2270 FOR_ALL_WINDOWS(win)
2271 if (win->w_buffer->b_fnum == qf_fnum)
2272 break;
2273 if (win == NULL)
2274 {
2275 /* Find a previous usable window */
2276 win = curwin;
2277 do
2278 {
2279 if (win->w_buffer->b_p_bt[0] == NUL)
2280 break;
2281 if (win->w_prev == NULL)
2282 win = lastwin; /* wrap around the top */
2283 else
2284 win = win->w_prev; /* go to previous window */
2285 } while (win != curwin);
2286 }
2287 }
2288 win_goto(win);
2289
2290 /* If the location list for the window is not set, then set it
2291 * to the location list from the location window */
2292 if (win->w_llist == NULL)
2293 {
2294 win->w_llist = ll_ref;
2295 ll_ref->qf_refcount++;
2296 }
2297 }
2298 else
2299 {
2300
2301 /*
2302 * Try to find a window that shows the right buffer.
2303 * Default to the window just above the quickfix buffer.
2304 */
2305 win = curwin;
2306 altwin = NULL;
2307 for (;;)
2308 {
2309 if (win->w_buffer->b_fnum == qf_fnum)
2310 break;
2311 if (win->w_prev == NULL)
2312 win = lastwin; /* wrap around the top */
2313 else
2314 win = win->w_prev; /* go to previous window */
2315
2316 if (IS_QF_WINDOW(win))
2317 {
2318 /* Didn't find it, go to the window before the quickfix
2319 * window. */
2320 if (altwin != NULL)
2321 win = altwin;
2322 else if (curwin->w_prev != NULL)
2323 win = curwin->w_prev;
2324 else
2325 win = curwin->w_next;
2326 break;
2327 }
2328
2329 /* Remember a usable window. */
2330 if (altwin == NULL && !win->w_p_pvw
2331 && win->w_buffer->b_p_bt[0] == NUL)
2332 altwin = win;
2333 }
2334
2335 win_goto(win);
2336 }
2337 }
2338
2339 return OK;
2340}
2341
2342/*
2343 * Edit the selected file or help file.
2344 */
2345 static int
2346qf_jump_edit_buffer(
2347 qf_info_T *qi,
2348 qfline_T *qf_ptr,
2349 int forceit,
2350 win_T *oldwin,
2351 int *opened_window,
2352 int *abort)
2353{
2354 int retval = OK;
2355
2356 if (qf_ptr->qf_type == 1)
2357 {
2358 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2359 * set b_p_ro flag). */
2360 if (!can_abandon(curbuf, forceit))
2361 {
2362 no_write_message();
2363 retval = FALSE;
2364 }
2365 else
2366 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2367 ECMD_HIDE + ECMD_SET_HELP,
2368 oldwin == curwin ? curwin : NULL);
2369 }
2370 else
2371 {
2372 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002373 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002374
2375 retval = buflist_getfile(qf_ptr->qf_fnum,
2376 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002377
2378 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002379 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002380 /*
2381 * Location list. Check whether the associated window is still
2382 * present and the list is still valid.
2383 */
2384 if (!win_valid_any_tab(oldwin))
2385 {
2386 EMSG(_("E924: Current window was closed"));
2387 *abort = TRUE;
2388 *opened_window = FALSE;
2389 }
2390 else if (!qflist_valid(oldwin, save_qfid))
2391 {
2392 EMSG(_(e_loc_list_changed));
2393 *abort = TRUE;
2394 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002395 }
2396 else if (old_qf_curlist != qi->qf_curlist
2397 || !is_qf_entry_present(qi, qf_ptr))
2398 {
2399 if (qi == &ql_info)
2400 EMSG(_("E925: Current quickfix was changed"));
2401 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002402 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002403 *abort = TRUE;
2404 }
2405
2406 if (*abort)
2407 retval = FALSE;
2408 }
2409
2410 return retval;
2411}
2412
2413/*
2414 * Goto the error line in the current file using either line/column number or a
2415 * search pattern.
2416 */
2417 static void
2418qf_jump_goto_line(
2419 linenr_T qf_lnum,
2420 int qf_col,
2421 char_u qf_viscol,
2422 char_u *qf_pattern)
2423{
2424 linenr_T i;
2425 char_u *line;
2426 colnr_T screen_col;
2427 colnr_T char_col;
2428
2429 if (qf_pattern == NULL)
2430 {
2431 /*
2432 * Go to line with error, unless qf_lnum is 0.
2433 */
2434 i = qf_lnum;
2435 if (i > 0)
2436 {
2437 if (i > curbuf->b_ml.ml_line_count)
2438 i = curbuf->b_ml.ml_line_count;
2439 curwin->w_cursor.lnum = i;
2440 }
2441 if (qf_col > 0)
2442 {
2443 curwin->w_cursor.col = qf_col - 1;
2444#ifdef FEAT_VIRTUALEDIT
2445 curwin->w_cursor.coladd = 0;
2446#endif
2447 if (qf_viscol == TRUE)
2448 {
2449 /*
2450 * Check each character from the beginning of the error
2451 * line up to the error column. For each tab character
2452 * found, reduce the error column value by the length of
2453 * a tab character.
2454 */
2455 line = ml_get_curline();
2456 screen_col = 0;
2457 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2458 {
2459 if (*line == NUL)
2460 break;
2461 if (*line++ == '\t')
2462 {
2463 curwin->w_cursor.col -= 7 - (screen_col % 8);
2464 screen_col += 8 - (screen_col % 8);
2465 }
2466 else
2467 ++screen_col;
2468 }
2469 }
2470 check_cursor();
2471 }
2472 else
2473 beginline(BL_WHITE | BL_FIX);
2474 }
2475 else
2476 {
2477 pos_T save_cursor;
2478
2479 /* Move the cursor to the first line in the buffer */
2480 save_cursor = curwin->w_cursor;
2481 curwin->w_cursor.lnum = 0;
2482 if (!do_search(NULL, '/', qf_pattern, (long)1,
2483 SEARCH_KEEP, NULL, NULL))
2484 curwin->w_cursor = save_cursor;
2485 }
2486}
2487
2488/*
2489 * Display quickfix list index and size message
2490 */
2491 static void
2492qf_jump_print_msg(
2493 qf_info_T *qi,
2494 int qf_index,
2495 qfline_T *qf_ptr,
2496 buf_T *old_curbuf,
2497 linenr_T old_lnum)
2498{
2499 linenr_T i;
2500 int len;
2501
2502 /* Update the screen before showing the message, unless the screen
2503 * scrolled up. */
2504 if (!msg_scrolled)
2505 update_topline_redraw();
2506 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2507 qi->qf_lists[qi->qf_curlist].qf_count,
2508 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2509 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2510 /* Add the message, skipping leading whitespace and newlines. */
2511 len = (int)STRLEN(IObuff);
2512 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2513
2514 /* Output the message. Overwrite to avoid scrolling when the 'O'
2515 * flag is present in 'shortmess'; But when not jumping, print the
2516 * whole message. */
2517 i = msg_scroll;
2518 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2519 msg_scroll = TRUE;
2520 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2521 msg_scroll = FALSE;
2522 msg_attr_keep(IObuff, 0, TRUE);
2523 msg_scroll = i;
2524}
2525
2526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 * jump to a quickfix line
2528 * if dir == FORWARD go "errornr" valid entries forward
2529 * if dir == BACKWARD go "errornr" valid entries backward
2530 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2531 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2532 * else if "errornr" is zero, redisplay the same line
2533 * else go to entry "errornr"
2534 */
2535 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002536qf_jump(qf_info_T *qi,
2537 int dir,
2538 int errornr,
2539 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002541 qfline_T *qf_ptr;
2542 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 buf_T *old_curbuf;
2546 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002547 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002548 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002550 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552#ifdef FEAT_FOLDING
2553 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2554#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002555 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002557 if (qi == NULL)
2558 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002559
2560 if (qi->qf_curlist >= qi->qf_listcount
2561 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 EMSG(_(e_quickfix));
2564 return;
2565 }
2566
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002567 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002569 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002571 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002573 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2574 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002576 qf_ptr = old_qf_ptr;
2577 qf_index = old_qf_index;
2578 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580 }
2581 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002582 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002584 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2585 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 /* No need to print the error message if it's visible in the error
2587 * window */
2588 print_message = FALSE;
2589
2590 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002591 * For ":helpgrep" find a help window or open one.
2592 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002593 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002594 if (jump_to_help_window(qi, &opened_window) == FAIL)
2595 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002596
2597 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 * If currently in the quickfix window, find another window to show the
2599 * file in.
2600 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002601 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
2603 /*
2604 * If there is no file specified, we don't know where to go.
2605 * But do advance, otherwise ":cn" gets stuck.
2606 */
2607 if (qf_ptr->qf_fnum == 0)
2608 goto theend;
2609
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002610 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2611 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
2614 /*
2615 * If there is a file name,
2616 * read the wanted file if needed, and check autowrite etc.
2617 */
2618 old_curbuf = curbuf;
2619 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002620
2621 if (qf_ptr->qf_fnum != 0)
2622 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002623 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002624
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002625 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2626 &opened_window, &abort);
2627 if (abort)
2628 {
2629 qi = NULL;
2630 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002631 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002632 }
2633
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002634 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 /* When not switched to another buffer, still need to set pc mark */
2637 if (curbuf == old_curbuf)
2638 setpcmark();
2639
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002640 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2641 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
2643#ifdef FEAT_FOLDING
2644 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2645 foldOpenCursor();
2646#endif
2647 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002648 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 else
2651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (opened_window)
2653 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002654 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
2656 /*
2657 * Couldn't open file, so put index back where it was. This could
2658 * happen if the file was readonly and we changed something.
2659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 qf_ptr = old_qf_ptr;
2662 qf_index = old_qf_index;
2663 }
2664 }
2665theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002666 if (qi != NULL)
2667 {
2668 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2669 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (p_swb != old_swb && opened_window)
2672 {
2673 /* Restore old 'switchbuf' value, but not when an autocommand or
2674 * modeline has changed the value. */
2675 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002676 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002678 swb_flags = old_swb_flags;
2679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 else
2681 free_string_option(old_swb);
2682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683}
2684
2685/*
2686 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002687 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
2689 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002690qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002692 buf_T *buf;
2693 char_u *fname;
2694 qfline_T *qfp;
2695 int i;
2696 int idx1 = 1;
2697 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002698 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002699 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002700 int qfFileAttr;
2701 int qfSepAttr;
2702 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002703 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002705 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002707 if (eap->cmdidx == CMD_llist)
2708 {
2709 qi = GET_LOC_LIST(curwin);
2710 if (qi == NULL)
2711 {
2712 EMSG(_(e_loclist));
2713 return;
2714 }
2715 }
2716
2717 if (qi->qf_curlist >= qi->qf_listcount
2718 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 EMSG(_(e_quickfix));
2721 return;
2722 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002723 if (*arg == '+')
2724 {
2725 ++arg;
2726 plus = TRUE;
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2729 {
2730 EMSG(_(e_trailing));
2731 return;
2732 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002733 if (plus)
2734 {
2735 i = qi->qf_lists[qi->qf_curlist].qf_index;
2736 idx2 = i + idx1;
2737 idx1 = i;
2738 }
2739 else
2740 {
2741 i = qi->qf_lists[qi->qf_curlist].qf_count;
2742 if (idx1 < 0)
2743 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2744 if (idx2 < 0)
2745 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002748 /*
2749 * Get the attributes for the different quickfix highlight items. Note
2750 * that this depends on syntax items defined in the qf.vim syntax file
2751 */
2752 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2753 if (qfFileAttr == 0)
2754 qfFileAttr = HL_ATTR(HLF_D);
2755 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2756 if (qfSepAttr == 0)
2757 qfSepAttr = HL_ATTR(HLF_D);
2758 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2759 if (qfLineAttr == 0)
2760 qfLineAttr = HL_ATTR(HLF_N);
2761
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002762 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002764 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2765 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
2767 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2768 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002769 msg_putchar('\n');
2770 if (got_int)
2771 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002772
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002773 fname = NULL;
2774 if (qfp->qf_fnum != 0
2775 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2776 {
2777 fname = buf->b_fname;
2778 if (qfp->qf_type == 1) /* :helpgrep */
2779 fname = gettail(fname);
2780 }
2781 if (fname == NULL)
2782 sprintf((char *)IObuff, "%2d", i);
2783 else
2784 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2785 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002786 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002787 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2788
2789 if (qfp->qf_lnum != 0)
2790 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002791 if (qfp->qf_lnum == 0)
2792 IObuff[0] = NUL;
2793 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002794 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002796 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002797 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002798 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002800 msg_puts_attr(IObuff, qfLineAttr);
2801 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002802 if (qfp->qf_pattern != NULL)
2803 {
2804 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002805 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002806 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002807 }
2808 msg_puts((char_u *)" ");
2809
2810 /* Remove newlines and leading whitespace from the text. For an
2811 * unrecognized line keep the indent, the compiler may mark a word
2812 * with ^^^^. */
2813 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2815 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816 msg_prt_line(IObuff, FALSE);
2817 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002819
2820 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002821 if (qfp == NULL)
2822 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002823 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 ui_breakcheck();
2825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
2827
2828/*
2829 * Remove newlines and leading whitespace from an error message.
2830 * Put the result in "buf[bufsize]".
2831 */
2832 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002833qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 int i;
2836 char_u *p = text;
2837
2838 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2839 {
2840 if (*p == '\n')
2841 {
2842 buf[i] = ' ';
2843 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002844 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 break;
2846 }
2847 else
2848 buf[i] = *p++;
2849 }
2850 buf[i] = NUL;
2851}
2852
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002853 static void
2854qf_msg(qf_info_T *qi, int which, char *lead)
2855{
2856 char *title = (char *)qi->qf_lists[which].qf_title;
2857 int count = qi->qf_lists[which].qf_count;
2858 char_u buf[IOSIZE];
2859
2860 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2861 lead,
2862 which + 1,
2863 qi->qf_listcount,
2864 count);
2865
2866 if (title != NULL)
2867 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002868 size_t len = STRLEN(buf);
2869
2870 if (len < 34)
2871 {
2872 vim_memset(buf + len, ' ', 34 - len);
2873 buf[34] = NUL;
2874 }
2875 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002876 }
2877 trunc_string(buf, buf, Columns - 1, IOSIZE);
2878 msg(buf);
2879}
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881/*
2882 * ":colder [count]": Up in the quickfix stack.
2883 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002884 * ":lolder [count]": Up in the location list stack.
2885 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 */
2887 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002888qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 int count;
2892
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002893 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2894 {
2895 qi = GET_LOC_LIST(curwin);
2896 if (qi == NULL)
2897 {
2898 EMSG(_(e_loclist));
2899 return;
2900 }
2901 }
2902
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 if (eap->addr_count != 0)
2904 count = eap->line2;
2905 else
2906 count = 1;
2907 while (count--)
2908 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002909 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
2913 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002914 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002916 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 else
2919 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002920 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002923 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002925 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002928 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002929 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930}
2931
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002932 void
2933qf_history(exarg_T *eap)
2934{
2935 qf_info_T *qi = &ql_info;
2936 int i;
2937
2938 if (eap->cmdidx == CMD_lhistory)
2939 qi = GET_LOC_LIST(curwin);
2940 if (qi == NULL || (qi->qf_listcount == 0
2941 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2942 MSG(_("No entries"));
2943 else
2944 for (i = 0; i < qi->qf_listcount; ++i)
2945 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2946}
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002949 * Free all the entries in the error list "idx". Note that other information
2950 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 */
2952 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002953qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002955 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002956 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002957 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002958 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002960 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002962 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002963 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002964 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002965 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002966 vim_free(qfp->qf_text);
2967 stop = (qfp == qfpnext);
2968 vim_free(qfp->qf_pattern);
2969 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002970 if (stop)
2971 /* Somehow qf_count may have an incorrect value, set it to 1
2972 * to avoid crashing when it's wrong.
2973 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002974 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002975 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002976 qfl->qf_start = qfpnext;
2977 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002979
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002980 qfl->qf_index = 0;
2981 qfl->qf_start = NULL;
2982 qfl->qf_last = NULL;
2983 qfl->qf_ptr = NULL;
2984 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002985
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002986 qf_clean_dir_stack(&qfl->qf_dir_stack);
2987 qfl->qf_directory = NULL;
2988 qf_clean_dir_stack(&qfl->qf_file_stack);
2989 qfl->qf_currfile = NULL;
2990 qfl->qf_multiline = FALSE;
2991 qfl->qf_multiignore = FALSE;
2992 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993}
2994
2995/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002996 * Free error list "idx". Frees all the entries in the quickfix list,
2997 * associated context information and the title.
2998 */
2999 static void
3000qf_free(qf_info_T *qi, int idx)
3001{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003002 qf_list_T *qfl = &qi->qf_lists[idx];
3003
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003004 qf_free_items(qi, idx);
3005
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003006 vim_free(qfl->qf_title);
3007 qfl->qf_title = NULL;
3008 free_tv(qfl->qf_ctx);
3009 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003010 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003011 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003012}
3013
3014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 * qf_mark_adjust: adjust marks
3016 */
3017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003018qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003019 win_T *wp,
3020 linenr_T line1,
3021 linenr_T line2,
3022 long amount,
3023 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003025 int i;
3026 qfline_T *qfp;
3027 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003028 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003029 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003030 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
Bram Moolenaarc1542742016-07-20 21:44:37 +02003032 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003033 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003034 if (wp != NULL)
3035 {
3036 if (wp->w_llist == NULL)
3037 return;
3038 qi = wp->w_llist;
3039 }
3040
3041 for (idx = 0; idx < qi->qf_listcount; ++idx)
3042 if (qi->qf_lists[idx].qf_count)
3043 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003044 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3045 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 if (qfp->qf_fnum == curbuf->b_fnum)
3047 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003048 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3050 {
3051 if (amount == MAXLNUM)
3052 qfp->qf_cleared = TRUE;
3053 else
3054 qfp->qf_lnum += amount;
3055 }
3056 else if (amount_after && qfp->qf_lnum > line2)
3057 qfp->qf_lnum += amount_after;
3058 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003059
3060 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003061 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062}
3063
3064/*
3065 * Make a nice message out of the error character and the error number:
3066 * char number message
3067 * e or E 0 " error"
3068 * w or W 0 " warning"
3069 * i or I 0 " info"
3070 * 0 0 ""
3071 * other 0 " c"
3072 * e or E n " error n"
3073 * w or W n " warning n"
3074 * i or I n " info n"
3075 * 0 n " error n"
3076 * other n " c n"
3077 * 1 x "" :helpgrep
3078 */
3079 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003080qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 static char_u buf[20];
3083 static char_u cc[3];
3084 char_u *p;
3085
3086 if (c == 'W' || c == 'w')
3087 p = (char_u *)" warning";
3088 else if (c == 'I' || c == 'i')
3089 p = (char_u *)" info";
3090 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3091 p = (char_u *)" error";
3092 else if (c == 0 || c == 1)
3093 p = (char_u *)"";
3094 else
3095 {
3096 cc[0] = ' ';
3097 cc[1] = c;
3098 cc[2] = NUL;
3099 p = cc;
3100 }
3101
3102 if (nr <= 0)
3103 return p;
3104
3105 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3106 return buf;
3107}
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109/*
3110 * ":cwindow": open the quickfix window if we have errors to display,
3111 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003112 * ":lwindow": open the location list window if we have locations to display,
3113 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 */
3115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003116ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003118 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 win_T *win;
3120
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003121 if (eap->cmdidx == CMD_lwindow)
3122 {
3123 qi = GET_LOC_LIST(curwin);
3124 if (qi == NULL)
3125 return;
3126 }
3127
3128 /* Look for an existing quickfix window. */
3129 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
3131 /*
3132 * If a quickfix window is open but we have no errors to display,
3133 * close the window. If a quickfix window is not open, then open
3134 * it if we have errors; otherwise, leave it closed.
3135 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003136 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003137 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003138 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 if (win != NULL)
3141 ex_cclose(eap);
3142 }
3143 else if (win == NULL)
3144 ex_copen(eap);
3145}
3146
3147/*
3148 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003149 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003152ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003154 win_T *win = NULL;
3155 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003157 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3158 {
3159 qi = GET_LOC_LIST(curwin);
3160 if (qi == NULL)
3161 return;
3162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003164 /* Find existing quickfix window and close it. */
3165 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (win != NULL)
3167 win_close(win, FALSE);
3168}
3169
3170/*
3171 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003172 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 */
3174 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003175ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003177 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003180 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003181 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003182 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003184 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3185 {
3186 qi = GET_LOC_LIST(curwin);
3187 if (qi == NULL)
3188 {
3189 EMSG(_(e_loclist));
3190 return;
3191 }
3192 }
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (eap->addr_count != 0)
3195 height = eap->line2;
3196 else
3197 height = QF_WINHEIGHT;
3198
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200#ifdef FEAT_GUI
3201 need_mouse_correct = TRUE;
3202#endif
3203
3204 /*
3205 * Find existing quickfix window, or open a new one.
3206 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003207 win = qf_find_win(qi);
3208
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003209 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003212 if (eap->addr_count != 0)
3213 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003214 if (cmdmod.split & WSP_VERT)
3215 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003216 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003217 win_setwidth(height);
3218 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003219 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003220 win_setheight(height);
3221 }
3222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 else
3224 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003225 int flags = 0;
3226
Bram Moolenaar9c102382006-05-03 21:26:49 +00003227 qf_buf = qf_find_buf(qi);
3228
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 /* The current window becomes the previous window afterwards. */
3230 win = curwin;
3231
Bram Moolenaar77642c02012-11-20 17:55:10 +01003232 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3233 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003234 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003235 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003236 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003237 /* Default is to open the window below the current window */
3238 if (cmdmod.split == 0)
3239 flags = WSP_BELOW;
3240 flags |= WSP_NEWLOC;
3241 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003243 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003245 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003247 /*
3248 * For the location list window, create a reference to the
3249 * location list from the window 'win'.
3250 */
3251 curwin->w_llist_ref = win->w_llist;
3252 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003254
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003255 if (oldwin != curwin)
3256 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003257 if (qf_buf != NULL)
3258 /* Use the existing quickfix buffer */
3259 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003260 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003261 else
3262 {
3263 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003264 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003265 /* switch off 'swapfile' */
3266 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3267 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003268 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003269 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003270 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003271#ifdef FEAT_DIFF
3272 curwin->w_p_diff = FALSE;
3273#endif
3274#ifdef FEAT_FOLDING
3275 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3276 OPT_LOCAL);
3277#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003280 /* Only set the height when still in the same tab page and there is no
3281 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003282 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 win_setheight(height);
3284 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3285 if (win_valid(win))
3286 prevwin = win;
3287 }
3288
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003289 qf_set_title_var(qi);
3290
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 /*
3292 * Fill the buffer with the quickfix list.
3293 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003294 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003296 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 curwin->w_cursor.col = 0;
3298 check_cursor();
3299 update_topline(); /* scroll to show the line */
3300}
3301
3302/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003303 * Move the cursor in the quickfix window to "lnum".
3304 */
3305 static void
3306qf_win_goto(win_T *win, linenr_T lnum)
3307{
3308 win_T *old_curwin = curwin;
3309
3310 curwin = win;
3311 curbuf = win->w_buffer;
3312 curwin->w_cursor.lnum = lnum;
3313 curwin->w_cursor.col = 0;
3314#ifdef FEAT_VIRTUALEDIT
3315 curwin->w_cursor.coladd = 0;
3316#endif
3317 curwin->w_curswant = 0;
3318 update_topline(); /* scroll to show the line */
3319 redraw_later(VALID);
3320 curwin->w_redr_status = TRUE; /* update ruler */
3321 curwin = old_curwin;
3322 curbuf = curwin->w_buffer;
3323}
3324
3325/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003326 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003327 */
3328 void
3329ex_cbottom(exarg_T *eap UNUSED)
3330{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003331 qf_info_T *qi = &ql_info;
3332 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003333
Bram Moolenaar537ef082016-07-09 17:56:19 +02003334 if (eap->cmdidx == CMD_lbottom)
3335 {
3336 qi = GET_LOC_LIST(curwin);
3337 if (qi == NULL)
3338 {
3339 EMSG(_(e_loclist));
3340 return;
3341 }
3342 }
3343
3344 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003345 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3346 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3347}
3348
3349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 * Return the number of the current entry (line number in the quickfix
3351 * window).
3352 */
3353 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003354qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003356 qf_info_T *qi = &ql_info;
3357
3358 if (IS_LL_WINDOW(wp))
3359 /* In the location list window, use the referenced location list */
3360 qi = wp->w_llist_ref;
3361
3362 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363}
3364
3365/*
3366 * Update the cursor position in the quickfix window to the current error.
3367 * Return TRUE if there is a quickfix window.
3368 */
3369 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003370qf_win_pos_update(
3371 qf_info_T *qi,
3372 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003375 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
3377 /*
3378 * Put the cursor on the current error in the quickfix window, so that
3379 * it's viewable.
3380 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003381 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (win != NULL
3383 && qf_index <= win->w_buffer->b_ml.ml_line_count
3384 && old_qf_index != qf_index)
3385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (qf_index > old_qf_index)
3387 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003388 win->w_redraw_top = old_qf_index;
3389 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
3391 else
3392 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003393 win->w_redraw_top = qf_index;
3394 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003396 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 return win != NULL;
3399}
3400
3401/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003402 * Check whether the given window is displaying the specified quickfix/location
3403 * list buffer
3404 */
3405 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003406is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003407{
3408 /*
3409 * A window displaying the quickfix buffer will have the w_llist_ref field
3410 * set to NULL.
3411 * A window displaying a location list buffer will have the w_llist_ref
3412 * pointing to the location list.
3413 */
3414 if (bt_quickfix(win->w_buffer))
3415 if ((qi == &ql_info && win->w_llist_ref == NULL)
3416 || (qi != &ql_info && win->w_llist_ref == qi))
3417 return TRUE;
3418
3419 return FALSE;
3420}
3421
3422/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003423 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003424 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003425 */
3426 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003427qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003428{
3429 win_T *win;
3430
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003431 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003432 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003433 return win;
3434 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003435}
3436
3437/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003438 * Find a quickfix buffer.
3439 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 */
3441 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003442qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003444 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003445 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
Bram Moolenaar9c102382006-05-03 21:26:49 +00003447 FOR_ALL_TAB_WINDOWS(tp, win)
3448 if (is_qf_win(win, qi))
3449 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003450
Bram Moolenaar9c102382006-05-03 21:26:49 +00003451 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452}
3453
3454/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003455 * Update the w:quickfix_title variable in the quickfix/location list window
3456 */
3457 static void
3458qf_update_win_titlevar(qf_info_T *qi)
3459{
3460 win_T *win;
3461 win_T *curwin_save;
3462
3463 if ((win = qf_find_win(qi)) != NULL)
3464 {
3465 curwin_save = curwin;
3466 curwin = win;
3467 qf_set_title_var(qi);
3468 curwin = curwin_save;
3469 }
3470}
3471
3472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 * Find the quickfix buffer. If it exists, update the contents.
3474 */
3475 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003476qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003479 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
3482 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003483 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (buf != NULL)
3485 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003486 linenr_T old_line_count = buf->b_ml.ml_line_count;
3487
3488 if (old_last == NULL)
3489 /* set curwin/curbuf to buf and save a few things */
3490 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
Bram Moolenaard823fa92016-08-12 16:29:27 +02003492 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003493
Bram Moolenaar864293a2016-06-02 13:40:04 +02003494 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003495 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003496
Bram Moolenaar864293a2016-06-02 13:40:04 +02003497 if (old_last == NULL)
3498 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003499 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003500
3501 /* restore curwin/curbuf and a few other things */
3502 aucmd_restbuf(&aco);
3503 }
3504
3505 /* Only redraw when added lines are visible. This avoids flickering
3506 * when the added lines are not visible. */
3507 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3508 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510}
3511
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003512/*
3513 * Set "w:quickfix_title" if "qi" has a title.
3514 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003515 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003516qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003517{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003518 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3519 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003520 qi->qf_lists[qi->qf_curlist].qf_title);
3521}
3522
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523/*
3524 * Fill current buffer with quickfix errors, replacing any previous contents.
3525 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003526 * If "old_last" is not NULL append the items after this one.
3527 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3528 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 */
3530 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003531qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003533 linenr_T lnum;
3534 qfline_T *qfp;
3535 buf_T *errbuf;
3536 int len;
3537 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
Bram Moolenaar864293a2016-06-02 13:40:04 +02003539 if (old_last == NULL)
3540 {
3541 if (buf != curbuf)
3542 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003543 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003544 return;
3545 }
3546
3547 /* delete all existing lines */
3548 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3549 (void)ml_delete((linenr_T)1, FALSE);
3550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003553 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
3555 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003556 if (old_last == NULL)
3557 {
3558 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3559 lnum = 0;
3560 }
3561 else
3562 {
3563 qfp = old_last->qf_next;
3564 lnum = buf->b_ml.ml_line_count;
3565 }
3566 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568 if (qfp->qf_fnum != 0
3569 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3570 && errbuf->b_fname != NULL)
3571 {
3572 if (qfp->qf_type == 1) /* :helpgrep */
3573 STRCPY(IObuff, gettail(errbuf->b_fname));
3574 else
3575 STRCPY(IObuff, errbuf->b_fname);
3576 len = (int)STRLEN(IObuff);
3577 }
3578 else
3579 len = 0;
3580 IObuff[len++] = '|';
3581
3582 if (qfp->qf_lnum > 0)
3583 {
3584 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3585 len += (int)STRLEN(IObuff + len);
3586
3587 if (qfp->qf_col > 0)
3588 {
3589 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3590 len += (int)STRLEN(IObuff + len);
3591 }
3592
3593 sprintf((char *)IObuff + len, "%s",
3594 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3595 len += (int)STRLEN(IObuff + len);
3596 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003597 else if (qfp->qf_pattern != NULL)
3598 {
3599 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3600 len += (int)STRLEN(IObuff + len);
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 IObuff[len++] = '|';
3603 IObuff[len++] = ' ';
3604
3605 /* Remove newlines and leading whitespace from the text.
3606 * For an unrecognized line keep the indent, the compiler may
3607 * mark a word with ^^^^. */
3608 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3609 IObuff + len, IOSIZE - len);
3610
Bram Moolenaar864293a2016-06-02 13:40:04 +02003611 if (ml_append_buf(buf, lnum, IObuff,
3612 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003614 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003616 if (qfp == NULL)
3617 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003619
3620 if (old_last == NULL)
3621 /* Delete the empty line which is now at the end */
3622 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624
3625 /* correct cursor position */
3626 check_lnums(TRUE);
3627
Bram Moolenaar864293a2016-06-02 13:40:04 +02003628 if (old_last == NULL)
3629 {
3630 /* Set the 'filetype' to "qf" each time after filling the buffer.
3631 * This resembles reading a file into a buffer, it's more logical when
3632 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003633#ifdef FEAT_AUTOCMD
3634 ++curbuf_lock;
3635#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003636 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3637 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003640 keep_filetype = TRUE; /* don't detect 'filetype' */
3641 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003643 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003645 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003646 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003648 /* make sure it will be redrawn */
3649 redraw_curbuf_later(NOT_VALID);
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
3652 /* Restore KeyTyped, setting 'filetype' may reset it. */
3653 KeyTyped = old_KeyTyped;
3654}
3655
Bram Moolenaarb254af32017-12-18 19:48:58 +01003656 static void
3657qf_list_changed(qf_info_T *qi, int qf_idx)
3658{
3659 qi->qf_lists[qf_idx].qf_changedtick++;
3660}
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003663 * Return TRUE when using ":vimgrep" for ":grep".
3664 */
3665 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003666grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003667{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003668 return ((cmdidx == CMD_grep
3669 || cmdidx == CMD_lgrep
3670 || cmdidx == CMD_grepadd
3671 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003672 && STRCMP("internal",
3673 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3674}
3675
3676/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003677 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 */
3679 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003680ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003682 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003684 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003686 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003687 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003688 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003689#ifdef FEAT_AUTOCMD
3690 char_u *au_name = NULL;
3691
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003692 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3693 if (grep_internal(eap->cmdidx))
3694 {
3695 ex_vimgrep(eap);
3696 return;
3697 }
3698
Bram Moolenaar7c626922005-02-07 22:01:03 +00003699 switch (eap->cmdidx)
3700 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003701 case CMD_make: au_name = (char_u *)"make"; break;
3702 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3703 case CMD_grep: au_name = (char_u *)"grep"; break;
3704 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3705 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3706 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003707 default: break;
3708 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003709 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3710 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003711 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003712# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003713 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003714 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003715# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003716 }
3717#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003718#ifdef FEAT_MBYTE
3719 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
Bram Moolenaara6557602006-02-04 22:43:20 +00003722 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3723 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003724 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003727 fname = get_mef_name();
3728 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003730 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
3732 /*
3733 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3734 */
3735 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3736 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003737 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 cmd = alloc(len);
3739 if (cmd == NULL)
3740 return;
3741 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3742 (char *)p_shq);
3743 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003744 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 /*
3746 * Output a newline if there's something else than the :make command that
3747 * was typed (in which case the cursor is in column 0).
3748 */
3749 if (msg_col == 0)
3750 msg_didout = FALSE;
3751 msg_start();
3752 MSG_PUTS(":!");
3753 msg_outtrans(cmd); /* show what we are doing */
3754
3755 /* let the shell know if we are redirecting output or not */
3756 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3757
3758#ifdef AMIGA
3759 out_flush();
3760 /* read window status report and redraw before message */
3761 (void)char_avail();
3762#endif
3763
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003764 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003765 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3766 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003767 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003768 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003769 if (wp != NULL)
3770 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003771 if (res >= 0 && qi != NULL)
3772 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003773#ifdef FEAT_AUTOCMD
3774 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003775 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003776 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3777 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003778 if (qi->qf_curlist < qi->qf_listcount)
3779 res = qi->qf_lists[qi->qf_curlist].qf_count;
3780 else
3781 res = 0;
3782 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003783#endif
3784 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003785 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
Bram Moolenaar7c626922005-02-07 22:01:03 +00003787 mch_remove(fname);
3788 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 vim_free(cmd);
3790}
3791
3792/*
3793 * Return the name for the errorfile, in allocated memory.
3794 * Find a new unique name when 'makeef' contains "##".
3795 * Returns NULL for error.
3796 */
3797 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003798get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
3800 char_u *p;
3801 char_u *name;
3802 static int start = -1;
3803 static int off = 0;
3804#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003805 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806#endif
3807
3808 if (*p_mef == NUL)
3809 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003810 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (name == NULL)
3812 EMSG(_(e_notmp));
3813 return name;
3814 }
3815
3816 for (p = p_mef; *p; ++p)
3817 if (p[0] == '#' && p[1] == '#')
3818 break;
3819
3820 if (*p == NUL)
3821 return vim_strsave(p_mef);
3822
3823 /* Keep trying until the name doesn't exist yet. */
3824 for (;;)
3825 {
3826 if (start == -1)
3827 start = mch_get_pid();
3828 else
3829 off += 19;
3830
3831 name = alloc((unsigned)STRLEN(p_mef) + 30);
3832 if (name == NULL)
3833 break;
3834 STRCPY(name, p_mef);
3835 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3836 STRCAT(name, p + 2);
3837 if (mch_getperm(name) < 0
3838#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003839 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 && mch_lstat((char *)name, &sb) < 0
3841#endif
3842 )
3843 break;
3844 vim_free(name);
3845 }
3846 return name;
3847}
3848
3849/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003850 * Returns the number of valid entries in the current quickfix/location list.
3851 */
3852 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003853qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003854{
3855 qf_info_T *qi = &ql_info;
3856 qfline_T *qfp;
3857 int i, sz = 0;
3858 int prev_fnum = 0;
3859
3860 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3861 {
3862 /* Location list */
3863 qi = GET_LOC_LIST(curwin);
3864 if (qi == NULL)
3865 return 0;
3866 }
3867
3868 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003869 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003870 ++i, qfp = qfp->qf_next)
3871 {
3872 if (qfp->qf_valid)
3873 {
3874 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3875 sz++; /* Count all valid entries */
3876 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3877 {
3878 /* Count the number of files */
3879 sz++;
3880 prev_fnum = qfp->qf_fnum;
3881 }
3882 }
3883 }
3884
3885 return sz;
3886}
3887
3888/*
3889 * Returns the current index of the quickfix/location list.
3890 * Returns 0 if there is an error.
3891 */
3892 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003893qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003894{
3895 qf_info_T *qi = &ql_info;
3896
3897 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3898 {
3899 /* Location list */
3900 qi = GET_LOC_LIST(curwin);
3901 if (qi == NULL)
3902 return 0;
3903 }
3904
3905 return qi->qf_lists[qi->qf_curlist].qf_index;
3906}
3907
3908/*
3909 * Returns the current index in the quickfix/location list (counting only valid
3910 * entries). If no valid entries are in the list, then returns 1.
3911 */
3912 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003913qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003914{
3915 qf_info_T *qi = &ql_info;
3916 qf_list_T *qfl;
3917 qfline_T *qfp;
3918 int i, eidx = 0;
3919 int prev_fnum = 0;
3920
3921 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3922 {
3923 /* Location list */
3924 qi = GET_LOC_LIST(curwin);
3925 if (qi == NULL)
3926 return 1;
3927 }
3928
3929 qfl = &qi->qf_lists[qi->qf_curlist];
3930 qfp = qfl->qf_start;
3931
3932 /* check if the list has valid errors */
3933 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3934 return 1;
3935
3936 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3937 {
3938 if (qfp->qf_valid)
3939 {
3940 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3941 {
3942 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3943 {
3944 /* Count the number of files */
3945 eidx++;
3946 prev_fnum = qfp->qf_fnum;
3947 }
3948 }
3949 else
3950 eidx++;
3951 }
3952 }
3953
3954 return eidx ? eidx : 1;
3955}
3956
3957/*
3958 * Get the 'n'th valid error entry in the quickfix or location list.
3959 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3960 * For :cdo and :ldo returns the 'n'th valid error entry.
3961 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3962 */
3963 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003964qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003965{
3966 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3967 qfline_T *qfp = qfl->qf_start;
3968 int i, eidx;
3969 int prev_fnum = 0;
3970
3971 /* check if the list has valid errors */
3972 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3973 return 1;
3974
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003975 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003976 i++, qfp = qfp->qf_next)
3977 {
3978 if (qfp->qf_valid)
3979 {
3980 if (fdo)
3981 {
3982 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3983 {
3984 /* Count the number of files */
3985 eidx++;
3986 prev_fnum = qfp->qf_fnum;
3987 }
3988 }
3989 else
3990 eidx++;
3991 }
3992
3993 if (eidx == n)
3994 break;
3995 }
3996
3997 if (i <= qfl->qf_count)
3998 return i;
3999 else
4000 return 1;
4001}
4002
4003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004005 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004006 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 */
4008 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004009ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004011 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004012 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004013
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004014 if (eap->cmdidx == CMD_ll
4015 || eap->cmdidx == CMD_lrewind
4016 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004017 || eap->cmdidx == CMD_llast
4018 || eap->cmdidx == CMD_ldo
4019 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004020 {
4021 qi = GET_LOC_LIST(curwin);
4022 if (qi == NULL)
4023 {
4024 EMSG(_(e_loclist));
4025 return;
4026 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004027 }
4028
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004029 if (eap->addr_count > 0)
4030 errornr = (int)eap->line2;
4031 else
4032 {
4033 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4034 errornr = 0;
4035 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4036 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4037 errornr = 1;
4038 else
4039 errornr = 32767;
4040 }
4041
4042 /* For cdo and ldo commands, jump to the nth valid error.
4043 * For cfdo and lfdo commands, jump to the nth valid file entry.
4044 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004045 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4046 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004047 errornr = qf_get_nth_valid_entry(qi,
4048 eap->addr_count > 0 ? (int)eap->line1 : 1,
4049 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4050
4051 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052}
4053
4054/*
4055 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004056 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004057 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 */
4059 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004060ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004062 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004063 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004064
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004065 if (eap->cmdidx == CMD_lnext
4066 || eap->cmdidx == CMD_lNext
4067 || eap->cmdidx == CMD_lprevious
4068 || eap->cmdidx == CMD_lnfile
4069 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004070 || eap->cmdidx == CMD_lpfile
4071 || eap->cmdidx == CMD_ldo
4072 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004073 {
4074 qi = GET_LOC_LIST(curwin);
4075 if (qi == NULL)
4076 {
4077 EMSG(_(e_loclist));
4078 return;
4079 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004080 }
4081
Bram Moolenaar55b69262017-08-13 13:42:01 +02004082 if (eap->addr_count > 0
4083 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4084 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004085 errornr = (int)eap->line2;
4086 else
4087 errornr = 1;
4088
4089 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4090 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004092 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4093 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004095 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4096 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 ? BACKWARD_FILE
4098 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004099 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100}
4101
4102/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004103 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004104 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 */
4106 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004107ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004109 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004110 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004111 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004112#ifdef FEAT_AUTOCMD
4113 char_u *au_name = NULL;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004114 int save_qfid;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004115#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004116 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004117
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004118#ifdef FEAT_AUTOCMD
4119 switch (eap->cmdidx)
4120 {
4121 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4122 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4123 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4124 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4125 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4126 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4127 default: break;
4128 }
4129 if (au_name != NULL)
4130 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
4131#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004132#ifdef FEAT_MBYTE
4133 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4134#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004135#ifdef FEAT_BROWSE
4136 if (cmdmod.browse)
4137 {
4138 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4139 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4140 if (browse_file == NULL)
4141 return;
4142 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4143 vim_free(browse_file);
4144 }
4145 else
4146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004148 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004149
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004150 if (eap->cmdidx == CMD_lfile
4151 || eap->cmdidx == CMD_lgetfile
4152 || eap->cmdidx == CMD_laddfile)
4153 wp = curwin;
4154
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004155 /*
4156 * This function is used by the :cfile, :cgetfile and :caddfile
4157 * commands.
4158 * :cfile always creates a new quickfix list and jumps to the
4159 * first error.
4160 * :cgetfile creates a new quickfix list but doesn't jump to the
4161 * first error.
4162 * :caddfile adds to an existing quickfix list. If there is no
4163 * quickfix list then a new list is created.
4164 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004165 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4166 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004167 if (wp != NULL)
4168 qi = GET_LOC_LIST(wp);
4169 if (res >= 0 && qi != NULL)
4170 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004171#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004172 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004173 if (au_name != NULL)
4174 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004175 /*
4176 * Autocmd might have freed the quickfix/location list. Check whether it is
4177 * still valid
4178 */
4179 if (!qflist_valid(wp, save_qfid))
4180 return;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004181#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004182 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
4183 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004184 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186}
4187
4188/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004189 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004190 * ":vimgrepadd {pattern} file(s)"
4191 * ":lvimgrep {pattern} file(s)"
4192 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004193 */
4194 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004195ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004196{
Bram Moolenaar81695252004-12-29 20:58:21 +00004197 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004198 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004199 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004200 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004201 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004202 char_u *s;
4203 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004204 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004205 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004206 int loclist_cmd = FALSE;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004207#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004208 int_u save_qfid;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004209 qfline_T *cur_qf_start;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004210 win_T *wp;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004211#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004212 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004213 buf_T *buf;
4214 int duplicate_name = FALSE;
4215 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004216 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004217 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004218 buf_T *first_match_buf = NULL;
4219 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004220 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004221#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4222 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004223#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004224 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004225 int flags = 0;
4226 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004227 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004228 char_u *dirname_start = NULL;
4229 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004230 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004231#ifdef FEAT_AUTOCMD
4232 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004233
4234 switch (eap->cmdidx)
4235 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004236 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4237 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4238 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004239 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004240 case CMD_grep: au_name = (char_u *)"grep"; break;
4241 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4242 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4243 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004244 default: break;
4245 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004246 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4247 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004248 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004249# ifdef FEAT_EVAL
4250 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004251 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004252# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004253 }
4254#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004255
Bram Moolenaar754b5602006-02-09 23:53:20 +00004256 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004257 || eap->cmdidx == CMD_lvimgrep
4258 || eap->cmdidx == CMD_lgrepadd
4259 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004260 {
4261 qi = ll_get_or_alloc_list(curwin);
4262 if (qi == NULL)
4263 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004264 loclist_cmd = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00004265 }
4266
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004267 if (eap->addr_count > 0)
4268 tomatch = eap->line2;
4269 else
4270 tomatch = MAXLNUM;
4271
Bram Moolenaar81695252004-12-29 20:58:21 +00004272 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004273 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004274 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004275 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004276 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004277 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004278 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004279 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004280 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004281
4282 if (s != NULL && *s == NUL)
4283 {
4284 /* Pattern is empty, use last search pattern. */
4285 if (last_search_pat() == NULL)
4286 {
4287 EMSG(_(e_noprevre));
4288 goto theend;
4289 }
4290 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4291 }
4292 else
4293 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4294
Bram Moolenaar86b68352004-12-27 21:59:20 +00004295 if (regmatch.regprog == NULL)
4296 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004297 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004298 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004299
4300 p = skipwhite(p);
4301 if (*p == NUL)
4302 {
4303 EMSG(_("E683: File name missing or invalid pattern"));
4304 goto theend;
4305 }
4306
Bram Moolenaar55b69262017-08-13 13:42:01 +02004307 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4308 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004309 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004310 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004311 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004312
4313 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004314 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004315 goto theend;
4316 if (fcount == 0)
4317 {
4318 EMSG(_(e_nomatch));
4319 goto theend;
4320 }
4321
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004322 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4323 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004324 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004325 {
4326 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004327 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004328 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004329
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004330 /* Remember the current directory, because a BufRead autocommand that does
4331 * ":lcd %:p:h" changes the meaning of short path names. */
4332 mch_dirname(dirname_start, MAXPATHL);
4333
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004334#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004335 /* Remember the current values of the quickfix list and qf_start, so that
4336 * we can check for autocommands changing the current quickfix list. */
4337 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004338 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4339#endif
4340
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004341 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004342 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004343 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004344 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004345 if (time(NULL) > seconds)
4346 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004347 /* Display the file name every second or so, show the user we are
4348 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004349 seconds = time(NULL);
4350 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004351 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004352 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004353 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004354 else
4355 {
4356 msg_outtrans(p);
4357 vim_free(p);
4358 }
4359 msg_clr_eos();
4360 msg_didout = FALSE; /* overwrite this message */
4361 msg_nowait = TRUE; /* don't wait for this message */
4362 msg_col = 0;
4363 out_flush();
4364 }
4365
Bram Moolenaar81695252004-12-29 20:58:21 +00004366 buf = buflist_findname_exp(fnames[fi]);
4367 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4368 {
4369 /* Remember that a buffer with this name already exists. */
4370 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004371 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004372 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004373
4374#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4375 /* Don't do Filetype autocommands to avoid loading syntax and
4376 * indent scripts, a great speed improvement. */
4377 save_ei = au_event_disable(",Filetype");
4378#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004379 /* Don't use modelines here, it's useless. */
4380 save_mls = p_mls;
4381 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004382
4383 /* Load file into a buffer, so that 'fileencoding' is detected,
4384 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004385 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004386
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004387 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004388#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4389 au_event_restore(save_ei);
4390#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004391 }
4392 else
4393 /* Use existing, loaded buffer. */
4394 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004395
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004396#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004397 if (loclist_cmd)
4398 {
4399 /*
4400 * Verify that the location list is still valid. An autocmd might
4401 * have freed the location list.
4402 */
4403 if (!qflist_valid(curwin, save_qfid))
4404 {
4405 EMSG(_(e_loc_list_changed));
4406 goto theend;
4407 }
4408 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004409 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4410 {
4411 int idx;
4412
4413 /* Autocommands changed the quickfix list. Find the one we were
4414 * using and restore it. */
4415 for (idx = 0; idx < LISTCOUNT; ++idx)
4416 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4417 {
4418 qi->qf_curlist = idx;
4419 break;
4420 }
4421 if (idx == LISTCOUNT)
4422 {
4423 /* List cannot be found, create a new one. */
4424 qf_new_list(qi, *eap->cmdlinep);
4425 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4426 }
4427 }
4428#endif
4429
Bram Moolenaar81695252004-12-29 20:58:21 +00004430 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004431 {
4432 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004433 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004434 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004435 else
4436 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004437 /* Try for a match in all lines of the buffer.
4438 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004439 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004440 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4441 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004442 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004443 col = 0;
4444 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004445 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004446 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004447 /* Pass the buffer number so that it gets used even for a
4448 * dummy buffer, unless duplicate_name is set, then the
4449 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004450 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004451 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004452 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004453 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004454 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004455 ml_get_buf(buf,
4456 regmatch.startpos[0].lnum + lnum, FALSE),
4457 regmatch.startpos[0].lnum + lnum,
4458 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004459 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004460 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004461 0, /* nr */
4462 0, /* type */
4463 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004464 ) == FAIL)
4465 {
4466 got_int = TRUE;
4467 break;
4468 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004469 found_match = TRUE;
4470 if (--tomatch == 0)
4471 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004472 if ((flags & VGR_GLOBAL) == 0
4473 || regmatch.endpos[0].lnum > 0)
4474 break;
4475 col = regmatch.endpos[0].col
4476 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004477 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004478 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004479 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004480 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004481 if (got_int)
4482 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004483 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004484#ifdef FEAT_AUTOCMD
4485 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4486#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004487
4488 if (using_dummy)
4489 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004490 if (found_match && first_match_buf == NULL)
4491 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004492 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004493 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004494 /* Never keep a dummy buffer if there is another buffer
4495 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004496 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004497 buf = NULL;
4498 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004499 else if (!cmdmod.hide
4500 || buf->b_p_bh[0] == 'u' /* "unload" */
4501 || buf->b_p_bh[0] == 'w' /* "wipe" */
4502 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004503 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004504 /* When no match was found we don't need to remember the
4505 * buffer, wipe it out. If there was a match and it
4506 * wasn't the first one or we won't jump there: only
4507 * unload the buffer.
4508 * Ignore 'hidden' here, because it may lead to having too
4509 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004510 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004511 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004512 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004513 buf = NULL;
4514 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004515 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004516 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004517 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004518 /* Keeping the buffer, remove the dummy flag. */
4519 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004520 buf = NULL;
4521 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004522 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004523
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004524 if (buf != NULL)
4525 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004526 /* Keeping the buffer, remove the dummy flag. */
4527 buf->b_flags &= ~BF_DUMMY;
4528
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004529 /* If the buffer is still loaded we need to use the
4530 * directory we jumped to below. */
4531 if (buf == first_match_buf
4532 && target_dir == NULL
4533 && STRCMP(dirname_start, dirname_now) != 0)
4534 target_dir = vim_strsave(dirname_now);
4535
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004536 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004537 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004538 * need to be done (again). But not the window-local
4539 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004540 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004541#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004542 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4543 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004544#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004545 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004546 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004547 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004548 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004549 }
4550 }
4551
4552 FreeWild(fcount, fnames);
4553
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004554 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4555 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4556 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004557 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004558
Bram Moolenaar864293a2016-06-02 13:40:04 +02004559 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004560
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004561#ifdef FEAT_AUTOCMD
4562 if (au_name != NULL)
4563 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4564 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004565 /*
4566 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4567 * is still valid.
4568 */
4569 wp = loclist_cmd ? curwin : NULL;
4570 if (!qflist_valid(wp, save_qfid))
4571 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004572#endif
4573
Bram Moolenaar86b68352004-12-27 21:59:20 +00004574 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004575 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004576 {
4577 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004578 {
4579 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004580 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004581 if (buf != curbuf)
4582 /* If we jumped to another buffer redrawing will already be
4583 * taken care of. */
4584 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004585
4586 /* Jump to the directory used after loading the buffer. */
4587 if (curbuf == first_match_buf && target_dir != NULL)
4588 {
4589 exarg_T ea;
4590
4591 ea.arg = target_dir;
4592 ea.cmdidx = CMD_lcd;
4593 ex_cd(&ea);
4594 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004595 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004596 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004597 else
4598 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004599
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004600 /* If we loaded a dummy buffer into the current window, the autocommands
4601 * may have messed up things, need to redraw and recompute folds. */
4602 if (redraw_for_dummy)
4603 {
4604#ifdef FEAT_FOLDING
4605 foldUpdateAll(curwin);
4606#else
4607 redraw_later(NOT_VALID);
4608#endif
4609 }
4610
Bram Moolenaar86b68352004-12-27 21:59:20 +00004611theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004612 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004613 vim_free(dirname_now);
4614 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004615 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004616 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004617}
4618
4619/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004620 * Restore current working directory to "dirname_start" if they differ, taking
4621 * into account whether it is set locally or globally.
4622 */
4623 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004624restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004625{
4626 char_u *dirname_now = alloc(MAXPATHL);
4627
4628 if (NULL != dirname_now)
4629 {
4630 mch_dirname(dirname_now, MAXPATHL);
4631 if (STRCMP(dirname_start, dirname_now) != 0)
4632 {
4633 /* If the directory has changed, change it back by building up an
4634 * appropriate ex command and executing it. */
4635 exarg_T ea;
4636
4637 ea.arg = dirname_start;
4638 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4639 ex_cd(&ea);
4640 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004641 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004642 }
4643}
4644
4645/*
4646 * Load file "fname" into a dummy buffer and return the buffer pointer,
4647 * placing the directory resulting from the buffer load into the
4648 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4649 * prior to calling this function. Restores directory to "dirname_start" prior
4650 * to returning, if autocmds or the 'autochdir' option have changed it.
4651 *
4652 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4653 * or wipe_dummy_buffer() later!
4654 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004655 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004656 */
4657 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004658load_dummy_buffer(
4659 char_u *fname,
4660 char_u *dirname_start, /* in: old directory */
4661 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004662{
4663 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004664 bufref_T newbufref;
4665 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004666 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004667 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004668 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004669
4670 /* Allocate a buffer without putting it in the buffer list. */
4671 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4672 if (newbuf == NULL)
4673 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004674 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004675
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004676 /* Init the options. */
4677 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4678
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004679 /* need to open the memfile before putting the buffer in a window */
4680 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004681 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004682 /* Make sure this buffer isn't wiped out by auto commands. */
4683 ++newbuf->b_locked;
4684
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004685 /* set curwin/curbuf to buf and save a few things */
4686 aucmd_prepbuf(&aco, newbuf);
4687
4688 /* Need to set the filename for autocommands. */
4689 (void)setfname(curbuf, fname, NULL, FALSE);
4690
Bram Moolenaar81695252004-12-29 20:58:21 +00004691 /* Create swap file now to avoid the ATTENTION message. */
4692 check_need_swap(TRUE);
4693
4694 /* Remove the "dummy" flag, otherwise autocommands may not
4695 * work. */
4696 curbuf->b_flags &= ~BF_DUMMY;
4697
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004698 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004699 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004700 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004701 NULL, READ_NEW | READ_DUMMY);
4702 --newbuf->b_locked;
4703 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004704 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004705 && !(curbuf->b_flags & BF_NEW))
4706 {
4707 failed = FALSE;
4708 if (curbuf != newbuf)
4709 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004710 /* Bloody autocommands changed the buffer! Can happen when
4711 * using netrw and editing a remote file. Use the current
4712 * buffer instead, delete the dummy one after restoring the
4713 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004714 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004715 newbuf = curbuf;
4716 }
4717 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004718
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004719 /* restore curwin/curbuf and a few other things */
4720 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004721 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4722 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004723
4724 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4725 * skip it. */
4726 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004727 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004728
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004729 /*
4730 * When autocommands/'autochdir' option changed directory: go back.
4731 * Let the caller know what the resulting dir was first, in case it is
4732 * important.
4733 */
4734 mch_dirname(resulting_dir, MAXPATHL);
4735 restore_start_dir(dirname_start);
4736
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004737 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004738 return NULL;
4739 if (failed)
4740 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004741 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004742 return NULL;
4743 }
4744 return newbuf;
4745}
4746
4747/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004748 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4749 * directory to "dirname_start" prior to returning, if autocmds or the
4750 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004751 */
4752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004753wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004754{
4755 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004756 {
4757#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4758 cleanup_T cs;
4759
4760 /* Reset the error/interrupt/exception state here so that aborting()
4761 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4762 * work when got_int is set. */
4763 enter_cleanup(&cs);
4764#endif
4765
Bram Moolenaar81695252004-12-29 20:58:21 +00004766 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004767
4768#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4769 /* Restore the error/interrupt/exception state if not discarded by a
4770 * new aborting error, interrupt, or uncaught exception. */
4771 leave_cleanup(&cs);
4772#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004773 /* When autocommands/'autochdir' option changed directory: go back. */
4774 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004775 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004776}
4777
4778/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004779 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4780 * directory to "dirname_start" prior to returning, if autocmds or the
4781 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004782 */
4783 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004784unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004785{
4786 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004787 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004788 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004789
4790 /* When autocommands/'autochdir' option changed directory: go back. */
4791 restore_start_dir(dirname_start);
4792 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004793}
4794
Bram Moolenaar05159a02005-02-26 23:04:13 +00004795#if defined(FEAT_EVAL) || defined(PROTO)
4796/*
4797 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004798 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004799 */
4800 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004801get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004802{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004803 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004804 dict_T *dict;
4805 char_u buf[2];
4806 qfline_T *qfp;
4807 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004808 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004809
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004810 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004811 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004812 qi = &ql_info;
4813 if (wp != NULL)
4814 {
4815 qi = GET_LOC_LIST(wp);
4816 if (qi == NULL)
4817 return FAIL;
4818 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004819 }
4820
Bram Moolenaard823fa92016-08-12 16:29:27 +02004821 if (qf_idx == -1)
4822 qf_idx = qi->qf_curlist;
4823
4824 if (qf_idx >= qi->qf_listcount
4825 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004826 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004827
Bram Moolenaard823fa92016-08-12 16:29:27 +02004828 qfp = qi->qf_lists[qf_idx].qf_start;
4829 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004830 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004831 /* Handle entries with a non-existing buffer number. */
4832 bufnum = qfp->qf_fnum;
4833 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4834 bufnum = 0;
4835
Bram Moolenaar05159a02005-02-26 23:04:13 +00004836 if ((dict = dict_alloc()) == NULL)
4837 return FAIL;
4838 if (list_append_dict(list, dict) == FAIL)
4839 return FAIL;
4840
4841 buf[0] = qfp->qf_type;
4842 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004843 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004844 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4845 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4846 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4847 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004848 || dict_add_nr_str(dict, "pattern", 0L,
4849 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4850 || dict_add_nr_str(dict, "text", 0L,
4851 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004852 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4853 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4854 return FAIL;
4855
4856 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004857 if (qfp == NULL)
4858 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004859 }
4860 return OK;
4861}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004862
4863/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004864 * Flags used by getqflist()/getloclist() to determine which fields to return.
4865 */
4866enum {
4867 QF_GETLIST_NONE = 0x0,
4868 QF_GETLIST_TITLE = 0x1,
4869 QF_GETLIST_ITEMS = 0x2,
4870 QF_GETLIST_NR = 0x4,
4871 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004872 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004873 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004874 QF_GETLIST_IDX = 0x40,
4875 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01004876 QF_GETLIST_TICK = 0x100,
4877 QF_GETLIST_ALL = 0x1FF
Bram Moolenaard823fa92016-08-12 16:29:27 +02004878};
4879
4880/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004881 * Parse text from 'di' and return the quickfix list items
4882 */
4883 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004884qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004885{
4886 int status = FAIL;
4887 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004888 char_u *errorformat = p_efm;
4889 dictitem_T *efm_di;
4890 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004891
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004892 /* Only a List value is supported */
4893 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004894 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004895 /* If errorformat is supplied then use it, otherwise use the 'efm'
4896 * option setting
4897 */
4898 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4899 {
4900 if (efm_di->di_tv.v_type != VAR_STRING ||
4901 efm_di->di_tv.vval.v_string == NULL)
4902 return FAIL;
4903 errorformat = efm_di->di_tv.vval.v_string;
4904 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004905
Bram Moolenaar36538222017-09-02 19:51:44 +02004906 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004907 if (l == NULL)
4908 return FAIL;
4909
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004910 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4911 if (qi != NULL)
4912 {
4913 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4914 qi->qf_refcount++;
4915
Bram Moolenaar36538222017-09-02 19:51:44 +02004916 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004917 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4918 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004919 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004920 qf_free(qi, 0);
4921 }
4922 free(qi);
4923 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004924 dict_add_list(retdict, "items", l);
4925 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004926 }
4927
4928 return status;
4929}
4930
4931/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004932 * Return the quickfix/location list number with the given identifier.
4933 * Returns -1 if list is not found.
4934 */
4935 static int
4936qf_id2nr(qf_info_T *qi, int_u qfid)
4937{
4938 int qf_idx;
4939
4940 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4941 if (qi->qf_lists[qf_idx].qf_id == qfid)
4942 return qf_idx;
4943 return -1;
4944}
4945
4946/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004947 * Return the quickfix/location list window identifier in the current tabpage.
4948 */
4949 static int
4950qf_winid(qf_info_T *qi)
4951{
4952 win_T *win;
4953
4954 /* The quickfix window can be opened even if the quickfix list is not set
4955 * using ":copen". This is not true for location lists. */
4956 if (qi == NULL)
4957 return 0;
4958 win = qf_find_win(qi);
4959 if (win != NULL)
4960 return win->w_id;
4961 return 0;
4962}
4963
4964/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004965 * Return quickfix/location list details (title) as a
4966 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4967 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004968 */
4969 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004970qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004971{
4972 qf_info_T *qi = &ql_info;
4973 int status = OK;
4974 int qf_idx;
4975 dictitem_T *di;
4976 int flags = QF_GETLIST_NONE;
4977
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004978 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004979 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004980
Bram Moolenaard823fa92016-08-12 16:29:27 +02004981 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004982 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004983
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004984 if (dict_find(what, (char_u *)"all", -1) != NULL)
4985 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004986
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004987 if (dict_find(what, (char_u *)"title", -1) != NULL)
4988 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004989
Bram Moolenaara6d48492017-12-12 22:45:31 +01004990 if (dict_find(what, (char_u *)"nr", -1) != NULL)
4991 flags |= QF_GETLIST_NR;
4992
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004993 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4994 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004995
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004996 if (dict_find(what, (char_u *)"context", -1) != NULL)
4997 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004998
Bram Moolenaara6d48492017-12-12 22:45:31 +01004999 if (dict_find(what, (char_u *)"id", -1) != NULL)
5000 flags |= QF_GETLIST_ID;
5001
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005002 if (dict_find(what, (char_u *)"items", -1) != NULL)
5003 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005004
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005005 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5006 flags |= QF_GETLIST_IDX;
5007
5008 if (dict_find(what, (char_u *)"size", -1) != NULL)
5009 flags |= QF_GETLIST_SIZE;
5010
Bram Moolenaarb254af32017-12-18 19:48:58 +01005011 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5012 flags |= QF_GETLIST_TICK;
5013
Bram Moolenaara6d48492017-12-12 22:45:31 +01005014 if (qi != NULL && qi->qf_listcount != 0)
5015 {
5016 qf_idx = qi->qf_curlist; /* default is the current list */
5017 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5018 {
5019 /* Use the specified quickfix/location list */
5020 if (di->di_tv.v_type == VAR_NUMBER)
5021 {
5022 /* for zero use the current list */
5023 if (di->di_tv.vval.v_number != 0)
5024 {
5025 qf_idx = di->di_tv.vval.v_number - 1;
5026 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
5027 qf_idx = -1;
5028 }
5029 }
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005030 else if (di->di_tv.v_type == VAR_STRING
5031 && di->di_tv.vval.v_string != NULL
5032 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005033 /* Get the last quickfix list number */
5034 qf_idx = qi->qf_listcount - 1;
5035 else
5036 qf_idx = -1;
5037 flags |= QF_GETLIST_NR;
5038 }
5039
5040 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5041 {
5042 /* Look for a list with the specified id */
5043 if (di->di_tv.v_type == VAR_NUMBER)
5044 {
5045 /*
5046 * For zero, use the current list or the list specifed by 'nr'
5047 */
5048 if (di->di_tv.vval.v_number != 0)
5049 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5050 flags |= QF_GETLIST_ID;
5051 }
5052 else
5053 qf_idx = -1;
5054 }
5055 }
5056
5057 /* List is not present or is empty */
5058 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
5059 {
5060 if (flags & QF_GETLIST_TITLE)
5061 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5062 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5063 {
5064 list_T *l = list_alloc();
5065 if (l != NULL)
5066 status = dict_add_list(retdict, "items", l);
5067 else
5068 status = FAIL;
5069 }
5070 if ((status == OK) && (flags & QF_GETLIST_NR))
5071 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5072 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005073 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005074 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5075 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5076 if ((status == OK) && (flags & QF_GETLIST_ID))
5077 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5078 if ((status == OK) && (flags & QF_GETLIST_IDX))
5079 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5080 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5081 status = dict_add_nr_str(retdict, "size", 0L, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005082 if ((status == OK) && (flags & QF_GETLIST_TICK))
5083 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005084
5085 return status;
5086 }
5087
Bram Moolenaard823fa92016-08-12 16:29:27 +02005088 if (flags & QF_GETLIST_TITLE)
5089 {
5090 char_u *t;
5091 t = qi->qf_lists[qf_idx].qf_title;
5092 if (t == NULL)
5093 t = (char_u *)"";
5094 status = dict_add_nr_str(retdict, "title", 0L, t);
5095 }
5096 if ((status == OK) && (flags & QF_GETLIST_NR))
5097 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5098 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005099 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005100 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5101 {
5102 list_T *l = list_alloc();
5103 if (l != NULL)
5104 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005105 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005106 dict_add_list(retdict, "items", l);
5107 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005108 else
5109 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005110 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005111
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005112 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5113 {
5114 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5115 {
5116 di = dictitem_alloc((char_u *)"context");
5117 if (di != NULL)
5118 {
5119 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005120 status = dict_add(retdict, di);
5121 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02005122 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005123 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005124 else
5125 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005126 }
5127 else
5128 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5129 }
5130
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005131 if ((status == OK) && (flags & QF_GETLIST_ID))
5132 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5133 NULL);
5134
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005135 if ((status == OK) && (flags & QF_GETLIST_IDX))
5136 {
5137 int idx = qi->qf_lists[qf_idx].qf_index;
5138 if (qi->qf_lists[qf_idx].qf_count == 0)
5139 /* For empty lists, qf_index is set to 1 */
5140 idx = 0;
5141 status = dict_add_nr_str(retdict, "idx", idx, NULL);
5142 }
5143
5144 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5145 status = dict_add_nr_str(retdict, "size",
5146 qi->qf_lists[qf_idx].qf_count, NULL);
5147
Bram Moolenaarb254af32017-12-18 19:48:58 +01005148 if ((status == OK) && (flags & QF_GETLIST_TICK))
5149 status = dict_add_nr_str(retdict, "changedtick",
5150 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5151
Bram Moolenaard823fa92016-08-12 16:29:27 +02005152 return status;
5153}
5154
5155/*
5156 * Add list of entries to quickfix/location list. Each list entry is
5157 * a dictionary with item information.
5158 */
5159 static int
5160qf_add_entries(
5161 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005162 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005163 list_T *list,
5164 char_u *title,
5165 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005166{
5167 listitem_T *li;
5168 dict_T *d;
5169 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005170 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005171 long lnum;
5172 int col, nr;
5173 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005174 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005175 int valid, status;
5176 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005177 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005178
Bram Moolenaara3921f42017-06-04 15:30:34 +02005179 if (action == ' ' || qf_idx == qi->qf_listcount)
5180 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005181 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005182 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005183 qf_idx = qi->qf_curlist;
5184 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005185 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005186 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005187 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005188 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005189 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005190 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005191 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005192 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005193
5194 for (li = list->lv_first; li != NULL; li = li->li_next)
5195 {
5196 if (li->li_tv.v_type != VAR_DICT)
5197 continue; /* Skip non-dict items */
5198
5199 d = li->li_tv.vval.v_dict;
5200 if (d == NULL)
5201 continue;
5202
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005203 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005204 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5205 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5206 col = (int)get_dict_number(d, (char_u *)"col");
5207 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5208 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005209 type = get_dict_string(d, (char_u *)"type", TRUE);
5210 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5211 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005212 if (text == NULL)
5213 text = vim_strsave((char_u *)"");
5214
5215 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005216 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005217 valid = FALSE;
5218
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005219 /* Mark entries with non-existing buffer number as not valid. Give the
5220 * error message only once. */
5221 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5222 {
5223 if (!did_bufnr_emsg)
5224 {
5225 did_bufnr_emsg = TRUE;
5226 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5227 }
5228 valid = FALSE;
5229 bufnum = 0;
5230 }
5231
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005232 /* If the 'valid' field is present it overrules the detected value. */
5233 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5234 valid = (int)get_dict_number(d, (char_u *)"valid");
5235
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005236 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005237 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005238 NULL, /* dir */
5239 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005240 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005241 text,
5242 lnum,
5243 col,
5244 vcol, /* vis_col */
5245 pattern, /* search pattern */
5246 nr,
5247 type == NULL ? NUL : *type,
5248 valid);
5249
5250 vim_free(filename);
5251 vim_free(pattern);
5252 vim_free(text);
5253 vim_free(type);
5254
5255 if (status == FAIL)
5256 {
5257 retval = FAIL;
5258 break;
5259 }
5260 }
5261
Bram Moolenaara3921f42017-06-04 15:30:34 +02005262 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005263 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005264 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005265 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005266 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005267 if (action != 'a')
5268 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005269 qi->qf_lists[qf_idx].qf_ptr =
5270 qi->qf_lists[qf_idx].qf_start;
5271 if (qi->qf_lists[qf_idx].qf_count > 0)
5272 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005273 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005274
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005275 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005276 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005277
5278 return retval;
5279}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005280
5281 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005282qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005283{
5284 dictitem_T *di;
5285 int retval = FAIL;
5286 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005287 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005288 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005289
5290 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5291 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005292
5293 qf_idx = qi->qf_curlist; /* default is the current list */
5294 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5295 {
5296 /* Use the specified quickfix/location list */
5297 if (di->di_tv.v_type == VAR_NUMBER)
5298 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005299 /* for zero use the current list */
5300 if (di->di_tv.vval.v_number != 0)
5301 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005302
Bram Moolenaar55b69262017-08-13 13:42:01 +02005303 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5304 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005305 /*
5306 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005307 * non-available list and add the new list at the end of the
5308 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005309 */
5310 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005311 qf_idx = qi->qf_listcount - 1;
5312 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005313 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005314 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005315 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005316 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005317 }
5318 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005319 && di->di_tv.vval.v_string != NULL
5320 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005321 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005322 if (qi->qf_listcount > 0)
5323 qf_idx = qi->qf_listcount - 1;
5324 else if (newlist)
5325 qf_idx = 0;
5326 else
5327 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005328 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005329 else
5330 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005331 }
5332
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005333 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5334 {
5335 /* Use the quickfix/location list with the specified id */
5336 if (di->di_tv.v_type == VAR_NUMBER)
5337 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005338 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5339 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005340 return FAIL; /* List not found */
5341 }
5342 else
5343 return FAIL;
5344 }
5345
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005346 if (newlist)
5347 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005348 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005349 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005350 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005351 }
5352
5353 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5354 {
5355 if (di->di_tv.v_type == VAR_STRING)
5356 {
5357 vim_free(qi->qf_lists[qf_idx].qf_title);
5358 qi->qf_lists[qf_idx].qf_title =
5359 get_dict_string(what, (char_u *)"title", TRUE);
5360 if (qf_idx == qi->qf_curlist)
5361 qf_update_win_titlevar(qi);
5362 retval = OK;
5363 }
5364 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005365
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005366 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5367 {
5368 if (di->di_tv.v_type == VAR_LIST)
5369 {
5370 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5371
5372 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5373 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005374 if (action == 'r')
5375 {
5376 /*
5377 * When replacing the quickfix list entries using
5378 * qf_add_entries(), the title is set with a ':' prefix.
5379 * Restore the title with the saved title.
5380 */
5381 vim_free(qi->qf_lists[qf_idx].qf_title);
5382 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5383 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005384 vim_free(title_save);
5385 }
5386 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005387
Bram Moolenaar36538222017-09-02 19:51:44 +02005388 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5389 {
5390 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5391 return FAIL;
5392 errorformat = di->di_tv.vval.v_string;
5393 }
5394
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005395 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005396 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005397 /* Only a List value is supported */
5398 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005399 {
5400 if (action == 'r')
5401 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005402 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005403 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5404 retval = OK;
5405 }
5406 else
5407 return FAIL;
5408 }
5409
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005410 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5411 {
5412 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005413
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005414 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005415 ctx = alloc_tv();
5416 if (ctx != NULL)
5417 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005418 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005419 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005420 }
5421
Bram Moolenaarb254af32017-12-18 19:48:58 +01005422 if (retval == OK)
5423 qf_list_changed(qi, qf_idx);
5424
Bram Moolenaard823fa92016-08-12 16:29:27 +02005425 return retval;
5426}
5427
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005428/*
5429 * Find the non-location list window with the specified location list.
5430 */
5431 static win_T *
5432find_win_with_ll(qf_info_T *qi)
5433{
5434 win_T *wp = NULL;
5435
5436 FOR_ALL_WINDOWS(wp)
5437 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5438 return wp;
5439
5440 return NULL;
5441}
5442
5443/*
5444 * Free the entire quickfix/location list stack.
5445 * If the quickfix/location list window is open, then clear it.
5446 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005447 static void
5448qf_free_stack(win_T *wp, qf_info_T *qi)
5449{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005450 win_T *qfwin = qf_find_win(qi);
5451 win_T *llwin = NULL;
5452 win_T *orig_wp = wp;
5453
5454 if (qfwin != NULL)
5455 {
5456 /* If the quickfix/location list window is open, then clear it */
5457 if (qi->qf_curlist < qi->qf_listcount)
5458 qf_free(qi, qi->qf_curlist);
5459 qf_update_buffer(qi, NULL);
5460 }
5461
5462 if (wp != NULL && IS_LL_WINDOW(wp))
5463 {
5464 /* If in the location list window, then use the non-location list
5465 * window with this location list (if present)
5466 */
5467 llwin = find_win_with_ll(qi);
5468 if (llwin != NULL)
5469 wp = llwin;
5470 }
5471
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005472 qf_free_all(wp);
5473 if (wp == NULL)
5474 {
5475 /* quickfix list */
5476 qi->qf_curlist = 0;
5477 qi->qf_listcount = 0;
5478 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005479 else if (IS_LL_WINDOW(orig_wp))
5480 {
5481 /* If the location list window is open, then create a new empty
5482 * location list */
5483 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005484
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005485 /* first free the list reference in the location list window */
5486 ll_free_all(&orig_wp->w_llist_ref);
5487
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005488 orig_wp->w_llist_ref = new_ll;
5489 if (llwin != NULL)
5490 {
5491 llwin->w_llist = new_ll;
5492 new_ll->qf_refcount++;
5493 }
5494 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005495}
5496
Bram Moolenaard823fa92016-08-12 16:29:27 +02005497/*
5498 * Populate the quickfix list with the items supplied in the list
5499 * of dictionaries. "title" will be copied to w:quickfix_title.
5500 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5501 */
5502 int
5503set_errorlist(
5504 win_T *wp,
5505 list_T *list,
5506 int action,
5507 char_u *title,
5508 dict_T *what)
5509{
5510 qf_info_T *qi = &ql_info;
5511 int retval = OK;
5512
5513 if (wp != NULL)
5514 {
5515 qi = ll_get_or_alloc_list(wp);
5516 if (qi == NULL)
5517 return FAIL;
5518 }
5519
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005520 if (action == 'f')
5521 {
5522 /* Free the entire quickfix or location list stack */
5523 qf_free_stack(wp, qi);
5524 }
5525 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005526 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005527 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005528 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005529 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005530 if (retval == OK)
5531 qf_list_changed(qi, qi->qf_curlist);
5532 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005533
5534 return retval;
5535}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005536
5537 static int
5538mark_quickfix_ctx(qf_info_T *qi, int copyID)
5539{
5540 int i;
5541 int abort = FALSE;
5542 typval_T *ctx;
5543
5544 for (i = 0; i < LISTCOUNT && !abort; ++i)
5545 {
5546 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005547 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5548 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005549 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5550 }
5551
5552 return abort;
5553}
5554
5555/*
5556 * Mark the context of the quickfix list and the location lists (if present) as
5557 * "in use". So that garabage collection doesn't free the context.
5558 */
5559 int
5560set_ref_in_quickfix(int copyID)
5561{
5562 int abort = FALSE;
5563 tabpage_T *tp;
5564 win_T *win;
5565
5566 abort = mark_quickfix_ctx(&ql_info, copyID);
5567 if (abort)
5568 return abort;
5569
5570 FOR_ALL_TAB_WINDOWS(tp, win)
5571 {
5572 if (win->w_llist != NULL)
5573 {
5574 abort = mark_quickfix_ctx(win->w_llist, copyID);
5575 if (abort)
5576 return abort;
5577 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005578 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5579 {
5580 /* In a location list window and none of the other windows is
5581 * referring to this location list. Mark the location list
5582 * context as still in use.
5583 */
5584 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5585 if (abort)
5586 return abort;
5587 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005588 }
5589
5590 return abort;
5591}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005592#endif
5593
Bram Moolenaar81695252004-12-29 20:58:21 +00005594/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005595 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005596 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005597 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005598 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005599 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005600 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005601 */
5602 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005603ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005604{
5605 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005606 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005607#ifdef FEAT_AUTOCMD
5608 char_u *au_name = NULL;
5609#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005610 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005611
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005612#ifdef FEAT_AUTOCMD
5613 switch (eap->cmdidx)
5614 {
5615 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5616 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5617 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5618 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5619 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5620 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5621 default: break;
5622 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005623 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5624 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005625 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005626# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005627 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005628 return;
5629# endif
5630 }
5631#endif
5632
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005633 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005634 if (eap->cmdidx == CMD_lbuffer
5635 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005636 || eap->cmdidx == CMD_laddbuffer)
5637 {
5638 qi = ll_get_or_alloc_list(curwin);
5639 if (qi == NULL)
5640 return;
5641 }
5642
Bram Moolenaar86b68352004-12-27 21:59:20 +00005643 if (*eap->arg == NUL)
5644 buf = curbuf;
5645 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5646 buf = buflist_findnr(atoi((char *)eap->arg));
5647 if (buf == NULL)
5648 EMSG(_(e_invarg));
5649 else if (buf->b_ml.ml_mfp == NULL)
5650 EMSG(_("E681: Buffer is not loaded"));
5651 else
5652 {
5653 if (eap->addr_count == 0)
5654 {
5655 eap->line1 = 1;
5656 eap->line2 = buf->b_ml.ml_line_count;
5657 }
5658 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5659 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5660 EMSG(_(e_invrange));
5661 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005662 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005663 char_u *qf_title = *eap->cmdlinep;
5664
5665 if (buf->b_sfname)
5666 {
5667 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5668 (char *)qf_title, (char *)buf->b_sfname);
5669 qf_title = IObuff;
5670 }
5671
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005672 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005673 (eap->cmdidx != CMD_caddbuffer
5674 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005675 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005676 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005677 if (res >= 0)
5678 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005679#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005680 if (au_name != NULL)
5681 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5682 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005683#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005684 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5685 eap->cmdidx == CMD_lbuffer))
5686 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005687 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005688 }
5689}
5690
Bram Moolenaar1e015462005-09-25 22:16:38 +00005691#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005692/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005693 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5694 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005695 */
5696 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005697ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005698{
5699 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005700 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005701#ifdef FEAT_AUTOCMD
5702 char_u *au_name = NULL;
5703#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005704 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005705
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005706#ifdef FEAT_AUTOCMD
5707 switch (eap->cmdidx)
5708 {
5709 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5710 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5711 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5712 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5713 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5714 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5715 default: break;
5716 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005717 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5718 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005719 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005720# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005721 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005722 return;
5723# endif
5724 }
5725#endif
5726
Bram Moolenaar3c097222017-12-21 20:54:49 +01005727 if (eap->cmdidx == CMD_lexpr
5728 || eap->cmdidx == CMD_lgetexpr
5729 || eap->cmdidx == CMD_laddexpr)
5730 {
5731 qi = ll_get_or_alloc_list(curwin);
5732 if (qi == NULL)
5733 return;
5734 }
5735
Bram Moolenaar4770d092006-01-12 23:22:24 +00005736 /* Evaluate the expression. When the result is a string or a list we can
5737 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005738 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005739 if (tv != NULL)
5740 {
5741 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5742 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5743 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005744 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005745 (eap->cmdidx != CMD_caddexpr
5746 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005747 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005748 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005749 if (res >= 0)
5750 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005751#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005752 if (au_name != NULL)
5753 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5754 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005755#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005756 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5757 eap->cmdidx == CMD_lexpr))
5758 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005759 }
5760 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005761 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005762 free_tv(tv);
5763 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005764}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005765#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005766
5767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 * ":helpgrep {pattern}"
5769 */
5770 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005771ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772{
5773 regmatch_T regmatch;
5774 char_u *save_cpo;
5775 char_u *p;
5776 int fcount;
5777 char_u **fnames;
5778 FILE *fd;
5779 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005781#ifdef FEAT_MULTI_LANG
5782 char_u *lang;
5783#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005784 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005785 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005786 int new_qi = FALSE;
5787 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005788#ifdef FEAT_AUTOCMD
5789 char_u *au_name = NULL;
5790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005792#ifdef FEAT_MULTI_LANG
5793 /* Check for a specified language */
5794 lang = check_help_lang(eap->arg);
5795#endif
5796
Bram Moolenaar73633f82012-01-20 13:39:07 +01005797#ifdef FEAT_AUTOCMD
5798 switch (eap->cmdidx)
5799 {
5800 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5801 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5802 default: break;
5803 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005804 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5805 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005806 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005807# ifdef FEAT_EVAL
5808 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005809 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005810# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005811 }
5812#endif
5813
5814 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5815 save_cpo = p_cpo;
5816 p_cpo = empty_option;
5817
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005818 if (eap->cmdidx == CMD_lhelpgrep)
5819 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005820 /* If the current window is a help window, then use it */
5821 if (bt_help(curwin->w_buffer))
5822 wp = curwin;
5823 else
5824 /* Find an existing help window */
5825 FOR_ALL_WINDOWS(wp)
5826 if (bt_help(wp->w_buffer))
5827 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005828
5829 if (wp == NULL) /* Help window not found */
5830 qi = NULL;
5831 else
5832 qi = wp->w_llist;
5833
5834 if (qi == NULL)
5835 {
5836 /* Allocate a new location list for help text matches */
5837 if ((qi = ll_new_list()) == NULL)
5838 return;
5839 new_qi = TRUE;
5840 }
5841 }
5842
Bram Moolenaaree85df32017-03-19 14:19:50 +01005843 /* Autocommands may change the list. Save it for later comparison */
5844 save_qi = qi;
5845
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5847 regmatch.rm_ic = FALSE;
5848 if (regmatch.regprog != NULL)
5849 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005850#ifdef FEAT_MBYTE
5851 vimconv_T vc;
5852
5853 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5854 * differs. */
5855 vc.vc_type = CONV_NONE;
5856 if (!enc_utf8)
5857 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5858#endif
5859
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005861 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863 /* Go through all directories in 'runtimepath' */
5864 p = p_rtp;
5865 while (*p != NUL && !got_int)
5866 {
5867 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5868
5869 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5870 add_pathsep(NameBuff);
5871 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5872 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5873 &fnames, EW_FILE|EW_SILENT) == OK
5874 && fcount > 0)
5875 {
5876 for (fi = 0; fi < fcount && !got_int; ++fi)
5877 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005878#ifdef FEAT_MULTI_LANG
5879 /* Skip files for a different language. */
5880 if (lang != NULL
5881 && STRNICMP(lang, fnames[fi]
5882 + STRLEN(fnames[fi]) - 3, 2) != 0
5883 && !(STRNICMP(lang, "en", 2) == 0
5884 && STRNICMP("txt", fnames[fi]
5885 + STRLEN(fnames[fi]) - 3, 3) == 0))
5886 continue;
5887#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005888 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 if (fd != NULL)
5890 {
5891 lnum = 1;
5892 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5893 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005894 char_u *line = IObuff;
5895#ifdef FEAT_MBYTE
5896 /* Convert a line if 'encoding' is not utf-8 and
5897 * the line contains a non-ASCII character. */
5898 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005899 && has_non_ascii(IObuff))
5900 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005901 line = string_convert(&vc, IObuff, NULL);
5902 if (line == NULL)
5903 line = IObuff;
5904 }
5905#endif
5906
5907 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005909 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
5911 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005912 while (l > 0 && line[l - 1] <= ' ')
5913 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005915 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005916 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 NULL, /* dir */
5918 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005919 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005920 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005922 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005923 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005924 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005925 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 0, /* nr */
5927 1, /* type */
5928 TRUE /* valid */
5929 ) == FAIL)
5930 {
5931 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005932#ifdef FEAT_MBYTE
5933 if (line != IObuff)
5934 vim_free(line);
5935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 break;
5937 }
5938 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005939#ifdef FEAT_MBYTE
5940 if (line != IObuff)
5941 vim_free(line);
5942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 ++lnum;
5944 line_breakcheck();
5945 }
5946 fclose(fd);
5947 }
5948 }
5949 FreeWild(fcount, fnames);
5950 }
5951 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005952
Bram Moolenaar473de612013-06-08 18:19:48 +02005953 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005954#ifdef FEAT_MBYTE
5955 if (vc.vc_type != CONV_NONE)
5956 convert_setup(&vc, NULL, NULL);
5957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005959 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5960 qi->qf_lists[qi->qf_curlist].qf_ptr =
5961 qi->qf_lists[qi->qf_curlist].qf_start;
5962 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 }
5964
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005965 if (p_cpo == empty_option)
5966 p_cpo = save_cpo;
5967 else
5968 /* Darn, some plugin changed the value. */
5969 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
Bram Moolenaarb254af32017-12-18 19:48:58 +01005971 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005972 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973
Bram Moolenaar73633f82012-01-20 13:39:07 +01005974#ifdef FEAT_AUTOCMD
5975 if (au_name != NULL)
5976 {
5977 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5978 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005979 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005980 /* autocommands made "qi" invalid */
5981 return;
5982 }
5983#endif
5984
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005986 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005987 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005988 else
5989 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005990
5991 if (eap->cmdidx == CMD_lhelpgrep)
5992 {
5993 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005994 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005995 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005996 {
5997 if (new_qi)
5998 ll_free_all(&qi);
5999 }
6000 else if (curwin->w_llist == NULL)
6001 curwin->w_llist = qi;
6002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003}
6004
6005#endif /* FEAT_QUICKFIX */