blob: 8e745213ac624fe8ecebdbf78eeeae5e4d12bed7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020050/*
51 * Quickfix/Location list definition
52 * Contains a list of entries (qfline_T). qf_start points to the first entry
53 * and qf_last points to the last entry. qf_count contains the list size.
54 *
55 * Usually the list contains one or more entries. But an empty list can be
56 * created using setqflist()/setloclist() with a title and/or user context
57 * information and entries can be added later using setqflist()/setloclist().
58 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000059typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000060{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020061 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000062 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020063 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020065 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 int qf_index; /* current index in the error list */
67 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020068 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020069 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020070 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020071
72 struct dir_stack_T *qf_dir_stack;
73 char_u *qf_directory;
74 struct dir_stack_T *qf_file_stack;
75 char_u *qf_currfile;
76 int qf_multiline;
77 int qf_multiignore;
78 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010079 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000080} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020082/*
83 * Quickfix/Location list stack definition
84 * Contains a list of quickfix/location lists (qf_list_T)
85 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000086struct qf_info_S
87{
88 /*
89 * Count of references to this list. Used only for location lists.
90 * When a location list window reference this list, qf_refcount
91 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
92 * reaches 0, the list is freed.
93 */
94 int qf_refcount;
95 int qf_listcount; /* current number of lists */
96 int qf_curlist; /* current error list */
97 qf_list_T qf_lists[LISTCOUNT];
98};
99
100static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200101static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000103#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/*
106 * Structure used to hold the info of one part of 'errorformat'
107 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000108typedef struct efm_S efm_T;
109struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110{
111 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000112 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
114 char_u prefix; /* prefix of this format line: */
115 /* 'D' enter directory */
116 /* 'X' leave directory */
117 /* 'A' start of multi-line message */
118 /* 'E' error message */
119 /* 'W' warning message */
120 /* 'I' informational message */
121 /* 'C' continuation line */
122 /* 'Z' end of multi-line message */
123 /* 'G' general, unspecific message */
124 /* 'P' push file (partial) message */
125 /* 'Q' pop/quit file (partial) message */
126 /* 'O' overread (partial) message */
127 char_u flags; /* additional flags given in prefix */
128 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000129 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000130 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131};
132
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100133static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
134
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200135static int qf_init_ext(qf_info_T *qi, int qf_idx, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title, char_u *enc);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200136static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_new_list(qf_info_T *qi, char_u *qf_title);
138static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200139static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void qf_free(qf_info_T *qi, int idx);
142static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200143static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200144static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200146static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaar3c097222017-12-21 20:54:49 +0100147static int qflist_valid(win_T *wp, int_u qf_id);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
149static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
151static int is_qf_win(win_T *win, qf_info_T *qi);
152static win_T *qf_find_win(qf_info_T *qi);
153static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200154static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200156static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static char_u *get_mef_name(void);
158static void restore_start_dir(char_u *dirname_start);
159static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
160static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
161static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
162static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164/* Quickfix window check helper macro */
165#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
166/* Location list window check helper macro */
167#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
168/*
169 * Return location list for window 'wp'
170 * For location list window, return the referenced location list
171 */
172#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
173
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200175 * Looking up a buffer can be slow if there are many. Remember the last one
176 * to make this a lot faster if there are multiple matches in the same file.
177 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200178static char_u *qf_last_bufname = NULL;
179static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200180
Bram Moolenaar3c097222017-12-21 20:54:49 +0100181static char *e_loc_list_changed =
182 N_("E926: Current location list was changed");
183
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200184/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000185 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200186 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 * Return -1 for error, number of errors for success.
188 */
189 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200190qf_init(win_T *wp,
191 char_u *efile,
192 char_u *errorformat,
193 int newlist, /* TRUE: start a new error list */
194 char_u *qf_title,
195 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000197 qf_info_T *qi = &ql_info;
198
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000199 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000200 {
201 qi = ll_get_or_alloc_list(wp);
202 if (qi == NULL)
203 return FAIL;
204 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000205
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200206 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
207 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000208}
209
210/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200211 * Maximum number of bytes allowed per line while reading a errorfile.
212 */
213#define LINE_MAXLEN 4096
214
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200215static struct fmtpattern
216{
217 char_u convchar;
218 char *pattern;
219} fmt_pat[FMT_PATTERNS] =
220 {
221 {'f', ".\\+"}, /* only used when at end */
222 {'n', "\\d\\+"},
223 {'l', "\\d\\+"},
224 {'c', "\\d\\+"},
225 {'t', "."},
226 {'m', ".\\+"},
227 {'r', ".*"},
228 {'p', "[- .]*"},
229 {'v', "\\d\\+"},
230 {'s', ".\\+"}
231 };
232
233/*
234 * Converts a 'errorformat' string to regular expression pattern
235 */
236 static int
237efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200238 char_u *efm,
239 int len,
240 efm_T *fmt_ptr,
241 char_u *regpat,
242 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200243{
244 char_u *ptr;
245 char_u *efmp;
246 char_u *srcptr;
247 int round;
248 int idx = 0;
249
250 /*
251 * Build regexp pattern from current 'errorformat' option
252 */
253 ptr = regpat;
254 *ptr++ = '^';
255 round = 0;
256 for (efmp = efm; efmp < efm + len; ++efmp)
257 {
258 if (*efmp == '%')
259 {
260 ++efmp;
261 for (idx = 0; idx < FMT_PATTERNS; ++idx)
262 if (fmt_pat[idx].convchar == *efmp)
263 break;
264 if (idx < FMT_PATTERNS)
265 {
266 if (fmt_ptr->addr[idx])
267 {
268 sprintf((char *)errmsg,
269 _("E372: Too many %%%c in format string"), *efmp);
270 EMSG(errmsg);
271 return -1;
272 }
273 if ((idx
274 && idx < 6
275 && vim_strchr((char_u *)"DXOPQ",
276 fmt_ptr->prefix) != NULL)
277 || (idx == 6
278 && vim_strchr((char_u *)"OPQ",
279 fmt_ptr->prefix) == NULL))
280 {
281 sprintf((char *)errmsg,
282 _("E373: Unexpected %%%c in format string"), *efmp);
283 EMSG(errmsg);
284 return -1;
285 }
286 fmt_ptr->addr[idx] = (char_u)++round;
287 *ptr++ = '\\';
288 *ptr++ = '(';
289#ifdef BACKSLASH_IN_FILENAME
290 if (*efmp == 'f')
291 {
292 /* Also match "c:" in the file name, even when
293 * checking for a colon next: "%f:".
294 * "\%(\a:\)\=" */
295 STRCPY(ptr, "\\%(\\a:\\)\\=");
296 ptr += 10;
297 }
298#endif
299 if (*efmp == 'f' && efmp[1] != NUL)
300 {
301 if (efmp[1] != '\\' && efmp[1] != '%')
302 {
303 /* A file name may contain spaces, but this isn't
304 * in "\f". For "%f:%l:%m" there may be a ":" in
305 * the file name. Use ".\{-1,}x" instead (x is
306 * the next character), the requirement that :999:
307 * follows should work. */
308 STRCPY(ptr, ".\\{-1,}");
309 ptr += 7;
310 }
311 else
312 {
313 /* File name followed by '\\' or '%': include as
314 * many file name chars as possible. */
315 STRCPY(ptr, "\\f\\+");
316 ptr += 4;
317 }
318 }
319 else
320 {
321 srcptr = (char_u *)fmt_pat[idx].pattern;
322 while ((*ptr = *srcptr++) != NUL)
323 ++ptr;
324 }
325 *ptr++ = '\\';
326 *ptr++ = ')';
327 }
328 else if (*efmp == '*')
329 {
330 if (*++efmp == '[' || *efmp == '\\')
331 {
332 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
333 {
334 if (efmp[1] == '^')
335 *ptr++ = *++efmp;
336 if (efmp < efm + len)
337 {
338 *ptr++ = *++efmp; /* could be ']' */
339 while (efmp < efm + len
340 && (*ptr++ = *++efmp) != ']')
341 /* skip */;
342 if (efmp == efm + len)
343 {
344 EMSG(_("E374: Missing ] in format string"));
345 return -1;
346 }
347 }
348 }
349 else if (efmp < efm + len) /* %*\D, %*\s etc. */
350 *ptr++ = *++efmp;
351 *ptr++ = '\\';
352 *ptr++ = '+';
353 }
354 else
355 {
356 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
357 sprintf((char *)errmsg,
358 _("E375: Unsupported %%%c in format string"), *efmp);
359 EMSG(errmsg);
360 return -1;
361 }
362 }
363 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
364 *ptr++ = *efmp; /* regexp magic characters */
365 else if (*efmp == '#')
366 *ptr++ = '*';
367 else if (*efmp == '>')
368 fmt_ptr->conthere = TRUE;
369 else if (efmp == efm + 1) /* analyse prefix */
370 {
371 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
372 fmt_ptr->flags = *efmp++;
373 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
374 fmt_ptr->prefix = *efmp;
375 else
376 {
377 sprintf((char *)errmsg,
378 _("E376: Invalid %%%c in format string prefix"), *efmp);
379 EMSG(errmsg);
380 return -1;
381 }
382 }
383 else
384 {
385 sprintf((char *)errmsg,
386 _("E377: Invalid %%%c in format string"), *efmp);
387 EMSG(errmsg);
388 return -1;
389 }
390 }
391 else /* copy normal character */
392 {
393 if (*efmp == '\\' && efmp + 1 < efm + len)
394 ++efmp;
395 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
396 *ptr++ = '\\'; /* escape regexp atoms */
397 if (*efmp)
398 *ptr++ = *efmp;
399 }
400 }
401 *ptr++ = '$';
402 *ptr = NUL;
403
404 return 0;
405}
406
407 static void
408free_efm_list(efm_T **efm_first)
409{
410 efm_T *efm_ptr;
411
412 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
413 {
414 *efm_first = efm_ptr->next;
415 vim_regfree(efm_ptr->prog);
416 vim_free(efm_ptr);
417 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100418 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419}
420
421/* Parse 'errorformat' option */
422 static efm_T *
423parse_efm_option(char_u *efm)
424{
425 char_u *errmsg = NULL;
426 int errmsglen;
427 efm_T *fmt_ptr = NULL;
428 efm_T *fmt_first = NULL;
429 efm_T *fmt_last = NULL;
430 char_u *fmtstr = NULL;
431 int len;
432 int i;
433 int round;
434
435 errmsglen = CMDBUFFSIZE + 1;
436 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
437 if (errmsg == NULL)
438 goto parse_efm_end;
439
440 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200441 * Each part of the format string is copied and modified from errorformat
442 * to regex prog. Only a few % characters are allowed.
443 */
444
445 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 * Get some space to modify the format string into.
447 */
448 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
449 for (round = FMT_PATTERNS; round > 0; )
450 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200451#ifdef BACKSLASH_IN_FILENAME
452 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453#else
454 i += 2; /* "%f" can become two chars longer */
455#endif
456 if ((fmtstr = alloc(i)) == NULL)
457 goto parse_efm_error;
458
459 while (efm[0] != NUL)
460 {
461 /*
462 * Allocate a new eformat structure and put it at the end of the list
463 */
464 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
465 if (fmt_ptr == NULL)
466 goto parse_efm_error;
467 if (fmt_first == NULL) /* first one */
468 fmt_first = fmt_ptr;
469 else
470 fmt_last->next = fmt_ptr;
471 fmt_last = fmt_ptr;
472
473 /*
474 * Isolate one part in the 'errorformat' option
475 */
476 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
477 if (efm[len] == '\\' && efm[len + 1] != NUL)
478 ++len;
479
480 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
481 goto parse_efm_error;
482 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
483 goto parse_efm_error;
484 /*
485 * Advance to next part
486 */
487 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
488 }
489
490 if (fmt_first == NULL) /* nothing found */
491 EMSG(_("E378: 'errorformat' contains no pattern"));
492
493 goto parse_efm_end;
494
495parse_efm_error:
496 free_efm_list(&fmt_first);
497
498parse_efm_end:
499 vim_free(fmtstr);
500 vim_free(errmsg);
501
502 return fmt_first;
503}
504
Bram Moolenaare0d37972016-07-15 22:36:01 +0200505enum {
506 QF_FAIL = 0,
507 QF_OK = 1,
508 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200509 QF_NOMEM = 3,
510 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200511};
512
513typedef struct {
514 char_u *linebuf;
515 int linelen;
516 char_u *growbuf;
517 int growbufsiz;
518 FILE *fd;
519 typval_T *tv;
520 char_u *p_str;
521 listitem_T *p_li;
522 buf_T *buf;
523 linenr_T buflnum;
524 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100525 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200526} qfstate_T;
527
528 static char_u *
529qf_grow_linebuf(qfstate_T *state, int newsz)
530{
531 /*
532 * If the line exceeds LINE_MAXLEN exclude the last
533 * byte since it's not a NL character.
534 */
535 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
536 if (state->growbuf == NULL)
537 {
538 state->growbuf = alloc(state->linelen + 1);
539 if (state->growbuf == NULL)
540 return NULL;
541 state->growbufsiz = state->linelen;
542 }
543 else if (state->linelen > state->growbufsiz)
544 {
545 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
546 if (state->growbuf == NULL)
547 return NULL;
548 state->growbufsiz = state->linelen;
549 }
550 return state->growbuf;
551}
552
553/*
554 * Get the next string (separated by newline) from state->p_str.
555 */
556 static int
557qf_get_next_str_line(qfstate_T *state)
558{
559 /* Get the next line from the supplied string */
560 char_u *p_str = state->p_str;
561 char_u *p;
562 int len;
563
564 if (*p_str == NUL) /* Reached the end of the string */
565 return QF_END_OF_INPUT;
566
567 p = vim_strchr(p_str, '\n');
568 if (p != NULL)
569 len = (int)(p - p_str) + 1;
570 else
571 len = (int)STRLEN(p_str);
572
573 if (len > IOSIZE - 2)
574 {
575 state->linebuf = qf_grow_linebuf(state, len);
576 if (state->linebuf == NULL)
577 return QF_NOMEM;
578 }
579 else
580 {
581 state->linebuf = IObuff;
582 state->linelen = len;
583 }
584 vim_strncpy(state->linebuf, p_str, state->linelen);
585
586 /*
587 * Increment using len in order to discard the rest of the
588 * line if it exceeds LINE_MAXLEN.
589 */
590 p_str += len;
591 state->p_str = p_str;
592
593 return QF_OK;
594}
595
596/*
597 * Get the next string from state->p_Li.
598 */
599 static int
600qf_get_next_list_line(qfstate_T *state)
601{
602 listitem_T *p_li = state->p_li;
603 int len;
604
605 while (p_li != NULL
606 && (p_li->li_tv.v_type != VAR_STRING
607 || p_li->li_tv.vval.v_string == NULL))
608 p_li = p_li->li_next; /* Skip non-string items */
609
610 if (p_li == NULL) /* End of the list */
611 {
612 state->p_li = NULL;
613 return QF_END_OF_INPUT;
614 }
615
616 len = (int)STRLEN(p_li->li_tv.vval.v_string);
617 if (len > IOSIZE - 2)
618 {
619 state->linebuf = qf_grow_linebuf(state, len);
620 if (state->linebuf == NULL)
621 return QF_NOMEM;
622 }
623 else
624 {
625 state->linebuf = IObuff;
626 state->linelen = len;
627 }
628
629 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
630
631 state->p_li = p_li->li_next; /* next item */
632 return QF_OK;
633}
634
635/*
636 * Get the next string from state->buf.
637 */
638 static int
639qf_get_next_buf_line(qfstate_T *state)
640{
641 char_u *p_buf = NULL;
642 int len;
643
644 /* Get the next line from the supplied buffer */
645 if (state->buflnum > state->lnumlast)
646 return QF_END_OF_INPUT;
647
648 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
649 state->buflnum += 1;
650
651 len = (int)STRLEN(p_buf);
652 if (len > IOSIZE - 2)
653 {
654 state->linebuf = qf_grow_linebuf(state, len);
655 if (state->linebuf == NULL)
656 return QF_NOMEM;
657 }
658 else
659 {
660 state->linebuf = IObuff;
661 state->linelen = len;
662 }
663 vim_strncpy(state->linebuf, p_buf, state->linelen);
664
665 return QF_OK;
666}
667
668/*
669 * Get the next string from file state->fd.
670 */
671 static int
672qf_get_next_file_line(qfstate_T *state)
673{
674 int discard;
675 int growbuflen;
676
677 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
678 return QF_END_OF_INPUT;
679
680 discard = FALSE;
681 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200682 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200683 {
684 /*
685 * The current line exceeds IObuff, continue reading using
686 * growbuf until EOL or LINE_MAXLEN bytes is read.
687 */
688 if (state->growbuf == NULL)
689 {
690 state->growbufsiz = 2 * (IOSIZE - 1);
691 state->growbuf = alloc(state->growbufsiz);
692 if (state->growbuf == NULL)
693 return QF_NOMEM;
694 }
695
696 /* Copy the read part of the line, excluding null-terminator */
697 memcpy(state->growbuf, IObuff, IOSIZE - 1);
698 growbuflen = state->linelen;
699
700 for (;;)
701 {
702 if (fgets((char *)state->growbuf + growbuflen,
703 state->growbufsiz - growbuflen, state->fd) == NULL)
704 break;
705 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
706 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200707 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200708 break;
709 if (state->growbufsiz == LINE_MAXLEN)
710 {
711 discard = TRUE;
712 break;
713 }
714
715 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
716 ? 2 * state->growbufsiz : LINE_MAXLEN;
717 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
718 if (state->growbuf == NULL)
719 return QF_NOMEM;
720 }
721
722 while (discard)
723 {
724 /*
725 * The current line is longer than LINE_MAXLEN, continue
726 * reading but discard everything until EOL or EOF is
727 * reached.
728 */
729 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
730 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200731 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200732 break;
733 }
734
735 state->linebuf = state->growbuf;
736 state->linelen = growbuflen;
737 }
738 else
739 state->linebuf = IObuff;
740
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100741#ifdef FEAT_MBYTE
742 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200743 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
744 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100745 char_u *line;
746
747 line = string_convert(&state->vc, state->linebuf, &state->linelen);
748 if (line != NULL)
749 {
750 if (state->linelen < IOSIZE)
751 {
752 STRCPY(state->linebuf, line);
753 vim_free(line);
754 }
755 else
756 {
757 vim_free(state->growbuf);
758 state->linebuf = state->growbuf = line;
759 state->growbufsiz = state->linelen < LINE_MAXLEN
760 ? state->linelen : LINE_MAXLEN;
761 }
762 }
763 }
764#endif
765
Bram Moolenaare0d37972016-07-15 22:36:01 +0200766 return QF_OK;
767}
768
769/*
770 * Get the next string from a file/buffer/list/string.
771 */
772 static int
773qf_get_nextline(qfstate_T *state)
774{
775 int status = QF_FAIL;
776
777 if (state->fd == NULL)
778 {
779 if (state->tv != NULL)
780 {
781 if (state->tv->v_type == VAR_STRING)
782 /* Get the next line from the supplied string */
783 status = qf_get_next_str_line(state);
784 else if (state->tv->v_type == VAR_LIST)
785 /* Get the next line from the supplied list */
786 status = qf_get_next_list_line(state);
787 }
788 else
789 /* Get the next line from the supplied buffer */
790 status = qf_get_next_buf_line(state);
791 }
792 else
793 /* Get the next line from the supplied file */
794 status = qf_get_next_file_line(state);
795
796 if (status != QF_OK)
797 return status;
798
799 /* remove newline/CR from the line */
800 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200801 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802 state->linebuf[state->linelen - 1] = NUL;
803#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200804 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
805 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200807 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808
809#ifdef FEAT_MBYTE
810 remove_bom(state->linebuf);
811#endif
812
813 return QF_OK;
814}
815
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200816typedef struct {
817 char_u *namebuf;
818 char_u *errmsg;
819 int errmsglen;
820 long lnum;
821 int col;
822 char_u use_viscol;
823 char_u *pattern;
824 int enr;
825 int type;
826 int valid;
827} qffields_T;
828
829/*
830 * Parse a line and get the quickfix fields.
831 * Return the QF_ status.
832 */
833 static int
834qf_parse_line(
835 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200836 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200837 char_u *linebuf,
838 int linelen,
839 efm_T *fmt_first,
840 qffields_T *fields)
841{
842 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200843 char_u *ptr;
844 int len;
845 int i;
846 int idx = 0;
847 char_u *tail = NULL;
848 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200849 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200850
851 /* Always ignore case when looking for a matching error. */
852 regmatch.rm_ic = TRUE;
853
854 /* If there was no %> item start at the first pattern */
855 if (fmt_start == NULL)
856 fmt_ptr = fmt_first;
857 else
858 {
859 fmt_ptr = fmt_start;
860 fmt_start = NULL;
861 }
862
863 /*
864 * Try to match each part of 'errorformat' until we find a complete
865 * match or no match.
866 */
867 fields->valid = TRUE;
868restofline:
869 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
870 {
871 int r;
872
873 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200874 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200875 continue;
876 fields->namebuf[0] = NUL;
877 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200878 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200879 fields->errmsg[0] = NUL;
880 fields->lnum = 0;
881 fields->col = 0;
882 fields->use_viscol = FALSE;
883 fields->enr = -1;
884 fields->type = 0;
885 tail = NULL;
886
887 regmatch.regprog = fmt_ptr->prog;
888 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
889 fmt_ptr->prog = regmatch.regprog;
890 if (r)
891 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200892 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200893 continue;
894 if (vim_strchr((char_u *)"EWI", idx) != NULL)
895 fields->type = idx;
896 else
897 fields->type = 0;
898 /*
899 * Extract error message data from matched line.
900 * We check for an actual submatch, because "\[" and "\]" in
901 * the 'errorformat' may cause the wrong submatch to be used.
902 */
903 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
904 {
905 int c;
906
907 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
908 continue;
909
910 /* Expand ~/file and $HOME/file to full path. */
911 c = *regmatch.endp[i];
912 *regmatch.endp[i] = NUL;
913 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
914 *regmatch.endp[i] = c;
915
916 if (vim_strchr((char_u *)"OPQ", idx) != NULL
917 && mch_getperm(fields->namebuf) == -1)
918 continue;
919 }
920 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
921 {
922 if (regmatch.startp[i] == NULL)
923 continue;
924 fields->enr = (int)atol((char *)regmatch.startp[i]);
925 }
926 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
927 {
928 if (regmatch.startp[i] == NULL)
929 continue;
930 fields->lnum = atol((char *)regmatch.startp[i]);
931 }
932 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
933 {
934 if (regmatch.startp[i] == NULL)
935 continue;
936 fields->col = (int)atol((char *)regmatch.startp[i]);
937 }
938 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
939 {
940 if (regmatch.startp[i] == NULL)
941 continue;
942 fields->type = *regmatch.startp[i];
943 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200944 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200945 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200946 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200947 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200948 /* linelen + null terminator */
949 if ((fields->errmsg = vim_realloc(fields->errmsg,
950 linelen + 1)) == NULL)
951 return QF_NOMEM;
952 fields->errmsglen = linelen + 1;
953 }
954 vim_strncpy(fields->errmsg, linebuf, linelen);
955 }
956 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
957 {
958 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
959 continue;
960 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200961 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200962 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200963 /* len + null terminator */
964 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
965 == NULL)
966 return QF_NOMEM;
967 fields->errmsglen = len + 1;
968 }
969 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
970 }
971 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
972 {
973 if (regmatch.startp[i] == NULL)
974 continue;
975 tail = regmatch.startp[i];
976 }
977 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
978 {
979 char_u *match_ptr;
980
981 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
982 continue;
983 fields->col = 0;
984 for (match_ptr = regmatch.startp[i];
985 match_ptr != regmatch.endp[i]; ++match_ptr)
986 {
987 ++fields->col;
988 if (*match_ptr == TAB)
989 {
990 fields->col += 7;
991 fields->col -= fields->col % 8;
992 }
993 }
994 ++fields->col;
995 fields->use_viscol = TRUE;
996 }
997 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
998 {
999 if (regmatch.startp[i] == NULL)
1000 continue;
1001 fields->col = (int)atol((char *)regmatch.startp[i]);
1002 fields->use_viscol = TRUE;
1003 }
1004 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1005 {
1006 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1007 continue;
1008 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1009 if (len > CMDBUFFSIZE - 5)
1010 len = CMDBUFFSIZE - 5;
1011 STRCPY(fields->pattern, "^\\V");
1012 STRNCAT(fields->pattern, regmatch.startp[i], len);
1013 fields->pattern[len + 3] = '\\';
1014 fields->pattern[len + 4] = '$';
1015 fields->pattern[len + 5] = NUL;
1016 }
1017 break;
1018 }
1019 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001020 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001021
1022 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1023 {
1024 if (fmt_ptr != NULL)
1025 {
1026 if (idx == 'D') /* enter directory */
1027 {
1028 if (*fields->namebuf == NUL)
1029 {
1030 EMSG(_("E379: Missing or empty directory name"));
1031 return QF_FAIL;
1032 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001033 qfl->qf_directory =
1034 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1035 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001036 return QF_FAIL;
1037 }
1038 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001039 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001040 }
1041 fields->namebuf[0] = NUL; /* no match found, remove file name */
1042 fields->lnum = 0; /* don't jump to this line */
1043 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001044 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001045 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001046 /* linelen + null terminator */
1047 if ((fields->errmsg = vim_realloc(fields->errmsg,
1048 linelen + 1)) == NULL)
1049 return QF_NOMEM;
1050 fields->errmsglen = linelen + 1;
1051 }
1052 /* copy whole line to error message */
1053 vim_strncpy(fields->errmsg, linebuf, linelen);
1054 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001055 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001056 }
1057 else if (fmt_ptr != NULL)
1058 {
1059 /* honor %> item */
1060 if (fmt_ptr->conthere)
1061 fmt_start = fmt_ptr;
1062
1063 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1064 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001065 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1066 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001067 }
1068 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1069 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001070 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001071 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001072 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001073
Bram Moolenaar9b457942016-10-09 16:10:05 +02001074 if (qfprev == NULL)
1075 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001076 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001077 {
1078 len = (int)STRLEN(qfprev->qf_text);
1079 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1080 == NULL)
1081 return QF_FAIL;
1082 STRCPY(ptr, qfprev->qf_text);
1083 vim_free(qfprev->qf_text);
1084 qfprev->qf_text = ptr;
1085 *(ptr += len) = '\n';
1086 STRCPY(++ptr, fields->errmsg);
1087 }
1088 if (qfprev->qf_nr == -1)
1089 qfprev->qf_nr = fields->enr;
1090 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1091 /* only printable chars allowed */
1092 qfprev->qf_type = fields->type;
1093
1094 if (!qfprev->qf_lnum)
1095 qfprev->qf_lnum = fields->lnum;
1096 if (!qfprev->qf_col)
1097 qfprev->qf_col = fields->col;
1098 qfprev->qf_viscol = fields->use_viscol;
1099 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001100 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1101 qfl->qf_directory,
1102 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001103 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001104 : qfl->qf_currfile != NULL && fields->valid
1105 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001106 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001107 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001108 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001109 line_breakcheck();
1110 return QF_IGNORE_LINE;
1111 }
1112 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1113 {
1114 /* global file names */
1115 fields->valid = FALSE;
1116 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1117 {
1118 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001119 qfl->qf_currfile =
1120 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001121 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001122 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001123 *fields->namebuf = NUL;
1124 if (tail && *tail)
1125 {
1126 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001127 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001128 goto restofline;
1129 }
1130 }
1131 }
1132 if (fmt_ptr->flags == '-') /* generally exclude this line */
1133 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001134 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001135 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001136 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001137 return QF_IGNORE_LINE;
1138 }
1139 }
1140
1141 return QF_OK;
1142}
1143
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001144/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001145 * Read the errorfile "efile" into memory, line by line, building the error
1146 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001147 * Alternative: when "efile" is NULL read errors from buffer "buf".
1148 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001149 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001150 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1151 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001152 * Return -1 for error, number of errors for success.
1153 */
1154 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001155qf_init_ext(
1156 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001157 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001158 char_u *efile,
1159 buf_T *buf,
1160 typval_T *tv,
1161 char_u *errorformat,
1162 int newlist, /* TRUE: start a new error list */
1163 linenr_T lnumfirst, /* first line number to use */
1164 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001165 char_u *qf_title,
1166 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001167{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001168 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001169 qfstate_T state;
1170 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001171 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001172 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001173 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001175 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001177 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001179 /* Do not used the cached buffer, it may have been wiped out. */
1180 vim_free(qf_last_bufname);
1181 qf_last_bufname = NULL;
1182
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001183 vim_memset(&state, 0, sizeof(state));
1184 vim_memset(&fields, 0, sizeof(fields));
1185#ifdef FEAT_MBYTE
1186 state.vc.vc_type = CONV_NONE;
1187 if (enc != NULL && *enc != NUL)
1188 convert_setup(&state.vc, enc, p_enc);
1189#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001190 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1191 fields.errmsglen = CMDBUFFSIZE + 1;
1192 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1193 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001194 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 goto qf_init_end;
1196
Bram Moolenaare0d37972016-07-15 22:36:01 +02001197 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
1199 EMSG2(_(e_openerrf), efile);
1200 goto qf_init_end;
1201 }
1202
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001203 if (newlist || qf_idx == qi->qf_listcount)
1204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001206 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001207 qf_idx = qi->qf_curlist;
1208 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001209 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001210 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001211 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001212 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001213 if (qi->qf_lists[qf_idx].qf_count > 0)
1214 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001217 qfl = &qi->qf_lists[qf_idx];
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001220 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001221 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 else
1223 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001225 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001226 * If the errorformat didn't change between calls, then reuse the
1227 * previously parsed values.
1228 */
1229 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1230 {
1231 /* free the previously parsed data */
1232 vim_free(last_efm);
1233 last_efm = NULL;
1234 free_efm_list(&fmt_first);
1235
1236 /* parse the current 'efm' */
1237 fmt_first = parse_efm_option(efm);
1238 if (fmt_first != NULL)
1239 last_efm = vim_strsave(efm);
1240 }
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /*
1246 * got_int is reset here, because it was probably set when killing the
1247 * ":make" command, but we still want to read the errorfile then.
1248 */
1249 got_int = FALSE;
1250
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001251 if (tv != NULL)
1252 {
1253 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001254 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001255 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001256 state.p_li = tv->vval.v_list->lv_first;
1257 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001258 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001259 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001260 state.buflnum = lnumfirst;
1261 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /*
1264 * Read the lines in the error file one by one.
1265 * Try to recognize one of the error formats in each line.
1266 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001267 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001269 /* Get the next line from a file/buffer/list/string */
1270 status = qf_get_nextline(&state);
1271 if (status == QF_NOMEM) /* memory alloc failure */
1272 goto qf_init_end;
1273 if (status == QF_END_OF_INPUT) /* end of input */
1274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001276 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1277 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001278 if (status == QF_FAIL)
1279 goto error2;
1280 if (status == QF_NOMEM)
1281 goto qf_init_end;
1282 if (status == QF_IGNORE_LINE)
1283 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001285 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001286 qf_idx,
1287 qfl->qf_directory,
1288 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001289 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001290 : ((qfl->qf_currfile != NULL && fields.valid)
1291 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001292 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001293 fields.errmsg,
1294 fields.lnum,
1295 fields.col,
1296 fields.use_viscol,
1297 fields.pattern,
1298 fields.enr,
1299 fields.type,
1300 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 goto error2;
1302 line_breakcheck();
1303 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001304 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001306 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001308 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001309 qfl->qf_ptr = qfl->qf_start;
1310 qfl->qf_index = 1;
1311 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 else
1314 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001315 qfl->qf_nonevalid = FALSE;
1316 if (qfl->qf_ptr == NULL)
1317 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001319 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001320 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001321 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 EMSG(_(e_readerrf));
1324error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001325 if (!adding)
1326 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001327 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001328 qf_free(qi, qi->qf_curlist);
1329 qi->qf_listcount--;
1330 if (qi->qf_curlist > 0)
1331 --qi->qf_curlist;
1332 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001333qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001334 if (state.fd != NULL)
1335 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001336 vim_free(fields.namebuf);
1337 vim_free(fields.errmsg);
1338 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001339 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001341 if (qf_idx == qi->qf_curlist)
1342 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001343#ifdef FEAT_MBYTE
1344 if (state.vc.vc_type != CONV_NONE)
1345 convert_setup(&state.vc, NULL, NULL);
1346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 return retval;
1349}
1350
Bram Moolenaarfb604092014-07-23 15:55:00 +02001351 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001352qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001353{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001354 vim_free(qi->qf_lists[qf_idx].qf_title);
1355 qi->qf_lists[qf_idx].qf_title = NULL;
1356
Bram Moolenaarfb604092014-07-23 15:55:00 +02001357 if (title != NULL)
1358 {
1359 char_u *p = alloc((int)STRLEN(title) + 2);
1360
Bram Moolenaara3921f42017-06-04 15:30:34 +02001361 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001362 if (p != NULL)
1363 sprintf((char *)p, ":%s", (char *)title);
1364 }
1365}
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001368 * Prepare for adding a new quickfix list. If the current list is in the
1369 * middle of the stack, then all the following lists are freed and then
1370 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 */
1372 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001373qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
1375 int i;
1376
1377 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001378 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 * the current entry. This makes it possible to browse in a tree-like
1380 * way with ":grep'.
1381 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001382 while (qi->qf_listcount > qi->qf_curlist + 1)
1383 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
1385 /*
1386 * When the stack is full, remove to oldest entry
1387 * Otherwise, add a new entry.
1388 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001389 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001391 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001393 qi->qf_lists[i - 1] = qi->qf_lists[i];
1394 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001397 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001398 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001399 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001400 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401}
1402
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001403/*
1404 * Free a location list
1405 */
1406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001407ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001408{
1409 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001410 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001411
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001412 qi = *pqi;
1413 if (qi == NULL)
1414 return;
1415 *pqi = NULL; /* Remove reference to this list */
1416
1417 qi->qf_refcount--;
1418 if (qi->qf_refcount < 1)
1419 {
1420 /* No references to this location list */
1421 for (i = 0; i < qi->qf_listcount; ++i)
1422 qf_free(qi, i);
1423 vim_free(qi);
1424 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001425}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001426
1427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001428qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001429{
1430 int i;
1431 qf_info_T *qi = &ql_info;
1432
1433 if (wp != NULL)
1434 {
1435 /* location list */
1436 ll_free_all(&wp->w_llist);
1437 ll_free_all(&wp->w_llist_ref);
1438 }
1439 else
1440 /* quickfix list */
1441 for (i = 0; i < qi->qf_listcount; ++i)
1442 qf_free(qi, i);
1443}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445/*
1446 * Add an entry to the end of the list of errors.
1447 * Returns OK or FAIL.
1448 */
1449 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001450qf_add_entry(
1451 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001452 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001453 char_u *dir, /* optional directory name */
1454 char_u *fname, /* file name or NULL */
1455 int bufnum, /* buffer number or zero */
1456 char_u *mesg, /* message */
1457 long lnum, /* line number */
1458 int col, /* column */
1459 int vis_col, /* using visual column */
1460 char_u *pattern, /* search pattern */
1461 int nr, /* error number */
1462 int type, /* type character */
1463 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001465 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001466 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001468 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001470 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001471 {
1472 buf_T *buf = buflist_findnr(bufnum);
1473
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001474 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001475 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001476 buf->b_has_qf_entry |=
1477 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001478 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001479 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001480 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1482 {
1483 vim_free(qfp);
1484 return FAIL;
1485 }
1486 qfp->qf_lnum = lnum;
1487 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001488 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001489 if (pattern == NULL || *pattern == NUL)
1490 qfp->qf_pattern = NULL;
1491 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1492 {
1493 vim_free(qfp->qf_text);
1494 vim_free(qfp);
1495 return FAIL;
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 qfp->qf_nr = nr;
1498 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1499 type = 0;
1500 qfp->qf_type = type;
1501 qfp->qf_valid = valid;
1502
Bram Moolenaara3921f42017-06-04 15:30:34 +02001503 lastp = &qi->qf_lists[qf_idx].qf_last;
1504 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001505 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001507 qi->qf_lists[qf_idx].qf_start = qfp;
1508 qi->qf_lists[qf_idx].qf_ptr = qfp;
1509 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001510 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 else
1513 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001514 qfp->qf_prev = *lastp;
1515 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001517 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001519 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001520 ++qi->qf_lists[qf_idx].qf_count;
1521 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001524 qi->qf_lists[qf_idx].qf_index =
1525 qi->qf_lists[qf_idx].qf_count;
1526 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
1528
1529 return OK;
1530}
1531
1532/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001533 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001535 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001536ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001537{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001538 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001539
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001540 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1541 if (qi != NULL)
1542 {
1543 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1544 qi->qf_refcount++;
1545 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001546
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001547 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548}
1549
1550/*
1551 * Return the location list for window 'wp'.
1552 * If not present, allocate a location list
1553 */
1554 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001555ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001556{
1557 if (IS_LL_WINDOW(wp))
1558 /* For a location list window, use the referenced location list */
1559 return wp->w_llist_ref;
1560
1561 /*
1562 * For a non-location list window, w_llist_ref should not point to a
1563 * location list.
1564 */
1565 ll_free_all(&wp->w_llist_ref);
1566
1567 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001568 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001569 return wp->w_llist;
1570}
1571
1572/*
1573 * Copy the location list from window "from" to window "to".
1574 */
1575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001576copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001577{
1578 qf_info_T *qi;
1579 int idx;
1580 int i;
1581
1582 /*
1583 * When copying from a location list window, copy the referenced
1584 * location list. For other windows, copy the location list for
1585 * that window.
1586 */
1587 if (IS_LL_WINDOW(from))
1588 qi = from->w_llist_ref;
1589 else
1590 qi = from->w_llist;
1591
1592 if (qi == NULL) /* no location list to copy */
1593 return;
1594
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001595 /* allocate a new location list */
1596 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001597 return;
1598
1599 to->w_llist->qf_listcount = qi->qf_listcount;
1600
1601 /* Copy the location lists one at a time */
1602 for (idx = 0; idx < qi->qf_listcount; idx++)
1603 {
1604 qf_list_T *from_qfl;
1605 qf_list_T *to_qfl;
1606
1607 to->w_llist->qf_curlist = idx;
1608
1609 from_qfl = &qi->qf_lists[idx];
1610 to_qfl = &to->w_llist->qf_lists[idx];
1611
1612 /* Some of the fields are populated by qf_add_entry() */
1613 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1614 to_qfl->qf_count = 0;
1615 to_qfl->qf_index = 0;
1616 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001617 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001618 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001619 if (from_qfl->qf_title != NULL)
1620 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1621 else
1622 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001623 if (from_qfl->qf_ctx != NULL)
1624 {
1625 to_qfl->qf_ctx = alloc_tv();
1626 if (to_qfl->qf_ctx != NULL)
1627 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1628 }
1629 else
1630 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631
1632 if (from_qfl->qf_count)
1633 {
1634 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001635 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001636
1637 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001638 for (i = 0, from_qfp = from_qfl->qf_start;
1639 i < from_qfl->qf_count && from_qfp != NULL;
1640 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001641 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001642 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001643 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 NULL,
1645 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001646 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001647 from_qfp->qf_text,
1648 from_qfp->qf_lnum,
1649 from_qfp->qf_col,
1650 from_qfp->qf_viscol,
1651 from_qfp->qf_pattern,
1652 from_qfp->qf_nr,
1653 0,
1654 from_qfp->qf_valid) == FAIL)
1655 {
1656 qf_free_all(to);
1657 return;
1658 }
1659 /*
1660 * qf_add_entry() will not set the qf_num field, as the
1661 * directory and file names are not supplied. So the qf_fnum
1662 * field is copied here.
1663 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001664 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001665 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1666 prevp->qf_type = from_qfp->qf_type; /* error type */
1667 if (from_qfl->qf_ptr == from_qfp)
1668 to_qfl->qf_ptr = prevp; /* current location */
1669 }
1670 }
1671
1672 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1673
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001674 /* Assign a new ID for the location list */
1675 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001676 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001677
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001678 /* When no valid entries are present in the list, qf_ptr points to
1679 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001680 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001681 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001682 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001683 to_qfl->qf_index = 1;
1684 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001685 }
1686
1687 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1688}
1689
1690/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001691 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001692 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 */
1694 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001695qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
Bram Moolenaar82404332016-07-10 17:00:38 +02001697 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001698 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001699 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (fname == NULL || *fname == NUL) /* no file name */
1702 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaare60acc12011-05-10 16:41:25 +02001704#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001705 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001706#endif
1707#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001708 if (directory != NULL)
1709 slash_adjust(directory);
1710 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001711#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001712 if (directory != NULL && !vim_isAbsName(fname)
1713 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1714 {
1715 /*
1716 * Here we check if the file really exists.
1717 * This should normally be true, but if make works without
1718 * "leaving directory"-messages we might have missed a
1719 * directory change.
1720 */
1721 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001724 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001725 if (directory)
1726 ptr = concat_fnames(directory, fname, TRUE);
1727 else
1728 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001730 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001731 bufname = ptr;
1732 }
1733 else
1734 bufname = fname;
1735
1736 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001737 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001738 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001739 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001740 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001742 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001743 {
1744 vim_free(qf_last_bufname);
1745 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1746 if (bufname == ptr)
1747 qf_last_bufname = bufname;
1748 else
1749 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001750 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001751 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001752 if (buf == NULL)
1753 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001754
Bram Moolenaarc1542742016-07-20 21:44:37 +02001755 buf->b_has_qf_entry =
1756 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001757 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758}
1759
1760/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001761 * Push dirbuf onto the directory stack and return pointer to actual dir or
1762 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 */
1764 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001765qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 struct dir_stack_T *ds_new;
1768 struct dir_stack_T *ds_ptr;
1769
1770 /* allocate new stack element and hook it in */
1771 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1772 if (ds_new == NULL)
1773 return NULL;
1774
1775 ds_new->next = *stackptr;
1776 *stackptr = ds_new;
1777
1778 /* store directory on the stack */
1779 if (vim_isAbsName(dirbuf)
1780 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001781 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 (*stackptr)->dirname = vim_strsave(dirbuf);
1783 else
1784 {
1785 /* Okay we don't have an absolute path.
1786 * dirbuf must be a subdir of one of the directories on the stack.
1787 * Let's search...
1788 */
1789 ds_new = (*stackptr)->next;
1790 (*stackptr)->dirname = NULL;
1791 while (ds_new)
1792 {
1793 vim_free((*stackptr)->dirname);
1794 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1795 TRUE);
1796 if (mch_isdir((*stackptr)->dirname) == TRUE)
1797 break;
1798
1799 ds_new = ds_new->next;
1800 }
1801
1802 /* clean up all dirs we already left */
1803 while ((*stackptr)->next != ds_new)
1804 {
1805 ds_ptr = (*stackptr)->next;
1806 (*stackptr)->next = (*stackptr)->next->next;
1807 vim_free(ds_ptr->dirname);
1808 vim_free(ds_ptr);
1809 }
1810
1811 /* Nothing found -> it must be on top level */
1812 if (ds_new == NULL)
1813 {
1814 vim_free((*stackptr)->dirname);
1815 (*stackptr)->dirname = vim_strsave(dirbuf);
1816 }
1817 }
1818
1819 if ((*stackptr)->dirname != NULL)
1820 return (*stackptr)->dirname;
1821 else
1822 {
1823 ds_ptr = *stackptr;
1824 *stackptr = (*stackptr)->next;
1825 vim_free(ds_ptr);
1826 return NULL;
1827 }
1828}
1829
1830
1831/*
1832 * pop dirbuf from the directory stack and return previous directory or NULL if
1833 * stack is empty
1834 */
1835 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001836qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 struct dir_stack_T *ds_ptr;
1839
1840 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1841 * What to do if it isn't? */
1842
1843 /* pop top element and free it */
1844 if (*stackptr != NULL)
1845 {
1846 ds_ptr = *stackptr;
1847 *stackptr = (*stackptr)->next;
1848 vim_free(ds_ptr->dirname);
1849 vim_free(ds_ptr);
1850 }
1851
1852 /* return NEW top element as current dir or NULL if stack is empty*/
1853 return *stackptr ? (*stackptr)->dirname : NULL;
1854}
1855
1856/*
1857 * clean up directory stack
1858 */
1859 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001860qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
1862 struct dir_stack_T *ds_ptr;
1863
1864 while ((ds_ptr = *stackptr) != NULL)
1865 {
1866 *stackptr = (*stackptr)->next;
1867 vim_free(ds_ptr->dirname);
1868 vim_free(ds_ptr);
1869 }
1870}
1871
1872/*
1873 * Check in which directory of the directory stack the given file can be
1874 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001875 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 * Cleans up intermediate directory entries.
1877 *
1878 * TODO: How to solve the following problem?
1879 * If we have the this directory tree:
1880 * ./
1881 * ./aa
1882 * ./aa/bb
1883 * ./bb
1884 * ./bb/x.c
1885 * and make says:
1886 * making all in aa
1887 * making all in bb
1888 * x.c:9: Error
1889 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1890 * qf_guess_filepath will return NULL.
1891 */
1892 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001893qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 struct dir_stack_T *ds_ptr;
1896 struct dir_stack_T *ds_tmp;
1897 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001898 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001901 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 return NULL;
1903
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001904 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 fullname = NULL;
1906 while (ds_ptr)
1907 {
1908 vim_free(fullname);
1909 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1910
1911 /* If concat_fnames failed, just go on. The worst thing that can happen
1912 * is that we delete the entire stack.
1913 */
1914 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1915 break;
1916
1917 ds_ptr = ds_ptr->next;
1918 }
1919
1920 vim_free(fullname);
1921
1922 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001923 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001925 ds_tmp = qfl->qf_dir_stack->next;
1926 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 vim_free(ds_tmp->dirname);
1928 vim_free(ds_tmp);
1929 }
1930
1931 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932}
1933
1934/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01001935 * Returns TRUE if a quickfix/location list with the given identifier exists.
1936 */
1937 static int
1938qflist_valid (win_T *wp, int_u qf_id)
1939{
1940 qf_info_T *qi = &ql_info;
1941 int i;
1942
1943 if (wp != NULL)
1944 {
1945 qi = GET_LOC_LIST(wp); /* Location list */
1946 if (qi == NULL)
1947 return FALSE;
1948 }
1949
1950 for (i = 0; i < qi->qf_listcount; ++i)
1951 if (qi->qf_lists[i].qf_id == qf_id)
1952 return TRUE;
1953
1954 return FALSE;
1955}
1956
1957/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001958 * When loading a file from the quickfix, the auto commands may modify it.
1959 * This may invalidate the current quickfix entry. This function checks
1960 * whether a entry is still present in the quickfix.
1961 * Similar to location list.
1962 */
1963 static int
1964is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1965{
1966 qf_list_T *qfl;
1967 qfline_T *qfp;
1968 int i;
1969
1970 qfl = &qi->qf_lists[qi->qf_curlist];
1971
1972 /* Search for the entry in the current list */
1973 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1974 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001975 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001976 break;
1977
1978 if (i == qfl->qf_count) /* Entry is not found */
1979 return FALSE;
1980
1981 return TRUE;
1982}
1983
1984/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001985 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001986 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001987 */
1988 static qfline_T *
1989get_next_valid_entry(
1990 qf_info_T *qi,
1991 qfline_T *qf_ptr,
1992 int *qf_index,
1993 int dir)
1994{
1995 int idx;
1996 int old_qf_fnum;
1997
1998 idx = *qf_index;
1999 old_qf_fnum = qf_ptr->qf_fnum;
2000
2001 do
2002 {
2003 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2004 || qf_ptr->qf_next == NULL)
2005 return NULL;
2006 ++idx;
2007 qf_ptr = qf_ptr->qf_next;
2008 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2009 && !qf_ptr->qf_valid)
2010 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2011
2012 *qf_index = idx;
2013 return qf_ptr;
2014}
2015
2016/*
2017 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002018 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002019 */
2020 static qfline_T *
2021get_prev_valid_entry(
2022 qf_info_T *qi,
2023 qfline_T *qf_ptr,
2024 int *qf_index,
2025 int dir)
2026{
2027 int idx;
2028 int old_qf_fnum;
2029
2030 idx = *qf_index;
2031 old_qf_fnum = qf_ptr->qf_fnum;
2032
2033 do
2034 {
2035 if (idx == 1 || qf_ptr->qf_prev == NULL)
2036 return NULL;
2037 --idx;
2038 qf_ptr = qf_ptr->qf_prev;
2039 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2040 && !qf_ptr->qf_valid)
2041 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2042
2043 *qf_index = idx;
2044 return qf_ptr;
2045}
2046
2047/*
2048 * Get the n'th (errornr) previous/next valid entry from the current entry in
2049 * the quickfix list.
2050 * dir == FORWARD or FORWARD_FILE: next valid entry
2051 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2052 */
2053 static qfline_T *
2054get_nth_valid_entry(
2055 qf_info_T *qi,
2056 int errornr,
2057 qfline_T *qf_ptr,
2058 int *qf_index,
2059 int dir)
2060{
2061 qfline_T *prev_qf_ptr;
2062 int prev_index;
2063 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2064 char_u *err = e_no_more_items;
2065
2066 while (errornr--)
2067 {
2068 prev_qf_ptr = qf_ptr;
2069 prev_index = *qf_index;
2070
2071 if (dir == FORWARD || dir == FORWARD_FILE)
2072 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2073 else
2074 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2075 if (qf_ptr == NULL)
2076 {
2077 qf_ptr = prev_qf_ptr;
2078 *qf_index = prev_index;
2079 if (err != NULL)
2080 {
2081 EMSG(_(err));
2082 return NULL;
2083 }
2084 break;
2085 }
2086
2087 err = NULL;
2088 }
2089
2090 return qf_ptr;
2091}
2092
2093/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002094 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002095 */
2096 static qfline_T *
2097get_nth_entry(
2098 qf_info_T *qi,
2099 int errornr,
2100 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002101 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002102{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002103 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002104
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002105 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002106 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2107 {
2108 --qf_idx;
2109 qf_ptr = qf_ptr->qf_prev;
2110 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002111 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002112 while (errornr > qf_idx &&
2113 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2114 qf_ptr->qf_next != NULL)
2115 {
2116 ++qf_idx;
2117 qf_ptr = qf_ptr->qf_next;
2118 }
2119
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002120 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002121 return qf_ptr;
2122}
2123
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002124/*
2125 * Find a help window or open one.
2126 */
2127 static int
2128jump_to_help_window(qf_info_T *qi, int *opened_window)
2129{
2130 win_T *wp;
2131 int flags;
2132
2133 if (cmdmod.tab != 0)
2134 wp = NULL;
2135 else
2136 FOR_ALL_WINDOWS(wp)
2137 if (bt_help(wp->w_buffer))
2138 break;
2139 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2140 win_enter(wp, TRUE);
2141 else
2142 {
2143 /*
2144 * Split off help window; put it at far top if no position
2145 * specified, the current window is vertically split and narrow.
2146 */
2147 flags = WSP_HELP;
2148 if (cmdmod.split == 0 && curwin->w_width != Columns
2149 && curwin->w_width < 80)
2150 flags |= WSP_TOP;
2151 if (qi != &ql_info)
2152 flags |= WSP_NEWLOC; /* don't copy the location list */
2153
2154 if (win_split(0, flags) == FAIL)
2155 return FAIL;
2156
2157 *opened_window = TRUE;
2158
2159 if (curwin->w_height < p_hh)
2160 win_setheight((int)p_hh);
2161
2162 if (qi != &ql_info) /* not a quickfix list */
2163 {
2164 /* The new window should use the supplied location list */
2165 curwin->w_llist = qi;
2166 qi->qf_refcount++;
2167 }
2168 }
2169
2170 if (!p_im)
2171 restart_edit = 0; /* don't want insert mode in help file */
2172
2173 return OK;
2174}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002175
2176/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002177 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2178 * If the file is already opened in a window, jump to it.
2179 */
2180 static int
2181qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2182{
2183 win_T *usable_win_ptr = NULL;
2184 int usable_win;
2185 qf_info_T *ll_ref;
2186 int flags;
2187 win_T *win;
2188 win_T *altwin;
2189
2190 usable_win = 0;
2191
2192 ll_ref = curwin->w_llist_ref;
2193 if (ll_ref != NULL)
2194 {
2195 /* Find a window using the same location list that is not a
2196 * quickfix window. */
2197 FOR_ALL_WINDOWS(usable_win_ptr)
2198 if (usable_win_ptr->w_llist == ll_ref
2199 && !bt_quickfix(usable_win_ptr->w_buffer))
2200 {
2201 usable_win = 1;
2202 break;
2203 }
2204 }
2205
2206 if (!usable_win)
2207 {
2208 /* Locate a window showing a normal buffer */
2209 FOR_ALL_WINDOWS(win)
2210 if (win->w_buffer->b_p_bt[0] == NUL)
2211 {
2212 usable_win = 1;
2213 break;
2214 }
2215 }
2216
2217 /*
2218 * If no usable window is found and 'switchbuf' contains "usetab"
2219 * then search in other tabs.
2220 */
2221 if (!usable_win && (swb_flags & SWB_USETAB))
2222 {
2223 tabpage_T *tp;
2224 win_T *wp;
2225
2226 FOR_ALL_TAB_WINDOWS(tp, wp)
2227 {
2228 if (wp->w_buffer->b_fnum == qf_fnum)
2229 {
2230 goto_tabpage_win(tp, wp);
2231 usable_win = 1;
2232 goto win_found;
2233 }
2234 }
2235 }
2236win_found:
2237
2238 /*
2239 * If there is only one window and it is the quickfix window, create a
2240 * new one above the quickfix window.
2241 */
2242 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2243 {
2244 flags = WSP_ABOVE;
2245 if (ll_ref != NULL)
2246 flags |= WSP_NEWLOC;
2247 if (win_split(0, flags) == FAIL)
2248 return FAIL; /* not enough room for window */
2249 *opened_window = TRUE; /* close it when fail */
2250 p_swb = empty_option; /* don't split again */
2251 swb_flags = 0;
2252 RESET_BINDING(curwin);
2253 if (ll_ref != NULL)
2254 {
2255 /* The new window should use the location list from the
2256 * location list window */
2257 curwin->w_llist = ll_ref;
2258 ll_ref->qf_refcount++;
2259 }
2260 }
2261 else
2262 {
2263 if (curwin->w_llist_ref != NULL)
2264 {
2265 /* In a location window */
2266 win = usable_win_ptr;
2267 if (win == NULL)
2268 {
2269 /* Find the window showing the selected file */
2270 FOR_ALL_WINDOWS(win)
2271 if (win->w_buffer->b_fnum == qf_fnum)
2272 break;
2273 if (win == NULL)
2274 {
2275 /* Find a previous usable window */
2276 win = curwin;
2277 do
2278 {
2279 if (win->w_buffer->b_p_bt[0] == NUL)
2280 break;
2281 if (win->w_prev == NULL)
2282 win = lastwin; /* wrap around the top */
2283 else
2284 win = win->w_prev; /* go to previous window */
2285 } while (win != curwin);
2286 }
2287 }
2288 win_goto(win);
2289
2290 /* If the location list for the window is not set, then set it
2291 * to the location list from the location window */
2292 if (win->w_llist == NULL)
2293 {
2294 win->w_llist = ll_ref;
2295 ll_ref->qf_refcount++;
2296 }
2297 }
2298 else
2299 {
2300
2301 /*
2302 * Try to find a window that shows the right buffer.
2303 * Default to the window just above the quickfix buffer.
2304 */
2305 win = curwin;
2306 altwin = NULL;
2307 for (;;)
2308 {
2309 if (win->w_buffer->b_fnum == qf_fnum)
2310 break;
2311 if (win->w_prev == NULL)
2312 win = lastwin; /* wrap around the top */
2313 else
2314 win = win->w_prev; /* go to previous window */
2315
2316 if (IS_QF_WINDOW(win))
2317 {
2318 /* Didn't find it, go to the window before the quickfix
2319 * window. */
2320 if (altwin != NULL)
2321 win = altwin;
2322 else if (curwin->w_prev != NULL)
2323 win = curwin->w_prev;
2324 else
2325 win = curwin->w_next;
2326 break;
2327 }
2328
2329 /* Remember a usable window. */
2330 if (altwin == NULL && !win->w_p_pvw
2331 && win->w_buffer->b_p_bt[0] == NUL)
2332 altwin = win;
2333 }
2334
2335 win_goto(win);
2336 }
2337 }
2338
2339 return OK;
2340}
2341
2342/*
2343 * Edit the selected file or help file.
2344 */
2345 static int
2346qf_jump_edit_buffer(
2347 qf_info_T *qi,
2348 qfline_T *qf_ptr,
2349 int forceit,
2350 win_T *oldwin,
2351 int *opened_window,
2352 int *abort)
2353{
2354 int retval = OK;
2355
2356 if (qf_ptr->qf_type == 1)
2357 {
2358 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2359 * set b_p_ro flag). */
2360 if (!can_abandon(curbuf, forceit))
2361 {
2362 no_write_message();
2363 retval = FALSE;
2364 }
2365 else
2366 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2367 ECMD_HIDE + ECMD_SET_HELP,
2368 oldwin == curwin ? curwin : NULL);
2369 }
2370 else
2371 {
2372 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002373 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002374
2375 retval = buflist_getfile(qf_ptr->qf_fnum,
2376 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002377
2378 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002379 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002380 /*
2381 * Location list. Check whether the associated window is still
2382 * present and the list is still valid.
2383 */
2384 if (!win_valid_any_tab(oldwin))
2385 {
2386 EMSG(_("E924: Current window was closed"));
2387 *abort = TRUE;
2388 *opened_window = FALSE;
2389 }
2390 else if (!qflist_valid(oldwin, save_qfid))
2391 {
2392 EMSG(_(e_loc_list_changed));
2393 *abort = TRUE;
2394 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002395 }
2396 else if (old_qf_curlist != qi->qf_curlist
2397 || !is_qf_entry_present(qi, qf_ptr))
2398 {
2399 if (qi == &ql_info)
2400 EMSG(_("E925: Current quickfix was changed"));
2401 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002402 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002403 *abort = TRUE;
2404 }
2405
2406 if (*abort)
2407 retval = FALSE;
2408 }
2409
2410 return retval;
2411}
2412
2413/*
2414 * Goto the error line in the current file using either line/column number or a
2415 * search pattern.
2416 */
2417 static void
2418qf_jump_goto_line(
2419 linenr_T qf_lnum,
2420 int qf_col,
2421 char_u qf_viscol,
2422 char_u *qf_pattern)
2423{
2424 linenr_T i;
2425 char_u *line;
2426 colnr_T screen_col;
2427 colnr_T char_col;
2428
2429 if (qf_pattern == NULL)
2430 {
2431 /*
2432 * Go to line with error, unless qf_lnum is 0.
2433 */
2434 i = qf_lnum;
2435 if (i > 0)
2436 {
2437 if (i > curbuf->b_ml.ml_line_count)
2438 i = curbuf->b_ml.ml_line_count;
2439 curwin->w_cursor.lnum = i;
2440 }
2441 if (qf_col > 0)
2442 {
2443 curwin->w_cursor.col = qf_col - 1;
2444#ifdef FEAT_VIRTUALEDIT
2445 curwin->w_cursor.coladd = 0;
2446#endif
2447 if (qf_viscol == TRUE)
2448 {
2449 /*
2450 * Check each character from the beginning of the error
2451 * line up to the error column. For each tab character
2452 * found, reduce the error column value by the length of
2453 * a tab character.
2454 */
2455 line = ml_get_curline();
2456 screen_col = 0;
2457 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2458 {
2459 if (*line == NUL)
2460 break;
2461 if (*line++ == '\t')
2462 {
2463 curwin->w_cursor.col -= 7 - (screen_col % 8);
2464 screen_col += 8 - (screen_col % 8);
2465 }
2466 else
2467 ++screen_col;
2468 }
2469 }
2470 check_cursor();
2471 }
2472 else
2473 beginline(BL_WHITE | BL_FIX);
2474 }
2475 else
2476 {
2477 pos_T save_cursor;
2478
2479 /* Move the cursor to the first line in the buffer */
2480 save_cursor = curwin->w_cursor;
2481 curwin->w_cursor.lnum = 0;
2482 if (!do_search(NULL, '/', qf_pattern, (long)1,
2483 SEARCH_KEEP, NULL, NULL))
2484 curwin->w_cursor = save_cursor;
2485 }
2486}
2487
2488/*
2489 * Display quickfix list index and size message
2490 */
2491 static void
2492qf_jump_print_msg(
2493 qf_info_T *qi,
2494 int qf_index,
2495 qfline_T *qf_ptr,
2496 buf_T *old_curbuf,
2497 linenr_T old_lnum)
2498{
2499 linenr_T i;
2500 int len;
2501
2502 /* Update the screen before showing the message, unless the screen
2503 * scrolled up. */
2504 if (!msg_scrolled)
2505 update_topline_redraw();
2506 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2507 qi->qf_lists[qi->qf_curlist].qf_count,
2508 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2509 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2510 /* Add the message, skipping leading whitespace and newlines. */
2511 len = (int)STRLEN(IObuff);
2512 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2513
2514 /* Output the message. Overwrite to avoid scrolling when the 'O'
2515 * flag is present in 'shortmess'; But when not jumping, print the
2516 * whole message. */
2517 i = msg_scroll;
2518 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2519 msg_scroll = TRUE;
2520 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2521 msg_scroll = FALSE;
2522 msg_attr_keep(IObuff, 0, TRUE);
2523 msg_scroll = i;
2524}
2525
2526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 * jump to a quickfix line
2528 * if dir == FORWARD go "errornr" valid entries forward
2529 * if dir == BACKWARD go "errornr" valid entries backward
2530 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2531 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2532 * else if "errornr" is zero, redisplay the same line
2533 * else go to entry "errornr"
2534 */
2535 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002536qf_jump(qf_info_T *qi,
2537 int dir,
2538 int errornr,
2539 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002541 qfline_T *qf_ptr;
2542 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 buf_T *old_curbuf;
2546 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002547 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002548 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002550 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552#ifdef FEAT_FOLDING
2553 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2554#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002555 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002557 if (qi == NULL)
2558 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002559
2560 if (qi->qf_curlist >= qi->qf_listcount
2561 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 EMSG(_(e_quickfix));
2564 return;
2565 }
2566
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002567 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002569 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002571 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002573 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2574 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002576 qf_ptr = old_qf_ptr;
2577 qf_index = old_qf_index;
2578 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580 }
2581 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002582 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002584 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2585 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 /* No need to print the error message if it's visible in the error
2587 * window */
2588 print_message = FALSE;
2589
2590 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002591 * For ":helpgrep" find a help window or open one.
2592 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002593 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002594 if (jump_to_help_window(qi, &opened_window) == FAIL)
2595 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002596
2597 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 * If currently in the quickfix window, find another window to show the
2599 * file in.
2600 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002601 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
2603 /*
2604 * If there is no file specified, we don't know where to go.
2605 * But do advance, otherwise ":cn" gets stuck.
2606 */
2607 if (qf_ptr->qf_fnum == 0)
2608 goto theend;
2609
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002610 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2611 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
2614 /*
2615 * If there is a file name,
2616 * read the wanted file if needed, and check autowrite etc.
2617 */
2618 old_curbuf = curbuf;
2619 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002620
2621 if (qf_ptr->qf_fnum != 0)
2622 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002623 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002624
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002625 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2626 &opened_window, &abort);
2627 if (abort)
2628 {
2629 qi = NULL;
2630 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002631 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002632 }
2633
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002634 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 /* When not switched to another buffer, still need to set pc mark */
2637 if (curbuf == old_curbuf)
2638 setpcmark();
2639
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002640 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2641 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
2643#ifdef FEAT_FOLDING
2644 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2645 foldOpenCursor();
2646#endif
2647 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002648 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 else
2651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (opened_window)
2653 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002654 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
2656 /*
2657 * Couldn't open file, so put index back where it was. This could
2658 * happen if the file was readonly and we changed something.
2659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 qf_ptr = old_qf_ptr;
2662 qf_index = old_qf_index;
2663 }
2664 }
2665theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002666 if (qi != NULL)
2667 {
2668 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2669 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (p_swb != old_swb && opened_window)
2672 {
2673 /* Restore old 'switchbuf' value, but not when an autocommand or
2674 * modeline has changed the value. */
2675 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002676 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002678 swb_flags = old_swb_flags;
2679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 else
2681 free_string_option(old_swb);
2682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683}
2684
2685/*
2686 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002687 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
2689 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002690qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002692 buf_T *buf;
2693 char_u *fname;
2694 qfline_T *qfp;
2695 int i;
2696 int idx1 = 1;
2697 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002698 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002699 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002700 int qfFileAttr;
2701 int qfSepAttr;
2702 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002703 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002705 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002707 if (eap->cmdidx == CMD_llist)
2708 {
2709 qi = GET_LOC_LIST(curwin);
2710 if (qi == NULL)
2711 {
2712 EMSG(_(e_loclist));
2713 return;
2714 }
2715 }
2716
2717 if (qi->qf_curlist >= qi->qf_listcount
2718 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 EMSG(_(e_quickfix));
2721 return;
2722 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002723 if (*arg == '+')
2724 {
2725 ++arg;
2726 plus = TRUE;
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2729 {
2730 EMSG(_(e_trailing));
2731 return;
2732 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002733 if (plus)
2734 {
2735 i = qi->qf_lists[qi->qf_curlist].qf_index;
2736 idx2 = i + idx1;
2737 idx1 = i;
2738 }
2739 else
2740 {
2741 i = qi->qf_lists[qi->qf_curlist].qf_count;
2742 if (idx1 < 0)
2743 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2744 if (idx2 < 0)
2745 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002748 /*
2749 * Get the attributes for the different quickfix highlight items. Note
2750 * that this depends on syntax items defined in the qf.vim syntax file
2751 */
2752 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2753 if (qfFileAttr == 0)
2754 qfFileAttr = HL_ATTR(HLF_D);
2755 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2756 if (qfSepAttr == 0)
2757 qfSepAttr = HL_ATTR(HLF_D);
2758 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2759 if (qfLineAttr == 0)
2760 qfLineAttr = HL_ATTR(HLF_N);
2761
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002762 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002764 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2765 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
2767 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2768 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002769 msg_putchar('\n');
2770 if (got_int)
2771 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002772
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002773 fname = NULL;
2774 if (qfp->qf_fnum != 0
2775 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2776 {
2777 fname = buf->b_fname;
2778 if (qfp->qf_type == 1) /* :helpgrep */
2779 fname = gettail(fname);
2780 }
2781 if (fname == NULL)
2782 sprintf((char *)IObuff, "%2d", i);
2783 else
2784 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2785 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002786 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002787 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2788
2789 if (qfp->qf_lnum != 0)
2790 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002791 if (qfp->qf_lnum == 0)
2792 IObuff[0] = NUL;
2793 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002794 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002796 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002797 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002798 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002800 msg_puts_attr(IObuff, qfLineAttr);
2801 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002802 if (qfp->qf_pattern != NULL)
2803 {
2804 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002805 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002806 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002807 }
2808 msg_puts((char_u *)" ");
2809
2810 /* Remove newlines and leading whitespace from the text. For an
2811 * unrecognized line keep the indent, the compiler may mark a word
2812 * with ^^^^. */
2813 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2815 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816 msg_prt_line(IObuff, FALSE);
2817 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002819
2820 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002821 if (qfp == NULL)
2822 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002823 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 ui_breakcheck();
2825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
2827
2828/*
2829 * Remove newlines and leading whitespace from an error message.
2830 * Put the result in "buf[bufsize]".
2831 */
2832 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002833qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 int i;
2836 char_u *p = text;
2837
2838 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2839 {
2840 if (*p == '\n')
2841 {
2842 buf[i] = ' ';
2843 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002844 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 break;
2846 }
2847 else
2848 buf[i] = *p++;
2849 }
2850 buf[i] = NUL;
2851}
2852
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002853 static void
2854qf_msg(qf_info_T *qi, int which, char *lead)
2855{
2856 char *title = (char *)qi->qf_lists[which].qf_title;
2857 int count = qi->qf_lists[which].qf_count;
2858 char_u buf[IOSIZE];
2859
2860 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2861 lead,
2862 which + 1,
2863 qi->qf_listcount,
2864 count);
2865
2866 if (title != NULL)
2867 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002868 size_t len = STRLEN(buf);
2869
2870 if (len < 34)
2871 {
2872 vim_memset(buf + len, ' ', 34 - len);
2873 buf[34] = NUL;
2874 }
2875 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002876 }
2877 trunc_string(buf, buf, Columns - 1, IOSIZE);
2878 msg(buf);
2879}
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881/*
2882 * ":colder [count]": Up in the quickfix stack.
2883 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002884 * ":lolder [count]": Up in the location list stack.
2885 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 */
2887 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002888qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 int count;
2892
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002893 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2894 {
2895 qi = GET_LOC_LIST(curwin);
2896 if (qi == NULL)
2897 {
2898 EMSG(_(e_loclist));
2899 return;
2900 }
2901 }
2902
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 if (eap->addr_count != 0)
2904 count = eap->line2;
2905 else
2906 count = 1;
2907 while (count--)
2908 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002909 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
2913 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002914 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002916 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 else
2919 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002920 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002923 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002925 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002928 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002929 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930}
2931
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002932 void
2933qf_history(exarg_T *eap)
2934{
2935 qf_info_T *qi = &ql_info;
2936 int i;
2937
2938 if (eap->cmdidx == CMD_lhistory)
2939 qi = GET_LOC_LIST(curwin);
2940 if (qi == NULL || (qi->qf_listcount == 0
2941 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2942 MSG(_("No entries"));
2943 else
2944 for (i = 0; i < qi->qf_listcount; ++i)
2945 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2946}
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002949 * Free all the entries in the error list "idx". Note that other information
2950 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 */
2952 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002953qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002955 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002956 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002957 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002958 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002960 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002962 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002963 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002964 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002965 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002966 vim_free(qfp->qf_text);
2967 stop = (qfp == qfpnext);
2968 vim_free(qfp->qf_pattern);
2969 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002970 if (stop)
2971 /* Somehow qf_count may have an incorrect value, set it to 1
2972 * to avoid crashing when it's wrong.
2973 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002974 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002975 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002976 qfl->qf_start = qfpnext;
2977 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002979
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002980 qfl->qf_index = 0;
2981 qfl->qf_start = NULL;
2982 qfl->qf_last = NULL;
2983 qfl->qf_ptr = NULL;
2984 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002985
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002986 qf_clean_dir_stack(&qfl->qf_dir_stack);
2987 qfl->qf_directory = NULL;
2988 qf_clean_dir_stack(&qfl->qf_file_stack);
2989 qfl->qf_currfile = NULL;
2990 qfl->qf_multiline = FALSE;
2991 qfl->qf_multiignore = FALSE;
2992 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993}
2994
2995/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002996 * Free error list "idx". Frees all the entries in the quickfix list,
2997 * associated context information and the title.
2998 */
2999 static void
3000qf_free(qf_info_T *qi, int idx)
3001{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003002 qf_list_T *qfl = &qi->qf_lists[idx];
3003
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003004 qf_free_items(qi, idx);
3005
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003006 vim_free(qfl->qf_title);
3007 qfl->qf_title = NULL;
3008 free_tv(qfl->qf_ctx);
3009 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003010 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003011 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003012}
3013
3014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 * qf_mark_adjust: adjust marks
3016 */
3017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003018qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003019 win_T *wp,
3020 linenr_T line1,
3021 linenr_T line2,
3022 long amount,
3023 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003025 int i;
3026 qfline_T *qfp;
3027 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003028 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003029 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003030 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
Bram Moolenaarc1542742016-07-20 21:44:37 +02003032 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003033 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003034 if (wp != NULL)
3035 {
3036 if (wp->w_llist == NULL)
3037 return;
3038 qi = wp->w_llist;
3039 }
3040
3041 for (idx = 0; idx < qi->qf_listcount; ++idx)
3042 if (qi->qf_lists[idx].qf_count)
3043 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003044 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3045 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 if (qfp->qf_fnum == curbuf->b_fnum)
3047 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003048 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3050 {
3051 if (amount == MAXLNUM)
3052 qfp->qf_cleared = TRUE;
3053 else
3054 qfp->qf_lnum += amount;
3055 }
3056 else if (amount_after && qfp->qf_lnum > line2)
3057 qfp->qf_lnum += amount_after;
3058 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003059
3060 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003061 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062}
3063
3064/*
3065 * Make a nice message out of the error character and the error number:
3066 * char number message
3067 * e or E 0 " error"
3068 * w or W 0 " warning"
3069 * i or I 0 " info"
3070 * 0 0 ""
3071 * other 0 " c"
3072 * e or E n " error n"
3073 * w or W n " warning n"
3074 * i or I n " info n"
3075 * 0 n " error n"
3076 * other n " c n"
3077 * 1 x "" :helpgrep
3078 */
3079 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003080qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 static char_u buf[20];
3083 static char_u cc[3];
3084 char_u *p;
3085
3086 if (c == 'W' || c == 'w')
3087 p = (char_u *)" warning";
3088 else if (c == 'I' || c == 'i')
3089 p = (char_u *)" info";
3090 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3091 p = (char_u *)" error";
3092 else if (c == 0 || c == 1)
3093 p = (char_u *)"";
3094 else
3095 {
3096 cc[0] = ' ';
3097 cc[1] = c;
3098 cc[2] = NUL;
3099 p = cc;
3100 }
3101
3102 if (nr <= 0)
3103 return p;
3104
3105 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3106 return buf;
3107}
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109/*
3110 * ":cwindow": open the quickfix window if we have errors to display,
3111 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003112 * ":lwindow": open the location list window if we have locations to display,
3113 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 */
3115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003116ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003118 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 win_T *win;
3120
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003121 if (eap->cmdidx == CMD_lwindow)
3122 {
3123 qi = GET_LOC_LIST(curwin);
3124 if (qi == NULL)
3125 return;
3126 }
3127
3128 /* Look for an existing quickfix window. */
3129 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
3131 /*
3132 * If a quickfix window is open but we have no errors to display,
3133 * close the window. If a quickfix window is not open, then open
3134 * it if we have errors; otherwise, leave it closed.
3135 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003136 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003137 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003138 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 if (win != NULL)
3141 ex_cclose(eap);
3142 }
3143 else if (win == NULL)
3144 ex_copen(eap);
3145}
3146
3147/*
3148 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003149 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003152ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003154 win_T *win = NULL;
3155 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003157 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3158 {
3159 qi = GET_LOC_LIST(curwin);
3160 if (qi == NULL)
3161 return;
3162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003164 /* Find existing quickfix window and close it. */
3165 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (win != NULL)
3167 win_close(win, FALSE);
3168}
3169
3170/*
3171 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003172 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 */
3174 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003175ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003177 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003180 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003181 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003182 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003184 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3185 {
3186 qi = GET_LOC_LIST(curwin);
3187 if (qi == NULL)
3188 {
3189 EMSG(_(e_loclist));
3190 return;
3191 }
3192 }
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (eap->addr_count != 0)
3195 height = eap->line2;
3196 else
3197 height = QF_WINHEIGHT;
3198
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200#ifdef FEAT_GUI
3201 need_mouse_correct = TRUE;
3202#endif
3203
3204 /*
3205 * Find existing quickfix window, or open a new one.
3206 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003207 win = qf_find_win(qi);
3208
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003209 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003212 if (eap->addr_count != 0)
3213 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003214 if (cmdmod.split & WSP_VERT)
3215 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003216 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003217 win_setwidth(height);
3218 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003219 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003220 win_setheight(height);
3221 }
3222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 else
3224 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003225 int flags = 0;
3226
Bram Moolenaar9c102382006-05-03 21:26:49 +00003227 qf_buf = qf_find_buf(qi);
3228
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 /* The current window becomes the previous window afterwards. */
3230 win = curwin;
3231
Bram Moolenaar77642c02012-11-20 17:55:10 +01003232 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3233 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003234 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003235 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003236 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003237 /* Default is to open the window below the current window */
3238 if (cmdmod.split == 0)
3239 flags = WSP_BELOW;
3240 flags |= WSP_NEWLOC;
3241 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003243 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003245 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003247 /*
3248 * For the location list window, create a reference to the
3249 * location list from the window 'win'.
3250 */
3251 curwin->w_llist_ref = win->w_llist;
3252 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003254
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003255 if (oldwin != curwin)
3256 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003257 if (qf_buf != NULL)
3258 /* Use the existing quickfix buffer */
3259 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003260 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003261 else
3262 {
3263 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003264 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003265 /* switch off 'swapfile' */
3266 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3267 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003268 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003269 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003270 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003271#ifdef FEAT_DIFF
3272 curwin->w_p_diff = FALSE;
3273#endif
3274#ifdef FEAT_FOLDING
3275 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3276 OPT_LOCAL);
3277#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003280 /* Only set the height when still in the same tab page and there is no
3281 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003282 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 win_setheight(height);
3284 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3285 if (win_valid(win))
3286 prevwin = win;
3287 }
3288
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003289 qf_set_title_var(qi);
3290
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 /*
3292 * Fill the buffer with the quickfix list.
3293 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003294 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003296 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 curwin->w_cursor.col = 0;
3298 check_cursor();
3299 update_topline(); /* scroll to show the line */
3300}
3301
3302/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003303 * Move the cursor in the quickfix window to "lnum".
3304 */
3305 static void
3306qf_win_goto(win_T *win, linenr_T lnum)
3307{
3308 win_T *old_curwin = curwin;
3309
3310 curwin = win;
3311 curbuf = win->w_buffer;
3312 curwin->w_cursor.lnum = lnum;
3313 curwin->w_cursor.col = 0;
3314#ifdef FEAT_VIRTUALEDIT
3315 curwin->w_cursor.coladd = 0;
3316#endif
3317 curwin->w_curswant = 0;
3318 update_topline(); /* scroll to show the line */
3319 redraw_later(VALID);
3320 curwin->w_redr_status = TRUE; /* update ruler */
3321 curwin = old_curwin;
3322 curbuf = curwin->w_buffer;
3323}
3324
3325/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003326 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003327 */
3328 void
3329ex_cbottom(exarg_T *eap UNUSED)
3330{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003331 qf_info_T *qi = &ql_info;
3332 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003333
Bram Moolenaar537ef082016-07-09 17:56:19 +02003334 if (eap->cmdidx == CMD_lbottom)
3335 {
3336 qi = GET_LOC_LIST(curwin);
3337 if (qi == NULL)
3338 {
3339 EMSG(_(e_loclist));
3340 return;
3341 }
3342 }
3343
3344 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003345 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3346 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3347}
3348
3349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 * Return the number of the current entry (line number in the quickfix
3351 * window).
3352 */
3353 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003354qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003356 qf_info_T *qi = &ql_info;
3357
3358 if (IS_LL_WINDOW(wp))
3359 /* In the location list window, use the referenced location list */
3360 qi = wp->w_llist_ref;
3361
3362 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363}
3364
3365/*
3366 * Update the cursor position in the quickfix window to the current error.
3367 * Return TRUE if there is a quickfix window.
3368 */
3369 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003370qf_win_pos_update(
3371 qf_info_T *qi,
3372 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003375 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
3377 /*
3378 * Put the cursor on the current error in the quickfix window, so that
3379 * it's viewable.
3380 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003381 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (win != NULL
3383 && qf_index <= win->w_buffer->b_ml.ml_line_count
3384 && old_qf_index != qf_index)
3385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (qf_index > old_qf_index)
3387 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003388 win->w_redraw_top = old_qf_index;
3389 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
3391 else
3392 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003393 win->w_redraw_top = qf_index;
3394 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003396 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 return win != NULL;
3399}
3400
3401/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003402 * Check whether the given window is displaying the specified quickfix/location
3403 * list buffer
3404 */
3405 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003406is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003407{
3408 /*
3409 * A window displaying the quickfix buffer will have the w_llist_ref field
3410 * set to NULL.
3411 * A window displaying a location list buffer will have the w_llist_ref
3412 * pointing to the location list.
3413 */
3414 if (bt_quickfix(win->w_buffer))
3415 if ((qi == &ql_info && win->w_llist_ref == NULL)
3416 || (qi != &ql_info && win->w_llist_ref == qi))
3417 return TRUE;
3418
3419 return FALSE;
3420}
3421
3422/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003423 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003424 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003425 */
3426 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003427qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003428{
3429 win_T *win;
3430
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003431 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003432 if (is_qf_win(win, qi))
3433 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003434
3435 return win;
3436}
3437
3438/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003439 * Find a quickfix buffer.
3440 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 */
3442 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003443qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003445 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003446 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
Bram Moolenaar9c102382006-05-03 21:26:49 +00003448 FOR_ALL_TAB_WINDOWS(tp, win)
3449 if (is_qf_win(win, qi))
3450 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003451
Bram Moolenaar9c102382006-05-03 21:26:49 +00003452 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453}
3454
3455/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003456 * Update the w:quickfix_title variable in the quickfix/location list window
3457 */
3458 static void
3459qf_update_win_titlevar(qf_info_T *qi)
3460{
3461 win_T *win;
3462 win_T *curwin_save;
3463
3464 if ((win = qf_find_win(qi)) != NULL)
3465 {
3466 curwin_save = curwin;
3467 curwin = win;
3468 qf_set_title_var(qi);
3469 curwin = curwin_save;
3470 }
3471}
3472
3473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 * Find the quickfix buffer. If it exists, update the contents.
3475 */
3476 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003477qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003480 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
3483 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003484 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (buf != NULL)
3486 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003487 linenr_T old_line_count = buf->b_ml.ml_line_count;
3488
3489 if (old_last == NULL)
3490 /* set curwin/curbuf to buf and save a few things */
3491 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaard823fa92016-08-12 16:29:27 +02003493 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003494
Bram Moolenaar864293a2016-06-02 13:40:04 +02003495 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003496 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003497
Bram Moolenaar864293a2016-06-02 13:40:04 +02003498 if (old_last == NULL)
3499 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003500 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003501
3502 /* restore curwin/curbuf and a few other things */
3503 aucmd_restbuf(&aco);
3504 }
3505
3506 /* Only redraw when added lines are visible. This avoids flickering
3507 * when the added lines are not visible. */
3508 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3509 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511}
3512
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003513/*
3514 * Set "w:quickfix_title" if "qi" has a title.
3515 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003516 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003517qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003518{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003519 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3520 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003521 qi->qf_lists[qi->qf_curlist].qf_title);
3522}
3523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524/*
3525 * Fill current buffer with quickfix errors, replacing any previous contents.
3526 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003527 * If "old_last" is not NULL append the items after this one.
3528 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3529 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 */
3531 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003532qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003534 linenr_T lnum;
3535 qfline_T *qfp;
3536 buf_T *errbuf;
3537 int len;
3538 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
Bram Moolenaar864293a2016-06-02 13:40:04 +02003540 if (old_last == NULL)
3541 {
3542 if (buf != curbuf)
3543 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003544 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003545 return;
3546 }
3547
3548 /* delete all existing lines */
3549 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3550 (void)ml_delete((linenr_T)1, FALSE);
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
3553 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003554 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
3556 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003557 if (old_last == NULL)
3558 {
3559 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3560 lnum = 0;
3561 }
3562 else
3563 {
3564 qfp = old_last->qf_next;
3565 lnum = buf->b_ml.ml_line_count;
3566 }
3567 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569 if (qfp->qf_fnum != 0
3570 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3571 && errbuf->b_fname != NULL)
3572 {
3573 if (qfp->qf_type == 1) /* :helpgrep */
3574 STRCPY(IObuff, gettail(errbuf->b_fname));
3575 else
3576 STRCPY(IObuff, errbuf->b_fname);
3577 len = (int)STRLEN(IObuff);
3578 }
3579 else
3580 len = 0;
3581 IObuff[len++] = '|';
3582
3583 if (qfp->qf_lnum > 0)
3584 {
3585 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3586 len += (int)STRLEN(IObuff + len);
3587
3588 if (qfp->qf_col > 0)
3589 {
3590 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3591 len += (int)STRLEN(IObuff + len);
3592 }
3593
3594 sprintf((char *)IObuff + len, "%s",
3595 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3596 len += (int)STRLEN(IObuff + len);
3597 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003598 else if (qfp->qf_pattern != NULL)
3599 {
3600 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3601 len += (int)STRLEN(IObuff + len);
3602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 IObuff[len++] = '|';
3604 IObuff[len++] = ' ';
3605
3606 /* Remove newlines and leading whitespace from the text.
3607 * For an unrecognized line keep the indent, the compiler may
3608 * mark a word with ^^^^. */
3609 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3610 IObuff + len, IOSIZE - len);
3611
Bram Moolenaar864293a2016-06-02 13:40:04 +02003612 if (ml_append_buf(buf, lnum, IObuff,
3613 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003615 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003617 if (qfp == NULL)
3618 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003620
3621 if (old_last == NULL)
3622 /* Delete the empty line which is now at the end */
3623 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
3625
3626 /* correct cursor position */
3627 check_lnums(TRUE);
3628
Bram Moolenaar864293a2016-06-02 13:40:04 +02003629 if (old_last == NULL)
3630 {
3631 /* Set the 'filetype' to "qf" each time after filling the buffer.
3632 * This resembles reading a file into a buffer, it's more logical when
3633 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003634#ifdef FEAT_AUTOCMD
3635 ++curbuf_lock;
3636#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003637 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3638 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003641 keep_filetype = TRUE; /* don't detect 'filetype' */
3642 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003644 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003646 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003647 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003649 /* make sure it will be redrawn */
3650 redraw_curbuf_later(NOT_VALID);
3651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
3653 /* Restore KeyTyped, setting 'filetype' may reset it. */
3654 KeyTyped = old_KeyTyped;
3655}
3656
Bram Moolenaarb254af32017-12-18 19:48:58 +01003657 static void
3658qf_list_changed(qf_info_T *qi, int qf_idx)
3659{
3660 qi->qf_lists[qf_idx].qf_changedtick++;
3661}
3662
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003664 * Return TRUE when using ":vimgrep" for ":grep".
3665 */
3666 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003667grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003668{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003669 return ((cmdidx == CMD_grep
3670 || cmdidx == CMD_lgrep
3671 || cmdidx == CMD_grepadd
3672 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003673 && STRCMP("internal",
3674 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3675}
3676
3677/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003678 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 */
3680 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003681ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003683 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003685 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003687 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003688 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003689 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003690#ifdef FEAT_AUTOCMD
3691 char_u *au_name = NULL;
3692
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003693 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3694 if (grep_internal(eap->cmdidx))
3695 {
3696 ex_vimgrep(eap);
3697 return;
3698 }
3699
Bram Moolenaar7c626922005-02-07 22:01:03 +00003700 switch (eap->cmdidx)
3701 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003702 case CMD_make: au_name = (char_u *)"make"; break;
3703 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3704 case CMD_grep: au_name = (char_u *)"grep"; break;
3705 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3706 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3707 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003708 default: break;
3709 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003710 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3711 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003712 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003713# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003714 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003715 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003716# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003717 }
3718#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003719#ifdef FEAT_MBYTE
3720 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
Bram Moolenaara6557602006-02-04 22:43:20 +00003723 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3724 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003725 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003728 fname = get_mef_name();
3729 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003731 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 /*
3734 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3735 */
3736 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3737 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003738 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 cmd = alloc(len);
3740 if (cmd == NULL)
3741 return;
3742 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3743 (char *)p_shq);
3744 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003745 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 /*
3747 * Output a newline if there's something else than the :make command that
3748 * was typed (in which case the cursor is in column 0).
3749 */
3750 if (msg_col == 0)
3751 msg_didout = FALSE;
3752 msg_start();
3753 MSG_PUTS(":!");
3754 msg_outtrans(cmd); /* show what we are doing */
3755
3756 /* let the shell know if we are redirecting output or not */
3757 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3758
3759#ifdef AMIGA
3760 out_flush();
3761 /* read window status report and redraw before message */
3762 (void)char_avail();
3763#endif
3764
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003765 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003766 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3767 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003768 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003769 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003770 if (wp != NULL)
3771 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003772 if (res >= 0 && qi != NULL)
3773 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003774#ifdef FEAT_AUTOCMD
3775 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003776 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003777 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3778 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003779 if (qi->qf_curlist < qi->qf_listcount)
3780 res = qi->qf_lists[qi->qf_curlist].qf_count;
3781 else
3782 res = 0;
3783 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003784#endif
3785 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003786 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
Bram Moolenaar7c626922005-02-07 22:01:03 +00003788 mch_remove(fname);
3789 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 vim_free(cmd);
3791}
3792
3793/*
3794 * Return the name for the errorfile, in allocated memory.
3795 * Find a new unique name when 'makeef' contains "##".
3796 * Returns NULL for error.
3797 */
3798 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003799get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
3801 char_u *p;
3802 char_u *name;
3803 static int start = -1;
3804 static int off = 0;
3805#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003806 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807#endif
3808
3809 if (*p_mef == NUL)
3810 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003811 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (name == NULL)
3813 EMSG(_(e_notmp));
3814 return name;
3815 }
3816
3817 for (p = p_mef; *p; ++p)
3818 if (p[0] == '#' && p[1] == '#')
3819 break;
3820
3821 if (*p == NUL)
3822 return vim_strsave(p_mef);
3823
3824 /* Keep trying until the name doesn't exist yet. */
3825 for (;;)
3826 {
3827 if (start == -1)
3828 start = mch_get_pid();
3829 else
3830 off += 19;
3831
3832 name = alloc((unsigned)STRLEN(p_mef) + 30);
3833 if (name == NULL)
3834 break;
3835 STRCPY(name, p_mef);
3836 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3837 STRCAT(name, p + 2);
3838 if (mch_getperm(name) < 0
3839#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003840 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 && mch_lstat((char *)name, &sb) < 0
3842#endif
3843 )
3844 break;
3845 vim_free(name);
3846 }
3847 return name;
3848}
3849
3850/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003851 * Returns the number of valid entries in the current quickfix/location list.
3852 */
3853 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003854qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003855{
3856 qf_info_T *qi = &ql_info;
3857 qfline_T *qfp;
3858 int i, sz = 0;
3859 int prev_fnum = 0;
3860
3861 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3862 {
3863 /* Location list */
3864 qi = GET_LOC_LIST(curwin);
3865 if (qi == NULL)
3866 return 0;
3867 }
3868
3869 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003870 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003871 ++i, qfp = qfp->qf_next)
3872 {
3873 if (qfp->qf_valid)
3874 {
3875 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3876 sz++; /* Count all valid entries */
3877 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3878 {
3879 /* Count the number of files */
3880 sz++;
3881 prev_fnum = qfp->qf_fnum;
3882 }
3883 }
3884 }
3885
3886 return sz;
3887}
3888
3889/*
3890 * Returns the current index of the quickfix/location list.
3891 * Returns 0 if there is an error.
3892 */
3893 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003894qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003895{
3896 qf_info_T *qi = &ql_info;
3897
3898 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3899 {
3900 /* Location list */
3901 qi = GET_LOC_LIST(curwin);
3902 if (qi == NULL)
3903 return 0;
3904 }
3905
3906 return qi->qf_lists[qi->qf_curlist].qf_index;
3907}
3908
3909/*
3910 * Returns the current index in the quickfix/location list (counting only valid
3911 * entries). If no valid entries are in the list, then returns 1.
3912 */
3913 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003914qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003915{
3916 qf_info_T *qi = &ql_info;
3917 qf_list_T *qfl;
3918 qfline_T *qfp;
3919 int i, eidx = 0;
3920 int prev_fnum = 0;
3921
3922 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3923 {
3924 /* Location list */
3925 qi = GET_LOC_LIST(curwin);
3926 if (qi == NULL)
3927 return 1;
3928 }
3929
3930 qfl = &qi->qf_lists[qi->qf_curlist];
3931 qfp = qfl->qf_start;
3932
3933 /* check if the list has valid errors */
3934 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3935 return 1;
3936
3937 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3938 {
3939 if (qfp->qf_valid)
3940 {
3941 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3942 {
3943 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3944 {
3945 /* Count the number of files */
3946 eidx++;
3947 prev_fnum = qfp->qf_fnum;
3948 }
3949 }
3950 else
3951 eidx++;
3952 }
3953 }
3954
3955 return eidx ? eidx : 1;
3956}
3957
3958/*
3959 * Get the 'n'th valid error entry in the quickfix or location list.
3960 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3961 * For :cdo and :ldo returns the 'n'th valid error entry.
3962 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3963 */
3964 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003965qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003966{
3967 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3968 qfline_T *qfp = qfl->qf_start;
3969 int i, eidx;
3970 int prev_fnum = 0;
3971
3972 /* check if the list has valid errors */
3973 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3974 return 1;
3975
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003976 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003977 i++, qfp = qfp->qf_next)
3978 {
3979 if (qfp->qf_valid)
3980 {
3981 if (fdo)
3982 {
3983 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3984 {
3985 /* Count the number of files */
3986 eidx++;
3987 prev_fnum = qfp->qf_fnum;
3988 }
3989 }
3990 else
3991 eidx++;
3992 }
3993
3994 if (eidx == n)
3995 break;
3996 }
3997
3998 if (i <= qfl->qf_count)
3999 return i;
4000 else
4001 return 1;
4002}
4003
4004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004006 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004007 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 */
4009 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004010ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004012 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004013 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004014
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004015 if (eap->cmdidx == CMD_ll
4016 || eap->cmdidx == CMD_lrewind
4017 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004018 || eap->cmdidx == CMD_llast
4019 || eap->cmdidx == CMD_ldo
4020 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004021 {
4022 qi = GET_LOC_LIST(curwin);
4023 if (qi == NULL)
4024 {
4025 EMSG(_(e_loclist));
4026 return;
4027 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004028 }
4029
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004030 if (eap->addr_count > 0)
4031 errornr = (int)eap->line2;
4032 else
4033 {
4034 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4035 errornr = 0;
4036 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4037 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4038 errornr = 1;
4039 else
4040 errornr = 32767;
4041 }
4042
4043 /* For cdo and ldo commands, jump to the nth valid error.
4044 * For cfdo and lfdo commands, jump to the nth valid file entry.
4045 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004046 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4047 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004048 errornr = qf_get_nth_valid_entry(qi,
4049 eap->addr_count > 0 ? (int)eap->line1 : 1,
4050 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4051
4052 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053}
4054
4055/*
4056 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004057 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004058 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 */
4060 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004061ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004063 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004064 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004065
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004066 if (eap->cmdidx == CMD_lnext
4067 || eap->cmdidx == CMD_lNext
4068 || eap->cmdidx == CMD_lprevious
4069 || eap->cmdidx == CMD_lnfile
4070 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004071 || eap->cmdidx == CMD_lpfile
4072 || eap->cmdidx == CMD_ldo
4073 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004074 {
4075 qi = GET_LOC_LIST(curwin);
4076 if (qi == NULL)
4077 {
4078 EMSG(_(e_loclist));
4079 return;
4080 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004081 }
4082
Bram Moolenaar55b69262017-08-13 13:42:01 +02004083 if (eap->addr_count > 0
4084 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4085 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004086 errornr = (int)eap->line2;
4087 else
4088 errornr = 1;
4089
4090 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4091 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004093 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4094 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004096 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4097 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 ? BACKWARD_FILE
4099 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004100 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101}
4102
4103/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004104 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004105 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 */
4107 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004108ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004110 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004111 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004112 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004113#ifdef FEAT_AUTOCMD
4114 char_u *au_name = NULL;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004115 int save_qfid;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004116#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004117 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004118
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004119#ifdef FEAT_AUTOCMD
4120 switch (eap->cmdidx)
4121 {
4122 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4123 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4124 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4125 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4126 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4127 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4128 default: break;
4129 }
4130 if (au_name != NULL)
4131 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
4132#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004133#ifdef FEAT_MBYTE
4134 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4135#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004136#ifdef FEAT_BROWSE
4137 if (cmdmod.browse)
4138 {
4139 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4140 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4141 if (browse_file == NULL)
4142 return;
4143 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4144 vim_free(browse_file);
4145 }
4146 else
4147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004149 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004150
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004151 if (eap->cmdidx == CMD_lfile
4152 || eap->cmdidx == CMD_lgetfile
4153 || eap->cmdidx == CMD_laddfile)
4154 wp = curwin;
4155
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004156 /*
4157 * This function is used by the :cfile, :cgetfile and :caddfile
4158 * commands.
4159 * :cfile always creates a new quickfix list and jumps to the
4160 * first error.
4161 * :cgetfile creates a new quickfix list but doesn't jump to the
4162 * first error.
4163 * :caddfile adds to an existing quickfix list. If there is no
4164 * quickfix list then a new list is created.
4165 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004166 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4167 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004168 if (wp != NULL)
4169 qi = GET_LOC_LIST(wp);
4170 if (res >= 0 && qi != NULL)
4171 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004172#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004173 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004174 if (au_name != NULL)
4175 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004176 /*
4177 * Autocmd might have freed the quickfix/location list. Check whether it is
4178 * still valid
4179 */
4180 if (!qflist_valid(wp, save_qfid))
4181 return;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004182#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004183 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
4184 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004185 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187}
4188
4189/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004190 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004191 * ":vimgrepadd {pattern} file(s)"
4192 * ":lvimgrep {pattern} file(s)"
4193 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004194 */
4195 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004196ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004197{
Bram Moolenaar81695252004-12-29 20:58:21 +00004198 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004199 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004200 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004201 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004202 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004203 char_u *s;
4204 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004205 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004206 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004207 int loclist_cmd = FALSE;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004208#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004209 int_u save_qfid;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004210 qfline_T *cur_qf_start;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004211 win_T *wp;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004212#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004213 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004214 buf_T *buf;
4215 int duplicate_name = FALSE;
4216 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004217 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004218 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004219 buf_T *first_match_buf = NULL;
4220 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004221 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004222#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4223 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004224#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004225 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004226 int flags = 0;
4227 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004228 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004229 char_u *dirname_start = NULL;
4230 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004231 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004232#ifdef FEAT_AUTOCMD
4233 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004234
4235 switch (eap->cmdidx)
4236 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004237 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4238 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4239 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004240 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004241 case CMD_grep: au_name = (char_u *)"grep"; break;
4242 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4243 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4244 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004245 default: break;
4246 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004247 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4248 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004249 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004250# ifdef FEAT_EVAL
4251 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004252 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004253# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004254 }
4255#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004256
Bram Moolenaar754b5602006-02-09 23:53:20 +00004257 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004258 || eap->cmdidx == CMD_lvimgrep
4259 || eap->cmdidx == CMD_lgrepadd
4260 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004261 {
4262 qi = ll_get_or_alloc_list(curwin);
4263 if (qi == NULL)
4264 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004265 loclist_cmd = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00004266 }
4267
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004268 if (eap->addr_count > 0)
4269 tomatch = eap->line2;
4270 else
4271 tomatch = MAXLNUM;
4272
Bram Moolenaar81695252004-12-29 20:58:21 +00004273 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004274 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004275 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004276 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004277 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004278 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004279 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004280 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004281 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004282
4283 if (s != NULL && *s == NUL)
4284 {
4285 /* Pattern is empty, use last search pattern. */
4286 if (last_search_pat() == NULL)
4287 {
4288 EMSG(_(e_noprevre));
4289 goto theend;
4290 }
4291 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4292 }
4293 else
4294 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4295
Bram Moolenaar86b68352004-12-27 21:59:20 +00004296 if (regmatch.regprog == NULL)
4297 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004298 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004299 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004300
4301 p = skipwhite(p);
4302 if (*p == NUL)
4303 {
4304 EMSG(_("E683: File name missing or invalid pattern"));
4305 goto theend;
4306 }
4307
Bram Moolenaar55b69262017-08-13 13:42:01 +02004308 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4309 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004310 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004311 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004312 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004313
4314 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004315 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004316 goto theend;
4317 if (fcount == 0)
4318 {
4319 EMSG(_(e_nomatch));
4320 goto theend;
4321 }
4322
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004323 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4324 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004325 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004326 {
4327 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004328 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004329 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004330
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004331 /* Remember the current directory, because a BufRead autocommand that does
4332 * ":lcd %:p:h" changes the meaning of short path names. */
4333 mch_dirname(dirname_start, MAXPATHL);
4334
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004335#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004336 /* Remember the current values of the quickfix list and qf_start, so that
4337 * we can check for autocommands changing the current quickfix list. */
4338 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004339 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4340#endif
4341
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004342 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004343 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004344 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004345 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004346 if (time(NULL) > seconds)
4347 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004348 /* Display the file name every second or so, show the user we are
4349 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004350 seconds = time(NULL);
4351 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004352 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004353 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004354 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004355 else
4356 {
4357 msg_outtrans(p);
4358 vim_free(p);
4359 }
4360 msg_clr_eos();
4361 msg_didout = FALSE; /* overwrite this message */
4362 msg_nowait = TRUE; /* don't wait for this message */
4363 msg_col = 0;
4364 out_flush();
4365 }
4366
Bram Moolenaar81695252004-12-29 20:58:21 +00004367 buf = buflist_findname_exp(fnames[fi]);
4368 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4369 {
4370 /* Remember that a buffer with this name already exists. */
4371 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004372 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004373 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004374
4375#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4376 /* Don't do Filetype autocommands to avoid loading syntax and
4377 * indent scripts, a great speed improvement. */
4378 save_ei = au_event_disable(",Filetype");
4379#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004380 /* Don't use modelines here, it's useless. */
4381 save_mls = p_mls;
4382 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004383
4384 /* Load file into a buffer, so that 'fileencoding' is detected,
4385 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004386 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004387
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004388 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004389#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4390 au_event_restore(save_ei);
4391#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004392 }
4393 else
4394 /* Use existing, loaded buffer. */
4395 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004396
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004397#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004398 if (loclist_cmd)
4399 {
4400 /*
4401 * Verify that the location list is still valid. An autocmd might
4402 * have freed the location list.
4403 */
4404 if (!qflist_valid(curwin, save_qfid))
4405 {
4406 EMSG(_(e_loc_list_changed));
4407 goto theend;
4408 }
4409 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004410 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4411 {
4412 int idx;
4413
4414 /* Autocommands changed the quickfix list. Find the one we were
4415 * using and restore it. */
4416 for (idx = 0; idx < LISTCOUNT; ++idx)
4417 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4418 {
4419 qi->qf_curlist = idx;
4420 break;
4421 }
4422 if (idx == LISTCOUNT)
4423 {
4424 /* List cannot be found, create a new one. */
4425 qf_new_list(qi, *eap->cmdlinep);
4426 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4427 }
4428 }
4429#endif
4430
Bram Moolenaar81695252004-12-29 20:58:21 +00004431 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004432 {
4433 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004434 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004435 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004436 else
4437 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004438 /* Try for a match in all lines of the buffer.
4439 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004440 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004441 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4442 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004443 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004444 col = 0;
4445 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004446 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004447 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004448 /* Pass the buffer number so that it gets used even for a
4449 * dummy buffer, unless duplicate_name is set, then the
4450 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004451 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004452 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004453 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004454 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004455 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004456 ml_get_buf(buf,
4457 regmatch.startpos[0].lnum + lnum, FALSE),
4458 regmatch.startpos[0].lnum + lnum,
4459 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004460 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004461 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004462 0, /* nr */
4463 0, /* type */
4464 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004465 ) == FAIL)
4466 {
4467 got_int = TRUE;
4468 break;
4469 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004470 found_match = TRUE;
4471 if (--tomatch == 0)
4472 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004473 if ((flags & VGR_GLOBAL) == 0
4474 || regmatch.endpos[0].lnum > 0)
4475 break;
4476 col = regmatch.endpos[0].col
4477 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004478 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004479 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004480 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004481 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004482 if (got_int)
4483 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004484 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004485#ifdef FEAT_AUTOCMD
4486 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4487#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004488
4489 if (using_dummy)
4490 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004491 if (found_match && first_match_buf == NULL)
4492 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004493 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004494 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004495 /* Never keep a dummy buffer if there is another buffer
4496 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004497 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004498 buf = NULL;
4499 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004500 else if (!cmdmod.hide
4501 || buf->b_p_bh[0] == 'u' /* "unload" */
4502 || buf->b_p_bh[0] == 'w' /* "wipe" */
4503 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004504 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004505 /* When no match was found we don't need to remember the
4506 * buffer, wipe it out. If there was a match and it
4507 * wasn't the first one or we won't jump there: only
4508 * unload the buffer.
4509 * Ignore 'hidden' here, because it may lead to having too
4510 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004511 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004512 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004513 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004514 buf = NULL;
4515 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004516 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004517 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004518 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004519 /* Keeping the buffer, remove the dummy flag. */
4520 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004521 buf = NULL;
4522 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004523 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004524
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004525 if (buf != NULL)
4526 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004527 /* Keeping the buffer, remove the dummy flag. */
4528 buf->b_flags &= ~BF_DUMMY;
4529
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004530 /* If the buffer is still loaded we need to use the
4531 * directory we jumped to below. */
4532 if (buf == first_match_buf
4533 && target_dir == NULL
4534 && STRCMP(dirname_start, dirname_now) != 0)
4535 target_dir = vim_strsave(dirname_now);
4536
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004537 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004538 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004539 * need to be done (again). But not the window-local
4540 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004541 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004542#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004543 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4544 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004545#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004546 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004547 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004548 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004549 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004550 }
4551 }
4552
4553 FreeWild(fcount, fnames);
4554
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004555 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4556 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4557 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004558 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004559
Bram Moolenaar864293a2016-06-02 13:40:04 +02004560 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004561
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004562#ifdef FEAT_AUTOCMD
4563 if (au_name != NULL)
4564 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4565 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004566 /*
4567 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4568 * is still valid.
4569 */
4570 wp = loclist_cmd ? curwin : NULL;
4571 if (!qflist_valid(wp, save_qfid))
4572 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004573#endif
4574
Bram Moolenaar86b68352004-12-27 21:59:20 +00004575 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004576 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004577 {
4578 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004579 {
4580 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004581 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004582 if (buf != curbuf)
4583 /* If we jumped to another buffer redrawing will already be
4584 * taken care of. */
4585 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004586
4587 /* Jump to the directory used after loading the buffer. */
4588 if (curbuf == first_match_buf && target_dir != NULL)
4589 {
4590 exarg_T ea;
4591
4592 ea.arg = target_dir;
4593 ea.cmdidx = CMD_lcd;
4594 ex_cd(&ea);
4595 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004596 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004597 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004598 else
4599 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004600
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004601 /* If we loaded a dummy buffer into the current window, the autocommands
4602 * may have messed up things, need to redraw and recompute folds. */
4603 if (redraw_for_dummy)
4604 {
4605#ifdef FEAT_FOLDING
4606 foldUpdateAll(curwin);
4607#else
4608 redraw_later(NOT_VALID);
4609#endif
4610 }
4611
Bram Moolenaar86b68352004-12-27 21:59:20 +00004612theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004613 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004614 vim_free(dirname_now);
4615 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004616 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004617 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004618}
4619
4620/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004621 * Restore current working directory to "dirname_start" if they differ, taking
4622 * into account whether it is set locally or globally.
4623 */
4624 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004625restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004626{
4627 char_u *dirname_now = alloc(MAXPATHL);
4628
4629 if (NULL != dirname_now)
4630 {
4631 mch_dirname(dirname_now, MAXPATHL);
4632 if (STRCMP(dirname_start, dirname_now) != 0)
4633 {
4634 /* If the directory has changed, change it back by building up an
4635 * appropriate ex command and executing it. */
4636 exarg_T ea;
4637
4638 ea.arg = dirname_start;
4639 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4640 ex_cd(&ea);
4641 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004642 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004643 }
4644}
4645
4646/*
4647 * Load file "fname" into a dummy buffer and return the buffer pointer,
4648 * placing the directory resulting from the buffer load into the
4649 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4650 * prior to calling this function. Restores directory to "dirname_start" prior
4651 * to returning, if autocmds or the 'autochdir' option have changed it.
4652 *
4653 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4654 * or wipe_dummy_buffer() later!
4655 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004656 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004657 */
4658 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004659load_dummy_buffer(
4660 char_u *fname,
4661 char_u *dirname_start, /* in: old directory */
4662 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004663{
4664 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004665 bufref_T newbufref;
4666 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004667 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004668 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004669 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004670
4671 /* Allocate a buffer without putting it in the buffer list. */
4672 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4673 if (newbuf == NULL)
4674 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004675 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004676
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004677 /* Init the options. */
4678 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4679
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004680 /* need to open the memfile before putting the buffer in a window */
4681 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004682 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004683 /* Make sure this buffer isn't wiped out by auto commands. */
4684 ++newbuf->b_locked;
4685
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004686 /* set curwin/curbuf to buf and save a few things */
4687 aucmd_prepbuf(&aco, newbuf);
4688
4689 /* Need to set the filename for autocommands. */
4690 (void)setfname(curbuf, fname, NULL, FALSE);
4691
Bram Moolenaar81695252004-12-29 20:58:21 +00004692 /* Create swap file now to avoid the ATTENTION message. */
4693 check_need_swap(TRUE);
4694
4695 /* Remove the "dummy" flag, otherwise autocommands may not
4696 * work. */
4697 curbuf->b_flags &= ~BF_DUMMY;
4698
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004699 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004700 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004701 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004702 NULL, READ_NEW | READ_DUMMY);
4703 --newbuf->b_locked;
4704 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004705 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004706 && !(curbuf->b_flags & BF_NEW))
4707 {
4708 failed = FALSE;
4709 if (curbuf != newbuf)
4710 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004711 /* Bloody autocommands changed the buffer! Can happen when
4712 * using netrw and editing a remote file. Use the current
4713 * buffer instead, delete the dummy one after restoring the
4714 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004715 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004716 newbuf = curbuf;
4717 }
4718 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004719
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004720 /* restore curwin/curbuf and a few other things */
4721 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004722 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4723 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004724
4725 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4726 * skip it. */
4727 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004728 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004729
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004730 /*
4731 * When autocommands/'autochdir' option changed directory: go back.
4732 * Let the caller know what the resulting dir was first, in case it is
4733 * important.
4734 */
4735 mch_dirname(resulting_dir, MAXPATHL);
4736 restore_start_dir(dirname_start);
4737
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004738 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004739 return NULL;
4740 if (failed)
4741 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004742 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004743 return NULL;
4744 }
4745 return newbuf;
4746}
4747
4748/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004749 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4750 * directory to "dirname_start" prior to returning, if autocmds or the
4751 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004752 */
4753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004754wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004755{
4756 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004757 {
4758#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4759 cleanup_T cs;
4760
4761 /* Reset the error/interrupt/exception state here so that aborting()
4762 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4763 * work when got_int is set. */
4764 enter_cleanup(&cs);
4765#endif
4766
Bram Moolenaar81695252004-12-29 20:58:21 +00004767 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004768
4769#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4770 /* Restore the error/interrupt/exception state if not discarded by a
4771 * new aborting error, interrupt, or uncaught exception. */
4772 leave_cleanup(&cs);
4773#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004774 /* When autocommands/'autochdir' option changed directory: go back. */
4775 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004776 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004777}
4778
4779/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004780 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4781 * directory to "dirname_start" prior to returning, if autocmds or the
4782 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004783 */
4784 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004785unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004786{
4787 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004788 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004789 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004790
4791 /* When autocommands/'autochdir' option changed directory: go back. */
4792 restore_start_dir(dirname_start);
4793 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004794}
4795
Bram Moolenaar05159a02005-02-26 23:04:13 +00004796#if defined(FEAT_EVAL) || defined(PROTO)
4797/*
4798 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004799 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004800 */
4801 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004802get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004803{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004804 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004805 dict_T *dict;
4806 char_u buf[2];
4807 qfline_T *qfp;
4808 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004809 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004810
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004811 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004812 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004813 qi = &ql_info;
4814 if (wp != NULL)
4815 {
4816 qi = GET_LOC_LIST(wp);
4817 if (qi == NULL)
4818 return FAIL;
4819 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004820 }
4821
Bram Moolenaard823fa92016-08-12 16:29:27 +02004822 if (qf_idx == -1)
4823 qf_idx = qi->qf_curlist;
4824
4825 if (qf_idx >= qi->qf_listcount
4826 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004827 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004828
Bram Moolenaard823fa92016-08-12 16:29:27 +02004829 qfp = qi->qf_lists[qf_idx].qf_start;
4830 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004831 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004832 /* Handle entries with a non-existing buffer number. */
4833 bufnum = qfp->qf_fnum;
4834 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4835 bufnum = 0;
4836
Bram Moolenaar05159a02005-02-26 23:04:13 +00004837 if ((dict = dict_alloc()) == NULL)
4838 return FAIL;
4839 if (list_append_dict(list, dict) == FAIL)
4840 return FAIL;
4841
4842 buf[0] = qfp->qf_type;
4843 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004844 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004845 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4846 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4847 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4848 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004849 || dict_add_nr_str(dict, "pattern", 0L,
4850 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4851 || dict_add_nr_str(dict, "text", 0L,
4852 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004853 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4854 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4855 return FAIL;
4856
4857 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004858 if (qfp == NULL)
4859 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004860 }
4861 return OK;
4862}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004863
4864/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004865 * Flags used by getqflist()/getloclist() to determine which fields to return.
4866 */
4867enum {
4868 QF_GETLIST_NONE = 0x0,
4869 QF_GETLIST_TITLE = 0x1,
4870 QF_GETLIST_ITEMS = 0x2,
4871 QF_GETLIST_NR = 0x4,
4872 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004873 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004874 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004875 QF_GETLIST_IDX = 0x40,
4876 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01004877 QF_GETLIST_TICK = 0x100,
4878 QF_GETLIST_ALL = 0x1FF
Bram Moolenaard823fa92016-08-12 16:29:27 +02004879};
4880
4881/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004882 * Parse text from 'di' and return the quickfix list items
4883 */
4884 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004885qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004886{
4887 int status = FAIL;
4888 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004889 char_u *errorformat = p_efm;
4890 dictitem_T *efm_di;
4891 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004892
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004893 /* Only a List value is supported */
4894 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004895 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004896 /* If errorformat is supplied then use it, otherwise use the 'efm'
4897 * option setting
4898 */
4899 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4900 {
4901 if (efm_di->di_tv.v_type != VAR_STRING ||
4902 efm_di->di_tv.vval.v_string == NULL)
4903 return FAIL;
4904 errorformat = efm_di->di_tv.vval.v_string;
4905 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004906
Bram Moolenaar36538222017-09-02 19:51:44 +02004907 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004908 if (l == NULL)
4909 return FAIL;
4910
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004911 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4912 if (qi != NULL)
4913 {
4914 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4915 qi->qf_refcount++;
4916
Bram Moolenaar36538222017-09-02 19:51:44 +02004917 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004918 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4919 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004920 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004921 qf_free(qi, 0);
4922 }
4923 free(qi);
4924 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004925 dict_add_list(retdict, "items", l);
4926 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004927 }
4928
4929 return status;
4930}
4931
4932/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004933 * Return the quickfix/location list number with the given identifier.
4934 * Returns -1 if list is not found.
4935 */
4936 static int
4937qf_id2nr(qf_info_T *qi, int_u qfid)
4938{
4939 int qf_idx;
4940
4941 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4942 if (qi->qf_lists[qf_idx].qf_id == qfid)
4943 return qf_idx;
4944 return -1;
4945}
4946
4947/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004948 * Return quickfix/location list details (title) as a
4949 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4950 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004951 */
4952 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004953qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004954{
4955 qf_info_T *qi = &ql_info;
4956 int status = OK;
4957 int qf_idx;
4958 dictitem_T *di;
4959 int flags = QF_GETLIST_NONE;
4960
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004961 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004962 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004963
Bram Moolenaard823fa92016-08-12 16:29:27 +02004964 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004965 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004966
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004967 if (dict_find(what, (char_u *)"all", -1) != NULL)
4968 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004969
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004970 if (dict_find(what, (char_u *)"title", -1) != NULL)
4971 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004972
Bram Moolenaara6d48492017-12-12 22:45:31 +01004973 if (dict_find(what, (char_u *)"nr", -1) != NULL)
4974 flags |= QF_GETLIST_NR;
4975
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004976 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4977 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004978
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004979 if (dict_find(what, (char_u *)"context", -1) != NULL)
4980 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004981
Bram Moolenaara6d48492017-12-12 22:45:31 +01004982 if (dict_find(what, (char_u *)"id", -1) != NULL)
4983 flags |= QF_GETLIST_ID;
4984
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004985 if (dict_find(what, (char_u *)"items", -1) != NULL)
4986 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004987
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004988 if (dict_find(what, (char_u *)"idx", -1) != NULL)
4989 flags |= QF_GETLIST_IDX;
4990
4991 if (dict_find(what, (char_u *)"size", -1) != NULL)
4992 flags |= QF_GETLIST_SIZE;
4993
Bram Moolenaarb254af32017-12-18 19:48:58 +01004994 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
4995 flags |= QF_GETLIST_TICK;
4996
Bram Moolenaara6d48492017-12-12 22:45:31 +01004997 if (qi != NULL && qi->qf_listcount != 0)
4998 {
4999 qf_idx = qi->qf_curlist; /* default is the current list */
5000 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5001 {
5002 /* Use the specified quickfix/location list */
5003 if (di->di_tv.v_type == VAR_NUMBER)
5004 {
5005 /* for zero use the current list */
5006 if (di->di_tv.vval.v_number != 0)
5007 {
5008 qf_idx = di->di_tv.vval.v_number - 1;
5009 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
5010 qf_idx = -1;
5011 }
5012 }
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005013 else if (di->di_tv.v_type == VAR_STRING
5014 && di->di_tv.vval.v_string != NULL
5015 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005016 /* Get the last quickfix list number */
5017 qf_idx = qi->qf_listcount - 1;
5018 else
5019 qf_idx = -1;
5020 flags |= QF_GETLIST_NR;
5021 }
5022
5023 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5024 {
5025 /* Look for a list with the specified id */
5026 if (di->di_tv.v_type == VAR_NUMBER)
5027 {
5028 /*
5029 * For zero, use the current list or the list specifed by 'nr'
5030 */
5031 if (di->di_tv.vval.v_number != 0)
5032 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5033 flags |= QF_GETLIST_ID;
5034 }
5035 else
5036 qf_idx = -1;
5037 }
5038 }
5039
5040 /* List is not present or is empty */
5041 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
5042 {
5043 if (flags & QF_GETLIST_TITLE)
5044 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5045 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5046 {
5047 list_T *l = list_alloc();
5048 if (l != NULL)
5049 status = dict_add_list(retdict, "items", l);
5050 else
5051 status = FAIL;
5052 }
5053 if ((status == OK) && (flags & QF_GETLIST_NR))
5054 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5055 if ((status == OK) && (flags & QF_GETLIST_WINID))
5056 status = dict_add_nr_str(retdict, "winid", 0L, NULL);
5057 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5058 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5059 if ((status == OK) && (flags & QF_GETLIST_ID))
5060 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5061 if ((status == OK) && (flags & QF_GETLIST_IDX))
5062 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5063 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5064 status = dict_add_nr_str(retdict, "size", 0L, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005065 if ((status == OK) && (flags & QF_GETLIST_TICK))
5066 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005067
5068 return status;
5069 }
5070
Bram Moolenaard823fa92016-08-12 16:29:27 +02005071 if (flags & QF_GETLIST_TITLE)
5072 {
5073 char_u *t;
5074 t = qi->qf_lists[qf_idx].qf_title;
5075 if (t == NULL)
5076 t = (char_u *)"";
5077 status = dict_add_nr_str(retdict, "title", 0L, t);
5078 }
5079 if ((status == OK) && (flags & QF_GETLIST_NR))
5080 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5081 if ((status == OK) && (flags & QF_GETLIST_WINID))
5082 {
5083 win_T *win;
Bram Moolenaar74240d32017-12-10 15:26:15 +01005084 int win_id = 0;
5085
Bram Moolenaard823fa92016-08-12 16:29:27 +02005086 win = qf_find_win(qi);
5087 if (win != NULL)
Bram Moolenaar74240d32017-12-10 15:26:15 +01005088 win_id = win->w_id;
5089 status = dict_add_nr_str(retdict, "winid", win_id, NULL);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005090 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005091 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5092 {
5093 list_T *l = list_alloc();
5094 if (l != NULL)
5095 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005096 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005097 dict_add_list(retdict, "items", l);
5098 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005099 else
5100 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005101 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005102
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005103 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5104 {
5105 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5106 {
5107 di = dictitem_alloc((char_u *)"context");
5108 if (di != NULL)
5109 {
5110 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005111 status = dict_add(retdict, di);
5112 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02005113 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005114 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005115 else
5116 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005117 }
5118 else
5119 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5120 }
5121
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005122 if ((status == OK) && (flags & QF_GETLIST_ID))
5123 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5124 NULL);
5125
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005126 if ((status == OK) && (flags & QF_GETLIST_IDX))
5127 {
5128 int idx = qi->qf_lists[qf_idx].qf_index;
5129 if (qi->qf_lists[qf_idx].qf_count == 0)
5130 /* For empty lists, qf_index is set to 1 */
5131 idx = 0;
5132 status = dict_add_nr_str(retdict, "idx", idx, NULL);
5133 }
5134
5135 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5136 status = dict_add_nr_str(retdict, "size",
5137 qi->qf_lists[qf_idx].qf_count, NULL);
5138
Bram Moolenaarb254af32017-12-18 19:48:58 +01005139 if ((status == OK) && (flags & QF_GETLIST_TICK))
5140 status = dict_add_nr_str(retdict, "changedtick",
5141 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5142
Bram Moolenaard823fa92016-08-12 16:29:27 +02005143 return status;
5144}
5145
5146/*
5147 * Add list of entries to quickfix/location list. Each list entry is
5148 * a dictionary with item information.
5149 */
5150 static int
5151qf_add_entries(
5152 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005153 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005154 list_T *list,
5155 char_u *title,
5156 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005157{
5158 listitem_T *li;
5159 dict_T *d;
5160 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005161 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005162 long lnum;
5163 int col, nr;
5164 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005165 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005166 int valid, status;
5167 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005168 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005169
Bram Moolenaara3921f42017-06-04 15:30:34 +02005170 if (action == ' ' || qf_idx == qi->qf_listcount)
5171 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005172 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005173 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005174 qf_idx = qi->qf_curlist;
5175 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005176 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005177 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005178 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005179 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005180 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005181 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005182 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005183 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005184
5185 for (li = list->lv_first; li != NULL; li = li->li_next)
5186 {
5187 if (li->li_tv.v_type != VAR_DICT)
5188 continue; /* Skip non-dict items */
5189
5190 d = li->li_tv.vval.v_dict;
5191 if (d == NULL)
5192 continue;
5193
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005194 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005195 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5196 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5197 col = (int)get_dict_number(d, (char_u *)"col");
5198 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5199 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005200 type = get_dict_string(d, (char_u *)"type", TRUE);
5201 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5202 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005203 if (text == NULL)
5204 text = vim_strsave((char_u *)"");
5205
5206 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005207 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005208 valid = FALSE;
5209
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005210 /* Mark entries with non-existing buffer number as not valid. Give the
5211 * error message only once. */
5212 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5213 {
5214 if (!did_bufnr_emsg)
5215 {
5216 did_bufnr_emsg = TRUE;
5217 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5218 }
5219 valid = FALSE;
5220 bufnum = 0;
5221 }
5222
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005223 /* If the 'valid' field is present it overrules the detected value. */
5224 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5225 valid = (int)get_dict_number(d, (char_u *)"valid");
5226
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005227 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005228 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005229 NULL, /* dir */
5230 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005231 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005232 text,
5233 lnum,
5234 col,
5235 vcol, /* vis_col */
5236 pattern, /* search pattern */
5237 nr,
5238 type == NULL ? NUL : *type,
5239 valid);
5240
5241 vim_free(filename);
5242 vim_free(pattern);
5243 vim_free(text);
5244 vim_free(type);
5245
5246 if (status == FAIL)
5247 {
5248 retval = FAIL;
5249 break;
5250 }
5251 }
5252
Bram Moolenaara3921f42017-06-04 15:30:34 +02005253 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005254 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005255 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005256 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005257 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005258 if (action != 'a')
5259 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005260 qi->qf_lists[qf_idx].qf_ptr =
5261 qi->qf_lists[qf_idx].qf_start;
5262 if (qi->qf_lists[qf_idx].qf_count > 0)
5263 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005264 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005265
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005266 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005267 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005268
5269 return retval;
5270}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005271
5272 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005273qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005274{
5275 dictitem_T *di;
5276 int retval = FAIL;
5277 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005278 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005279 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005280
5281 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5282 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005283
5284 qf_idx = qi->qf_curlist; /* default is the current list */
5285 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5286 {
5287 /* Use the specified quickfix/location list */
5288 if (di->di_tv.v_type == VAR_NUMBER)
5289 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005290 /* for zero use the current list */
5291 if (di->di_tv.vval.v_number != 0)
5292 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005293
Bram Moolenaar55b69262017-08-13 13:42:01 +02005294 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5295 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005296 /*
5297 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005298 * non-available list and add the new list at the end of the
5299 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005300 */
5301 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005302 qf_idx = qi->qf_listcount - 1;
5303 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005304 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005305 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005306 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005307 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005308 }
5309 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005310 && di->di_tv.vval.v_string != NULL
5311 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005312 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005313 if (qi->qf_listcount > 0)
5314 qf_idx = qi->qf_listcount - 1;
5315 else if (newlist)
5316 qf_idx = 0;
5317 else
5318 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005319 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005320 else
5321 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005322 }
5323
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005324 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5325 {
5326 /* Use the quickfix/location list with the specified id */
5327 if (di->di_tv.v_type == VAR_NUMBER)
5328 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005329 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5330 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005331 return FAIL; /* List not found */
5332 }
5333 else
5334 return FAIL;
5335 }
5336
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005337 if (newlist)
5338 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005339 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005340 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005341 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005342 }
5343
5344 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5345 {
5346 if (di->di_tv.v_type == VAR_STRING)
5347 {
5348 vim_free(qi->qf_lists[qf_idx].qf_title);
5349 qi->qf_lists[qf_idx].qf_title =
5350 get_dict_string(what, (char_u *)"title", TRUE);
5351 if (qf_idx == qi->qf_curlist)
5352 qf_update_win_titlevar(qi);
5353 retval = OK;
5354 }
5355 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005356
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005357 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5358 {
5359 if (di->di_tv.v_type == VAR_LIST)
5360 {
5361 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5362
5363 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5364 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005365 if (action == 'r')
5366 {
5367 /*
5368 * When replacing the quickfix list entries using
5369 * qf_add_entries(), the title is set with a ':' prefix.
5370 * Restore the title with the saved title.
5371 */
5372 vim_free(qi->qf_lists[qf_idx].qf_title);
5373 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5374 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005375 vim_free(title_save);
5376 }
5377 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005378
Bram Moolenaar36538222017-09-02 19:51:44 +02005379 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5380 {
5381 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5382 return FAIL;
5383 errorformat = di->di_tv.vval.v_string;
5384 }
5385
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005386 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005387 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005388 /* Only a List value is supported */
5389 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005390 {
5391 if (action == 'r')
5392 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005393 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005394 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5395 retval = OK;
5396 }
5397 else
5398 return FAIL;
5399 }
5400
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005401 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5402 {
5403 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005404
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005405 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005406 ctx = alloc_tv();
5407 if (ctx != NULL)
5408 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005409 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005410 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005411 }
5412
Bram Moolenaarb254af32017-12-18 19:48:58 +01005413 if (retval == OK)
5414 qf_list_changed(qi, qf_idx);
5415
Bram Moolenaard823fa92016-08-12 16:29:27 +02005416 return retval;
5417}
5418
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005419/*
5420 * Find the non-location list window with the specified location list.
5421 */
5422 static win_T *
5423find_win_with_ll(qf_info_T *qi)
5424{
5425 win_T *wp = NULL;
5426
5427 FOR_ALL_WINDOWS(wp)
5428 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5429 return wp;
5430
5431 return NULL;
5432}
5433
5434/*
5435 * Free the entire quickfix/location list stack.
5436 * If the quickfix/location list window is open, then clear it.
5437 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005438 static void
5439qf_free_stack(win_T *wp, qf_info_T *qi)
5440{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005441 win_T *qfwin = qf_find_win(qi);
5442 win_T *llwin = NULL;
5443 win_T *orig_wp = wp;
5444
5445 if (qfwin != NULL)
5446 {
5447 /* If the quickfix/location list window is open, then clear it */
5448 if (qi->qf_curlist < qi->qf_listcount)
5449 qf_free(qi, qi->qf_curlist);
5450 qf_update_buffer(qi, NULL);
5451 }
5452
5453 if (wp != NULL && IS_LL_WINDOW(wp))
5454 {
5455 /* If in the location list window, then use the non-location list
5456 * window with this location list (if present)
5457 */
5458 llwin = find_win_with_ll(qi);
5459 if (llwin != NULL)
5460 wp = llwin;
5461 }
5462
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005463 qf_free_all(wp);
5464 if (wp == NULL)
5465 {
5466 /* quickfix list */
5467 qi->qf_curlist = 0;
5468 qi->qf_listcount = 0;
5469 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005470 else if (IS_LL_WINDOW(orig_wp))
5471 {
5472 /* If the location list window is open, then create a new empty
5473 * location list */
5474 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005475
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005476 /* first free the list reference in the location list window */
5477 ll_free_all(&orig_wp->w_llist_ref);
5478
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005479 orig_wp->w_llist_ref = new_ll;
5480 if (llwin != NULL)
5481 {
5482 llwin->w_llist = new_ll;
5483 new_ll->qf_refcount++;
5484 }
5485 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005486}
5487
Bram Moolenaard823fa92016-08-12 16:29:27 +02005488/*
5489 * Populate the quickfix list with the items supplied in the list
5490 * of dictionaries. "title" will be copied to w:quickfix_title.
5491 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5492 */
5493 int
5494set_errorlist(
5495 win_T *wp,
5496 list_T *list,
5497 int action,
5498 char_u *title,
5499 dict_T *what)
5500{
5501 qf_info_T *qi = &ql_info;
5502 int retval = OK;
5503
5504 if (wp != NULL)
5505 {
5506 qi = ll_get_or_alloc_list(wp);
5507 if (qi == NULL)
5508 return FAIL;
5509 }
5510
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005511 if (action == 'f')
5512 {
5513 /* Free the entire quickfix or location list stack */
5514 qf_free_stack(wp, qi);
5515 }
5516 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005517 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005518 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005519 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005520 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005521 if (retval == OK)
5522 qf_list_changed(qi, qi->qf_curlist);
5523 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005524
5525 return retval;
5526}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005527
5528 static int
5529mark_quickfix_ctx(qf_info_T *qi, int copyID)
5530{
5531 int i;
5532 int abort = FALSE;
5533 typval_T *ctx;
5534
5535 for (i = 0; i < LISTCOUNT && !abort; ++i)
5536 {
5537 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005538 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5539 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005540 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5541 }
5542
5543 return abort;
5544}
5545
5546/*
5547 * Mark the context of the quickfix list and the location lists (if present) as
5548 * "in use". So that garabage collection doesn't free the context.
5549 */
5550 int
5551set_ref_in_quickfix(int copyID)
5552{
5553 int abort = FALSE;
5554 tabpage_T *tp;
5555 win_T *win;
5556
5557 abort = mark_quickfix_ctx(&ql_info, copyID);
5558 if (abort)
5559 return abort;
5560
5561 FOR_ALL_TAB_WINDOWS(tp, win)
5562 {
5563 if (win->w_llist != NULL)
5564 {
5565 abort = mark_quickfix_ctx(win->w_llist, copyID);
5566 if (abort)
5567 return abort;
5568 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005569 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5570 {
5571 /* In a location list window and none of the other windows is
5572 * referring to this location list. Mark the location list
5573 * context as still in use.
5574 */
5575 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5576 if (abort)
5577 return abort;
5578 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005579 }
5580
5581 return abort;
5582}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005583#endif
5584
Bram Moolenaar81695252004-12-29 20:58:21 +00005585/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005586 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005587 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005588 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005589 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005590 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005591 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005592 */
5593 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005594ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005595{
5596 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005597 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005598#ifdef FEAT_AUTOCMD
5599 char_u *au_name = NULL;
5600#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005601 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005602
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005603#ifdef FEAT_AUTOCMD
5604 switch (eap->cmdidx)
5605 {
5606 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5607 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5608 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5609 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5610 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5611 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5612 default: break;
5613 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005614 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5615 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005616 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005617# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005618 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005619 return;
5620# endif
5621 }
5622#endif
5623
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005624 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005625 if (eap->cmdidx == CMD_lbuffer
5626 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005627 || eap->cmdidx == CMD_laddbuffer)
5628 {
5629 qi = ll_get_or_alloc_list(curwin);
5630 if (qi == NULL)
5631 return;
5632 }
5633
Bram Moolenaar86b68352004-12-27 21:59:20 +00005634 if (*eap->arg == NUL)
5635 buf = curbuf;
5636 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5637 buf = buflist_findnr(atoi((char *)eap->arg));
5638 if (buf == NULL)
5639 EMSG(_(e_invarg));
5640 else if (buf->b_ml.ml_mfp == NULL)
5641 EMSG(_("E681: Buffer is not loaded"));
5642 else
5643 {
5644 if (eap->addr_count == 0)
5645 {
5646 eap->line1 = 1;
5647 eap->line2 = buf->b_ml.ml_line_count;
5648 }
5649 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5650 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5651 EMSG(_(e_invrange));
5652 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005653 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005654 char_u *qf_title = *eap->cmdlinep;
5655
5656 if (buf->b_sfname)
5657 {
5658 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5659 (char *)qf_title, (char *)buf->b_sfname);
5660 qf_title = IObuff;
5661 }
5662
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005663 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005664 (eap->cmdidx != CMD_caddbuffer
5665 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005666 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005667 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005668 if (res >= 0)
5669 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005670#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005671 if (au_name != NULL)
5672 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5673 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005674#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005675 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5676 eap->cmdidx == CMD_lbuffer))
5677 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005678 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005679 }
5680}
5681
Bram Moolenaar1e015462005-09-25 22:16:38 +00005682#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005683/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005684 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5685 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005686 */
5687 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005688ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005689{
5690 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005691 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005692#ifdef FEAT_AUTOCMD
5693 char_u *au_name = NULL;
5694#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005695 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005696
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005697#ifdef FEAT_AUTOCMD
5698 switch (eap->cmdidx)
5699 {
5700 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5701 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5702 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5703 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5704 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5705 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5706 default: break;
5707 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005708 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5709 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005710 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005711# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005712 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005713 return;
5714# endif
5715 }
5716#endif
5717
Bram Moolenaar3c097222017-12-21 20:54:49 +01005718 if (eap->cmdidx == CMD_lexpr
5719 || eap->cmdidx == CMD_lgetexpr
5720 || eap->cmdidx == CMD_laddexpr)
5721 {
5722 qi = ll_get_or_alloc_list(curwin);
5723 if (qi == NULL)
5724 return;
5725 }
5726
Bram Moolenaar4770d092006-01-12 23:22:24 +00005727 /* Evaluate the expression. When the result is a string or a list we can
5728 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005729 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005730 if (tv != NULL)
5731 {
5732 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5733 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5734 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005735 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005736 (eap->cmdidx != CMD_caddexpr
5737 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005738 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005739 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005740 if (res >= 0)
5741 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005742#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005743 if (au_name != NULL)
5744 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5745 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005746#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005747 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5748 eap->cmdidx == CMD_lexpr))
5749 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005750 }
5751 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005752 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005753 free_tv(tv);
5754 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005755}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005756#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005757
5758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 * ":helpgrep {pattern}"
5760 */
5761 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005762ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
5764 regmatch_T regmatch;
5765 char_u *save_cpo;
5766 char_u *p;
5767 int fcount;
5768 char_u **fnames;
5769 FILE *fd;
5770 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005772#ifdef FEAT_MULTI_LANG
5773 char_u *lang;
5774#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005775 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005776 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005777 int new_qi = FALSE;
5778 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005779#ifdef FEAT_AUTOCMD
5780 char_u *au_name = NULL;
5781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005783#ifdef FEAT_MULTI_LANG
5784 /* Check for a specified language */
5785 lang = check_help_lang(eap->arg);
5786#endif
5787
Bram Moolenaar73633f82012-01-20 13:39:07 +01005788#ifdef FEAT_AUTOCMD
5789 switch (eap->cmdidx)
5790 {
5791 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5792 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5793 default: break;
5794 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005795 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5796 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005797 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005798# ifdef FEAT_EVAL
5799 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005800 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005801# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005802 }
5803#endif
5804
5805 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5806 save_cpo = p_cpo;
5807 p_cpo = empty_option;
5808
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005809 if (eap->cmdidx == CMD_lhelpgrep)
5810 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005811 /* If the current window is a help window, then use it */
5812 if (bt_help(curwin->w_buffer))
5813 wp = curwin;
5814 else
5815 /* Find an existing help window */
5816 FOR_ALL_WINDOWS(wp)
5817 if (bt_help(wp->w_buffer))
5818 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005819
5820 if (wp == NULL) /* Help window not found */
5821 qi = NULL;
5822 else
5823 qi = wp->w_llist;
5824
5825 if (qi == NULL)
5826 {
5827 /* Allocate a new location list for help text matches */
5828 if ((qi = ll_new_list()) == NULL)
5829 return;
5830 new_qi = TRUE;
5831 }
5832 }
5833
Bram Moolenaaree85df32017-03-19 14:19:50 +01005834 /* Autocommands may change the list. Save it for later comparison */
5835 save_qi = qi;
5836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5838 regmatch.rm_ic = FALSE;
5839 if (regmatch.regprog != NULL)
5840 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005841#ifdef FEAT_MBYTE
5842 vimconv_T vc;
5843
5844 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5845 * differs. */
5846 vc.vc_type = CONV_NONE;
5847 if (!enc_utf8)
5848 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5849#endif
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005852 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
5854 /* Go through all directories in 'runtimepath' */
5855 p = p_rtp;
5856 while (*p != NUL && !got_int)
5857 {
5858 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5859
5860 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5861 add_pathsep(NameBuff);
5862 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5863 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5864 &fnames, EW_FILE|EW_SILENT) == OK
5865 && fcount > 0)
5866 {
5867 for (fi = 0; fi < fcount && !got_int; ++fi)
5868 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005869#ifdef FEAT_MULTI_LANG
5870 /* Skip files for a different language. */
5871 if (lang != NULL
5872 && STRNICMP(lang, fnames[fi]
5873 + STRLEN(fnames[fi]) - 3, 2) != 0
5874 && !(STRNICMP(lang, "en", 2) == 0
5875 && STRNICMP("txt", fnames[fi]
5876 + STRLEN(fnames[fi]) - 3, 3) == 0))
5877 continue;
5878#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005879 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 if (fd != NULL)
5881 {
5882 lnum = 1;
5883 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5884 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005885 char_u *line = IObuff;
5886#ifdef FEAT_MBYTE
5887 /* Convert a line if 'encoding' is not utf-8 and
5888 * the line contains a non-ASCII character. */
5889 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005890 && has_non_ascii(IObuff))
5891 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005892 line = string_convert(&vc, IObuff, NULL);
5893 if (line == NULL)
5894 line = IObuff;
5895 }
5896#endif
5897
5898 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005900 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005903 while (l > 0 && line[l - 1] <= ' ')
5904 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005906 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005907 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 NULL, /* dir */
5909 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005910 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005911 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005913 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005914 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005915 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005916 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 0, /* nr */
5918 1, /* type */
5919 TRUE /* valid */
5920 ) == FAIL)
5921 {
5922 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005923#ifdef FEAT_MBYTE
5924 if (line != IObuff)
5925 vim_free(line);
5926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 break;
5928 }
5929 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005930#ifdef FEAT_MBYTE
5931 if (line != IObuff)
5932 vim_free(line);
5933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 ++lnum;
5935 line_breakcheck();
5936 }
5937 fclose(fd);
5938 }
5939 }
5940 FreeWild(fcount, fnames);
5941 }
5942 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005943
Bram Moolenaar473de612013-06-08 18:19:48 +02005944 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005945#ifdef FEAT_MBYTE
5946 if (vc.vc_type != CONV_NONE)
5947 convert_setup(&vc, NULL, NULL);
5948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005950 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5951 qi->qf_lists[qi->qf_curlist].qf_ptr =
5952 qi->qf_lists[qi->qf_curlist].qf_start;
5953 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 }
5955
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005956 if (p_cpo == empty_option)
5957 p_cpo = save_cpo;
5958 else
5959 /* Darn, some plugin changed the value. */
5960 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961
Bram Moolenaarb254af32017-12-18 19:48:58 +01005962 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005963 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Bram Moolenaar73633f82012-01-20 13:39:07 +01005965#ifdef FEAT_AUTOCMD
5966 if (au_name != NULL)
5967 {
5968 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5969 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005970 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005971 /* autocommands made "qi" invalid */
5972 return;
5973 }
5974#endif
5975
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005977 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005978 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005979 else
5980 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005981
5982 if (eap->cmdidx == CMD_lhelpgrep)
5983 {
5984 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005985 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005986 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005987 {
5988 if (new_qi)
5989 ll_free_all(&qi);
5990 }
5991 else if (curwin->w_llist == NULL)
5992 curwin->w_llist = qi;
5993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994}
5995
5996#endif /* FEAT_QUICKFIX */