blob: 66d33478f242ec74c59e554c63672999f2c59411 [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 Moolenaard12f5c12006-01-25 22:10:52 +000079} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020081/*
82 * Quickfix/Location list stack definition
83 * Contains a list of quickfix/location lists (qf_list_T)
84 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000085struct qf_info_S
86{
87 /*
88 * Count of references to this list. Used only for location lists.
89 * When a location list window reference this list, qf_refcount
90 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
91 * reaches 0, the list is freed.
92 */
93 int qf_refcount;
94 int qf_listcount; /* current number of lists */
95 int qf_curlist; /* current error list */
96 qf_list_T qf_lists[LISTCOUNT];
97};
98
99static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200100static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000102#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
104/*
105 * Structure used to hold the info of one part of 'errorformat'
106 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000107typedef struct efm_S efm_T;
108struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109{
110 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000111 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
113 char_u prefix; /* prefix of this format line: */
114 /* 'D' enter directory */
115 /* 'X' leave directory */
116 /* 'A' start of multi-line message */
117 /* 'E' error message */
118 /* 'W' warning message */
119 /* 'I' informational message */
120 /* 'C' continuation line */
121 /* 'Z' end of multi-line message */
122 /* 'G' general, unspecific message */
123 /* 'P' push file (partial) message */
124 /* 'Q' pop/quit file (partial) message */
125 /* 'O' overread (partial) message */
126 char_u flags; /* additional flags given in prefix */
127 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000128 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000129 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130};
131
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100132static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
133
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200134static 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 +0200135static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void qf_new_list(qf_info_T *qi, char_u *qf_title);
137static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200138static 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 +0100139static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void qf_free(qf_info_T *qi, int idx);
141static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200142static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200143static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200145static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
147static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
149static int is_qf_win(win_T *win, qf_info_T *qi);
150static win_T *qf_find_win(qf_info_T *qi);
151static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200152static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200154static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static char_u *get_mef_name(void);
156static void restore_start_dir(char_u *dirname_start);
157static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
158static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
159static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
160static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162/* Quickfix window check helper macro */
163#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
164/* Location list window check helper macro */
165#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
166/*
167 * Return location list for window 'wp'
168 * For location list window, return the referenced location list
169 */
170#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200173 * Looking up a buffer can be slow if there are many. Remember the last one
174 * to make this a lot faster if there are multiple matches in the same file.
175 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200176static char_u *qf_last_bufname = NULL;
177static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178
179/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000180 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200181 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 * Return -1 for error, number of errors for success.
183 */
184 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200185qf_init(win_T *wp,
186 char_u *efile,
187 char_u *errorformat,
188 int newlist, /* TRUE: start a new error list */
189 char_u *qf_title,
190 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192 qf_info_T *qi = &ql_info;
193
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000194 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000195 {
196 qi = ll_get_or_alloc_list(wp);
197 if (qi == NULL)
198 return FAIL;
199 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000200
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200201 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
202 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000203}
204
205/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200206 * Maximum number of bytes allowed per line while reading a errorfile.
207 */
208#define LINE_MAXLEN 4096
209
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200210static struct fmtpattern
211{
212 char_u convchar;
213 char *pattern;
214} fmt_pat[FMT_PATTERNS] =
215 {
216 {'f', ".\\+"}, /* only used when at end */
217 {'n', "\\d\\+"},
218 {'l', "\\d\\+"},
219 {'c', "\\d\\+"},
220 {'t', "."},
221 {'m', ".\\+"},
222 {'r', ".*"},
223 {'p', "[- .]*"},
224 {'v', "\\d\\+"},
225 {'s', ".\\+"}
226 };
227
228/*
229 * Converts a 'errorformat' string to regular expression pattern
230 */
231 static int
232efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200233 char_u *efm,
234 int len,
235 efm_T *fmt_ptr,
236 char_u *regpat,
237 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200238{
239 char_u *ptr;
240 char_u *efmp;
241 char_u *srcptr;
242 int round;
243 int idx = 0;
244
245 /*
246 * Build regexp pattern from current 'errorformat' option
247 */
248 ptr = regpat;
249 *ptr++ = '^';
250 round = 0;
251 for (efmp = efm; efmp < efm + len; ++efmp)
252 {
253 if (*efmp == '%')
254 {
255 ++efmp;
256 for (idx = 0; idx < FMT_PATTERNS; ++idx)
257 if (fmt_pat[idx].convchar == *efmp)
258 break;
259 if (idx < FMT_PATTERNS)
260 {
261 if (fmt_ptr->addr[idx])
262 {
263 sprintf((char *)errmsg,
264 _("E372: Too many %%%c in format string"), *efmp);
265 EMSG(errmsg);
266 return -1;
267 }
268 if ((idx
269 && idx < 6
270 && vim_strchr((char_u *)"DXOPQ",
271 fmt_ptr->prefix) != NULL)
272 || (idx == 6
273 && vim_strchr((char_u *)"OPQ",
274 fmt_ptr->prefix) == NULL))
275 {
276 sprintf((char *)errmsg,
277 _("E373: Unexpected %%%c in format string"), *efmp);
278 EMSG(errmsg);
279 return -1;
280 }
281 fmt_ptr->addr[idx] = (char_u)++round;
282 *ptr++ = '\\';
283 *ptr++ = '(';
284#ifdef BACKSLASH_IN_FILENAME
285 if (*efmp == 'f')
286 {
287 /* Also match "c:" in the file name, even when
288 * checking for a colon next: "%f:".
289 * "\%(\a:\)\=" */
290 STRCPY(ptr, "\\%(\\a:\\)\\=");
291 ptr += 10;
292 }
293#endif
294 if (*efmp == 'f' && efmp[1] != NUL)
295 {
296 if (efmp[1] != '\\' && efmp[1] != '%')
297 {
298 /* A file name may contain spaces, but this isn't
299 * in "\f". For "%f:%l:%m" there may be a ":" in
300 * the file name. Use ".\{-1,}x" instead (x is
301 * the next character), the requirement that :999:
302 * follows should work. */
303 STRCPY(ptr, ".\\{-1,}");
304 ptr += 7;
305 }
306 else
307 {
308 /* File name followed by '\\' or '%': include as
309 * many file name chars as possible. */
310 STRCPY(ptr, "\\f\\+");
311 ptr += 4;
312 }
313 }
314 else
315 {
316 srcptr = (char_u *)fmt_pat[idx].pattern;
317 while ((*ptr = *srcptr++) != NUL)
318 ++ptr;
319 }
320 *ptr++ = '\\';
321 *ptr++ = ')';
322 }
323 else if (*efmp == '*')
324 {
325 if (*++efmp == '[' || *efmp == '\\')
326 {
327 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
328 {
329 if (efmp[1] == '^')
330 *ptr++ = *++efmp;
331 if (efmp < efm + len)
332 {
333 *ptr++ = *++efmp; /* could be ']' */
334 while (efmp < efm + len
335 && (*ptr++ = *++efmp) != ']')
336 /* skip */;
337 if (efmp == efm + len)
338 {
339 EMSG(_("E374: Missing ] in format string"));
340 return -1;
341 }
342 }
343 }
344 else if (efmp < efm + len) /* %*\D, %*\s etc. */
345 *ptr++ = *++efmp;
346 *ptr++ = '\\';
347 *ptr++ = '+';
348 }
349 else
350 {
351 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
352 sprintf((char *)errmsg,
353 _("E375: Unsupported %%%c in format string"), *efmp);
354 EMSG(errmsg);
355 return -1;
356 }
357 }
358 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
359 *ptr++ = *efmp; /* regexp magic characters */
360 else if (*efmp == '#')
361 *ptr++ = '*';
362 else if (*efmp == '>')
363 fmt_ptr->conthere = TRUE;
364 else if (efmp == efm + 1) /* analyse prefix */
365 {
366 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
367 fmt_ptr->flags = *efmp++;
368 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
369 fmt_ptr->prefix = *efmp;
370 else
371 {
372 sprintf((char *)errmsg,
373 _("E376: Invalid %%%c in format string prefix"), *efmp);
374 EMSG(errmsg);
375 return -1;
376 }
377 }
378 else
379 {
380 sprintf((char *)errmsg,
381 _("E377: Invalid %%%c in format string"), *efmp);
382 EMSG(errmsg);
383 return -1;
384 }
385 }
386 else /* copy normal character */
387 {
388 if (*efmp == '\\' && efmp + 1 < efm + len)
389 ++efmp;
390 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
391 *ptr++ = '\\'; /* escape regexp atoms */
392 if (*efmp)
393 *ptr++ = *efmp;
394 }
395 }
396 *ptr++ = '$';
397 *ptr = NUL;
398
399 return 0;
400}
401
402 static void
403free_efm_list(efm_T **efm_first)
404{
405 efm_T *efm_ptr;
406
407 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
408 {
409 *efm_first = efm_ptr->next;
410 vim_regfree(efm_ptr->prog);
411 vim_free(efm_ptr);
412 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100413 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200414}
415
416/* Parse 'errorformat' option */
417 static efm_T *
418parse_efm_option(char_u *efm)
419{
420 char_u *errmsg = NULL;
421 int errmsglen;
422 efm_T *fmt_ptr = NULL;
423 efm_T *fmt_first = NULL;
424 efm_T *fmt_last = NULL;
425 char_u *fmtstr = NULL;
426 int len;
427 int i;
428 int round;
429
430 errmsglen = CMDBUFFSIZE + 1;
431 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
432 if (errmsg == NULL)
433 goto parse_efm_end;
434
435 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200436 * Each part of the format string is copied and modified from errorformat
437 * to regex prog. Only a few % characters are allowed.
438 */
439
440 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200441 * Get some space to modify the format string into.
442 */
443 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
444 for (round = FMT_PATTERNS; round > 0; )
445 i += (int)STRLEN(fmt_pat[--round].pattern);
446#ifdef COLON_IN_FILENAME
447 i += 12; /* "%f" can become twelve chars longer */
448#else
449 i += 2; /* "%f" can become two chars longer */
450#endif
451 if ((fmtstr = alloc(i)) == NULL)
452 goto parse_efm_error;
453
454 while (efm[0] != NUL)
455 {
456 /*
457 * Allocate a new eformat structure and put it at the end of the list
458 */
459 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
460 if (fmt_ptr == NULL)
461 goto parse_efm_error;
462 if (fmt_first == NULL) /* first one */
463 fmt_first = fmt_ptr;
464 else
465 fmt_last->next = fmt_ptr;
466 fmt_last = fmt_ptr;
467
468 /*
469 * Isolate one part in the 'errorformat' option
470 */
471 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
472 if (efm[len] == '\\' && efm[len + 1] != NUL)
473 ++len;
474
475 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
476 goto parse_efm_error;
477 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
478 goto parse_efm_error;
479 /*
480 * Advance to next part
481 */
482 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
483 }
484
485 if (fmt_first == NULL) /* nothing found */
486 EMSG(_("E378: 'errorformat' contains no pattern"));
487
488 goto parse_efm_end;
489
490parse_efm_error:
491 free_efm_list(&fmt_first);
492
493parse_efm_end:
494 vim_free(fmtstr);
495 vim_free(errmsg);
496
497 return fmt_first;
498}
499
Bram Moolenaare0d37972016-07-15 22:36:01 +0200500enum {
501 QF_FAIL = 0,
502 QF_OK = 1,
503 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200504 QF_NOMEM = 3,
505 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200506};
507
508typedef struct {
509 char_u *linebuf;
510 int linelen;
511 char_u *growbuf;
512 int growbufsiz;
513 FILE *fd;
514 typval_T *tv;
515 char_u *p_str;
516 listitem_T *p_li;
517 buf_T *buf;
518 linenr_T buflnum;
519 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100520 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200521} qfstate_T;
522
523 static char_u *
524qf_grow_linebuf(qfstate_T *state, int newsz)
525{
526 /*
527 * If the line exceeds LINE_MAXLEN exclude the last
528 * byte since it's not a NL character.
529 */
530 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
531 if (state->growbuf == NULL)
532 {
533 state->growbuf = alloc(state->linelen + 1);
534 if (state->growbuf == NULL)
535 return NULL;
536 state->growbufsiz = state->linelen;
537 }
538 else if (state->linelen > state->growbufsiz)
539 {
540 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
541 if (state->growbuf == NULL)
542 return NULL;
543 state->growbufsiz = state->linelen;
544 }
545 return state->growbuf;
546}
547
548/*
549 * Get the next string (separated by newline) from state->p_str.
550 */
551 static int
552qf_get_next_str_line(qfstate_T *state)
553{
554 /* Get the next line from the supplied string */
555 char_u *p_str = state->p_str;
556 char_u *p;
557 int len;
558
559 if (*p_str == NUL) /* Reached the end of the string */
560 return QF_END_OF_INPUT;
561
562 p = vim_strchr(p_str, '\n');
563 if (p != NULL)
564 len = (int)(p - p_str) + 1;
565 else
566 len = (int)STRLEN(p_str);
567
568 if (len > IOSIZE - 2)
569 {
570 state->linebuf = qf_grow_linebuf(state, len);
571 if (state->linebuf == NULL)
572 return QF_NOMEM;
573 }
574 else
575 {
576 state->linebuf = IObuff;
577 state->linelen = len;
578 }
579 vim_strncpy(state->linebuf, p_str, state->linelen);
580
581 /*
582 * Increment using len in order to discard the rest of the
583 * line if it exceeds LINE_MAXLEN.
584 */
585 p_str += len;
586 state->p_str = p_str;
587
588 return QF_OK;
589}
590
591/*
592 * Get the next string from state->p_Li.
593 */
594 static int
595qf_get_next_list_line(qfstate_T *state)
596{
597 listitem_T *p_li = state->p_li;
598 int len;
599
600 while (p_li != NULL
601 && (p_li->li_tv.v_type != VAR_STRING
602 || p_li->li_tv.vval.v_string == NULL))
603 p_li = p_li->li_next; /* Skip non-string items */
604
605 if (p_li == NULL) /* End of the list */
606 {
607 state->p_li = NULL;
608 return QF_END_OF_INPUT;
609 }
610
611 len = (int)STRLEN(p_li->li_tv.vval.v_string);
612 if (len > IOSIZE - 2)
613 {
614 state->linebuf = qf_grow_linebuf(state, len);
615 if (state->linebuf == NULL)
616 return QF_NOMEM;
617 }
618 else
619 {
620 state->linebuf = IObuff;
621 state->linelen = len;
622 }
623
624 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
625
626 state->p_li = p_li->li_next; /* next item */
627 return QF_OK;
628}
629
630/*
631 * Get the next string from state->buf.
632 */
633 static int
634qf_get_next_buf_line(qfstate_T *state)
635{
636 char_u *p_buf = NULL;
637 int len;
638
639 /* Get the next line from the supplied buffer */
640 if (state->buflnum > state->lnumlast)
641 return QF_END_OF_INPUT;
642
643 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
644 state->buflnum += 1;
645
646 len = (int)STRLEN(p_buf);
647 if (len > IOSIZE - 2)
648 {
649 state->linebuf = qf_grow_linebuf(state, len);
650 if (state->linebuf == NULL)
651 return QF_NOMEM;
652 }
653 else
654 {
655 state->linebuf = IObuff;
656 state->linelen = len;
657 }
658 vim_strncpy(state->linebuf, p_buf, state->linelen);
659
660 return QF_OK;
661}
662
663/*
664 * Get the next string from file state->fd.
665 */
666 static int
667qf_get_next_file_line(qfstate_T *state)
668{
669 int discard;
670 int growbuflen;
671
672 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
673 return QF_END_OF_INPUT;
674
675 discard = FALSE;
676 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200677 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200678 {
679 /*
680 * The current line exceeds IObuff, continue reading using
681 * growbuf until EOL or LINE_MAXLEN bytes is read.
682 */
683 if (state->growbuf == NULL)
684 {
685 state->growbufsiz = 2 * (IOSIZE - 1);
686 state->growbuf = alloc(state->growbufsiz);
687 if (state->growbuf == NULL)
688 return QF_NOMEM;
689 }
690
691 /* Copy the read part of the line, excluding null-terminator */
692 memcpy(state->growbuf, IObuff, IOSIZE - 1);
693 growbuflen = state->linelen;
694
695 for (;;)
696 {
697 if (fgets((char *)state->growbuf + growbuflen,
698 state->growbufsiz - growbuflen, state->fd) == NULL)
699 break;
700 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
701 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200702 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200703 break;
704 if (state->growbufsiz == LINE_MAXLEN)
705 {
706 discard = TRUE;
707 break;
708 }
709
710 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
711 ? 2 * state->growbufsiz : LINE_MAXLEN;
712 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
713 if (state->growbuf == NULL)
714 return QF_NOMEM;
715 }
716
717 while (discard)
718 {
719 /*
720 * The current line is longer than LINE_MAXLEN, continue
721 * reading but discard everything until EOL or EOF is
722 * reached.
723 */
724 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
725 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200726 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200727 break;
728 }
729
730 state->linebuf = state->growbuf;
731 state->linelen = growbuflen;
732 }
733 else
734 state->linebuf = IObuff;
735
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100736#ifdef FEAT_MBYTE
737 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200738 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
739 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100740 char_u *line;
741
742 line = string_convert(&state->vc, state->linebuf, &state->linelen);
743 if (line != NULL)
744 {
745 if (state->linelen < IOSIZE)
746 {
747 STRCPY(state->linebuf, line);
748 vim_free(line);
749 }
750 else
751 {
752 vim_free(state->growbuf);
753 state->linebuf = state->growbuf = line;
754 state->growbufsiz = state->linelen < LINE_MAXLEN
755 ? state->linelen : LINE_MAXLEN;
756 }
757 }
758 }
759#endif
760
Bram Moolenaare0d37972016-07-15 22:36:01 +0200761 return QF_OK;
762}
763
764/*
765 * Get the next string from a file/buffer/list/string.
766 */
767 static int
768qf_get_nextline(qfstate_T *state)
769{
770 int status = QF_FAIL;
771
772 if (state->fd == NULL)
773 {
774 if (state->tv != NULL)
775 {
776 if (state->tv->v_type == VAR_STRING)
777 /* Get the next line from the supplied string */
778 status = qf_get_next_str_line(state);
779 else if (state->tv->v_type == VAR_LIST)
780 /* Get the next line from the supplied list */
781 status = qf_get_next_list_line(state);
782 }
783 else
784 /* Get the next line from the supplied buffer */
785 status = qf_get_next_buf_line(state);
786 }
787 else
788 /* Get the next line from the supplied file */
789 status = qf_get_next_file_line(state);
790
791 if (status != QF_OK)
792 return status;
793
794 /* remove newline/CR from the line */
795 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200796 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200797 state->linebuf[state->linelen - 1] = NUL;
798#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200799 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
800 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200801#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200802 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200803
804#ifdef FEAT_MBYTE
805 remove_bom(state->linebuf);
806#endif
807
808 return QF_OK;
809}
810
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200811typedef struct {
812 char_u *namebuf;
813 char_u *errmsg;
814 int errmsglen;
815 long lnum;
816 int col;
817 char_u use_viscol;
818 char_u *pattern;
819 int enr;
820 int type;
821 int valid;
822} qffields_T;
823
824/*
825 * Parse a line and get the quickfix fields.
826 * Return the QF_ status.
827 */
828 static int
829qf_parse_line(
830 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200831 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200832 char_u *linebuf,
833 int linelen,
834 efm_T *fmt_first,
835 qffields_T *fields)
836{
837 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200838 char_u *ptr;
839 int len;
840 int i;
841 int idx = 0;
842 char_u *tail = NULL;
843 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200844 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200845
846 /* Always ignore case when looking for a matching error. */
847 regmatch.rm_ic = TRUE;
848
849 /* If there was no %> item start at the first pattern */
850 if (fmt_start == NULL)
851 fmt_ptr = fmt_first;
852 else
853 {
854 fmt_ptr = fmt_start;
855 fmt_start = NULL;
856 }
857
858 /*
859 * Try to match each part of 'errorformat' until we find a complete
860 * match or no match.
861 */
862 fields->valid = TRUE;
863restofline:
864 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
865 {
866 int r;
867
868 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200869 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200870 continue;
871 fields->namebuf[0] = NUL;
872 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200873 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200874 fields->errmsg[0] = NUL;
875 fields->lnum = 0;
876 fields->col = 0;
877 fields->use_viscol = FALSE;
878 fields->enr = -1;
879 fields->type = 0;
880 tail = NULL;
881
882 regmatch.regprog = fmt_ptr->prog;
883 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
884 fmt_ptr->prog = regmatch.regprog;
885 if (r)
886 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200887 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200888 continue;
889 if (vim_strchr((char_u *)"EWI", idx) != NULL)
890 fields->type = idx;
891 else
892 fields->type = 0;
893 /*
894 * Extract error message data from matched line.
895 * We check for an actual submatch, because "\[" and "\]" in
896 * the 'errorformat' may cause the wrong submatch to be used.
897 */
898 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
899 {
900 int c;
901
902 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
903 continue;
904
905 /* Expand ~/file and $HOME/file to full path. */
906 c = *regmatch.endp[i];
907 *regmatch.endp[i] = NUL;
908 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
909 *regmatch.endp[i] = c;
910
911 if (vim_strchr((char_u *)"OPQ", idx) != NULL
912 && mch_getperm(fields->namebuf) == -1)
913 continue;
914 }
915 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
916 {
917 if (regmatch.startp[i] == NULL)
918 continue;
919 fields->enr = (int)atol((char *)regmatch.startp[i]);
920 }
921 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
922 {
923 if (regmatch.startp[i] == NULL)
924 continue;
925 fields->lnum = atol((char *)regmatch.startp[i]);
926 }
927 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
928 {
929 if (regmatch.startp[i] == NULL)
930 continue;
931 fields->col = (int)atol((char *)regmatch.startp[i]);
932 }
933 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
934 {
935 if (regmatch.startp[i] == NULL)
936 continue;
937 fields->type = *regmatch.startp[i];
938 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200939 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200940 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200941 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200942 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200943 /* linelen + null terminator */
944 if ((fields->errmsg = vim_realloc(fields->errmsg,
945 linelen + 1)) == NULL)
946 return QF_NOMEM;
947 fields->errmsglen = linelen + 1;
948 }
949 vim_strncpy(fields->errmsg, linebuf, linelen);
950 }
951 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
952 {
953 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
954 continue;
955 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200956 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200957 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200958 /* len + null terminator */
959 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
960 == NULL)
961 return QF_NOMEM;
962 fields->errmsglen = len + 1;
963 }
964 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
965 }
966 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
967 {
968 if (regmatch.startp[i] == NULL)
969 continue;
970 tail = regmatch.startp[i];
971 }
972 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
973 {
974 char_u *match_ptr;
975
976 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
977 continue;
978 fields->col = 0;
979 for (match_ptr = regmatch.startp[i];
980 match_ptr != regmatch.endp[i]; ++match_ptr)
981 {
982 ++fields->col;
983 if (*match_ptr == TAB)
984 {
985 fields->col += 7;
986 fields->col -= fields->col % 8;
987 }
988 }
989 ++fields->col;
990 fields->use_viscol = TRUE;
991 }
992 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
993 {
994 if (regmatch.startp[i] == NULL)
995 continue;
996 fields->col = (int)atol((char *)regmatch.startp[i]);
997 fields->use_viscol = TRUE;
998 }
999 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1000 {
1001 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1002 continue;
1003 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1004 if (len > CMDBUFFSIZE - 5)
1005 len = CMDBUFFSIZE - 5;
1006 STRCPY(fields->pattern, "^\\V");
1007 STRNCAT(fields->pattern, regmatch.startp[i], len);
1008 fields->pattern[len + 3] = '\\';
1009 fields->pattern[len + 4] = '$';
1010 fields->pattern[len + 5] = NUL;
1011 }
1012 break;
1013 }
1014 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001015 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001016
1017 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1018 {
1019 if (fmt_ptr != NULL)
1020 {
1021 if (idx == 'D') /* enter directory */
1022 {
1023 if (*fields->namebuf == NUL)
1024 {
1025 EMSG(_("E379: Missing or empty directory name"));
1026 return QF_FAIL;
1027 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001028 qfl->qf_directory =
1029 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1030 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001031 return QF_FAIL;
1032 }
1033 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001034 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001035 }
1036 fields->namebuf[0] = NUL; /* no match found, remove file name */
1037 fields->lnum = 0; /* don't jump to this line */
1038 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001039 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001040 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001041 /* linelen + null terminator */
1042 if ((fields->errmsg = vim_realloc(fields->errmsg,
1043 linelen + 1)) == NULL)
1044 return QF_NOMEM;
1045 fields->errmsglen = linelen + 1;
1046 }
1047 /* copy whole line to error message */
1048 vim_strncpy(fields->errmsg, linebuf, linelen);
1049 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001050 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001051 }
1052 else if (fmt_ptr != NULL)
1053 {
1054 /* honor %> item */
1055 if (fmt_ptr->conthere)
1056 fmt_start = fmt_ptr;
1057
1058 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1059 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001060 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1061 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001062 }
1063 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1064 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001065 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001066 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001067 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001068
Bram Moolenaar9b457942016-10-09 16:10:05 +02001069 if (qfprev == NULL)
1070 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001071 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001072 {
1073 len = (int)STRLEN(qfprev->qf_text);
1074 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1075 == NULL)
1076 return QF_FAIL;
1077 STRCPY(ptr, qfprev->qf_text);
1078 vim_free(qfprev->qf_text);
1079 qfprev->qf_text = ptr;
1080 *(ptr += len) = '\n';
1081 STRCPY(++ptr, fields->errmsg);
1082 }
1083 if (qfprev->qf_nr == -1)
1084 qfprev->qf_nr = fields->enr;
1085 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1086 /* only printable chars allowed */
1087 qfprev->qf_type = fields->type;
1088
1089 if (!qfprev->qf_lnum)
1090 qfprev->qf_lnum = fields->lnum;
1091 if (!qfprev->qf_col)
1092 qfprev->qf_col = fields->col;
1093 qfprev->qf_viscol = fields->use_viscol;
1094 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001095 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1096 qfl->qf_directory,
1097 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001098 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001099 : qfl->qf_currfile != NULL && fields->valid
1100 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001101 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001102 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001103 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001104 line_breakcheck();
1105 return QF_IGNORE_LINE;
1106 }
1107 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1108 {
1109 /* global file names */
1110 fields->valid = FALSE;
1111 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1112 {
1113 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001114 qfl->qf_currfile =
1115 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001116 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001117 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001118 *fields->namebuf = NUL;
1119 if (tail && *tail)
1120 {
1121 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001122 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001123 goto restofline;
1124 }
1125 }
1126 }
1127 if (fmt_ptr->flags == '-') /* generally exclude this line */
1128 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001129 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001130 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001131 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001132 return QF_IGNORE_LINE;
1133 }
1134 }
1135
1136 return QF_OK;
1137}
1138
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001139/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001140 * Read the errorfile "efile" into memory, line by line, building the error
1141 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001142 * Alternative: when "efile" is NULL read errors from buffer "buf".
1143 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001144 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001145 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1146 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001147 * Return -1 for error, number of errors for success.
1148 */
1149 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001150qf_init_ext(
1151 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001152 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001153 char_u *efile,
1154 buf_T *buf,
1155 typval_T *tv,
1156 char_u *errorformat,
1157 int newlist, /* TRUE: start a new error list */
1158 linenr_T lnumfirst, /* first line number to use */
1159 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001160 char_u *qf_title,
1161 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001162{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001163 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001164 qfstate_T state;
1165 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001166 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001167 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001168 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001170 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001172 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001174 /* Do not used the cached buffer, it may have been wiped out. */
1175 vim_free(qf_last_bufname);
1176 qf_last_bufname = NULL;
1177
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001178 vim_memset(&state, 0, sizeof(state));
1179 vim_memset(&fields, 0, sizeof(fields));
1180#ifdef FEAT_MBYTE
1181 state.vc.vc_type = CONV_NONE;
1182 if (enc != NULL && *enc != NUL)
1183 convert_setup(&state.vc, enc, p_enc);
1184#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001185 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1186 fields.errmsglen = CMDBUFFSIZE + 1;
1187 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1188 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001189 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 goto qf_init_end;
1191
Bram Moolenaare0d37972016-07-15 22:36:01 +02001192 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
1194 EMSG2(_(e_openerrf), efile);
1195 goto qf_init_end;
1196 }
1197
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001198 if (newlist || qf_idx == qi->qf_listcount)
1199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001201 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001202 qf_idx = qi->qf_curlist;
1203 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001204 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001205 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001206 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001207 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001208 if (qi->qf_lists[qf_idx].qf_count > 0)
1209 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001212 qfl = &qi->qf_lists[qf_idx];
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001215 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001216 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 else
1218 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001220 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001221 * If the errorformat didn't change between calls, then reuse the
1222 * previously parsed values.
1223 */
1224 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1225 {
1226 /* free the previously parsed data */
1227 vim_free(last_efm);
1228 last_efm = NULL;
1229 free_efm_list(&fmt_first);
1230
1231 /* parse the current 'efm' */
1232 fmt_first = parse_efm_option(efm);
1233 if (fmt_first != NULL)
1234 last_efm = vim_strsave(efm);
1235 }
1236
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
1240 /*
1241 * got_int is reset here, because it was probably set when killing the
1242 * ":make" command, but we still want to read the errorfile then.
1243 */
1244 got_int = FALSE;
1245
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001246 if (tv != NULL)
1247 {
1248 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001249 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001250 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001251 state.p_li = tv->vval.v_list->lv_first;
1252 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001253 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001254 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001255 state.buflnum = lnumfirst;
1256 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 /*
1259 * Read the lines in the error file one by one.
1260 * Try to recognize one of the error formats in each line.
1261 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001262 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001264 /* Get the next line from a file/buffer/list/string */
1265 status = qf_get_nextline(&state);
1266 if (status == QF_NOMEM) /* memory alloc failure */
1267 goto qf_init_end;
1268 if (status == QF_END_OF_INPUT) /* end of input */
1269 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001271 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1272 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001273 if (status == QF_FAIL)
1274 goto error2;
1275 if (status == QF_NOMEM)
1276 goto qf_init_end;
1277 if (status == QF_IGNORE_LINE)
1278 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001280 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001281 qf_idx,
1282 qfl->qf_directory,
1283 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001284 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001285 : ((qfl->qf_currfile != NULL && fields.valid)
1286 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001287 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001288 fields.errmsg,
1289 fields.lnum,
1290 fields.col,
1291 fields.use_viscol,
1292 fields.pattern,
1293 fields.enr,
1294 fields.type,
1295 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 goto error2;
1297 line_breakcheck();
1298 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001299 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001301 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001303 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001304 qfl->qf_ptr = qfl->qf_start;
1305 qfl->qf_index = 1;
1306 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308 else
1309 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001310 qfl->qf_nonevalid = FALSE;
1311 if (qfl->qf_ptr == NULL)
1312 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001314 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001315 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001316 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318 EMSG(_(e_readerrf));
1319error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001320 if (!adding)
1321 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001322 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001323 qf_free(qi, qi->qf_curlist);
1324 qi->qf_listcount--;
1325 if (qi->qf_curlist > 0)
1326 --qi->qf_curlist;
1327 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001328qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001329 if (state.fd != NULL)
1330 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001331 vim_free(fields.namebuf);
1332 vim_free(fields.errmsg);
1333 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001334 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001336 if (qf_idx == qi->qf_curlist)
1337 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001338#ifdef FEAT_MBYTE
1339 if (state.vc.vc_type != CONV_NONE)
1340 convert_setup(&state.vc, NULL, NULL);
1341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 return retval;
1344}
1345
Bram Moolenaarfb604092014-07-23 15:55:00 +02001346 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001347qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001348{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001349 vim_free(qi->qf_lists[qf_idx].qf_title);
1350 qi->qf_lists[qf_idx].qf_title = NULL;
1351
Bram Moolenaarfb604092014-07-23 15:55:00 +02001352 if (title != NULL)
1353 {
1354 char_u *p = alloc((int)STRLEN(title) + 2);
1355
Bram Moolenaara3921f42017-06-04 15:30:34 +02001356 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001357 if (p != NULL)
1358 sprintf((char *)p, ":%s", (char *)title);
1359 }
1360}
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001363 * Prepare for adding a new quickfix list. If the current list is in the
1364 * middle of the stack, then all the following lists are freed and then
1365 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 */
1367 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001368qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
1370 int i;
1371
1372 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001373 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 * the current entry. This makes it possible to browse in a tree-like
1375 * way with ":grep'.
1376 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001377 while (qi->qf_listcount > qi->qf_curlist + 1)
1378 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
1380 /*
1381 * When the stack is full, remove to oldest entry
1382 * Otherwise, add a new entry.
1383 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001384 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001386 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001388 qi->qf_lists[i - 1] = qi->qf_lists[i];
1389 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001392 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001393 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001394 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001395 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396}
1397
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001398/*
1399 * Free a location list
1400 */
1401 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001402ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001403{
1404 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001405 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001406
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001407 qi = *pqi;
1408 if (qi == NULL)
1409 return;
1410 *pqi = NULL; /* Remove reference to this list */
1411
1412 qi->qf_refcount--;
1413 if (qi->qf_refcount < 1)
1414 {
1415 /* No references to this location list */
1416 for (i = 0; i < qi->qf_listcount; ++i)
1417 qf_free(qi, i);
1418 vim_free(qi);
1419 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001420}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001421
1422 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001423qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001424{
1425 int i;
1426 qf_info_T *qi = &ql_info;
1427
1428 if (wp != NULL)
1429 {
1430 /* location list */
1431 ll_free_all(&wp->w_llist);
1432 ll_free_all(&wp->w_llist_ref);
1433 }
1434 else
1435 /* quickfix list */
1436 for (i = 0; i < qi->qf_listcount; ++i)
1437 qf_free(qi, i);
1438}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440/*
1441 * Add an entry to the end of the list of errors.
1442 * Returns OK or FAIL.
1443 */
1444 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001445qf_add_entry(
1446 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001447 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001448 char_u *dir, /* optional directory name */
1449 char_u *fname, /* file name or NULL */
1450 int bufnum, /* buffer number or zero */
1451 char_u *mesg, /* message */
1452 long lnum, /* line number */
1453 int col, /* column */
1454 int vis_col, /* using visual column */
1455 char_u *pattern, /* search pattern */
1456 int nr, /* error number */
1457 int type, /* type character */
1458 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001460 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001461 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001463 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001465 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001466 {
1467 buf_T *buf = buflist_findnr(bufnum);
1468
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001469 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001470 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001471 buf->b_has_qf_entry |=
1472 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001473 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001474 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001475 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1477 {
1478 vim_free(qfp);
1479 return FAIL;
1480 }
1481 qfp->qf_lnum = lnum;
1482 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001483 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001484 if (pattern == NULL || *pattern == NUL)
1485 qfp->qf_pattern = NULL;
1486 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1487 {
1488 vim_free(qfp->qf_text);
1489 vim_free(qfp);
1490 return FAIL;
1491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 qfp->qf_nr = nr;
1493 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1494 type = 0;
1495 qfp->qf_type = type;
1496 qfp->qf_valid = valid;
1497
Bram Moolenaara3921f42017-06-04 15:30:34 +02001498 lastp = &qi->qf_lists[qf_idx].qf_last;
1499 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001500 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001502 qi->qf_lists[qf_idx].qf_start = qfp;
1503 qi->qf_lists[qf_idx].qf_ptr = qfp;
1504 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001505 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507 else
1508 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001509 qfp->qf_prev = *lastp;
1510 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001512 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001514 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001515 ++qi->qf_lists[qf_idx].qf_count;
1516 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001517 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001519 qi->qf_lists[qf_idx].qf_index =
1520 qi->qf_lists[qf_idx].qf_count;
1521 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 }
1523
1524 return OK;
1525}
1526
1527/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001528 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001529 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001530 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001531ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001532{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001533 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001535 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1536 if (qi != NULL)
1537 {
1538 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1539 qi->qf_refcount++;
1540 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001541
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001542 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543}
1544
1545/*
1546 * Return the location list for window 'wp'.
1547 * If not present, allocate a location list
1548 */
1549 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001550ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001551{
1552 if (IS_LL_WINDOW(wp))
1553 /* For a location list window, use the referenced location list */
1554 return wp->w_llist_ref;
1555
1556 /*
1557 * For a non-location list window, w_llist_ref should not point to a
1558 * location list.
1559 */
1560 ll_free_all(&wp->w_llist_ref);
1561
1562 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001563 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001564 return wp->w_llist;
1565}
1566
1567/*
1568 * Copy the location list from window "from" to window "to".
1569 */
1570 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001571copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001572{
1573 qf_info_T *qi;
1574 int idx;
1575 int i;
1576
1577 /*
1578 * When copying from a location list window, copy the referenced
1579 * location list. For other windows, copy the location list for
1580 * that window.
1581 */
1582 if (IS_LL_WINDOW(from))
1583 qi = from->w_llist_ref;
1584 else
1585 qi = from->w_llist;
1586
1587 if (qi == NULL) /* no location list to copy */
1588 return;
1589
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001590 /* allocate a new location list */
1591 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001592 return;
1593
1594 to->w_llist->qf_listcount = qi->qf_listcount;
1595
1596 /* Copy the location lists one at a time */
1597 for (idx = 0; idx < qi->qf_listcount; idx++)
1598 {
1599 qf_list_T *from_qfl;
1600 qf_list_T *to_qfl;
1601
1602 to->w_llist->qf_curlist = idx;
1603
1604 from_qfl = &qi->qf_lists[idx];
1605 to_qfl = &to->w_llist->qf_lists[idx];
1606
1607 /* Some of the fields are populated by qf_add_entry() */
1608 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1609 to_qfl->qf_count = 0;
1610 to_qfl->qf_index = 0;
1611 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001612 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001613 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001614 if (from_qfl->qf_title != NULL)
1615 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1616 else
1617 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001618 if (from_qfl->qf_ctx != NULL)
1619 {
1620 to_qfl->qf_ctx = alloc_tv();
1621 if (to_qfl->qf_ctx != NULL)
1622 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1623 }
1624 else
1625 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001626
1627 if (from_qfl->qf_count)
1628 {
1629 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001630 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631
1632 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001633 for (i = 0, from_qfp = from_qfl->qf_start;
1634 i < from_qfl->qf_count && from_qfp != NULL;
1635 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001636 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001637 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001638 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001639 NULL,
1640 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001641 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001642 from_qfp->qf_text,
1643 from_qfp->qf_lnum,
1644 from_qfp->qf_col,
1645 from_qfp->qf_viscol,
1646 from_qfp->qf_pattern,
1647 from_qfp->qf_nr,
1648 0,
1649 from_qfp->qf_valid) == FAIL)
1650 {
1651 qf_free_all(to);
1652 return;
1653 }
1654 /*
1655 * qf_add_entry() will not set the qf_num field, as the
1656 * directory and file names are not supplied. So the qf_fnum
1657 * field is copied here.
1658 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001659 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001660 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1661 prevp->qf_type = from_qfp->qf_type; /* error type */
1662 if (from_qfl->qf_ptr == from_qfp)
1663 to_qfl->qf_ptr = prevp; /* current location */
1664 }
1665 }
1666
1667 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1668
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001669 /* Assign a new ID for the location list */
1670 to_qfl->qf_id = ++last_qf_id;
1671
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001672 /* When no valid entries are present in the list, qf_ptr points to
1673 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001674 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001675 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001676 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001677 to_qfl->qf_index = 1;
1678 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001679 }
1680
1681 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1682}
1683
1684/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001685 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001686 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
1688 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001689qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
Bram Moolenaar82404332016-07-10 17:00:38 +02001691 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001692 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001693 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001694
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (fname == NULL || *fname == NUL) /* no file name */
1696 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
Bram Moolenaare60acc12011-05-10 16:41:25 +02001698#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001699 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001700#endif
1701#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001702 if (directory != NULL)
1703 slash_adjust(directory);
1704 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001705#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001706 if (directory != NULL && !vim_isAbsName(fname)
1707 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1708 {
1709 /*
1710 * Here we check if the file really exists.
1711 * This should normally be true, but if make works without
1712 * "leaving directory"-messages we might have missed a
1713 * directory change.
1714 */
1715 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001718 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001719 if (directory)
1720 ptr = concat_fnames(directory, fname, TRUE);
1721 else
1722 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001724 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001725 bufname = ptr;
1726 }
1727 else
1728 bufname = fname;
1729
1730 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001731 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001732 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001733 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001734 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001736 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001737 {
1738 vim_free(qf_last_bufname);
1739 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1740 if (bufname == ptr)
1741 qf_last_bufname = bufname;
1742 else
1743 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001744 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001745 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001746 if (buf == NULL)
1747 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001748
Bram Moolenaarc1542742016-07-20 21:44:37 +02001749 buf->b_has_qf_entry =
1750 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001751 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
1754/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001755 * Push dirbuf onto the directory stack and return pointer to actual dir or
1756 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 */
1758 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001759qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 struct dir_stack_T *ds_new;
1762 struct dir_stack_T *ds_ptr;
1763
1764 /* allocate new stack element and hook it in */
1765 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1766 if (ds_new == NULL)
1767 return NULL;
1768
1769 ds_new->next = *stackptr;
1770 *stackptr = ds_new;
1771
1772 /* store directory on the stack */
1773 if (vim_isAbsName(dirbuf)
1774 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001775 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 (*stackptr)->dirname = vim_strsave(dirbuf);
1777 else
1778 {
1779 /* Okay we don't have an absolute path.
1780 * dirbuf must be a subdir of one of the directories on the stack.
1781 * Let's search...
1782 */
1783 ds_new = (*stackptr)->next;
1784 (*stackptr)->dirname = NULL;
1785 while (ds_new)
1786 {
1787 vim_free((*stackptr)->dirname);
1788 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1789 TRUE);
1790 if (mch_isdir((*stackptr)->dirname) == TRUE)
1791 break;
1792
1793 ds_new = ds_new->next;
1794 }
1795
1796 /* clean up all dirs we already left */
1797 while ((*stackptr)->next != ds_new)
1798 {
1799 ds_ptr = (*stackptr)->next;
1800 (*stackptr)->next = (*stackptr)->next->next;
1801 vim_free(ds_ptr->dirname);
1802 vim_free(ds_ptr);
1803 }
1804
1805 /* Nothing found -> it must be on top level */
1806 if (ds_new == NULL)
1807 {
1808 vim_free((*stackptr)->dirname);
1809 (*stackptr)->dirname = vim_strsave(dirbuf);
1810 }
1811 }
1812
1813 if ((*stackptr)->dirname != NULL)
1814 return (*stackptr)->dirname;
1815 else
1816 {
1817 ds_ptr = *stackptr;
1818 *stackptr = (*stackptr)->next;
1819 vim_free(ds_ptr);
1820 return NULL;
1821 }
1822}
1823
1824
1825/*
1826 * pop dirbuf from the directory stack and return previous directory or NULL if
1827 * stack is empty
1828 */
1829 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001830qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 struct dir_stack_T *ds_ptr;
1833
1834 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1835 * What to do if it isn't? */
1836
1837 /* pop top element and free it */
1838 if (*stackptr != NULL)
1839 {
1840 ds_ptr = *stackptr;
1841 *stackptr = (*stackptr)->next;
1842 vim_free(ds_ptr->dirname);
1843 vim_free(ds_ptr);
1844 }
1845
1846 /* return NEW top element as current dir or NULL if stack is empty*/
1847 return *stackptr ? (*stackptr)->dirname : NULL;
1848}
1849
1850/*
1851 * clean up directory stack
1852 */
1853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001854qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 struct dir_stack_T *ds_ptr;
1857
1858 while ((ds_ptr = *stackptr) != NULL)
1859 {
1860 *stackptr = (*stackptr)->next;
1861 vim_free(ds_ptr->dirname);
1862 vim_free(ds_ptr);
1863 }
1864}
1865
1866/*
1867 * Check in which directory of the directory stack the given file can be
1868 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001869 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 * Cleans up intermediate directory entries.
1871 *
1872 * TODO: How to solve the following problem?
1873 * If we have the this directory tree:
1874 * ./
1875 * ./aa
1876 * ./aa/bb
1877 * ./bb
1878 * ./bb/x.c
1879 * and make says:
1880 * making all in aa
1881 * making all in bb
1882 * x.c:9: Error
1883 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1884 * qf_guess_filepath will return NULL.
1885 */
1886 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001887qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
1889 struct dir_stack_T *ds_ptr;
1890 struct dir_stack_T *ds_tmp;
1891 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001892 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
1894 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001895 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 return NULL;
1897
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001898 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 fullname = NULL;
1900 while (ds_ptr)
1901 {
1902 vim_free(fullname);
1903 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1904
1905 /* If concat_fnames failed, just go on. The worst thing that can happen
1906 * is that we delete the entire stack.
1907 */
1908 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1909 break;
1910
1911 ds_ptr = ds_ptr->next;
1912 }
1913
1914 vim_free(fullname);
1915
1916 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001917 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001919 ds_tmp = qfl->qf_dir_stack->next;
1920 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 vim_free(ds_tmp->dirname);
1922 vim_free(ds_tmp);
1923 }
1924
1925 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926}
1927
1928/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001929 * When loading a file from the quickfix, the auto commands may modify it.
1930 * This may invalidate the current quickfix entry. This function checks
1931 * whether a entry is still present in the quickfix.
1932 * Similar to location list.
1933 */
1934 static int
1935is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1936{
1937 qf_list_T *qfl;
1938 qfline_T *qfp;
1939 int i;
1940
1941 qfl = &qi->qf_lists[qi->qf_curlist];
1942
1943 /* Search for the entry in the current list */
1944 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1945 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001946 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001947 break;
1948
1949 if (i == qfl->qf_count) /* Entry is not found */
1950 return FALSE;
1951
1952 return TRUE;
1953}
1954
1955/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001956 * Get the next valid entry in the current quickfix/location list. The search
1957 * starts from the current entry. If next_file is TRUE, then return the next
1958 * valid entry in the next file in the list. Returns NULL on failure.
1959 */
1960 static qfline_T *
1961get_next_valid_entry(
1962 qf_info_T *qi,
1963 qfline_T *qf_ptr,
1964 int *qf_index,
1965 int dir)
1966{
1967 int idx;
1968 int old_qf_fnum;
1969
1970 idx = *qf_index;
1971 old_qf_fnum = qf_ptr->qf_fnum;
1972
1973 do
1974 {
1975 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
1976 || qf_ptr->qf_next == NULL)
1977 return NULL;
1978 ++idx;
1979 qf_ptr = qf_ptr->qf_next;
1980 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1981 && !qf_ptr->qf_valid)
1982 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1983
1984 *qf_index = idx;
1985 return qf_ptr;
1986}
1987
1988/*
1989 * Get the previous valid entry in the current quickfix/location list. The
1990 * search starts from the current entry. If prev_file is TRUE, then return the
1991 * previous valid entry in the previous file in the list. Returns NULL on
1992 * failure.
1993 */
1994 static qfline_T *
1995get_prev_valid_entry(
1996 qf_info_T *qi,
1997 qfline_T *qf_ptr,
1998 int *qf_index,
1999 int dir)
2000{
2001 int idx;
2002 int old_qf_fnum;
2003
2004 idx = *qf_index;
2005 old_qf_fnum = qf_ptr->qf_fnum;
2006
2007 do
2008 {
2009 if (idx == 1 || qf_ptr->qf_prev == NULL)
2010 return NULL;
2011 --idx;
2012 qf_ptr = qf_ptr->qf_prev;
2013 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2014 && !qf_ptr->qf_valid)
2015 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2016
2017 *qf_index = idx;
2018 return qf_ptr;
2019}
2020
2021/*
2022 * Get the n'th (errornr) previous/next valid entry from the current entry in
2023 * the quickfix list.
2024 * dir == FORWARD or FORWARD_FILE: next valid entry
2025 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2026 */
2027 static qfline_T *
2028get_nth_valid_entry(
2029 qf_info_T *qi,
2030 int errornr,
2031 qfline_T *qf_ptr,
2032 int *qf_index,
2033 int dir)
2034{
2035 qfline_T *prev_qf_ptr;
2036 int prev_index;
2037 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2038 char_u *err = e_no_more_items;
2039
2040 while (errornr--)
2041 {
2042 prev_qf_ptr = qf_ptr;
2043 prev_index = *qf_index;
2044
2045 if (dir == FORWARD || dir == FORWARD_FILE)
2046 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2047 else
2048 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2049 if (qf_ptr == NULL)
2050 {
2051 qf_ptr = prev_qf_ptr;
2052 *qf_index = prev_index;
2053 if (err != NULL)
2054 {
2055 EMSG(_(err));
2056 return NULL;
2057 }
2058 break;
2059 }
2060
2061 err = NULL;
2062 }
2063
2064 return qf_ptr;
2065}
2066
2067/*
2068 * Get n'th quickfix entry
2069 */
2070 static qfline_T *
2071get_nth_entry(
2072 qf_info_T *qi,
2073 int errornr,
2074 qfline_T *qf_ptr,
2075 int *qf_index)
2076{
2077 int qf_idx = *qf_index;
2078
2079 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2080 {
2081 --qf_idx;
2082 qf_ptr = qf_ptr->qf_prev;
2083 }
2084 while (errornr > qf_idx &&
2085 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2086 qf_ptr->qf_next != NULL)
2087 {
2088 ++qf_idx;
2089 qf_ptr = qf_ptr->qf_next;
2090 }
2091
2092 *qf_index = qf_idx;
2093 return qf_ptr;
2094}
2095
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002096/*
2097 * Find a help window or open one.
2098 */
2099 static int
2100jump_to_help_window(qf_info_T *qi, int *opened_window)
2101{
2102 win_T *wp;
2103 int flags;
2104
2105 if (cmdmod.tab != 0)
2106 wp = NULL;
2107 else
2108 FOR_ALL_WINDOWS(wp)
2109 if (bt_help(wp->w_buffer))
2110 break;
2111 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2112 win_enter(wp, TRUE);
2113 else
2114 {
2115 /*
2116 * Split off help window; put it at far top if no position
2117 * specified, the current window is vertically split and narrow.
2118 */
2119 flags = WSP_HELP;
2120 if (cmdmod.split == 0 && curwin->w_width != Columns
2121 && curwin->w_width < 80)
2122 flags |= WSP_TOP;
2123 if (qi != &ql_info)
2124 flags |= WSP_NEWLOC; /* don't copy the location list */
2125
2126 if (win_split(0, flags) == FAIL)
2127 return FAIL;
2128
2129 *opened_window = TRUE;
2130
2131 if (curwin->w_height < p_hh)
2132 win_setheight((int)p_hh);
2133
2134 if (qi != &ql_info) /* not a quickfix list */
2135 {
2136 /* The new window should use the supplied location list */
2137 curwin->w_llist = qi;
2138 qi->qf_refcount++;
2139 }
2140 }
2141
2142 if (!p_im)
2143 restart_edit = 0; /* don't want insert mode in help file */
2144
2145 return OK;
2146}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002147
2148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 * jump to a quickfix line
2150 * if dir == FORWARD go "errornr" valid entries forward
2151 * if dir == BACKWARD go "errornr" valid entries backward
2152 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2153 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2154 * else if "errornr" is zero, redisplay the same line
2155 * else go to entry "errornr"
2156 */
2157 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002158qf_jump(qf_info_T *qi,
2159 int dir,
2160 int errornr,
2161 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002163 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002164 qfline_T *qf_ptr;
2165 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 linenr_T i;
2169 buf_T *old_curbuf;
2170 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 colnr_T screen_col;
2172 colnr_T char_col;
2173 char_u *line;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002174 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002175 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 int opened_window = FALSE;
2177 win_T *win;
2178 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002179 int flags;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002180 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 int print_message = TRUE;
2182 int len;
2183#ifdef FEAT_FOLDING
2184 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2185#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002186 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002187 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002189 if (qi == NULL)
2190 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002191
2192 if (qi->qf_curlist >= qi->qf_listcount
2193 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
2195 EMSG(_(e_quickfix));
2196 return;
2197 }
2198
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002199 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002201 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002203 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002205 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2206 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002208 qf_ptr = old_qf_ptr;
2209 qf_index = old_qf_index;
2210 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212 }
2213 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002214 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002216 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2217 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 /* No need to print the error message if it's visible in the error
2219 * window */
2220 print_message = FALSE;
2221
2222 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002223 * For ":helpgrep" find a help window or open one.
2224 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002225 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002226 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002227 if (jump_to_help_window(qi, &opened_window) == FAIL)
2228 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002229 }
2230
2231 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 * If currently in the quickfix window, find another window to show the
2233 * file in.
2234 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002235 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002237 win_T *usable_win_ptr = NULL;
2238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 /*
2240 * If there is no file specified, we don't know where to go.
2241 * But do advance, otherwise ":cn" gets stuck.
2242 */
2243 if (qf_ptr->qf_fnum == 0)
2244 goto theend;
2245
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002246 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002247
2248 ll_ref = curwin->w_llist_ref;
2249 if (ll_ref != NULL)
2250 {
2251 /* Find a window using the same location list that is not a
2252 * quickfix window. */
2253 FOR_ALL_WINDOWS(usable_win_ptr)
2254 if (usable_win_ptr->w_llist == ll_ref
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02002255 && !bt_quickfix(usable_win_ptr->w_buffer))
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002256 {
2257 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002258 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002259 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002260 }
2261
2262 if (!usable_win)
2263 {
2264 /* Locate a window showing a normal buffer */
2265 FOR_ALL_WINDOWS(win)
2266 if (win->w_buffer->b_p_bt[0] == NUL)
2267 {
2268 usable_win = 1;
2269 break;
2270 }
2271 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002272
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002274 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002275 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002277 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002278 {
2279 tabpage_T *tp;
2280 win_T *wp;
2281
2282 FOR_ALL_TAB_WINDOWS(tp, wp)
2283 {
2284 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2285 {
2286 goto_tabpage_win(tp, wp);
2287 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002288 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002289 }
2290 }
2291 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002292win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002293
2294 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002295 * If there is only one window and it is the quickfix window, create a
2296 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002297 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002298 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002300 flags = WSP_ABOVE;
2301 if (ll_ref != NULL)
2302 flags |= WSP_NEWLOC;
2303 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 goto failed; /* not enough room for window */
2305 opened_window = TRUE; /* close it when fail */
2306 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002307 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002308 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002309 if (ll_ref != NULL)
2310 {
2311 /* The new window should use the location list from the
2312 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002313 curwin->w_llist = ll_ref;
2314 ll_ref->qf_refcount++;
2315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317 else
2318 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002319 if (curwin->w_llist_ref != NULL)
2320 {
2321 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002322 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002323 if (win == NULL)
2324 {
2325 /* Find the window showing the selected file */
2326 FOR_ALL_WINDOWS(win)
2327 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2328 break;
2329 if (win == NULL)
2330 {
2331 /* Find a previous usable window */
2332 win = curwin;
2333 do
2334 {
2335 if (win->w_buffer->b_p_bt[0] == NUL)
2336 break;
2337 if (win->w_prev == NULL)
2338 win = lastwin; /* wrap around the top */
2339 else
2340 win = win->w_prev; /* go to previous window */
2341 } while (win != curwin);
2342 }
2343 }
2344 win_goto(win);
2345
2346 /* If the location list for the window is not set, then set it
2347 * to the location list from the location window */
2348 if (win->w_llist == NULL)
2349 {
2350 win->w_llist = ll_ref;
2351 ll_ref->qf_refcount++;
2352 }
2353 }
2354 else
2355 {
2356
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 /*
2358 * Try to find a window that shows the right buffer.
2359 * Default to the window just above the quickfix buffer.
2360 */
2361 win = curwin;
2362 altwin = NULL;
2363 for (;;)
2364 {
2365 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2366 break;
2367 if (win->w_prev == NULL)
2368 win = lastwin; /* wrap around the top */
2369 else
2370 win = win->w_prev; /* go to previous window */
2371
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002372 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
2374 /* Didn't find it, go to the window before the quickfix
2375 * window. */
2376 if (altwin != NULL)
2377 win = altwin;
2378 else if (curwin->w_prev != NULL)
2379 win = curwin->w_prev;
2380 else
2381 win = curwin->w_next;
2382 break;
2383 }
2384
2385 /* Remember a usable window. */
2386 if (altwin == NULL && !win->w_p_pvw
2387 && win->w_buffer->b_p_bt[0] == NUL)
2388 altwin = win;
2389 }
2390
2391 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
2396 /*
2397 * If there is a file name,
2398 * read the wanted file if needed, and check autowrite etc.
2399 */
2400 old_curbuf = curbuf;
2401 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002402
2403 if (qf_ptr->qf_fnum != 0)
2404 {
2405 if (qf_ptr->qf_type == 1)
2406 {
2407 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2408 * set b_p_ro flag). */
2409 if (!can_abandon(curbuf, forceit))
2410 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002411 no_write_message();
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002412 ok = FALSE;
2413 }
2414 else
2415 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002416 ECMD_HIDE + ECMD_SET_HELP,
2417 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002418 }
2419 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002420 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002421 int old_qf_curlist = qi->qf_curlist;
2422 int is_abort = FALSE;
2423
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002424 ok = buflist_getfile(qf_ptr->qf_fnum,
2425 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002426 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002427 {
2428 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002429 is_abort = TRUE;
2430 opened_window = FALSE;
2431 }
2432 else if (old_qf_curlist != qi->qf_curlist
2433 || !is_qf_entry_present(qi, qf_ptr))
2434 {
2435 if (qi == &ql_info)
2436 EMSG(_("E925: Current quickfix was changed"));
2437 else
2438 EMSG(_("E926: Current location list was changed"));
2439 is_abort = TRUE;
2440 }
2441
2442 if (is_abort)
2443 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002444 ok = FALSE;
2445 qi = NULL;
2446 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002447 }
2448 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002449 }
2450
2451 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
2453 /* When not switched to another buffer, still need to set pc mark */
2454 if (curbuf == old_curbuf)
2455 setpcmark();
2456
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002457 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002459 /*
2460 * Go to line with error, unless qf_lnum is 0.
2461 */
2462 i = qf_ptr->qf_lnum;
2463 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002465 if (i > curbuf->b_ml.ml_line_count)
2466 i = curbuf->b_ml.ml_line_count;
2467 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002469 if (qf_ptr->qf_col > 0)
2470 {
2471 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002472#ifdef FEAT_VIRTUALEDIT
2473 curwin->w_cursor.coladd = 0;
2474#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002475 if (qf_ptr->qf_viscol == TRUE)
2476 {
2477 /*
2478 * Check each character from the beginning of the error
2479 * line up to the error column. For each tab character
2480 * found, reduce the error column value by the length of
2481 * a tab character.
2482 */
2483 line = ml_get_curline();
2484 screen_col = 0;
2485 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2486 {
2487 if (*line == NUL)
2488 break;
2489 if (*line++ == '\t')
2490 {
2491 curwin->w_cursor.col -= 7 - (screen_col % 8);
2492 screen_col += 8 - (screen_col % 8);
2493 }
2494 else
2495 ++screen_col;
2496 }
2497 }
2498 check_cursor();
2499 }
2500 else
2501 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 }
2503 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002504 {
2505 pos_T save_cursor;
2506
2507 /* Move the cursor to the first line in the buffer */
2508 save_cursor = curwin->w_cursor;
2509 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002510 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002511 SEARCH_KEEP, NULL, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002512 curwin->w_cursor = save_cursor;
2513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
2515#ifdef FEAT_FOLDING
2516 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2517 foldOpenCursor();
2518#endif
2519 if (print_message)
2520 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002521 /* Update the screen before showing the message, unless the screen
2522 * scrolled up. */
2523 if (!msg_scrolled)
2524 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002526 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2528 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2529 /* Add the message, skipping leading whitespace and newlines. */
2530 len = (int)STRLEN(IObuff);
2531 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2532
2533 /* Output the message. Overwrite to avoid scrolling when the 'O'
2534 * flag is present in 'shortmess'; But when not jumping, print the
2535 * whole message. */
2536 i = msg_scroll;
2537 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2538 msg_scroll = TRUE;
2539 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2540 msg_scroll = FALSE;
2541 msg_attr_keep(IObuff, 0, TRUE);
2542 msg_scroll = i;
2543 }
2544 }
2545 else
2546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (opened_window)
2548 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002549 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
2551 /*
2552 * Couldn't open file, so put index back where it was. This could
2553 * happen if the file was readonly and we changed something.
2554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 qf_ptr = old_qf_ptr;
2557 qf_index = old_qf_index;
2558 }
2559 }
2560theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002561 if (qi != NULL)
2562 {
2563 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2564 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (p_swb != old_swb && opened_window)
2567 {
2568 /* Restore old 'switchbuf' value, but not when an autocommand or
2569 * modeline has changed the value. */
2570 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002573 swb_flags = old_swb_flags;
2574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 else
2576 free_string_option(old_swb);
2577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578}
2579
2580/*
2581 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002582 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 */
2584 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002585qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002587 buf_T *buf;
2588 char_u *fname;
2589 qfline_T *qfp;
2590 int i;
2591 int idx1 = 1;
2592 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002593 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002594 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002595 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002597 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002599 if (eap->cmdidx == CMD_llist)
2600 {
2601 qi = GET_LOC_LIST(curwin);
2602 if (qi == NULL)
2603 {
2604 EMSG(_(e_loclist));
2605 return;
2606 }
2607 }
2608
2609 if (qi->qf_curlist >= qi->qf_listcount
2610 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
2612 EMSG(_(e_quickfix));
2613 return;
2614 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002615 if (*arg == '+')
2616 {
2617 ++arg;
2618 plus = TRUE;
2619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2621 {
2622 EMSG(_(e_trailing));
2623 return;
2624 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002625 if (plus)
2626 {
2627 i = qi->qf_lists[qi->qf_curlist].qf_index;
2628 idx2 = i + idx1;
2629 idx1 = i;
2630 }
2631 else
2632 {
2633 i = qi->qf_lists[qi->qf_curlist].qf_count;
2634 if (idx1 < 0)
2635 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2636 if (idx2 < 0)
2637 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002640 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002642 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2643 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 {
2645 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2646 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002647 msg_putchar('\n');
2648 if (got_int)
2649 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002650
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002651 fname = NULL;
2652 if (qfp->qf_fnum != 0
2653 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2654 {
2655 fname = buf->b_fname;
2656 if (qfp->qf_type == 1) /* :helpgrep */
2657 fname = gettail(fname);
2658 }
2659 if (fname == NULL)
2660 sprintf((char *)IObuff, "%2d", i);
2661 else
2662 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2663 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002664 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar21020352017-06-13 17:21:04 +02002665 ? HL_ATTR(HLF_QFL) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002666 if (qfp->qf_lnum == 0)
2667 IObuff[0] = NUL;
2668 else if (qfp->qf_col == 0)
2669 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2670 else
2671 sprintf((char *)IObuff, ":%ld col %d",
2672 qfp->qf_lnum, qfp->qf_col);
2673 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2674 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002675 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002676 if (qfp->qf_pattern != NULL)
2677 {
2678 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2679 STRCAT(IObuff, ":");
2680 msg_puts(IObuff);
2681 }
2682 msg_puts((char_u *)" ");
2683
2684 /* Remove newlines and leading whitespace from the text. For an
2685 * unrecognized line keep the indent, the compiler may mark a word
2686 * with ^^^^. */
2687 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2689 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002690 msg_prt_line(IObuff, FALSE);
2691 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002693
2694 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002695 if (qfp == NULL)
2696 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002697 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 ui_breakcheck();
2699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700}
2701
2702/*
2703 * Remove newlines and leading whitespace from an error message.
2704 * Put the result in "buf[bufsize]".
2705 */
2706 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002707qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 int i;
2710 char_u *p = text;
2711
2712 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2713 {
2714 if (*p == '\n')
2715 {
2716 buf[i] = ' ';
2717 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002718 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 break;
2720 }
2721 else
2722 buf[i] = *p++;
2723 }
2724 buf[i] = NUL;
2725}
2726
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002727 static void
2728qf_msg(qf_info_T *qi, int which, char *lead)
2729{
2730 char *title = (char *)qi->qf_lists[which].qf_title;
2731 int count = qi->qf_lists[which].qf_count;
2732 char_u buf[IOSIZE];
2733
2734 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2735 lead,
2736 which + 1,
2737 qi->qf_listcount,
2738 count);
2739
2740 if (title != NULL)
2741 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002742 size_t len = STRLEN(buf);
2743
2744 if (len < 34)
2745 {
2746 vim_memset(buf + len, ' ', 34 - len);
2747 buf[34] = NUL;
2748 }
2749 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002750 }
2751 trunc_string(buf, buf, Columns - 1, IOSIZE);
2752 msg(buf);
2753}
2754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755/*
2756 * ":colder [count]": Up in the quickfix stack.
2757 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002758 * ":lolder [count]": Up in the location list stack.
2759 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 */
2761 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002762qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002764 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 int count;
2766
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002767 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2768 {
2769 qi = GET_LOC_LIST(curwin);
2770 if (qi == NULL)
2771 {
2772 EMSG(_(e_loclist));
2773 return;
2774 }
2775 }
2776
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 if (eap->addr_count != 0)
2778 count = eap->line2;
2779 else
2780 count = 1;
2781 while (count--)
2782 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002785 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
2787 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002788 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002790 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
2792 else
2793 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002794 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 {
2796 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002797 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002799 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002802 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002803 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804}
2805
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002806 void
2807qf_history(exarg_T *eap)
2808{
2809 qf_info_T *qi = &ql_info;
2810 int i;
2811
2812 if (eap->cmdidx == CMD_lhistory)
2813 qi = GET_LOC_LIST(curwin);
2814 if (qi == NULL || (qi->qf_listcount == 0
2815 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2816 MSG(_("No entries"));
2817 else
2818 for (i = 0; i < qi->qf_listcount; ++i)
2819 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2820}
2821
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002823 * Free all the entries in the error list "idx". Note that other information
2824 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 */
2826 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002827qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002829 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002830 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002831 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002832 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002834 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002836 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002837 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002838 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002839 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002840 vim_free(qfp->qf_text);
2841 stop = (qfp == qfpnext);
2842 vim_free(qfp->qf_pattern);
2843 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002844 if (stop)
2845 /* Somehow qf_count may have an incorrect value, set it to 1
2846 * to avoid crashing when it's wrong.
2847 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002848 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002849 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002850 qfl->qf_start = qfpnext;
2851 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002853
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002854 qfl->qf_index = 0;
2855 qfl->qf_start = NULL;
2856 qfl->qf_last = NULL;
2857 qfl->qf_ptr = NULL;
2858 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002859
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002860 qf_clean_dir_stack(&qfl->qf_dir_stack);
2861 qfl->qf_directory = NULL;
2862 qf_clean_dir_stack(&qfl->qf_file_stack);
2863 qfl->qf_currfile = NULL;
2864 qfl->qf_multiline = FALSE;
2865 qfl->qf_multiignore = FALSE;
2866 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867}
2868
2869/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002870 * Free error list "idx". Frees all the entries in the quickfix list,
2871 * associated context information and the title.
2872 */
2873 static void
2874qf_free(qf_info_T *qi, int idx)
2875{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002876 qf_list_T *qfl = &qi->qf_lists[idx];
2877
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002878 qf_free_items(qi, idx);
2879
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002880 vim_free(qfl->qf_title);
2881 qfl->qf_title = NULL;
2882 free_tv(qfl->qf_ctx);
2883 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002884 qfl->qf_id = 0;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002885}
2886
2887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 * qf_mark_adjust: adjust marks
2889 */
2890 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002891qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002892 win_T *wp,
2893 linenr_T line1,
2894 linenr_T line2,
2895 long amount,
2896 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002898 int i;
2899 qfline_T *qfp;
2900 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002901 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002902 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002903 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904
Bram Moolenaarc1542742016-07-20 21:44:37 +02002905 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002906 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002907 if (wp != NULL)
2908 {
2909 if (wp->w_llist == NULL)
2910 return;
2911 qi = wp->w_llist;
2912 }
2913
2914 for (idx = 0; idx < qi->qf_listcount; ++idx)
2915 if (qi->qf_lists[idx].qf_count)
2916 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002917 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2918 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (qfp->qf_fnum == curbuf->b_fnum)
2920 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002921 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2923 {
2924 if (amount == MAXLNUM)
2925 qfp->qf_cleared = TRUE;
2926 else
2927 qfp->qf_lnum += amount;
2928 }
2929 else if (amount_after && qfp->qf_lnum > line2)
2930 qfp->qf_lnum += amount_after;
2931 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002932
2933 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002934 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935}
2936
2937/*
2938 * Make a nice message out of the error character and the error number:
2939 * char number message
2940 * e or E 0 " error"
2941 * w or W 0 " warning"
2942 * i or I 0 " info"
2943 * 0 0 ""
2944 * other 0 " c"
2945 * e or E n " error n"
2946 * w or W n " warning n"
2947 * i or I n " info n"
2948 * 0 n " error n"
2949 * other n " c n"
2950 * 1 x "" :helpgrep
2951 */
2952 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002953qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 static char_u buf[20];
2956 static char_u cc[3];
2957 char_u *p;
2958
2959 if (c == 'W' || c == 'w')
2960 p = (char_u *)" warning";
2961 else if (c == 'I' || c == 'i')
2962 p = (char_u *)" info";
2963 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2964 p = (char_u *)" error";
2965 else if (c == 0 || c == 1)
2966 p = (char_u *)"";
2967 else
2968 {
2969 cc[0] = ' ';
2970 cc[1] = c;
2971 cc[2] = NUL;
2972 p = cc;
2973 }
2974
2975 if (nr <= 0)
2976 return p;
2977
2978 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2979 return buf;
2980}
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982/*
2983 * ":cwindow": open the quickfix window if we have errors to display,
2984 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002985 * ":lwindow": open the location list window if we have locations to display,
2986 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 */
2988 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002989ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002991 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 win_T *win;
2993
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002994 if (eap->cmdidx == CMD_lwindow)
2995 {
2996 qi = GET_LOC_LIST(curwin);
2997 if (qi == NULL)
2998 return;
2999 }
3000
3001 /* Look for an existing quickfix window. */
3002 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 /*
3005 * If a quickfix window is open but we have no errors to display,
3006 * close the window. If a quickfix window is not open, then open
3007 * it if we have errors; otherwise, leave it closed.
3008 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003009 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003010 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003011 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
3013 if (win != NULL)
3014 ex_cclose(eap);
3015 }
3016 else if (win == NULL)
3017 ex_copen(eap);
3018}
3019
3020/*
3021 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003022 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003025ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003027 win_T *win = NULL;
3028 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003030 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3031 {
3032 qi = GET_LOC_LIST(curwin);
3033 if (qi == NULL)
3034 return;
3035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003037 /* Find existing quickfix window and close it. */
3038 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (win != NULL)
3040 win_close(win, FALSE);
3041}
3042
3043/*
3044 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003045 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 */
3047 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003048ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003050 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003053 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003054 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003055 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003057 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3058 {
3059 qi = GET_LOC_LIST(curwin);
3060 if (qi == NULL)
3061 {
3062 EMSG(_(e_loclist));
3063 return;
3064 }
3065 }
3066
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 if (eap->addr_count != 0)
3068 height = eap->line2;
3069 else
3070 height = QF_WINHEIGHT;
3071
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073#ifdef FEAT_GUI
3074 need_mouse_correct = TRUE;
3075#endif
3076
3077 /*
3078 * Find existing quickfix window, or open a new one.
3079 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003080 win = qf_find_win(qi);
3081
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003082 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003085 if (eap->addr_count != 0)
3086 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003087 if (cmdmod.split & WSP_VERT)
3088 {
3089 if (height != W_WIDTH(win))
3090 win_setwidth(height);
3091 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003092 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003093 win_setheight(height);
3094 }
3095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 else
3097 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00003098 qf_buf = qf_find_buf(qi);
3099
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 /* The current window becomes the previous window afterwards. */
3101 win = curwin;
3102
Bram Moolenaar77642c02012-11-20 17:55:10 +01003103 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3104 && cmdmod.split == 0)
3105 /* Create the new window at the very bottom, except when
3106 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003107 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003108 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003110 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003112 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003114 /*
3115 * For the location list window, create a reference to the
3116 * location list from the window 'win'.
3117 */
3118 curwin->w_llist_ref = win->w_llist;
3119 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003121
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003122 if (oldwin != curwin)
3123 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003124 if (qf_buf != NULL)
3125 /* Use the existing quickfix buffer */
3126 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003127 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003128 else
3129 {
3130 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003131 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003132 /* switch off 'swapfile' */
3133 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3134 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003135 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003136 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003137 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003138#ifdef FEAT_DIFF
3139 curwin->w_p_diff = FALSE;
3140#endif
3141#ifdef FEAT_FOLDING
3142 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3143 OPT_LOCAL);
3144#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003147 /* Only set the height when still in the same tab page and there is no
3148 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003149 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 win_setheight(height);
3151 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3152 if (win_valid(win))
3153 prevwin = win;
3154 }
3155
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003156 qf_set_title_var(qi);
3157
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 /*
3159 * Fill the buffer with the quickfix list.
3160 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003161 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003163 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 curwin->w_cursor.col = 0;
3165 check_cursor();
3166 update_topline(); /* scroll to show the line */
3167}
3168
3169/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003170 * Move the cursor in the quickfix window to "lnum".
3171 */
3172 static void
3173qf_win_goto(win_T *win, linenr_T lnum)
3174{
3175 win_T *old_curwin = curwin;
3176
3177 curwin = win;
3178 curbuf = win->w_buffer;
3179 curwin->w_cursor.lnum = lnum;
3180 curwin->w_cursor.col = 0;
3181#ifdef FEAT_VIRTUALEDIT
3182 curwin->w_cursor.coladd = 0;
3183#endif
3184 curwin->w_curswant = 0;
3185 update_topline(); /* scroll to show the line */
3186 redraw_later(VALID);
3187 curwin->w_redr_status = TRUE; /* update ruler */
3188 curwin = old_curwin;
3189 curbuf = curwin->w_buffer;
3190}
3191
3192/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003193 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003194 */
3195 void
3196ex_cbottom(exarg_T *eap UNUSED)
3197{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003198 qf_info_T *qi = &ql_info;
3199 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003200
Bram Moolenaar537ef082016-07-09 17:56:19 +02003201 if (eap->cmdidx == CMD_lbottom)
3202 {
3203 qi = GET_LOC_LIST(curwin);
3204 if (qi == NULL)
3205 {
3206 EMSG(_(e_loclist));
3207 return;
3208 }
3209 }
3210
3211 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003212 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3213 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3214}
3215
3216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 * Return the number of the current entry (line number in the quickfix
3218 * window).
3219 */
3220 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003221qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003223 qf_info_T *qi = &ql_info;
3224
3225 if (IS_LL_WINDOW(wp))
3226 /* In the location list window, use the referenced location list */
3227 qi = wp->w_llist_ref;
3228
3229 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230}
3231
3232/*
3233 * Update the cursor position in the quickfix window to the current error.
3234 * Return TRUE if there is a quickfix window.
3235 */
3236 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003237qf_win_pos_update(
3238 qf_info_T *qi,
3239 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240{
3241 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003242 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
3244 /*
3245 * Put the cursor on the current error in the quickfix window, so that
3246 * it's viewable.
3247 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003248 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 if (win != NULL
3250 && qf_index <= win->w_buffer->b_ml.ml_line_count
3251 && old_qf_index != qf_index)
3252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (qf_index > old_qf_index)
3254 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003255 win->w_redraw_top = old_qf_index;
3256 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258 else
3259 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003260 win->w_redraw_top = qf_index;
3261 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003263 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 }
3265 return win != NULL;
3266}
3267
3268/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003269 * Check whether the given window is displaying the specified quickfix/location
3270 * list buffer
3271 */
3272 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003273is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003274{
3275 /*
3276 * A window displaying the quickfix buffer will have the w_llist_ref field
3277 * set to NULL.
3278 * A window displaying a location list buffer will have the w_llist_ref
3279 * pointing to the location list.
3280 */
3281 if (bt_quickfix(win->w_buffer))
3282 if ((qi == &ql_info && win->w_llist_ref == NULL)
3283 || (qi != &ql_info && win->w_llist_ref == qi))
3284 return TRUE;
3285
3286 return FALSE;
3287}
3288
3289/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003290 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003291 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003292 */
3293 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003294qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003295{
3296 win_T *win;
3297
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003298 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003299 if (is_qf_win(win, qi))
3300 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003301
3302 return win;
3303}
3304
3305/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003306 * Find a quickfix buffer.
3307 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 */
3309 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003310qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003312 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003313 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
Bram Moolenaar9c102382006-05-03 21:26:49 +00003315 FOR_ALL_TAB_WINDOWS(tp, win)
3316 if (is_qf_win(win, qi))
3317 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003318
Bram Moolenaar9c102382006-05-03 21:26:49 +00003319 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320}
3321
3322/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003323 * Update the w:quickfix_title variable in the quickfix/location list window
3324 */
3325 static void
3326qf_update_win_titlevar(qf_info_T *qi)
3327{
3328 win_T *win;
3329 win_T *curwin_save;
3330
3331 if ((win = qf_find_win(qi)) != NULL)
3332 {
3333 curwin_save = curwin;
3334 curwin = win;
3335 qf_set_title_var(qi);
3336 curwin = curwin_save;
3337 }
3338}
3339
3340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 * Find the quickfix buffer. If it exists, update the contents.
3342 */
3343 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003344qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003347 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003351 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (buf != NULL)
3353 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003354 linenr_T old_line_count = buf->b_ml.ml_line_count;
3355
3356 if (old_last == NULL)
3357 /* set curwin/curbuf to buf and save a few things */
3358 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
Bram Moolenaard823fa92016-08-12 16:29:27 +02003360 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003361
Bram Moolenaar864293a2016-06-02 13:40:04 +02003362 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003363 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003364
Bram Moolenaar864293a2016-06-02 13:40:04 +02003365 if (old_last == NULL)
3366 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003367 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003368
3369 /* restore curwin/curbuf and a few other things */
3370 aucmd_restbuf(&aco);
3371 }
3372
3373 /* Only redraw when added lines are visible. This avoids flickering
3374 * when the added lines are not visible. */
3375 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3376 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378}
3379
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003380/*
3381 * Set "w:quickfix_title" if "qi" has a title.
3382 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003383 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003384qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003385{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003386 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3387 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003388 qi->qf_lists[qi->qf_curlist].qf_title);
3389}
3390
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391/*
3392 * Fill current buffer with quickfix errors, replacing any previous contents.
3393 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003394 * If "old_last" is not NULL append the items after this one.
3395 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3396 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 */
3398 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003399qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003401 linenr_T lnum;
3402 qfline_T *qfp;
3403 buf_T *errbuf;
3404 int len;
3405 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
Bram Moolenaar864293a2016-06-02 13:40:04 +02003407 if (old_last == NULL)
3408 {
3409 if (buf != curbuf)
3410 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003411 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003412 return;
3413 }
3414
3415 /* delete all existing lines */
3416 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3417 (void)ml_delete((linenr_T)1, FALSE);
3418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003421 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
3423 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003424 if (old_last == NULL)
3425 {
3426 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3427 lnum = 0;
3428 }
3429 else
3430 {
3431 qfp = old_last->qf_next;
3432 lnum = buf->b_ml.ml_line_count;
3433 }
3434 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
3436 if (qfp->qf_fnum != 0
3437 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3438 && errbuf->b_fname != NULL)
3439 {
3440 if (qfp->qf_type == 1) /* :helpgrep */
3441 STRCPY(IObuff, gettail(errbuf->b_fname));
3442 else
3443 STRCPY(IObuff, errbuf->b_fname);
3444 len = (int)STRLEN(IObuff);
3445 }
3446 else
3447 len = 0;
3448 IObuff[len++] = '|';
3449
3450 if (qfp->qf_lnum > 0)
3451 {
3452 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3453 len += (int)STRLEN(IObuff + len);
3454
3455 if (qfp->qf_col > 0)
3456 {
3457 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3458 len += (int)STRLEN(IObuff + len);
3459 }
3460
3461 sprintf((char *)IObuff + len, "%s",
3462 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3463 len += (int)STRLEN(IObuff + len);
3464 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003465 else if (qfp->qf_pattern != NULL)
3466 {
3467 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3468 len += (int)STRLEN(IObuff + len);
3469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 IObuff[len++] = '|';
3471 IObuff[len++] = ' ';
3472
3473 /* Remove newlines and leading whitespace from the text.
3474 * For an unrecognized line keep the indent, the compiler may
3475 * mark a word with ^^^^. */
3476 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3477 IObuff + len, IOSIZE - len);
3478
Bram Moolenaar864293a2016-06-02 13:40:04 +02003479 if (ml_append_buf(buf, lnum, IObuff,
3480 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003482 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003484 if (qfp == NULL)
3485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003487
3488 if (old_last == NULL)
3489 /* Delete the empty line which is now at the end */
3490 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
3492
3493 /* correct cursor position */
3494 check_lnums(TRUE);
3495
Bram Moolenaar864293a2016-06-02 13:40:04 +02003496 if (old_last == NULL)
3497 {
3498 /* Set the 'filetype' to "qf" each time after filling the buffer.
3499 * This resembles reading a file into a buffer, it's more logical when
3500 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003501#ifdef FEAT_AUTOCMD
3502 ++curbuf_lock;
3503#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003504 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3505 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003508 keep_filetype = TRUE; /* don't detect 'filetype' */
3509 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003511 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003513 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003514 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003516 /* make sure it will be redrawn */
3517 redraw_curbuf_later(NOT_VALID);
3518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
3520 /* Restore KeyTyped, setting 'filetype' may reset it. */
3521 KeyTyped = old_KeyTyped;
3522}
3523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003525 * Return TRUE when using ":vimgrep" for ":grep".
3526 */
3527 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003528grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003529{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003530 return ((cmdidx == CMD_grep
3531 || cmdidx == CMD_lgrep
3532 || cmdidx == CMD_grepadd
3533 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003534 && STRCMP("internal",
3535 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3536}
3537
3538/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003539 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 */
3541 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003542ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003544 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003546 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003548 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003549 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003550 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003551#ifdef FEAT_AUTOCMD
3552 char_u *au_name = NULL;
3553
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003554 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3555 if (grep_internal(eap->cmdidx))
3556 {
3557 ex_vimgrep(eap);
3558 return;
3559 }
3560
Bram Moolenaar7c626922005-02-07 22:01:03 +00003561 switch (eap->cmdidx)
3562 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003563 case CMD_make: au_name = (char_u *)"make"; break;
3564 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3565 case CMD_grep: au_name = (char_u *)"grep"; break;
3566 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3567 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3568 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003569 default: break;
3570 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003571 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3572 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003573 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003574# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003575 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003576 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003577# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003578 }
3579#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003580#ifdef FEAT_MBYTE
3581 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
Bram Moolenaara6557602006-02-04 22:43:20 +00003584 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3585 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003586 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003589 fname = get_mef_name();
3590 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003592 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
3594 /*
3595 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3596 */
3597 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3598 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003599 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 cmd = alloc(len);
3601 if (cmd == NULL)
3602 return;
3603 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3604 (char *)p_shq);
3605 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003606 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 /*
3608 * Output a newline if there's something else than the :make command that
3609 * was typed (in which case the cursor is in column 0).
3610 */
3611 if (msg_col == 0)
3612 msg_didout = FALSE;
3613 msg_start();
3614 MSG_PUTS(":!");
3615 msg_outtrans(cmd); /* show what we are doing */
3616
3617 /* let the shell know if we are redirecting output or not */
3618 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3619
3620#ifdef AMIGA
3621 out_flush();
3622 /* read window status report and redraw before message */
3623 (void)char_avail();
3624#endif
3625
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003626 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003627 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3628 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003629 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003630 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003631 if (wp != NULL)
3632 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003633#ifdef FEAT_AUTOCMD
3634 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003635 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003636 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3637 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003638 if (qi->qf_curlist < qi->qf_listcount)
3639 res = qi->qf_lists[qi->qf_curlist].qf_count;
3640 else
3641 res = 0;
3642 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003643#endif
3644 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003645 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
Bram Moolenaar7c626922005-02-07 22:01:03 +00003647 mch_remove(fname);
3648 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 vim_free(cmd);
3650}
3651
3652/*
3653 * Return the name for the errorfile, in allocated memory.
3654 * Find a new unique name when 'makeef' contains "##".
3655 * Returns NULL for error.
3656 */
3657 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003658get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
3660 char_u *p;
3661 char_u *name;
3662 static int start = -1;
3663 static int off = 0;
3664#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003665 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666#endif
3667
3668 if (*p_mef == NUL)
3669 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003670 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (name == NULL)
3672 EMSG(_(e_notmp));
3673 return name;
3674 }
3675
3676 for (p = p_mef; *p; ++p)
3677 if (p[0] == '#' && p[1] == '#')
3678 break;
3679
3680 if (*p == NUL)
3681 return vim_strsave(p_mef);
3682
3683 /* Keep trying until the name doesn't exist yet. */
3684 for (;;)
3685 {
3686 if (start == -1)
3687 start = mch_get_pid();
3688 else
3689 off += 19;
3690
3691 name = alloc((unsigned)STRLEN(p_mef) + 30);
3692 if (name == NULL)
3693 break;
3694 STRCPY(name, p_mef);
3695 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3696 STRCAT(name, p + 2);
3697 if (mch_getperm(name) < 0
3698#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003699 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 && mch_lstat((char *)name, &sb) < 0
3701#endif
3702 )
3703 break;
3704 vim_free(name);
3705 }
3706 return name;
3707}
3708
3709/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003710 * Returns the number of valid entries in the current quickfix/location list.
3711 */
3712 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003713qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003714{
3715 qf_info_T *qi = &ql_info;
3716 qfline_T *qfp;
3717 int i, sz = 0;
3718 int prev_fnum = 0;
3719
3720 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3721 {
3722 /* Location list */
3723 qi = GET_LOC_LIST(curwin);
3724 if (qi == NULL)
3725 return 0;
3726 }
3727
3728 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003729 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003730 ++i, qfp = qfp->qf_next)
3731 {
3732 if (qfp->qf_valid)
3733 {
3734 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3735 sz++; /* Count all valid entries */
3736 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3737 {
3738 /* Count the number of files */
3739 sz++;
3740 prev_fnum = qfp->qf_fnum;
3741 }
3742 }
3743 }
3744
3745 return sz;
3746}
3747
3748/*
3749 * Returns the current index of the quickfix/location list.
3750 * Returns 0 if there is an error.
3751 */
3752 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003753qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003754{
3755 qf_info_T *qi = &ql_info;
3756
3757 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3758 {
3759 /* Location list */
3760 qi = GET_LOC_LIST(curwin);
3761 if (qi == NULL)
3762 return 0;
3763 }
3764
3765 return qi->qf_lists[qi->qf_curlist].qf_index;
3766}
3767
3768/*
3769 * Returns the current index in the quickfix/location list (counting only valid
3770 * entries). If no valid entries are in the list, then returns 1.
3771 */
3772 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003773qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003774{
3775 qf_info_T *qi = &ql_info;
3776 qf_list_T *qfl;
3777 qfline_T *qfp;
3778 int i, eidx = 0;
3779 int prev_fnum = 0;
3780
3781 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3782 {
3783 /* Location list */
3784 qi = GET_LOC_LIST(curwin);
3785 if (qi == NULL)
3786 return 1;
3787 }
3788
3789 qfl = &qi->qf_lists[qi->qf_curlist];
3790 qfp = qfl->qf_start;
3791
3792 /* check if the list has valid errors */
3793 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3794 return 1;
3795
3796 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3797 {
3798 if (qfp->qf_valid)
3799 {
3800 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3801 {
3802 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3803 {
3804 /* Count the number of files */
3805 eidx++;
3806 prev_fnum = qfp->qf_fnum;
3807 }
3808 }
3809 else
3810 eidx++;
3811 }
3812 }
3813
3814 return eidx ? eidx : 1;
3815}
3816
3817/*
3818 * Get the 'n'th valid error entry in the quickfix or location list.
3819 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3820 * For :cdo and :ldo returns the 'n'th valid error entry.
3821 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3822 */
3823 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003824qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003825{
3826 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3827 qfline_T *qfp = qfl->qf_start;
3828 int i, eidx;
3829 int prev_fnum = 0;
3830
3831 /* check if the list has valid errors */
3832 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3833 return 1;
3834
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003835 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003836 i++, qfp = qfp->qf_next)
3837 {
3838 if (qfp->qf_valid)
3839 {
3840 if (fdo)
3841 {
3842 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3843 {
3844 /* Count the number of files */
3845 eidx++;
3846 prev_fnum = qfp->qf_fnum;
3847 }
3848 }
3849 else
3850 eidx++;
3851 }
3852
3853 if (eidx == n)
3854 break;
3855 }
3856
3857 if (i <= qfl->qf_count)
3858 return i;
3859 else
3860 return 1;
3861}
3862
3863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003865 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003866 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 */
3868 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003869ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003871 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003872 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003873
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003874 if (eap->cmdidx == CMD_ll
3875 || eap->cmdidx == CMD_lrewind
3876 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003877 || eap->cmdidx == CMD_llast
3878 || eap->cmdidx == CMD_ldo
3879 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003880 {
3881 qi = GET_LOC_LIST(curwin);
3882 if (qi == NULL)
3883 {
3884 EMSG(_(e_loclist));
3885 return;
3886 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003887 }
3888
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003889 if (eap->addr_count > 0)
3890 errornr = (int)eap->line2;
3891 else
3892 {
3893 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3894 errornr = 0;
3895 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3896 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3897 errornr = 1;
3898 else
3899 errornr = 32767;
3900 }
3901
3902 /* For cdo and ldo commands, jump to the nth valid error.
3903 * For cfdo and lfdo commands, jump to the nth valid file entry.
3904 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02003905 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
3906 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003907 errornr = qf_get_nth_valid_entry(qi,
3908 eap->addr_count > 0 ? (int)eap->line1 : 1,
3909 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3910
3911 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912}
3913
3914/*
3915 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003916 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003917 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 */
3919 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003920ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003922 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003923 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003924
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003925 if (eap->cmdidx == CMD_lnext
3926 || eap->cmdidx == CMD_lNext
3927 || eap->cmdidx == CMD_lprevious
3928 || eap->cmdidx == CMD_lnfile
3929 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003930 || eap->cmdidx == CMD_lpfile
3931 || eap->cmdidx == CMD_ldo
3932 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003933 {
3934 qi = GET_LOC_LIST(curwin);
3935 if (qi == NULL)
3936 {
3937 EMSG(_(e_loclist));
3938 return;
3939 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003940 }
3941
Bram Moolenaar55b69262017-08-13 13:42:01 +02003942 if (eap->addr_count > 0
3943 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
3944 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003945 errornr = (int)eap->line2;
3946 else
3947 errornr = 1;
3948
3949 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3950 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003952 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3953 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003955 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3956 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 ? BACKWARD_FILE
3958 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003959 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960}
3961
3962/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003963 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003964 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 */
3966 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003967ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003969 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003970 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003971 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003972#ifdef FEAT_AUTOCMD
3973 char_u *au_name = NULL;
3974#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003975
3976 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003977 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003978 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003979
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003980#ifdef FEAT_AUTOCMD
3981 switch (eap->cmdidx)
3982 {
3983 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3984 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3985 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3986 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3987 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3988 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3989 default: break;
3990 }
3991 if (au_name != NULL)
3992 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3993#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003994#ifdef FEAT_MBYTE
3995 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3996#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003997#ifdef FEAT_BROWSE
3998 if (cmdmod.browse)
3999 {
4000 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4001 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4002 if (browse_file == NULL)
4003 return;
4004 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4005 vim_free(browse_file);
4006 }
4007 else
4008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004010 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004011
4012 /*
4013 * This function is used by the :cfile, :cgetfile and :caddfile
4014 * commands.
4015 * :cfile always creates a new quickfix list and jumps to the
4016 * first error.
4017 * :cgetfile creates a new quickfix list but doesn't jump to the
4018 * first error.
4019 * :caddfile adds to an existing quickfix list. If there is no
4020 * quickfix list then a new list is created.
4021 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004022 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004023 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004024 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004025 && (eap->cmdidx == CMD_cfile
4026 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004027 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004028#ifdef FEAT_AUTOCMD
4029 if (au_name != NULL)
4030 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4031#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004032 if (wp != NULL)
4033 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004034 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004035 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004036
4037 else
4038 {
4039#ifdef FEAT_AUTOCMD
4040 if (au_name != NULL)
4041 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4042#endif
4043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044}
4045
4046/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004047 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004048 * ":vimgrepadd {pattern} file(s)"
4049 * ":lvimgrep {pattern} file(s)"
4050 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004051 */
4052 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004053ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004054{
Bram Moolenaar81695252004-12-29 20:58:21 +00004055 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004056 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004057 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004058 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004059 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004060 char_u *s;
4061 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004062 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004063 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004064#ifdef FEAT_AUTOCMD
4065 qfline_T *cur_qf_start;
4066#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004067 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004068 buf_T *buf;
4069 int duplicate_name = FALSE;
4070 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004071 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004072 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004073 buf_T *first_match_buf = NULL;
4074 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004075 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004076#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4077 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004078#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004079 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004080 int flags = 0;
4081 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004082 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004083 char_u *dirname_start = NULL;
4084 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004085 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004086#ifdef FEAT_AUTOCMD
4087 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004088
4089 switch (eap->cmdidx)
4090 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004091 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4092 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4093 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004094 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004095 case CMD_grep: au_name = (char_u *)"grep"; break;
4096 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4097 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4098 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004099 default: break;
4100 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004101 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4102 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004103 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004104# ifdef FEAT_EVAL
4105 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004106 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004107# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004108 }
4109#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004110
Bram Moolenaar754b5602006-02-09 23:53:20 +00004111 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004112 || eap->cmdidx == CMD_lvimgrep
4113 || eap->cmdidx == CMD_lgrepadd
4114 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004115 {
4116 qi = ll_get_or_alloc_list(curwin);
4117 if (qi == NULL)
4118 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004119 }
4120
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004121 if (eap->addr_count > 0)
4122 tomatch = eap->line2;
4123 else
4124 tomatch = MAXLNUM;
4125
Bram Moolenaar81695252004-12-29 20:58:21 +00004126 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004127 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004128 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004129 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004130 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004131 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004132 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004133 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004134 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004135
4136 if (s != NULL && *s == NUL)
4137 {
4138 /* Pattern is empty, use last search pattern. */
4139 if (last_search_pat() == NULL)
4140 {
4141 EMSG(_(e_noprevre));
4142 goto theend;
4143 }
4144 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4145 }
4146 else
4147 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4148
Bram Moolenaar86b68352004-12-27 21:59:20 +00004149 if (regmatch.regprog == NULL)
4150 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004151 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004152 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004153
4154 p = skipwhite(p);
4155 if (*p == NUL)
4156 {
4157 EMSG(_("E683: File name missing or invalid pattern"));
4158 goto theend;
4159 }
4160
Bram Moolenaar55b69262017-08-13 13:42:01 +02004161 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4162 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004163 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004164 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004165 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004166
4167 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004168 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004169 goto theend;
4170 if (fcount == 0)
4171 {
4172 EMSG(_(e_nomatch));
4173 goto theend;
4174 }
4175
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004176 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4177 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004178 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004179 {
4180 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004181 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004182 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004183
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004184 /* Remember the current directory, because a BufRead autocommand that does
4185 * ":lcd %:p:h" changes the meaning of short path names. */
4186 mch_dirname(dirname_start, MAXPATHL);
4187
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004188#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004189 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004190 * changing the current quickfix list. */
4191 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4192#endif
4193
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004194 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004195 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004196 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004197 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004198 if (time(NULL) > seconds)
4199 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004200 /* Display the file name every second or so, show the user we are
4201 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004202 seconds = time(NULL);
4203 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004204 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004205 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004206 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004207 else
4208 {
4209 msg_outtrans(p);
4210 vim_free(p);
4211 }
4212 msg_clr_eos();
4213 msg_didout = FALSE; /* overwrite this message */
4214 msg_nowait = TRUE; /* don't wait for this message */
4215 msg_col = 0;
4216 out_flush();
4217 }
4218
Bram Moolenaar81695252004-12-29 20:58:21 +00004219 buf = buflist_findname_exp(fnames[fi]);
4220 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4221 {
4222 /* Remember that a buffer with this name already exists. */
4223 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004224 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004225 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004226
4227#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4228 /* Don't do Filetype autocommands to avoid loading syntax and
4229 * indent scripts, a great speed improvement. */
4230 save_ei = au_event_disable(",Filetype");
4231#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004232 /* Don't use modelines here, it's useless. */
4233 save_mls = p_mls;
4234 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004235
4236 /* Load file into a buffer, so that 'fileencoding' is detected,
4237 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004238 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004239
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004240 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004241#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4242 au_event_restore(save_ei);
4243#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004244 }
4245 else
4246 /* Use existing, loaded buffer. */
4247 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004248
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004249#ifdef FEAT_AUTOCMD
4250 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4251 {
4252 int idx;
4253
4254 /* Autocommands changed the quickfix list. Find the one we were
4255 * using and restore it. */
4256 for (idx = 0; idx < LISTCOUNT; ++idx)
4257 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4258 {
4259 qi->qf_curlist = idx;
4260 break;
4261 }
4262 if (idx == LISTCOUNT)
4263 {
4264 /* List cannot be found, create a new one. */
4265 qf_new_list(qi, *eap->cmdlinep);
4266 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4267 }
4268 }
4269#endif
4270
Bram Moolenaar81695252004-12-29 20:58:21 +00004271 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004272 {
4273 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004274 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004275 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004276 else
4277 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004278 /* Try for a match in all lines of the buffer.
4279 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004280 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004281 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4282 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004283 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004284 col = 0;
4285 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004286 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004287 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004288 /* Pass the buffer number so that it gets used even for a
4289 * dummy buffer, unless duplicate_name is set, then the
4290 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004291 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004292 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004293 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004294 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004295 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004296 ml_get_buf(buf,
4297 regmatch.startpos[0].lnum + lnum, FALSE),
4298 regmatch.startpos[0].lnum + lnum,
4299 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004300 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004301 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004302 0, /* nr */
4303 0, /* type */
4304 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004305 ) == FAIL)
4306 {
4307 got_int = TRUE;
4308 break;
4309 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004310 found_match = TRUE;
4311 if (--tomatch == 0)
4312 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004313 if ((flags & VGR_GLOBAL) == 0
4314 || regmatch.endpos[0].lnum > 0)
4315 break;
4316 col = regmatch.endpos[0].col
4317 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004318 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004319 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004320 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004321 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004322 if (got_int)
4323 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004324 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004325#ifdef FEAT_AUTOCMD
4326 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4327#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004328
4329 if (using_dummy)
4330 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004331 if (found_match && first_match_buf == NULL)
4332 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004333 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004334 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004335 /* Never keep a dummy buffer if there is another buffer
4336 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004337 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004338 buf = NULL;
4339 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004340 else if (!cmdmod.hide
4341 || buf->b_p_bh[0] == 'u' /* "unload" */
4342 || buf->b_p_bh[0] == 'w' /* "wipe" */
4343 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004344 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004345 /* When no match was found we don't need to remember the
4346 * buffer, wipe it out. If there was a match and it
4347 * wasn't the first one or we won't jump there: only
4348 * unload the buffer.
4349 * Ignore 'hidden' here, because it may lead to having too
4350 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004351 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004352 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004353 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004354 buf = NULL;
4355 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004356 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004357 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004358 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004359 /* Keeping the buffer, remove the dummy flag. */
4360 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004361 buf = NULL;
4362 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004363 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004364
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004365 if (buf != NULL)
4366 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004367 /* Keeping the buffer, remove the dummy flag. */
4368 buf->b_flags &= ~BF_DUMMY;
4369
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004370 /* If the buffer is still loaded we need to use the
4371 * directory we jumped to below. */
4372 if (buf == first_match_buf
4373 && target_dir == NULL
4374 && STRCMP(dirname_start, dirname_now) != 0)
4375 target_dir = vim_strsave(dirname_now);
4376
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004377 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004378 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004379 * need to be done (again). But not the window-local
4380 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004381 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004382#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004383 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4384 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004385#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004386 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004387 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004388 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004389 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004390 }
4391 }
4392
4393 FreeWild(fcount, fnames);
4394
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004395 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4396 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4397 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004398
Bram Moolenaar864293a2016-06-02 13:40:04 +02004399 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004400
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004401#ifdef FEAT_AUTOCMD
4402 if (au_name != NULL)
4403 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4404 curbuf->b_fname, TRUE, curbuf);
4405#endif
4406
Bram Moolenaar86b68352004-12-27 21:59:20 +00004407 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004408 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004409 {
4410 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004411 {
4412 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004413 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004414 if (buf != curbuf)
4415 /* If we jumped to another buffer redrawing will already be
4416 * taken care of. */
4417 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004418
4419 /* Jump to the directory used after loading the buffer. */
4420 if (curbuf == first_match_buf && target_dir != NULL)
4421 {
4422 exarg_T ea;
4423
4424 ea.arg = target_dir;
4425 ea.cmdidx = CMD_lcd;
4426 ex_cd(&ea);
4427 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004428 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004429 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004430 else
4431 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004432
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004433 /* If we loaded a dummy buffer into the current window, the autocommands
4434 * may have messed up things, need to redraw and recompute folds. */
4435 if (redraw_for_dummy)
4436 {
4437#ifdef FEAT_FOLDING
4438 foldUpdateAll(curwin);
4439#else
4440 redraw_later(NOT_VALID);
4441#endif
4442 }
4443
Bram Moolenaar86b68352004-12-27 21:59:20 +00004444theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004445 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004446 vim_free(dirname_now);
4447 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004448 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004449 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004450}
4451
4452/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004453 * Restore current working directory to "dirname_start" if they differ, taking
4454 * into account whether it is set locally or globally.
4455 */
4456 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004457restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004458{
4459 char_u *dirname_now = alloc(MAXPATHL);
4460
4461 if (NULL != dirname_now)
4462 {
4463 mch_dirname(dirname_now, MAXPATHL);
4464 if (STRCMP(dirname_start, dirname_now) != 0)
4465 {
4466 /* If the directory has changed, change it back by building up an
4467 * appropriate ex command and executing it. */
4468 exarg_T ea;
4469
4470 ea.arg = dirname_start;
4471 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4472 ex_cd(&ea);
4473 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004474 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004475 }
4476}
4477
4478/*
4479 * Load file "fname" into a dummy buffer and return the buffer pointer,
4480 * placing the directory resulting from the buffer load into the
4481 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4482 * prior to calling this function. Restores directory to "dirname_start" prior
4483 * to returning, if autocmds or the 'autochdir' option have changed it.
4484 *
4485 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4486 * or wipe_dummy_buffer() later!
4487 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004488 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004489 */
4490 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004491load_dummy_buffer(
4492 char_u *fname,
4493 char_u *dirname_start, /* in: old directory */
4494 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004495{
4496 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004497 bufref_T newbufref;
4498 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004499 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004500 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004501
4502 /* Allocate a buffer without putting it in the buffer list. */
4503 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4504 if (newbuf == NULL)
4505 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004506 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004507
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004508 /* Init the options. */
4509 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4510
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004511 /* need to open the memfile before putting the buffer in a window */
4512 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004513 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004514 /* set curwin/curbuf to buf and save a few things */
4515 aucmd_prepbuf(&aco, newbuf);
4516
4517 /* Need to set the filename for autocommands. */
4518 (void)setfname(curbuf, fname, NULL, FALSE);
4519
Bram Moolenaar81695252004-12-29 20:58:21 +00004520 /* Create swap file now to avoid the ATTENTION message. */
4521 check_need_swap(TRUE);
4522
4523 /* Remove the "dummy" flag, otherwise autocommands may not
4524 * work. */
4525 curbuf->b_flags &= ~BF_DUMMY;
4526
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004527 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004528 if (readfile(fname, NULL,
4529 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4530 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004531 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004532 && !(curbuf->b_flags & BF_NEW))
4533 {
4534 failed = FALSE;
4535 if (curbuf != newbuf)
4536 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004537 /* Bloody autocommands changed the buffer! Can happen when
4538 * using netrw and editing a remote file. Use the current
4539 * buffer instead, delete the dummy one after restoring the
4540 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004541 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004542 newbuf = curbuf;
4543 }
4544 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004545
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004546 /* restore curwin/curbuf and a few other things */
4547 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004548 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4549 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004550
4551 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4552 * skip it. */
4553 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004554 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004555
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004556 /*
4557 * When autocommands/'autochdir' option changed directory: go back.
4558 * Let the caller know what the resulting dir was first, in case it is
4559 * important.
4560 */
4561 mch_dirname(resulting_dir, MAXPATHL);
4562 restore_start_dir(dirname_start);
4563
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004564 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004565 return NULL;
4566 if (failed)
4567 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004568 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004569 return NULL;
4570 }
4571 return newbuf;
4572}
4573
4574/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004575 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4576 * directory to "dirname_start" prior to returning, if autocmds or the
4577 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004578 */
4579 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004580wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004581{
4582 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004583 {
4584#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4585 cleanup_T cs;
4586
4587 /* Reset the error/interrupt/exception state here so that aborting()
4588 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4589 * work when got_int is set. */
4590 enter_cleanup(&cs);
4591#endif
4592
Bram Moolenaar81695252004-12-29 20:58:21 +00004593 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004594
4595#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4596 /* Restore the error/interrupt/exception state if not discarded by a
4597 * new aborting error, interrupt, or uncaught exception. */
4598 leave_cleanup(&cs);
4599#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004600 /* When autocommands/'autochdir' option changed directory: go back. */
4601 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004602 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004603}
4604
4605/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004606 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4607 * directory to "dirname_start" prior to returning, if autocmds or the
4608 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004609 */
4610 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004611unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004612{
4613 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004614 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004615 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004616
4617 /* When autocommands/'autochdir' option changed directory: go back. */
4618 restore_start_dir(dirname_start);
4619 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004620}
4621
Bram Moolenaar05159a02005-02-26 23:04:13 +00004622#if defined(FEAT_EVAL) || defined(PROTO)
4623/*
4624 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004625 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004626 */
4627 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004628get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004629{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004630 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004631 dict_T *dict;
4632 char_u buf[2];
4633 qfline_T *qfp;
4634 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004635 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004636
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004637 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004638 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004639 qi = &ql_info;
4640 if (wp != NULL)
4641 {
4642 qi = GET_LOC_LIST(wp);
4643 if (qi == NULL)
4644 return FAIL;
4645 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004646 }
4647
Bram Moolenaard823fa92016-08-12 16:29:27 +02004648 if (qf_idx == -1)
4649 qf_idx = qi->qf_curlist;
4650
4651 if (qf_idx >= qi->qf_listcount
4652 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004653 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004654
Bram Moolenaard823fa92016-08-12 16:29:27 +02004655 qfp = qi->qf_lists[qf_idx].qf_start;
4656 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004657 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004658 /* Handle entries with a non-existing buffer number. */
4659 bufnum = qfp->qf_fnum;
4660 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4661 bufnum = 0;
4662
Bram Moolenaar05159a02005-02-26 23:04:13 +00004663 if ((dict = dict_alloc()) == NULL)
4664 return FAIL;
4665 if (list_append_dict(list, dict) == FAIL)
4666 return FAIL;
4667
4668 buf[0] = qfp->qf_type;
4669 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004670 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004671 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4672 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4673 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4674 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004675 || dict_add_nr_str(dict, "pattern", 0L,
4676 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4677 || dict_add_nr_str(dict, "text", 0L,
4678 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004679 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4680 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4681 return FAIL;
4682
4683 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004684 if (qfp == NULL)
4685 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004686 }
4687 return OK;
4688}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004689
4690/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004691 * Flags used by getqflist()/getloclist() to determine which fields to return.
4692 */
4693enum {
4694 QF_GETLIST_NONE = 0x0,
4695 QF_GETLIST_TITLE = 0x1,
4696 QF_GETLIST_ITEMS = 0x2,
4697 QF_GETLIST_NR = 0x4,
4698 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004699 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004700 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004701 QF_GETLIST_IDX = 0x40,
4702 QF_GETLIST_SIZE = 0x80,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004703 QF_GETLIST_ALL = 0xFF
4704};
4705
4706/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004707 * Parse text from 'di' and return the quickfix list items
4708 */
4709 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004710qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004711{
4712 int status = FAIL;
4713 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004714 char_u *errorformat = p_efm;
4715 dictitem_T *efm_di;
4716 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004717
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004718 /* Only a List value is supported */
4719 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004720 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004721 /* If errorformat is supplied then use it, otherwise use the 'efm'
4722 * option setting
4723 */
4724 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4725 {
4726 if (efm_di->di_tv.v_type != VAR_STRING ||
4727 efm_di->di_tv.vval.v_string == NULL)
4728 return FAIL;
4729 errorformat = efm_di->di_tv.vval.v_string;
4730 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004731
Bram Moolenaar36538222017-09-02 19:51:44 +02004732 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004733 if (l == NULL)
4734 return FAIL;
4735
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004736 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4737 if (qi != NULL)
4738 {
4739 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4740 qi->qf_refcount++;
4741
Bram Moolenaar36538222017-09-02 19:51:44 +02004742 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004743 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4744 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004745 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004746 qf_free(qi, 0);
4747 }
4748 free(qi);
4749 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004750 dict_add_list(retdict, "items", l);
4751 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004752 }
4753
4754 return status;
4755}
4756
4757/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004758 * Return the quickfix/location list number with the given identifier.
4759 * Returns -1 if list is not found.
4760 */
4761 static int
4762qf_id2nr(qf_info_T *qi, int_u qfid)
4763{
4764 int qf_idx;
4765
4766 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4767 if (qi->qf_lists[qf_idx].qf_id == qfid)
4768 return qf_idx;
4769 return -1;
4770}
4771
4772/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004773 * Return quickfix/location list details (title) as a
4774 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4775 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004776 */
4777 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004778qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004779{
4780 qf_info_T *qi = &ql_info;
4781 int status = OK;
4782 int qf_idx;
4783 dictitem_T *di;
4784 int flags = QF_GETLIST_NONE;
4785
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004786 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004787 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004788
Bram Moolenaard823fa92016-08-12 16:29:27 +02004789 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004790 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004791
4792 /* List is not present or is empty */
4793 if (qi == NULL || qi->qf_listcount == 0)
4794 {
4795 /* If querying for the size of the list, return 0 */
4796 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4797 && (di->di_tv.v_type == VAR_STRING)
4798 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4799 return dict_add_nr_str(retdict, "nr", 0, NULL);
4800 return FAIL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004801 }
4802
4803 qf_idx = qi->qf_curlist; /* default is the current list */
4804 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4805 {
4806 /* Use the specified quickfix/location list */
4807 if (di->di_tv.v_type == VAR_NUMBER)
4808 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004809 /* for zero use the current list */
4810 if (di->di_tv.vval.v_number != 0)
4811 {
4812 qf_idx = di->di_tv.vval.v_number - 1;
4813 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4814 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004815 }
Bram Moolenaar55b69262017-08-13 13:42:01 +02004816 }
4817 else if ((di->di_tv.v_type == VAR_STRING)
4818 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004819 /* Get the last quickfix list number */
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004820 qf_idx = qi->qf_listcount - 1;
4821 else
4822 return FAIL;
4823 flags |= QF_GETLIST_NR;
4824 }
4825
4826 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
4827 {
4828 /* Look for a list with the specified id */
4829 if (di->di_tv.v_type == VAR_NUMBER)
4830 {
4831 /* For zero, use the current list or the list specifed by 'nr' */
4832 if (di->di_tv.vval.v_number != 0)
4833 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004834 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
4835 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004836 return FAIL; /* List not found */
4837 }
4838 flags |= QF_GETLIST_ID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004839 }
4840 else
4841 return FAIL;
4842 }
4843
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004844 if (dict_find(what, (char_u *)"all", -1) != NULL)
4845 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004846
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004847 if (dict_find(what, (char_u *)"title", -1) != NULL)
4848 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004849
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004850 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4851 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004852
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004853 if (dict_find(what, (char_u *)"context", -1) != NULL)
4854 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004855
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004856 if (dict_find(what, (char_u *)"items", -1) != NULL)
4857 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004858
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004859 if (dict_find(what, (char_u *)"idx", -1) != NULL)
4860 flags |= QF_GETLIST_IDX;
4861
4862 if (dict_find(what, (char_u *)"size", -1) != NULL)
4863 flags |= QF_GETLIST_SIZE;
4864
Bram Moolenaard823fa92016-08-12 16:29:27 +02004865 if (flags & QF_GETLIST_TITLE)
4866 {
4867 char_u *t;
4868 t = qi->qf_lists[qf_idx].qf_title;
4869 if (t == NULL)
4870 t = (char_u *)"";
4871 status = dict_add_nr_str(retdict, "title", 0L, t);
4872 }
4873 if ((status == OK) && (flags & QF_GETLIST_NR))
4874 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4875 if ((status == OK) && (flags & QF_GETLIST_WINID))
4876 {
4877 win_T *win;
4878 win = qf_find_win(qi);
4879 if (win != NULL)
4880 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4881 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004882 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4883 {
4884 list_T *l = list_alloc();
4885 if (l != NULL)
4886 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004887 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004888 dict_add_list(retdict, "items", l);
4889 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004890 else
4891 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004892 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004893
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004894 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4895 {
4896 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4897 {
4898 di = dictitem_alloc((char_u *)"context");
4899 if (di != NULL)
4900 {
4901 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004902 status = dict_add(retdict, di);
4903 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004904 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004905 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004906 else
4907 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004908 }
4909 else
4910 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4911 }
4912
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004913 if ((status == OK) && (flags & QF_GETLIST_ID))
4914 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
4915 NULL);
4916
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004917 if ((status == OK) && (flags & QF_GETLIST_IDX))
4918 {
4919 int idx = qi->qf_lists[qf_idx].qf_index;
4920 if (qi->qf_lists[qf_idx].qf_count == 0)
4921 /* For empty lists, qf_index is set to 1 */
4922 idx = 0;
4923 status = dict_add_nr_str(retdict, "idx", idx, NULL);
4924 }
4925
4926 if ((status == OK) && (flags & QF_GETLIST_SIZE))
4927 status = dict_add_nr_str(retdict, "size",
4928 qi->qf_lists[qf_idx].qf_count, NULL);
4929
Bram Moolenaard823fa92016-08-12 16:29:27 +02004930 return status;
4931}
4932
4933/*
4934 * Add list of entries to quickfix/location list. Each list entry is
4935 * a dictionary with item information.
4936 */
4937 static int
4938qf_add_entries(
4939 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004940 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004941 list_T *list,
4942 char_u *title,
4943 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004944{
4945 listitem_T *li;
4946 dict_T *d;
4947 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004948 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004949 long lnum;
4950 int col, nr;
4951 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004952 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004953 int valid, status;
4954 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004955 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004956
Bram Moolenaara3921f42017-06-04 15:30:34 +02004957 if (action == ' ' || qf_idx == qi->qf_listcount)
4958 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004959 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004960 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004961 qf_idx = qi->qf_curlist;
4962 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02004963 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004964 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004965 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004966 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004967 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004968 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004969 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004970 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004971
4972 for (li = list->lv_first; li != NULL; li = li->li_next)
4973 {
4974 if (li->li_tv.v_type != VAR_DICT)
4975 continue; /* Skip non-dict items */
4976
4977 d = li->li_tv.vval.v_dict;
4978 if (d == NULL)
4979 continue;
4980
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004981 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004982 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4983 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4984 col = (int)get_dict_number(d, (char_u *)"col");
4985 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4986 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004987 type = get_dict_string(d, (char_u *)"type", TRUE);
4988 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4989 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004990 if (text == NULL)
4991 text = vim_strsave((char_u *)"");
4992
4993 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004994 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004995 valid = FALSE;
4996
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004997 /* Mark entries with non-existing buffer number as not valid. Give the
4998 * error message only once. */
4999 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5000 {
5001 if (!did_bufnr_emsg)
5002 {
5003 did_bufnr_emsg = TRUE;
5004 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5005 }
5006 valid = FALSE;
5007 bufnum = 0;
5008 }
5009
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005010 /* If the 'valid' field is present it overrules the detected value. */
5011 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5012 valid = (int)get_dict_number(d, (char_u *)"valid");
5013
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005014 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005015 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005016 NULL, /* dir */
5017 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005018 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005019 text,
5020 lnum,
5021 col,
5022 vcol, /* vis_col */
5023 pattern, /* search pattern */
5024 nr,
5025 type == NULL ? NUL : *type,
5026 valid);
5027
5028 vim_free(filename);
5029 vim_free(pattern);
5030 vim_free(text);
5031 vim_free(type);
5032
5033 if (status == FAIL)
5034 {
5035 retval = FAIL;
5036 break;
5037 }
5038 }
5039
Bram Moolenaara3921f42017-06-04 15:30:34 +02005040 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005041 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005042 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005043 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005044 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005045 if (action != 'a')
5046 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005047 qi->qf_lists[qf_idx].qf_ptr =
5048 qi->qf_lists[qf_idx].qf_start;
5049 if (qi->qf_lists[qf_idx].qf_count > 0)
5050 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005051 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005052
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005053 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005054 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005055
5056 return retval;
5057}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005058
5059 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005060qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005061{
5062 dictitem_T *di;
5063 int retval = FAIL;
5064 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005065 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005066 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005067
5068 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5069 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005070
5071 qf_idx = qi->qf_curlist; /* default is the current list */
5072 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5073 {
5074 /* Use the specified quickfix/location list */
5075 if (di->di_tv.v_type == VAR_NUMBER)
5076 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005077 /* for zero use the current list */
5078 if (di->di_tv.vval.v_number != 0)
5079 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005080
Bram Moolenaar55b69262017-08-13 13:42:01 +02005081 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5082 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005083 /*
5084 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005085 * non-available list and add the new list at the end of the
5086 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005087 */
5088 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005089 qf_idx = qi->qf_listcount - 1;
5090 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005091 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005092 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005093 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005094 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005095 }
5096 else if (di->di_tv.v_type == VAR_STRING
5097 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005098 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005099 if (qi->qf_listcount > 0)
5100 qf_idx = qi->qf_listcount - 1;
5101 else if (newlist)
5102 qf_idx = 0;
5103 else
5104 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005105 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005106 else
5107 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005108 }
5109
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005110 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5111 {
5112 /* Use the quickfix/location list with the specified id */
5113 if (di->di_tv.v_type == VAR_NUMBER)
5114 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005115 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5116 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005117 return FAIL; /* List not found */
5118 }
5119 else
5120 return FAIL;
5121 }
5122
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005123 if (newlist)
5124 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005125 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005126 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005127 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005128 }
5129
5130 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5131 {
5132 if (di->di_tv.v_type == VAR_STRING)
5133 {
5134 vim_free(qi->qf_lists[qf_idx].qf_title);
5135 qi->qf_lists[qf_idx].qf_title =
5136 get_dict_string(what, (char_u *)"title", TRUE);
5137 if (qf_idx == qi->qf_curlist)
5138 qf_update_win_titlevar(qi);
5139 retval = OK;
5140 }
5141 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005142
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005143 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5144 {
5145 if (di->di_tv.v_type == VAR_LIST)
5146 {
5147 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5148
5149 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5150 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005151 if (action == 'r')
5152 {
5153 /*
5154 * When replacing the quickfix list entries using
5155 * qf_add_entries(), the title is set with a ':' prefix.
5156 * Restore the title with the saved title.
5157 */
5158 vim_free(qi->qf_lists[qf_idx].qf_title);
5159 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5160 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005161 vim_free(title_save);
5162 }
5163 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005164
Bram Moolenaar36538222017-09-02 19:51:44 +02005165 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5166 {
5167 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5168 return FAIL;
5169 errorformat = di->di_tv.vval.v_string;
5170 }
5171
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005172 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005173 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005174 /* Only a List value is supported */
5175 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005176 {
5177 if (action == 'r')
5178 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005179 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005180 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5181 retval = OK;
5182 }
5183 else
5184 return FAIL;
5185 }
5186
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005187 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5188 {
5189 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005190
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005191 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005192 ctx = alloc_tv();
5193 if (ctx != NULL)
5194 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005195 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005196 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005197 }
5198
Bram Moolenaard823fa92016-08-12 16:29:27 +02005199 return retval;
5200}
5201
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005202/*
5203 * Find the non-location list window with the specified location list.
5204 */
5205 static win_T *
5206find_win_with_ll(qf_info_T *qi)
5207{
5208 win_T *wp = NULL;
5209
5210 FOR_ALL_WINDOWS(wp)
5211 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5212 return wp;
5213
5214 return NULL;
5215}
5216
5217/*
5218 * Free the entire quickfix/location list stack.
5219 * If the quickfix/location list window is open, then clear it.
5220 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005221 static void
5222qf_free_stack(win_T *wp, qf_info_T *qi)
5223{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005224 win_T *qfwin = qf_find_win(qi);
5225 win_T *llwin = NULL;
5226 win_T *orig_wp = wp;
5227
5228 if (qfwin != NULL)
5229 {
5230 /* If the quickfix/location list window is open, then clear it */
5231 if (qi->qf_curlist < qi->qf_listcount)
5232 qf_free(qi, qi->qf_curlist);
5233 qf_update_buffer(qi, NULL);
5234 }
5235
5236 if (wp != NULL && IS_LL_WINDOW(wp))
5237 {
5238 /* If in the location list window, then use the non-location list
5239 * window with this location list (if present)
5240 */
5241 llwin = find_win_with_ll(qi);
5242 if (llwin != NULL)
5243 wp = llwin;
5244 }
5245
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005246 qf_free_all(wp);
5247 if (wp == NULL)
5248 {
5249 /* quickfix list */
5250 qi->qf_curlist = 0;
5251 qi->qf_listcount = 0;
5252 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005253 else if (IS_LL_WINDOW(orig_wp))
5254 {
5255 /* If the location list window is open, then create a new empty
5256 * location list */
5257 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005258
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005259 /* first free the list reference in the location list window */
5260 ll_free_all(&orig_wp->w_llist_ref);
5261
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005262 orig_wp->w_llist_ref = new_ll;
5263 if (llwin != NULL)
5264 {
5265 llwin->w_llist = new_ll;
5266 new_ll->qf_refcount++;
5267 }
5268 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005269}
5270
Bram Moolenaard823fa92016-08-12 16:29:27 +02005271/*
5272 * Populate the quickfix list with the items supplied in the list
5273 * of dictionaries. "title" will be copied to w:quickfix_title.
5274 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5275 */
5276 int
5277set_errorlist(
5278 win_T *wp,
5279 list_T *list,
5280 int action,
5281 char_u *title,
5282 dict_T *what)
5283{
5284 qf_info_T *qi = &ql_info;
5285 int retval = OK;
5286
5287 if (wp != NULL)
5288 {
5289 qi = ll_get_or_alloc_list(wp);
5290 if (qi == NULL)
5291 return FAIL;
5292 }
5293
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005294 if (action == 'f')
5295 {
5296 /* Free the entire quickfix or location list stack */
5297 qf_free_stack(wp, qi);
5298 }
5299 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005300 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005301 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005302 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005303
5304 return retval;
5305}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005306
5307 static int
5308mark_quickfix_ctx(qf_info_T *qi, int copyID)
5309{
5310 int i;
5311 int abort = FALSE;
5312 typval_T *ctx;
5313
5314 for (i = 0; i < LISTCOUNT && !abort; ++i)
5315 {
5316 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005317 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5318 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005319 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5320 }
5321
5322 return abort;
5323}
5324
5325/*
5326 * Mark the context of the quickfix list and the location lists (if present) as
5327 * "in use". So that garabage collection doesn't free the context.
5328 */
5329 int
5330set_ref_in_quickfix(int copyID)
5331{
5332 int abort = FALSE;
5333 tabpage_T *tp;
5334 win_T *win;
5335
5336 abort = mark_quickfix_ctx(&ql_info, copyID);
5337 if (abort)
5338 return abort;
5339
5340 FOR_ALL_TAB_WINDOWS(tp, win)
5341 {
5342 if (win->w_llist != NULL)
5343 {
5344 abort = mark_quickfix_ctx(win->w_llist, copyID);
5345 if (abort)
5346 return abort;
5347 }
5348 }
5349
5350 return abort;
5351}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005352#endif
5353
Bram Moolenaar81695252004-12-29 20:58:21 +00005354/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005355 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005356 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005357 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005358 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005359 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005360 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005361 */
5362 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005363ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005364{
5365 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005366 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005367#ifdef FEAT_AUTOCMD
5368 char_u *au_name = NULL;
5369#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005370
Bram Moolenaardb552d602006-03-23 22:59:57 +00005371 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5372 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005373 {
5374 qi = ll_get_or_alloc_list(curwin);
5375 if (qi == NULL)
5376 return;
5377 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005378
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005379#ifdef FEAT_AUTOCMD
5380 switch (eap->cmdidx)
5381 {
5382 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5383 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5384 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5385 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5386 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5387 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5388 default: break;
5389 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005390 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5391 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005392 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005393# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005394 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005395 return;
5396# endif
5397 }
5398#endif
5399
Bram Moolenaar86b68352004-12-27 21:59:20 +00005400 if (*eap->arg == NUL)
5401 buf = curbuf;
5402 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5403 buf = buflist_findnr(atoi((char *)eap->arg));
5404 if (buf == NULL)
5405 EMSG(_(e_invarg));
5406 else if (buf->b_ml.ml_mfp == NULL)
5407 EMSG(_("E681: Buffer is not loaded"));
5408 else
5409 {
5410 if (eap->addr_count == 0)
5411 {
5412 eap->line1 = 1;
5413 eap->line2 = buf->b_ml.ml_line_count;
5414 }
5415 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5416 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5417 EMSG(_(e_invrange));
5418 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005419 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005420 char_u *qf_title = *eap->cmdlinep;
5421
5422 if (buf->b_sfname)
5423 {
5424 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5425 (char *)qf_title, (char *)buf->b_sfname);
5426 qf_title = IObuff;
5427 }
5428
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005429 if (qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005430 (eap->cmdidx != CMD_caddbuffer
5431 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005432 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005433 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005434 {
5435#ifdef FEAT_AUTOCMD
5436 if (au_name != NULL)
5437 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5438 curbuf->b_fname, TRUE, curbuf);
5439#endif
5440 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5441 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5442 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005443 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005444 }
5445}
5446
Bram Moolenaar1e015462005-09-25 22:16:38 +00005447#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005448/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005449 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5450 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005451 */
5452 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005453ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005454{
5455 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005456 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005457#ifdef FEAT_AUTOCMD
5458 char_u *au_name = NULL;
5459#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005460
Bram Moolenaardb552d602006-03-23 22:59:57 +00005461 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5462 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005463 {
5464 qi = ll_get_or_alloc_list(curwin);
5465 if (qi == NULL)
5466 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005467 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005468
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005469#ifdef FEAT_AUTOCMD
5470 switch (eap->cmdidx)
5471 {
5472 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5473 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5474 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5475 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5476 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5477 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5478 default: break;
5479 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005480 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5481 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005482 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005483# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005484 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005485 return;
5486# endif
5487 }
5488#endif
5489
Bram Moolenaar4770d092006-01-12 23:22:24 +00005490 /* Evaluate the expression. When the result is a string or a list we can
5491 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005492 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005493 if (tv != NULL)
5494 {
5495 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5496 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5497 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005498 if (qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005499 (eap->cmdidx != CMD_caddexpr
5500 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005501 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5502 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005503 {
5504#ifdef FEAT_AUTOCMD
5505 if (au_name != NULL)
5506 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5507 curbuf->b_fname, TRUE, curbuf);
5508#endif
5509 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5510 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5511 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005512 }
5513 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005514 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005515 free_tv(tv);
5516 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005517}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005518#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005519
5520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 * ":helpgrep {pattern}"
5522 */
5523 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005524ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 regmatch_T regmatch;
5527 char_u *save_cpo;
5528 char_u *p;
5529 int fcount;
5530 char_u **fnames;
5531 FILE *fd;
5532 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005534#ifdef FEAT_MULTI_LANG
5535 char_u *lang;
5536#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005537 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005538 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005539 int new_qi = FALSE;
5540 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005541#ifdef FEAT_AUTOCMD
5542 char_u *au_name = NULL;
5543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005545#ifdef FEAT_MULTI_LANG
5546 /* Check for a specified language */
5547 lang = check_help_lang(eap->arg);
5548#endif
5549
Bram Moolenaar73633f82012-01-20 13:39:07 +01005550#ifdef FEAT_AUTOCMD
5551 switch (eap->cmdidx)
5552 {
5553 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5554 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5555 default: break;
5556 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005557 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5558 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005559 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005560# ifdef FEAT_EVAL
5561 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005562 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005563# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005564 }
5565#endif
5566
5567 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5568 save_cpo = p_cpo;
5569 p_cpo = empty_option;
5570
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005571 if (eap->cmdidx == CMD_lhelpgrep)
5572 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005573 /* If the current window is a help window, then use it */
5574 if (bt_help(curwin->w_buffer))
5575 wp = curwin;
5576 else
5577 /* Find an existing help window */
5578 FOR_ALL_WINDOWS(wp)
5579 if (bt_help(wp->w_buffer))
5580 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005581
5582 if (wp == NULL) /* Help window not found */
5583 qi = NULL;
5584 else
5585 qi = wp->w_llist;
5586
5587 if (qi == NULL)
5588 {
5589 /* Allocate a new location list for help text matches */
5590 if ((qi = ll_new_list()) == NULL)
5591 return;
5592 new_qi = TRUE;
5593 }
5594 }
5595
Bram Moolenaaree85df32017-03-19 14:19:50 +01005596 /* Autocommands may change the list. Save it for later comparison */
5597 save_qi = qi;
5598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5600 regmatch.rm_ic = FALSE;
5601 if (regmatch.regprog != NULL)
5602 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005603#ifdef FEAT_MBYTE
5604 vimconv_T vc;
5605
5606 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5607 * differs. */
5608 vc.vc_type = CONV_NONE;
5609 if (!enc_utf8)
5610 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5611#endif
5612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005614 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615
5616 /* Go through all directories in 'runtimepath' */
5617 p = p_rtp;
5618 while (*p != NUL && !got_int)
5619 {
5620 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5621
5622 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5623 add_pathsep(NameBuff);
5624 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5625 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5626 &fnames, EW_FILE|EW_SILENT) == OK
5627 && fcount > 0)
5628 {
5629 for (fi = 0; fi < fcount && !got_int; ++fi)
5630 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005631#ifdef FEAT_MULTI_LANG
5632 /* Skip files for a different language. */
5633 if (lang != NULL
5634 && STRNICMP(lang, fnames[fi]
5635 + STRLEN(fnames[fi]) - 3, 2) != 0
5636 && !(STRNICMP(lang, "en", 2) == 0
5637 && STRNICMP("txt", fnames[fi]
5638 + STRLEN(fnames[fi]) - 3, 3) == 0))
5639 continue;
5640#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005641 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 if (fd != NULL)
5643 {
5644 lnum = 1;
5645 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5646 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005647 char_u *line = IObuff;
5648#ifdef FEAT_MBYTE
5649 /* Convert a line if 'encoding' is not utf-8 and
5650 * the line contains a non-ASCII character. */
5651 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005652 && has_non_ascii(IObuff))
5653 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005654 line = string_convert(&vc, IObuff, NULL);
5655 if (line == NULL)
5656 line = IObuff;
5657 }
5658#endif
5659
5660 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005662 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
5664 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005665 while (l > 0 && line[l - 1] <= ' ')
5666 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005668 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005669 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 NULL, /* dir */
5671 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005672 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005673 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005675 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005676 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005677 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005678 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 0, /* nr */
5680 1, /* type */
5681 TRUE /* valid */
5682 ) == FAIL)
5683 {
5684 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005685#ifdef FEAT_MBYTE
5686 if (line != IObuff)
5687 vim_free(line);
5688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 break;
5690 }
5691 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005692#ifdef FEAT_MBYTE
5693 if (line != IObuff)
5694 vim_free(line);
5695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 ++lnum;
5697 line_breakcheck();
5698 }
5699 fclose(fd);
5700 }
5701 }
5702 FreeWild(fcount, fnames);
5703 }
5704 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005705
Bram Moolenaar473de612013-06-08 18:19:48 +02005706 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005707#ifdef FEAT_MBYTE
5708 if (vc.vc_type != CONV_NONE)
5709 convert_setup(&vc, NULL, NULL);
5710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005712 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5713 qi->qf_lists[qi->qf_curlist].qf_ptr =
5714 qi->qf_lists[qi->qf_curlist].qf_start;
5715 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005718 if (p_cpo == empty_option)
5719 p_cpo = save_cpo;
5720 else
5721 /* Darn, some plugin changed the value. */
5722 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
Bram Moolenaar864293a2016-06-02 13:40:04 +02005724 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar73633f82012-01-20 13:39:07 +01005726#ifdef FEAT_AUTOCMD
5727 if (au_name != NULL)
5728 {
5729 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5730 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005731 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005732 /* autocommands made "qi" invalid */
5733 return;
5734 }
5735#endif
5736
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005738 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005739 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005740 else
5741 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005742
5743 if (eap->cmdidx == CMD_lhelpgrep)
5744 {
5745 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005746 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005747 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005748 {
5749 if (new_qi)
5750 ll_free_all(&qi);
5751 }
5752 else if (curwin->w_llist == NULL)
5753 curwin->w_llist = qi;
5754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755}
5756
5757#endif /* FEAT_QUICKFIX */