blob: e8fe59b331c419e0c7855bd5b5651eef6c3e8d45 [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 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003630 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3631 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar864293a2016-06-02 13:40:04 +02003633 keep_filetype = TRUE; /* don't detect 'filetype' */
3634 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003636 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003638 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003639 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003640
Bram Moolenaar864293a2016-06-02 13:40:04 +02003641 /* make sure it will be redrawn */
3642 redraw_curbuf_later(NOT_VALID);
3643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 /* Restore KeyTyped, setting 'filetype' may reset it. */
3646 KeyTyped = old_KeyTyped;
3647}
3648
Bram Moolenaarb254af32017-12-18 19:48:58 +01003649 static void
3650qf_list_changed(qf_info_T *qi, int qf_idx)
3651{
3652 qi->qf_lists[qf_idx].qf_changedtick++;
3653}
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003656 * Return TRUE when using ":vimgrep" for ":grep".
3657 */
3658 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003659grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003660{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003661 return ((cmdidx == CMD_grep
3662 || cmdidx == CMD_lgrep
3663 || cmdidx == CMD_grepadd
3664 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003665 && STRCMP("internal",
3666 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3667}
3668
3669/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003670 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 */
3672 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003673ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003675 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003677 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003679 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003680 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003681 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003682 char_u *au_name = NULL;
3683
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003684 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3685 if (grep_internal(eap->cmdidx))
3686 {
3687 ex_vimgrep(eap);
3688 return;
3689 }
3690
Bram Moolenaar7c626922005-02-07 22:01:03 +00003691 switch (eap->cmdidx)
3692 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003693 case CMD_make: au_name = (char_u *)"make"; break;
3694 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3695 case CMD_grep: au_name = (char_u *)"grep"; break;
3696 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3697 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3698 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003699 default: break;
3700 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003701 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3702 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003703 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003704#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003705 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003706 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003707#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003708 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003709#ifdef FEAT_MBYTE
3710 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaara6557602006-02-04 22:43:20 +00003713 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3714 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003715 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003718 fname = get_mef_name();
3719 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003721 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723 /*
3724 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3725 */
3726 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3727 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003728 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 cmd = alloc(len);
3730 if (cmd == NULL)
3731 return;
3732 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3733 (char *)p_shq);
3734 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003735 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 /*
3737 * Output a newline if there's something else than the :make command that
3738 * was typed (in which case the cursor is in column 0).
3739 */
3740 if (msg_col == 0)
3741 msg_didout = FALSE;
3742 msg_start();
3743 MSG_PUTS(":!");
3744 msg_outtrans(cmd); /* show what we are doing */
3745
3746 /* let the shell know if we are redirecting output or not */
3747 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3748
3749#ifdef AMIGA
3750 out_flush();
3751 /* read window status report and redraw before message */
3752 (void)char_avail();
3753#endif
3754
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003755 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003756 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3757 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003758 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003759 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003760 if (wp != NULL)
3761 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003762 if (res >= 0 && qi != NULL)
3763 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003764 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003765 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003766 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3767 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01003768 if (qi != NULL && qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003769 res = qi->qf_lists[qi->qf_curlist].qf_count;
3770 else
3771 res = 0;
3772 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003773 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003774 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
Bram Moolenaar7c626922005-02-07 22:01:03 +00003776 mch_remove(fname);
3777 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 vim_free(cmd);
3779}
3780
3781/*
3782 * Return the name for the errorfile, in allocated memory.
3783 * Find a new unique name when 'makeef' contains "##".
3784 * Returns NULL for error.
3785 */
3786 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003787get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788{
3789 char_u *p;
3790 char_u *name;
3791 static int start = -1;
3792 static int off = 0;
3793#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003794 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795#endif
3796
3797 if (*p_mef == NUL)
3798 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003799 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 if (name == NULL)
3801 EMSG(_(e_notmp));
3802 return name;
3803 }
3804
3805 for (p = p_mef; *p; ++p)
3806 if (p[0] == '#' && p[1] == '#')
3807 break;
3808
3809 if (*p == NUL)
3810 return vim_strsave(p_mef);
3811
3812 /* Keep trying until the name doesn't exist yet. */
3813 for (;;)
3814 {
3815 if (start == -1)
3816 start = mch_get_pid();
3817 else
3818 off += 19;
3819
3820 name = alloc((unsigned)STRLEN(p_mef) + 30);
3821 if (name == NULL)
3822 break;
3823 STRCPY(name, p_mef);
3824 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3825 STRCAT(name, p + 2);
3826 if (mch_getperm(name) < 0
3827#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003828 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 && mch_lstat((char *)name, &sb) < 0
3830#endif
3831 )
3832 break;
3833 vim_free(name);
3834 }
3835 return name;
3836}
3837
3838/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003839 * Returns the number of valid entries in the current quickfix/location list.
3840 */
3841 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003842qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003843{
3844 qf_info_T *qi = &ql_info;
3845 qfline_T *qfp;
3846 int i, sz = 0;
3847 int prev_fnum = 0;
3848
3849 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3850 {
3851 /* Location list */
3852 qi = GET_LOC_LIST(curwin);
3853 if (qi == NULL)
3854 return 0;
3855 }
3856
3857 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003858 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003859 ++i, qfp = qfp->qf_next)
3860 {
3861 if (qfp->qf_valid)
3862 {
3863 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3864 sz++; /* Count all valid entries */
3865 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3866 {
3867 /* Count the number of files */
3868 sz++;
3869 prev_fnum = qfp->qf_fnum;
3870 }
3871 }
3872 }
3873
3874 return sz;
3875}
3876
3877/*
3878 * Returns the current index of the quickfix/location list.
3879 * Returns 0 if there is an error.
3880 */
3881 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003882qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003883{
3884 qf_info_T *qi = &ql_info;
3885
3886 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3887 {
3888 /* Location list */
3889 qi = GET_LOC_LIST(curwin);
3890 if (qi == NULL)
3891 return 0;
3892 }
3893
3894 return qi->qf_lists[qi->qf_curlist].qf_index;
3895}
3896
3897/*
3898 * Returns the current index in the quickfix/location list (counting only valid
3899 * entries). If no valid entries are in the list, then returns 1.
3900 */
3901 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003902qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003903{
3904 qf_info_T *qi = &ql_info;
3905 qf_list_T *qfl;
3906 qfline_T *qfp;
3907 int i, eidx = 0;
3908 int prev_fnum = 0;
3909
3910 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3911 {
3912 /* Location list */
3913 qi = GET_LOC_LIST(curwin);
3914 if (qi == NULL)
3915 return 1;
3916 }
3917
3918 qfl = &qi->qf_lists[qi->qf_curlist];
3919 qfp = qfl->qf_start;
3920
3921 /* check if the list has valid errors */
3922 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3923 return 1;
3924
3925 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3926 {
3927 if (qfp->qf_valid)
3928 {
3929 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3930 {
3931 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3932 {
3933 /* Count the number of files */
3934 eidx++;
3935 prev_fnum = qfp->qf_fnum;
3936 }
3937 }
3938 else
3939 eidx++;
3940 }
3941 }
3942
3943 return eidx ? eidx : 1;
3944}
3945
3946/*
3947 * Get the 'n'th valid error entry in the quickfix or location list.
3948 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3949 * For :cdo and :ldo returns the 'n'th valid error entry.
3950 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3951 */
3952 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003953qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003954{
3955 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3956 qfline_T *qfp = qfl->qf_start;
3957 int i, eidx;
3958 int prev_fnum = 0;
3959
3960 /* check if the list has valid errors */
3961 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3962 return 1;
3963
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003964 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003965 i++, qfp = qfp->qf_next)
3966 {
3967 if (qfp->qf_valid)
3968 {
3969 if (fdo)
3970 {
3971 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3972 {
3973 /* Count the number of files */
3974 eidx++;
3975 prev_fnum = qfp->qf_fnum;
3976 }
3977 }
3978 else
3979 eidx++;
3980 }
3981
3982 if (eidx == n)
3983 break;
3984 }
3985
3986 if (i <= qfl->qf_count)
3987 return i;
3988 else
3989 return 1;
3990}
3991
3992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003994 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003995 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 */
3997 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003998ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004000 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004001 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004002
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004003 if (eap->cmdidx == CMD_ll
4004 || eap->cmdidx == CMD_lrewind
4005 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004006 || eap->cmdidx == CMD_llast
4007 || eap->cmdidx == CMD_ldo
4008 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004009 {
4010 qi = GET_LOC_LIST(curwin);
4011 if (qi == NULL)
4012 {
4013 EMSG(_(e_loclist));
4014 return;
4015 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004016 }
4017
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004018 if (eap->addr_count > 0)
4019 errornr = (int)eap->line2;
4020 else
4021 {
4022 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4023 errornr = 0;
4024 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4025 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4026 errornr = 1;
4027 else
4028 errornr = 32767;
4029 }
4030
4031 /* For cdo and ldo commands, jump to the nth valid error.
4032 * For cfdo and lfdo commands, jump to the nth valid file entry.
4033 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004034 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4035 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004036 errornr = qf_get_nth_valid_entry(qi,
4037 eap->addr_count > 0 ? (int)eap->line1 : 1,
4038 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4039
4040 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041}
4042
4043/*
4044 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004045 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004046 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 */
4048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004049ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004051 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004052 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004053
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004054 if (eap->cmdidx == CMD_lnext
4055 || eap->cmdidx == CMD_lNext
4056 || eap->cmdidx == CMD_lprevious
4057 || eap->cmdidx == CMD_lnfile
4058 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004059 || eap->cmdidx == CMD_lpfile
4060 || eap->cmdidx == CMD_ldo
4061 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004062 {
4063 qi = GET_LOC_LIST(curwin);
4064 if (qi == NULL)
4065 {
4066 EMSG(_(e_loclist));
4067 return;
4068 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004069 }
4070
Bram Moolenaar55b69262017-08-13 13:42:01 +02004071 if (eap->addr_count > 0
4072 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4073 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004074 errornr = (int)eap->line2;
4075 else
4076 errornr = 1;
4077
4078 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4079 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004081 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4082 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004084 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4085 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 ? BACKWARD_FILE
4087 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004088 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089}
4090
4091/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004092 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004093 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 */
4095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004096ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004098 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004099 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004100 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004101 char_u *au_name = NULL;
Bram Moolenaarfc6f16b2018-03-06 17:43:22 +01004102 int save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004103 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004104
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004105 switch (eap->cmdidx)
4106 {
4107 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4108 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4109 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4110 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4111 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4112 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4113 default: break;
4114 }
4115 if (au_name != NULL)
4116 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004117#ifdef FEAT_MBYTE
4118 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4119#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004120#ifdef FEAT_BROWSE
4121 if (cmdmod.browse)
4122 {
4123 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4124 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4125 if (browse_file == NULL)
4126 return;
4127 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4128 vim_free(browse_file);
4129 }
4130 else
4131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004133 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004134
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004135 if (eap->cmdidx == CMD_lfile
4136 || eap->cmdidx == CMD_lgetfile
4137 || eap->cmdidx == CMD_laddfile)
4138 wp = curwin;
4139
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004140 /*
4141 * This function is used by the :cfile, :cgetfile and :caddfile
4142 * commands.
4143 * :cfile always creates a new quickfix list and jumps to the
4144 * first error.
4145 * :cgetfile creates a new quickfix list but doesn't jump to the
4146 * first error.
4147 * :caddfile adds to an existing quickfix list. If there is no
4148 * quickfix list then a new list is created.
4149 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004150 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4151 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004152 if (wp != NULL)
4153 qi = GET_LOC_LIST(wp);
4154 if (res >= 0 && qi != NULL)
4155 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004156 if (qi != NULL)
4157 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004158 if (au_name != NULL)
4159 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004160
4161 /* An autocmd might have freed the quickfix/location list. Check whether it
4162 * is still valid. */
4163 if (qi != NULL && !qflist_valid(wp, save_qfid))
Bram Moolenaar3c097222017-12-21 20:54:49 +01004164 return;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004165 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004166 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167}
4168
4169/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004170 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004171 * ":vimgrepadd {pattern} file(s)"
4172 * ":lvimgrep {pattern} file(s)"
4173 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004174 */
4175 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004176ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004177{
Bram Moolenaar81695252004-12-29 20:58:21 +00004178 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004179 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004180 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004181 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004182 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004183 char_u *s;
4184 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004185 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004186 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004187 int loclist_cmd = FALSE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004188 int_u save_qfid;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004189 qfline_T *cur_qf_start;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004190 win_T *wp;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004191 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004192 buf_T *buf;
4193 int duplicate_name = FALSE;
4194 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004195 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004196 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004197 buf_T *first_match_buf = NULL;
4198 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004199 int save_mls;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004200#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004201 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004202#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004203 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004204 int flags = 0;
4205 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004206 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004207 char_u *dirname_start = NULL;
4208 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004209 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004210 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004211
4212 switch (eap->cmdidx)
4213 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004214 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4215 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4216 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004217 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004218 case CMD_grep: au_name = (char_u *)"grep"; break;
4219 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4220 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4221 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004222 default: break;
4223 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004224 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4225 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004226 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004227#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004228 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004229 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004230#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004231 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004232
Bram Moolenaar754b5602006-02-09 23:53:20 +00004233 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004234 || eap->cmdidx == CMD_lvimgrep
4235 || eap->cmdidx == CMD_lgrepadd
4236 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004237 {
4238 qi = ll_get_or_alloc_list(curwin);
4239 if (qi == NULL)
4240 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004241 loclist_cmd = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00004242 }
4243
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004244 if (eap->addr_count > 0)
4245 tomatch = eap->line2;
4246 else
4247 tomatch = MAXLNUM;
4248
Bram Moolenaar81695252004-12-29 20:58:21 +00004249 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004250 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004251 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004252 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004253 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004254 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004255 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004256 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004257 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004258
4259 if (s != NULL && *s == NUL)
4260 {
4261 /* Pattern is empty, use last search pattern. */
4262 if (last_search_pat() == NULL)
4263 {
4264 EMSG(_(e_noprevre));
4265 goto theend;
4266 }
4267 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4268 }
4269 else
4270 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4271
Bram Moolenaar86b68352004-12-27 21:59:20 +00004272 if (regmatch.regprog == NULL)
4273 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004274 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004275 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004276
4277 p = skipwhite(p);
4278 if (*p == NUL)
4279 {
4280 EMSG(_("E683: File name missing or invalid pattern"));
4281 goto theend;
4282 }
4283
Bram Moolenaar55b69262017-08-13 13:42:01 +02004284 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4285 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004286 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004287 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004288 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004289
4290 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004291 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004292 goto theend;
4293 if (fcount == 0)
4294 {
4295 EMSG(_(e_nomatch));
4296 goto theend;
4297 }
4298
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004299 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4300 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004301 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004302 {
4303 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004304 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004305 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004306
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004307 /* Remember the current directory, because a BufRead autocommand that does
4308 * ":lcd %:p:h" changes the meaning of short path names. */
4309 mch_dirname(dirname_start, MAXPATHL);
4310
Bram Moolenaar3c097222017-12-21 20:54:49 +01004311 /* Remember the current values of the quickfix list and qf_start, so that
4312 * we can check for autocommands changing the current quickfix list. */
4313 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004314 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004315
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004316 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004317 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004318 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004319 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004320 if (time(NULL) > seconds)
4321 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004322 /* Display the file name every second or so, show the user we are
4323 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004324 seconds = time(NULL);
4325 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004326 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004327 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004328 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004329 else
4330 {
4331 msg_outtrans(p);
4332 vim_free(p);
4333 }
4334 msg_clr_eos();
4335 msg_didout = FALSE; /* overwrite this message */
4336 msg_nowait = TRUE; /* don't wait for this message */
4337 msg_col = 0;
4338 out_flush();
4339 }
4340
Bram Moolenaar81695252004-12-29 20:58:21 +00004341 buf = buflist_findname_exp(fnames[fi]);
4342 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4343 {
4344 /* Remember that a buffer with this name already exists. */
4345 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004346 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004347 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004348
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004349#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004350 /* Don't do Filetype autocommands to avoid loading syntax and
4351 * indent scripts, a great speed improvement. */
4352 save_ei = au_event_disable(",Filetype");
4353#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004354 /* Don't use modelines here, it's useless. */
4355 save_mls = p_mls;
4356 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004357
4358 /* Load file into a buffer, so that 'fileencoding' is detected,
4359 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004360 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004361
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004362 p_mls = save_mls;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004363#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004364 au_event_restore(save_ei);
4365#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004366 }
4367 else
4368 /* Use existing, loaded buffer. */
4369 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004370
Bram Moolenaar3c097222017-12-21 20:54:49 +01004371 if (loclist_cmd)
4372 {
4373 /*
4374 * Verify that the location list is still valid. An autocmd might
4375 * have freed the location list.
4376 */
4377 if (!qflist_valid(curwin, save_qfid))
4378 {
4379 EMSG(_(e_loc_list_changed));
4380 goto theend;
4381 }
4382 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004383 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4384 {
4385 int idx;
4386
4387 /* Autocommands changed the quickfix list. Find the one we were
4388 * using and restore it. */
4389 for (idx = 0; idx < LISTCOUNT; ++idx)
4390 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4391 {
4392 qi->qf_curlist = idx;
4393 break;
4394 }
4395 if (idx == LISTCOUNT)
4396 {
4397 /* List cannot be found, create a new one. */
4398 qf_new_list(qi, *eap->cmdlinep);
4399 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4400 }
4401 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004402
Bram Moolenaar81695252004-12-29 20:58:21 +00004403 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004404 {
4405 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004406 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004407 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004408 else
4409 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004410 /* Try for a match in all lines of the buffer.
4411 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004412 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004413 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4414 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004415 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004416 col = 0;
4417 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004418 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004419 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004420 /* Pass the buffer number so that it gets used even for a
4421 * dummy buffer, unless duplicate_name is set, then the
4422 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004423 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004424 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004425 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004426 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004427 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004428 ml_get_buf(buf,
4429 regmatch.startpos[0].lnum + lnum, FALSE),
4430 regmatch.startpos[0].lnum + lnum,
4431 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004432 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004433 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004434 0, /* nr */
4435 0, /* type */
4436 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004437 ) == FAIL)
4438 {
4439 got_int = TRUE;
4440 break;
4441 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004442 found_match = TRUE;
4443 if (--tomatch == 0)
4444 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004445 if ((flags & VGR_GLOBAL) == 0
4446 || regmatch.endpos[0].lnum > 0)
4447 break;
4448 col = regmatch.endpos[0].col
4449 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004450 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004451 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004452 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004453 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004454 if (got_int)
4455 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004456 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004457 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00004458
4459 if (using_dummy)
4460 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004461 if (found_match && first_match_buf == NULL)
4462 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004463 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004464 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004465 /* Never keep a dummy buffer if there is another buffer
4466 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004467 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004468 buf = NULL;
4469 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004470 else if (!cmdmod.hide
4471 || buf->b_p_bh[0] == 'u' /* "unload" */
4472 || buf->b_p_bh[0] == 'w' /* "wipe" */
4473 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004474 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004475 /* When no match was found we don't need to remember the
4476 * buffer, wipe it out. If there was a match and it
4477 * wasn't the first one or we won't jump there: only
4478 * unload the buffer.
4479 * Ignore 'hidden' here, because it may lead to having too
4480 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004481 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004482 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004483 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004484 buf = NULL;
4485 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004486 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004487 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004488 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004489 /* Keeping the buffer, remove the dummy flag. */
4490 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004491 buf = NULL;
4492 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004493 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004494
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004495 if (buf != NULL)
4496 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004497 /* Keeping the buffer, remove the dummy flag. */
4498 buf->b_flags &= ~BF_DUMMY;
4499
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004500 /* If the buffer is still loaded we need to use the
4501 * directory we jumped to below. */
4502 if (buf == first_match_buf
4503 && target_dir == NULL
4504 && STRCMP(dirname_start, dirname_now) != 0)
4505 target_dir = vim_strsave(dirname_now);
4506
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004507 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004508 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004509 * need to be done (again). But not the window-local
4510 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004511 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004512#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004513 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4514 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004515#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004516 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004517 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004518 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004519 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004520 }
4521 }
4522
4523 FreeWild(fcount, fnames);
4524
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004525 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4526 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4527 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004528 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004529
Bram Moolenaar864293a2016-06-02 13:40:04 +02004530 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004531
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004532 if (au_name != NULL)
4533 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4534 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004535 /*
4536 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4537 * is still valid.
4538 */
4539 wp = loclist_cmd ? curwin : NULL;
4540 if (!qflist_valid(wp, save_qfid))
4541 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004542
Bram Moolenaar86b68352004-12-27 21:59:20 +00004543 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004544 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004545 {
4546 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004547 {
4548 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004549 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004550 if (buf != curbuf)
4551 /* If we jumped to another buffer redrawing will already be
4552 * taken care of. */
4553 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004554
4555 /* Jump to the directory used after loading the buffer. */
4556 if (curbuf == first_match_buf && target_dir != NULL)
4557 {
4558 exarg_T ea;
4559
4560 ea.arg = target_dir;
4561 ea.cmdidx = CMD_lcd;
4562 ex_cd(&ea);
4563 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004564 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004565 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004566 else
4567 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004568
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004569 /* If we loaded a dummy buffer into the current window, the autocommands
4570 * may have messed up things, need to redraw and recompute folds. */
4571 if (redraw_for_dummy)
4572 {
4573#ifdef FEAT_FOLDING
4574 foldUpdateAll(curwin);
4575#else
4576 redraw_later(NOT_VALID);
4577#endif
4578 }
4579
Bram Moolenaar86b68352004-12-27 21:59:20 +00004580theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004581 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004582 vim_free(dirname_now);
4583 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004584 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004585 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004586}
4587
4588/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004589 * Restore current working directory to "dirname_start" if they differ, taking
4590 * into account whether it is set locally or globally.
4591 */
4592 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004593restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004594{
4595 char_u *dirname_now = alloc(MAXPATHL);
4596
4597 if (NULL != dirname_now)
4598 {
4599 mch_dirname(dirname_now, MAXPATHL);
4600 if (STRCMP(dirname_start, dirname_now) != 0)
4601 {
4602 /* If the directory has changed, change it back by building up an
4603 * appropriate ex command and executing it. */
4604 exarg_T ea;
4605
4606 ea.arg = dirname_start;
4607 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4608 ex_cd(&ea);
4609 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004610 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004611 }
4612}
4613
4614/*
4615 * Load file "fname" into a dummy buffer and return the buffer pointer,
4616 * placing the directory resulting from the buffer load into the
4617 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4618 * prior to calling this function. Restores directory to "dirname_start" prior
4619 * to returning, if autocmds or the 'autochdir' option have changed it.
4620 *
4621 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4622 * or wipe_dummy_buffer() later!
4623 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004624 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004625 */
4626 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004627load_dummy_buffer(
4628 char_u *fname,
4629 char_u *dirname_start, /* in: old directory */
4630 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004631{
4632 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004633 bufref_T newbufref;
4634 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004635 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004636 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004637 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004638
4639 /* Allocate a buffer without putting it in the buffer list. */
4640 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4641 if (newbuf == NULL)
4642 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004643 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004644
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004645 /* Init the options. */
4646 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4647
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004648 /* need to open the memfile before putting the buffer in a window */
4649 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004650 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004651 /* Make sure this buffer isn't wiped out by auto commands. */
4652 ++newbuf->b_locked;
4653
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004654 /* set curwin/curbuf to buf and save a few things */
4655 aucmd_prepbuf(&aco, newbuf);
4656
4657 /* Need to set the filename for autocommands. */
4658 (void)setfname(curbuf, fname, NULL, FALSE);
4659
Bram Moolenaar81695252004-12-29 20:58:21 +00004660 /* Create swap file now to avoid the ATTENTION message. */
4661 check_need_swap(TRUE);
4662
4663 /* Remove the "dummy" flag, otherwise autocommands may not
4664 * work. */
4665 curbuf->b_flags &= ~BF_DUMMY;
4666
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004667 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004668 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004669 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004670 NULL, READ_NEW | READ_DUMMY);
4671 --newbuf->b_locked;
4672 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004673 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004674 && !(curbuf->b_flags & BF_NEW))
4675 {
4676 failed = FALSE;
4677 if (curbuf != newbuf)
4678 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004679 /* Bloody autocommands changed the buffer! Can happen when
4680 * using netrw and editing a remote file. Use the current
4681 * buffer instead, delete the dummy one after restoring the
4682 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004683 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004684 newbuf = curbuf;
4685 }
4686 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004687
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004688 /* restore curwin/curbuf and a few other things */
4689 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004690 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4691 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004692
4693 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4694 * skip it. */
4695 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004696 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004697
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004698 /*
4699 * When autocommands/'autochdir' option changed directory: go back.
4700 * Let the caller know what the resulting dir was first, in case it is
4701 * important.
4702 */
4703 mch_dirname(resulting_dir, MAXPATHL);
4704 restore_start_dir(dirname_start);
4705
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004706 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004707 return NULL;
4708 if (failed)
4709 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004710 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004711 return NULL;
4712 }
4713 return newbuf;
4714}
4715
4716/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004717 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4718 * directory to "dirname_start" prior to returning, if autocmds or the
4719 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004720 */
4721 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004722wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004723{
4724 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004725 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004726#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004727 cleanup_T cs;
4728
4729 /* Reset the error/interrupt/exception state here so that aborting()
4730 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4731 * work when got_int is set. */
4732 enter_cleanup(&cs);
4733#endif
4734
Bram Moolenaar81695252004-12-29 20:58:21 +00004735 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004736
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004737#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004738 /* Restore the error/interrupt/exception state if not discarded by a
4739 * new aborting error, interrupt, or uncaught exception. */
4740 leave_cleanup(&cs);
4741#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004742 /* When autocommands/'autochdir' option changed directory: go back. */
4743 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004744 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004745}
4746
4747/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004748 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4749 * directory to "dirname_start" prior to returning, if autocmds or the
4750 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004751 */
4752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004753unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004754{
4755 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004756 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004757 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004758
4759 /* When autocommands/'autochdir' option changed directory: go back. */
4760 restore_start_dir(dirname_start);
4761 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004762}
4763
Bram Moolenaar05159a02005-02-26 23:04:13 +00004764#if defined(FEAT_EVAL) || defined(PROTO)
4765/*
4766 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004767 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004768 */
4769 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004770get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004771{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004772 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004773 dict_T *dict;
4774 char_u buf[2];
4775 qfline_T *qfp;
4776 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004777 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004778
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004779 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004780 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004781 qi = &ql_info;
4782 if (wp != NULL)
4783 {
4784 qi = GET_LOC_LIST(wp);
4785 if (qi == NULL)
4786 return FAIL;
4787 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004788 }
4789
Bram Moolenaard823fa92016-08-12 16:29:27 +02004790 if (qf_idx == -1)
4791 qf_idx = qi->qf_curlist;
4792
4793 if (qf_idx >= qi->qf_listcount
4794 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004795 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004796
Bram Moolenaard823fa92016-08-12 16:29:27 +02004797 qfp = qi->qf_lists[qf_idx].qf_start;
4798 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004799 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004800 /* Handle entries with a non-existing buffer number. */
4801 bufnum = qfp->qf_fnum;
4802 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4803 bufnum = 0;
4804
Bram Moolenaar05159a02005-02-26 23:04:13 +00004805 if ((dict = dict_alloc()) == NULL)
4806 return FAIL;
4807 if (list_append_dict(list, dict) == FAIL)
4808 return FAIL;
4809
4810 buf[0] = qfp->qf_type;
4811 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004812 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004813 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4814 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4815 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4816 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004817 || dict_add_nr_str(dict, "pattern", 0L,
4818 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4819 || dict_add_nr_str(dict, "text", 0L,
4820 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004821 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4822 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4823 return FAIL;
4824
4825 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004826 if (qfp == NULL)
4827 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004828 }
4829 return OK;
4830}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004831
4832/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004833 * Flags used by getqflist()/getloclist() to determine which fields to return.
4834 */
4835enum {
4836 QF_GETLIST_NONE = 0x0,
4837 QF_GETLIST_TITLE = 0x1,
4838 QF_GETLIST_ITEMS = 0x2,
4839 QF_GETLIST_NR = 0x4,
4840 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004841 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004842 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004843 QF_GETLIST_IDX = 0x40,
4844 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01004845 QF_GETLIST_TICK = 0x100,
4846 QF_GETLIST_ALL = 0x1FF
Bram Moolenaard823fa92016-08-12 16:29:27 +02004847};
4848
4849/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004850 * Parse text from 'di' and return the quickfix list items
4851 */
4852 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004853qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004854{
4855 int status = FAIL;
4856 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004857 char_u *errorformat = p_efm;
4858 dictitem_T *efm_di;
4859 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004860
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004861 /* Only a List value is supported */
4862 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004863 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004864 /* If errorformat is supplied then use it, otherwise use the 'efm'
4865 * option setting
4866 */
4867 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4868 {
4869 if (efm_di->di_tv.v_type != VAR_STRING ||
4870 efm_di->di_tv.vval.v_string == NULL)
4871 return FAIL;
4872 errorformat = efm_di->di_tv.vval.v_string;
4873 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004874
Bram Moolenaar36538222017-09-02 19:51:44 +02004875 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004876 if (l == NULL)
4877 return FAIL;
4878
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004879 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4880 if (qi != NULL)
4881 {
4882 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4883 qi->qf_refcount++;
4884
Bram Moolenaar36538222017-09-02 19:51:44 +02004885 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004886 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4887 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004888 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004889 qf_free(qi, 0);
4890 }
4891 free(qi);
4892 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004893 dict_add_list(retdict, "items", l);
4894 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004895 }
4896
4897 return status;
4898}
4899
4900/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004901 * Return the quickfix/location list number with the given identifier.
4902 * Returns -1 if list is not found.
4903 */
4904 static int
4905qf_id2nr(qf_info_T *qi, int_u qfid)
4906{
4907 int qf_idx;
4908
4909 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4910 if (qi->qf_lists[qf_idx].qf_id == qfid)
4911 return qf_idx;
4912 return -1;
4913}
4914
4915/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004916 * Return the quickfix/location list window identifier in the current tabpage.
4917 */
4918 static int
4919qf_winid(qf_info_T *qi)
4920{
4921 win_T *win;
4922
4923 /* The quickfix window can be opened even if the quickfix list is not set
4924 * using ":copen". This is not true for location lists. */
4925 if (qi == NULL)
4926 return 0;
4927 win = qf_find_win(qi);
4928 if (win != NULL)
4929 return win->w_id;
4930 return 0;
4931}
4932
4933/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004934 * Return quickfix/location list details (title) as a
4935 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4936 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004937 */
4938 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004939qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004940{
4941 qf_info_T *qi = &ql_info;
4942 int status = OK;
4943 int qf_idx;
4944 dictitem_T *di;
4945 int flags = QF_GETLIST_NONE;
4946
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004947 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004948 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004949
Bram Moolenaard823fa92016-08-12 16:29:27 +02004950 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004951 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004952
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004953 if (dict_find(what, (char_u *)"all", -1) != NULL)
4954 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004955
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004956 if (dict_find(what, (char_u *)"title", -1) != NULL)
4957 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004958
Bram Moolenaara6d48492017-12-12 22:45:31 +01004959 if (dict_find(what, (char_u *)"nr", -1) != NULL)
4960 flags |= QF_GETLIST_NR;
4961
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004962 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4963 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004964
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004965 if (dict_find(what, (char_u *)"context", -1) != NULL)
4966 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004967
Bram Moolenaara6d48492017-12-12 22:45:31 +01004968 if (dict_find(what, (char_u *)"id", -1) != NULL)
4969 flags |= QF_GETLIST_ID;
4970
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004971 if (dict_find(what, (char_u *)"items", -1) != NULL)
4972 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004973
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004974 if (dict_find(what, (char_u *)"idx", -1) != NULL)
4975 flags |= QF_GETLIST_IDX;
4976
4977 if (dict_find(what, (char_u *)"size", -1) != NULL)
4978 flags |= QF_GETLIST_SIZE;
4979
Bram Moolenaarb254af32017-12-18 19:48:58 +01004980 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
4981 flags |= QF_GETLIST_TICK;
4982
Bram Moolenaara6d48492017-12-12 22:45:31 +01004983 if (qi != NULL && qi->qf_listcount != 0)
4984 {
4985 qf_idx = qi->qf_curlist; /* default is the current list */
4986 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4987 {
4988 /* Use the specified quickfix/location list */
4989 if (di->di_tv.v_type == VAR_NUMBER)
4990 {
4991 /* for zero use the current list */
4992 if (di->di_tv.vval.v_number != 0)
4993 {
4994 qf_idx = di->di_tv.vval.v_number - 1;
4995 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4996 qf_idx = -1;
4997 }
4998 }
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01004999 else if (di->di_tv.v_type == VAR_STRING
5000 && di->di_tv.vval.v_string != NULL
5001 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005002 /* Get the last quickfix list number */
5003 qf_idx = qi->qf_listcount - 1;
5004 else
5005 qf_idx = -1;
5006 flags |= QF_GETLIST_NR;
5007 }
5008
5009 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5010 {
5011 /* Look for a list with the specified id */
5012 if (di->di_tv.v_type == VAR_NUMBER)
5013 {
5014 /*
5015 * For zero, use the current list or the list specifed by 'nr'
5016 */
5017 if (di->di_tv.vval.v_number != 0)
5018 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5019 flags |= QF_GETLIST_ID;
5020 }
5021 else
5022 qf_idx = -1;
5023 }
5024 }
5025
5026 /* List is not present or is empty */
5027 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
5028 {
5029 if (flags & QF_GETLIST_TITLE)
5030 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5031 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5032 {
5033 list_T *l = list_alloc();
5034 if (l != NULL)
5035 status = dict_add_list(retdict, "items", l);
5036 else
5037 status = FAIL;
5038 }
5039 if ((status == OK) && (flags & QF_GETLIST_NR))
5040 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5041 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005042 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005043 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5044 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5045 if ((status == OK) && (flags & QF_GETLIST_ID))
5046 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5047 if ((status == OK) && (flags & QF_GETLIST_IDX))
5048 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5049 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5050 status = dict_add_nr_str(retdict, "size", 0L, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005051 if ((status == OK) && (flags & QF_GETLIST_TICK))
5052 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005053
5054 return status;
5055 }
5056
Bram Moolenaard823fa92016-08-12 16:29:27 +02005057 if (flags & QF_GETLIST_TITLE)
5058 {
5059 char_u *t;
5060 t = qi->qf_lists[qf_idx].qf_title;
5061 if (t == NULL)
5062 t = (char_u *)"";
5063 status = dict_add_nr_str(retdict, "title", 0L, t);
5064 }
5065 if ((status == OK) && (flags & QF_GETLIST_NR))
5066 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5067 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005068 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005069 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5070 {
5071 list_T *l = list_alloc();
5072 if (l != NULL)
5073 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005074 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005075 dict_add_list(retdict, "items", l);
5076 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005077 else
5078 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005079 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005080
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005081 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5082 {
5083 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5084 {
5085 di = dictitem_alloc((char_u *)"context");
5086 if (di != NULL)
5087 {
5088 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005089 status = dict_add(retdict, di);
5090 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02005091 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005092 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005093 else
5094 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005095 }
5096 else
5097 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5098 }
5099
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005100 if ((status == OK) && (flags & QF_GETLIST_ID))
5101 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5102 NULL);
5103
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005104 if ((status == OK) && (flags & QF_GETLIST_IDX))
5105 {
5106 int idx = qi->qf_lists[qf_idx].qf_index;
5107 if (qi->qf_lists[qf_idx].qf_count == 0)
5108 /* For empty lists, qf_index is set to 1 */
5109 idx = 0;
5110 status = dict_add_nr_str(retdict, "idx", idx, NULL);
5111 }
5112
5113 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5114 status = dict_add_nr_str(retdict, "size",
5115 qi->qf_lists[qf_idx].qf_count, NULL);
5116
Bram Moolenaarb254af32017-12-18 19:48:58 +01005117 if ((status == OK) && (flags & QF_GETLIST_TICK))
5118 status = dict_add_nr_str(retdict, "changedtick",
5119 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5120
Bram Moolenaard823fa92016-08-12 16:29:27 +02005121 return status;
5122}
5123
5124/*
5125 * Add list of entries to quickfix/location list. Each list entry is
5126 * a dictionary with item information.
5127 */
5128 static int
5129qf_add_entries(
5130 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005131 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005132 list_T *list,
5133 char_u *title,
5134 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005135{
5136 listitem_T *li;
5137 dict_T *d;
5138 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005139 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005140 long lnum;
5141 int col, nr;
5142 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005143 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005144 int valid, status;
5145 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005146 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005147
Bram Moolenaara3921f42017-06-04 15:30:34 +02005148 if (action == ' ' || qf_idx == qi->qf_listcount)
5149 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005150 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005151 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005152 qf_idx = qi->qf_curlist;
5153 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005154 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005155 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005156 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005157 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005158 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005159 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005160 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005161 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005162
5163 for (li = list->lv_first; li != NULL; li = li->li_next)
5164 {
5165 if (li->li_tv.v_type != VAR_DICT)
5166 continue; /* Skip non-dict items */
5167
5168 d = li->li_tv.vval.v_dict;
5169 if (d == NULL)
5170 continue;
5171
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005172 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005173 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5174 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5175 col = (int)get_dict_number(d, (char_u *)"col");
5176 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5177 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005178 type = get_dict_string(d, (char_u *)"type", TRUE);
5179 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5180 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005181 if (text == NULL)
5182 text = vim_strsave((char_u *)"");
5183
5184 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005185 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005186 valid = FALSE;
5187
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005188 /* Mark entries with non-existing buffer number as not valid. Give the
5189 * error message only once. */
5190 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5191 {
5192 if (!did_bufnr_emsg)
5193 {
5194 did_bufnr_emsg = TRUE;
5195 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5196 }
5197 valid = FALSE;
5198 bufnum = 0;
5199 }
5200
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005201 /* If the 'valid' field is present it overrules the detected value. */
5202 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5203 valid = (int)get_dict_number(d, (char_u *)"valid");
5204
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005205 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005206 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005207 NULL, /* dir */
5208 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005209 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005210 text,
5211 lnum,
5212 col,
5213 vcol, /* vis_col */
5214 pattern, /* search pattern */
5215 nr,
5216 type == NULL ? NUL : *type,
5217 valid);
5218
5219 vim_free(filename);
5220 vim_free(pattern);
5221 vim_free(text);
5222 vim_free(type);
5223
5224 if (status == FAIL)
5225 {
5226 retval = FAIL;
5227 break;
5228 }
5229 }
5230
Bram Moolenaara3921f42017-06-04 15:30:34 +02005231 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005232 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005233 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005234 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005235 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005236 if (action != 'a')
5237 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005238 qi->qf_lists[qf_idx].qf_ptr =
5239 qi->qf_lists[qf_idx].qf_start;
5240 if (qi->qf_lists[qf_idx].qf_count > 0)
5241 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005242 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005243
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005244 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005245 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005246
5247 return retval;
5248}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005249
5250 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005251qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005252{
5253 dictitem_T *di;
5254 int retval = FAIL;
5255 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005256 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005257 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005258
5259 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5260 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005261
5262 qf_idx = qi->qf_curlist; /* default is the current list */
5263 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5264 {
5265 /* Use the specified quickfix/location list */
5266 if (di->di_tv.v_type == VAR_NUMBER)
5267 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005268 /* for zero use the current list */
5269 if (di->di_tv.vval.v_number != 0)
5270 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005271
Bram Moolenaar55b69262017-08-13 13:42:01 +02005272 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5273 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005274 /*
5275 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005276 * non-available list and add the new list at the end of the
5277 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005278 */
5279 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005280 qf_idx = qi->qf_listcount - 1;
5281 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005282 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005283 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005284 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005285 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005286 }
5287 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005288 && di->di_tv.vval.v_string != NULL
5289 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005290 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005291 if (qi->qf_listcount > 0)
5292 qf_idx = qi->qf_listcount - 1;
5293 else if (newlist)
5294 qf_idx = 0;
5295 else
5296 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005297 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005298 else
5299 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005300 }
5301
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005302 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5303 {
5304 /* Use the quickfix/location list with the specified id */
5305 if (di->di_tv.v_type == VAR_NUMBER)
5306 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005307 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5308 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005309 return FAIL; /* List not found */
5310 }
5311 else
5312 return FAIL;
5313 }
5314
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005315 if (newlist)
5316 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005317 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005318 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005319 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005320 }
5321
5322 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5323 {
5324 if (di->di_tv.v_type == VAR_STRING)
5325 {
5326 vim_free(qi->qf_lists[qf_idx].qf_title);
5327 qi->qf_lists[qf_idx].qf_title =
5328 get_dict_string(what, (char_u *)"title", TRUE);
5329 if (qf_idx == qi->qf_curlist)
5330 qf_update_win_titlevar(qi);
5331 retval = OK;
5332 }
5333 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005334
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005335 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5336 {
5337 if (di->di_tv.v_type == VAR_LIST)
5338 {
5339 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5340
5341 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5342 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005343 if (action == 'r')
5344 {
5345 /*
5346 * When replacing the quickfix list entries using
5347 * qf_add_entries(), the title is set with a ':' prefix.
5348 * Restore the title with the saved title.
5349 */
5350 vim_free(qi->qf_lists[qf_idx].qf_title);
5351 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5352 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005353 vim_free(title_save);
5354 }
5355 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005356
Bram Moolenaar36538222017-09-02 19:51:44 +02005357 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5358 {
5359 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5360 return FAIL;
5361 errorformat = di->di_tv.vval.v_string;
5362 }
5363
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005364 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005365 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005366 /* Only a List value is supported */
5367 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005368 {
5369 if (action == 'r')
5370 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005371 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005372 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5373 retval = OK;
5374 }
5375 else
5376 return FAIL;
5377 }
5378
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005379 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5380 {
5381 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005382
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005383 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005384 ctx = alloc_tv();
5385 if (ctx != NULL)
5386 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005387 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005388 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005389 }
5390
Bram Moolenaarb254af32017-12-18 19:48:58 +01005391 if (retval == OK)
5392 qf_list_changed(qi, qf_idx);
5393
Bram Moolenaard823fa92016-08-12 16:29:27 +02005394 return retval;
5395}
5396
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005397/*
5398 * Find the non-location list window with the specified location list.
5399 */
5400 static win_T *
5401find_win_with_ll(qf_info_T *qi)
5402{
5403 win_T *wp = NULL;
5404
5405 FOR_ALL_WINDOWS(wp)
5406 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5407 return wp;
5408
5409 return NULL;
5410}
5411
5412/*
5413 * Free the entire quickfix/location list stack.
5414 * If the quickfix/location list window is open, then clear it.
5415 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005416 static void
5417qf_free_stack(win_T *wp, qf_info_T *qi)
5418{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005419 win_T *qfwin = qf_find_win(qi);
5420 win_T *llwin = NULL;
5421 win_T *orig_wp = wp;
5422
5423 if (qfwin != NULL)
5424 {
5425 /* If the quickfix/location list window is open, then clear it */
5426 if (qi->qf_curlist < qi->qf_listcount)
5427 qf_free(qi, qi->qf_curlist);
5428 qf_update_buffer(qi, NULL);
5429 }
5430
5431 if (wp != NULL && IS_LL_WINDOW(wp))
5432 {
5433 /* If in the location list window, then use the non-location list
5434 * window with this location list (if present)
5435 */
5436 llwin = find_win_with_ll(qi);
5437 if (llwin != NULL)
5438 wp = llwin;
5439 }
5440
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005441 qf_free_all(wp);
5442 if (wp == NULL)
5443 {
5444 /* quickfix list */
5445 qi->qf_curlist = 0;
5446 qi->qf_listcount = 0;
5447 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005448 else if (IS_LL_WINDOW(orig_wp))
5449 {
5450 /* If the location list window is open, then create a new empty
5451 * location list */
5452 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005453
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005454 /* first free the list reference in the location list window */
5455 ll_free_all(&orig_wp->w_llist_ref);
5456
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005457 orig_wp->w_llist_ref = new_ll;
5458 if (llwin != NULL)
5459 {
5460 llwin->w_llist = new_ll;
5461 new_ll->qf_refcount++;
5462 }
5463 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005464}
5465
Bram Moolenaard823fa92016-08-12 16:29:27 +02005466/*
5467 * Populate the quickfix list with the items supplied in the list
5468 * of dictionaries. "title" will be copied to w:quickfix_title.
5469 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5470 */
5471 int
5472set_errorlist(
5473 win_T *wp,
5474 list_T *list,
5475 int action,
5476 char_u *title,
5477 dict_T *what)
5478{
5479 qf_info_T *qi = &ql_info;
5480 int retval = OK;
5481
5482 if (wp != NULL)
5483 {
5484 qi = ll_get_or_alloc_list(wp);
5485 if (qi == NULL)
5486 return FAIL;
5487 }
5488
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005489 if (action == 'f')
5490 {
5491 /* Free the entire quickfix or location list stack */
5492 qf_free_stack(wp, qi);
5493 }
5494 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005495 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005496 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005497 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005498 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005499 if (retval == OK)
5500 qf_list_changed(qi, qi->qf_curlist);
5501 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005502
5503 return retval;
5504}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005505
5506 static int
5507mark_quickfix_ctx(qf_info_T *qi, int copyID)
5508{
5509 int i;
5510 int abort = FALSE;
5511 typval_T *ctx;
5512
5513 for (i = 0; i < LISTCOUNT && !abort; ++i)
5514 {
5515 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005516 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5517 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005518 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5519 }
5520
5521 return abort;
5522}
5523
5524/*
5525 * Mark the context of the quickfix list and the location lists (if present) as
5526 * "in use". So that garabage collection doesn't free the context.
5527 */
5528 int
5529set_ref_in_quickfix(int copyID)
5530{
5531 int abort = FALSE;
5532 tabpage_T *tp;
5533 win_T *win;
5534
5535 abort = mark_quickfix_ctx(&ql_info, copyID);
5536 if (abort)
5537 return abort;
5538
5539 FOR_ALL_TAB_WINDOWS(tp, win)
5540 {
5541 if (win->w_llist != NULL)
5542 {
5543 abort = mark_quickfix_ctx(win->w_llist, copyID);
5544 if (abort)
5545 return abort;
5546 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005547 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5548 {
5549 /* In a location list window and none of the other windows is
5550 * referring to this location list. Mark the location list
5551 * context as still in use.
5552 */
5553 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5554 if (abort)
5555 return abort;
5556 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005557 }
5558
5559 return abort;
5560}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005561#endif
5562
Bram Moolenaar81695252004-12-29 20:58:21 +00005563/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005564 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005565 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005566 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005567 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005568 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005569 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005570 */
5571 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005572ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005573{
5574 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005575 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005576 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005577 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005578
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005579 switch (eap->cmdidx)
5580 {
5581 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5582 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5583 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5584 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5585 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5586 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5587 default: break;
5588 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005589 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5590 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005591 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005592#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005593 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005594 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005595#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005596 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005597
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005598 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005599 if (eap->cmdidx == CMD_lbuffer
5600 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005601 || eap->cmdidx == CMD_laddbuffer)
5602 {
5603 qi = ll_get_or_alloc_list(curwin);
5604 if (qi == NULL)
5605 return;
5606 }
5607
Bram Moolenaar86b68352004-12-27 21:59:20 +00005608 if (*eap->arg == NUL)
5609 buf = curbuf;
5610 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5611 buf = buflist_findnr(atoi((char *)eap->arg));
5612 if (buf == NULL)
5613 EMSG(_(e_invarg));
5614 else if (buf->b_ml.ml_mfp == NULL)
5615 EMSG(_("E681: Buffer is not loaded"));
5616 else
5617 {
5618 if (eap->addr_count == 0)
5619 {
5620 eap->line1 = 1;
5621 eap->line2 = buf->b_ml.ml_line_count;
5622 }
5623 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5624 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5625 EMSG(_(e_invrange));
5626 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005627 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005628 char_u *qf_title = *eap->cmdlinep;
5629
5630 if (buf->b_sfname)
5631 {
5632 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5633 (char *)qf_title, (char *)buf->b_sfname);
5634 qf_title = IObuff;
5635 }
5636
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005637 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005638 (eap->cmdidx != CMD_caddbuffer
5639 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005640 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005641 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005642 if (res >= 0)
5643 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005644 if (au_name != NULL)
5645 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5646 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005647 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5648 eap->cmdidx == CMD_lbuffer))
5649 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005650 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005651 }
5652}
5653
Bram Moolenaar1e015462005-09-25 22:16:38 +00005654#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005655/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005656 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5657 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005658 */
5659 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005660ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005661{
5662 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005663 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005664 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005665 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005666
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005667 switch (eap->cmdidx)
5668 {
5669 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5670 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5671 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5672 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5673 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5674 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5675 default: break;
5676 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005677 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5678 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005679 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005680#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005681 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005682 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005683#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005684 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005685
Bram Moolenaar3c097222017-12-21 20:54:49 +01005686 if (eap->cmdidx == CMD_lexpr
5687 || eap->cmdidx == CMD_lgetexpr
5688 || eap->cmdidx == CMD_laddexpr)
5689 {
5690 qi = ll_get_or_alloc_list(curwin);
5691 if (qi == NULL)
5692 return;
5693 }
5694
Bram Moolenaar4770d092006-01-12 23:22:24 +00005695 /* Evaluate the expression. When the result is a string or a list we can
5696 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005697 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005698 if (tv != NULL)
5699 {
5700 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5701 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5702 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005703 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005704 (eap->cmdidx != CMD_caddexpr
5705 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005706 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005707 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005708 if (res >= 0)
5709 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005710 if (au_name != NULL)
5711 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5712 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005713 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5714 eap->cmdidx == CMD_lexpr))
5715 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005716 }
5717 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005718 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005719 free_tv(tv);
5720 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005721}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005722#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005723
5724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * ":helpgrep {pattern}"
5726 */
5727 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005728ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 regmatch_T regmatch;
5731 char_u *save_cpo;
5732 char_u *p;
5733 int fcount;
5734 char_u **fnames;
5735 FILE *fd;
5736 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005738#ifdef FEAT_MULTI_LANG
5739 char_u *lang;
5740#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005741 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005742 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005743 int new_qi = FALSE;
5744 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005745 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005747#ifdef FEAT_MULTI_LANG
5748 /* Check for a specified language */
5749 lang = check_help_lang(eap->arg);
5750#endif
5751
Bram Moolenaar73633f82012-01-20 13:39:07 +01005752 switch (eap->cmdidx)
5753 {
5754 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5755 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5756 default: break;
5757 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005758 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5759 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005760 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005761#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005762 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005763 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005764#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005765 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01005766
5767 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5768 save_cpo = p_cpo;
5769 p_cpo = empty_option;
5770
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005771 if (eap->cmdidx == CMD_lhelpgrep)
5772 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005773 /* If the current window is a help window, then use it */
5774 if (bt_help(curwin->w_buffer))
5775 wp = curwin;
5776 else
5777 /* Find an existing help window */
5778 FOR_ALL_WINDOWS(wp)
5779 if (bt_help(wp->w_buffer))
5780 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005781
5782 if (wp == NULL) /* Help window not found */
5783 qi = NULL;
5784 else
5785 qi = wp->w_llist;
5786
5787 if (qi == NULL)
5788 {
5789 /* Allocate a new location list for help text matches */
5790 if ((qi = ll_new_list()) == NULL)
5791 return;
5792 new_qi = TRUE;
5793 }
5794 }
5795
Bram Moolenaaree85df32017-03-19 14:19:50 +01005796 /* Autocommands may change the list. Save it for later comparison */
5797 save_qi = qi;
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5800 regmatch.rm_ic = FALSE;
5801 if (regmatch.regprog != NULL)
5802 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005803#ifdef FEAT_MBYTE
5804 vimconv_T vc;
5805
5806 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5807 * differs. */
5808 vc.vc_type = CONV_NONE;
5809 if (!enc_utf8)
5810 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5811#endif
5812
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005814 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815
5816 /* Go through all directories in 'runtimepath' */
5817 p = p_rtp;
5818 while (*p != NUL && !got_int)
5819 {
5820 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5821
5822 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5823 add_pathsep(NameBuff);
5824 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5825 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5826 &fnames, EW_FILE|EW_SILENT) == OK
5827 && fcount > 0)
5828 {
5829 for (fi = 0; fi < fcount && !got_int; ++fi)
5830 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005831#ifdef FEAT_MULTI_LANG
5832 /* Skip files for a different language. */
5833 if (lang != NULL
5834 && STRNICMP(lang, fnames[fi]
5835 + STRLEN(fnames[fi]) - 3, 2) != 0
5836 && !(STRNICMP(lang, "en", 2) == 0
5837 && STRNICMP("txt", fnames[fi]
5838 + STRLEN(fnames[fi]) - 3, 3) == 0))
5839 continue;
5840#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005841 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 if (fd != NULL)
5843 {
5844 lnum = 1;
5845 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5846 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005847 char_u *line = IObuff;
5848#ifdef FEAT_MBYTE
5849 /* Convert a line if 'encoding' is not utf-8 and
5850 * the line contains a non-ASCII character. */
5851 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005852 && has_non_ascii(IObuff))
5853 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005854 line = string_convert(&vc, IObuff, NULL);
5855 if (line == NULL)
5856 line = IObuff;
5857 }
5858#endif
5859
5860 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005862 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
5864 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005865 while (l > 0 && line[l - 1] <= ' ')
5866 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005868 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005869 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 NULL, /* dir */
5871 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005872 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005873 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005875 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005876 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005877 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005878 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 0, /* nr */
5880 1, /* type */
5881 TRUE /* valid */
5882 ) == FAIL)
5883 {
5884 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005885#ifdef FEAT_MBYTE
5886 if (line != IObuff)
5887 vim_free(line);
5888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 break;
5890 }
5891 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005892#ifdef FEAT_MBYTE
5893 if (line != IObuff)
5894 vim_free(line);
5895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 ++lnum;
5897 line_breakcheck();
5898 }
5899 fclose(fd);
5900 }
5901 }
5902 FreeWild(fcount, fnames);
5903 }
5904 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005905
Bram Moolenaar473de612013-06-08 18:19:48 +02005906 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005907#ifdef FEAT_MBYTE
5908 if (vc.vc_type != CONV_NONE)
5909 convert_setup(&vc, NULL, NULL);
5910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005912 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5913 qi->qf_lists[qi->qf_curlist].qf_ptr =
5914 qi->qf_lists[qi->qf_curlist].qf_start;
5915 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005918 if (p_cpo == empty_option)
5919 p_cpo = save_cpo;
5920 else
5921 /* Darn, some plugin changed the value. */
5922 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
Bram Moolenaarb254af32017-12-18 19:48:58 +01005924 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005925 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
Bram Moolenaar73633f82012-01-20 13:39:07 +01005927 if (au_name != NULL)
5928 {
5929 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5930 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005931 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005932 /* autocommands made "qi" invalid */
5933 return;
5934 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01005935
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005937 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005938 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005939 else
5940 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005941
5942 if (eap->cmdidx == CMD_lhelpgrep)
5943 {
5944 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005945 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005946 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005947 {
5948 if (new_qi)
5949 ll_free_all(&qi);
5950 }
5951 else if (curwin->w_llist == NULL)
5952 curwin->w_llist = qi;
5953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954}
5955
5956#endif /* FEAT_QUICKFIX */