blob: c3b28e4ec3244d65f0570e63f952b3d3f560e14a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020050/*
51 * Quickfix/Location list definition
52 * Contains a list of entries (qfline_T). qf_start points to the first entry
53 * and qf_last points to the last entry. qf_count contains the list size.
54 *
55 * Usually the list contains one or more entries. But an empty list can be
56 * created using setqflist()/setloclist() with a title and/or user context
57 * information and entries can be added later using setqflist()/setloclist().
58 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000059typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000060{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020061 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000062 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020063 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020065 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 int qf_index; /* current index in the error list */
67 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020068 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020069 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020070 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020071
72 struct dir_stack_T *qf_dir_stack;
73 char_u *qf_directory;
74 struct dir_stack_T *qf_file_stack;
75 char_u *qf_currfile;
76 int qf_multiline;
77 int qf_multiignore;
78 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010079 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000080} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020082/*
83 * Quickfix/Location list stack definition
84 * Contains a list of quickfix/location lists (qf_list_T)
85 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000086struct qf_info_S
87{
88 /*
89 * Count of references to this list. Used only for location lists.
90 * When a location list window reference this list, qf_refcount
91 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
92 * reaches 0, the list is freed.
93 */
94 int qf_refcount;
95 int qf_listcount; /* current number of lists */
96 int qf_curlist; /* current error list */
97 qf_list_T qf_lists[LISTCOUNT];
98};
99
100static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200101static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000103#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/*
106 * Structure used to hold the info of one part of 'errorformat'
107 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000108typedef struct efm_S efm_T;
109struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110{
111 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000112 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
114 char_u prefix; /* prefix of this format line: */
115 /* 'D' enter directory */
116 /* 'X' leave directory */
117 /* 'A' start of multi-line message */
118 /* 'E' error message */
119 /* 'W' warning message */
120 /* 'I' informational message */
121 /* 'C' continuation line */
122 /* 'Z' end of multi-line message */
123 /* 'G' general, unspecific message */
124 /* 'P' push file (partial) message */
125 /* 'Q' pop/quit file (partial) message */
126 /* 'O' overread (partial) message */
127 char_u flags; /* additional flags given in prefix */
128 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000129 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000130 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131};
132
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100133static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
134
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200135static int qf_init_ext(qf_info_T *qi, int qf_idx, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title, char_u *enc);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200136static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_new_list(qf_info_T *qi, char_u *qf_title);
138static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200139static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void qf_free(qf_info_T *qi, int idx);
142static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200143static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200144static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200146static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaar3c097222017-12-21 20:54:49 +0100147static int qflist_valid(win_T *wp, int_u qf_id);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
149static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
151static int is_qf_win(win_T *win, qf_info_T *qi);
152static win_T *qf_find_win(qf_info_T *qi);
153static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200154static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200156static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static char_u *get_mef_name(void);
158static void restore_start_dir(char_u *dirname_start);
159static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
160static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
161static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
162static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164/* Quickfix window check helper macro */
165#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
166/* Location list window check helper macro */
167#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
168/*
169 * Return location list for window 'wp'
170 * For location list window, return the referenced location list
171 */
172#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
173
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200175 * Looking up a buffer can be slow if there are many. Remember the last one
176 * to make this a lot faster if there are multiple matches in the same file.
177 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200178static char_u *qf_last_bufname = NULL;
179static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200180
Bram Moolenaar3c097222017-12-21 20:54:49 +0100181static char *e_loc_list_changed =
182 N_("E926: Current location list was changed");
183
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200184/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000185 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200186 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 * Return -1 for error, number of errors for success.
188 */
189 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200190qf_init(win_T *wp,
191 char_u *efile,
192 char_u *errorformat,
193 int newlist, /* TRUE: start a new error list */
194 char_u *qf_title,
195 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000197 qf_info_T *qi = &ql_info;
198
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000199 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000200 {
201 qi = ll_get_or_alloc_list(wp);
202 if (qi == NULL)
203 return FAIL;
204 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000205
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200206 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
207 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000208}
209
210/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200211 * Maximum number of bytes allowed per line while reading a errorfile.
212 */
213#define LINE_MAXLEN 4096
214
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200215static struct fmtpattern
216{
217 char_u convchar;
218 char *pattern;
219} fmt_pat[FMT_PATTERNS] =
220 {
221 {'f', ".\\+"}, /* only used when at end */
222 {'n', "\\d\\+"},
223 {'l', "\\d\\+"},
224 {'c', "\\d\\+"},
225 {'t', "."},
226 {'m', ".\\+"},
227 {'r', ".*"},
228 {'p', "[- .]*"},
229 {'v', "\\d\\+"},
230 {'s', ".\\+"}
231 };
232
233/*
234 * Converts a 'errorformat' string to regular expression pattern
235 */
236 static int
237efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200238 char_u *efm,
239 int len,
240 efm_T *fmt_ptr,
241 char_u *regpat,
242 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200243{
244 char_u *ptr;
245 char_u *efmp;
246 char_u *srcptr;
247 int round;
248 int idx = 0;
249
250 /*
251 * Build regexp pattern from current 'errorformat' option
252 */
253 ptr = regpat;
254 *ptr++ = '^';
255 round = 0;
256 for (efmp = efm; efmp < efm + len; ++efmp)
257 {
258 if (*efmp == '%')
259 {
260 ++efmp;
261 for (idx = 0; idx < FMT_PATTERNS; ++idx)
262 if (fmt_pat[idx].convchar == *efmp)
263 break;
264 if (idx < FMT_PATTERNS)
265 {
266 if (fmt_ptr->addr[idx])
267 {
268 sprintf((char *)errmsg,
269 _("E372: Too many %%%c in format string"), *efmp);
270 EMSG(errmsg);
271 return -1;
272 }
273 if ((idx
274 && idx < 6
275 && vim_strchr((char_u *)"DXOPQ",
276 fmt_ptr->prefix) != NULL)
277 || (idx == 6
278 && vim_strchr((char_u *)"OPQ",
279 fmt_ptr->prefix) == NULL))
280 {
281 sprintf((char *)errmsg,
282 _("E373: Unexpected %%%c in format string"), *efmp);
283 EMSG(errmsg);
284 return -1;
285 }
286 fmt_ptr->addr[idx] = (char_u)++round;
287 *ptr++ = '\\';
288 *ptr++ = '(';
289#ifdef BACKSLASH_IN_FILENAME
290 if (*efmp == 'f')
291 {
292 /* Also match "c:" in the file name, even when
293 * checking for a colon next: "%f:".
294 * "\%(\a:\)\=" */
295 STRCPY(ptr, "\\%(\\a:\\)\\=");
296 ptr += 10;
297 }
298#endif
299 if (*efmp == 'f' && efmp[1] != NUL)
300 {
301 if (efmp[1] != '\\' && efmp[1] != '%')
302 {
303 /* A file name may contain spaces, but this isn't
304 * in "\f". For "%f:%l:%m" there may be a ":" in
305 * the file name. Use ".\{-1,}x" instead (x is
306 * the next character), the requirement that :999:
307 * follows should work. */
308 STRCPY(ptr, ".\\{-1,}");
309 ptr += 7;
310 }
311 else
312 {
313 /* File name followed by '\\' or '%': include as
314 * many file name chars as possible. */
315 STRCPY(ptr, "\\f\\+");
316 ptr += 4;
317 }
318 }
319 else
320 {
321 srcptr = (char_u *)fmt_pat[idx].pattern;
322 while ((*ptr = *srcptr++) != NUL)
323 ++ptr;
324 }
325 *ptr++ = '\\';
326 *ptr++ = ')';
327 }
328 else if (*efmp == '*')
329 {
330 if (*++efmp == '[' || *efmp == '\\')
331 {
332 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
333 {
334 if (efmp[1] == '^')
335 *ptr++ = *++efmp;
336 if (efmp < efm + len)
337 {
338 *ptr++ = *++efmp; /* could be ']' */
339 while (efmp < efm + len
340 && (*ptr++ = *++efmp) != ']')
341 /* skip */;
342 if (efmp == efm + len)
343 {
344 EMSG(_("E374: Missing ] in format string"));
345 return -1;
346 }
347 }
348 }
349 else if (efmp < efm + len) /* %*\D, %*\s etc. */
350 *ptr++ = *++efmp;
351 *ptr++ = '\\';
352 *ptr++ = '+';
353 }
354 else
355 {
356 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
357 sprintf((char *)errmsg,
358 _("E375: Unsupported %%%c in format string"), *efmp);
359 EMSG(errmsg);
360 return -1;
361 }
362 }
363 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
364 *ptr++ = *efmp; /* regexp magic characters */
365 else if (*efmp == '#')
366 *ptr++ = '*';
367 else if (*efmp == '>')
368 fmt_ptr->conthere = TRUE;
369 else if (efmp == efm + 1) /* analyse prefix */
370 {
371 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
372 fmt_ptr->flags = *efmp++;
373 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
374 fmt_ptr->prefix = *efmp;
375 else
376 {
377 sprintf((char *)errmsg,
378 _("E376: Invalid %%%c in format string prefix"), *efmp);
379 EMSG(errmsg);
380 return -1;
381 }
382 }
383 else
384 {
385 sprintf((char *)errmsg,
386 _("E377: Invalid %%%c in format string"), *efmp);
387 EMSG(errmsg);
388 return -1;
389 }
390 }
391 else /* copy normal character */
392 {
393 if (*efmp == '\\' && efmp + 1 < efm + len)
394 ++efmp;
395 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
396 *ptr++ = '\\'; /* escape regexp atoms */
397 if (*efmp)
398 *ptr++ = *efmp;
399 }
400 }
401 *ptr++ = '$';
402 *ptr = NUL;
403
404 return 0;
405}
406
407 static void
408free_efm_list(efm_T **efm_first)
409{
410 efm_T *efm_ptr;
411
412 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
413 {
414 *efm_first = efm_ptr->next;
415 vim_regfree(efm_ptr->prog);
416 vim_free(efm_ptr);
417 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100418 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419}
420
421/* Parse 'errorformat' option */
422 static efm_T *
423parse_efm_option(char_u *efm)
424{
425 char_u *errmsg = NULL;
426 int errmsglen;
427 efm_T *fmt_ptr = NULL;
428 efm_T *fmt_first = NULL;
429 efm_T *fmt_last = NULL;
430 char_u *fmtstr = NULL;
431 int len;
432 int i;
433 int round;
434
435 errmsglen = CMDBUFFSIZE + 1;
436 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
437 if (errmsg == NULL)
438 goto parse_efm_end;
439
440 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200441 * Each part of the format string is copied and modified from errorformat
442 * to regex prog. Only a few % characters are allowed.
443 */
444
445 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 * Get some space to modify the format string into.
447 */
448 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
449 for (round = FMT_PATTERNS; round > 0; )
450 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200451#ifdef BACKSLASH_IN_FILENAME
452 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453#else
454 i += 2; /* "%f" can become two chars longer */
455#endif
456 if ((fmtstr = alloc(i)) == NULL)
457 goto parse_efm_error;
458
459 while (efm[0] != NUL)
460 {
461 /*
462 * Allocate a new eformat structure and put it at the end of the list
463 */
464 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
465 if (fmt_ptr == NULL)
466 goto parse_efm_error;
467 if (fmt_first == NULL) /* first one */
468 fmt_first = fmt_ptr;
469 else
470 fmt_last->next = fmt_ptr;
471 fmt_last = fmt_ptr;
472
473 /*
474 * Isolate one part in the 'errorformat' option
475 */
476 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
477 if (efm[len] == '\\' && efm[len + 1] != NUL)
478 ++len;
479
480 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
481 goto parse_efm_error;
482 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
483 goto parse_efm_error;
484 /*
485 * Advance to next part
486 */
487 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
488 }
489
490 if (fmt_first == NULL) /* nothing found */
491 EMSG(_("E378: 'errorformat' contains no pattern"));
492
493 goto parse_efm_end;
494
495parse_efm_error:
496 free_efm_list(&fmt_first);
497
498parse_efm_end:
499 vim_free(fmtstr);
500 vim_free(errmsg);
501
502 return fmt_first;
503}
504
Bram Moolenaare0d37972016-07-15 22:36:01 +0200505enum {
506 QF_FAIL = 0,
507 QF_OK = 1,
508 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200509 QF_NOMEM = 3,
510 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200511};
512
513typedef struct {
514 char_u *linebuf;
515 int linelen;
516 char_u *growbuf;
517 int growbufsiz;
518 FILE *fd;
519 typval_T *tv;
520 char_u *p_str;
521 listitem_T *p_li;
522 buf_T *buf;
523 linenr_T buflnum;
524 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100525 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200526} qfstate_T;
527
528 static char_u *
529qf_grow_linebuf(qfstate_T *state, int newsz)
530{
531 /*
532 * If the line exceeds LINE_MAXLEN exclude the last
533 * byte since it's not a NL character.
534 */
535 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
536 if (state->growbuf == NULL)
537 {
538 state->growbuf = alloc(state->linelen + 1);
539 if (state->growbuf == NULL)
540 return NULL;
541 state->growbufsiz = state->linelen;
542 }
543 else if (state->linelen > state->growbufsiz)
544 {
545 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
546 if (state->growbuf == NULL)
547 return NULL;
548 state->growbufsiz = state->linelen;
549 }
550 return state->growbuf;
551}
552
553/*
554 * Get the next string (separated by newline) from state->p_str.
555 */
556 static int
557qf_get_next_str_line(qfstate_T *state)
558{
559 /* Get the next line from the supplied string */
560 char_u *p_str = state->p_str;
561 char_u *p;
562 int len;
563
564 if (*p_str == NUL) /* Reached the end of the string */
565 return QF_END_OF_INPUT;
566
567 p = vim_strchr(p_str, '\n');
568 if (p != NULL)
569 len = (int)(p - p_str) + 1;
570 else
571 len = (int)STRLEN(p_str);
572
573 if (len > IOSIZE - 2)
574 {
575 state->linebuf = qf_grow_linebuf(state, len);
576 if (state->linebuf == NULL)
577 return QF_NOMEM;
578 }
579 else
580 {
581 state->linebuf = IObuff;
582 state->linelen = len;
583 }
584 vim_strncpy(state->linebuf, p_str, state->linelen);
585
586 /*
587 * Increment using len in order to discard the rest of the
588 * line if it exceeds LINE_MAXLEN.
589 */
590 p_str += len;
591 state->p_str = p_str;
592
593 return QF_OK;
594}
595
596/*
597 * Get the next string from state->p_Li.
598 */
599 static int
600qf_get_next_list_line(qfstate_T *state)
601{
602 listitem_T *p_li = state->p_li;
603 int len;
604
605 while (p_li != NULL
606 && (p_li->li_tv.v_type != VAR_STRING
607 || p_li->li_tv.vval.v_string == NULL))
608 p_li = p_li->li_next; /* Skip non-string items */
609
610 if (p_li == NULL) /* End of the list */
611 {
612 state->p_li = NULL;
613 return QF_END_OF_INPUT;
614 }
615
616 len = (int)STRLEN(p_li->li_tv.vval.v_string);
617 if (len > IOSIZE - 2)
618 {
619 state->linebuf = qf_grow_linebuf(state, len);
620 if (state->linebuf == NULL)
621 return QF_NOMEM;
622 }
623 else
624 {
625 state->linebuf = IObuff;
626 state->linelen = len;
627 }
628
629 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
630
631 state->p_li = p_li->li_next; /* next item */
632 return QF_OK;
633}
634
635/*
636 * Get the next string from state->buf.
637 */
638 static int
639qf_get_next_buf_line(qfstate_T *state)
640{
641 char_u *p_buf = NULL;
642 int len;
643
644 /* Get the next line from the supplied buffer */
645 if (state->buflnum > state->lnumlast)
646 return QF_END_OF_INPUT;
647
648 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
649 state->buflnum += 1;
650
651 len = (int)STRLEN(p_buf);
652 if (len > IOSIZE - 2)
653 {
654 state->linebuf = qf_grow_linebuf(state, len);
655 if (state->linebuf == NULL)
656 return QF_NOMEM;
657 }
658 else
659 {
660 state->linebuf = IObuff;
661 state->linelen = len;
662 }
663 vim_strncpy(state->linebuf, p_buf, state->linelen);
664
665 return QF_OK;
666}
667
668/*
669 * Get the next string from file state->fd.
670 */
671 static int
672qf_get_next_file_line(qfstate_T *state)
673{
674 int discard;
675 int growbuflen;
676
677 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
678 return QF_END_OF_INPUT;
679
680 discard = FALSE;
681 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200682 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200683 {
684 /*
685 * The current line exceeds IObuff, continue reading using
686 * growbuf until EOL or LINE_MAXLEN bytes is read.
687 */
688 if (state->growbuf == NULL)
689 {
690 state->growbufsiz = 2 * (IOSIZE - 1);
691 state->growbuf = alloc(state->growbufsiz);
692 if (state->growbuf == NULL)
693 return QF_NOMEM;
694 }
695
696 /* Copy the read part of the line, excluding null-terminator */
697 memcpy(state->growbuf, IObuff, IOSIZE - 1);
698 growbuflen = state->linelen;
699
700 for (;;)
701 {
702 if (fgets((char *)state->growbuf + growbuflen,
703 state->growbufsiz - growbuflen, state->fd) == NULL)
704 break;
705 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
706 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200707 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200708 break;
709 if (state->growbufsiz == LINE_MAXLEN)
710 {
711 discard = TRUE;
712 break;
713 }
714
715 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
716 ? 2 * state->growbufsiz : LINE_MAXLEN;
717 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
718 if (state->growbuf == NULL)
719 return QF_NOMEM;
720 }
721
722 while (discard)
723 {
724 /*
725 * The current line is longer than LINE_MAXLEN, continue
726 * reading but discard everything until EOL or EOF is
727 * reached.
728 */
729 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
730 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200731 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200732 break;
733 }
734
735 state->linebuf = state->growbuf;
736 state->linelen = growbuflen;
737 }
738 else
739 state->linebuf = IObuff;
740
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100741#ifdef FEAT_MBYTE
742 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200743 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
744 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100745 char_u *line;
746
747 line = string_convert(&state->vc, state->linebuf, &state->linelen);
748 if (line != NULL)
749 {
750 if (state->linelen < IOSIZE)
751 {
752 STRCPY(state->linebuf, line);
753 vim_free(line);
754 }
755 else
756 {
757 vim_free(state->growbuf);
758 state->linebuf = state->growbuf = line;
759 state->growbufsiz = state->linelen < LINE_MAXLEN
760 ? state->linelen : LINE_MAXLEN;
761 }
762 }
763 }
764#endif
765
Bram Moolenaare0d37972016-07-15 22:36:01 +0200766 return QF_OK;
767}
768
769/*
770 * Get the next string from a file/buffer/list/string.
771 */
772 static int
773qf_get_nextline(qfstate_T *state)
774{
775 int status = QF_FAIL;
776
777 if (state->fd == NULL)
778 {
779 if (state->tv != NULL)
780 {
781 if (state->tv->v_type == VAR_STRING)
782 /* Get the next line from the supplied string */
783 status = qf_get_next_str_line(state);
784 else if (state->tv->v_type == VAR_LIST)
785 /* Get the next line from the supplied list */
786 status = qf_get_next_list_line(state);
787 }
788 else
789 /* Get the next line from the supplied buffer */
790 status = qf_get_next_buf_line(state);
791 }
792 else
793 /* Get the next line from the supplied file */
794 status = qf_get_next_file_line(state);
795
796 if (status != QF_OK)
797 return status;
798
799 /* remove newline/CR from the line */
800 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200801 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802 state->linebuf[state->linelen - 1] = NUL;
803#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200804 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
805 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200807 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808
809#ifdef FEAT_MBYTE
810 remove_bom(state->linebuf);
811#endif
812
813 return QF_OK;
814}
815
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200816typedef struct {
817 char_u *namebuf;
818 char_u *errmsg;
819 int errmsglen;
820 long lnum;
821 int col;
822 char_u use_viscol;
823 char_u *pattern;
824 int enr;
825 int type;
826 int valid;
827} qffields_T;
828
829/*
830 * Parse a line and get the quickfix fields.
831 * Return the QF_ status.
832 */
833 static int
834qf_parse_line(
835 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200836 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200837 char_u *linebuf,
838 int linelen,
839 efm_T *fmt_first,
840 qffields_T *fields)
841{
842 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200843 char_u *ptr;
844 int len;
845 int i;
846 int idx = 0;
847 char_u *tail = NULL;
848 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200849 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200850
851 /* Always ignore case when looking for a matching error. */
852 regmatch.rm_ic = TRUE;
853
854 /* If there was no %> item start at the first pattern */
855 if (fmt_start == NULL)
856 fmt_ptr = fmt_first;
857 else
858 {
859 fmt_ptr = fmt_start;
860 fmt_start = NULL;
861 }
862
863 /*
864 * Try to match each part of 'errorformat' until we find a complete
865 * match or no match.
866 */
867 fields->valid = TRUE;
868restofline:
869 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
870 {
871 int r;
872
873 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200874 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200875 continue;
876 fields->namebuf[0] = NUL;
877 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200878 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200879 fields->errmsg[0] = NUL;
880 fields->lnum = 0;
881 fields->col = 0;
882 fields->use_viscol = FALSE;
883 fields->enr = -1;
884 fields->type = 0;
885 tail = NULL;
886
887 regmatch.regprog = fmt_ptr->prog;
888 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
889 fmt_ptr->prog = regmatch.regprog;
890 if (r)
891 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200892 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200893 continue;
894 if (vim_strchr((char_u *)"EWI", idx) != NULL)
895 fields->type = idx;
896 else
897 fields->type = 0;
898 /*
899 * Extract error message data from matched line.
900 * We check for an actual submatch, because "\[" and "\]" in
901 * the 'errorformat' may cause the wrong submatch to be used.
902 */
903 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
904 {
905 int c;
906
907 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
908 continue;
909
910 /* Expand ~/file and $HOME/file to full path. */
911 c = *regmatch.endp[i];
912 *regmatch.endp[i] = NUL;
913 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
914 *regmatch.endp[i] = c;
915
916 if (vim_strchr((char_u *)"OPQ", idx) != NULL
917 && mch_getperm(fields->namebuf) == -1)
918 continue;
919 }
920 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
921 {
922 if (regmatch.startp[i] == NULL)
923 continue;
924 fields->enr = (int)atol((char *)regmatch.startp[i]);
925 }
926 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
927 {
928 if (regmatch.startp[i] == NULL)
929 continue;
930 fields->lnum = atol((char *)regmatch.startp[i]);
931 }
932 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
933 {
934 if (regmatch.startp[i] == NULL)
935 continue;
936 fields->col = (int)atol((char *)regmatch.startp[i]);
937 }
938 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
939 {
940 if (regmatch.startp[i] == NULL)
941 continue;
942 fields->type = *regmatch.startp[i];
943 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200944 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200945 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200946 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200947 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200948 /* linelen + null terminator */
949 if ((fields->errmsg = vim_realloc(fields->errmsg,
950 linelen + 1)) == NULL)
951 return QF_NOMEM;
952 fields->errmsglen = linelen + 1;
953 }
954 vim_strncpy(fields->errmsg, linebuf, linelen);
955 }
956 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
957 {
958 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
959 continue;
960 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200961 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200962 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200963 /* len + null terminator */
964 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
965 == NULL)
966 return QF_NOMEM;
967 fields->errmsglen = len + 1;
968 }
969 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
970 }
971 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
972 {
973 if (regmatch.startp[i] == NULL)
974 continue;
975 tail = regmatch.startp[i];
976 }
977 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
978 {
979 char_u *match_ptr;
980
981 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
982 continue;
983 fields->col = 0;
984 for (match_ptr = regmatch.startp[i];
985 match_ptr != regmatch.endp[i]; ++match_ptr)
986 {
987 ++fields->col;
988 if (*match_ptr == TAB)
989 {
990 fields->col += 7;
991 fields->col -= fields->col % 8;
992 }
993 }
994 ++fields->col;
995 fields->use_viscol = TRUE;
996 }
997 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
998 {
999 if (regmatch.startp[i] == NULL)
1000 continue;
1001 fields->col = (int)atol((char *)regmatch.startp[i]);
1002 fields->use_viscol = TRUE;
1003 }
1004 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1005 {
1006 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1007 continue;
1008 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1009 if (len > CMDBUFFSIZE - 5)
1010 len = CMDBUFFSIZE - 5;
1011 STRCPY(fields->pattern, "^\\V");
1012 STRNCAT(fields->pattern, regmatch.startp[i], len);
1013 fields->pattern[len + 3] = '\\';
1014 fields->pattern[len + 4] = '$';
1015 fields->pattern[len + 5] = NUL;
1016 }
1017 break;
1018 }
1019 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001020 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001021
1022 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1023 {
1024 if (fmt_ptr != NULL)
1025 {
1026 if (idx == 'D') /* enter directory */
1027 {
1028 if (*fields->namebuf == NUL)
1029 {
1030 EMSG(_("E379: Missing or empty directory name"));
1031 return QF_FAIL;
1032 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001033 qfl->qf_directory =
1034 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1035 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001036 return QF_FAIL;
1037 }
1038 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001039 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001040 }
1041 fields->namebuf[0] = NUL; /* no match found, remove file name */
1042 fields->lnum = 0; /* don't jump to this line */
1043 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001044 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001045 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001046 /* linelen + null terminator */
1047 if ((fields->errmsg = vim_realloc(fields->errmsg,
1048 linelen + 1)) == NULL)
1049 return QF_NOMEM;
1050 fields->errmsglen = linelen + 1;
1051 }
1052 /* copy whole line to error message */
1053 vim_strncpy(fields->errmsg, linebuf, linelen);
1054 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001055 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001056 }
1057 else if (fmt_ptr != NULL)
1058 {
1059 /* honor %> item */
1060 if (fmt_ptr->conthere)
1061 fmt_start = fmt_ptr;
1062
1063 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1064 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001065 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1066 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001067 }
1068 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1069 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001070 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001071 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001072 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001073
Bram Moolenaar9b457942016-10-09 16:10:05 +02001074 if (qfprev == NULL)
1075 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001076 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001077 {
1078 len = (int)STRLEN(qfprev->qf_text);
1079 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1080 == NULL)
1081 return QF_FAIL;
1082 STRCPY(ptr, qfprev->qf_text);
1083 vim_free(qfprev->qf_text);
1084 qfprev->qf_text = ptr;
1085 *(ptr += len) = '\n';
1086 STRCPY(++ptr, fields->errmsg);
1087 }
1088 if (qfprev->qf_nr == -1)
1089 qfprev->qf_nr = fields->enr;
1090 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1091 /* only printable chars allowed */
1092 qfprev->qf_type = fields->type;
1093
1094 if (!qfprev->qf_lnum)
1095 qfprev->qf_lnum = fields->lnum;
1096 if (!qfprev->qf_col)
1097 qfprev->qf_col = fields->col;
1098 qfprev->qf_viscol = fields->use_viscol;
1099 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001100 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1101 qfl->qf_directory,
1102 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001103 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001104 : qfl->qf_currfile != NULL && fields->valid
1105 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001106 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001107 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001108 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001109 line_breakcheck();
1110 return QF_IGNORE_LINE;
1111 }
1112 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1113 {
1114 /* global file names */
1115 fields->valid = FALSE;
1116 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1117 {
1118 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001119 qfl->qf_currfile =
1120 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001121 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001122 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001123 *fields->namebuf = NUL;
1124 if (tail && *tail)
1125 {
1126 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001127 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001128 goto restofline;
1129 }
1130 }
1131 }
1132 if (fmt_ptr->flags == '-') /* generally exclude this line */
1133 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001134 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001135 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001136 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001137 return QF_IGNORE_LINE;
1138 }
1139 }
1140
1141 return QF_OK;
1142}
1143
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001144/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001145 * Read the errorfile "efile" into memory, line by line, building the error
1146 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001147 * Alternative: when "efile" is NULL read errors from buffer "buf".
1148 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001149 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001150 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1151 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001152 * Return -1 for error, number of errors for success.
1153 */
1154 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001155qf_init_ext(
1156 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001157 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001158 char_u *efile,
1159 buf_T *buf,
1160 typval_T *tv,
1161 char_u *errorformat,
1162 int newlist, /* TRUE: start a new error list */
1163 linenr_T lnumfirst, /* first line number to use */
1164 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001165 char_u *qf_title,
1166 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001167{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001168 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001169 qfstate_T state;
1170 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001171 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001172 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001173 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001175 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001177 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001179 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001180 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001181
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001182 vim_memset(&state, 0, sizeof(state));
1183 vim_memset(&fields, 0, sizeof(fields));
1184#ifdef FEAT_MBYTE
1185 state.vc.vc_type = CONV_NONE;
1186 if (enc != NULL && *enc != NUL)
1187 convert_setup(&state.vc, enc, p_enc);
1188#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001189 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1190 fields.errmsglen = CMDBUFFSIZE + 1;
1191 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1192 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001193 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 goto qf_init_end;
1195
Bram Moolenaare0d37972016-07-15 22:36:01 +02001196 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
1198 EMSG2(_(e_openerrf), efile);
1199 goto qf_init_end;
1200 }
1201
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001202 if (newlist || qf_idx == qi->qf_listcount)
1203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001205 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001206 qf_idx = qi->qf_curlist;
1207 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001208 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001209 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001210 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001211 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001212 if (qi->qf_lists[qf_idx].qf_count > 0)
1213 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001216 qfl = &qi->qf_lists[qf_idx];
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001219 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001220 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 else
1222 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001224 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001225 * If the errorformat didn't change between calls, then reuse the
1226 * previously parsed values.
1227 */
1228 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1229 {
1230 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001231 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001232 free_efm_list(&fmt_first);
1233
1234 /* parse the current 'efm' */
1235 fmt_first = parse_efm_option(efm);
1236 if (fmt_first != NULL)
1237 last_efm = vim_strsave(efm);
1238 }
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
1243 /*
1244 * got_int is reset here, because it was probably set when killing the
1245 * ":make" command, but we still want to read the errorfile then.
1246 */
1247 got_int = FALSE;
1248
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001249 if (tv != NULL)
1250 {
1251 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001252 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001253 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001254 state.p_li = tv->vval.v_list->lv_first;
1255 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001256 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001257 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001258 state.buflnum = lnumfirst;
1259 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 /*
1262 * Read the lines in the error file one by one.
1263 * Try to recognize one of the error formats in each line.
1264 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001265 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001267 /* Get the next line from a file/buffer/list/string */
1268 status = qf_get_nextline(&state);
1269 if (status == QF_NOMEM) /* memory alloc failure */
1270 goto qf_init_end;
1271 if (status == QF_END_OF_INPUT) /* end of input */
1272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001274 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1275 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001276 if (status == QF_FAIL)
1277 goto error2;
1278 if (status == QF_NOMEM)
1279 goto qf_init_end;
1280 if (status == QF_IGNORE_LINE)
1281 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001283 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001284 qf_idx,
1285 qfl->qf_directory,
1286 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001287 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001288 : ((qfl->qf_currfile != NULL && fields.valid)
1289 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001290 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001291 fields.errmsg,
1292 fields.lnum,
1293 fields.col,
1294 fields.use_viscol,
1295 fields.pattern,
1296 fields.enr,
1297 fields.type,
1298 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 goto error2;
1300 line_breakcheck();
1301 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001302 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001304 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001306 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001307 qfl->qf_ptr = qfl->qf_start;
1308 qfl->qf_index = 1;
1309 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 else
1312 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001313 qfl->qf_nonevalid = FALSE;
1314 if (qfl->qf_ptr == NULL)
1315 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001317 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001318 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001319 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 EMSG(_(e_readerrf));
1322error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001323 if (!adding)
1324 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001325 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001326 qf_free(qi, qi->qf_curlist);
1327 qi->qf_listcount--;
1328 if (qi->qf_curlist > 0)
1329 --qi->qf_curlist;
1330 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001331qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001332 if (state.fd != NULL)
1333 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001334 vim_free(fields.namebuf);
1335 vim_free(fields.errmsg);
1336 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001337 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001339 if (qf_idx == qi->qf_curlist)
1340 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001341#ifdef FEAT_MBYTE
1342 if (state.vc.vc_type != CONV_NONE)
1343 convert_setup(&state.vc, NULL, NULL);
1344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 return retval;
1347}
1348
Bram Moolenaarfb604092014-07-23 15:55:00 +02001349 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001350qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001351{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001352 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001353
Bram Moolenaarfb604092014-07-23 15:55:00 +02001354 if (title != NULL)
1355 {
1356 char_u *p = alloc((int)STRLEN(title) + 2);
1357
Bram Moolenaara3921f42017-06-04 15:30:34 +02001358 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001359 if (p != NULL)
1360 sprintf((char *)p, ":%s", (char *)title);
1361 }
1362}
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001365 * Prepare for adding a new quickfix list. If the current list is in the
1366 * middle of the stack, then all the following lists are freed and then
1367 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 */
1369 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001370qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
1372 int i;
1373
1374 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001375 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 * the current entry. This makes it possible to browse in a tree-like
1377 * way with ":grep'.
1378 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001379 while (qi->qf_listcount > qi->qf_curlist + 1)
1380 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382 /*
1383 * When the stack is full, remove to oldest entry
1384 * Otherwise, add a new entry.
1385 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001386 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001388 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001390 qi->qf_lists[i - 1] = qi->qf_lists[i];
1391 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001394 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001395 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001396 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001397 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398}
1399
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001400/*
1401 * Free a location list
1402 */
1403 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001404ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001405{
1406 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001407 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001408
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001409 qi = *pqi;
1410 if (qi == NULL)
1411 return;
1412 *pqi = NULL; /* Remove reference to this list */
1413
1414 qi->qf_refcount--;
1415 if (qi->qf_refcount < 1)
1416 {
1417 /* No references to this location list */
1418 for (i = 0; i < qi->qf_listcount; ++i)
1419 qf_free(qi, i);
1420 vim_free(qi);
1421 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001422}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001423
1424 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001425qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001426{
1427 int i;
1428 qf_info_T *qi = &ql_info;
1429
1430 if (wp != NULL)
1431 {
1432 /* location list */
1433 ll_free_all(&wp->w_llist);
1434 ll_free_all(&wp->w_llist_ref);
1435 }
1436 else
1437 /* quickfix list */
1438 for (i = 0; i < qi->qf_listcount; ++i)
1439 qf_free(qi, i);
1440}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001441
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442/*
1443 * Add an entry to the end of the list of errors.
1444 * Returns OK or FAIL.
1445 */
1446 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001447qf_add_entry(
1448 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001449 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001450 char_u *dir, /* optional directory name */
1451 char_u *fname, /* file name or NULL */
1452 int bufnum, /* buffer number or zero */
1453 char_u *mesg, /* message */
1454 long lnum, /* line number */
1455 int col, /* column */
1456 int vis_col, /* using visual column */
1457 char_u *pattern, /* search pattern */
1458 int nr, /* error number */
1459 int type, /* type character */
1460 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001462 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001463 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001465 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001467 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001468 {
1469 buf_T *buf = buflist_findnr(bufnum);
1470
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001471 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001472 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001473 buf->b_has_qf_entry |=
1474 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001475 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001476 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001477 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1479 {
1480 vim_free(qfp);
1481 return FAIL;
1482 }
1483 qfp->qf_lnum = lnum;
1484 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001485 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001486 if (pattern == NULL || *pattern == NUL)
1487 qfp->qf_pattern = NULL;
1488 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1489 {
1490 vim_free(qfp->qf_text);
1491 vim_free(qfp);
1492 return FAIL;
1493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 qfp->qf_nr = nr;
1495 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1496 type = 0;
1497 qfp->qf_type = type;
1498 qfp->qf_valid = valid;
1499
Bram Moolenaara3921f42017-06-04 15:30:34 +02001500 lastp = &qi->qf_lists[qf_idx].qf_last;
1501 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001502 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001504 qi->qf_lists[qf_idx].qf_start = qfp;
1505 qi->qf_lists[qf_idx].qf_ptr = qfp;
1506 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001507 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509 else
1510 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001511 qfp->qf_prev = *lastp;
1512 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001514 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001516 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001517 ++qi->qf_lists[qf_idx].qf_count;
1518 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001519 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001521 qi->qf_lists[qf_idx].qf_index =
1522 qi->qf_lists[qf_idx].qf_count;
1523 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 }
1525
1526 return OK;
1527}
1528
1529/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001530 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001531 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001532 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001533ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001535 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001536
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001537 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1538 if (qi != NULL)
1539 {
1540 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1541 qi->qf_refcount++;
1542 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001544 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001545}
1546
1547/*
1548 * Return the location list for window 'wp'.
1549 * If not present, allocate a location list
1550 */
1551 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001552ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001553{
1554 if (IS_LL_WINDOW(wp))
1555 /* For a location list window, use the referenced location list */
1556 return wp->w_llist_ref;
1557
1558 /*
1559 * For a non-location list window, w_llist_ref should not point to a
1560 * location list.
1561 */
1562 ll_free_all(&wp->w_llist_ref);
1563
1564 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001565 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001566 return wp->w_llist;
1567}
1568
1569/*
1570 * Copy the location list from window "from" to window "to".
1571 */
1572 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001573copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001574{
1575 qf_info_T *qi;
1576 int idx;
1577 int i;
1578
1579 /*
1580 * When copying from a location list window, copy the referenced
1581 * location list. For other windows, copy the location list for
1582 * that window.
1583 */
1584 if (IS_LL_WINDOW(from))
1585 qi = from->w_llist_ref;
1586 else
1587 qi = from->w_llist;
1588
1589 if (qi == NULL) /* no location list to copy */
1590 return;
1591
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001592 /* allocate a new location list */
1593 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001594 return;
1595
1596 to->w_llist->qf_listcount = qi->qf_listcount;
1597
1598 /* Copy the location lists one at a time */
1599 for (idx = 0; idx < qi->qf_listcount; idx++)
1600 {
1601 qf_list_T *from_qfl;
1602 qf_list_T *to_qfl;
1603
1604 to->w_llist->qf_curlist = idx;
1605
1606 from_qfl = &qi->qf_lists[idx];
1607 to_qfl = &to->w_llist->qf_lists[idx];
1608
1609 /* Some of the fields are populated by qf_add_entry() */
1610 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1611 to_qfl->qf_count = 0;
1612 to_qfl->qf_index = 0;
1613 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001614 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001615 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001616 if (from_qfl->qf_title != NULL)
1617 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1618 else
1619 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001620 if (from_qfl->qf_ctx != NULL)
1621 {
1622 to_qfl->qf_ctx = alloc_tv();
1623 if (to_qfl->qf_ctx != NULL)
1624 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1625 }
1626 else
1627 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001628
1629 if (from_qfl->qf_count)
1630 {
1631 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001632 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001633
1634 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001635 for (i = 0, from_qfp = from_qfl->qf_start;
1636 i < from_qfl->qf_count && from_qfp != NULL;
1637 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001638 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001639 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001640 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001641 NULL,
1642 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001643 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 from_qfp->qf_text,
1645 from_qfp->qf_lnum,
1646 from_qfp->qf_col,
1647 from_qfp->qf_viscol,
1648 from_qfp->qf_pattern,
1649 from_qfp->qf_nr,
1650 0,
1651 from_qfp->qf_valid) == FAIL)
1652 {
1653 qf_free_all(to);
1654 return;
1655 }
1656 /*
1657 * qf_add_entry() will not set the qf_num field, as the
1658 * directory and file names are not supplied. So the qf_fnum
1659 * field is copied here.
1660 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001661 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001662 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1663 prevp->qf_type = from_qfp->qf_type; /* error type */
1664 if (from_qfl->qf_ptr == from_qfp)
1665 to_qfl->qf_ptr = prevp; /* current location */
1666 }
1667 }
1668
1669 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1670
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001671 /* Assign a new ID for the location list */
1672 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001673 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001674
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001675 /* When no valid entries are present in the list, qf_ptr points to
1676 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001677 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001678 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001679 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001680 to_qfl->qf_index = 1;
1681 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001682 }
1683
1684 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1685}
1686
1687/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001688 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001689 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 */
1691 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001692qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
Bram Moolenaar82404332016-07-10 17:00:38 +02001694 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001695 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001696 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001697
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 if (fname == NULL || *fname == NUL) /* no file name */
1699 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaare60acc12011-05-10 16:41:25 +02001701#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001702 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001703#endif
1704#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001705 if (directory != NULL)
1706 slash_adjust(directory);
1707 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001708#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001709 if (directory != NULL && !vim_isAbsName(fname)
1710 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1711 {
1712 /*
1713 * Here we check if the file really exists.
1714 * This should normally be true, but if make works without
1715 * "leaving directory"-messages we might have missed a
1716 * directory change.
1717 */
1718 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001721 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001722 if (directory)
1723 ptr = concat_fnames(directory, fname, TRUE);
1724 else
1725 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001727 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001728 bufname = ptr;
1729 }
1730 else
1731 bufname = fname;
1732
1733 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001734 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001735 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001736 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001737 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001739 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001740 {
1741 vim_free(qf_last_bufname);
1742 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1743 if (bufname == ptr)
1744 qf_last_bufname = bufname;
1745 else
1746 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001747 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001748 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001749 if (buf == NULL)
1750 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001751
Bram Moolenaarc1542742016-07-20 21:44:37 +02001752 buf->b_has_qf_entry =
1753 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001754 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755}
1756
1757/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001758 * Push dirbuf onto the directory stack and return pointer to actual dir or
1759 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 */
1761 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001762qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 struct dir_stack_T *ds_new;
1765 struct dir_stack_T *ds_ptr;
1766
1767 /* allocate new stack element and hook it in */
1768 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1769 if (ds_new == NULL)
1770 return NULL;
1771
1772 ds_new->next = *stackptr;
1773 *stackptr = ds_new;
1774
1775 /* store directory on the stack */
1776 if (vim_isAbsName(dirbuf)
1777 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001778 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 (*stackptr)->dirname = vim_strsave(dirbuf);
1780 else
1781 {
1782 /* Okay we don't have an absolute path.
1783 * dirbuf must be a subdir of one of the directories on the stack.
1784 * Let's search...
1785 */
1786 ds_new = (*stackptr)->next;
1787 (*stackptr)->dirname = NULL;
1788 while (ds_new)
1789 {
1790 vim_free((*stackptr)->dirname);
1791 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1792 TRUE);
1793 if (mch_isdir((*stackptr)->dirname) == TRUE)
1794 break;
1795
1796 ds_new = ds_new->next;
1797 }
1798
1799 /* clean up all dirs we already left */
1800 while ((*stackptr)->next != ds_new)
1801 {
1802 ds_ptr = (*stackptr)->next;
1803 (*stackptr)->next = (*stackptr)->next->next;
1804 vim_free(ds_ptr->dirname);
1805 vim_free(ds_ptr);
1806 }
1807
1808 /* Nothing found -> it must be on top level */
1809 if (ds_new == NULL)
1810 {
1811 vim_free((*stackptr)->dirname);
1812 (*stackptr)->dirname = vim_strsave(dirbuf);
1813 }
1814 }
1815
1816 if ((*stackptr)->dirname != NULL)
1817 return (*stackptr)->dirname;
1818 else
1819 {
1820 ds_ptr = *stackptr;
1821 *stackptr = (*stackptr)->next;
1822 vim_free(ds_ptr);
1823 return NULL;
1824 }
1825}
1826
1827
1828/*
1829 * pop dirbuf from the directory stack and return previous directory or NULL if
1830 * stack is empty
1831 */
1832 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001833qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 struct dir_stack_T *ds_ptr;
1836
1837 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1838 * What to do if it isn't? */
1839
1840 /* pop top element and free it */
1841 if (*stackptr != NULL)
1842 {
1843 ds_ptr = *stackptr;
1844 *stackptr = (*stackptr)->next;
1845 vim_free(ds_ptr->dirname);
1846 vim_free(ds_ptr);
1847 }
1848
1849 /* return NEW top element as current dir or NULL if stack is empty*/
1850 return *stackptr ? (*stackptr)->dirname : NULL;
1851}
1852
1853/*
1854 * clean up directory stack
1855 */
1856 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001857qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
1859 struct dir_stack_T *ds_ptr;
1860
1861 while ((ds_ptr = *stackptr) != NULL)
1862 {
1863 *stackptr = (*stackptr)->next;
1864 vim_free(ds_ptr->dirname);
1865 vim_free(ds_ptr);
1866 }
1867}
1868
1869/*
1870 * Check in which directory of the directory stack the given file can be
1871 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001872 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 * Cleans up intermediate directory entries.
1874 *
1875 * TODO: How to solve the following problem?
1876 * If we have the this directory tree:
1877 * ./
1878 * ./aa
1879 * ./aa/bb
1880 * ./bb
1881 * ./bb/x.c
1882 * and make says:
1883 * making all in aa
1884 * making all in bb
1885 * x.c:9: Error
1886 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1887 * qf_guess_filepath will return NULL.
1888 */
1889 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001890qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 struct dir_stack_T *ds_ptr;
1893 struct dir_stack_T *ds_tmp;
1894 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001895 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001898 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 return NULL;
1900
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001901 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 fullname = NULL;
1903 while (ds_ptr)
1904 {
1905 vim_free(fullname);
1906 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1907
1908 /* If concat_fnames failed, just go on. The worst thing that can happen
1909 * is that we delete the entire stack.
1910 */
1911 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1912 break;
1913
1914 ds_ptr = ds_ptr->next;
1915 }
1916
1917 vim_free(fullname);
1918
1919 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001920 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001922 ds_tmp = qfl->qf_dir_stack->next;
1923 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 vim_free(ds_tmp->dirname);
1925 vim_free(ds_tmp);
1926 }
1927
1928 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929}
1930
1931/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01001932 * Returns TRUE if a quickfix/location list with the given identifier exists.
1933 */
1934 static int
1935qflist_valid (win_T *wp, int_u qf_id)
1936{
1937 qf_info_T *qi = &ql_info;
1938 int i;
1939
1940 if (wp != NULL)
1941 {
1942 qi = GET_LOC_LIST(wp); /* Location list */
1943 if (qi == NULL)
1944 return FALSE;
1945 }
1946
1947 for (i = 0; i < qi->qf_listcount; ++i)
1948 if (qi->qf_lists[i].qf_id == qf_id)
1949 return TRUE;
1950
1951 return FALSE;
1952}
1953
1954/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001955 * When loading a file from the quickfix, the auto commands may modify it.
1956 * This may invalidate the current quickfix entry. This function checks
1957 * whether a entry is still present in the quickfix.
1958 * Similar to location list.
1959 */
1960 static int
1961is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1962{
1963 qf_list_T *qfl;
1964 qfline_T *qfp;
1965 int i;
1966
1967 qfl = &qi->qf_lists[qi->qf_curlist];
1968
1969 /* Search for the entry in the current list */
1970 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1971 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001972 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001973 break;
1974
1975 if (i == qfl->qf_count) /* Entry is not found */
1976 return FALSE;
1977
1978 return TRUE;
1979}
1980
1981/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001982 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001983 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001984 */
1985 static qfline_T *
1986get_next_valid_entry(
1987 qf_info_T *qi,
1988 qfline_T *qf_ptr,
1989 int *qf_index,
1990 int dir)
1991{
1992 int idx;
1993 int old_qf_fnum;
1994
1995 idx = *qf_index;
1996 old_qf_fnum = qf_ptr->qf_fnum;
1997
1998 do
1999 {
2000 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2001 || qf_ptr->qf_next == NULL)
2002 return NULL;
2003 ++idx;
2004 qf_ptr = qf_ptr->qf_next;
2005 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2006 && !qf_ptr->qf_valid)
2007 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2008
2009 *qf_index = idx;
2010 return qf_ptr;
2011}
2012
2013/*
2014 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002015 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002016 */
2017 static qfline_T *
2018get_prev_valid_entry(
2019 qf_info_T *qi,
2020 qfline_T *qf_ptr,
2021 int *qf_index,
2022 int dir)
2023{
2024 int idx;
2025 int old_qf_fnum;
2026
2027 idx = *qf_index;
2028 old_qf_fnum = qf_ptr->qf_fnum;
2029
2030 do
2031 {
2032 if (idx == 1 || qf_ptr->qf_prev == NULL)
2033 return NULL;
2034 --idx;
2035 qf_ptr = qf_ptr->qf_prev;
2036 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2037 && !qf_ptr->qf_valid)
2038 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2039
2040 *qf_index = idx;
2041 return qf_ptr;
2042}
2043
2044/*
2045 * Get the n'th (errornr) previous/next valid entry from the current entry in
2046 * the quickfix list.
2047 * dir == FORWARD or FORWARD_FILE: next valid entry
2048 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2049 */
2050 static qfline_T *
2051get_nth_valid_entry(
2052 qf_info_T *qi,
2053 int errornr,
2054 qfline_T *qf_ptr,
2055 int *qf_index,
2056 int dir)
2057{
2058 qfline_T *prev_qf_ptr;
2059 int prev_index;
2060 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2061 char_u *err = e_no_more_items;
2062
2063 while (errornr--)
2064 {
2065 prev_qf_ptr = qf_ptr;
2066 prev_index = *qf_index;
2067
2068 if (dir == FORWARD || dir == FORWARD_FILE)
2069 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2070 else
2071 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2072 if (qf_ptr == NULL)
2073 {
2074 qf_ptr = prev_qf_ptr;
2075 *qf_index = prev_index;
2076 if (err != NULL)
2077 {
2078 EMSG(_(err));
2079 return NULL;
2080 }
2081 break;
2082 }
2083
2084 err = NULL;
2085 }
2086
2087 return qf_ptr;
2088}
2089
2090/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002091 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002092 */
2093 static qfline_T *
2094get_nth_entry(
2095 qf_info_T *qi,
2096 int errornr,
2097 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002098 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002099{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002100 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002101
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002102 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002103 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2104 {
2105 --qf_idx;
2106 qf_ptr = qf_ptr->qf_prev;
2107 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002108 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002109 while (errornr > qf_idx &&
2110 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2111 qf_ptr->qf_next != NULL)
2112 {
2113 ++qf_idx;
2114 qf_ptr = qf_ptr->qf_next;
2115 }
2116
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002117 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002118 return qf_ptr;
2119}
2120
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002121/*
2122 * Find a help window or open one.
2123 */
2124 static int
2125jump_to_help_window(qf_info_T *qi, int *opened_window)
2126{
2127 win_T *wp;
2128 int flags;
2129
2130 if (cmdmod.tab != 0)
2131 wp = NULL;
2132 else
2133 FOR_ALL_WINDOWS(wp)
2134 if (bt_help(wp->w_buffer))
2135 break;
2136 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2137 win_enter(wp, TRUE);
2138 else
2139 {
2140 /*
2141 * Split off help window; put it at far top if no position
2142 * specified, the current window is vertically split and narrow.
2143 */
2144 flags = WSP_HELP;
2145 if (cmdmod.split == 0 && curwin->w_width != Columns
2146 && curwin->w_width < 80)
2147 flags |= WSP_TOP;
2148 if (qi != &ql_info)
2149 flags |= WSP_NEWLOC; /* don't copy the location list */
2150
2151 if (win_split(0, flags) == FAIL)
2152 return FAIL;
2153
2154 *opened_window = TRUE;
2155
2156 if (curwin->w_height < p_hh)
2157 win_setheight((int)p_hh);
2158
2159 if (qi != &ql_info) /* not a quickfix list */
2160 {
2161 /* The new window should use the supplied location list */
2162 curwin->w_llist = qi;
2163 qi->qf_refcount++;
2164 }
2165 }
2166
2167 if (!p_im)
2168 restart_edit = 0; /* don't want insert mode in help file */
2169
2170 return OK;
2171}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002172
2173/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002174 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2175 * If the file is already opened in a window, jump to it.
2176 */
2177 static int
2178qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2179{
2180 win_T *usable_win_ptr = NULL;
2181 int usable_win;
2182 qf_info_T *ll_ref;
2183 int flags;
2184 win_T *win;
2185 win_T *altwin;
2186
2187 usable_win = 0;
2188
2189 ll_ref = curwin->w_llist_ref;
2190 if (ll_ref != NULL)
2191 {
2192 /* Find a window using the same location list that is not a
2193 * quickfix window. */
2194 FOR_ALL_WINDOWS(usable_win_ptr)
2195 if (usable_win_ptr->w_llist == ll_ref
2196 && !bt_quickfix(usable_win_ptr->w_buffer))
2197 {
2198 usable_win = 1;
2199 break;
2200 }
2201 }
2202
2203 if (!usable_win)
2204 {
2205 /* Locate a window showing a normal buffer */
2206 FOR_ALL_WINDOWS(win)
2207 if (win->w_buffer->b_p_bt[0] == NUL)
2208 {
2209 usable_win = 1;
2210 break;
2211 }
2212 }
2213
2214 /*
2215 * If no usable window is found and 'switchbuf' contains "usetab"
2216 * then search in other tabs.
2217 */
2218 if (!usable_win && (swb_flags & SWB_USETAB))
2219 {
2220 tabpage_T *tp;
2221 win_T *wp;
2222
2223 FOR_ALL_TAB_WINDOWS(tp, wp)
2224 {
2225 if (wp->w_buffer->b_fnum == qf_fnum)
2226 {
2227 goto_tabpage_win(tp, wp);
2228 usable_win = 1;
2229 goto win_found;
2230 }
2231 }
2232 }
2233win_found:
2234
2235 /*
2236 * If there is only one window and it is the quickfix window, create a
2237 * new one above the quickfix window.
2238 */
2239 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2240 {
2241 flags = WSP_ABOVE;
2242 if (ll_ref != NULL)
2243 flags |= WSP_NEWLOC;
2244 if (win_split(0, flags) == FAIL)
2245 return FAIL; /* not enough room for window */
2246 *opened_window = TRUE; /* close it when fail */
2247 p_swb = empty_option; /* don't split again */
2248 swb_flags = 0;
2249 RESET_BINDING(curwin);
2250 if (ll_ref != NULL)
2251 {
2252 /* The new window should use the location list from the
2253 * location list window */
2254 curwin->w_llist = ll_ref;
2255 ll_ref->qf_refcount++;
2256 }
2257 }
2258 else
2259 {
2260 if (curwin->w_llist_ref != NULL)
2261 {
2262 /* In a location window */
2263 win = usable_win_ptr;
2264 if (win == NULL)
2265 {
2266 /* Find the window showing the selected file */
2267 FOR_ALL_WINDOWS(win)
2268 if (win->w_buffer->b_fnum == qf_fnum)
2269 break;
2270 if (win == NULL)
2271 {
2272 /* Find a previous usable window */
2273 win = curwin;
2274 do
2275 {
2276 if (win->w_buffer->b_p_bt[0] == NUL)
2277 break;
2278 if (win->w_prev == NULL)
2279 win = lastwin; /* wrap around the top */
2280 else
2281 win = win->w_prev; /* go to previous window */
2282 } while (win != curwin);
2283 }
2284 }
2285 win_goto(win);
2286
2287 /* If the location list for the window is not set, then set it
2288 * to the location list from the location window */
2289 if (win->w_llist == NULL)
2290 {
2291 win->w_llist = ll_ref;
2292 ll_ref->qf_refcount++;
2293 }
2294 }
2295 else
2296 {
2297
2298 /*
2299 * Try to find a window that shows the right buffer.
2300 * Default to the window just above the quickfix buffer.
2301 */
2302 win = curwin;
2303 altwin = NULL;
2304 for (;;)
2305 {
2306 if (win->w_buffer->b_fnum == qf_fnum)
2307 break;
2308 if (win->w_prev == NULL)
2309 win = lastwin; /* wrap around the top */
2310 else
2311 win = win->w_prev; /* go to previous window */
2312
2313 if (IS_QF_WINDOW(win))
2314 {
2315 /* Didn't find it, go to the window before the quickfix
2316 * window. */
2317 if (altwin != NULL)
2318 win = altwin;
2319 else if (curwin->w_prev != NULL)
2320 win = curwin->w_prev;
2321 else
2322 win = curwin->w_next;
2323 break;
2324 }
2325
2326 /* Remember a usable window. */
2327 if (altwin == NULL && !win->w_p_pvw
2328 && win->w_buffer->b_p_bt[0] == NUL)
2329 altwin = win;
2330 }
2331
2332 win_goto(win);
2333 }
2334 }
2335
2336 return OK;
2337}
2338
2339/*
2340 * Edit the selected file or help file.
2341 */
2342 static int
2343qf_jump_edit_buffer(
2344 qf_info_T *qi,
2345 qfline_T *qf_ptr,
2346 int forceit,
2347 win_T *oldwin,
2348 int *opened_window,
2349 int *abort)
2350{
2351 int retval = OK;
2352
2353 if (qf_ptr->qf_type == 1)
2354 {
2355 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2356 * set b_p_ro flag). */
2357 if (!can_abandon(curbuf, forceit))
2358 {
2359 no_write_message();
2360 retval = FALSE;
2361 }
2362 else
2363 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2364 ECMD_HIDE + ECMD_SET_HELP,
2365 oldwin == curwin ? curwin : NULL);
2366 }
2367 else
2368 {
2369 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002370 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002371
2372 retval = buflist_getfile(qf_ptr->qf_fnum,
2373 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002374
2375 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002376 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002377 /*
2378 * Location list. Check whether the associated window is still
2379 * present and the list is still valid.
2380 */
2381 if (!win_valid_any_tab(oldwin))
2382 {
2383 EMSG(_("E924: Current window was closed"));
2384 *abort = TRUE;
2385 *opened_window = FALSE;
2386 }
2387 else if (!qflist_valid(oldwin, save_qfid))
2388 {
2389 EMSG(_(e_loc_list_changed));
2390 *abort = TRUE;
2391 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002392 }
2393 else if (old_qf_curlist != qi->qf_curlist
2394 || !is_qf_entry_present(qi, qf_ptr))
2395 {
2396 if (qi == &ql_info)
2397 EMSG(_("E925: Current quickfix was changed"));
2398 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002399 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002400 *abort = TRUE;
2401 }
2402
2403 if (*abort)
2404 retval = FALSE;
2405 }
2406
2407 return retval;
2408}
2409
2410/*
2411 * Goto the error line in the current file using either line/column number or a
2412 * search pattern.
2413 */
2414 static void
2415qf_jump_goto_line(
2416 linenr_T qf_lnum,
2417 int qf_col,
2418 char_u qf_viscol,
2419 char_u *qf_pattern)
2420{
2421 linenr_T i;
2422 char_u *line;
2423 colnr_T screen_col;
2424 colnr_T char_col;
2425
2426 if (qf_pattern == NULL)
2427 {
2428 /*
2429 * Go to line with error, unless qf_lnum is 0.
2430 */
2431 i = qf_lnum;
2432 if (i > 0)
2433 {
2434 if (i > curbuf->b_ml.ml_line_count)
2435 i = curbuf->b_ml.ml_line_count;
2436 curwin->w_cursor.lnum = i;
2437 }
2438 if (qf_col > 0)
2439 {
2440 curwin->w_cursor.col = qf_col - 1;
2441#ifdef FEAT_VIRTUALEDIT
2442 curwin->w_cursor.coladd = 0;
2443#endif
2444 if (qf_viscol == TRUE)
2445 {
2446 /*
2447 * Check each character from the beginning of the error
2448 * line up to the error column. For each tab character
2449 * found, reduce the error column value by the length of
2450 * a tab character.
2451 */
2452 line = ml_get_curline();
2453 screen_col = 0;
2454 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2455 {
2456 if (*line == NUL)
2457 break;
2458 if (*line++ == '\t')
2459 {
2460 curwin->w_cursor.col -= 7 - (screen_col % 8);
2461 screen_col += 8 - (screen_col % 8);
2462 }
2463 else
2464 ++screen_col;
2465 }
2466 }
2467 check_cursor();
2468 }
2469 else
2470 beginline(BL_WHITE | BL_FIX);
2471 }
2472 else
2473 {
2474 pos_T save_cursor;
2475
2476 /* Move the cursor to the first line in the buffer */
2477 save_cursor = curwin->w_cursor;
2478 curwin->w_cursor.lnum = 0;
2479 if (!do_search(NULL, '/', qf_pattern, (long)1,
2480 SEARCH_KEEP, NULL, NULL))
2481 curwin->w_cursor = save_cursor;
2482 }
2483}
2484
2485/*
2486 * Display quickfix list index and size message
2487 */
2488 static void
2489qf_jump_print_msg(
2490 qf_info_T *qi,
2491 int qf_index,
2492 qfline_T *qf_ptr,
2493 buf_T *old_curbuf,
2494 linenr_T old_lnum)
2495{
2496 linenr_T i;
2497 int len;
2498
2499 /* Update the screen before showing the message, unless the screen
2500 * scrolled up. */
2501 if (!msg_scrolled)
2502 update_topline_redraw();
2503 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2504 qi->qf_lists[qi->qf_curlist].qf_count,
2505 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2506 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2507 /* Add the message, skipping leading whitespace and newlines. */
2508 len = (int)STRLEN(IObuff);
2509 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2510
2511 /* Output the message. Overwrite to avoid scrolling when the 'O'
2512 * flag is present in 'shortmess'; But when not jumping, print the
2513 * whole message. */
2514 i = msg_scroll;
2515 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2516 msg_scroll = TRUE;
2517 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2518 msg_scroll = FALSE;
2519 msg_attr_keep(IObuff, 0, TRUE);
2520 msg_scroll = i;
2521}
2522
2523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * jump to a quickfix line
2525 * if dir == FORWARD go "errornr" valid entries forward
2526 * if dir == BACKWARD go "errornr" valid entries backward
2527 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2528 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2529 * else if "errornr" is zero, redisplay the same line
2530 * else go to entry "errornr"
2531 */
2532 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002533qf_jump(qf_info_T *qi,
2534 int dir,
2535 int errornr,
2536 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002538 qfline_T *qf_ptr;
2539 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 buf_T *old_curbuf;
2543 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002544 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002545 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002547 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549#ifdef FEAT_FOLDING
2550 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2551#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002552 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002554 if (qi == NULL)
2555 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002556
2557 if (qi->qf_curlist >= qi->qf_listcount
2558 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 EMSG(_(e_quickfix));
2561 return;
2562 }
2563
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002564 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002566 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002568 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002570 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2571 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002573 qf_ptr = old_qf_ptr;
2574 qf_index = old_qf_index;
2575 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577 }
2578 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002579 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002581 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2582 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 /* No need to print the error message if it's visible in the error
2584 * window */
2585 print_message = FALSE;
2586
2587 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002588 * For ":helpgrep" find a help window or open one.
2589 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002590 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002591 if (jump_to_help_window(qi, &opened_window) == FAIL)
2592 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002593
2594 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 * If currently in the quickfix window, find another window to show the
2596 * file in.
2597 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002598 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
2600 /*
2601 * If there is no file specified, we don't know where to go.
2602 * But do advance, otherwise ":cn" gets stuck.
2603 */
2604 if (qf_ptr->qf_fnum == 0)
2605 goto theend;
2606
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002607 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2608 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
2611 /*
2612 * If there is a file name,
2613 * read the wanted file if needed, and check autowrite etc.
2614 */
2615 old_curbuf = curbuf;
2616 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002617
2618 if (qf_ptr->qf_fnum != 0)
2619 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002620 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002621
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002622 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2623 &opened_window, &abort);
2624 if (abort)
2625 {
2626 qi = NULL;
2627 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002628 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002629 }
2630
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002631 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
2633 /* When not switched to another buffer, still need to set pc mark */
2634 if (curbuf == old_curbuf)
2635 setpcmark();
2636
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002637 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2638 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
2640#ifdef FEAT_FOLDING
2641 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2642 foldOpenCursor();
2643#endif
2644 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002645 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
2647 else
2648 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (opened_window)
2650 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002651 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
2653 /*
2654 * Couldn't open file, so put index back where it was. This could
2655 * happen if the file was readonly and we changed something.
2656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 qf_ptr = old_qf_ptr;
2659 qf_index = old_qf_index;
2660 }
2661 }
2662theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002663 if (qi != NULL)
2664 {
2665 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2666 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (p_swb != old_swb && opened_window)
2669 {
2670 /* Restore old 'switchbuf' value, but not when an autocommand or
2671 * modeline has changed the value. */
2672 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002673 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002675 swb_flags = old_swb_flags;
2676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 else
2678 free_string_option(old_swb);
2679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680}
2681
2682/*
2683 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002684 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 */
2686 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002687qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002689 buf_T *buf;
2690 char_u *fname;
2691 qfline_T *qfp;
2692 int i;
2693 int idx1 = 1;
2694 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002695 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002696 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002697 int qfFileAttr;
2698 int qfSepAttr;
2699 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002700 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002702 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002704 if (eap->cmdidx == CMD_llist)
2705 {
2706 qi = GET_LOC_LIST(curwin);
2707 if (qi == NULL)
2708 {
2709 EMSG(_(e_loclist));
2710 return;
2711 }
2712 }
2713
2714 if (qi->qf_curlist >= qi->qf_listcount
2715 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
2717 EMSG(_(e_quickfix));
2718 return;
2719 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002720 if (*arg == '+')
2721 {
2722 ++arg;
2723 plus = TRUE;
2724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2726 {
2727 EMSG(_(e_trailing));
2728 return;
2729 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002730 if (plus)
2731 {
2732 i = qi->qf_lists[qi->qf_curlist].qf_index;
2733 idx2 = i + idx1;
2734 idx1 = i;
2735 }
2736 else
2737 {
2738 i = qi->qf_lists[qi->qf_curlist].qf_count;
2739 if (idx1 < 0)
2740 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2741 if (idx2 < 0)
2742 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002745 /*
2746 * Get the attributes for the different quickfix highlight items. Note
2747 * that this depends on syntax items defined in the qf.vim syntax file
2748 */
2749 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2750 if (qfFileAttr == 0)
2751 qfFileAttr = HL_ATTR(HLF_D);
2752 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2753 if (qfSepAttr == 0)
2754 qfSepAttr = HL_ATTR(HLF_D);
2755 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2756 if (qfLineAttr == 0)
2757 qfLineAttr = HL_ATTR(HLF_N);
2758
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002759 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002761 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2762 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
2764 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2765 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002766 msg_putchar('\n');
2767 if (got_int)
2768 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002769
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002770 fname = NULL;
2771 if (qfp->qf_fnum != 0
2772 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2773 {
2774 fname = buf->b_fname;
2775 if (qfp->qf_type == 1) /* :helpgrep */
2776 fname = gettail(fname);
2777 }
2778 if (fname == NULL)
2779 sprintf((char *)IObuff, "%2d", i);
2780 else
2781 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2782 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002784 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2785
2786 if (qfp->qf_lnum != 0)
2787 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788 if (qfp->qf_lnum == 0)
2789 IObuff[0] = NUL;
2790 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002791 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002792 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002793 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002794 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002795 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002796 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002797 msg_puts_attr(IObuff, qfLineAttr);
2798 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 if (qfp->qf_pattern != NULL)
2800 {
2801 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002802 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002803 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002804 }
2805 msg_puts((char_u *)" ");
2806
2807 /* Remove newlines and leading whitespace from the text. For an
2808 * unrecognized line keep the indent, the compiler may mark a word
2809 * with ^^^^. */
2810 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2812 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813 msg_prt_line(IObuff, FALSE);
2814 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816
2817 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002818 if (qfp == NULL)
2819 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002820 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 ui_breakcheck();
2822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823}
2824
2825/*
2826 * Remove newlines and leading whitespace from an error message.
2827 * Put the result in "buf[bufsize]".
2828 */
2829 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002830qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
2832 int i;
2833 char_u *p = text;
2834
2835 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2836 {
2837 if (*p == '\n')
2838 {
2839 buf[i] = ' ';
2840 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002841 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 break;
2843 }
2844 else
2845 buf[i] = *p++;
2846 }
2847 buf[i] = NUL;
2848}
2849
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002850 static void
2851qf_msg(qf_info_T *qi, int which, char *lead)
2852{
2853 char *title = (char *)qi->qf_lists[which].qf_title;
2854 int count = qi->qf_lists[which].qf_count;
2855 char_u buf[IOSIZE];
2856
2857 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2858 lead,
2859 which + 1,
2860 qi->qf_listcount,
2861 count);
2862
2863 if (title != NULL)
2864 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002865 size_t len = STRLEN(buf);
2866
2867 if (len < 34)
2868 {
2869 vim_memset(buf + len, ' ', 34 - len);
2870 buf[34] = NUL;
2871 }
2872 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002873 }
2874 trunc_string(buf, buf, Columns - 1, IOSIZE);
2875 msg(buf);
2876}
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878/*
2879 * ":colder [count]": Up in the quickfix stack.
2880 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002881 * ":lolder [count]": Up in the location list stack.
2882 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 */
2884 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002885qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002887 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 int count;
2889
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2891 {
2892 qi = GET_LOC_LIST(curwin);
2893 if (qi == NULL)
2894 {
2895 EMSG(_(e_loclist));
2896 return;
2897 }
2898 }
2899
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 if (eap->addr_count != 0)
2901 count = eap->line2;
2902 else
2903 count = 1;
2904 while (count--)
2905 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002906 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002908 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
2910 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002911 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002913 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 else
2916 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002917 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
2919 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002920 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002922 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002925 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002926 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927}
2928
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002929 void
2930qf_history(exarg_T *eap)
2931{
2932 qf_info_T *qi = &ql_info;
2933 int i;
2934
2935 if (eap->cmdidx == CMD_lhistory)
2936 qi = GET_LOC_LIST(curwin);
2937 if (qi == NULL || (qi->qf_listcount == 0
2938 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2939 MSG(_("No entries"));
2940 else
2941 for (i = 0; i < qi->qf_listcount; ++i)
2942 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2943}
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002946 * Free all the entries in the error list "idx". Note that other information
2947 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 */
2949 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002950qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002952 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002953 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002954 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002955 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002957 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002959 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002960 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002961 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002962 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002963 vim_free(qfp->qf_text);
2964 stop = (qfp == qfpnext);
2965 vim_free(qfp->qf_pattern);
2966 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002967 if (stop)
2968 /* Somehow qf_count may have an incorrect value, set it to 1
2969 * to avoid crashing when it's wrong.
2970 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002971 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002972 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002973 qfl->qf_start = qfpnext;
2974 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002976
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002977 qfl->qf_index = 0;
2978 qfl->qf_start = NULL;
2979 qfl->qf_last = NULL;
2980 qfl->qf_ptr = NULL;
2981 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002982
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002983 qf_clean_dir_stack(&qfl->qf_dir_stack);
2984 qfl->qf_directory = NULL;
2985 qf_clean_dir_stack(&qfl->qf_file_stack);
2986 qfl->qf_currfile = NULL;
2987 qfl->qf_multiline = FALSE;
2988 qfl->qf_multiignore = FALSE;
2989 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990}
2991
2992/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002993 * Free error list "idx". Frees all the entries in the quickfix list,
2994 * associated context information and the title.
2995 */
2996 static void
2997qf_free(qf_info_T *qi, int idx)
2998{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002999 qf_list_T *qfl = &qi->qf_lists[idx];
3000
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003001 qf_free_items(qi, idx);
3002
Bram Moolenaard23a8232018-02-10 18:45:26 +01003003 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003004 free_tv(qfl->qf_ctx);
3005 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003006 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003007 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003008}
3009
3010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 * qf_mark_adjust: adjust marks
3012 */
3013 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003014qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003015 win_T *wp,
3016 linenr_T line1,
3017 linenr_T line2,
3018 long amount,
3019 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003021 int i;
3022 qfline_T *qfp;
3023 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003024 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003025 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003026 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaarc1542742016-07-20 21:44:37 +02003028 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003029 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003030 if (wp != NULL)
3031 {
3032 if (wp->w_llist == NULL)
3033 return;
3034 qi = wp->w_llist;
3035 }
3036
3037 for (idx = 0; idx < qi->qf_listcount; ++idx)
3038 if (qi->qf_lists[idx].qf_count)
3039 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003040 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3041 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 if (qfp->qf_fnum == curbuf->b_fnum)
3043 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003044 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3046 {
3047 if (amount == MAXLNUM)
3048 qfp->qf_cleared = TRUE;
3049 else
3050 qfp->qf_lnum += amount;
3051 }
3052 else if (amount_after && qfp->qf_lnum > line2)
3053 qfp->qf_lnum += amount_after;
3054 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003055
3056 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003057 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058}
3059
3060/*
3061 * Make a nice message out of the error character and the error number:
3062 * char number message
3063 * e or E 0 " error"
3064 * w or W 0 " warning"
3065 * i or I 0 " info"
3066 * 0 0 ""
3067 * other 0 " c"
3068 * e or E n " error n"
3069 * w or W n " warning n"
3070 * i or I n " info n"
3071 * 0 n " error n"
3072 * other n " c n"
3073 * 1 x "" :helpgrep
3074 */
3075 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003076qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 static char_u buf[20];
3079 static char_u cc[3];
3080 char_u *p;
3081
3082 if (c == 'W' || c == 'w')
3083 p = (char_u *)" warning";
3084 else if (c == 'I' || c == 'i')
3085 p = (char_u *)" info";
3086 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3087 p = (char_u *)" error";
3088 else if (c == 0 || c == 1)
3089 p = (char_u *)"";
3090 else
3091 {
3092 cc[0] = ' ';
3093 cc[1] = c;
3094 cc[2] = NUL;
3095 p = cc;
3096 }
3097
3098 if (nr <= 0)
3099 return p;
3100
3101 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3102 return buf;
3103}
3104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105/*
3106 * ":cwindow": open the quickfix window if we have errors to display,
3107 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003108 * ":lwindow": open the location list window if we have locations to display,
3109 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 */
3111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003112ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003114 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 win_T *win;
3116
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003117 if (eap->cmdidx == CMD_lwindow)
3118 {
3119 qi = GET_LOC_LIST(curwin);
3120 if (qi == NULL)
3121 return;
3122 }
3123
3124 /* Look for an existing quickfix window. */
3125 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127 /*
3128 * If a quickfix window is open but we have no errors to display,
3129 * close the window. If a quickfix window is not open, then open
3130 * it if we have errors; otherwise, leave it closed.
3131 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003132 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003133 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003134 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 {
3136 if (win != NULL)
3137 ex_cclose(eap);
3138 }
3139 else if (win == NULL)
3140 ex_copen(eap);
3141}
3142
3143/*
3144 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003145 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003148ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003150 win_T *win = NULL;
3151 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003153 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3154 {
3155 qi = GET_LOC_LIST(curwin);
3156 if (qi == NULL)
3157 return;
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003160 /* Find existing quickfix window and close it. */
3161 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (win != NULL)
3163 win_close(win, FALSE);
3164}
3165
3166/*
3167 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003168 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 */
3170 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003171ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003173 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003176 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003177 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003178 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003180 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3181 {
3182 qi = GET_LOC_LIST(curwin);
3183 if (qi == NULL)
3184 {
3185 EMSG(_(e_loclist));
3186 return;
3187 }
3188 }
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (eap->addr_count != 0)
3191 height = eap->line2;
3192 else
3193 height = QF_WINHEIGHT;
3194
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#ifdef FEAT_GUI
3197 need_mouse_correct = TRUE;
3198#endif
3199
3200 /*
3201 * Find existing quickfix window, or open a new one.
3202 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003203 win = qf_find_win(qi);
3204
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003205 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003208 if (eap->addr_count != 0)
3209 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003210 if (cmdmod.split & WSP_VERT)
3211 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003212 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003213 win_setwidth(height);
3214 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003215 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003216 win_setheight(height);
3217 }
3218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 else
3220 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003221 int flags = 0;
3222
Bram Moolenaar9c102382006-05-03 21:26:49 +00003223 qf_buf = qf_find_buf(qi);
3224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 /* The current window becomes the previous window afterwards. */
3226 win = curwin;
3227
Bram Moolenaar77642c02012-11-20 17:55:10 +01003228 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3229 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003230 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003231 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003232 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003233 /* Default is to open the window below the current window */
3234 if (cmdmod.split == 0)
3235 flags = WSP_BELOW;
3236 flags |= WSP_NEWLOC;
3237 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003239 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003241 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003243 /*
3244 * For the location list window, create a reference to the
3245 * location list from the window 'win'.
3246 */
3247 curwin->w_llist_ref = win->w_llist;
3248 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003250
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003251 if (oldwin != curwin)
3252 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003253 if (qf_buf != NULL)
3254 /* Use the existing quickfix buffer */
3255 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003256 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003257 else
3258 {
3259 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003260 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003261 /* switch off 'swapfile' */
3262 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3263 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003264 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003265 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003266 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003267#ifdef FEAT_DIFF
3268 curwin->w_p_diff = FALSE;
3269#endif
3270#ifdef FEAT_FOLDING
3271 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3272 OPT_LOCAL);
3273#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003276 /* Only set the height when still in the same tab page and there is no
3277 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003278 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 win_setheight(height);
3280 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3281 if (win_valid(win))
3282 prevwin = win;
3283 }
3284
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003285 qf_set_title_var(qi);
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 /*
3288 * Fill the buffer with the quickfix list.
3289 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003290 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003292 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 curwin->w_cursor.col = 0;
3294 check_cursor();
3295 update_topline(); /* scroll to show the line */
3296}
3297
3298/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003299 * Move the cursor in the quickfix window to "lnum".
3300 */
3301 static void
3302qf_win_goto(win_T *win, linenr_T lnum)
3303{
3304 win_T *old_curwin = curwin;
3305
3306 curwin = win;
3307 curbuf = win->w_buffer;
3308 curwin->w_cursor.lnum = lnum;
3309 curwin->w_cursor.col = 0;
3310#ifdef FEAT_VIRTUALEDIT
3311 curwin->w_cursor.coladd = 0;
3312#endif
3313 curwin->w_curswant = 0;
3314 update_topline(); /* scroll to show the line */
3315 redraw_later(VALID);
3316 curwin->w_redr_status = TRUE; /* update ruler */
3317 curwin = old_curwin;
3318 curbuf = curwin->w_buffer;
3319}
3320
3321/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003322 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003323 */
3324 void
3325ex_cbottom(exarg_T *eap UNUSED)
3326{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003327 qf_info_T *qi = &ql_info;
3328 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003329
Bram Moolenaar537ef082016-07-09 17:56:19 +02003330 if (eap->cmdidx == CMD_lbottom)
3331 {
3332 qi = GET_LOC_LIST(curwin);
3333 if (qi == NULL)
3334 {
3335 EMSG(_(e_loclist));
3336 return;
3337 }
3338 }
3339
3340 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003341 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3342 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3343}
3344
3345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 * Return the number of the current entry (line number in the quickfix
3347 * window).
3348 */
3349 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003350qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003352 qf_info_T *qi = &ql_info;
3353
3354 if (IS_LL_WINDOW(wp))
3355 /* In the location list window, use the referenced location list */
3356 qi = wp->w_llist_ref;
3357
3358 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359}
3360
3361/*
3362 * Update the cursor position in the quickfix window to the current error.
3363 * Return TRUE if there is a quickfix window.
3364 */
3365 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003366qf_win_pos_update(
3367 qf_info_T *qi,
3368 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003371 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372
3373 /*
3374 * Put the cursor on the current error in the quickfix window, so that
3375 * it's viewable.
3376 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003377 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (win != NULL
3379 && qf_index <= win->w_buffer->b_ml.ml_line_count
3380 && old_qf_index != qf_index)
3381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (qf_index > old_qf_index)
3383 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003384 win->w_redraw_top = old_qf_index;
3385 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387 else
3388 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003389 win->w_redraw_top = qf_index;
3390 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003392 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 }
3394 return win != NULL;
3395}
3396
3397/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003398 * Check whether the given window is displaying the specified quickfix/location
3399 * list buffer
3400 */
3401 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003402is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003403{
3404 /*
3405 * A window displaying the quickfix buffer will have the w_llist_ref field
3406 * set to NULL.
3407 * A window displaying a location list buffer will have the w_llist_ref
3408 * pointing to the location list.
3409 */
3410 if (bt_quickfix(win->w_buffer))
3411 if ((qi == &ql_info && win->w_llist_ref == NULL)
3412 || (qi != &ql_info && win->w_llist_ref == qi))
3413 return TRUE;
3414
3415 return FALSE;
3416}
3417
3418/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003419 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003420 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003421 */
3422 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003423qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003424{
3425 win_T *win;
3426
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003427 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003428 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003429 return win;
3430 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003431}
3432
3433/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003434 * Find a quickfix buffer.
3435 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 */
3437 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003438qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003440 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003441 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar9c102382006-05-03 21:26:49 +00003443 FOR_ALL_TAB_WINDOWS(tp, win)
3444 if (is_qf_win(win, qi))
3445 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003446
Bram Moolenaar9c102382006-05-03 21:26:49 +00003447 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448}
3449
3450/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003451 * Update the w:quickfix_title variable in the quickfix/location list window
3452 */
3453 static void
3454qf_update_win_titlevar(qf_info_T *qi)
3455{
3456 win_T *win;
3457 win_T *curwin_save;
3458
3459 if ((win = qf_find_win(qi)) != NULL)
3460 {
3461 curwin_save = curwin;
3462 curwin = win;
3463 qf_set_title_var(qi);
3464 curwin = curwin_save;
3465 }
3466}
3467
3468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 * Find the quickfix buffer. If it exists, update the contents.
3470 */
3471 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003472qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
3474 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003475 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
3478 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (buf != NULL)
3481 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003482 linenr_T old_line_count = buf->b_ml.ml_line_count;
3483
3484 if (old_last == NULL)
3485 /* set curwin/curbuf to buf and save a few things */
3486 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
Bram Moolenaard823fa92016-08-12 16:29:27 +02003488 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003489
Bram Moolenaar864293a2016-06-02 13:40:04 +02003490 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003491 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003492
Bram Moolenaar864293a2016-06-02 13:40:04 +02003493 if (old_last == NULL)
3494 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003495 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003496
3497 /* restore curwin/curbuf and a few other things */
3498 aucmd_restbuf(&aco);
3499 }
3500
3501 /* Only redraw when added lines are visible. This avoids flickering
3502 * when the added lines are not visible. */
3503 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3504 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506}
3507
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003508/*
3509 * Set "w:quickfix_title" if "qi" has a title.
3510 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003511 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003512qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003513{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003514 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3515 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003516 qi->qf_lists[qi->qf_curlist].qf_title);
3517}
3518
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519/*
3520 * Fill current buffer with quickfix errors, replacing any previous contents.
3521 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003522 * If "old_last" is not NULL append the items after this one.
3523 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3524 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 */
3526 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003527qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003529 linenr_T lnum;
3530 qfline_T *qfp;
3531 buf_T *errbuf;
3532 int len;
3533 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaar864293a2016-06-02 13:40:04 +02003535 if (old_last == NULL)
3536 {
3537 if (buf != curbuf)
3538 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003539 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003540 return;
3541 }
3542
3543 /* delete all existing lines */
3544 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3545 (void)ml_delete((linenr_T)1, FALSE);
3546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
3548 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003549 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
3551 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003552 if (old_last == NULL)
3553 {
3554 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3555 lnum = 0;
3556 }
3557 else
3558 {
3559 qfp = old_last->qf_next;
3560 lnum = buf->b_ml.ml_line_count;
3561 }
3562 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
3564 if (qfp->qf_fnum != 0
3565 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3566 && errbuf->b_fname != NULL)
3567 {
3568 if (qfp->qf_type == 1) /* :helpgrep */
3569 STRCPY(IObuff, gettail(errbuf->b_fname));
3570 else
3571 STRCPY(IObuff, errbuf->b_fname);
3572 len = (int)STRLEN(IObuff);
3573 }
3574 else
3575 len = 0;
3576 IObuff[len++] = '|';
3577
3578 if (qfp->qf_lnum > 0)
3579 {
3580 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3581 len += (int)STRLEN(IObuff + len);
3582
3583 if (qfp->qf_col > 0)
3584 {
3585 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3586 len += (int)STRLEN(IObuff + len);
3587 }
3588
3589 sprintf((char *)IObuff + len, "%s",
3590 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3591 len += (int)STRLEN(IObuff + len);
3592 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003593 else if (qfp->qf_pattern != NULL)
3594 {
3595 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3596 len += (int)STRLEN(IObuff + len);
3597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 IObuff[len++] = '|';
3599 IObuff[len++] = ' ';
3600
3601 /* Remove newlines and leading whitespace from the text.
3602 * For an unrecognized line keep the indent, the compiler may
3603 * mark a word with ^^^^. */
3604 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3605 IObuff + len, IOSIZE - len);
3606
Bram Moolenaar864293a2016-06-02 13:40:04 +02003607 if (ml_append_buf(buf, lnum, IObuff,
3608 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003610 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003612 if (qfp == NULL)
3613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003615
3616 if (old_last == NULL)
3617 /* Delete the empty line which is now at the end */
3618 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
3620
3621 /* correct cursor position */
3622 check_lnums(TRUE);
3623
Bram Moolenaar864293a2016-06-02 13:40:04 +02003624 if (old_last == NULL)
3625 {
3626 /* Set the 'filetype' to "qf" each time after filling the buffer.
3627 * This resembles reading a file into a buffer, it's more logical when
3628 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003629#ifdef FEAT_AUTOCMD
3630 ++curbuf_lock;
3631#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003632 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3633 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
3635#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003636 keep_filetype = TRUE; /* don't detect 'filetype' */
3637 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003639 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003641 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003642 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003644 /* make sure it will be redrawn */
3645 redraw_curbuf_later(NOT_VALID);
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 /* Restore KeyTyped, setting 'filetype' may reset it. */
3649 KeyTyped = old_KeyTyped;
3650}
3651
Bram Moolenaarb254af32017-12-18 19:48:58 +01003652 static void
3653qf_list_changed(qf_info_T *qi, int qf_idx)
3654{
3655 qi->qf_lists[qf_idx].qf_changedtick++;
3656}
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003659 * Return TRUE when using ":vimgrep" for ":grep".
3660 */
3661 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003662grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003663{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003664 return ((cmdidx == CMD_grep
3665 || cmdidx == CMD_lgrep
3666 || cmdidx == CMD_grepadd
3667 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003668 && STRCMP("internal",
3669 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3670}
3671
3672/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003673 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
3675 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003676ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003678 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003680 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003682 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003683 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003684 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003685#ifdef FEAT_AUTOCMD
3686 char_u *au_name = NULL;
3687
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003688 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3689 if (grep_internal(eap->cmdidx))
3690 {
3691 ex_vimgrep(eap);
3692 return;
3693 }
3694
Bram Moolenaar7c626922005-02-07 22:01:03 +00003695 switch (eap->cmdidx)
3696 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003697 case CMD_make: au_name = (char_u *)"make"; break;
3698 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3699 case CMD_grep: au_name = (char_u *)"grep"; break;
3700 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3701 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3702 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003703 default: break;
3704 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003705 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3706 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003707 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003708# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003709 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003710 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003711# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003712 }
3713#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003714#ifdef FEAT_MBYTE
3715 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaara6557602006-02-04 22:43:20 +00003718 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3719 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003720 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003723 fname = get_mef_name();
3724 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003726 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727
3728 /*
3729 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3730 */
3731 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3732 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003733 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 cmd = alloc(len);
3735 if (cmd == NULL)
3736 return;
3737 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3738 (char *)p_shq);
3739 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003740 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 /*
3742 * Output a newline if there's something else than the :make command that
3743 * was typed (in which case the cursor is in column 0).
3744 */
3745 if (msg_col == 0)
3746 msg_didout = FALSE;
3747 msg_start();
3748 MSG_PUTS(":!");
3749 msg_outtrans(cmd); /* show what we are doing */
3750
3751 /* let the shell know if we are redirecting output or not */
3752 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3753
3754#ifdef AMIGA
3755 out_flush();
3756 /* read window status report and redraw before message */
3757 (void)char_avail();
3758#endif
3759
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003760 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003761 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3762 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003763 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003764 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003765 if (wp != NULL)
3766 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003767 if (res >= 0 && qi != NULL)
3768 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003769#ifdef FEAT_AUTOCMD
3770 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003771 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003772 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3773 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003774 if (qi->qf_curlist < qi->qf_listcount)
3775 res = qi->qf_lists[qi->qf_curlist].qf_count;
3776 else
3777 res = 0;
3778 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003779#endif
3780 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003781 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
Bram Moolenaar7c626922005-02-07 22:01:03 +00003783 mch_remove(fname);
3784 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 vim_free(cmd);
3786}
3787
3788/*
3789 * Return the name for the errorfile, in allocated memory.
3790 * Find a new unique name when 'makeef' contains "##".
3791 * Returns NULL for error.
3792 */
3793 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003794get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
3796 char_u *p;
3797 char_u *name;
3798 static int start = -1;
3799 static int off = 0;
3800#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003801 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802#endif
3803
3804 if (*p_mef == NUL)
3805 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003806 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (name == NULL)
3808 EMSG(_(e_notmp));
3809 return name;
3810 }
3811
3812 for (p = p_mef; *p; ++p)
3813 if (p[0] == '#' && p[1] == '#')
3814 break;
3815
3816 if (*p == NUL)
3817 return vim_strsave(p_mef);
3818
3819 /* Keep trying until the name doesn't exist yet. */
3820 for (;;)
3821 {
3822 if (start == -1)
3823 start = mch_get_pid();
3824 else
3825 off += 19;
3826
3827 name = alloc((unsigned)STRLEN(p_mef) + 30);
3828 if (name == NULL)
3829 break;
3830 STRCPY(name, p_mef);
3831 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3832 STRCAT(name, p + 2);
3833 if (mch_getperm(name) < 0
3834#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003835 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 && mch_lstat((char *)name, &sb) < 0
3837#endif
3838 )
3839 break;
3840 vim_free(name);
3841 }
3842 return name;
3843}
3844
3845/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003846 * Returns the number of valid entries in the current quickfix/location list.
3847 */
3848 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003849qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003850{
3851 qf_info_T *qi = &ql_info;
3852 qfline_T *qfp;
3853 int i, sz = 0;
3854 int prev_fnum = 0;
3855
3856 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3857 {
3858 /* Location list */
3859 qi = GET_LOC_LIST(curwin);
3860 if (qi == NULL)
3861 return 0;
3862 }
3863
3864 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003865 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003866 ++i, qfp = qfp->qf_next)
3867 {
3868 if (qfp->qf_valid)
3869 {
3870 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3871 sz++; /* Count all valid entries */
3872 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3873 {
3874 /* Count the number of files */
3875 sz++;
3876 prev_fnum = qfp->qf_fnum;
3877 }
3878 }
3879 }
3880
3881 return sz;
3882}
3883
3884/*
3885 * Returns the current index of the quickfix/location list.
3886 * Returns 0 if there is an error.
3887 */
3888 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003889qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003890{
3891 qf_info_T *qi = &ql_info;
3892
3893 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3894 {
3895 /* Location list */
3896 qi = GET_LOC_LIST(curwin);
3897 if (qi == NULL)
3898 return 0;
3899 }
3900
3901 return qi->qf_lists[qi->qf_curlist].qf_index;
3902}
3903
3904/*
3905 * Returns the current index in the quickfix/location list (counting only valid
3906 * entries). If no valid entries are in the list, then returns 1.
3907 */
3908 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003909qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003910{
3911 qf_info_T *qi = &ql_info;
3912 qf_list_T *qfl;
3913 qfline_T *qfp;
3914 int i, eidx = 0;
3915 int prev_fnum = 0;
3916
3917 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3918 {
3919 /* Location list */
3920 qi = GET_LOC_LIST(curwin);
3921 if (qi == NULL)
3922 return 1;
3923 }
3924
3925 qfl = &qi->qf_lists[qi->qf_curlist];
3926 qfp = qfl->qf_start;
3927
3928 /* check if the list has valid errors */
3929 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3930 return 1;
3931
3932 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3933 {
3934 if (qfp->qf_valid)
3935 {
3936 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3937 {
3938 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3939 {
3940 /* Count the number of files */
3941 eidx++;
3942 prev_fnum = qfp->qf_fnum;
3943 }
3944 }
3945 else
3946 eidx++;
3947 }
3948 }
3949
3950 return eidx ? eidx : 1;
3951}
3952
3953/*
3954 * Get the 'n'th valid error entry in the quickfix or location list.
3955 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3956 * For :cdo and :ldo returns the 'n'th valid error entry.
3957 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3958 */
3959 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003960qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003961{
3962 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3963 qfline_T *qfp = qfl->qf_start;
3964 int i, eidx;
3965 int prev_fnum = 0;
3966
3967 /* check if the list has valid errors */
3968 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3969 return 1;
3970
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003971 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003972 i++, qfp = qfp->qf_next)
3973 {
3974 if (qfp->qf_valid)
3975 {
3976 if (fdo)
3977 {
3978 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3979 {
3980 /* Count the number of files */
3981 eidx++;
3982 prev_fnum = qfp->qf_fnum;
3983 }
3984 }
3985 else
3986 eidx++;
3987 }
3988
3989 if (eidx == n)
3990 break;
3991 }
3992
3993 if (i <= qfl->qf_count)
3994 return i;
3995 else
3996 return 1;
3997}
3998
3999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004001 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004002 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 */
4004 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004005ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004007 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004008 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004009
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004010 if (eap->cmdidx == CMD_ll
4011 || eap->cmdidx == CMD_lrewind
4012 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004013 || eap->cmdidx == CMD_llast
4014 || eap->cmdidx == CMD_ldo
4015 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004016 {
4017 qi = GET_LOC_LIST(curwin);
4018 if (qi == NULL)
4019 {
4020 EMSG(_(e_loclist));
4021 return;
4022 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004023 }
4024
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004025 if (eap->addr_count > 0)
4026 errornr = (int)eap->line2;
4027 else
4028 {
4029 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4030 errornr = 0;
4031 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4032 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4033 errornr = 1;
4034 else
4035 errornr = 32767;
4036 }
4037
4038 /* For cdo and ldo commands, jump to the nth valid error.
4039 * For cfdo and lfdo commands, jump to the nth valid file entry.
4040 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004041 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4042 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004043 errornr = qf_get_nth_valid_entry(qi,
4044 eap->addr_count > 0 ? (int)eap->line1 : 1,
4045 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4046
4047 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048}
4049
4050/*
4051 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004052 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004053 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 */
4055 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004056ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004058 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004059 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004060
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004061 if (eap->cmdidx == CMD_lnext
4062 || eap->cmdidx == CMD_lNext
4063 || eap->cmdidx == CMD_lprevious
4064 || eap->cmdidx == CMD_lnfile
4065 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004066 || eap->cmdidx == CMD_lpfile
4067 || eap->cmdidx == CMD_ldo
4068 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004069 {
4070 qi = GET_LOC_LIST(curwin);
4071 if (qi == NULL)
4072 {
4073 EMSG(_(e_loclist));
4074 return;
4075 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004076 }
4077
Bram Moolenaar55b69262017-08-13 13:42:01 +02004078 if (eap->addr_count > 0
4079 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4080 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004081 errornr = (int)eap->line2;
4082 else
4083 errornr = 1;
4084
4085 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4086 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004088 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4089 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004091 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4092 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 ? BACKWARD_FILE
4094 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004095 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096}
4097
4098/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004099 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004100 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 */
4102 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004103ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004105 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004106 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004107 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004108#ifdef FEAT_AUTOCMD
4109 char_u *au_name = NULL;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004110 int save_qfid;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004111#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004112 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004113
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004114#ifdef FEAT_AUTOCMD
4115 switch (eap->cmdidx)
4116 {
4117 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4118 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4119 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4120 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4121 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4122 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4123 default: break;
4124 }
4125 if (au_name != NULL)
4126 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
4127#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004128#ifdef FEAT_MBYTE
4129 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4130#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004131#ifdef FEAT_BROWSE
4132 if (cmdmod.browse)
4133 {
4134 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4135 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4136 if (browse_file == NULL)
4137 return;
4138 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4139 vim_free(browse_file);
4140 }
4141 else
4142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004144 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004145
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004146 if (eap->cmdidx == CMD_lfile
4147 || eap->cmdidx == CMD_lgetfile
4148 || eap->cmdidx == CMD_laddfile)
4149 wp = curwin;
4150
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004151 /*
4152 * This function is used by the :cfile, :cgetfile and :caddfile
4153 * commands.
4154 * :cfile always creates a new quickfix list and jumps to the
4155 * first error.
4156 * :cgetfile creates a new quickfix list but doesn't jump to the
4157 * first error.
4158 * :caddfile adds to an existing quickfix list. If there is no
4159 * quickfix list then a new list is created.
4160 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004161 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4162 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004163 if (wp != NULL)
4164 qi = GET_LOC_LIST(wp);
4165 if (res >= 0 && qi != NULL)
4166 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004167#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004168 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004169 if (au_name != NULL)
4170 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004171 /*
4172 * Autocmd might have freed the quickfix/location list. Check whether it is
4173 * still valid
4174 */
4175 if (!qflist_valid(wp, save_qfid))
4176 return;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004177#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004178 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
4179 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004180 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182}
4183
4184/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004185 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004186 * ":vimgrepadd {pattern} file(s)"
4187 * ":lvimgrep {pattern} file(s)"
4188 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004189 */
4190 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004191ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004192{
Bram Moolenaar81695252004-12-29 20:58:21 +00004193 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004194 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004195 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004196 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004197 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004198 char_u *s;
4199 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004200 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004201 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004202 int loclist_cmd = FALSE;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004203#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004204 int_u save_qfid;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004205 qfline_T *cur_qf_start;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004206 win_T *wp;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004207#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004208 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004209 buf_T *buf;
4210 int duplicate_name = FALSE;
4211 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004212 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004213 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004214 buf_T *first_match_buf = NULL;
4215 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004216 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004217#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4218 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004219#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004220 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004221 int flags = 0;
4222 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004223 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004224 char_u *dirname_start = NULL;
4225 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004226 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004227#ifdef FEAT_AUTOCMD
4228 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004229
4230 switch (eap->cmdidx)
4231 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004232 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4233 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4234 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004235 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004236 case CMD_grep: au_name = (char_u *)"grep"; break;
4237 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4238 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4239 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004240 default: break;
4241 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004242 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4243 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004244 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004245# ifdef FEAT_EVAL
4246 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004247 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004248# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004249 }
4250#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004251
Bram Moolenaar754b5602006-02-09 23:53:20 +00004252 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004253 || eap->cmdidx == CMD_lvimgrep
4254 || eap->cmdidx == CMD_lgrepadd
4255 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004256 {
4257 qi = ll_get_or_alloc_list(curwin);
4258 if (qi == NULL)
4259 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004260 loclist_cmd = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00004261 }
4262
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004263 if (eap->addr_count > 0)
4264 tomatch = eap->line2;
4265 else
4266 tomatch = MAXLNUM;
4267
Bram Moolenaar81695252004-12-29 20:58:21 +00004268 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004269 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004270 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004271 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004272 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004273 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004274 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004275 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004276 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004277
4278 if (s != NULL && *s == NUL)
4279 {
4280 /* Pattern is empty, use last search pattern. */
4281 if (last_search_pat() == NULL)
4282 {
4283 EMSG(_(e_noprevre));
4284 goto theend;
4285 }
4286 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4287 }
4288 else
4289 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4290
Bram Moolenaar86b68352004-12-27 21:59:20 +00004291 if (regmatch.regprog == NULL)
4292 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004293 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004294 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004295
4296 p = skipwhite(p);
4297 if (*p == NUL)
4298 {
4299 EMSG(_("E683: File name missing or invalid pattern"));
4300 goto theend;
4301 }
4302
Bram Moolenaar55b69262017-08-13 13:42:01 +02004303 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4304 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004305 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004306 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004307 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004308
4309 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004310 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004311 goto theend;
4312 if (fcount == 0)
4313 {
4314 EMSG(_(e_nomatch));
4315 goto theend;
4316 }
4317
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004318 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4319 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004320 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004321 {
4322 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004323 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004324 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004325
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004326 /* Remember the current directory, because a BufRead autocommand that does
4327 * ":lcd %:p:h" changes the meaning of short path names. */
4328 mch_dirname(dirname_start, MAXPATHL);
4329
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004330#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004331 /* Remember the current values of the quickfix list and qf_start, so that
4332 * we can check for autocommands changing the current quickfix list. */
4333 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004334 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4335#endif
4336
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004337 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004338 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004339 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004340 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004341 if (time(NULL) > seconds)
4342 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004343 /* Display the file name every second or so, show the user we are
4344 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004345 seconds = time(NULL);
4346 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004347 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004348 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004349 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004350 else
4351 {
4352 msg_outtrans(p);
4353 vim_free(p);
4354 }
4355 msg_clr_eos();
4356 msg_didout = FALSE; /* overwrite this message */
4357 msg_nowait = TRUE; /* don't wait for this message */
4358 msg_col = 0;
4359 out_flush();
4360 }
4361
Bram Moolenaar81695252004-12-29 20:58:21 +00004362 buf = buflist_findname_exp(fnames[fi]);
4363 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4364 {
4365 /* Remember that a buffer with this name already exists. */
4366 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004367 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004368 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004369
4370#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4371 /* Don't do Filetype autocommands to avoid loading syntax and
4372 * indent scripts, a great speed improvement. */
4373 save_ei = au_event_disable(",Filetype");
4374#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004375 /* Don't use modelines here, it's useless. */
4376 save_mls = p_mls;
4377 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004378
4379 /* Load file into a buffer, so that 'fileencoding' is detected,
4380 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004381 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004382
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004383 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004384#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4385 au_event_restore(save_ei);
4386#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004387 }
4388 else
4389 /* Use existing, loaded buffer. */
4390 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004391
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004392#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004393 if (loclist_cmd)
4394 {
4395 /*
4396 * Verify that the location list is still valid. An autocmd might
4397 * have freed the location list.
4398 */
4399 if (!qflist_valid(curwin, save_qfid))
4400 {
4401 EMSG(_(e_loc_list_changed));
4402 goto theend;
4403 }
4404 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004405 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4406 {
4407 int idx;
4408
4409 /* Autocommands changed the quickfix list. Find the one we were
4410 * using and restore it. */
4411 for (idx = 0; idx < LISTCOUNT; ++idx)
4412 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4413 {
4414 qi->qf_curlist = idx;
4415 break;
4416 }
4417 if (idx == LISTCOUNT)
4418 {
4419 /* List cannot be found, create a new one. */
4420 qf_new_list(qi, *eap->cmdlinep);
4421 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4422 }
4423 }
4424#endif
4425
Bram Moolenaar81695252004-12-29 20:58:21 +00004426 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004427 {
4428 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004429 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004430 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004431 else
4432 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004433 /* Try for a match in all lines of the buffer.
4434 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004435 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004436 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4437 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004438 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004439 col = 0;
4440 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004441 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004442 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004443 /* Pass the buffer number so that it gets used even for a
4444 * dummy buffer, unless duplicate_name is set, then the
4445 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004446 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004447 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004448 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004449 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004450 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004451 ml_get_buf(buf,
4452 regmatch.startpos[0].lnum + lnum, FALSE),
4453 regmatch.startpos[0].lnum + lnum,
4454 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004455 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004456 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004457 0, /* nr */
4458 0, /* type */
4459 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004460 ) == FAIL)
4461 {
4462 got_int = TRUE;
4463 break;
4464 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004465 found_match = TRUE;
4466 if (--tomatch == 0)
4467 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004468 if ((flags & VGR_GLOBAL) == 0
4469 || regmatch.endpos[0].lnum > 0)
4470 break;
4471 col = regmatch.endpos[0].col
4472 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004473 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004474 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004475 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004476 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004477 if (got_int)
4478 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004479 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004480#ifdef FEAT_AUTOCMD
4481 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4482#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004483
4484 if (using_dummy)
4485 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004486 if (found_match && first_match_buf == NULL)
4487 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004488 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004489 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004490 /* Never keep a dummy buffer if there is another buffer
4491 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004492 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004493 buf = NULL;
4494 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004495 else if (!cmdmod.hide
4496 || buf->b_p_bh[0] == 'u' /* "unload" */
4497 || buf->b_p_bh[0] == 'w' /* "wipe" */
4498 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004499 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004500 /* When no match was found we don't need to remember the
4501 * buffer, wipe it out. If there was a match and it
4502 * wasn't the first one or we won't jump there: only
4503 * unload the buffer.
4504 * Ignore 'hidden' here, because it may lead to having too
4505 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004506 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004507 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004508 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004509 buf = NULL;
4510 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004511 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004512 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004513 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004514 /* Keeping the buffer, remove the dummy flag. */
4515 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004516 buf = NULL;
4517 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004518 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004519
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004520 if (buf != NULL)
4521 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004522 /* Keeping the buffer, remove the dummy flag. */
4523 buf->b_flags &= ~BF_DUMMY;
4524
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004525 /* If the buffer is still loaded we need to use the
4526 * directory we jumped to below. */
4527 if (buf == first_match_buf
4528 && target_dir == NULL
4529 && STRCMP(dirname_start, dirname_now) != 0)
4530 target_dir = vim_strsave(dirname_now);
4531
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004532 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004533 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004534 * need to be done (again). But not the window-local
4535 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004536 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004537#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004538 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4539 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004540#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004541 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004542 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004543 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004544 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004545 }
4546 }
4547
4548 FreeWild(fcount, fnames);
4549
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004550 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4551 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4552 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004553 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004554
Bram Moolenaar864293a2016-06-02 13:40:04 +02004555 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004556
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004557#ifdef FEAT_AUTOCMD
4558 if (au_name != NULL)
4559 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4560 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004561 /*
4562 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4563 * is still valid.
4564 */
4565 wp = loclist_cmd ? curwin : NULL;
4566 if (!qflist_valid(wp, save_qfid))
4567 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004568#endif
4569
Bram Moolenaar86b68352004-12-27 21:59:20 +00004570 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004571 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004572 {
4573 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004574 {
4575 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004576 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004577 if (buf != curbuf)
4578 /* If we jumped to another buffer redrawing will already be
4579 * taken care of. */
4580 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004581
4582 /* Jump to the directory used after loading the buffer. */
4583 if (curbuf == first_match_buf && target_dir != NULL)
4584 {
4585 exarg_T ea;
4586
4587 ea.arg = target_dir;
4588 ea.cmdidx = CMD_lcd;
4589 ex_cd(&ea);
4590 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004591 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004592 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004593 else
4594 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004595
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004596 /* If we loaded a dummy buffer into the current window, the autocommands
4597 * may have messed up things, need to redraw and recompute folds. */
4598 if (redraw_for_dummy)
4599 {
4600#ifdef FEAT_FOLDING
4601 foldUpdateAll(curwin);
4602#else
4603 redraw_later(NOT_VALID);
4604#endif
4605 }
4606
Bram Moolenaar86b68352004-12-27 21:59:20 +00004607theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004608 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004609 vim_free(dirname_now);
4610 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004611 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004612 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004613}
4614
4615/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004616 * Restore current working directory to "dirname_start" if they differ, taking
4617 * into account whether it is set locally or globally.
4618 */
4619 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004620restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004621{
4622 char_u *dirname_now = alloc(MAXPATHL);
4623
4624 if (NULL != dirname_now)
4625 {
4626 mch_dirname(dirname_now, MAXPATHL);
4627 if (STRCMP(dirname_start, dirname_now) != 0)
4628 {
4629 /* If the directory has changed, change it back by building up an
4630 * appropriate ex command and executing it. */
4631 exarg_T ea;
4632
4633 ea.arg = dirname_start;
4634 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4635 ex_cd(&ea);
4636 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004637 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004638 }
4639}
4640
4641/*
4642 * Load file "fname" into a dummy buffer and return the buffer pointer,
4643 * placing the directory resulting from the buffer load into the
4644 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4645 * prior to calling this function. Restores directory to "dirname_start" prior
4646 * to returning, if autocmds or the 'autochdir' option have changed it.
4647 *
4648 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4649 * or wipe_dummy_buffer() later!
4650 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004651 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004652 */
4653 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004654load_dummy_buffer(
4655 char_u *fname,
4656 char_u *dirname_start, /* in: old directory */
4657 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004658{
4659 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004660 bufref_T newbufref;
4661 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004662 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004663 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004664 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004665
4666 /* Allocate a buffer without putting it in the buffer list. */
4667 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4668 if (newbuf == NULL)
4669 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004670 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004671
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004672 /* Init the options. */
4673 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4674
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004675 /* need to open the memfile before putting the buffer in a window */
4676 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004677 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004678 /* Make sure this buffer isn't wiped out by auto commands. */
4679 ++newbuf->b_locked;
4680
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004681 /* set curwin/curbuf to buf and save a few things */
4682 aucmd_prepbuf(&aco, newbuf);
4683
4684 /* Need to set the filename for autocommands. */
4685 (void)setfname(curbuf, fname, NULL, FALSE);
4686
Bram Moolenaar81695252004-12-29 20:58:21 +00004687 /* Create swap file now to avoid the ATTENTION message. */
4688 check_need_swap(TRUE);
4689
4690 /* Remove the "dummy" flag, otherwise autocommands may not
4691 * work. */
4692 curbuf->b_flags &= ~BF_DUMMY;
4693
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004694 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004695 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004696 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004697 NULL, READ_NEW | READ_DUMMY);
4698 --newbuf->b_locked;
4699 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004700 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004701 && !(curbuf->b_flags & BF_NEW))
4702 {
4703 failed = FALSE;
4704 if (curbuf != newbuf)
4705 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004706 /* Bloody autocommands changed the buffer! Can happen when
4707 * using netrw and editing a remote file. Use the current
4708 * buffer instead, delete the dummy one after restoring the
4709 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004710 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004711 newbuf = curbuf;
4712 }
4713 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004714
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004715 /* restore curwin/curbuf and a few other things */
4716 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004717 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4718 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004719
4720 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4721 * skip it. */
4722 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004723 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004724
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004725 /*
4726 * When autocommands/'autochdir' option changed directory: go back.
4727 * Let the caller know what the resulting dir was first, in case it is
4728 * important.
4729 */
4730 mch_dirname(resulting_dir, MAXPATHL);
4731 restore_start_dir(dirname_start);
4732
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004733 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004734 return NULL;
4735 if (failed)
4736 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004737 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004738 return NULL;
4739 }
4740 return newbuf;
4741}
4742
4743/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004744 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4745 * directory to "dirname_start" prior to returning, if autocmds or the
4746 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004747 */
4748 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004749wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004750{
4751 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004752 {
4753#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4754 cleanup_T cs;
4755
4756 /* Reset the error/interrupt/exception state here so that aborting()
4757 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4758 * work when got_int is set. */
4759 enter_cleanup(&cs);
4760#endif
4761
Bram Moolenaar81695252004-12-29 20:58:21 +00004762 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004763
4764#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4765 /* Restore the error/interrupt/exception state if not discarded by a
4766 * new aborting error, interrupt, or uncaught exception. */
4767 leave_cleanup(&cs);
4768#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004769 /* When autocommands/'autochdir' option changed directory: go back. */
4770 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004771 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004772}
4773
4774/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004775 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4776 * directory to "dirname_start" prior to returning, if autocmds or the
4777 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004778 */
4779 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004780unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004781{
4782 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004783 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004784 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004785
4786 /* When autocommands/'autochdir' option changed directory: go back. */
4787 restore_start_dir(dirname_start);
4788 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004789}
4790
Bram Moolenaar05159a02005-02-26 23:04:13 +00004791#if defined(FEAT_EVAL) || defined(PROTO)
4792/*
4793 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004794 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004795 */
4796 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004797get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004798{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004799 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004800 dict_T *dict;
4801 char_u buf[2];
4802 qfline_T *qfp;
4803 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004804 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004805
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004806 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004807 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004808 qi = &ql_info;
4809 if (wp != NULL)
4810 {
4811 qi = GET_LOC_LIST(wp);
4812 if (qi == NULL)
4813 return FAIL;
4814 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004815 }
4816
Bram Moolenaard823fa92016-08-12 16:29:27 +02004817 if (qf_idx == -1)
4818 qf_idx = qi->qf_curlist;
4819
4820 if (qf_idx >= qi->qf_listcount
4821 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004822 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004823
Bram Moolenaard823fa92016-08-12 16:29:27 +02004824 qfp = qi->qf_lists[qf_idx].qf_start;
4825 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004826 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004827 /* Handle entries with a non-existing buffer number. */
4828 bufnum = qfp->qf_fnum;
4829 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4830 bufnum = 0;
4831
Bram Moolenaar05159a02005-02-26 23:04:13 +00004832 if ((dict = dict_alloc()) == NULL)
4833 return FAIL;
4834 if (list_append_dict(list, dict) == FAIL)
4835 return FAIL;
4836
4837 buf[0] = qfp->qf_type;
4838 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004839 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004840 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4841 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4842 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4843 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004844 || dict_add_nr_str(dict, "pattern", 0L,
4845 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4846 || dict_add_nr_str(dict, "text", 0L,
4847 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004848 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4849 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4850 return FAIL;
4851
4852 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004853 if (qfp == NULL)
4854 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004855 }
4856 return OK;
4857}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004858
4859/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004860 * Flags used by getqflist()/getloclist() to determine which fields to return.
4861 */
4862enum {
4863 QF_GETLIST_NONE = 0x0,
4864 QF_GETLIST_TITLE = 0x1,
4865 QF_GETLIST_ITEMS = 0x2,
4866 QF_GETLIST_NR = 0x4,
4867 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004868 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004869 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004870 QF_GETLIST_IDX = 0x40,
4871 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01004872 QF_GETLIST_TICK = 0x100,
4873 QF_GETLIST_ALL = 0x1FF
Bram Moolenaard823fa92016-08-12 16:29:27 +02004874};
4875
4876/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004877 * Parse text from 'di' and return the quickfix list items
4878 */
4879 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004880qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004881{
4882 int status = FAIL;
4883 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004884 char_u *errorformat = p_efm;
4885 dictitem_T *efm_di;
4886 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004887
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004888 /* Only a List value is supported */
4889 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004890 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004891 /* If errorformat is supplied then use it, otherwise use the 'efm'
4892 * option setting
4893 */
4894 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4895 {
4896 if (efm_di->di_tv.v_type != VAR_STRING ||
4897 efm_di->di_tv.vval.v_string == NULL)
4898 return FAIL;
4899 errorformat = efm_di->di_tv.vval.v_string;
4900 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004901
Bram Moolenaar36538222017-09-02 19:51:44 +02004902 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004903 if (l == NULL)
4904 return FAIL;
4905
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004906 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4907 if (qi != NULL)
4908 {
4909 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4910 qi->qf_refcount++;
4911
Bram Moolenaar36538222017-09-02 19:51:44 +02004912 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004913 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4914 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004915 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004916 qf_free(qi, 0);
4917 }
4918 free(qi);
4919 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004920 dict_add_list(retdict, "items", l);
4921 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004922 }
4923
4924 return status;
4925}
4926
4927/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004928 * Return the quickfix/location list number with the given identifier.
4929 * Returns -1 if list is not found.
4930 */
4931 static int
4932qf_id2nr(qf_info_T *qi, int_u qfid)
4933{
4934 int qf_idx;
4935
4936 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4937 if (qi->qf_lists[qf_idx].qf_id == qfid)
4938 return qf_idx;
4939 return -1;
4940}
4941
4942/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004943 * Return the quickfix/location list window identifier in the current tabpage.
4944 */
4945 static int
4946qf_winid(qf_info_T *qi)
4947{
4948 win_T *win;
4949
4950 /* The quickfix window can be opened even if the quickfix list is not set
4951 * using ":copen". This is not true for location lists. */
4952 if (qi == NULL)
4953 return 0;
4954 win = qf_find_win(qi);
4955 if (win != NULL)
4956 return win->w_id;
4957 return 0;
4958}
4959
4960/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004961 * Return quickfix/location list details (title) as a
4962 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4963 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004964 */
4965 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004966qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004967{
4968 qf_info_T *qi = &ql_info;
4969 int status = OK;
4970 int qf_idx;
4971 dictitem_T *di;
4972 int flags = QF_GETLIST_NONE;
4973
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004974 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004975 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004976
Bram Moolenaard823fa92016-08-12 16:29:27 +02004977 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004978 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004979
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004980 if (dict_find(what, (char_u *)"all", -1) != NULL)
4981 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004982
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004983 if (dict_find(what, (char_u *)"title", -1) != NULL)
4984 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004985
Bram Moolenaara6d48492017-12-12 22:45:31 +01004986 if (dict_find(what, (char_u *)"nr", -1) != NULL)
4987 flags |= QF_GETLIST_NR;
4988
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004989 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4990 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004991
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004992 if (dict_find(what, (char_u *)"context", -1) != NULL)
4993 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004994
Bram Moolenaara6d48492017-12-12 22:45:31 +01004995 if (dict_find(what, (char_u *)"id", -1) != NULL)
4996 flags |= QF_GETLIST_ID;
4997
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004998 if (dict_find(what, (char_u *)"items", -1) != NULL)
4999 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005000
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005001 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5002 flags |= QF_GETLIST_IDX;
5003
5004 if (dict_find(what, (char_u *)"size", -1) != NULL)
5005 flags |= QF_GETLIST_SIZE;
5006
Bram Moolenaarb254af32017-12-18 19:48:58 +01005007 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5008 flags |= QF_GETLIST_TICK;
5009
Bram Moolenaara6d48492017-12-12 22:45:31 +01005010 if (qi != NULL && qi->qf_listcount != 0)
5011 {
5012 qf_idx = qi->qf_curlist; /* default is the current list */
5013 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5014 {
5015 /* Use the specified quickfix/location list */
5016 if (di->di_tv.v_type == VAR_NUMBER)
5017 {
5018 /* for zero use the current list */
5019 if (di->di_tv.vval.v_number != 0)
5020 {
5021 qf_idx = di->di_tv.vval.v_number - 1;
5022 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
5023 qf_idx = -1;
5024 }
5025 }
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005026 else if (di->di_tv.v_type == VAR_STRING
5027 && di->di_tv.vval.v_string != NULL
5028 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005029 /* Get the last quickfix list number */
5030 qf_idx = qi->qf_listcount - 1;
5031 else
5032 qf_idx = -1;
5033 flags |= QF_GETLIST_NR;
5034 }
5035
5036 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5037 {
5038 /* Look for a list with the specified id */
5039 if (di->di_tv.v_type == VAR_NUMBER)
5040 {
5041 /*
5042 * For zero, use the current list or the list specifed by 'nr'
5043 */
5044 if (di->di_tv.vval.v_number != 0)
5045 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5046 flags |= QF_GETLIST_ID;
5047 }
5048 else
5049 qf_idx = -1;
5050 }
5051 }
5052
5053 /* List is not present or is empty */
5054 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
5055 {
5056 if (flags & QF_GETLIST_TITLE)
5057 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5058 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5059 {
5060 list_T *l = list_alloc();
5061 if (l != NULL)
5062 status = dict_add_list(retdict, "items", l);
5063 else
5064 status = FAIL;
5065 }
5066 if ((status == OK) && (flags & QF_GETLIST_NR))
5067 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5068 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005069 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005070 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5071 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5072 if ((status == OK) && (flags & QF_GETLIST_ID))
5073 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5074 if ((status == OK) && (flags & QF_GETLIST_IDX))
5075 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5076 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5077 status = dict_add_nr_str(retdict, "size", 0L, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005078 if ((status == OK) && (flags & QF_GETLIST_TICK))
5079 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005080
5081 return status;
5082 }
5083
Bram Moolenaard823fa92016-08-12 16:29:27 +02005084 if (flags & QF_GETLIST_TITLE)
5085 {
5086 char_u *t;
5087 t = qi->qf_lists[qf_idx].qf_title;
5088 if (t == NULL)
5089 t = (char_u *)"";
5090 status = dict_add_nr_str(retdict, "title", 0L, t);
5091 }
5092 if ((status == OK) && (flags & QF_GETLIST_NR))
5093 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5094 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005095 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005096 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5097 {
5098 list_T *l = list_alloc();
5099 if (l != NULL)
5100 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005101 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005102 dict_add_list(retdict, "items", l);
5103 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005104 else
5105 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005106 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005107
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005108 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5109 {
5110 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5111 {
5112 di = dictitem_alloc((char_u *)"context");
5113 if (di != NULL)
5114 {
5115 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005116 status = dict_add(retdict, di);
5117 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02005118 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005119 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005120 else
5121 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005122 }
5123 else
5124 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5125 }
5126
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005127 if ((status == OK) && (flags & QF_GETLIST_ID))
5128 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5129 NULL);
5130
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005131 if ((status == OK) && (flags & QF_GETLIST_IDX))
5132 {
5133 int idx = qi->qf_lists[qf_idx].qf_index;
5134 if (qi->qf_lists[qf_idx].qf_count == 0)
5135 /* For empty lists, qf_index is set to 1 */
5136 idx = 0;
5137 status = dict_add_nr_str(retdict, "idx", idx, NULL);
5138 }
5139
5140 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5141 status = dict_add_nr_str(retdict, "size",
5142 qi->qf_lists[qf_idx].qf_count, NULL);
5143
Bram Moolenaarb254af32017-12-18 19:48:58 +01005144 if ((status == OK) && (flags & QF_GETLIST_TICK))
5145 status = dict_add_nr_str(retdict, "changedtick",
5146 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5147
Bram Moolenaard823fa92016-08-12 16:29:27 +02005148 return status;
5149}
5150
5151/*
5152 * Add list of entries to quickfix/location list. Each list entry is
5153 * a dictionary with item information.
5154 */
5155 static int
5156qf_add_entries(
5157 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005158 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005159 list_T *list,
5160 char_u *title,
5161 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005162{
5163 listitem_T *li;
5164 dict_T *d;
5165 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005166 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005167 long lnum;
5168 int col, nr;
5169 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005170 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005171 int valid, status;
5172 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005173 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005174
Bram Moolenaara3921f42017-06-04 15:30:34 +02005175 if (action == ' ' || qf_idx == qi->qf_listcount)
5176 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005177 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005178 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005179 qf_idx = qi->qf_curlist;
5180 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005181 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005182 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005183 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005184 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005185 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005186 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005187 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005188 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005189
5190 for (li = list->lv_first; li != NULL; li = li->li_next)
5191 {
5192 if (li->li_tv.v_type != VAR_DICT)
5193 continue; /* Skip non-dict items */
5194
5195 d = li->li_tv.vval.v_dict;
5196 if (d == NULL)
5197 continue;
5198
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005199 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005200 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5201 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5202 col = (int)get_dict_number(d, (char_u *)"col");
5203 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5204 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005205 type = get_dict_string(d, (char_u *)"type", TRUE);
5206 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5207 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005208 if (text == NULL)
5209 text = vim_strsave((char_u *)"");
5210
5211 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005212 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005213 valid = FALSE;
5214
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005215 /* Mark entries with non-existing buffer number as not valid. Give the
5216 * error message only once. */
5217 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5218 {
5219 if (!did_bufnr_emsg)
5220 {
5221 did_bufnr_emsg = TRUE;
5222 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5223 }
5224 valid = FALSE;
5225 bufnum = 0;
5226 }
5227
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005228 /* If the 'valid' field is present it overrules the detected value. */
5229 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5230 valid = (int)get_dict_number(d, (char_u *)"valid");
5231
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005232 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005233 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005234 NULL, /* dir */
5235 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005236 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005237 text,
5238 lnum,
5239 col,
5240 vcol, /* vis_col */
5241 pattern, /* search pattern */
5242 nr,
5243 type == NULL ? NUL : *type,
5244 valid);
5245
5246 vim_free(filename);
5247 vim_free(pattern);
5248 vim_free(text);
5249 vim_free(type);
5250
5251 if (status == FAIL)
5252 {
5253 retval = FAIL;
5254 break;
5255 }
5256 }
5257
Bram Moolenaara3921f42017-06-04 15:30:34 +02005258 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005259 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005260 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005261 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005262 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005263 if (action != 'a')
5264 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005265 qi->qf_lists[qf_idx].qf_ptr =
5266 qi->qf_lists[qf_idx].qf_start;
5267 if (qi->qf_lists[qf_idx].qf_count > 0)
5268 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005269 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005270
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005271 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005272 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005273
5274 return retval;
5275}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005276
5277 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005278qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005279{
5280 dictitem_T *di;
5281 int retval = FAIL;
5282 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005283 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005284 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005285
5286 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5287 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005288
5289 qf_idx = qi->qf_curlist; /* default is the current list */
5290 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5291 {
5292 /* Use the specified quickfix/location list */
5293 if (di->di_tv.v_type == VAR_NUMBER)
5294 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005295 /* for zero use the current list */
5296 if (di->di_tv.vval.v_number != 0)
5297 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005298
Bram Moolenaar55b69262017-08-13 13:42:01 +02005299 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5300 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005301 /*
5302 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005303 * non-available list and add the new list at the end of the
5304 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005305 */
5306 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005307 qf_idx = qi->qf_listcount - 1;
5308 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005309 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005310 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005311 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005312 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005313 }
5314 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005315 && di->di_tv.vval.v_string != NULL
5316 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005317 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005318 if (qi->qf_listcount > 0)
5319 qf_idx = qi->qf_listcount - 1;
5320 else if (newlist)
5321 qf_idx = 0;
5322 else
5323 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005324 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005325 else
5326 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005327 }
5328
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005329 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5330 {
5331 /* Use the quickfix/location list with the specified id */
5332 if (di->di_tv.v_type == VAR_NUMBER)
5333 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005334 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5335 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005336 return FAIL; /* List not found */
5337 }
5338 else
5339 return FAIL;
5340 }
5341
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005342 if (newlist)
5343 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005344 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005345 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005346 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005347 }
5348
5349 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5350 {
5351 if (di->di_tv.v_type == VAR_STRING)
5352 {
5353 vim_free(qi->qf_lists[qf_idx].qf_title);
5354 qi->qf_lists[qf_idx].qf_title =
5355 get_dict_string(what, (char_u *)"title", TRUE);
5356 if (qf_idx == qi->qf_curlist)
5357 qf_update_win_titlevar(qi);
5358 retval = OK;
5359 }
5360 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005361
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005362 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5363 {
5364 if (di->di_tv.v_type == VAR_LIST)
5365 {
5366 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5367
5368 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5369 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005370 if (action == 'r')
5371 {
5372 /*
5373 * When replacing the quickfix list entries using
5374 * qf_add_entries(), the title is set with a ':' prefix.
5375 * Restore the title with the saved title.
5376 */
5377 vim_free(qi->qf_lists[qf_idx].qf_title);
5378 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5379 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005380 vim_free(title_save);
5381 }
5382 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005383
Bram Moolenaar36538222017-09-02 19:51:44 +02005384 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5385 {
5386 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5387 return FAIL;
5388 errorformat = di->di_tv.vval.v_string;
5389 }
5390
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005391 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005392 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005393 /* Only a List value is supported */
5394 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005395 {
5396 if (action == 'r')
5397 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005398 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005399 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5400 retval = OK;
5401 }
5402 else
5403 return FAIL;
5404 }
5405
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005406 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5407 {
5408 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005409
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005410 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005411 ctx = alloc_tv();
5412 if (ctx != NULL)
5413 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005414 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005415 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005416 }
5417
Bram Moolenaarb254af32017-12-18 19:48:58 +01005418 if (retval == OK)
5419 qf_list_changed(qi, qf_idx);
5420
Bram Moolenaard823fa92016-08-12 16:29:27 +02005421 return retval;
5422}
5423
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005424/*
5425 * Find the non-location list window with the specified location list.
5426 */
5427 static win_T *
5428find_win_with_ll(qf_info_T *qi)
5429{
5430 win_T *wp = NULL;
5431
5432 FOR_ALL_WINDOWS(wp)
5433 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5434 return wp;
5435
5436 return NULL;
5437}
5438
5439/*
5440 * Free the entire quickfix/location list stack.
5441 * If the quickfix/location list window is open, then clear it.
5442 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005443 static void
5444qf_free_stack(win_T *wp, qf_info_T *qi)
5445{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005446 win_T *qfwin = qf_find_win(qi);
5447 win_T *llwin = NULL;
5448 win_T *orig_wp = wp;
5449
5450 if (qfwin != NULL)
5451 {
5452 /* If the quickfix/location list window is open, then clear it */
5453 if (qi->qf_curlist < qi->qf_listcount)
5454 qf_free(qi, qi->qf_curlist);
5455 qf_update_buffer(qi, NULL);
5456 }
5457
5458 if (wp != NULL && IS_LL_WINDOW(wp))
5459 {
5460 /* If in the location list window, then use the non-location list
5461 * window with this location list (if present)
5462 */
5463 llwin = find_win_with_ll(qi);
5464 if (llwin != NULL)
5465 wp = llwin;
5466 }
5467
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005468 qf_free_all(wp);
5469 if (wp == NULL)
5470 {
5471 /* quickfix list */
5472 qi->qf_curlist = 0;
5473 qi->qf_listcount = 0;
5474 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005475 else if (IS_LL_WINDOW(orig_wp))
5476 {
5477 /* If the location list window is open, then create a new empty
5478 * location list */
5479 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005480
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005481 /* first free the list reference in the location list window */
5482 ll_free_all(&orig_wp->w_llist_ref);
5483
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005484 orig_wp->w_llist_ref = new_ll;
5485 if (llwin != NULL)
5486 {
5487 llwin->w_llist = new_ll;
5488 new_ll->qf_refcount++;
5489 }
5490 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005491}
5492
Bram Moolenaard823fa92016-08-12 16:29:27 +02005493/*
5494 * Populate the quickfix list with the items supplied in the list
5495 * of dictionaries. "title" will be copied to w:quickfix_title.
5496 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5497 */
5498 int
5499set_errorlist(
5500 win_T *wp,
5501 list_T *list,
5502 int action,
5503 char_u *title,
5504 dict_T *what)
5505{
5506 qf_info_T *qi = &ql_info;
5507 int retval = OK;
5508
5509 if (wp != NULL)
5510 {
5511 qi = ll_get_or_alloc_list(wp);
5512 if (qi == NULL)
5513 return FAIL;
5514 }
5515
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005516 if (action == 'f')
5517 {
5518 /* Free the entire quickfix or location list stack */
5519 qf_free_stack(wp, qi);
5520 }
5521 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005522 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005523 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005524 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005525 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005526 if (retval == OK)
5527 qf_list_changed(qi, qi->qf_curlist);
5528 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005529
5530 return retval;
5531}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005532
5533 static int
5534mark_quickfix_ctx(qf_info_T *qi, int copyID)
5535{
5536 int i;
5537 int abort = FALSE;
5538 typval_T *ctx;
5539
5540 for (i = 0; i < LISTCOUNT && !abort; ++i)
5541 {
5542 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005543 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5544 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005545 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5546 }
5547
5548 return abort;
5549}
5550
5551/*
5552 * Mark the context of the quickfix list and the location lists (if present) as
5553 * "in use". So that garabage collection doesn't free the context.
5554 */
5555 int
5556set_ref_in_quickfix(int copyID)
5557{
5558 int abort = FALSE;
5559 tabpage_T *tp;
5560 win_T *win;
5561
5562 abort = mark_quickfix_ctx(&ql_info, copyID);
5563 if (abort)
5564 return abort;
5565
5566 FOR_ALL_TAB_WINDOWS(tp, win)
5567 {
5568 if (win->w_llist != NULL)
5569 {
5570 abort = mark_quickfix_ctx(win->w_llist, copyID);
5571 if (abort)
5572 return abort;
5573 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005574 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5575 {
5576 /* In a location list window and none of the other windows is
5577 * referring to this location list. Mark the location list
5578 * context as still in use.
5579 */
5580 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5581 if (abort)
5582 return abort;
5583 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005584 }
5585
5586 return abort;
5587}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005588#endif
5589
Bram Moolenaar81695252004-12-29 20:58:21 +00005590/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005591 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005592 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005593 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005594 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005595 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005596 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005597 */
5598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005599ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005600{
5601 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005602 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005603#ifdef FEAT_AUTOCMD
5604 char_u *au_name = NULL;
5605#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005606 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005607
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005608#ifdef FEAT_AUTOCMD
5609 switch (eap->cmdidx)
5610 {
5611 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5612 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5613 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5614 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5615 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5616 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5617 default: break;
5618 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005619 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5620 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005621 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005622# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005623 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005624 return;
5625# endif
5626 }
5627#endif
5628
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005629 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005630 if (eap->cmdidx == CMD_lbuffer
5631 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005632 || eap->cmdidx == CMD_laddbuffer)
5633 {
5634 qi = ll_get_or_alloc_list(curwin);
5635 if (qi == NULL)
5636 return;
5637 }
5638
Bram Moolenaar86b68352004-12-27 21:59:20 +00005639 if (*eap->arg == NUL)
5640 buf = curbuf;
5641 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5642 buf = buflist_findnr(atoi((char *)eap->arg));
5643 if (buf == NULL)
5644 EMSG(_(e_invarg));
5645 else if (buf->b_ml.ml_mfp == NULL)
5646 EMSG(_("E681: Buffer is not loaded"));
5647 else
5648 {
5649 if (eap->addr_count == 0)
5650 {
5651 eap->line1 = 1;
5652 eap->line2 = buf->b_ml.ml_line_count;
5653 }
5654 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5655 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5656 EMSG(_(e_invrange));
5657 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005658 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005659 char_u *qf_title = *eap->cmdlinep;
5660
5661 if (buf->b_sfname)
5662 {
5663 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5664 (char *)qf_title, (char *)buf->b_sfname);
5665 qf_title = IObuff;
5666 }
5667
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005668 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005669 (eap->cmdidx != CMD_caddbuffer
5670 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005671 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005672 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005673 if (res >= 0)
5674 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005675#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005676 if (au_name != NULL)
5677 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5678 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005679#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005680 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5681 eap->cmdidx == CMD_lbuffer))
5682 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005683 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005684 }
5685}
5686
Bram Moolenaar1e015462005-09-25 22:16:38 +00005687#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005688/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005689 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5690 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005691 */
5692 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005693ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005694{
5695 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005696 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005697#ifdef FEAT_AUTOCMD
5698 char_u *au_name = NULL;
5699#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005700 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005701
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005702#ifdef FEAT_AUTOCMD
5703 switch (eap->cmdidx)
5704 {
5705 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5706 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5707 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5708 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5709 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5710 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5711 default: break;
5712 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005713 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5714 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005715 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005716# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005717 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005718 return;
5719# endif
5720 }
5721#endif
5722
Bram Moolenaar3c097222017-12-21 20:54:49 +01005723 if (eap->cmdidx == CMD_lexpr
5724 || eap->cmdidx == CMD_lgetexpr
5725 || eap->cmdidx == CMD_laddexpr)
5726 {
5727 qi = ll_get_or_alloc_list(curwin);
5728 if (qi == NULL)
5729 return;
5730 }
5731
Bram Moolenaar4770d092006-01-12 23:22:24 +00005732 /* Evaluate the expression. When the result is a string or a list we can
5733 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005734 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005735 if (tv != NULL)
5736 {
5737 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5738 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5739 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005740 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005741 (eap->cmdidx != CMD_caddexpr
5742 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005743 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005744 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005745 if (res >= 0)
5746 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005747#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005748 if (au_name != NULL)
5749 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5750 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005751#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005752 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5753 eap->cmdidx == CMD_lexpr))
5754 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005755 }
5756 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005757 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005758 free_tv(tv);
5759 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005760}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005761#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005762
5763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 * ":helpgrep {pattern}"
5765 */
5766 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005767ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768{
5769 regmatch_T regmatch;
5770 char_u *save_cpo;
5771 char_u *p;
5772 int fcount;
5773 char_u **fnames;
5774 FILE *fd;
5775 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005777#ifdef FEAT_MULTI_LANG
5778 char_u *lang;
5779#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005780 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005781 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005782 int new_qi = FALSE;
5783 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005784#ifdef FEAT_AUTOCMD
5785 char_u *au_name = NULL;
5786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005788#ifdef FEAT_MULTI_LANG
5789 /* Check for a specified language */
5790 lang = check_help_lang(eap->arg);
5791#endif
5792
Bram Moolenaar73633f82012-01-20 13:39:07 +01005793#ifdef FEAT_AUTOCMD
5794 switch (eap->cmdidx)
5795 {
5796 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5797 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5798 default: break;
5799 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005800 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5801 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005802 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005803# ifdef FEAT_EVAL
5804 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005805 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005806# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005807 }
5808#endif
5809
5810 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5811 save_cpo = p_cpo;
5812 p_cpo = empty_option;
5813
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005814 if (eap->cmdidx == CMD_lhelpgrep)
5815 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005816 /* If the current window is a help window, then use it */
5817 if (bt_help(curwin->w_buffer))
5818 wp = curwin;
5819 else
5820 /* Find an existing help window */
5821 FOR_ALL_WINDOWS(wp)
5822 if (bt_help(wp->w_buffer))
5823 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005824
5825 if (wp == NULL) /* Help window not found */
5826 qi = NULL;
5827 else
5828 qi = wp->w_llist;
5829
5830 if (qi == NULL)
5831 {
5832 /* Allocate a new location list for help text matches */
5833 if ((qi = ll_new_list()) == NULL)
5834 return;
5835 new_qi = TRUE;
5836 }
5837 }
5838
Bram Moolenaaree85df32017-03-19 14:19:50 +01005839 /* Autocommands may change the list. Save it for later comparison */
5840 save_qi = qi;
5841
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5843 regmatch.rm_ic = FALSE;
5844 if (regmatch.regprog != NULL)
5845 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005846#ifdef FEAT_MBYTE
5847 vimconv_T vc;
5848
5849 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5850 * differs. */
5851 vc.vc_type = CONV_NONE;
5852 if (!enc_utf8)
5853 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5854#endif
5855
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005857 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
5859 /* Go through all directories in 'runtimepath' */
5860 p = p_rtp;
5861 while (*p != NUL && !got_int)
5862 {
5863 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5864
5865 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5866 add_pathsep(NameBuff);
5867 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5868 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5869 &fnames, EW_FILE|EW_SILENT) == OK
5870 && fcount > 0)
5871 {
5872 for (fi = 0; fi < fcount && !got_int; ++fi)
5873 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005874#ifdef FEAT_MULTI_LANG
5875 /* Skip files for a different language. */
5876 if (lang != NULL
5877 && STRNICMP(lang, fnames[fi]
5878 + STRLEN(fnames[fi]) - 3, 2) != 0
5879 && !(STRNICMP(lang, "en", 2) == 0
5880 && STRNICMP("txt", fnames[fi]
5881 + STRLEN(fnames[fi]) - 3, 3) == 0))
5882 continue;
5883#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005884 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 if (fd != NULL)
5886 {
5887 lnum = 1;
5888 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5889 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005890 char_u *line = IObuff;
5891#ifdef FEAT_MBYTE
5892 /* Convert a line if 'encoding' is not utf-8 and
5893 * the line contains a non-ASCII character. */
5894 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005895 && has_non_ascii(IObuff))
5896 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005897 line = string_convert(&vc, IObuff, NULL);
5898 if (line == NULL)
5899 line = IObuff;
5900 }
5901#endif
5902
5903 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005905 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
5907 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005908 while (l > 0 && line[l - 1] <= ' ')
5909 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005911 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005912 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 NULL, /* dir */
5914 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005915 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005916 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005918 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005919 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005920 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005921 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 0, /* nr */
5923 1, /* type */
5924 TRUE /* valid */
5925 ) == FAIL)
5926 {
5927 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005928#ifdef FEAT_MBYTE
5929 if (line != IObuff)
5930 vim_free(line);
5931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 break;
5933 }
5934 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005935#ifdef FEAT_MBYTE
5936 if (line != IObuff)
5937 vim_free(line);
5938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 ++lnum;
5940 line_breakcheck();
5941 }
5942 fclose(fd);
5943 }
5944 }
5945 FreeWild(fcount, fnames);
5946 }
5947 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005948
Bram Moolenaar473de612013-06-08 18:19:48 +02005949 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005950#ifdef FEAT_MBYTE
5951 if (vc.vc_type != CONV_NONE)
5952 convert_setup(&vc, NULL, NULL);
5953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005955 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5956 qi->qf_lists[qi->qf_curlist].qf_ptr =
5957 qi->qf_lists[qi->qf_curlist].qf_start;
5958 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 }
5960
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005961 if (p_cpo == empty_option)
5962 p_cpo = save_cpo;
5963 else
5964 /* Darn, some plugin changed the value. */
5965 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
Bram Moolenaarb254af32017-12-18 19:48:58 +01005967 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005968 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
Bram Moolenaar73633f82012-01-20 13:39:07 +01005970#ifdef FEAT_AUTOCMD
5971 if (au_name != NULL)
5972 {
5973 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5974 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005975 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005976 /* autocommands made "qi" invalid */
5977 return;
5978 }
5979#endif
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005982 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005983 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005984 else
5985 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005986
5987 if (eap->cmdidx == CMD_lhelpgrep)
5988 {
5989 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005990 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005991 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005992 {
5993 if (new_qi)
5994 ll_free_all(&qi);
5995 }
5996 else if (curwin->w_llist == NULL)
5997 curwin->w_llist = qi;
5998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999}
6000
6001#endif /* FEAT_QUICKFIX */