blob: 5cbb1519ad28f780195c80effcb6ed261e2dba10 [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 Moolenaar071d4272004-06-13 20:20:40 +0000148#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
150static int is_qf_win(win_T *win, qf_info_T *qi);
151static win_T *qf_find_win(qf_info_T *qi);
152static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200153static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200155static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static char_u *get_mef_name(void);
158static void restore_start_dir(char_u *dirname_start);
159static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
160static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
161static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
162static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164/* Quickfix window check helper macro */
165#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
166/* Location list window check helper macro */
167#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
168/*
169 * Return location list for window 'wp'
170 * For location list window, return the referenced location list
171 */
172#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
173
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200175 * Looking up a buffer can be slow if there are many. Remember the last one
176 * to make this a lot faster if there are multiple matches in the same file.
177 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200178static char_u *qf_last_bufname = NULL;
179static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200180
181/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200183 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 * Return -1 for error, number of errors for success.
185 */
186 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100187qf_init(
188 win_T *wp,
189 char_u *efile,
190 char_u *errorformat,
191 int newlist, /* TRUE: start a new error list */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100192 char_u *qf_title,
193 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000195 qf_info_T *qi = &ql_info;
196
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000197 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000198 {
199 qi = ll_get_or_alloc_list(wp);
200 if (qi == NULL)
201 return FAIL;
202 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000203
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200204 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
205 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000206}
207
208/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200209 * Maximum number of bytes allowed per line while reading a errorfile.
210 */
211#define LINE_MAXLEN 4096
212
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200213static struct fmtpattern
214{
215 char_u convchar;
216 char *pattern;
217} fmt_pat[FMT_PATTERNS] =
218 {
219 {'f', ".\\+"}, /* only used when at end */
220 {'n', "\\d\\+"},
221 {'l', "\\d\\+"},
222 {'c', "\\d\\+"},
223 {'t', "."},
224 {'m', ".\\+"},
225 {'r', ".*"},
226 {'p', "[- .]*"},
227 {'v', "\\d\\+"},
228 {'s', ".\\+"}
229 };
230
231/*
232 * Converts a 'errorformat' string to regular expression pattern
233 */
234 static int
235efm_to_regpat(
236 char_u *efm,
237 int len,
238 efm_T *fmt_ptr,
239 char_u *regpat,
240 char_u *errmsg)
241{
242 char_u *ptr;
243 char_u *efmp;
244 char_u *srcptr;
245 int round;
246 int idx = 0;
247
248 /*
249 * Build regexp pattern from current 'errorformat' option
250 */
251 ptr = regpat;
252 *ptr++ = '^';
253 round = 0;
254 for (efmp = efm; efmp < efm + len; ++efmp)
255 {
256 if (*efmp == '%')
257 {
258 ++efmp;
259 for (idx = 0; idx < FMT_PATTERNS; ++idx)
260 if (fmt_pat[idx].convchar == *efmp)
261 break;
262 if (idx < FMT_PATTERNS)
263 {
264 if (fmt_ptr->addr[idx])
265 {
266 sprintf((char *)errmsg,
267 _("E372: Too many %%%c in format string"), *efmp);
268 EMSG(errmsg);
269 return -1;
270 }
271 if ((idx
272 && idx < 6
273 && vim_strchr((char_u *)"DXOPQ",
274 fmt_ptr->prefix) != NULL)
275 || (idx == 6
276 && vim_strchr((char_u *)"OPQ",
277 fmt_ptr->prefix) == NULL))
278 {
279 sprintf((char *)errmsg,
280 _("E373: Unexpected %%%c in format string"), *efmp);
281 EMSG(errmsg);
282 return -1;
283 }
284 fmt_ptr->addr[idx] = (char_u)++round;
285 *ptr++ = '\\';
286 *ptr++ = '(';
287#ifdef BACKSLASH_IN_FILENAME
288 if (*efmp == 'f')
289 {
290 /* Also match "c:" in the file name, even when
291 * checking for a colon next: "%f:".
292 * "\%(\a:\)\=" */
293 STRCPY(ptr, "\\%(\\a:\\)\\=");
294 ptr += 10;
295 }
296#endif
297 if (*efmp == 'f' && efmp[1] != NUL)
298 {
299 if (efmp[1] != '\\' && efmp[1] != '%')
300 {
301 /* A file name may contain spaces, but this isn't
302 * in "\f". For "%f:%l:%m" there may be a ":" in
303 * the file name. Use ".\{-1,}x" instead (x is
304 * the next character), the requirement that :999:
305 * follows should work. */
306 STRCPY(ptr, ".\\{-1,}");
307 ptr += 7;
308 }
309 else
310 {
311 /* File name followed by '\\' or '%': include as
312 * many file name chars as possible. */
313 STRCPY(ptr, "\\f\\+");
314 ptr += 4;
315 }
316 }
317 else
318 {
319 srcptr = (char_u *)fmt_pat[idx].pattern;
320 while ((*ptr = *srcptr++) != NUL)
321 ++ptr;
322 }
323 *ptr++ = '\\';
324 *ptr++ = ')';
325 }
326 else if (*efmp == '*')
327 {
328 if (*++efmp == '[' || *efmp == '\\')
329 {
330 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
331 {
332 if (efmp[1] == '^')
333 *ptr++ = *++efmp;
334 if (efmp < efm + len)
335 {
336 *ptr++ = *++efmp; /* could be ']' */
337 while (efmp < efm + len
338 && (*ptr++ = *++efmp) != ']')
339 /* skip */;
340 if (efmp == efm + len)
341 {
342 EMSG(_("E374: Missing ] in format string"));
343 return -1;
344 }
345 }
346 }
347 else if (efmp < efm + len) /* %*\D, %*\s etc. */
348 *ptr++ = *++efmp;
349 *ptr++ = '\\';
350 *ptr++ = '+';
351 }
352 else
353 {
354 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
355 sprintf((char *)errmsg,
356 _("E375: Unsupported %%%c in format string"), *efmp);
357 EMSG(errmsg);
358 return -1;
359 }
360 }
361 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
362 *ptr++ = *efmp; /* regexp magic characters */
363 else if (*efmp == '#')
364 *ptr++ = '*';
365 else if (*efmp == '>')
366 fmt_ptr->conthere = TRUE;
367 else if (efmp == efm + 1) /* analyse prefix */
368 {
369 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
370 fmt_ptr->flags = *efmp++;
371 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
372 fmt_ptr->prefix = *efmp;
373 else
374 {
375 sprintf((char *)errmsg,
376 _("E376: Invalid %%%c in format string prefix"), *efmp);
377 EMSG(errmsg);
378 return -1;
379 }
380 }
381 else
382 {
383 sprintf((char *)errmsg,
384 _("E377: Invalid %%%c in format string"), *efmp);
385 EMSG(errmsg);
386 return -1;
387 }
388 }
389 else /* copy normal character */
390 {
391 if (*efmp == '\\' && efmp + 1 < efm + len)
392 ++efmp;
393 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
394 *ptr++ = '\\'; /* escape regexp atoms */
395 if (*efmp)
396 *ptr++ = *efmp;
397 }
398 }
399 *ptr++ = '$';
400 *ptr = NUL;
401
402 return 0;
403}
404
405 static void
406free_efm_list(efm_T **efm_first)
407{
408 efm_T *efm_ptr;
409
410 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
411 {
412 *efm_first = efm_ptr->next;
413 vim_regfree(efm_ptr->prog);
414 vim_free(efm_ptr);
415 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100416 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200417}
418
419/* Parse 'errorformat' option */
420 static efm_T *
421parse_efm_option(char_u *efm)
422{
423 char_u *errmsg = NULL;
424 int errmsglen;
425 efm_T *fmt_ptr = NULL;
426 efm_T *fmt_first = NULL;
427 efm_T *fmt_last = NULL;
428 char_u *fmtstr = NULL;
429 int len;
430 int i;
431 int round;
432
433 errmsglen = CMDBUFFSIZE + 1;
434 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
435 if (errmsg == NULL)
436 goto parse_efm_end;
437
438 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200439 * Each part of the format string is copied and modified from errorformat
440 * to regex prog. Only a few % characters are allowed.
441 */
442
443 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200444 * Get some space to modify the format string into.
445 */
446 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
447 for (round = FMT_PATTERNS; round > 0; )
448 i += (int)STRLEN(fmt_pat[--round].pattern);
449#ifdef COLON_IN_FILENAME
450 i += 12; /* "%f" can become twelve chars longer */
451#else
452 i += 2; /* "%f" can become two chars longer */
453#endif
454 if ((fmtstr = alloc(i)) == NULL)
455 goto parse_efm_error;
456
457 while (efm[0] != NUL)
458 {
459 /*
460 * Allocate a new eformat structure and put it at the end of the list
461 */
462 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
463 if (fmt_ptr == NULL)
464 goto parse_efm_error;
465 if (fmt_first == NULL) /* first one */
466 fmt_first = fmt_ptr;
467 else
468 fmt_last->next = fmt_ptr;
469 fmt_last = fmt_ptr;
470
471 /*
472 * Isolate one part in the 'errorformat' option
473 */
474 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
475 if (efm[len] == '\\' && efm[len + 1] != NUL)
476 ++len;
477
478 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
479 goto parse_efm_error;
480 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
481 goto parse_efm_error;
482 /*
483 * Advance to next part
484 */
485 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
486 }
487
488 if (fmt_first == NULL) /* nothing found */
489 EMSG(_("E378: 'errorformat' contains no pattern"));
490
491 goto parse_efm_end;
492
493parse_efm_error:
494 free_efm_list(&fmt_first);
495
496parse_efm_end:
497 vim_free(fmtstr);
498 vim_free(errmsg);
499
500 return fmt_first;
501}
502
Bram Moolenaare0d37972016-07-15 22:36:01 +0200503enum {
504 QF_FAIL = 0,
505 QF_OK = 1,
506 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200507 QF_NOMEM = 3,
508 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200509};
510
511typedef struct {
512 char_u *linebuf;
513 int linelen;
514 char_u *growbuf;
515 int growbufsiz;
516 FILE *fd;
517 typval_T *tv;
518 char_u *p_str;
519 listitem_T *p_li;
520 buf_T *buf;
521 linenr_T buflnum;
522 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100523 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200524} qfstate_T;
525
526 static char_u *
527qf_grow_linebuf(qfstate_T *state, int newsz)
528{
529 /*
530 * If the line exceeds LINE_MAXLEN exclude the last
531 * byte since it's not a NL character.
532 */
533 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
534 if (state->growbuf == NULL)
535 {
536 state->growbuf = alloc(state->linelen + 1);
537 if (state->growbuf == NULL)
538 return NULL;
539 state->growbufsiz = state->linelen;
540 }
541 else if (state->linelen > state->growbufsiz)
542 {
543 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
544 if (state->growbuf == NULL)
545 return NULL;
546 state->growbufsiz = state->linelen;
547 }
548 return state->growbuf;
549}
550
551/*
552 * Get the next string (separated by newline) from state->p_str.
553 */
554 static int
555qf_get_next_str_line(qfstate_T *state)
556{
557 /* Get the next line from the supplied string */
558 char_u *p_str = state->p_str;
559 char_u *p;
560 int len;
561
562 if (*p_str == NUL) /* Reached the end of the string */
563 return QF_END_OF_INPUT;
564
565 p = vim_strchr(p_str, '\n');
566 if (p != NULL)
567 len = (int)(p - p_str) + 1;
568 else
569 len = (int)STRLEN(p_str);
570
571 if (len > IOSIZE - 2)
572 {
573 state->linebuf = qf_grow_linebuf(state, len);
574 if (state->linebuf == NULL)
575 return QF_NOMEM;
576 }
577 else
578 {
579 state->linebuf = IObuff;
580 state->linelen = len;
581 }
582 vim_strncpy(state->linebuf, p_str, state->linelen);
583
584 /*
585 * Increment using len in order to discard the rest of the
586 * line if it exceeds LINE_MAXLEN.
587 */
588 p_str += len;
589 state->p_str = p_str;
590
591 return QF_OK;
592}
593
594/*
595 * Get the next string from state->p_Li.
596 */
597 static int
598qf_get_next_list_line(qfstate_T *state)
599{
600 listitem_T *p_li = state->p_li;
601 int len;
602
603 while (p_li != NULL
604 && (p_li->li_tv.v_type != VAR_STRING
605 || p_li->li_tv.vval.v_string == NULL))
606 p_li = p_li->li_next; /* Skip non-string items */
607
608 if (p_li == NULL) /* End of the list */
609 {
610 state->p_li = NULL;
611 return QF_END_OF_INPUT;
612 }
613
614 len = (int)STRLEN(p_li->li_tv.vval.v_string);
615 if (len > IOSIZE - 2)
616 {
617 state->linebuf = qf_grow_linebuf(state, len);
618 if (state->linebuf == NULL)
619 return QF_NOMEM;
620 }
621 else
622 {
623 state->linebuf = IObuff;
624 state->linelen = len;
625 }
626
627 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
628
629 state->p_li = p_li->li_next; /* next item */
630 return QF_OK;
631}
632
633/*
634 * Get the next string from state->buf.
635 */
636 static int
637qf_get_next_buf_line(qfstate_T *state)
638{
639 char_u *p_buf = NULL;
640 int len;
641
642 /* Get the next line from the supplied buffer */
643 if (state->buflnum > state->lnumlast)
644 return QF_END_OF_INPUT;
645
646 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
647 state->buflnum += 1;
648
649 len = (int)STRLEN(p_buf);
650 if (len > IOSIZE - 2)
651 {
652 state->linebuf = qf_grow_linebuf(state, len);
653 if (state->linebuf == NULL)
654 return QF_NOMEM;
655 }
656 else
657 {
658 state->linebuf = IObuff;
659 state->linelen = len;
660 }
661 vim_strncpy(state->linebuf, p_buf, state->linelen);
662
663 return QF_OK;
664}
665
666/*
667 * Get the next string from file state->fd.
668 */
669 static int
670qf_get_next_file_line(qfstate_T *state)
671{
672 int discard;
673 int growbuflen;
674
675 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
676 return QF_END_OF_INPUT;
677
678 discard = FALSE;
679 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200680 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200681 {
682 /*
683 * The current line exceeds IObuff, continue reading using
684 * growbuf until EOL or LINE_MAXLEN bytes is read.
685 */
686 if (state->growbuf == NULL)
687 {
688 state->growbufsiz = 2 * (IOSIZE - 1);
689 state->growbuf = alloc(state->growbufsiz);
690 if (state->growbuf == NULL)
691 return QF_NOMEM;
692 }
693
694 /* Copy the read part of the line, excluding null-terminator */
695 memcpy(state->growbuf, IObuff, IOSIZE - 1);
696 growbuflen = state->linelen;
697
698 for (;;)
699 {
700 if (fgets((char *)state->growbuf + growbuflen,
701 state->growbufsiz - growbuflen, state->fd) == NULL)
702 break;
703 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
704 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200705 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200706 break;
707 if (state->growbufsiz == LINE_MAXLEN)
708 {
709 discard = TRUE;
710 break;
711 }
712
713 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
714 ? 2 * state->growbufsiz : LINE_MAXLEN;
715 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
716 if (state->growbuf == NULL)
717 return QF_NOMEM;
718 }
719
720 while (discard)
721 {
722 /*
723 * The current line is longer than LINE_MAXLEN, continue
724 * reading but discard everything until EOL or EOF is
725 * reached.
726 */
727 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
728 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200729 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200730 break;
731 }
732
733 state->linebuf = state->growbuf;
734 state->linelen = growbuflen;
735 }
736 else
737 state->linebuf = IObuff;
738
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100739#ifdef FEAT_MBYTE
740 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200741 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
742 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100743 char_u *line;
744
745 line = string_convert(&state->vc, state->linebuf, &state->linelen);
746 if (line != NULL)
747 {
748 if (state->linelen < IOSIZE)
749 {
750 STRCPY(state->linebuf, line);
751 vim_free(line);
752 }
753 else
754 {
755 vim_free(state->growbuf);
756 state->linebuf = state->growbuf = line;
757 state->growbufsiz = state->linelen < LINE_MAXLEN
758 ? state->linelen : LINE_MAXLEN;
759 }
760 }
761 }
762#endif
763
Bram Moolenaare0d37972016-07-15 22:36:01 +0200764 return QF_OK;
765}
766
767/*
768 * Get the next string from a file/buffer/list/string.
769 */
770 static int
771qf_get_nextline(qfstate_T *state)
772{
773 int status = QF_FAIL;
774
775 if (state->fd == NULL)
776 {
777 if (state->tv != NULL)
778 {
779 if (state->tv->v_type == VAR_STRING)
780 /* Get the next line from the supplied string */
781 status = qf_get_next_str_line(state);
782 else if (state->tv->v_type == VAR_LIST)
783 /* Get the next line from the supplied list */
784 status = qf_get_next_list_line(state);
785 }
786 else
787 /* Get the next line from the supplied buffer */
788 status = qf_get_next_buf_line(state);
789 }
790 else
791 /* Get the next line from the supplied file */
792 status = qf_get_next_file_line(state);
793
794 if (status != QF_OK)
795 return status;
796
797 /* remove newline/CR from the line */
798 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200799 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200800 state->linebuf[state->linelen - 1] = NUL;
801#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200802 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
803 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200805 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806
807#ifdef FEAT_MBYTE
808 remove_bom(state->linebuf);
809#endif
810
811 return QF_OK;
812}
813
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200814typedef struct {
815 char_u *namebuf;
816 char_u *errmsg;
817 int errmsglen;
818 long lnum;
819 int col;
820 char_u use_viscol;
821 char_u *pattern;
822 int enr;
823 int type;
824 int valid;
825} qffields_T;
826
827/*
828 * Parse a line and get the quickfix fields.
829 * Return the QF_ status.
830 */
831 static int
832qf_parse_line(
833 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200834 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200835 char_u *linebuf,
836 int linelen,
837 efm_T *fmt_first,
838 qffields_T *fields)
839{
840 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200841 char_u *ptr;
842 int len;
843 int i;
844 int idx = 0;
845 char_u *tail = NULL;
846 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200847 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200848
849 /* Always ignore case when looking for a matching error. */
850 regmatch.rm_ic = TRUE;
851
852 /* If there was no %> item start at the first pattern */
853 if (fmt_start == NULL)
854 fmt_ptr = fmt_first;
855 else
856 {
857 fmt_ptr = fmt_start;
858 fmt_start = NULL;
859 }
860
861 /*
862 * Try to match each part of 'errorformat' until we find a complete
863 * match or no match.
864 */
865 fields->valid = TRUE;
866restofline:
867 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
868 {
869 int r;
870
871 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200872 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200873 continue;
874 fields->namebuf[0] = NUL;
875 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200876 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200877 fields->errmsg[0] = NUL;
878 fields->lnum = 0;
879 fields->col = 0;
880 fields->use_viscol = FALSE;
881 fields->enr = -1;
882 fields->type = 0;
883 tail = NULL;
884
885 regmatch.regprog = fmt_ptr->prog;
886 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
887 fmt_ptr->prog = regmatch.regprog;
888 if (r)
889 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200890 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200891 continue;
892 if (vim_strchr((char_u *)"EWI", idx) != NULL)
893 fields->type = idx;
894 else
895 fields->type = 0;
896 /*
897 * Extract error message data from matched line.
898 * We check for an actual submatch, because "\[" and "\]" in
899 * the 'errorformat' may cause the wrong submatch to be used.
900 */
901 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
902 {
903 int c;
904
905 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
906 continue;
907
908 /* Expand ~/file and $HOME/file to full path. */
909 c = *regmatch.endp[i];
910 *regmatch.endp[i] = NUL;
911 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
912 *regmatch.endp[i] = c;
913
914 if (vim_strchr((char_u *)"OPQ", idx) != NULL
915 && mch_getperm(fields->namebuf) == -1)
916 continue;
917 }
918 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
919 {
920 if (regmatch.startp[i] == NULL)
921 continue;
922 fields->enr = (int)atol((char *)regmatch.startp[i]);
923 }
924 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
925 {
926 if (regmatch.startp[i] == NULL)
927 continue;
928 fields->lnum = atol((char *)regmatch.startp[i]);
929 }
930 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
931 {
932 if (regmatch.startp[i] == NULL)
933 continue;
934 fields->col = (int)atol((char *)regmatch.startp[i]);
935 }
936 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
937 {
938 if (regmatch.startp[i] == NULL)
939 continue;
940 fields->type = *regmatch.startp[i];
941 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200942 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200943 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200944 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200945 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200946 /* linelen + null terminator */
947 if ((fields->errmsg = vim_realloc(fields->errmsg,
948 linelen + 1)) == NULL)
949 return QF_NOMEM;
950 fields->errmsglen = linelen + 1;
951 }
952 vim_strncpy(fields->errmsg, linebuf, linelen);
953 }
954 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
955 {
956 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
957 continue;
958 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200959 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200960 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200961 /* len + null terminator */
962 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
963 == NULL)
964 return QF_NOMEM;
965 fields->errmsglen = len + 1;
966 }
967 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
968 }
969 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
970 {
971 if (regmatch.startp[i] == NULL)
972 continue;
973 tail = regmatch.startp[i];
974 }
975 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
976 {
977 char_u *match_ptr;
978
979 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
980 continue;
981 fields->col = 0;
982 for (match_ptr = regmatch.startp[i];
983 match_ptr != regmatch.endp[i]; ++match_ptr)
984 {
985 ++fields->col;
986 if (*match_ptr == TAB)
987 {
988 fields->col += 7;
989 fields->col -= fields->col % 8;
990 }
991 }
992 ++fields->col;
993 fields->use_viscol = TRUE;
994 }
995 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
996 {
997 if (regmatch.startp[i] == NULL)
998 continue;
999 fields->col = (int)atol((char *)regmatch.startp[i]);
1000 fields->use_viscol = TRUE;
1001 }
1002 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1003 {
1004 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1005 continue;
1006 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1007 if (len > CMDBUFFSIZE - 5)
1008 len = CMDBUFFSIZE - 5;
1009 STRCPY(fields->pattern, "^\\V");
1010 STRNCAT(fields->pattern, regmatch.startp[i], len);
1011 fields->pattern[len + 3] = '\\';
1012 fields->pattern[len + 4] = '$';
1013 fields->pattern[len + 5] = NUL;
1014 }
1015 break;
1016 }
1017 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001018 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001019
1020 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1021 {
1022 if (fmt_ptr != NULL)
1023 {
1024 if (idx == 'D') /* enter directory */
1025 {
1026 if (*fields->namebuf == NUL)
1027 {
1028 EMSG(_("E379: Missing or empty directory name"));
1029 return QF_FAIL;
1030 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001031 qfl->qf_directory =
1032 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1033 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001034 return QF_FAIL;
1035 }
1036 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001037 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001038 }
1039 fields->namebuf[0] = NUL; /* no match found, remove file name */
1040 fields->lnum = 0; /* don't jump to this line */
1041 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001042 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001043 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001044 /* linelen + null terminator */
1045 if ((fields->errmsg = vim_realloc(fields->errmsg,
1046 linelen + 1)) == NULL)
1047 return QF_NOMEM;
1048 fields->errmsglen = linelen + 1;
1049 }
1050 /* copy whole line to error message */
1051 vim_strncpy(fields->errmsg, linebuf, linelen);
1052 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001053 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001054 }
1055 else if (fmt_ptr != NULL)
1056 {
1057 /* honor %> item */
1058 if (fmt_ptr->conthere)
1059 fmt_start = fmt_ptr;
1060
1061 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1062 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001063 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1064 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001065 }
1066 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1067 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001068 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001069 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001070 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001071
Bram Moolenaar9b457942016-10-09 16:10:05 +02001072 if (qfprev == NULL)
1073 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001074 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001075 {
1076 len = (int)STRLEN(qfprev->qf_text);
1077 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1078 == NULL)
1079 return QF_FAIL;
1080 STRCPY(ptr, qfprev->qf_text);
1081 vim_free(qfprev->qf_text);
1082 qfprev->qf_text = ptr;
1083 *(ptr += len) = '\n';
1084 STRCPY(++ptr, fields->errmsg);
1085 }
1086 if (qfprev->qf_nr == -1)
1087 qfprev->qf_nr = fields->enr;
1088 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1089 /* only printable chars allowed */
1090 qfprev->qf_type = fields->type;
1091
1092 if (!qfprev->qf_lnum)
1093 qfprev->qf_lnum = fields->lnum;
1094 if (!qfprev->qf_col)
1095 qfprev->qf_col = fields->col;
1096 qfprev->qf_viscol = fields->use_viscol;
1097 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001098 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1099 qfl->qf_directory,
1100 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001101 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001102 : qfl->qf_currfile != NULL && fields->valid
1103 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001104 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001105 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001106 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001107 line_breakcheck();
1108 return QF_IGNORE_LINE;
1109 }
1110 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1111 {
1112 /* global file names */
1113 fields->valid = FALSE;
1114 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1115 {
1116 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001117 qfl->qf_currfile =
1118 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001119 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001120 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001121 *fields->namebuf = NUL;
1122 if (tail && *tail)
1123 {
1124 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001125 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001126 goto restofline;
1127 }
1128 }
1129 }
1130 if (fmt_ptr->flags == '-') /* generally exclude this line */
1131 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001132 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001133 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001134 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001135 return QF_IGNORE_LINE;
1136 }
1137 }
1138
1139 return QF_OK;
1140}
1141
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001142/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001143 * Read the errorfile "efile" into memory, line by line, building the error
1144 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001145 * Alternative: when "efile" is NULL read errors from buffer "buf".
1146 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001147 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001148 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1149 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001150 * Return -1 for error, number of errors for success.
1151 */
1152 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001153qf_init_ext(
1154 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001155 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001156 char_u *efile,
1157 buf_T *buf,
1158 typval_T *tv,
1159 char_u *errorformat,
1160 int newlist, /* TRUE: start a new error list */
1161 linenr_T lnumfirst, /* first line number to use */
1162 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001163 char_u *qf_title,
1164 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001165{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001166 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001167 qfstate_T state;
1168 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001169#ifdef FEAT_WINDOWS
1170 qfline_T *old_last = NULL;
1171#endif
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001172 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001173 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001175 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001177 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001179 /* Do not used the cached buffer, it may have been wiped out. */
1180 vim_free(qf_last_bufname);
1181 qf_last_bufname = NULL;
1182
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001183 vim_memset(&state, 0, sizeof(state));
1184 vim_memset(&fields, 0, sizeof(fields));
1185#ifdef FEAT_MBYTE
1186 state.vc.vc_type = CONV_NONE;
1187 if (enc != NULL && *enc != NUL)
1188 convert_setup(&state.vc, enc, p_enc);
1189#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001190 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1191 fields.errmsglen = CMDBUFFSIZE + 1;
1192 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1193 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001194 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 goto qf_init_end;
1196
Bram Moolenaare0d37972016-07-15 22:36:01 +02001197 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
1199 EMSG2(_(e_openerrf), efile);
1200 goto qf_init_end;
1201 }
1202
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001203 if (newlist || qf_idx == qi->qf_listcount)
1204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001206 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001207 qf_idx = qi->qf_curlist;
1208 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001209 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001210 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001211 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001212 adding = TRUE;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001213#ifdef FEAT_WINDOWS
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001214 if (qi->qf_lists[qf_idx].qf_count > 0)
1215 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001216#endif
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001219 qfl = &qi->qf_lists[qf_idx];
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001222 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001223 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 else
1225 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001227 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001228 * If the errorformat didn't change between calls, then reuse the
1229 * previously parsed values.
1230 */
1231 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1232 {
1233 /* free the previously parsed data */
1234 vim_free(last_efm);
1235 last_efm = NULL;
1236 free_efm_list(&fmt_first);
1237
1238 /* parse the current 'efm' */
1239 fmt_first = parse_efm_option(efm);
1240 if (fmt_first != NULL)
1241 last_efm = vim_strsave(efm);
1242 }
1243
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247 /*
1248 * got_int is reset here, because it was probably set when killing the
1249 * ":make" command, but we still want to read the errorfile then.
1250 */
1251 got_int = FALSE;
1252
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001253 if (tv != NULL)
1254 {
1255 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001256 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001257 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001258 state.p_li = tv->vval.v_list->lv_first;
1259 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001260 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001261 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001262 state.buflnum = lnumfirst;
1263 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001264
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 /*
1266 * Read the lines in the error file one by one.
1267 * Try to recognize one of the error formats in each line.
1268 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001269 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001271 /* Get the next line from a file/buffer/list/string */
1272 status = qf_get_nextline(&state);
1273 if (status == QF_NOMEM) /* memory alloc failure */
1274 goto qf_init_end;
1275 if (status == QF_END_OF_INPUT) /* end of input */
1276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001278 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1279 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001280 if (status == QF_FAIL)
1281 goto error2;
1282 if (status == QF_NOMEM)
1283 goto qf_init_end;
1284 if (status == QF_IGNORE_LINE)
1285 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001287 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001288 qf_idx,
1289 qfl->qf_directory,
1290 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001291 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001292 : ((qfl->qf_currfile != NULL && fields.valid)
1293 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001294 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001295 fields.errmsg,
1296 fields.lnum,
1297 fields.col,
1298 fields.use_viscol,
1299 fields.pattern,
1300 fields.enr,
1301 fields.type,
1302 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 goto error2;
1304 line_breakcheck();
1305 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001306 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001308 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001310 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001311 qfl->qf_ptr = qfl->qf_start;
1312 qfl->qf_index = 1;
1313 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 else
1316 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001317 qfl->qf_nonevalid = FALSE;
1318 if (qfl->qf_ptr == NULL)
1319 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001321 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001322 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001323 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
1325 EMSG(_(e_readerrf));
1326error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001327 if (!adding)
1328 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001329 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001330 qf_free(qi, qi->qf_curlist);
1331 qi->qf_listcount--;
1332 if (qi->qf_curlist > 0)
1333 --qi->qf_curlist;
1334 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001335qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001336 if (state.fd != NULL)
1337 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001338 vim_free(fields.namebuf);
1339 vim_free(fields.errmsg);
1340 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001341 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343#ifdef FEAT_WINDOWS
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001344 if (qf_idx == qi->qf_curlist)
1345 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001347#ifdef FEAT_MBYTE
1348 if (state.vc.vc_type != CONV_NONE)
1349 convert_setup(&state.vc, NULL, NULL);
1350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352 return retval;
1353}
1354
Bram Moolenaarfb604092014-07-23 15:55:00 +02001355 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001356qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001357{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001358 vim_free(qi->qf_lists[qf_idx].qf_title);
1359 qi->qf_lists[qf_idx].qf_title = NULL;
1360
Bram Moolenaarfb604092014-07-23 15:55:00 +02001361 if (title != NULL)
1362 {
1363 char_u *p = alloc((int)STRLEN(title) + 2);
1364
Bram Moolenaara3921f42017-06-04 15:30:34 +02001365 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001366 if (p != NULL)
1367 sprintf((char *)p, ":%s", (char *)title);
1368 }
1369}
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001372 * Prepare for adding a new quickfix list. If the current list is in the
1373 * middle of the stack, then all the following lists are freed and then
1374 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 */
1376 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001377qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
1379 int i;
1380
1381 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001382 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 * the current entry. This makes it possible to browse in a tree-like
1384 * way with ":grep'.
1385 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001386 while (qi->qf_listcount > qi->qf_curlist + 1)
1387 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389 /*
1390 * When the stack is full, remove to oldest entry
1391 * Otherwise, add a new entry.
1392 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001393 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001395 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001397 qi->qf_lists[i - 1] = qi->qf_lists[i];
1398 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
1400 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001401 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001402 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001403 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001404 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405}
1406
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001407/*
1408 * Free a location list
1409 */
1410 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001411ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001412{
1413 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001414 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001415
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001416 qi = *pqi;
1417 if (qi == NULL)
1418 return;
1419 *pqi = NULL; /* Remove reference to this list */
1420
1421 qi->qf_refcount--;
1422 if (qi->qf_refcount < 1)
1423 {
1424 /* No references to this location list */
1425 for (i = 0; i < qi->qf_listcount; ++i)
1426 qf_free(qi, i);
1427 vim_free(qi);
1428 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001429}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001430
1431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001432qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001433{
1434 int i;
1435 qf_info_T *qi = &ql_info;
1436
1437 if (wp != NULL)
1438 {
1439 /* location list */
1440 ll_free_all(&wp->w_llist);
1441 ll_free_all(&wp->w_llist_ref);
1442 }
1443 else
1444 /* quickfix list */
1445 for (i = 0; i < qi->qf_listcount; ++i)
1446 qf_free(qi, i);
1447}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001448
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449/*
1450 * Add an entry to the end of the list of errors.
1451 * Returns OK or FAIL.
1452 */
1453 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001454qf_add_entry(
1455 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001456 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001457 char_u *dir, /* optional directory name */
1458 char_u *fname, /* file name or NULL */
1459 int bufnum, /* buffer number or zero */
1460 char_u *mesg, /* message */
1461 long lnum, /* line number */
1462 int col, /* column */
1463 int vis_col, /* using visual column */
1464 char_u *pattern, /* search pattern */
1465 int nr, /* error number */
1466 int type, /* type character */
1467 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001469 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001470 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001472 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001474 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001475 {
1476 buf_T *buf = buflist_findnr(bufnum);
1477
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001478 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001479 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001480 buf->b_has_qf_entry |=
1481 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001482 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001483 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001484 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1486 {
1487 vim_free(qfp);
1488 return FAIL;
1489 }
1490 qfp->qf_lnum = lnum;
1491 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001492 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001493 if (pattern == NULL || *pattern == NUL)
1494 qfp->qf_pattern = NULL;
1495 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1496 {
1497 vim_free(qfp->qf_text);
1498 vim_free(qfp);
1499 return FAIL;
1500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 qfp->qf_nr = nr;
1502 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1503 type = 0;
1504 qfp->qf_type = type;
1505 qfp->qf_valid = valid;
1506
Bram Moolenaara3921f42017-06-04 15:30:34 +02001507 lastp = &qi->qf_lists[qf_idx].qf_last;
1508 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001509 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001511 qi->qf_lists[qf_idx].qf_start = qfp;
1512 qi->qf_lists[qf_idx].qf_ptr = qfp;
1513 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001514 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 }
1516 else
1517 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001518 qfp->qf_prev = *lastp;
1519 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001521 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001523 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001524 ++qi->qf_lists[qf_idx].qf_count;
1525 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001526 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001528 qi->qf_lists[qf_idx].qf_index =
1529 qi->qf_lists[qf_idx].qf_count;
1530 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532
1533 return OK;
1534}
1535
1536/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001537 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001538 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001539 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001540ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001541{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001542 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001544 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1545 if (qi != NULL)
1546 {
1547 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1548 qi->qf_refcount++;
1549 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001550
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001551 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001552}
1553
1554/*
1555 * Return the location list for window 'wp'.
1556 * If not present, allocate a location list
1557 */
1558 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001559ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001560{
1561 if (IS_LL_WINDOW(wp))
1562 /* For a location list window, use the referenced location list */
1563 return wp->w_llist_ref;
1564
1565 /*
1566 * For a non-location list window, w_llist_ref should not point to a
1567 * location list.
1568 */
1569 ll_free_all(&wp->w_llist_ref);
1570
1571 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001572 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001573 return wp->w_llist;
1574}
1575
1576/*
1577 * Copy the location list from window "from" to window "to".
1578 */
1579 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001580copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001581{
1582 qf_info_T *qi;
1583 int idx;
1584 int i;
1585
1586 /*
1587 * When copying from a location list window, copy the referenced
1588 * location list. For other windows, copy the location list for
1589 * that window.
1590 */
1591 if (IS_LL_WINDOW(from))
1592 qi = from->w_llist_ref;
1593 else
1594 qi = from->w_llist;
1595
1596 if (qi == NULL) /* no location list to copy */
1597 return;
1598
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001599 /* allocate a new location list */
1600 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001601 return;
1602
1603 to->w_llist->qf_listcount = qi->qf_listcount;
1604
1605 /* Copy the location lists one at a time */
1606 for (idx = 0; idx < qi->qf_listcount; idx++)
1607 {
1608 qf_list_T *from_qfl;
1609 qf_list_T *to_qfl;
1610
1611 to->w_llist->qf_curlist = idx;
1612
1613 from_qfl = &qi->qf_lists[idx];
1614 to_qfl = &to->w_llist->qf_lists[idx];
1615
1616 /* Some of the fields are populated by qf_add_entry() */
1617 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1618 to_qfl->qf_count = 0;
1619 to_qfl->qf_index = 0;
1620 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001621 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001622 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001623 if (from_qfl->qf_title != NULL)
1624 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1625 else
1626 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001627 if (from_qfl->qf_ctx != NULL)
1628 {
1629 to_qfl->qf_ctx = alloc_tv();
1630 if (to_qfl->qf_ctx != NULL)
1631 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1632 }
1633 else
1634 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001635
1636 if (from_qfl->qf_count)
1637 {
1638 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001639 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001640
1641 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001642 for (i = 0, from_qfp = from_qfl->qf_start;
1643 i < from_qfl->qf_count && from_qfp != NULL;
1644 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001645 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001646 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001647 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001648 NULL,
1649 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001650 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001651 from_qfp->qf_text,
1652 from_qfp->qf_lnum,
1653 from_qfp->qf_col,
1654 from_qfp->qf_viscol,
1655 from_qfp->qf_pattern,
1656 from_qfp->qf_nr,
1657 0,
1658 from_qfp->qf_valid) == FAIL)
1659 {
1660 qf_free_all(to);
1661 return;
1662 }
1663 /*
1664 * qf_add_entry() will not set the qf_num field, as the
1665 * directory and file names are not supplied. So the qf_fnum
1666 * field is copied here.
1667 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001668 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001669 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1670 prevp->qf_type = from_qfp->qf_type; /* error type */
1671 if (from_qfl->qf_ptr == from_qfp)
1672 to_qfl->qf_ptr = prevp; /* current location */
1673 }
1674 }
1675
1676 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1677
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001678 /* Assign a new ID for the location list */
1679 to_qfl->qf_id = ++last_qf_id;
1680
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001681 /* When no valid entries are present in the list, qf_ptr points to
1682 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001683 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001684 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001685 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001686 to_qfl->qf_index = 1;
1687 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001688 }
1689
1690 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1691}
1692
1693/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001694 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001695 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 */
1697 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001698qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
Bram Moolenaar82404332016-07-10 17:00:38 +02001700 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001701 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001702 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001703
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 if (fname == NULL || *fname == NUL) /* no file name */
1705 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
Bram Moolenaare60acc12011-05-10 16:41:25 +02001707#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001708 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001709#endif
1710#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001711 if (directory != NULL)
1712 slash_adjust(directory);
1713 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001714#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001715 if (directory != NULL && !vim_isAbsName(fname)
1716 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1717 {
1718 /*
1719 * Here we check if the file really exists.
1720 * This should normally be true, but if make works without
1721 * "leaving directory"-messages we might have missed a
1722 * directory change.
1723 */
1724 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001727 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001728 if (directory)
1729 ptr = concat_fnames(directory, fname, TRUE);
1730 else
1731 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001733 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001734 bufname = ptr;
1735 }
1736 else
1737 bufname = fname;
1738
1739 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001740 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001741 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001742 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001743 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001745 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001746 {
1747 vim_free(qf_last_bufname);
1748 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1749 if (bufname == ptr)
1750 qf_last_bufname = bufname;
1751 else
1752 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001753 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001754 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001755 if (buf == NULL)
1756 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001757
Bram Moolenaarc1542742016-07-20 21:44:37 +02001758 buf->b_has_qf_entry =
1759 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001760 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761}
1762
1763/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001764 * Push dirbuf onto the directory stack and return pointer to actual dir or
1765 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 */
1767 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001768qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
1770 struct dir_stack_T *ds_new;
1771 struct dir_stack_T *ds_ptr;
1772
1773 /* allocate new stack element and hook it in */
1774 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1775 if (ds_new == NULL)
1776 return NULL;
1777
1778 ds_new->next = *stackptr;
1779 *stackptr = ds_new;
1780
1781 /* store directory on the stack */
1782 if (vim_isAbsName(dirbuf)
1783 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001784 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 (*stackptr)->dirname = vim_strsave(dirbuf);
1786 else
1787 {
1788 /* Okay we don't have an absolute path.
1789 * dirbuf must be a subdir of one of the directories on the stack.
1790 * Let's search...
1791 */
1792 ds_new = (*stackptr)->next;
1793 (*stackptr)->dirname = NULL;
1794 while (ds_new)
1795 {
1796 vim_free((*stackptr)->dirname);
1797 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1798 TRUE);
1799 if (mch_isdir((*stackptr)->dirname) == TRUE)
1800 break;
1801
1802 ds_new = ds_new->next;
1803 }
1804
1805 /* clean up all dirs we already left */
1806 while ((*stackptr)->next != ds_new)
1807 {
1808 ds_ptr = (*stackptr)->next;
1809 (*stackptr)->next = (*stackptr)->next->next;
1810 vim_free(ds_ptr->dirname);
1811 vim_free(ds_ptr);
1812 }
1813
1814 /* Nothing found -> it must be on top level */
1815 if (ds_new == NULL)
1816 {
1817 vim_free((*stackptr)->dirname);
1818 (*stackptr)->dirname = vim_strsave(dirbuf);
1819 }
1820 }
1821
1822 if ((*stackptr)->dirname != NULL)
1823 return (*stackptr)->dirname;
1824 else
1825 {
1826 ds_ptr = *stackptr;
1827 *stackptr = (*stackptr)->next;
1828 vim_free(ds_ptr);
1829 return NULL;
1830 }
1831}
1832
1833
1834/*
1835 * pop dirbuf from the directory stack and return previous directory or NULL if
1836 * stack is empty
1837 */
1838 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001839qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 struct dir_stack_T *ds_ptr;
1842
1843 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1844 * What to do if it isn't? */
1845
1846 /* pop top element and free it */
1847 if (*stackptr != NULL)
1848 {
1849 ds_ptr = *stackptr;
1850 *stackptr = (*stackptr)->next;
1851 vim_free(ds_ptr->dirname);
1852 vim_free(ds_ptr);
1853 }
1854
1855 /* return NEW top element as current dir or NULL if stack is empty*/
1856 return *stackptr ? (*stackptr)->dirname : NULL;
1857}
1858
1859/*
1860 * clean up directory stack
1861 */
1862 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001863qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
1865 struct dir_stack_T *ds_ptr;
1866
1867 while ((ds_ptr = *stackptr) != NULL)
1868 {
1869 *stackptr = (*stackptr)->next;
1870 vim_free(ds_ptr->dirname);
1871 vim_free(ds_ptr);
1872 }
1873}
1874
1875/*
1876 * Check in which directory of the directory stack the given file can be
1877 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001878 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 * Cleans up intermediate directory entries.
1880 *
1881 * TODO: How to solve the following problem?
1882 * If we have the this directory tree:
1883 * ./
1884 * ./aa
1885 * ./aa/bb
1886 * ./bb
1887 * ./bb/x.c
1888 * and make says:
1889 * making all in aa
1890 * making all in bb
1891 * x.c:9: Error
1892 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1893 * qf_guess_filepath will return NULL.
1894 */
1895 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001896qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
1898 struct dir_stack_T *ds_ptr;
1899 struct dir_stack_T *ds_tmp;
1900 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001901 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
1903 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001904 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 return NULL;
1906
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001907 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 fullname = NULL;
1909 while (ds_ptr)
1910 {
1911 vim_free(fullname);
1912 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1913
1914 /* If concat_fnames failed, just go on. The worst thing that can happen
1915 * is that we delete the entire stack.
1916 */
1917 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1918 break;
1919
1920 ds_ptr = ds_ptr->next;
1921 }
1922
1923 vim_free(fullname);
1924
1925 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001926 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001928 ds_tmp = qfl->qf_dir_stack->next;
1929 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 vim_free(ds_tmp->dirname);
1931 vim_free(ds_tmp);
1932 }
1933
1934 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935}
1936
1937/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001938 * When loading a file from the quickfix, the auto commands may modify it.
1939 * This may invalidate the current quickfix entry. This function checks
1940 * whether a entry is still present in the quickfix.
1941 * Similar to location list.
1942 */
1943 static int
1944is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1945{
1946 qf_list_T *qfl;
1947 qfline_T *qfp;
1948 int i;
1949
1950 qfl = &qi->qf_lists[qi->qf_curlist];
1951
1952 /* Search for the entry in the current list */
1953 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1954 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001955 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001956 break;
1957
1958 if (i == qfl->qf_count) /* Entry is not found */
1959 return FALSE;
1960
1961 return TRUE;
1962}
1963
1964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 * jump to a quickfix line
1966 * if dir == FORWARD go "errornr" valid entries forward
1967 * if dir == BACKWARD go "errornr" valid entries backward
1968 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1969 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1970 * else if "errornr" is zero, redisplay the same line
1971 * else go to entry "errornr"
1972 */
1973 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001974qf_jump(
1975 qf_info_T *qi,
1976 int dir,
1977 int errornr,
1978 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001981 qfline_T *qf_ptr;
1982 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 int qf_index;
1984 int old_qf_fnum;
1985 int old_qf_index;
1986 int prev_index;
1987 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1988 char_u *err = e_no_more_items;
1989 linenr_T i;
1990 buf_T *old_curbuf;
1991 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 colnr_T screen_col;
1993 colnr_T char_col;
1994 char_u *line;
1995#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001996 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001997 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 int opened_window = FALSE;
1999 win_T *win;
2000 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002001 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002003 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 int print_message = TRUE;
2005 int len;
2006#ifdef FEAT_FOLDING
2007 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2008#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002009 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002010 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002012 if (qi == NULL)
2013 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002014
2015 if (qi->qf_curlist >= qi->qf_listcount
2016 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
2018 EMSG(_(e_quickfix));
2019 return;
2020 }
2021
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002022 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002024 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 old_qf_index = qf_index;
2026 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
2027 {
2028 while (errornr--)
2029 {
2030 old_qf_ptr = qf_ptr;
2031 prev_index = qf_index;
2032 old_qf_fnum = qf_ptr->qf_fnum;
2033 do
2034 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002035 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 || qf_ptr->qf_next == NULL)
2037 {
2038 qf_ptr = old_qf_ptr;
2039 qf_index = prev_index;
2040 if (err != NULL)
2041 {
2042 EMSG(_(err));
2043 goto theend;
2044 }
2045 errornr = 0;
2046 break;
2047 }
2048 ++qf_index;
2049 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002050 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2051 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2053 err = NULL;
2054 }
2055 }
2056 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
2057 {
2058 while (errornr--)
2059 {
2060 old_qf_ptr = qf_ptr;
2061 prev_index = qf_index;
2062 old_qf_fnum = qf_ptr->qf_fnum;
2063 do
2064 {
2065 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
2066 {
2067 qf_ptr = old_qf_ptr;
2068 qf_index = prev_index;
2069 if (err != NULL)
2070 {
2071 EMSG(_(err));
2072 goto theend;
2073 }
2074 errornr = 0;
2075 break;
2076 }
2077 --qf_index;
2078 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002079 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2080 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2082 err = NULL;
2083 }
2084 }
2085 else if (errornr != 0) /* go to specified number */
2086 {
2087 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2088 {
2089 --qf_index;
2090 qf_ptr = qf_ptr->qf_prev;
2091 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002092 while (errornr > qf_index && qf_index <
2093 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 && qf_ptr->qf_next != NULL)
2095 {
2096 ++qf_index;
2097 qf_ptr = qf_ptr->qf_next;
2098 }
2099 }
2100
2101#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002102 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2103 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 /* No need to print the error message if it's visible in the error
2105 * window */
2106 print_message = FALSE;
2107
2108 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002109 * For ":helpgrep" find a help window or open one.
2110 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002111 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002112 {
2113 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002114
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002115 if (cmdmod.tab != 0)
2116 wp = NULL;
2117 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002118 FOR_ALL_WINDOWS(wp)
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002119 if (bt_help(wp->w_buffer))
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002120 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002121 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2122 win_enter(wp, TRUE);
2123 else
2124 {
2125 /*
2126 * Split off help window; put it at far top if no position
2127 * specified, the current window is vertically split and narrow.
2128 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002129 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002130 if (cmdmod.split == 0 && curwin->w_width != Columns
2131 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002132 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002133 if (qi != &ql_info)
2134 flags |= WSP_NEWLOC; /* don't copy the location list */
2135
2136 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002137 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002138 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002139
2140 if (curwin->w_height < p_hh)
2141 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002142
2143 if (qi != &ql_info) /* not a quickfix list */
2144 {
2145 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002146 curwin->w_llist = qi;
2147 qi->qf_refcount++;
2148 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002149 }
2150
2151 if (!p_im)
2152 restart_edit = 0; /* don't want insert mode in help file */
2153 }
2154
2155 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 * If currently in the quickfix window, find another window to show the
2157 * file in.
2158 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002159 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002161 win_T *usable_win_ptr = NULL;
2162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 /*
2164 * If there is no file specified, we don't know where to go.
2165 * But do advance, otherwise ":cn" gets stuck.
2166 */
2167 if (qf_ptr->qf_fnum == 0)
2168 goto theend;
2169
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002170 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002171
2172 ll_ref = curwin->w_llist_ref;
2173 if (ll_ref != NULL)
2174 {
2175 /* Find a window using the same location list that is not a
2176 * quickfix window. */
2177 FOR_ALL_WINDOWS(usable_win_ptr)
2178 if (usable_win_ptr->w_llist == ll_ref
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02002179 && !bt_quickfix(usable_win_ptr->w_buffer))
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002180 {
2181 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002182 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002183 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002184 }
2185
2186 if (!usable_win)
2187 {
2188 /* Locate a window showing a normal buffer */
2189 FOR_ALL_WINDOWS(win)
2190 if (win->w_buffer->b_p_bt[0] == NUL)
2191 {
2192 usable_win = 1;
2193 break;
2194 }
2195 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002198 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002199 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002201 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002202 {
2203 tabpage_T *tp;
2204 win_T *wp;
2205
2206 FOR_ALL_TAB_WINDOWS(tp, wp)
2207 {
2208 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2209 {
2210 goto_tabpage_win(tp, wp);
2211 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002212 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002213 }
2214 }
2215 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002216win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002217
2218 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002219 * If there is only one window and it is the quickfix window, create a
2220 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002221 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002222 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002224 flags = WSP_ABOVE;
2225 if (ll_ref != NULL)
2226 flags |= WSP_NEWLOC;
2227 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 goto failed; /* not enough room for window */
2229 opened_window = TRUE; /* close it when fail */
2230 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002231 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002232 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233 if (ll_ref != NULL)
2234 {
2235 /* The new window should use the location list from the
2236 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002237 curwin->w_llist = ll_ref;
2238 ll_ref->qf_refcount++;
2239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241 else
2242 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002243 if (curwin->w_llist_ref != NULL)
2244 {
2245 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002246 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002247 if (win == NULL)
2248 {
2249 /* Find the window showing the selected file */
2250 FOR_ALL_WINDOWS(win)
2251 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2252 break;
2253 if (win == NULL)
2254 {
2255 /* Find a previous usable window */
2256 win = curwin;
2257 do
2258 {
2259 if (win->w_buffer->b_p_bt[0] == NUL)
2260 break;
2261 if (win->w_prev == NULL)
2262 win = lastwin; /* wrap around the top */
2263 else
2264 win = win->w_prev; /* go to previous window */
2265 } while (win != curwin);
2266 }
2267 }
2268 win_goto(win);
2269
2270 /* If the location list for the window is not set, then set it
2271 * to the location list from the location window */
2272 if (win->w_llist == NULL)
2273 {
2274 win->w_llist = ll_ref;
2275 ll_ref->qf_refcount++;
2276 }
2277 }
2278 else
2279 {
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 /*
2282 * Try to find a window that shows the right buffer.
2283 * Default to the window just above the quickfix buffer.
2284 */
2285 win = curwin;
2286 altwin = NULL;
2287 for (;;)
2288 {
2289 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2290 break;
2291 if (win->w_prev == NULL)
2292 win = lastwin; /* wrap around the top */
2293 else
2294 win = win->w_prev; /* go to previous window */
2295
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002296 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 /* Didn't find it, go to the window before the quickfix
2299 * window. */
2300 if (altwin != NULL)
2301 win = altwin;
2302 else if (curwin->w_prev != NULL)
2303 win = curwin->w_prev;
2304 else
2305 win = curwin->w_next;
2306 break;
2307 }
2308
2309 /* Remember a usable window. */
2310 if (altwin == NULL && !win->w_p_pvw
2311 && win->w_buffer->b_p_bt[0] == NUL)
2312 altwin = win;
2313 }
2314
2315 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318 }
2319#endif
2320
2321 /*
2322 * If there is a file name,
2323 * read the wanted file if needed, and check autowrite etc.
2324 */
2325 old_curbuf = curbuf;
2326 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002327
2328 if (qf_ptr->qf_fnum != 0)
2329 {
2330 if (qf_ptr->qf_type == 1)
2331 {
2332 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2333 * set b_p_ro flag). */
2334 if (!can_abandon(curbuf, forceit))
2335 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002336 no_write_message();
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002337 ok = FALSE;
2338 }
2339 else
2340 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002341 ECMD_HIDE + ECMD_SET_HELP,
2342 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002343 }
2344 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002345 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002346 int old_qf_curlist = qi->qf_curlist;
2347 int is_abort = FALSE;
2348
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002349 ok = buflist_getfile(qf_ptr->qf_fnum,
2350 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002351 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002352 {
2353 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002354 is_abort = TRUE;
2355 opened_window = FALSE;
2356 }
2357 else if (old_qf_curlist != qi->qf_curlist
2358 || !is_qf_entry_present(qi, qf_ptr))
2359 {
2360 if (qi == &ql_info)
2361 EMSG(_("E925: Current quickfix was changed"));
2362 else
2363 EMSG(_("E926: Current location list was changed"));
2364 is_abort = TRUE;
2365 }
2366
2367 if (is_abort)
2368 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002369 ok = FALSE;
2370 qi = NULL;
2371 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002372 }
2373 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002374 }
2375
2376 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
2378 /* When not switched to another buffer, still need to set pc mark */
2379 if (curbuf == old_curbuf)
2380 setpcmark();
2381
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002382 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002384 /*
2385 * Go to line with error, unless qf_lnum is 0.
2386 */
2387 i = qf_ptr->qf_lnum;
2388 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002390 if (i > curbuf->b_ml.ml_line_count)
2391 i = curbuf->b_ml.ml_line_count;
2392 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002394 if (qf_ptr->qf_col > 0)
2395 {
2396 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002397#ifdef FEAT_VIRTUALEDIT
2398 curwin->w_cursor.coladd = 0;
2399#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002400 if (qf_ptr->qf_viscol == TRUE)
2401 {
2402 /*
2403 * Check each character from the beginning of the error
2404 * line up to the error column. For each tab character
2405 * found, reduce the error column value by the length of
2406 * a tab character.
2407 */
2408 line = ml_get_curline();
2409 screen_col = 0;
2410 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2411 {
2412 if (*line == NUL)
2413 break;
2414 if (*line++ == '\t')
2415 {
2416 curwin->w_cursor.col -= 7 - (screen_col % 8);
2417 screen_col += 8 - (screen_col % 8);
2418 }
2419 else
2420 ++screen_col;
2421 }
2422 }
2423 check_cursor();
2424 }
2425 else
2426 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 }
2428 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002429 {
2430 pos_T save_cursor;
2431
2432 /* Move the cursor to the first line in the buffer */
2433 save_cursor = curwin->w_cursor;
2434 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002435 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002436 SEARCH_KEEP, NULL, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002437 curwin->w_cursor = save_cursor;
2438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440#ifdef FEAT_FOLDING
2441 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2442 foldOpenCursor();
2443#endif
2444 if (print_message)
2445 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002446 /* Update the screen before showing the message, unless the screen
2447 * scrolled up. */
2448 if (!msg_scrolled)
2449 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002451 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2453 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2454 /* Add the message, skipping leading whitespace and newlines. */
2455 len = (int)STRLEN(IObuff);
2456 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2457
2458 /* Output the message. Overwrite to avoid scrolling when the 'O'
2459 * flag is present in 'shortmess'; But when not jumping, print the
2460 * whole message. */
2461 i = msg_scroll;
2462 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2463 msg_scroll = TRUE;
2464 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2465 msg_scroll = FALSE;
2466 msg_attr_keep(IObuff, 0, TRUE);
2467 msg_scroll = i;
2468 }
2469 }
2470 else
2471 {
2472#ifdef FEAT_WINDOWS
2473 if (opened_window)
2474 win_close(curwin, TRUE); /* Close opened window */
2475#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002476 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
2478 /*
2479 * Couldn't open file, so put index back where it was. This could
2480 * happen if the file was readonly and we changed something.
2481 */
2482#ifdef FEAT_WINDOWS
2483failed:
2484#endif
2485 qf_ptr = old_qf_ptr;
2486 qf_index = old_qf_index;
2487 }
2488 }
2489theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002490 if (qi != NULL)
2491 {
2492 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2493 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495#ifdef FEAT_WINDOWS
2496 if (p_swb != old_swb && opened_window)
2497 {
2498 /* Restore old 'switchbuf' value, but not when an autocommand or
2499 * modeline has changed the value. */
2500 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002503 swb_flags = old_swb_flags;
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 else
2506 free_string_option(old_swb);
2507 }
2508#endif
2509}
2510
2511/*
2512 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002513 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 */
2515 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002516qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002518 buf_T *buf;
2519 char_u *fname;
2520 qfline_T *qfp;
2521 int i;
2522 int idx1 = 1;
2523 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002524 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002525 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002526 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002528 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002530 if (eap->cmdidx == CMD_llist)
2531 {
2532 qi = GET_LOC_LIST(curwin);
2533 if (qi == NULL)
2534 {
2535 EMSG(_(e_loclist));
2536 return;
2537 }
2538 }
2539
2540 if (qi->qf_curlist >= qi->qf_listcount
2541 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 {
2543 EMSG(_(e_quickfix));
2544 return;
2545 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002546 if (*arg == '+')
2547 {
2548 ++arg;
2549 plus = TRUE;
2550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2552 {
2553 EMSG(_(e_trailing));
2554 return;
2555 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002556 if (plus)
2557 {
2558 i = qi->qf_lists[qi->qf_curlist].qf_index;
2559 idx2 = i + idx1;
2560 idx1 = i;
2561 }
2562 else
2563 {
2564 i = qi->qf_lists[qi->qf_curlist].qf_count;
2565 if (idx1 < 0)
2566 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2567 if (idx2 < 0)
2568 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002571 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002573 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2574 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2577 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002578 msg_putchar('\n');
2579 if (got_int)
2580 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002581
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002582 fname = NULL;
2583 if (qfp->qf_fnum != 0
2584 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2585 {
2586 fname = buf->b_fname;
2587 if (qfp->qf_type == 1) /* :helpgrep */
2588 fname = gettail(fname);
2589 }
2590 if (fname == NULL)
2591 sprintf((char *)IObuff, "%2d", i);
2592 else
2593 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2594 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002595 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar21020352017-06-13 17:21:04 +02002596 ? HL_ATTR(HLF_QFL) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002597 if (qfp->qf_lnum == 0)
2598 IObuff[0] = NUL;
2599 else if (qfp->qf_col == 0)
2600 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2601 else
2602 sprintf((char *)IObuff, ":%ld col %d",
2603 qfp->qf_lnum, qfp->qf_col);
2604 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2605 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002606 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002607 if (qfp->qf_pattern != NULL)
2608 {
2609 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2610 STRCAT(IObuff, ":");
2611 msg_puts(IObuff);
2612 }
2613 msg_puts((char_u *)" ");
2614
2615 /* Remove newlines and leading whitespace from the text. For an
2616 * unrecognized line keep the indent, the compiler may mark a word
2617 * with ^^^^. */
2618 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2620 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002621 msg_prt_line(IObuff, FALSE);
2622 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002624
2625 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002626 if (qfp == NULL)
2627 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002628 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 ui_breakcheck();
2630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631}
2632
2633/*
2634 * Remove newlines and leading whitespace from an error message.
2635 * Put the result in "buf[bufsize]".
2636 */
2637 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002638qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
2640 int i;
2641 char_u *p = text;
2642
2643 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2644 {
2645 if (*p == '\n')
2646 {
2647 buf[i] = ' ';
2648 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002649 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 break;
2651 }
2652 else
2653 buf[i] = *p++;
2654 }
2655 buf[i] = NUL;
2656}
2657
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002658 static void
2659qf_msg(qf_info_T *qi, int which, char *lead)
2660{
2661 char *title = (char *)qi->qf_lists[which].qf_title;
2662 int count = qi->qf_lists[which].qf_count;
2663 char_u buf[IOSIZE];
2664
2665 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2666 lead,
2667 which + 1,
2668 qi->qf_listcount,
2669 count);
2670
2671 if (title != NULL)
2672 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002673 size_t len = STRLEN(buf);
2674
2675 if (len < 34)
2676 {
2677 vim_memset(buf + len, ' ', 34 - len);
2678 buf[34] = NUL;
2679 }
2680 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002681 }
2682 trunc_string(buf, buf, Columns - 1, IOSIZE);
2683 msg(buf);
2684}
2685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686/*
2687 * ":colder [count]": Up in the quickfix stack.
2688 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002689 * ":lolder [count]": Up in the location list stack.
2690 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
2692 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002693qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002695 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 int count;
2697
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002698 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2699 {
2700 qi = GET_LOC_LIST(curwin);
2701 if (qi == NULL)
2702 {
2703 EMSG(_(e_loclist));
2704 return;
2705 }
2706 }
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (eap->addr_count != 0)
2709 count = eap->line2;
2710 else
2711 count = 1;
2712 while (count--)
2713 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002714 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002716 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
2718 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002719 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002721 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723 else
2724 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002725 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002728 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002730 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002733 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002735 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736#endif
2737}
2738
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002739 void
2740qf_history(exarg_T *eap)
2741{
2742 qf_info_T *qi = &ql_info;
2743 int i;
2744
2745 if (eap->cmdidx == CMD_lhistory)
2746 qi = GET_LOC_LIST(curwin);
2747 if (qi == NULL || (qi->qf_listcount == 0
2748 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2749 MSG(_("No entries"));
2750 else
2751 for (i = 0; i < qi->qf_listcount; ++i)
2752 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2753}
2754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002756 * Free all the entries in the error list "idx". Note that other information
2757 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 */
2759 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002760qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002762 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002763 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002764 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002765 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002767 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002769 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002770 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002771 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002772 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002773 vim_free(qfp->qf_text);
2774 stop = (qfp == qfpnext);
2775 vim_free(qfp->qf_pattern);
2776 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002777 if (stop)
2778 /* Somehow qf_count may have an incorrect value, set it to 1
2779 * to avoid crashing when it's wrong.
2780 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002781 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002782 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002783 qfl->qf_start = qfpnext;
2784 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002786
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002787 qfl->qf_index = 0;
2788 qfl->qf_start = NULL;
2789 qfl->qf_last = NULL;
2790 qfl->qf_ptr = NULL;
2791 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002792
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002793 qf_clean_dir_stack(&qfl->qf_dir_stack);
2794 qfl->qf_directory = NULL;
2795 qf_clean_dir_stack(&qfl->qf_file_stack);
2796 qfl->qf_currfile = NULL;
2797 qfl->qf_multiline = FALSE;
2798 qfl->qf_multiignore = FALSE;
2799 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800}
2801
2802/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002803 * Free error list "idx". Frees all the entries in the quickfix list,
2804 * associated context information and the title.
2805 */
2806 static void
2807qf_free(qf_info_T *qi, int idx)
2808{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002809 qf_list_T *qfl = &qi->qf_lists[idx];
2810
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002811 qf_free_items(qi, idx);
2812
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002813 vim_free(qfl->qf_title);
2814 qfl->qf_title = NULL;
2815 free_tv(qfl->qf_ctx);
2816 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002817 qfl->qf_id = 0;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002818}
2819
2820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 * qf_mark_adjust: adjust marks
2822 */
2823 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002824qf_mark_adjust(
2825 win_T *wp,
2826 linenr_T line1,
2827 linenr_T line2,
2828 long amount,
2829 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002831 int i;
2832 qfline_T *qfp;
2833 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002834 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002835 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002836 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
Bram Moolenaarc1542742016-07-20 21:44:37 +02002838 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002839 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002840 if (wp != NULL)
2841 {
2842 if (wp->w_llist == NULL)
2843 return;
2844 qi = wp->w_llist;
2845 }
2846
2847 for (idx = 0; idx < qi->qf_listcount; ++idx)
2848 if (qi->qf_lists[idx].qf_count)
2849 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002850 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2851 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (qfp->qf_fnum == curbuf->b_fnum)
2853 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002854 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2856 {
2857 if (amount == MAXLNUM)
2858 qfp->qf_cleared = TRUE;
2859 else
2860 qfp->qf_lnum += amount;
2861 }
2862 else if (amount_after && qfp->qf_lnum > line2)
2863 qfp->qf_lnum += amount_after;
2864 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002865
2866 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002867 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868}
2869
2870/*
2871 * Make a nice message out of the error character and the error number:
2872 * char number message
2873 * e or E 0 " error"
2874 * w or W 0 " warning"
2875 * i or I 0 " info"
2876 * 0 0 ""
2877 * other 0 " c"
2878 * e or E n " error n"
2879 * w or W n " warning n"
2880 * i or I n " info n"
2881 * 0 n " error n"
2882 * other n " c n"
2883 * 1 x "" :helpgrep
2884 */
2885 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002886qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888 static char_u buf[20];
2889 static char_u cc[3];
2890 char_u *p;
2891
2892 if (c == 'W' || c == 'w')
2893 p = (char_u *)" warning";
2894 else if (c == 'I' || c == 'i')
2895 p = (char_u *)" info";
2896 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2897 p = (char_u *)" error";
2898 else if (c == 0 || c == 1)
2899 p = (char_u *)"";
2900 else
2901 {
2902 cc[0] = ' ';
2903 cc[1] = c;
2904 cc[2] = NUL;
2905 p = cc;
2906 }
2907
2908 if (nr <= 0)
2909 return p;
2910
2911 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2912 return buf;
2913}
2914
2915#if defined(FEAT_WINDOWS) || defined(PROTO)
2916/*
2917 * ":cwindow": open the quickfix window if we have errors to display,
2918 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 * ":lwindow": open the location list window if we have locations to display,
2920 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 */
2922 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002923ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002925 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 win_T *win;
2927
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002928 if (eap->cmdidx == CMD_lwindow)
2929 {
2930 qi = GET_LOC_LIST(curwin);
2931 if (qi == NULL)
2932 return;
2933 }
2934
2935 /* Look for an existing quickfix window. */
2936 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
2938 /*
2939 * If a quickfix window is open but we have no errors to display,
2940 * close the window. If a quickfix window is not open, then open
2941 * it if we have errors; otherwise, leave it closed.
2942 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002943 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002944 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002945 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 {
2947 if (win != NULL)
2948 ex_cclose(eap);
2949 }
2950 else if (win == NULL)
2951 ex_copen(eap);
2952}
2953
2954/*
2955 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002956 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002959ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002961 win_T *win = NULL;
2962 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002964 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2965 {
2966 qi = GET_LOC_LIST(curwin);
2967 if (qi == NULL)
2968 return;
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002971 /* Find existing quickfix window and close it. */
2972 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if (win != NULL)
2974 win_close(win, FALSE);
2975}
2976
2977/*
2978 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002979 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 */
2981 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002982ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002984 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002987 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002988 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002989 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002991 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2992 {
2993 qi = GET_LOC_LIST(curwin);
2994 if (qi == NULL)
2995 {
2996 EMSG(_(e_loclist));
2997 return;
2998 }
2999 }
3000
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 if (eap->addr_count != 0)
3002 height = eap->line2;
3003 else
3004 height = QF_WINHEIGHT;
3005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#ifdef FEAT_GUI
3008 need_mouse_correct = TRUE;
3009#endif
3010
3011 /*
3012 * Find existing quickfix window, or open a new one.
3013 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003014 win = qf_find_win(qi);
3015
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003016 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003019 if (eap->addr_count != 0)
3020 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003021 if (cmdmod.split & WSP_VERT)
3022 {
3023 if (height != W_WIDTH(win))
3024 win_setwidth(height);
3025 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003026 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003027 win_setheight(height);
3028 }
3029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 else
3031 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00003032 qf_buf = qf_find_buf(qi);
3033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 /* The current window becomes the previous window afterwards. */
3035 win = curwin;
3036
Bram Moolenaar77642c02012-11-20 17:55:10 +01003037 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3038 && cmdmod.split == 0)
3039 /* Create the new window at the very bottom, except when
3040 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003041 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003042 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003044 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003046 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003048 /*
3049 * For the location list window, create a reference to the
3050 * location list from the window 'win'.
3051 */
3052 curwin->w_llist_ref = win->w_llist;
3053 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003055
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003056 if (oldwin != curwin)
3057 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003058 if (qf_buf != NULL)
3059 /* Use the existing quickfix buffer */
3060 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003061 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003062 else
3063 {
3064 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003065 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003066 /* switch off 'swapfile' */
3067 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3068 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003069 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003070 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003071 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003072#ifdef FEAT_DIFF
3073 curwin->w_p_diff = FALSE;
3074#endif
3075#ifdef FEAT_FOLDING
3076 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3077 OPT_LOCAL);
3078#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003081 /* Only set the height when still in the same tab page and there is no
3082 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003083 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 win_setheight(height);
3085 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3086 if (win_valid(win))
3087 prevwin = win;
3088 }
3089
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003090 qf_set_title_var(qi);
3091
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 /*
3093 * Fill the buffer with the quickfix list.
3094 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003095 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003097 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 curwin->w_cursor.col = 0;
3099 check_cursor();
3100 update_topline(); /* scroll to show the line */
3101}
3102
3103/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003104 * Move the cursor in the quickfix window to "lnum".
3105 */
3106 static void
3107qf_win_goto(win_T *win, linenr_T lnum)
3108{
3109 win_T *old_curwin = curwin;
3110
3111 curwin = win;
3112 curbuf = win->w_buffer;
3113 curwin->w_cursor.lnum = lnum;
3114 curwin->w_cursor.col = 0;
3115#ifdef FEAT_VIRTUALEDIT
3116 curwin->w_cursor.coladd = 0;
3117#endif
3118 curwin->w_curswant = 0;
3119 update_topline(); /* scroll to show the line */
3120 redraw_later(VALID);
3121 curwin->w_redr_status = TRUE; /* update ruler */
3122 curwin = old_curwin;
3123 curbuf = curwin->w_buffer;
3124}
3125
3126/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003127 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003128 */
3129 void
3130ex_cbottom(exarg_T *eap UNUSED)
3131{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003132 qf_info_T *qi = &ql_info;
3133 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003134
Bram Moolenaar537ef082016-07-09 17:56:19 +02003135 if (eap->cmdidx == CMD_lbottom)
3136 {
3137 qi = GET_LOC_LIST(curwin);
3138 if (qi == NULL)
3139 {
3140 EMSG(_(e_loclist));
3141 return;
3142 }
3143 }
3144
3145 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003146 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3147 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3148}
3149
3150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 * Return the number of the current entry (line number in the quickfix
3152 * window).
3153 */
3154 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003155qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003157 qf_info_T *qi = &ql_info;
3158
3159 if (IS_LL_WINDOW(wp))
3160 /* In the location list window, use the referenced location list */
3161 qi = wp->w_llist_ref;
3162
3163 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164}
3165
3166/*
3167 * Update the cursor position in the quickfix window to the current error.
3168 * Return TRUE if there is a quickfix window.
3169 */
3170 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003171qf_win_pos_update(
3172 qf_info_T *qi,
3173 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174{
3175 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003176 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
3178 /*
3179 * Put the cursor on the current error in the quickfix window, so that
3180 * it's viewable.
3181 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003182 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 if (win != NULL
3184 && qf_index <= win->w_buffer->b_ml.ml_line_count
3185 && old_qf_index != qf_index)
3186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (qf_index > old_qf_index)
3188 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003189 win->w_redraw_top = old_qf_index;
3190 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
3192 else
3193 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003194 win->w_redraw_top = qf_index;
3195 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003197 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199 return win != NULL;
3200}
3201
3202/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003203 * Check whether the given window is displaying the specified quickfix/location
3204 * list buffer
3205 */
3206 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003207is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003208{
3209 /*
3210 * A window displaying the quickfix buffer will have the w_llist_ref field
3211 * set to NULL.
3212 * A window displaying a location list buffer will have the w_llist_ref
3213 * pointing to the location list.
3214 */
3215 if (bt_quickfix(win->w_buffer))
3216 if ((qi == &ql_info && win->w_llist_ref == NULL)
3217 || (qi != &ql_info && win->w_llist_ref == qi))
3218 return TRUE;
3219
3220 return FALSE;
3221}
3222
3223/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003224 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003225 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003226 */
3227 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003228qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003229{
3230 win_T *win;
3231
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003232 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003233 if (is_qf_win(win, qi))
3234 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003235
3236 return win;
3237}
3238
3239/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003240 * Find a quickfix buffer.
3241 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 */
3243 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003244qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003246 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003247 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaar9c102382006-05-03 21:26:49 +00003249 FOR_ALL_TAB_WINDOWS(tp, win)
3250 if (is_qf_win(win, qi))
3251 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003252
Bram Moolenaar9c102382006-05-03 21:26:49 +00003253 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254}
3255
3256/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003257 * Update the w:quickfix_title variable in the quickfix/location list window
3258 */
3259 static void
3260qf_update_win_titlevar(qf_info_T *qi)
3261{
3262 win_T *win;
3263 win_T *curwin_save;
3264
3265 if ((win = qf_find_win(qi)) != NULL)
3266 {
3267 curwin_save = curwin;
3268 curwin = win;
3269 qf_set_title_var(qi);
3270 curwin = curwin_save;
3271 }
3272}
3273
3274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 * Find the quickfix buffer. If it exists, update the contents.
3276 */
3277 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003278qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003281 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003285 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (buf != NULL)
3287 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003288 linenr_T old_line_count = buf->b_ml.ml_line_count;
3289
3290 if (old_last == NULL)
3291 /* set curwin/curbuf to buf and save a few things */
3292 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
Bram Moolenaard823fa92016-08-12 16:29:27 +02003294 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003295
Bram Moolenaar864293a2016-06-02 13:40:04 +02003296 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003297 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003298
Bram Moolenaar864293a2016-06-02 13:40:04 +02003299 if (old_last == NULL)
3300 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003301 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003302
3303 /* restore curwin/curbuf and a few other things */
3304 aucmd_restbuf(&aco);
3305 }
3306
3307 /* Only redraw when added lines are visible. This avoids flickering
3308 * when the added lines are not visible. */
3309 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3310 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312}
3313
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003314/*
3315 * Set "w:quickfix_title" if "qi" has a title.
3316 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003317 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003318qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003319{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003320 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3321 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003322 qi->qf_lists[qi->qf_curlist].qf_title);
3323}
3324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325/*
3326 * Fill current buffer with quickfix errors, replacing any previous contents.
3327 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003328 * If "old_last" is not NULL append the items after this one.
3329 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3330 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 */
3332 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003333qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003335 linenr_T lnum;
3336 qfline_T *qfp;
3337 buf_T *errbuf;
3338 int len;
3339 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar864293a2016-06-02 13:40:04 +02003341 if (old_last == NULL)
3342 {
3343 if (buf != curbuf)
3344 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003345 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003346 return;
3347 }
3348
3349 /* delete all existing lines */
3350 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3351 (void)ml_delete((linenr_T)1, FALSE);
3352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
3354 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003355 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
3357 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003358 if (old_last == NULL)
3359 {
3360 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3361 lnum = 0;
3362 }
3363 else
3364 {
3365 qfp = old_last->qf_next;
3366 lnum = buf->b_ml.ml_line_count;
3367 }
3368 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
3370 if (qfp->qf_fnum != 0
3371 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3372 && errbuf->b_fname != NULL)
3373 {
3374 if (qfp->qf_type == 1) /* :helpgrep */
3375 STRCPY(IObuff, gettail(errbuf->b_fname));
3376 else
3377 STRCPY(IObuff, errbuf->b_fname);
3378 len = (int)STRLEN(IObuff);
3379 }
3380 else
3381 len = 0;
3382 IObuff[len++] = '|';
3383
3384 if (qfp->qf_lnum > 0)
3385 {
3386 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3387 len += (int)STRLEN(IObuff + len);
3388
3389 if (qfp->qf_col > 0)
3390 {
3391 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3392 len += (int)STRLEN(IObuff + len);
3393 }
3394
3395 sprintf((char *)IObuff + len, "%s",
3396 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3397 len += (int)STRLEN(IObuff + len);
3398 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003399 else if (qfp->qf_pattern != NULL)
3400 {
3401 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3402 len += (int)STRLEN(IObuff + len);
3403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 IObuff[len++] = '|';
3405 IObuff[len++] = ' ';
3406
3407 /* Remove newlines and leading whitespace from the text.
3408 * For an unrecognized line keep the indent, the compiler may
3409 * mark a word with ^^^^. */
3410 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3411 IObuff + len, IOSIZE - len);
3412
Bram Moolenaar864293a2016-06-02 13:40:04 +02003413 if (ml_append_buf(buf, lnum, IObuff,
3414 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003416 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003418 if (qfp == NULL)
3419 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003421
3422 if (old_last == NULL)
3423 /* Delete the empty line which is now at the end */
3424 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426
3427 /* correct cursor position */
3428 check_lnums(TRUE);
3429
Bram Moolenaar864293a2016-06-02 13:40:04 +02003430 if (old_last == NULL)
3431 {
3432 /* Set the 'filetype' to "qf" each time after filling the buffer.
3433 * This resembles reading a file into a buffer, it's more logical when
3434 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003435#ifdef FEAT_AUTOCMD
3436 ++curbuf_lock;
3437#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003438 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3439 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003442 keep_filetype = TRUE; /* don't detect 'filetype' */
3443 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003445 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003447 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003448 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003450 /* make sure it will be redrawn */
3451 redraw_curbuf_later(NOT_VALID);
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 /* Restore KeyTyped, setting 'filetype' may reset it. */
3455 KeyTyped = old_KeyTyped;
3456}
3457
3458#endif /* FEAT_WINDOWS */
3459
3460/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003461 * Return TRUE when using ":vimgrep" for ":grep".
3462 */
3463 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003464grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003465{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003466 return ((cmdidx == CMD_grep
3467 || cmdidx == CMD_lgrep
3468 || cmdidx == CMD_grepadd
3469 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003470 && STRCMP("internal",
3471 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3472}
3473
3474/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003475 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 */
3477 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003478ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003480 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003482 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003484 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003485 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003486 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003487#ifdef FEAT_AUTOCMD
3488 char_u *au_name = NULL;
3489
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003490 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3491 if (grep_internal(eap->cmdidx))
3492 {
3493 ex_vimgrep(eap);
3494 return;
3495 }
3496
Bram Moolenaar7c626922005-02-07 22:01:03 +00003497 switch (eap->cmdidx)
3498 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003499 case CMD_make: au_name = (char_u *)"make"; break;
3500 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3501 case CMD_grep: au_name = (char_u *)"grep"; break;
3502 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3503 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3504 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003505 default: break;
3506 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003507 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3508 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003509 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003510# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003511 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003512 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003513# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003514 }
3515#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003516#ifdef FEAT_MBYTE
3517 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
Bram Moolenaara6557602006-02-04 22:43:20 +00003520 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3521 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003522 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003525 fname = get_mef_name();
3526 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003528 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 /*
3531 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3532 */
3533 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3534 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003535 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 cmd = alloc(len);
3537 if (cmd == NULL)
3538 return;
3539 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3540 (char *)p_shq);
3541 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003542 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /*
3544 * Output a newline if there's something else than the :make command that
3545 * was typed (in which case the cursor is in column 0).
3546 */
3547 if (msg_col == 0)
3548 msg_didout = FALSE;
3549 msg_start();
3550 MSG_PUTS(":!");
3551 msg_outtrans(cmd); /* show what we are doing */
3552
3553 /* let the shell know if we are redirecting output or not */
3554 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3555
3556#ifdef AMIGA
3557 out_flush();
3558 /* read window status report and redraw before message */
3559 (void)char_avail();
3560#endif
3561
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003562 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003563 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3564 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003565 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003566 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003567 if (wp != NULL)
3568 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003569#ifdef FEAT_AUTOCMD
3570 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003571 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003572 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3573 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003574 if (qi->qf_curlist < qi->qf_listcount)
3575 res = qi->qf_lists[qi->qf_curlist].qf_count;
3576 else
3577 res = 0;
3578 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003579#endif
3580 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003581 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
Bram Moolenaar7c626922005-02-07 22:01:03 +00003583 mch_remove(fname);
3584 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 vim_free(cmd);
3586}
3587
3588/*
3589 * Return the name for the errorfile, in allocated memory.
3590 * Find a new unique name when 'makeef' contains "##".
3591 * Returns NULL for error.
3592 */
3593 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003594get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
3596 char_u *p;
3597 char_u *name;
3598 static int start = -1;
3599 static int off = 0;
3600#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003601 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602#endif
3603
3604 if (*p_mef == NUL)
3605 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003606 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (name == NULL)
3608 EMSG(_(e_notmp));
3609 return name;
3610 }
3611
3612 for (p = p_mef; *p; ++p)
3613 if (p[0] == '#' && p[1] == '#')
3614 break;
3615
3616 if (*p == NUL)
3617 return vim_strsave(p_mef);
3618
3619 /* Keep trying until the name doesn't exist yet. */
3620 for (;;)
3621 {
3622 if (start == -1)
3623 start = mch_get_pid();
3624 else
3625 off += 19;
3626
3627 name = alloc((unsigned)STRLEN(p_mef) + 30);
3628 if (name == NULL)
3629 break;
3630 STRCPY(name, p_mef);
3631 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3632 STRCAT(name, p + 2);
3633 if (mch_getperm(name) < 0
3634#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003635 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 && mch_lstat((char *)name, &sb) < 0
3637#endif
3638 )
3639 break;
3640 vim_free(name);
3641 }
3642 return name;
3643}
3644
3645/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003646 * Returns the number of valid entries in the current quickfix/location list.
3647 */
3648 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003649qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003650{
3651 qf_info_T *qi = &ql_info;
3652 qfline_T *qfp;
3653 int i, sz = 0;
3654 int prev_fnum = 0;
3655
3656 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3657 {
3658 /* Location list */
3659 qi = GET_LOC_LIST(curwin);
3660 if (qi == NULL)
3661 return 0;
3662 }
3663
3664 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003665 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003666 ++i, qfp = qfp->qf_next)
3667 {
3668 if (qfp->qf_valid)
3669 {
3670 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3671 sz++; /* Count all valid entries */
3672 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3673 {
3674 /* Count the number of files */
3675 sz++;
3676 prev_fnum = qfp->qf_fnum;
3677 }
3678 }
3679 }
3680
3681 return sz;
3682}
3683
3684/*
3685 * Returns the current index of the quickfix/location list.
3686 * Returns 0 if there is an error.
3687 */
3688 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003689qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003690{
3691 qf_info_T *qi = &ql_info;
3692
3693 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3694 {
3695 /* Location list */
3696 qi = GET_LOC_LIST(curwin);
3697 if (qi == NULL)
3698 return 0;
3699 }
3700
3701 return qi->qf_lists[qi->qf_curlist].qf_index;
3702}
3703
3704/*
3705 * Returns the current index in the quickfix/location list (counting only valid
3706 * entries). If no valid entries are in the list, then returns 1.
3707 */
3708 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003709qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003710{
3711 qf_info_T *qi = &ql_info;
3712 qf_list_T *qfl;
3713 qfline_T *qfp;
3714 int i, eidx = 0;
3715 int prev_fnum = 0;
3716
3717 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3718 {
3719 /* Location list */
3720 qi = GET_LOC_LIST(curwin);
3721 if (qi == NULL)
3722 return 1;
3723 }
3724
3725 qfl = &qi->qf_lists[qi->qf_curlist];
3726 qfp = qfl->qf_start;
3727
3728 /* check if the list has valid errors */
3729 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3730 return 1;
3731
3732 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3733 {
3734 if (qfp->qf_valid)
3735 {
3736 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3737 {
3738 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3739 {
3740 /* Count the number of files */
3741 eidx++;
3742 prev_fnum = qfp->qf_fnum;
3743 }
3744 }
3745 else
3746 eidx++;
3747 }
3748 }
3749
3750 return eidx ? eidx : 1;
3751}
3752
3753/*
3754 * Get the 'n'th valid error entry in the quickfix or location list.
3755 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3756 * For :cdo and :ldo returns the 'n'th valid error entry.
3757 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3758 */
3759 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003760qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003761{
3762 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3763 qfline_T *qfp = qfl->qf_start;
3764 int i, eidx;
3765 int prev_fnum = 0;
3766
3767 /* check if the list has valid errors */
3768 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3769 return 1;
3770
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003771 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003772 i++, qfp = qfp->qf_next)
3773 {
3774 if (qfp->qf_valid)
3775 {
3776 if (fdo)
3777 {
3778 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3779 {
3780 /* Count the number of files */
3781 eidx++;
3782 prev_fnum = qfp->qf_fnum;
3783 }
3784 }
3785 else
3786 eidx++;
3787 }
3788
3789 if (eidx == n)
3790 break;
3791 }
3792
3793 if (i <= qfl->qf_count)
3794 return i;
3795 else
3796 return 1;
3797}
3798
3799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003801 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003802 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 */
3804 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003805ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003807 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003808 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003809
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003810 if (eap->cmdidx == CMD_ll
3811 || eap->cmdidx == CMD_lrewind
3812 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003813 || eap->cmdidx == CMD_llast
3814 || eap->cmdidx == CMD_ldo
3815 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003816 {
3817 qi = GET_LOC_LIST(curwin);
3818 if (qi == NULL)
3819 {
3820 EMSG(_(e_loclist));
3821 return;
3822 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003823 }
3824
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003825 if (eap->addr_count > 0)
3826 errornr = (int)eap->line2;
3827 else
3828 {
3829 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3830 errornr = 0;
3831 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3832 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3833 errornr = 1;
3834 else
3835 errornr = 32767;
3836 }
3837
3838 /* For cdo and ldo commands, jump to the nth valid error.
3839 * For cfdo and lfdo commands, jump to the nth valid file entry.
3840 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02003841 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
3842 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003843 errornr = qf_get_nth_valid_entry(qi,
3844 eap->addr_count > 0 ? (int)eap->line1 : 1,
3845 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3846
3847 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848}
3849
3850/*
3851 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003852 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003853 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 */
3855 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003856ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003858 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003859 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003860
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003861 if (eap->cmdidx == CMD_lnext
3862 || eap->cmdidx == CMD_lNext
3863 || eap->cmdidx == CMD_lprevious
3864 || eap->cmdidx == CMD_lnfile
3865 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003866 || eap->cmdidx == CMD_lpfile
3867 || eap->cmdidx == CMD_ldo
3868 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003869 {
3870 qi = GET_LOC_LIST(curwin);
3871 if (qi == NULL)
3872 {
3873 EMSG(_(e_loclist));
3874 return;
3875 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003876 }
3877
Bram Moolenaar55b69262017-08-13 13:42:01 +02003878 if (eap->addr_count > 0
3879 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
3880 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003881 errornr = (int)eap->line2;
3882 else
3883 errornr = 1;
3884
3885 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3886 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003888 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3889 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003891 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3892 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 ? BACKWARD_FILE
3894 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003895 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896}
3897
3898/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003899 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003900 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 */
3902 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003903ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003905 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003906 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003907 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003908#ifdef FEAT_AUTOCMD
3909 char_u *au_name = NULL;
3910#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003911
3912 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003913 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003914 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003915
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003916#ifdef FEAT_AUTOCMD
3917 switch (eap->cmdidx)
3918 {
3919 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3920 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3921 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3922 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3923 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3924 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3925 default: break;
3926 }
3927 if (au_name != NULL)
3928 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3929#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003930#ifdef FEAT_MBYTE
3931 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3932#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003933#ifdef FEAT_BROWSE
3934 if (cmdmod.browse)
3935 {
3936 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3937 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3938 if (browse_file == NULL)
3939 return;
3940 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3941 vim_free(browse_file);
3942 }
3943 else
3944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003946 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003947
3948 /*
3949 * This function is used by the :cfile, :cgetfile and :caddfile
3950 * commands.
3951 * :cfile always creates a new quickfix list and jumps to the
3952 * first error.
3953 * :cgetfile creates a new quickfix list but doesn't jump to the
3954 * first error.
3955 * :caddfile adds to an existing quickfix list. If there is no
3956 * quickfix list then a new list is created.
3957 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003958 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003959 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003960 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003961 && (eap->cmdidx == CMD_cfile
3962 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003963 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003964#ifdef FEAT_AUTOCMD
3965 if (au_name != NULL)
3966 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3967#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003968 if (wp != NULL)
3969 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003970 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003971 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003972
3973 else
3974 {
3975#ifdef FEAT_AUTOCMD
3976 if (au_name != NULL)
3977 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3978#endif
3979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980}
3981
3982/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003983 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003984 * ":vimgrepadd {pattern} file(s)"
3985 * ":lvimgrep {pattern} file(s)"
3986 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003987 */
3988 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003989ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003990{
Bram Moolenaar81695252004-12-29 20:58:21 +00003991 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003992 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003993 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003994 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003995 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003996 char_u *s;
3997 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003998 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003999 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004000#ifdef FEAT_AUTOCMD
4001 qfline_T *cur_qf_start;
4002#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004003 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004004 buf_T *buf;
4005 int duplicate_name = FALSE;
4006 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004007 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004008 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004009 buf_T *first_match_buf = NULL;
4010 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004011 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004012#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4013 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004014#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004015 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004016 int flags = 0;
4017 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004018 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004019 char_u *dirname_start = NULL;
4020 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004021 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004022#ifdef FEAT_AUTOCMD
4023 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004024
4025 switch (eap->cmdidx)
4026 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004027 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4028 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4029 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004030 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004031 case CMD_grep: au_name = (char_u *)"grep"; break;
4032 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4033 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4034 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004035 default: break;
4036 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004037 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4038 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004039 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004040# ifdef FEAT_EVAL
4041 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004042 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004043# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004044 }
4045#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004046
Bram Moolenaar754b5602006-02-09 23:53:20 +00004047 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004048 || eap->cmdidx == CMD_lvimgrep
4049 || eap->cmdidx == CMD_lgrepadd
4050 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004051 {
4052 qi = ll_get_or_alloc_list(curwin);
4053 if (qi == NULL)
4054 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004055 }
4056
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004057 if (eap->addr_count > 0)
4058 tomatch = eap->line2;
4059 else
4060 tomatch = MAXLNUM;
4061
Bram Moolenaar81695252004-12-29 20:58:21 +00004062 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004063 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004064 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004065 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004066 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004067 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004068 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004069 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004070 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004071
4072 if (s != NULL && *s == NUL)
4073 {
4074 /* Pattern is empty, use last search pattern. */
4075 if (last_search_pat() == NULL)
4076 {
4077 EMSG(_(e_noprevre));
4078 goto theend;
4079 }
4080 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4081 }
4082 else
4083 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4084
Bram Moolenaar86b68352004-12-27 21:59:20 +00004085 if (regmatch.regprog == NULL)
4086 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004087 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004088 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004089
4090 p = skipwhite(p);
4091 if (*p == NUL)
4092 {
4093 EMSG(_("E683: File name missing or invalid pattern"));
4094 goto theend;
4095 }
4096
Bram Moolenaar55b69262017-08-13 13:42:01 +02004097 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4098 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004099 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004100 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004101 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004102
4103 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004104 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004105 goto theend;
4106 if (fcount == 0)
4107 {
4108 EMSG(_(e_nomatch));
4109 goto theend;
4110 }
4111
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004112 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4113 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004114 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004115 {
4116 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004117 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004118 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004119
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004120 /* Remember the current directory, because a BufRead autocommand that does
4121 * ":lcd %:p:h" changes the meaning of short path names. */
4122 mch_dirname(dirname_start, MAXPATHL);
4123
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004124#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004125 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004126 * changing the current quickfix list. */
4127 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4128#endif
4129
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004130 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004131 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004132 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004133 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004134 if (time(NULL) > seconds)
4135 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004136 /* Display the file name every second or so, show the user we are
4137 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004138 seconds = time(NULL);
4139 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004140 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004141 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004142 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004143 else
4144 {
4145 msg_outtrans(p);
4146 vim_free(p);
4147 }
4148 msg_clr_eos();
4149 msg_didout = FALSE; /* overwrite this message */
4150 msg_nowait = TRUE; /* don't wait for this message */
4151 msg_col = 0;
4152 out_flush();
4153 }
4154
Bram Moolenaar81695252004-12-29 20:58:21 +00004155 buf = buflist_findname_exp(fnames[fi]);
4156 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4157 {
4158 /* Remember that a buffer with this name already exists. */
4159 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004160 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004161 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004162
4163#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4164 /* Don't do Filetype autocommands to avoid loading syntax and
4165 * indent scripts, a great speed improvement. */
4166 save_ei = au_event_disable(",Filetype");
4167#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004168 /* Don't use modelines here, it's useless. */
4169 save_mls = p_mls;
4170 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004171
4172 /* Load file into a buffer, so that 'fileencoding' is detected,
4173 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004174 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004175
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004176 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004177#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4178 au_event_restore(save_ei);
4179#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004180 }
4181 else
4182 /* Use existing, loaded buffer. */
4183 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004184
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004185#ifdef FEAT_AUTOCMD
4186 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4187 {
4188 int idx;
4189
4190 /* Autocommands changed the quickfix list. Find the one we were
4191 * using and restore it. */
4192 for (idx = 0; idx < LISTCOUNT; ++idx)
4193 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4194 {
4195 qi->qf_curlist = idx;
4196 break;
4197 }
4198 if (idx == LISTCOUNT)
4199 {
4200 /* List cannot be found, create a new one. */
4201 qf_new_list(qi, *eap->cmdlinep);
4202 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4203 }
4204 }
4205#endif
4206
Bram Moolenaar81695252004-12-29 20:58:21 +00004207 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004208 {
4209 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004210 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004211 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004212 else
4213 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004214 /* Try for a match in all lines of the buffer.
4215 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004216 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004217 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4218 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004219 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004220 col = 0;
4221 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004222 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004223 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004224 /* Pass the buffer number so that it gets used even for a
4225 * dummy buffer, unless duplicate_name is set, then the
4226 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004227 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004228 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004229 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004230 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004231 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004232 ml_get_buf(buf,
4233 regmatch.startpos[0].lnum + lnum, FALSE),
4234 regmatch.startpos[0].lnum + lnum,
4235 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004236 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004237 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004238 0, /* nr */
4239 0, /* type */
4240 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004241 ) == FAIL)
4242 {
4243 got_int = TRUE;
4244 break;
4245 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004246 found_match = TRUE;
4247 if (--tomatch == 0)
4248 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004249 if ((flags & VGR_GLOBAL) == 0
4250 || regmatch.endpos[0].lnum > 0)
4251 break;
4252 col = regmatch.endpos[0].col
4253 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004254 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004255 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004256 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004257 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004258 if (got_int)
4259 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004260 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004261#ifdef FEAT_AUTOCMD
4262 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4263#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004264
4265 if (using_dummy)
4266 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004267 if (found_match && first_match_buf == NULL)
4268 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004269 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004270 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004271 /* Never keep a dummy buffer if there is another buffer
4272 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004273 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004274 buf = NULL;
4275 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004276 else if (!cmdmod.hide
4277 || buf->b_p_bh[0] == 'u' /* "unload" */
4278 || buf->b_p_bh[0] == 'w' /* "wipe" */
4279 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004280 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004281 /* When no match was found we don't need to remember the
4282 * buffer, wipe it out. If there was a match and it
4283 * wasn't the first one or we won't jump there: only
4284 * unload the buffer.
4285 * Ignore 'hidden' here, because it may lead to having too
4286 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004287 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004288 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004289 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004290 buf = NULL;
4291 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004292 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004293 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004294 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004295 /* Keeping the buffer, remove the dummy flag. */
4296 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004297 buf = NULL;
4298 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004299 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004300
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004301 if (buf != NULL)
4302 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004303 /* Keeping the buffer, remove the dummy flag. */
4304 buf->b_flags &= ~BF_DUMMY;
4305
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004306 /* If the buffer is still loaded we need to use the
4307 * directory we jumped to below. */
4308 if (buf == first_match_buf
4309 && target_dir == NULL
4310 && STRCMP(dirname_start, dirname_now) != 0)
4311 target_dir = vim_strsave(dirname_now);
4312
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004313 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004314 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004315 * need to be done (again). But not the window-local
4316 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004317 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004318#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004319 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4320 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004321#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004322 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004323 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004324 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004325 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004326 }
4327 }
4328
4329 FreeWild(fcount, fnames);
4330
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004331 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4332 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4333 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004334
4335#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004336 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004337#endif
4338
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004339#ifdef FEAT_AUTOCMD
4340 if (au_name != NULL)
4341 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4342 curbuf->b_fname, TRUE, curbuf);
4343#endif
4344
Bram Moolenaar86b68352004-12-27 21:59:20 +00004345 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004346 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004347 {
4348 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004349 {
4350 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004351 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004352 if (buf != curbuf)
4353 /* If we jumped to another buffer redrawing will already be
4354 * taken care of. */
4355 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004356
4357 /* Jump to the directory used after loading the buffer. */
4358 if (curbuf == first_match_buf && target_dir != NULL)
4359 {
4360 exarg_T ea;
4361
4362 ea.arg = target_dir;
4363 ea.cmdidx = CMD_lcd;
4364 ex_cd(&ea);
4365 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004366 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004367 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004368 else
4369 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004370
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004371 /* If we loaded a dummy buffer into the current window, the autocommands
4372 * may have messed up things, need to redraw and recompute folds. */
4373 if (redraw_for_dummy)
4374 {
4375#ifdef FEAT_FOLDING
4376 foldUpdateAll(curwin);
4377#else
4378 redraw_later(NOT_VALID);
4379#endif
4380 }
4381
Bram Moolenaar86b68352004-12-27 21:59:20 +00004382theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004383 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004384 vim_free(dirname_now);
4385 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004386 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004387 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004388}
4389
4390/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004391 * Restore current working directory to "dirname_start" if they differ, taking
4392 * into account whether it is set locally or globally.
4393 */
4394 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004395restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004396{
4397 char_u *dirname_now = alloc(MAXPATHL);
4398
4399 if (NULL != dirname_now)
4400 {
4401 mch_dirname(dirname_now, MAXPATHL);
4402 if (STRCMP(dirname_start, dirname_now) != 0)
4403 {
4404 /* If the directory has changed, change it back by building up an
4405 * appropriate ex command and executing it. */
4406 exarg_T ea;
4407
4408 ea.arg = dirname_start;
4409 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4410 ex_cd(&ea);
4411 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004412 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004413 }
4414}
4415
4416/*
4417 * Load file "fname" into a dummy buffer and return the buffer pointer,
4418 * placing the directory resulting from the buffer load into the
4419 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4420 * prior to calling this function. Restores directory to "dirname_start" prior
4421 * to returning, if autocmds or the 'autochdir' option have changed it.
4422 *
4423 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4424 * or wipe_dummy_buffer() later!
4425 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004426 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004427 */
4428 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004429load_dummy_buffer(
4430 char_u *fname,
4431 char_u *dirname_start, /* in: old directory */
4432 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004433{
4434 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004435 bufref_T newbufref;
4436 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004437 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004438 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004439
4440 /* Allocate a buffer without putting it in the buffer list. */
4441 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4442 if (newbuf == NULL)
4443 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004444 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004445
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004446 /* Init the options. */
4447 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4448
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004449 /* need to open the memfile before putting the buffer in a window */
4450 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004451 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004452 /* set curwin/curbuf to buf and save a few things */
4453 aucmd_prepbuf(&aco, newbuf);
4454
4455 /* Need to set the filename for autocommands. */
4456 (void)setfname(curbuf, fname, NULL, FALSE);
4457
Bram Moolenaar81695252004-12-29 20:58:21 +00004458 /* Create swap file now to avoid the ATTENTION message. */
4459 check_need_swap(TRUE);
4460
4461 /* Remove the "dummy" flag, otherwise autocommands may not
4462 * work. */
4463 curbuf->b_flags &= ~BF_DUMMY;
4464
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004465 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004466 if (readfile(fname, NULL,
4467 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4468 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004469 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004470 && !(curbuf->b_flags & BF_NEW))
4471 {
4472 failed = FALSE;
4473 if (curbuf != newbuf)
4474 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004475 /* Bloody autocommands changed the buffer! Can happen when
4476 * using netrw and editing a remote file. Use the current
4477 * buffer instead, delete the dummy one after restoring the
4478 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004479 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004480 newbuf = curbuf;
4481 }
4482 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004483
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004484 /* restore curwin/curbuf and a few other things */
4485 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004486 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4487 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004488
4489 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4490 * skip it. */
4491 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004492 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004493
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004494 /*
4495 * When autocommands/'autochdir' option changed directory: go back.
4496 * Let the caller know what the resulting dir was first, in case it is
4497 * important.
4498 */
4499 mch_dirname(resulting_dir, MAXPATHL);
4500 restore_start_dir(dirname_start);
4501
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004502 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004503 return NULL;
4504 if (failed)
4505 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004506 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004507 return NULL;
4508 }
4509 return newbuf;
4510}
4511
4512/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004513 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4514 * directory to "dirname_start" prior to returning, if autocmds or the
4515 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004516 */
4517 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004518wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004519{
4520 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004521 {
4522#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4523 cleanup_T cs;
4524
4525 /* Reset the error/interrupt/exception state here so that aborting()
4526 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4527 * work when got_int is set. */
4528 enter_cleanup(&cs);
4529#endif
4530
Bram Moolenaar81695252004-12-29 20:58:21 +00004531 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004532
4533#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4534 /* Restore the error/interrupt/exception state if not discarded by a
4535 * new aborting error, interrupt, or uncaught exception. */
4536 leave_cleanup(&cs);
4537#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004538 /* When autocommands/'autochdir' option changed directory: go back. */
4539 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004540 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004541}
4542
4543/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004544 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4545 * directory to "dirname_start" prior to returning, if autocmds or the
4546 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004547 */
4548 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004549unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004550{
4551 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004552 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004553 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004554
4555 /* When autocommands/'autochdir' option changed directory: go back. */
4556 restore_start_dir(dirname_start);
4557 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004558}
4559
Bram Moolenaar05159a02005-02-26 23:04:13 +00004560#if defined(FEAT_EVAL) || defined(PROTO)
4561/*
4562 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004563 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004564 */
4565 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004566get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004567{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004568 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004569 dict_T *dict;
4570 char_u buf[2];
4571 qfline_T *qfp;
4572 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004573 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004574
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004575 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004576 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004577 qi = &ql_info;
4578 if (wp != NULL)
4579 {
4580 qi = GET_LOC_LIST(wp);
4581 if (qi == NULL)
4582 return FAIL;
4583 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004584 }
4585
Bram Moolenaard823fa92016-08-12 16:29:27 +02004586 if (qf_idx == -1)
4587 qf_idx = qi->qf_curlist;
4588
4589 if (qf_idx >= qi->qf_listcount
4590 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004591 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004592
Bram Moolenaard823fa92016-08-12 16:29:27 +02004593 qfp = qi->qf_lists[qf_idx].qf_start;
4594 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004595 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004596 /* Handle entries with a non-existing buffer number. */
4597 bufnum = qfp->qf_fnum;
4598 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4599 bufnum = 0;
4600
Bram Moolenaar05159a02005-02-26 23:04:13 +00004601 if ((dict = dict_alloc()) == NULL)
4602 return FAIL;
4603 if (list_append_dict(list, dict) == FAIL)
4604 return FAIL;
4605
4606 buf[0] = qfp->qf_type;
4607 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004608 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004609 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4610 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4611 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4612 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004613 || dict_add_nr_str(dict, "pattern", 0L,
4614 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4615 || dict_add_nr_str(dict, "text", 0L,
4616 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004617 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4618 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4619 return FAIL;
4620
4621 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004622 if (qfp == NULL)
4623 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004624 }
4625 return OK;
4626}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004627
4628/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004629 * Flags used by getqflist()/getloclist() to determine which fields to return.
4630 */
4631enum {
4632 QF_GETLIST_NONE = 0x0,
4633 QF_GETLIST_TITLE = 0x1,
4634 QF_GETLIST_ITEMS = 0x2,
4635 QF_GETLIST_NR = 0x4,
4636 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004637 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004638 QF_GETLIST_ID = 0x20,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004639 QF_GETLIST_ALL = 0xFF
4640};
4641
4642/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004643 * Parse text from 'di' and return the quickfix list items
4644 */
4645 static int
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004646qf_get_list_from_lines(dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004647{
4648 int status = FAIL;
4649 qf_info_T *qi;
4650
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004651 /* Only a List value is supported */
4652 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004653 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004654 list_T *l = list_alloc();
4655
4656 if (l == NULL)
4657 return FAIL;
4658
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004659 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4660 if (qi != NULL)
4661 {
4662 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4663 qi->qf_refcount++;
4664
4665 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, p_efm,
4666 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4667 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004668 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004669 qf_free(qi, 0);
4670 }
4671 free(qi);
4672 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004673 dict_add_list(retdict, "items", l);
4674 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004675 }
4676
4677 return status;
4678}
4679
4680/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004681 * Return quickfix/location list details (title) as a
4682 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4683 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004684 */
4685 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004686get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4687{
4688 qf_info_T *qi = &ql_info;
4689 int status = OK;
4690 int qf_idx;
4691 dictitem_T *di;
4692 int flags = QF_GETLIST_NONE;
4693
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004694 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
4695 return qf_get_list_from_lines(di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004696
Bram Moolenaard823fa92016-08-12 16:29:27 +02004697 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004698 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004699
4700 /* List is not present or is empty */
4701 if (qi == NULL || qi->qf_listcount == 0)
4702 {
4703 /* If querying for the size of the list, return 0 */
4704 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4705 && (di->di_tv.v_type == VAR_STRING)
4706 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4707 return dict_add_nr_str(retdict, "nr", 0, NULL);
4708 return FAIL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004709 }
4710
4711 qf_idx = qi->qf_curlist; /* default is the current list */
4712 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4713 {
4714 /* Use the specified quickfix/location list */
4715 if (di->di_tv.v_type == VAR_NUMBER)
4716 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004717 /* for zero use the current list */
4718 if (di->di_tv.vval.v_number != 0)
4719 {
4720 qf_idx = di->di_tv.vval.v_number - 1;
4721 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4722 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004723 }
Bram Moolenaar55b69262017-08-13 13:42:01 +02004724 }
4725 else if ((di->di_tv.v_type == VAR_STRING)
4726 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004727 /* Get the last quickfix list number */
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004728 qf_idx = qi->qf_listcount - 1;
4729 else
4730 return FAIL;
4731 flags |= QF_GETLIST_NR;
4732 }
4733
4734 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
4735 {
4736 /* Look for a list with the specified id */
4737 if (di->di_tv.v_type == VAR_NUMBER)
4738 {
4739 /* For zero, use the current list or the list specifed by 'nr' */
4740 if (di->di_tv.vval.v_number != 0)
4741 {
4742 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4743 {
4744 if (qi->qf_lists[qf_idx].qf_id == di->di_tv.vval.v_number)
4745 break;
4746 }
4747 if (qf_idx == qi->qf_listcount)
4748 return FAIL; /* List not found */
4749 }
4750 flags |= QF_GETLIST_ID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004751 }
4752 else
4753 return FAIL;
4754 }
4755
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004756 if (dict_find(what, (char_u *)"all", -1) != NULL)
4757 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004758
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004759 if (dict_find(what, (char_u *)"title", -1) != NULL)
4760 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004761
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004762 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4763 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004764
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004765 if (dict_find(what, (char_u *)"context", -1) != NULL)
4766 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004767
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004768 if (dict_find(what, (char_u *)"items", -1) != NULL)
4769 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004770
Bram Moolenaard823fa92016-08-12 16:29:27 +02004771 if (flags & QF_GETLIST_TITLE)
4772 {
4773 char_u *t;
4774 t = qi->qf_lists[qf_idx].qf_title;
4775 if (t == NULL)
4776 t = (char_u *)"";
4777 status = dict_add_nr_str(retdict, "title", 0L, t);
4778 }
4779 if ((status == OK) && (flags & QF_GETLIST_NR))
4780 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4781 if ((status == OK) && (flags & QF_GETLIST_WINID))
4782 {
4783 win_T *win;
4784 win = qf_find_win(qi);
4785 if (win != NULL)
4786 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4787 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004788 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4789 {
4790 list_T *l = list_alloc();
4791 if (l != NULL)
4792 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004793 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004794 dict_add_list(retdict, "items", l);
4795 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004796 else
4797 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004798 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004799
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004800 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4801 {
4802 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4803 {
4804 di = dictitem_alloc((char_u *)"context");
4805 if (di != NULL)
4806 {
4807 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004808 status = dict_add(retdict, di);
4809 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004810 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004811 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004812 else
4813 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004814 }
4815 else
4816 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4817 }
4818
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004819 if ((status == OK) && (flags & QF_GETLIST_ID))
4820 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
4821 NULL);
4822
Bram Moolenaard823fa92016-08-12 16:29:27 +02004823 return status;
4824}
4825
4826/*
4827 * Add list of entries to quickfix/location list. Each list entry is
4828 * a dictionary with item information.
4829 */
4830 static int
4831qf_add_entries(
4832 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004833 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004834 list_T *list,
4835 char_u *title,
4836 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004837{
4838 listitem_T *li;
4839 dict_T *d;
4840 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004841 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004842 long lnum;
4843 int col, nr;
4844 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004845#ifdef FEAT_WINDOWS
4846 qfline_T *old_last = NULL;
4847#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004848 int valid, status;
4849 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004850 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004851
Bram Moolenaara3921f42017-06-04 15:30:34 +02004852 if (action == ' ' || qf_idx == qi->qf_listcount)
4853 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004854 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004855 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004856 qf_idx = qi->qf_curlist;
4857 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004858#ifdef FEAT_WINDOWS
Bram Moolenaara3921f42017-06-04 15:30:34 +02004859 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004860 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004861 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004862#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004863 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004864 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004865 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004866 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004867 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004868
4869 for (li = list->lv_first; li != NULL; li = li->li_next)
4870 {
4871 if (li->li_tv.v_type != VAR_DICT)
4872 continue; /* Skip non-dict items */
4873
4874 d = li->li_tv.vval.v_dict;
4875 if (d == NULL)
4876 continue;
4877
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004878 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004879 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4880 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4881 col = (int)get_dict_number(d, (char_u *)"col");
4882 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4883 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004884 type = get_dict_string(d, (char_u *)"type", TRUE);
4885 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4886 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004887 if (text == NULL)
4888 text = vim_strsave((char_u *)"");
4889
4890 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004891 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004892 valid = FALSE;
4893
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004894 /* Mark entries with non-existing buffer number as not valid. Give the
4895 * error message only once. */
4896 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4897 {
4898 if (!did_bufnr_emsg)
4899 {
4900 did_bufnr_emsg = TRUE;
4901 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4902 }
4903 valid = FALSE;
4904 bufnum = 0;
4905 }
4906
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02004907 /* If the 'valid' field is present it overrules the detected value. */
4908 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
4909 valid = (int)get_dict_number(d, (char_u *)"valid");
4910
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004911 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004912 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004913 NULL, /* dir */
4914 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004915 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004916 text,
4917 lnum,
4918 col,
4919 vcol, /* vis_col */
4920 pattern, /* search pattern */
4921 nr,
4922 type == NULL ? NUL : *type,
4923 valid);
4924
4925 vim_free(filename);
4926 vim_free(pattern);
4927 vim_free(text);
4928 vim_free(type);
4929
4930 if (status == FAIL)
4931 {
4932 retval = FAIL;
4933 break;
4934 }
4935 }
4936
Bram Moolenaara3921f42017-06-04 15:30:34 +02004937 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004938 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004939 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004940 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02004941 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004942 if (action != 'a')
4943 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02004944 qi->qf_lists[qf_idx].qf_ptr =
4945 qi->qf_lists[qf_idx].qf_start;
4946 if (qi->qf_lists[qf_idx].qf_count > 0)
4947 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004948 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004949
4950#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004951 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004952 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004953#endif
4954
4955 return retval;
4956}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004957
4958 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02004959qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004960{
4961 dictitem_T *di;
4962 int retval = FAIL;
4963 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004964 int newlist = FALSE;
4965
4966 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4967 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004968
4969 qf_idx = qi->qf_curlist; /* default is the current list */
4970 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4971 {
4972 /* Use the specified quickfix/location list */
4973 if (di->di_tv.v_type == VAR_NUMBER)
4974 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004975 /* for zero use the current list */
4976 if (di->di_tv.vval.v_number != 0)
4977 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004978
Bram Moolenaar55b69262017-08-13 13:42:01 +02004979 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
4980 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004981 /*
4982 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02004983 * non-available list and add the new list at the end of the
4984 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004985 */
4986 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004987 qf_idx = qi->qf_listcount - 1;
4988 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004989 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004990 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004991 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004992 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004993 }
4994 else if (di->di_tv.v_type == VAR_STRING
4995 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004996 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02004997 if (qi->qf_listcount > 0)
4998 qf_idx = qi->qf_listcount - 1;
4999 else if (newlist)
5000 qf_idx = 0;
5001 else
5002 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005003 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005004 else
5005 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005006 }
5007
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005008 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5009 {
5010 /* Use the quickfix/location list with the specified id */
5011 if (di->di_tv.v_type == VAR_NUMBER)
5012 {
5013 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
5014 if (qi->qf_lists[qf_idx].qf_id == di->di_tv.vval.v_number)
5015 break;
5016 if (qf_idx == qi->qf_listcount)
5017 return FAIL; /* List not found */
5018 }
5019 else
5020 return FAIL;
5021 }
5022
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005023 if (newlist)
5024 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005025 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005026 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005027 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005028 }
5029
5030 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5031 {
5032 if (di->di_tv.v_type == VAR_STRING)
5033 {
5034 vim_free(qi->qf_lists[qf_idx].qf_title);
5035 qi->qf_lists[qf_idx].qf_title =
5036 get_dict_string(what, (char_u *)"title", TRUE);
5037 if (qf_idx == qi->qf_curlist)
5038 qf_update_win_titlevar(qi);
5039 retval = OK;
5040 }
5041 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005042 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5043 {
5044 if (di->di_tv.v_type == VAR_LIST)
5045 {
5046 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5047
5048 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5049 title_save, action == ' ' ? 'a' : action);
5050 vim_free(title_save);
5051 }
5052 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005053
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005054 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005055 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005056 /* Only a List value is supported */
5057 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005058 {
5059 if (action == 'r')
5060 qf_free_items(qi, qf_idx);
5061 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, p_efm,
5062 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5063 retval = OK;
5064 }
5065 else
5066 return FAIL;
5067 }
5068
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005069 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5070 {
5071 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005072
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005073 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005074 ctx = alloc_tv();
5075 if (ctx != NULL)
5076 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005077 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005078 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005079 }
5080
Bram Moolenaard823fa92016-08-12 16:29:27 +02005081 return retval;
5082}
5083
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005084/*
5085 * Find the non-location list window with the specified location list.
5086 */
5087 static win_T *
5088find_win_with_ll(qf_info_T *qi)
5089{
5090 win_T *wp = NULL;
5091
5092 FOR_ALL_WINDOWS(wp)
5093 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5094 return wp;
5095
5096 return NULL;
5097}
5098
5099/*
5100 * Free the entire quickfix/location list stack.
5101 * If the quickfix/location list window is open, then clear it.
5102 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005103 static void
5104qf_free_stack(win_T *wp, qf_info_T *qi)
5105{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005106 win_T *qfwin = qf_find_win(qi);
5107 win_T *llwin = NULL;
5108 win_T *orig_wp = wp;
5109
5110 if (qfwin != NULL)
5111 {
5112 /* If the quickfix/location list window is open, then clear it */
5113 if (qi->qf_curlist < qi->qf_listcount)
5114 qf_free(qi, qi->qf_curlist);
5115 qf_update_buffer(qi, NULL);
5116 }
5117
5118 if (wp != NULL && IS_LL_WINDOW(wp))
5119 {
5120 /* If in the location list window, then use the non-location list
5121 * window with this location list (if present)
5122 */
5123 llwin = find_win_with_ll(qi);
5124 if (llwin != NULL)
5125 wp = llwin;
5126 }
5127
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005128 qf_free_all(wp);
5129 if (wp == NULL)
5130 {
5131 /* quickfix list */
5132 qi->qf_curlist = 0;
5133 qi->qf_listcount = 0;
5134 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005135 else if (IS_LL_WINDOW(orig_wp))
5136 {
5137 /* If the location list window is open, then create a new empty
5138 * location list */
5139 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005140
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005141 /* first free the list reference in the location list window */
5142 ll_free_all(&orig_wp->w_llist_ref);
5143
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005144 orig_wp->w_llist_ref = new_ll;
5145 if (llwin != NULL)
5146 {
5147 llwin->w_llist = new_ll;
5148 new_ll->qf_refcount++;
5149 }
5150 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005151}
5152
Bram Moolenaard823fa92016-08-12 16:29:27 +02005153/*
5154 * Populate the quickfix list with the items supplied in the list
5155 * of dictionaries. "title" will be copied to w:quickfix_title.
5156 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5157 */
5158 int
5159set_errorlist(
5160 win_T *wp,
5161 list_T *list,
5162 int action,
5163 char_u *title,
5164 dict_T *what)
5165{
5166 qf_info_T *qi = &ql_info;
5167 int retval = OK;
5168
5169 if (wp != NULL)
5170 {
5171 qi = ll_get_or_alloc_list(wp);
5172 if (qi == NULL)
5173 return FAIL;
5174 }
5175
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005176 if (action == 'f')
5177 {
5178 /* Free the entire quickfix or location list stack */
5179 qf_free_stack(wp, qi);
5180 }
5181 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005182 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005183 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005184 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005185
5186 return retval;
5187}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005188
5189 static int
5190mark_quickfix_ctx(qf_info_T *qi, int copyID)
5191{
5192 int i;
5193 int abort = FALSE;
5194 typval_T *ctx;
5195
5196 for (i = 0; i < LISTCOUNT && !abort; ++i)
5197 {
5198 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005199 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5200 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005201 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5202 }
5203
5204 return abort;
5205}
5206
5207/*
5208 * Mark the context of the quickfix list and the location lists (if present) as
5209 * "in use". So that garabage collection doesn't free the context.
5210 */
5211 int
5212set_ref_in_quickfix(int copyID)
5213{
5214 int abort = FALSE;
5215 tabpage_T *tp;
5216 win_T *win;
5217
5218 abort = mark_quickfix_ctx(&ql_info, copyID);
5219 if (abort)
5220 return abort;
5221
5222 FOR_ALL_TAB_WINDOWS(tp, win)
5223 {
5224 if (win->w_llist != NULL)
5225 {
5226 abort = mark_quickfix_ctx(win->w_llist, copyID);
5227 if (abort)
5228 return abort;
5229 }
5230 }
5231
5232 return abort;
5233}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005234#endif
5235
Bram Moolenaar81695252004-12-29 20:58:21 +00005236/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005237 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005238 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005239 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005240 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005241 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005242 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005243 */
5244 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005245ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005246{
5247 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005248 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005249#ifdef FEAT_AUTOCMD
5250 char_u *au_name = NULL;
5251#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005252
Bram Moolenaardb552d602006-03-23 22:59:57 +00005253 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5254 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005255 {
5256 qi = ll_get_or_alloc_list(curwin);
5257 if (qi == NULL)
5258 return;
5259 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005260
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005261#ifdef FEAT_AUTOCMD
5262 switch (eap->cmdidx)
5263 {
5264 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5265 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5266 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5267 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5268 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5269 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5270 default: break;
5271 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005272 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5273 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005274 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005275# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005276 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005277 return;
5278# endif
5279 }
5280#endif
5281
Bram Moolenaar86b68352004-12-27 21:59:20 +00005282 if (*eap->arg == NUL)
5283 buf = curbuf;
5284 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5285 buf = buflist_findnr(atoi((char *)eap->arg));
5286 if (buf == NULL)
5287 EMSG(_(e_invarg));
5288 else if (buf->b_ml.ml_mfp == NULL)
5289 EMSG(_("E681: Buffer is not loaded"));
5290 else
5291 {
5292 if (eap->addr_count == 0)
5293 {
5294 eap->line1 = 1;
5295 eap->line2 = buf->b_ml.ml_line_count;
5296 }
5297 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5298 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5299 EMSG(_(e_invrange));
5300 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005301 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005302 char_u *qf_title = *eap->cmdlinep;
5303
5304 if (buf->b_sfname)
5305 {
5306 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5307 (char *)qf_title, (char *)buf->b_sfname);
5308 qf_title = IObuff;
5309 }
5310
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005311 if (qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005312 (eap->cmdidx != CMD_caddbuffer
5313 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005314 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005315 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005316 {
5317#ifdef FEAT_AUTOCMD
5318 if (au_name != NULL)
5319 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5320 curbuf->b_fname, TRUE, curbuf);
5321#endif
5322 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5323 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5324 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005325 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005326 }
5327}
5328
Bram Moolenaar1e015462005-09-25 22:16:38 +00005329#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005330/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005331 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5332 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005333 */
5334 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005335ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005336{
5337 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005338 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005339#ifdef FEAT_AUTOCMD
5340 char_u *au_name = NULL;
5341#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005342
Bram Moolenaardb552d602006-03-23 22:59:57 +00005343 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5344 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005345 {
5346 qi = ll_get_or_alloc_list(curwin);
5347 if (qi == NULL)
5348 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005349 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005350
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005351#ifdef FEAT_AUTOCMD
5352 switch (eap->cmdidx)
5353 {
5354 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5355 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5356 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5357 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5358 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5359 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5360 default: break;
5361 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005362 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5363 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005364 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005365# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005366 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005367 return;
5368# endif
5369 }
5370#endif
5371
Bram Moolenaar4770d092006-01-12 23:22:24 +00005372 /* Evaluate the expression. When the result is a string or a list we can
5373 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005374 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005375 if (tv != NULL)
5376 {
5377 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5378 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5379 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005380 if (qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005381 (eap->cmdidx != CMD_caddexpr
5382 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005383 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5384 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005385 {
5386#ifdef FEAT_AUTOCMD
5387 if (au_name != NULL)
5388 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5389 curbuf->b_fname, TRUE, curbuf);
5390#endif
5391 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5392 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5393 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005394 }
5395 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005396 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005397 free_tv(tv);
5398 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005399}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005400#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005401
5402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 * ":helpgrep {pattern}"
5404 */
5405 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005406ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407{
5408 regmatch_T regmatch;
5409 char_u *save_cpo;
5410 char_u *p;
5411 int fcount;
5412 char_u **fnames;
5413 FILE *fd;
5414 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005416#ifdef FEAT_MULTI_LANG
5417 char_u *lang;
5418#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005419 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005420 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005421 int new_qi = FALSE;
5422 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005423#ifdef FEAT_AUTOCMD
5424 char_u *au_name = NULL;
5425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005427#ifdef FEAT_MULTI_LANG
5428 /* Check for a specified language */
5429 lang = check_help_lang(eap->arg);
5430#endif
5431
Bram Moolenaar73633f82012-01-20 13:39:07 +01005432#ifdef FEAT_AUTOCMD
5433 switch (eap->cmdidx)
5434 {
5435 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5436 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5437 default: break;
5438 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005439 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5440 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005441 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005442# ifdef FEAT_EVAL
5443 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005444 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005445# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005446 }
5447#endif
5448
5449 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5450 save_cpo = p_cpo;
5451 p_cpo = empty_option;
5452
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005453 if (eap->cmdidx == CMD_lhelpgrep)
5454 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005455 /* If the current window is a help window, then use it */
5456 if (bt_help(curwin->w_buffer))
5457 wp = curwin;
5458 else
5459 /* Find an existing help window */
5460 FOR_ALL_WINDOWS(wp)
5461 if (bt_help(wp->w_buffer))
5462 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005463
5464 if (wp == NULL) /* Help window not found */
5465 qi = NULL;
5466 else
5467 qi = wp->w_llist;
5468
5469 if (qi == NULL)
5470 {
5471 /* Allocate a new location list for help text matches */
5472 if ((qi = ll_new_list()) == NULL)
5473 return;
5474 new_qi = TRUE;
5475 }
5476 }
5477
Bram Moolenaaree85df32017-03-19 14:19:50 +01005478 /* Autocommands may change the list. Save it for later comparison */
5479 save_qi = qi;
5480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5482 regmatch.rm_ic = FALSE;
5483 if (regmatch.regprog != NULL)
5484 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005485#ifdef FEAT_MBYTE
5486 vimconv_T vc;
5487
5488 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5489 * differs. */
5490 vc.vc_type = CONV_NONE;
5491 if (!enc_utf8)
5492 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5493#endif
5494
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005496 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497
5498 /* Go through all directories in 'runtimepath' */
5499 p = p_rtp;
5500 while (*p != NUL && !got_int)
5501 {
5502 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5503
5504 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5505 add_pathsep(NameBuff);
5506 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5507 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5508 &fnames, EW_FILE|EW_SILENT) == OK
5509 && fcount > 0)
5510 {
5511 for (fi = 0; fi < fcount && !got_int; ++fi)
5512 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005513#ifdef FEAT_MULTI_LANG
5514 /* Skip files for a different language. */
5515 if (lang != NULL
5516 && STRNICMP(lang, fnames[fi]
5517 + STRLEN(fnames[fi]) - 3, 2) != 0
5518 && !(STRNICMP(lang, "en", 2) == 0
5519 && STRNICMP("txt", fnames[fi]
5520 + STRLEN(fnames[fi]) - 3, 3) == 0))
5521 continue;
5522#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005523 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 if (fd != NULL)
5525 {
5526 lnum = 1;
5527 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5528 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005529 char_u *line = IObuff;
5530#ifdef FEAT_MBYTE
5531 /* Convert a line if 'encoding' is not utf-8 and
5532 * the line contains a non-ASCII character. */
5533 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005534 && has_non_ascii(IObuff))
5535 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005536 line = string_convert(&vc, IObuff, NULL);
5537 if (line == NULL)
5538 line = IObuff;
5539 }
5540#endif
5541
5542 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005544 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
5546 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005547 while (l > 0 && line[l - 1] <= ' ')
5548 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005550 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005551 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 NULL, /* dir */
5553 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005554 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005555 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005557 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005558 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005559 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005560 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 0, /* nr */
5562 1, /* type */
5563 TRUE /* valid */
5564 ) == FAIL)
5565 {
5566 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005567#ifdef FEAT_MBYTE
5568 if (line != IObuff)
5569 vim_free(line);
5570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 break;
5572 }
5573 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005574#ifdef FEAT_MBYTE
5575 if (line != IObuff)
5576 vim_free(line);
5577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 ++lnum;
5579 line_breakcheck();
5580 }
5581 fclose(fd);
5582 }
5583 }
5584 FreeWild(fcount, fnames);
5585 }
5586 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005587
Bram Moolenaar473de612013-06-08 18:19:48 +02005588 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005589#ifdef FEAT_MBYTE
5590 if (vc.vc_type != CONV_NONE)
5591 convert_setup(&vc, NULL, NULL);
5592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005594 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5595 qi->qf_lists[qi->qf_curlist].qf_ptr =
5596 qi->qf_lists[qi->qf_curlist].qf_start;
5597 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005600 if (p_cpo == empty_option)
5601 p_cpo = save_cpo;
5602 else
5603 /* Darn, some plugin changed the value. */
5604 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
5606#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005607 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608#endif
5609
Bram Moolenaar73633f82012-01-20 13:39:07 +01005610#ifdef FEAT_AUTOCMD
5611 if (au_name != NULL)
5612 {
5613 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5614 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005615 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005616 /* autocommands made "qi" invalid */
5617 return;
5618 }
5619#endif
5620
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005622 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005623 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005624 else
5625 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005626
5627 if (eap->cmdidx == CMD_lhelpgrep)
5628 {
5629 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005630 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005631 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005632 {
5633 if (new_qi)
5634 ll_free_all(&qi);
5635 }
5636 else if (curwin->w_llist == NULL)
5637 curwin->w_llist = qi;
5638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639}
5640
5641#endif /* FEAT_QUICKFIX */