blob: bc891bfde27850549ac6bf0747526017a592dad4 [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 Moolenaar68b76a62005-03-25 21:53:48 +000061 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020062 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000063 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020064 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000065 int qf_index; /* current index in the error list */
66 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020067 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020068 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020069 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020070
71 struct dir_stack_T *qf_dir_stack;
72 char_u *qf_directory;
73 struct dir_stack_T *qf_file_stack;
74 char_u *qf_currfile;
75 int qf_multiline;
76 int qf_multiignore;
77 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000078} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020080/*
81 * Quickfix/Location list stack definition
82 * Contains a list of quickfix/location lists (qf_list_T)
83 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000084struct qf_info_S
85{
86 /*
87 * Count of references to this list. Used only for location lists.
88 * When a location list window reference this list, qf_refcount
89 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
90 * reaches 0, the list is freed.
91 */
92 int qf_refcount;
93 int qf_listcount; /* current number of lists */
94 int qf_curlist; /* current error list */
95 qf_list_T qf_lists[LISTCOUNT];
96};
97
98static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000100#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
102/*
103 * Structure used to hold the info of one part of 'errorformat'
104 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000105typedef struct efm_S efm_T;
106struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107{
108 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000109 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
111 char_u prefix; /* prefix of this format line: */
112 /* 'D' enter directory */
113 /* 'X' leave directory */
114 /* 'A' start of multi-line message */
115 /* 'E' error message */
116 /* 'W' warning message */
117 /* 'I' informational message */
118 /* 'C' continuation line */
119 /* 'Z' end of multi-line message */
120 /* 'G' general, unspecific message */
121 /* 'P' push file (partial) message */
122 /* 'Q' pop/quit file (partial) message */
123 /* 'O' overread (partial) message */
124 char_u flags; /* additional flags given in prefix */
125 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000126 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000127 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128};
129
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100130static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
131
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200132static 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 +0200133static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static void qf_new_list(qf_info_T *qi, char_u *qf_title);
135static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200136static 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 +0100137static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void qf_free(qf_info_T *qi, int idx);
139static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200140static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200141static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200143static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
145static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
148static int is_qf_win(win_T *win, qf_info_T *qi);
149static win_T *qf_find_win(qf_info_T *qi);
150static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200151static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200153static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static char_u *get_mef_name(void);
156static void restore_start_dir(char_u *dirname_start);
157static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
158static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
159static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
160static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162/* Quickfix window check helper macro */
163#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
164/* Location list window check helper macro */
165#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
166/*
167 * Return location list for window 'wp'
168 * For location list window, return the referenced location list
169 */
170#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200173 * Looking up a buffer can be slow if there are many. Remember the last one
174 * to make this a lot faster if there are multiple matches in the same file.
175 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200176static char_u *qf_last_bufname = NULL;
177static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178
179/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000180 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200181 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 * Return -1 for error, number of errors for success.
183 */
184 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100185qf_init(
186 win_T *wp,
187 char_u *efile,
188 char_u *errorformat,
189 int newlist, /* TRUE: start a new error list */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100190 char_u *qf_title,
191 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000193 qf_info_T *qi = &ql_info;
194
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000195 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000196 {
197 qi = ll_get_or_alloc_list(wp);
198 if (qi == NULL)
199 return FAIL;
200 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000201
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200202 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
203 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000204}
205
206/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200207 * Maximum number of bytes allowed per line while reading a errorfile.
208 */
209#define LINE_MAXLEN 4096
210
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200211static struct fmtpattern
212{
213 char_u convchar;
214 char *pattern;
215} fmt_pat[FMT_PATTERNS] =
216 {
217 {'f', ".\\+"}, /* only used when at end */
218 {'n', "\\d\\+"},
219 {'l', "\\d\\+"},
220 {'c', "\\d\\+"},
221 {'t', "."},
222 {'m', ".\\+"},
223 {'r', ".*"},
224 {'p', "[- .]*"},
225 {'v', "\\d\\+"},
226 {'s', ".\\+"}
227 };
228
229/*
230 * Converts a 'errorformat' string to regular expression pattern
231 */
232 static int
233efm_to_regpat(
234 char_u *efm,
235 int len,
236 efm_T *fmt_ptr,
237 char_u *regpat,
238 char_u *errmsg)
239{
240 char_u *ptr;
241 char_u *efmp;
242 char_u *srcptr;
243 int round;
244 int idx = 0;
245
246 /*
247 * Build regexp pattern from current 'errorformat' option
248 */
249 ptr = regpat;
250 *ptr++ = '^';
251 round = 0;
252 for (efmp = efm; efmp < efm + len; ++efmp)
253 {
254 if (*efmp == '%')
255 {
256 ++efmp;
257 for (idx = 0; idx < FMT_PATTERNS; ++idx)
258 if (fmt_pat[idx].convchar == *efmp)
259 break;
260 if (idx < FMT_PATTERNS)
261 {
262 if (fmt_ptr->addr[idx])
263 {
264 sprintf((char *)errmsg,
265 _("E372: Too many %%%c in format string"), *efmp);
266 EMSG(errmsg);
267 return -1;
268 }
269 if ((idx
270 && idx < 6
271 && vim_strchr((char_u *)"DXOPQ",
272 fmt_ptr->prefix) != NULL)
273 || (idx == 6
274 && vim_strchr((char_u *)"OPQ",
275 fmt_ptr->prefix) == NULL))
276 {
277 sprintf((char *)errmsg,
278 _("E373: Unexpected %%%c in format string"), *efmp);
279 EMSG(errmsg);
280 return -1;
281 }
282 fmt_ptr->addr[idx] = (char_u)++round;
283 *ptr++ = '\\';
284 *ptr++ = '(';
285#ifdef BACKSLASH_IN_FILENAME
286 if (*efmp == 'f')
287 {
288 /* Also match "c:" in the file name, even when
289 * checking for a colon next: "%f:".
290 * "\%(\a:\)\=" */
291 STRCPY(ptr, "\\%(\\a:\\)\\=");
292 ptr += 10;
293 }
294#endif
295 if (*efmp == 'f' && efmp[1] != NUL)
296 {
297 if (efmp[1] != '\\' && efmp[1] != '%')
298 {
299 /* A file name may contain spaces, but this isn't
300 * in "\f". For "%f:%l:%m" there may be a ":" in
301 * the file name. Use ".\{-1,}x" instead (x is
302 * the next character), the requirement that :999:
303 * follows should work. */
304 STRCPY(ptr, ".\\{-1,}");
305 ptr += 7;
306 }
307 else
308 {
309 /* File name followed by '\\' or '%': include as
310 * many file name chars as possible. */
311 STRCPY(ptr, "\\f\\+");
312 ptr += 4;
313 }
314 }
315 else
316 {
317 srcptr = (char_u *)fmt_pat[idx].pattern;
318 while ((*ptr = *srcptr++) != NUL)
319 ++ptr;
320 }
321 *ptr++ = '\\';
322 *ptr++ = ')';
323 }
324 else if (*efmp == '*')
325 {
326 if (*++efmp == '[' || *efmp == '\\')
327 {
328 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
329 {
330 if (efmp[1] == '^')
331 *ptr++ = *++efmp;
332 if (efmp < efm + len)
333 {
334 *ptr++ = *++efmp; /* could be ']' */
335 while (efmp < efm + len
336 && (*ptr++ = *++efmp) != ']')
337 /* skip */;
338 if (efmp == efm + len)
339 {
340 EMSG(_("E374: Missing ] in format string"));
341 return -1;
342 }
343 }
344 }
345 else if (efmp < efm + len) /* %*\D, %*\s etc. */
346 *ptr++ = *++efmp;
347 *ptr++ = '\\';
348 *ptr++ = '+';
349 }
350 else
351 {
352 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
353 sprintf((char *)errmsg,
354 _("E375: Unsupported %%%c in format string"), *efmp);
355 EMSG(errmsg);
356 return -1;
357 }
358 }
359 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
360 *ptr++ = *efmp; /* regexp magic characters */
361 else if (*efmp == '#')
362 *ptr++ = '*';
363 else if (*efmp == '>')
364 fmt_ptr->conthere = TRUE;
365 else if (efmp == efm + 1) /* analyse prefix */
366 {
367 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
368 fmt_ptr->flags = *efmp++;
369 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
370 fmt_ptr->prefix = *efmp;
371 else
372 {
373 sprintf((char *)errmsg,
374 _("E376: Invalid %%%c in format string prefix"), *efmp);
375 EMSG(errmsg);
376 return -1;
377 }
378 }
379 else
380 {
381 sprintf((char *)errmsg,
382 _("E377: Invalid %%%c in format string"), *efmp);
383 EMSG(errmsg);
384 return -1;
385 }
386 }
387 else /* copy normal character */
388 {
389 if (*efmp == '\\' && efmp + 1 < efm + len)
390 ++efmp;
391 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
392 *ptr++ = '\\'; /* escape regexp atoms */
393 if (*efmp)
394 *ptr++ = *efmp;
395 }
396 }
397 *ptr++ = '$';
398 *ptr = NUL;
399
400 return 0;
401}
402
403 static void
404free_efm_list(efm_T **efm_first)
405{
406 efm_T *efm_ptr;
407
408 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
409 {
410 *efm_first = efm_ptr->next;
411 vim_regfree(efm_ptr->prog);
412 vim_free(efm_ptr);
413 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100414 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200415}
416
417/* Parse 'errorformat' option */
418 static efm_T *
419parse_efm_option(char_u *efm)
420{
421 char_u *errmsg = NULL;
422 int errmsglen;
423 efm_T *fmt_ptr = NULL;
424 efm_T *fmt_first = NULL;
425 efm_T *fmt_last = NULL;
426 char_u *fmtstr = NULL;
427 int len;
428 int i;
429 int round;
430
431 errmsglen = CMDBUFFSIZE + 1;
432 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
433 if (errmsg == NULL)
434 goto parse_efm_end;
435
436 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200437 * Each part of the format string is copied and modified from errorformat
438 * to regex prog. Only a few % characters are allowed.
439 */
440
441 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 * Get some space to modify the format string into.
443 */
444 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
445 for (round = FMT_PATTERNS; round > 0; )
446 i += (int)STRLEN(fmt_pat[--round].pattern);
447#ifdef COLON_IN_FILENAME
448 i += 12; /* "%f" can become twelve chars longer */
449#else
450 i += 2; /* "%f" can become two chars longer */
451#endif
452 if ((fmtstr = alloc(i)) == NULL)
453 goto parse_efm_error;
454
455 while (efm[0] != NUL)
456 {
457 /*
458 * Allocate a new eformat structure and put it at the end of the list
459 */
460 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
461 if (fmt_ptr == NULL)
462 goto parse_efm_error;
463 if (fmt_first == NULL) /* first one */
464 fmt_first = fmt_ptr;
465 else
466 fmt_last->next = fmt_ptr;
467 fmt_last = fmt_ptr;
468
469 /*
470 * Isolate one part in the 'errorformat' option
471 */
472 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
473 if (efm[len] == '\\' && efm[len + 1] != NUL)
474 ++len;
475
476 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
477 goto parse_efm_error;
478 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
479 goto parse_efm_error;
480 /*
481 * Advance to next part
482 */
483 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
484 }
485
486 if (fmt_first == NULL) /* nothing found */
487 EMSG(_("E378: 'errorformat' contains no pattern"));
488
489 goto parse_efm_end;
490
491parse_efm_error:
492 free_efm_list(&fmt_first);
493
494parse_efm_end:
495 vim_free(fmtstr);
496 vim_free(errmsg);
497
498 return fmt_first;
499}
500
Bram Moolenaare0d37972016-07-15 22:36:01 +0200501enum {
502 QF_FAIL = 0,
503 QF_OK = 1,
504 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200505 QF_NOMEM = 3,
506 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200507};
508
509typedef struct {
510 char_u *linebuf;
511 int linelen;
512 char_u *growbuf;
513 int growbufsiz;
514 FILE *fd;
515 typval_T *tv;
516 char_u *p_str;
517 listitem_T *p_li;
518 buf_T *buf;
519 linenr_T buflnum;
520 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100521 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200522} qfstate_T;
523
524 static char_u *
525qf_grow_linebuf(qfstate_T *state, int newsz)
526{
527 /*
528 * If the line exceeds LINE_MAXLEN exclude the last
529 * byte since it's not a NL character.
530 */
531 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
532 if (state->growbuf == NULL)
533 {
534 state->growbuf = alloc(state->linelen + 1);
535 if (state->growbuf == NULL)
536 return NULL;
537 state->growbufsiz = state->linelen;
538 }
539 else if (state->linelen > state->growbufsiz)
540 {
541 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
542 if (state->growbuf == NULL)
543 return NULL;
544 state->growbufsiz = state->linelen;
545 }
546 return state->growbuf;
547}
548
549/*
550 * Get the next string (separated by newline) from state->p_str.
551 */
552 static int
553qf_get_next_str_line(qfstate_T *state)
554{
555 /* Get the next line from the supplied string */
556 char_u *p_str = state->p_str;
557 char_u *p;
558 int len;
559
560 if (*p_str == NUL) /* Reached the end of the string */
561 return QF_END_OF_INPUT;
562
563 p = vim_strchr(p_str, '\n');
564 if (p != NULL)
565 len = (int)(p - p_str) + 1;
566 else
567 len = (int)STRLEN(p_str);
568
569 if (len > IOSIZE - 2)
570 {
571 state->linebuf = qf_grow_linebuf(state, len);
572 if (state->linebuf == NULL)
573 return QF_NOMEM;
574 }
575 else
576 {
577 state->linebuf = IObuff;
578 state->linelen = len;
579 }
580 vim_strncpy(state->linebuf, p_str, state->linelen);
581
582 /*
583 * Increment using len in order to discard the rest of the
584 * line if it exceeds LINE_MAXLEN.
585 */
586 p_str += len;
587 state->p_str = p_str;
588
589 return QF_OK;
590}
591
592/*
593 * Get the next string from state->p_Li.
594 */
595 static int
596qf_get_next_list_line(qfstate_T *state)
597{
598 listitem_T *p_li = state->p_li;
599 int len;
600
601 while (p_li != NULL
602 && (p_li->li_tv.v_type != VAR_STRING
603 || p_li->li_tv.vval.v_string == NULL))
604 p_li = p_li->li_next; /* Skip non-string items */
605
606 if (p_li == NULL) /* End of the list */
607 {
608 state->p_li = NULL;
609 return QF_END_OF_INPUT;
610 }
611
612 len = (int)STRLEN(p_li->li_tv.vval.v_string);
613 if (len > IOSIZE - 2)
614 {
615 state->linebuf = qf_grow_linebuf(state, len);
616 if (state->linebuf == NULL)
617 return QF_NOMEM;
618 }
619 else
620 {
621 state->linebuf = IObuff;
622 state->linelen = len;
623 }
624
625 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
626
627 state->p_li = p_li->li_next; /* next item */
628 return QF_OK;
629}
630
631/*
632 * Get the next string from state->buf.
633 */
634 static int
635qf_get_next_buf_line(qfstate_T *state)
636{
637 char_u *p_buf = NULL;
638 int len;
639
640 /* Get the next line from the supplied buffer */
641 if (state->buflnum > state->lnumlast)
642 return QF_END_OF_INPUT;
643
644 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
645 state->buflnum += 1;
646
647 len = (int)STRLEN(p_buf);
648 if (len > IOSIZE - 2)
649 {
650 state->linebuf = qf_grow_linebuf(state, len);
651 if (state->linebuf == NULL)
652 return QF_NOMEM;
653 }
654 else
655 {
656 state->linebuf = IObuff;
657 state->linelen = len;
658 }
659 vim_strncpy(state->linebuf, p_buf, state->linelen);
660
661 return QF_OK;
662}
663
664/*
665 * Get the next string from file state->fd.
666 */
667 static int
668qf_get_next_file_line(qfstate_T *state)
669{
670 int discard;
671 int growbuflen;
672
673 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
674 return QF_END_OF_INPUT;
675
676 discard = FALSE;
677 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200678 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200679 {
680 /*
681 * The current line exceeds IObuff, continue reading using
682 * growbuf until EOL or LINE_MAXLEN bytes is read.
683 */
684 if (state->growbuf == NULL)
685 {
686 state->growbufsiz = 2 * (IOSIZE - 1);
687 state->growbuf = alloc(state->growbufsiz);
688 if (state->growbuf == NULL)
689 return QF_NOMEM;
690 }
691
692 /* Copy the read part of the line, excluding null-terminator */
693 memcpy(state->growbuf, IObuff, IOSIZE - 1);
694 growbuflen = state->linelen;
695
696 for (;;)
697 {
698 if (fgets((char *)state->growbuf + growbuflen,
699 state->growbufsiz - growbuflen, state->fd) == NULL)
700 break;
701 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
702 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200703 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200704 break;
705 if (state->growbufsiz == LINE_MAXLEN)
706 {
707 discard = TRUE;
708 break;
709 }
710
711 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
712 ? 2 * state->growbufsiz : LINE_MAXLEN;
713 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
714 if (state->growbuf == NULL)
715 return QF_NOMEM;
716 }
717
718 while (discard)
719 {
720 /*
721 * The current line is longer than LINE_MAXLEN, continue
722 * reading but discard everything until EOL or EOF is
723 * reached.
724 */
725 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
726 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200727 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200728 break;
729 }
730
731 state->linebuf = state->growbuf;
732 state->linelen = growbuflen;
733 }
734 else
735 state->linebuf = IObuff;
736
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100737#ifdef FEAT_MBYTE
738 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200739 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
740 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100741 char_u *line;
742
743 line = string_convert(&state->vc, state->linebuf, &state->linelen);
744 if (line != NULL)
745 {
746 if (state->linelen < IOSIZE)
747 {
748 STRCPY(state->linebuf, line);
749 vim_free(line);
750 }
751 else
752 {
753 vim_free(state->growbuf);
754 state->linebuf = state->growbuf = line;
755 state->growbufsiz = state->linelen < LINE_MAXLEN
756 ? state->linelen : LINE_MAXLEN;
757 }
758 }
759 }
760#endif
761
Bram Moolenaare0d37972016-07-15 22:36:01 +0200762 return QF_OK;
763}
764
765/*
766 * Get the next string from a file/buffer/list/string.
767 */
768 static int
769qf_get_nextline(qfstate_T *state)
770{
771 int status = QF_FAIL;
772
773 if (state->fd == NULL)
774 {
775 if (state->tv != NULL)
776 {
777 if (state->tv->v_type == VAR_STRING)
778 /* Get the next line from the supplied string */
779 status = qf_get_next_str_line(state);
780 else if (state->tv->v_type == VAR_LIST)
781 /* Get the next line from the supplied list */
782 status = qf_get_next_list_line(state);
783 }
784 else
785 /* Get the next line from the supplied buffer */
786 status = qf_get_next_buf_line(state);
787 }
788 else
789 /* Get the next line from the supplied file */
790 status = qf_get_next_file_line(state);
791
792 if (status != QF_OK)
793 return status;
794
795 /* remove newline/CR from the line */
796 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200797 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 state->linebuf[state->linelen - 1] = NUL;
799#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200800 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
801 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200803 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804
805#ifdef FEAT_MBYTE
806 remove_bom(state->linebuf);
807#endif
808
809 return QF_OK;
810}
811
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200812typedef struct {
813 char_u *namebuf;
814 char_u *errmsg;
815 int errmsglen;
816 long lnum;
817 int col;
818 char_u use_viscol;
819 char_u *pattern;
820 int enr;
821 int type;
822 int valid;
823} qffields_T;
824
825/*
826 * Parse a line and get the quickfix fields.
827 * Return the QF_ status.
828 */
829 static int
830qf_parse_line(
831 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200832 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200833 char_u *linebuf,
834 int linelen,
835 efm_T *fmt_first,
836 qffields_T *fields)
837{
838 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200839 char_u *ptr;
840 int len;
841 int i;
842 int idx = 0;
843 char_u *tail = NULL;
844 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200845 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200846
847 /* Always ignore case when looking for a matching error. */
848 regmatch.rm_ic = TRUE;
849
850 /* If there was no %> item start at the first pattern */
851 if (fmt_start == NULL)
852 fmt_ptr = fmt_first;
853 else
854 {
855 fmt_ptr = fmt_start;
856 fmt_start = NULL;
857 }
858
859 /*
860 * Try to match each part of 'errorformat' until we find a complete
861 * match or no match.
862 */
863 fields->valid = TRUE;
864restofline:
865 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
866 {
867 int r;
868
869 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200870 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200871 continue;
872 fields->namebuf[0] = NUL;
873 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200874 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200875 fields->errmsg[0] = NUL;
876 fields->lnum = 0;
877 fields->col = 0;
878 fields->use_viscol = FALSE;
879 fields->enr = -1;
880 fields->type = 0;
881 tail = NULL;
882
883 regmatch.regprog = fmt_ptr->prog;
884 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
885 fmt_ptr->prog = regmatch.regprog;
886 if (r)
887 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200888 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200889 continue;
890 if (vim_strchr((char_u *)"EWI", idx) != NULL)
891 fields->type = idx;
892 else
893 fields->type = 0;
894 /*
895 * Extract error message data from matched line.
896 * We check for an actual submatch, because "\[" and "\]" in
897 * the 'errorformat' may cause the wrong submatch to be used.
898 */
899 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
900 {
901 int c;
902
903 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
904 continue;
905
906 /* Expand ~/file and $HOME/file to full path. */
907 c = *regmatch.endp[i];
908 *regmatch.endp[i] = NUL;
909 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
910 *regmatch.endp[i] = c;
911
912 if (vim_strchr((char_u *)"OPQ", idx) != NULL
913 && mch_getperm(fields->namebuf) == -1)
914 continue;
915 }
916 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
917 {
918 if (regmatch.startp[i] == NULL)
919 continue;
920 fields->enr = (int)atol((char *)regmatch.startp[i]);
921 }
922 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
923 {
924 if (regmatch.startp[i] == NULL)
925 continue;
926 fields->lnum = atol((char *)regmatch.startp[i]);
927 }
928 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
929 {
930 if (regmatch.startp[i] == NULL)
931 continue;
932 fields->col = (int)atol((char *)regmatch.startp[i]);
933 }
934 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
935 {
936 if (regmatch.startp[i] == NULL)
937 continue;
938 fields->type = *regmatch.startp[i];
939 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200940 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200941 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200942 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200943 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200944 /* linelen + null terminator */
945 if ((fields->errmsg = vim_realloc(fields->errmsg,
946 linelen + 1)) == NULL)
947 return QF_NOMEM;
948 fields->errmsglen = linelen + 1;
949 }
950 vim_strncpy(fields->errmsg, linebuf, linelen);
951 }
952 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
953 {
954 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
955 continue;
956 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200957 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200958 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200959 /* len + null terminator */
960 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
961 == NULL)
962 return QF_NOMEM;
963 fields->errmsglen = len + 1;
964 }
965 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
966 }
967 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
968 {
969 if (regmatch.startp[i] == NULL)
970 continue;
971 tail = regmatch.startp[i];
972 }
973 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
974 {
975 char_u *match_ptr;
976
977 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
978 continue;
979 fields->col = 0;
980 for (match_ptr = regmatch.startp[i];
981 match_ptr != regmatch.endp[i]; ++match_ptr)
982 {
983 ++fields->col;
984 if (*match_ptr == TAB)
985 {
986 fields->col += 7;
987 fields->col -= fields->col % 8;
988 }
989 }
990 ++fields->col;
991 fields->use_viscol = TRUE;
992 }
993 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
994 {
995 if (regmatch.startp[i] == NULL)
996 continue;
997 fields->col = (int)atol((char *)regmatch.startp[i]);
998 fields->use_viscol = TRUE;
999 }
1000 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1001 {
1002 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1003 continue;
1004 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1005 if (len > CMDBUFFSIZE - 5)
1006 len = CMDBUFFSIZE - 5;
1007 STRCPY(fields->pattern, "^\\V");
1008 STRNCAT(fields->pattern, regmatch.startp[i], len);
1009 fields->pattern[len + 3] = '\\';
1010 fields->pattern[len + 4] = '$';
1011 fields->pattern[len + 5] = NUL;
1012 }
1013 break;
1014 }
1015 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001016 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001017
1018 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1019 {
1020 if (fmt_ptr != NULL)
1021 {
1022 if (idx == 'D') /* enter directory */
1023 {
1024 if (*fields->namebuf == NUL)
1025 {
1026 EMSG(_("E379: Missing or empty directory name"));
1027 return QF_FAIL;
1028 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001029 qfl->qf_directory =
1030 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1031 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001032 return QF_FAIL;
1033 }
1034 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001035 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001036 }
1037 fields->namebuf[0] = NUL; /* no match found, remove file name */
1038 fields->lnum = 0; /* don't jump to this line */
1039 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001040 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001041 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001042 /* linelen + null terminator */
1043 if ((fields->errmsg = vim_realloc(fields->errmsg,
1044 linelen + 1)) == NULL)
1045 return QF_NOMEM;
1046 fields->errmsglen = linelen + 1;
1047 }
1048 /* copy whole line to error message */
1049 vim_strncpy(fields->errmsg, linebuf, linelen);
1050 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001051 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001052 }
1053 else if (fmt_ptr != NULL)
1054 {
1055 /* honor %> item */
1056 if (fmt_ptr->conthere)
1057 fmt_start = fmt_ptr;
1058
1059 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1060 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001061 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1062 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001063 }
1064 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1065 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001066 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001067 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001068 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001069
Bram Moolenaar9b457942016-10-09 16:10:05 +02001070 if (qfprev == NULL)
1071 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001072 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001073 {
1074 len = (int)STRLEN(qfprev->qf_text);
1075 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1076 == NULL)
1077 return QF_FAIL;
1078 STRCPY(ptr, qfprev->qf_text);
1079 vim_free(qfprev->qf_text);
1080 qfprev->qf_text = ptr;
1081 *(ptr += len) = '\n';
1082 STRCPY(++ptr, fields->errmsg);
1083 }
1084 if (qfprev->qf_nr == -1)
1085 qfprev->qf_nr = fields->enr;
1086 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1087 /* only printable chars allowed */
1088 qfprev->qf_type = fields->type;
1089
1090 if (!qfprev->qf_lnum)
1091 qfprev->qf_lnum = fields->lnum;
1092 if (!qfprev->qf_col)
1093 qfprev->qf_col = fields->col;
1094 qfprev->qf_viscol = fields->use_viscol;
1095 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001096 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1097 qfl->qf_directory,
1098 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001099 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001100 : qfl->qf_currfile != NULL && fields->valid
1101 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001102 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001103 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001104 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001105 line_breakcheck();
1106 return QF_IGNORE_LINE;
1107 }
1108 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1109 {
1110 /* global file names */
1111 fields->valid = FALSE;
1112 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1113 {
1114 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001115 qfl->qf_currfile =
1116 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001117 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001118 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001119 *fields->namebuf = NUL;
1120 if (tail && *tail)
1121 {
1122 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001123 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001124 goto restofline;
1125 }
1126 }
1127 }
1128 if (fmt_ptr->flags == '-') /* generally exclude this line */
1129 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001130 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001131 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001132 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001133 return QF_IGNORE_LINE;
1134 }
1135 }
1136
1137 return QF_OK;
1138}
1139
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001140/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001141 * Read the errorfile "efile" into memory, line by line, building the error
1142 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001143 * Alternative: when "efile" is NULL read errors from buffer "buf".
1144 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001145 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001146 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1147 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001148 * Return -1 for error, number of errors for success.
1149 */
1150 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001151qf_init_ext(
1152 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001153 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001154 char_u *efile,
1155 buf_T *buf,
1156 typval_T *tv,
1157 char_u *errorformat,
1158 int newlist, /* TRUE: start a new error list */
1159 linenr_T lnumfirst, /* first line number to use */
1160 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001161 char_u *qf_title,
1162 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001163{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001164 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001165 qfstate_T state;
1166 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001167#ifdef FEAT_WINDOWS
1168 qfline_T *old_last = NULL;
1169#endif
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001170 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001171 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001173 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001175 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001177 /* Do not used the cached buffer, it may have been wiped out. */
1178 vim_free(qf_last_bufname);
1179 qf_last_bufname = NULL;
1180
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001181 vim_memset(&state, 0, sizeof(state));
1182 vim_memset(&fields, 0, sizeof(fields));
1183#ifdef FEAT_MBYTE
1184 state.vc.vc_type = CONV_NONE;
1185 if (enc != NULL && *enc != NUL)
1186 convert_setup(&state.vc, enc, p_enc);
1187#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001188 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1189 fields.errmsglen = CMDBUFFSIZE + 1;
1190 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1191 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1192 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1193 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 goto qf_init_end;
1195
Bram Moolenaare0d37972016-07-15 22:36:01 +02001196 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
1198 EMSG2(_(e_openerrf), efile);
1199 goto qf_init_end;
1200 }
1201
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001202 if (newlist || qf_idx == qi->qf_listcount)
1203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001205 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001206 qf_idx = qi->qf_curlist;
1207 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001208 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001209 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001210 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001211 adding = TRUE;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001212#ifdef FEAT_WINDOWS
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001213 if (qi->qf_lists[qf_idx].qf_count > 0)
1214 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001215#endif
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001218 qfl = &qi->qf_lists[qf_idx];
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001221 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001222 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 else
1224 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001226 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001227 * If the errorformat didn't change between calls, then reuse the
1228 * previously parsed values.
1229 */
1230 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1231 {
1232 /* free the previously parsed data */
1233 vim_free(last_efm);
1234 last_efm = NULL;
1235 free_efm_list(&fmt_first);
1236
1237 /* parse the current 'efm' */
1238 fmt_first = parse_efm_option(efm);
1239 if (fmt_first != NULL)
1240 last_efm = vim_strsave(efm);
1241 }
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
1246 /*
1247 * got_int is reset here, because it was probably set when killing the
1248 * ":make" command, but we still want to read the errorfile then.
1249 */
1250 got_int = FALSE;
1251
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001252 if (tv != NULL)
1253 {
1254 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001255 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001256 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001257 state.p_li = tv->vval.v_list->lv_first;
1258 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001259 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001260 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001261 state.buflnum = lnumfirst;
1262 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /*
1265 * Read the lines in the error file one by one.
1266 * Try to recognize one of the error formats in each line.
1267 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001268 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001270 /* Get the next line from a file/buffer/list/string */
1271 status = qf_get_nextline(&state);
1272 if (status == QF_NOMEM) /* memory alloc failure */
1273 goto qf_init_end;
1274 if (status == QF_END_OF_INPUT) /* end of input */
1275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001277 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1278 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001279 if (status == QF_FAIL)
1280 goto error2;
1281 if (status == QF_NOMEM)
1282 goto qf_init_end;
1283 if (status == QF_IGNORE_LINE)
1284 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001286 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001287 qf_idx,
1288 qfl->qf_directory,
1289 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001290 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001291 : ((qfl->qf_currfile != NULL && fields.valid)
1292 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001293 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001294 fields.errmsg,
1295 fields.lnum,
1296 fields.col,
1297 fields.use_viscol,
1298 fields.pattern,
1299 fields.enr,
1300 fields.type,
1301 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 goto error2;
1303 line_breakcheck();
1304 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001305 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001307 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001309 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001310 qfl->qf_ptr = qfl->qf_start;
1311 qfl->qf_index = 1;
1312 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 else
1315 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001316 qfl->qf_nonevalid = FALSE;
1317 if (qfl->qf_ptr == NULL)
1318 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001320 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001321 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001322 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324 EMSG(_(e_readerrf));
1325error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001326 if (!adding)
1327 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001328 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001329 qf_free(qi, qi->qf_curlist);
1330 qi->qf_listcount--;
1331 if (qi->qf_curlist > 0)
1332 --qi->qf_curlist;
1333 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001334qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001335 if (state.fd != NULL)
1336 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001337 vim_free(fields.namebuf);
1338 vim_free(fields.errmsg);
1339 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001340 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342#ifdef FEAT_WINDOWS
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001343 if (qf_idx == qi->qf_curlist)
1344 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001346#ifdef FEAT_MBYTE
1347 if (state.vc.vc_type != CONV_NONE)
1348 convert_setup(&state.vc, NULL, NULL);
1349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
1351 return retval;
1352}
1353
Bram Moolenaarfb604092014-07-23 15:55:00 +02001354 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001355qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001356{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001357 vim_free(qi->qf_lists[qf_idx].qf_title);
1358 qi->qf_lists[qf_idx].qf_title = NULL;
1359
Bram Moolenaarfb604092014-07-23 15:55:00 +02001360 if (title != NULL)
1361 {
1362 char_u *p = alloc((int)STRLEN(title) + 2);
1363
Bram Moolenaara3921f42017-06-04 15:30:34 +02001364 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001365 if (p != NULL)
1366 sprintf((char *)p, ":%s", (char *)title);
1367 }
1368}
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370/*
1371 * Prepare for adding a new quickfix list.
1372 */
1373 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001374qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
1376 int i;
1377
1378 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001379 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 * the current entry. This makes it possible to browse in a tree-like
1381 * way with ":grep'.
1382 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001383 while (qi->qf_listcount > qi->qf_curlist + 1)
1384 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
1386 /*
1387 * When the stack is full, remove to oldest entry
1388 * Otherwise, add a new entry.
1389 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001390 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001392 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001394 qi->qf_lists[i - 1] = qi->qf_lists[i];
1395 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001398 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001399 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001400 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401}
1402
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001403/*
1404 * Free a location list
1405 */
1406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001407ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001408{
1409 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001410 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001411
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001412 qi = *pqi;
1413 if (qi == NULL)
1414 return;
1415 *pqi = NULL; /* Remove reference to this list */
1416
1417 qi->qf_refcount--;
1418 if (qi->qf_refcount < 1)
1419 {
1420 /* No references to this location list */
1421 for (i = 0; i < qi->qf_listcount; ++i)
1422 qf_free(qi, i);
1423 vim_free(qi);
1424 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001425}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001426
1427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001428qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001429{
1430 int i;
1431 qf_info_T *qi = &ql_info;
1432
1433 if (wp != NULL)
1434 {
1435 /* location list */
1436 ll_free_all(&wp->w_llist);
1437 ll_free_all(&wp->w_llist_ref);
1438 }
1439 else
1440 /* quickfix list */
1441 for (i = 0; i < qi->qf_listcount; ++i)
1442 qf_free(qi, i);
1443}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445/*
1446 * Add an entry to the end of the list of errors.
1447 * Returns OK or FAIL.
1448 */
1449 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001450qf_add_entry(
1451 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001452 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001453 char_u *dir, /* optional directory name */
1454 char_u *fname, /* file name or NULL */
1455 int bufnum, /* buffer number or zero */
1456 char_u *mesg, /* message */
1457 long lnum, /* line number */
1458 int col, /* column */
1459 int vis_col, /* using visual column */
1460 char_u *pattern, /* search pattern */
1461 int nr, /* error number */
1462 int type, /* type character */
1463 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001465 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001466 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001468 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001470 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001471 {
1472 buf_T *buf = buflist_findnr(bufnum);
1473
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001474 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001475 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001476 buf->b_has_qf_entry |=
1477 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001478 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001479 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001480 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1482 {
1483 vim_free(qfp);
1484 return FAIL;
1485 }
1486 qfp->qf_lnum = lnum;
1487 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001488 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001489 if (pattern == NULL || *pattern == NUL)
1490 qfp->qf_pattern = NULL;
1491 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1492 {
1493 vim_free(qfp->qf_text);
1494 vim_free(qfp);
1495 return FAIL;
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 qfp->qf_nr = nr;
1498 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1499 type = 0;
1500 qfp->qf_type = type;
1501 qfp->qf_valid = valid;
1502
Bram Moolenaara3921f42017-06-04 15:30:34 +02001503 lastp = &qi->qf_lists[qf_idx].qf_last;
1504 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001505 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001507 qi->qf_lists[qf_idx].qf_start = qfp;
1508 qi->qf_lists[qf_idx].qf_ptr = qfp;
1509 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001510 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 else
1513 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001514 qfp->qf_prev = *lastp;
1515 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001517 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001519 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001520 ++qi->qf_lists[qf_idx].qf_count;
1521 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001524 qi->qf_lists[qf_idx].qf_index =
1525 qi->qf_lists[qf_idx].qf_count;
1526 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
1528
1529 return OK;
1530}
1531
1532/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001533 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001535 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001536ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001537{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001538 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001539
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001540 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1541 if (qi != NULL)
1542 {
1543 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1544 qi->qf_refcount++;
1545 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001546
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001547 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548}
1549
1550/*
1551 * Return the location list for window 'wp'.
1552 * If not present, allocate a location list
1553 */
1554 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001555ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001556{
1557 if (IS_LL_WINDOW(wp))
1558 /* For a location list window, use the referenced location list */
1559 return wp->w_llist_ref;
1560
1561 /*
1562 * For a non-location list window, w_llist_ref should not point to a
1563 * location list.
1564 */
1565 ll_free_all(&wp->w_llist_ref);
1566
1567 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001568 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001569 return wp->w_llist;
1570}
1571
1572/*
1573 * Copy the location list from window "from" to window "to".
1574 */
1575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001576copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001577{
1578 qf_info_T *qi;
1579 int idx;
1580 int i;
1581
1582 /*
1583 * When copying from a location list window, copy the referenced
1584 * location list. For other windows, copy the location list for
1585 * that window.
1586 */
1587 if (IS_LL_WINDOW(from))
1588 qi = from->w_llist_ref;
1589 else
1590 qi = from->w_llist;
1591
1592 if (qi == NULL) /* no location list to copy */
1593 return;
1594
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001595 /* allocate a new location list */
1596 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001597 return;
1598
1599 to->w_llist->qf_listcount = qi->qf_listcount;
1600
1601 /* Copy the location lists one at a time */
1602 for (idx = 0; idx < qi->qf_listcount; idx++)
1603 {
1604 qf_list_T *from_qfl;
1605 qf_list_T *to_qfl;
1606
1607 to->w_llist->qf_curlist = idx;
1608
1609 from_qfl = &qi->qf_lists[idx];
1610 to_qfl = &to->w_llist->qf_lists[idx];
1611
1612 /* Some of the fields are populated by qf_add_entry() */
1613 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1614 to_qfl->qf_count = 0;
1615 to_qfl->qf_index = 0;
1616 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001617 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001618 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001619 if (from_qfl->qf_title != NULL)
1620 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1621 else
1622 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001623 if (from_qfl->qf_ctx != NULL)
1624 {
1625 to_qfl->qf_ctx = alloc_tv();
1626 if (to_qfl->qf_ctx != NULL)
1627 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1628 }
1629 else
1630 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631
1632 if (from_qfl->qf_count)
1633 {
1634 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001635 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001636
1637 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001638 for (i = 0, from_qfp = from_qfl->qf_start;
1639 i < from_qfl->qf_count && from_qfp != NULL;
1640 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001641 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001642 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001643 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 NULL,
1645 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001646 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001647 from_qfp->qf_text,
1648 from_qfp->qf_lnum,
1649 from_qfp->qf_col,
1650 from_qfp->qf_viscol,
1651 from_qfp->qf_pattern,
1652 from_qfp->qf_nr,
1653 0,
1654 from_qfp->qf_valid) == FAIL)
1655 {
1656 qf_free_all(to);
1657 return;
1658 }
1659 /*
1660 * qf_add_entry() will not set the qf_num field, as the
1661 * directory and file names are not supplied. So the qf_fnum
1662 * field is copied here.
1663 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001664 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001665 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1666 prevp->qf_type = from_qfp->qf_type; /* error type */
1667 if (from_qfl->qf_ptr == from_qfp)
1668 to_qfl->qf_ptr = prevp; /* current location */
1669 }
1670 }
1671
1672 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1673
1674 /* When no valid entries are present in the list, qf_ptr points to
1675 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001676 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001677 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001678 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001679 to_qfl->qf_index = 1;
1680 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001681 }
1682
1683 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1684}
1685
1686/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001687 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001688 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 */
1690 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001691qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
Bram Moolenaar82404332016-07-10 17:00:38 +02001693 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001694 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001695 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001696
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (fname == NULL || *fname == NUL) /* no file name */
1698 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
Bram Moolenaare60acc12011-05-10 16:41:25 +02001700#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001701 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001702#endif
1703#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001704 if (directory != NULL)
1705 slash_adjust(directory);
1706 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001707#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001708 if (directory != NULL && !vim_isAbsName(fname)
1709 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1710 {
1711 /*
1712 * Here we check if the file really exists.
1713 * This should normally be true, but if make works without
1714 * "leaving directory"-messages we might have missed a
1715 * directory change.
1716 */
1717 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001720 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001721 if (directory)
1722 ptr = concat_fnames(directory, fname, TRUE);
1723 else
1724 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001726 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001727 bufname = ptr;
1728 }
1729 else
1730 bufname = fname;
1731
1732 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001733 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001734 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001735 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001736 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001738 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001739 {
1740 vim_free(qf_last_bufname);
1741 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1742 if (bufname == ptr)
1743 qf_last_bufname = bufname;
1744 else
1745 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001746 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001747 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001748 if (buf == NULL)
1749 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001750
Bram Moolenaarc1542742016-07-20 21:44:37 +02001751 buf->b_has_qf_entry =
1752 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001753 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754}
1755
1756/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001757 * Push dirbuf onto the directory stack and return pointer to actual dir or
1758 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 */
1760 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001761qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
1763 struct dir_stack_T *ds_new;
1764 struct dir_stack_T *ds_ptr;
1765
1766 /* allocate new stack element and hook it in */
1767 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1768 if (ds_new == NULL)
1769 return NULL;
1770
1771 ds_new->next = *stackptr;
1772 *stackptr = ds_new;
1773
1774 /* store directory on the stack */
1775 if (vim_isAbsName(dirbuf)
1776 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001777 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 (*stackptr)->dirname = vim_strsave(dirbuf);
1779 else
1780 {
1781 /* Okay we don't have an absolute path.
1782 * dirbuf must be a subdir of one of the directories on the stack.
1783 * Let's search...
1784 */
1785 ds_new = (*stackptr)->next;
1786 (*stackptr)->dirname = NULL;
1787 while (ds_new)
1788 {
1789 vim_free((*stackptr)->dirname);
1790 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1791 TRUE);
1792 if (mch_isdir((*stackptr)->dirname) == TRUE)
1793 break;
1794
1795 ds_new = ds_new->next;
1796 }
1797
1798 /* clean up all dirs we already left */
1799 while ((*stackptr)->next != ds_new)
1800 {
1801 ds_ptr = (*stackptr)->next;
1802 (*stackptr)->next = (*stackptr)->next->next;
1803 vim_free(ds_ptr->dirname);
1804 vim_free(ds_ptr);
1805 }
1806
1807 /* Nothing found -> it must be on top level */
1808 if (ds_new == NULL)
1809 {
1810 vim_free((*stackptr)->dirname);
1811 (*stackptr)->dirname = vim_strsave(dirbuf);
1812 }
1813 }
1814
1815 if ((*stackptr)->dirname != NULL)
1816 return (*stackptr)->dirname;
1817 else
1818 {
1819 ds_ptr = *stackptr;
1820 *stackptr = (*stackptr)->next;
1821 vim_free(ds_ptr);
1822 return NULL;
1823 }
1824}
1825
1826
1827/*
1828 * pop dirbuf from the directory stack and return previous directory or NULL if
1829 * stack is empty
1830 */
1831 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001832qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833{
1834 struct dir_stack_T *ds_ptr;
1835
1836 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1837 * What to do if it isn't? */
1838
1839 /* pop top element and free it */
1840 if (*stackptr != NULL)
1841 {
1842 ds_ptr = *stackptr;
1843 *stackptr = (*stackptr)->next;
1844 vim_free(ds_ptr->dirname);
1845 vim_free(ds_ptr);
1846 }
1847
1848 /* return NEW top element as current dir or NULL if stack is empty*/
1849 return *stackptr ? (*stackptr)->dirname : NULL;
1850}
1851
1852/*
1853 * clean up directory stack
1854 */
1855 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001856qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 struct dir_stack_T *ds_ptr;
1859
1860 while ((ds_ptr = *stackptr) != NULL)
1861 {
1862 *stackptr = (*stackptr)->next;
1863 vim_free(ds_ptr->dirname);
1864 vim_free(ds_ptr);
1865 }
1866}
1867
1868/*
1869 * Check in which directory of the directory stack the given file can be
1870 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001871 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 * Cleans up intermediate directory entries.
1873 *
1874 * TODO: How to solve the following problem?
1875 * If we have the this directory tree:
1876 * ./
1877 * ./aa
1878 * ./aa/bb
1879 * ./bb
1880 * ./bb/x.c
1881 * and make says:
1882 * making all in aa
1883 * making all in bb
1884 * x.c:9: Error
1885 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1886 * qf_guess_filepath will return NULL.
1887 */
1888 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001889qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 struct dir_stack_T *ds_ptr;
1892 struct dir_stack_T *ds_tmp;
1893 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001894 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001897 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 return NULL;
1899
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001900 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 fullname = NULL;
1902 while (ds_ptr)
1903 {
1904 vim_free(fullname);
1905 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1906
1907 /* If concat_fnames failed, just go on. The worst thing that can happen
1908 * is that we delete the entire stack.
1909 */
1910 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1911 break;
1912
1913 ds_ptr = ds_ptr->next;
1914 }
1915
1916 vim_free(fullname);
1917
1918 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001919 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001921 ds_tmp = qfl->qf_dir_stack->next;
1922 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 vim_free(ds_tmp->dirname);
1924 vim_free(ds_tmp);
1925 }
1926
1927 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928}
1929
1930/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001931 * When loading a file from the quickfix, the auto commands may modify it.
1932 * This may invalidate the current quickfix entry. This function checks
1933 * whether a entry is still present in the quickfix.
1934 * Similar to location list.
1935 */
1936 static int
1937is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1938{
1939 qf_list_T *qfl;
1940 qfline_T *qfp;
1941 int i;
1942
1943 qfl = &qi->qf_lists[qi->qf_curlist];
1944
1945 /* Search for the entry in the current list */
1946 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1947 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001948 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001949 break;
1950
1951 if (i == qfl->qf_count) /* Entry is not found */
1952 return FALSE;
1953
1954 return TRUE;
1955}
1956
1957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 * jump to a quickfix line
1959 * if dir == FORWARD go "errornr" valid entries forward
1960 * if dir == BACKWARD go "errornr" valid entries backward
1961 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1962 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1963 * else if "errornr" is zero, redisplay the same line
1964 * else go to entry "errornr"
1965 */
1966 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001967qf_jump(
1968 qf_info_T *qi,
1969 int dir,
1970 int errornr,
1971 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001974 qfline_T *qf_ptr;
1975 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 int qf_index;
1977 int old_qf_fnum;
1978 int old_qf_index;
1979 int prev_index;
1980 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1981 char_u *err = e_no_more_items;
1982 linenr_T i;
1983 buf_T *old_curbuf;
1984 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 colnr_T screen_col;
1986 colnr_T char_col;
1987 char_u *line;
1988#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001989 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001990 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 int opened_window = FALSE;
1992 win_T *win;
1993 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001994 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001996 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 int print_message = TRUE;
1998 int len;
1999#ifdef FEAT_FOLDING
2000 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2001#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002002 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002003 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002005 if (qi == NULL)
2006 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002007
2008 if (qi->qf_curlist >= qi->qf_listcount
2009 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
2011 EMSG(_(e_quickfix));
2012 return;
2013 }
2014
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002015 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 old_qf_index = qf_index;
2019 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
2020 {
2021 while (errornr--)
2022 {
2023 old_qf_ptr = qf_ptr;
2024 prev_index = qf_index;
2025 old_qf_fnum = qf_ptr->qf_fnum;
2026 do
2027 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002028 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 || qf_ptr->qf_next == NULL)
2030 {
2031 qf_ptr = old_qf_ptr;
2032 qf_index = prev_index;
2033 if (err != NULL)
2034 {
2035 EMSG(_(err));
2036 goto theend;
2037 }
2038 errornr = 0;
2039 break;
2040 }
2041 ++qf_index;
2042 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002043 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2044 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2046 err = NULL;
2047 }
2048 }
2049 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
2050 {
2051 while (errornr--)
2052 {
2053 old_qf_ptr = qf_ptr;
2054 prev_index = qf_index;
2055 old_qf_fnum = qf_ptr->qf_fnum;
2056 do
2057 {
2058 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
2059 {
2060 qf_ptr = old_qf_ptr;
2061 qf_index = prev_index;
2062 if (err != NULL)
2063 {
2064 EMSG(_(err));
2065 goto theend;
2066 }
2067 errornr = 0;
2068 break;
2069 }
2070 --qf_index;
2071 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002072 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2073 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2075 err = NULL;
2076 }
2077 }
2078 else if (errornr != 0) /* go to specified number */
2079 {
2080 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2081 {
2082 --qf_index;
2083 qf_ptr = qf_ptr->qf_prev;
2084 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002085 while (errornr > qf_index && qf_index <
2086 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 && qf_ptr->qf_next != NULL)
2088 {
2089 ++qf_index;
2090 qf_ptr = qf_ptr->qf_next;
2091 }
2092 }
2093
2094#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002095 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2096 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 /* No need to print the error message if it's visible in the error
2098 * window */
2099 print_message = FALSE;
2100
2101 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002102 * For ":helpgrep" find a help window or open one.
2103 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002104 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002105 {
2106 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002107
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002108 if (cmdmod.tab != 0)
2109 wp = NULL;
2110 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002111 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002112 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2113 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002114 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2115 win_enter(wp, TRUE);
2116 else
2117 {
2118 /*
2119 * Split off help window; put it at far top if no position
2120 * specified, the current window is vertically split and narrow.
2121 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002122 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002123 if (cmdmod.split == 0 && curwin->w_width != Columns
2124 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002125 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002126 if (qi != &ql_info)
2127 flags |= WSP_NEWLOC; /* don't copy the location list */
2128
2129 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002130 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002131 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002132
2133 if (curwin->w_height < p_hh)
2134 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002135
2136 if (qi != &ql_info) /* not a quickfix list */
2137 {
2138 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002139 curwin->w_llist = qi;
2140 qi->qf_refcount++;
2141 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002142 }
2143
2144 if (!p_im)
2145 restart_edit = 0; /* don't want insert mode in help file */
2146 }
2147
2148 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 * If currently in the quickfix window, find another window to show the
2150 * file in.
2151 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002152 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002154 win_T *usable_win_ptr = NULL;
2155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 /*
2157 * If there is no file specified, we don't know where to go.
2158 * But do advance, otherwise ":cn" gets stuck.
2159 */
2160 if (qf_ptr->qf_fnum == 0)
2161 goto theend;
2162
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002163 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002164
2165 ll_ref = curwin->w_llist_ref;
2166 if (ll_ref != NULL)
2167 {
2168 /* Find a window using the same location list that is not a
2169 * quickfix window. */
2170 FOR_ALL_WINDOWS(usable_win_ptr)
2171 if (usable_win_ptr->w_llist == ll_ref
2172 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002173 {
2174 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002175 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002176 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002177 }
2178
2179 if (!usable_win)
2180 {
2181 /* Locate a window showing a normal buffer */
2182 FOR_ALL_WINDOWS(win)
2183 if (win->w_buffer->b_p_bt[0] == NUL)
2184 {
2185 usable_win = 1;
2186 break;
2187 }
2188 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002189
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002191 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002192 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002194 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002195 {
2196 tabpage_T *tp;
2197 win_T *wp;
2198
2199 FOR_ALL_TAB_WINDOWS(tp, wp)
2200 {
2201 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2202 {
2203 goto_tabpage_win(tp, wp);
2204 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002205 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002206 }
2207 }
2208 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002209win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002210
2211 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002212 * If there is only one window and it is the quickfix window, create a
2213 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002214 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002215 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002217 flags = WSP_ABOVE;
2218 if (ll_ref != NULL)
2219 flags |= WSP_NEWLOC;
2220 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 goto failed; /* not enough room for window */
2222 opened_window = TRUE; /* close it when fail */
2223 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002224 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002225 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002226 if (ll_ref != NULL)
2227 {
2228 /* The new window should use the location list from the
2229 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002230 curwin->w_llist = ll_ref;
2231 ll_ref->qf_refcount++;
2232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
2234 else
2235 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002236 if (curwin->w_llist_ref != NULL)
2237 {
2238 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002239 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002240 if (win == NULL)
2241 {
2242 /* Find the window showing the selected file */
2243 FOR_ALL_WINDOWS(win)
2244 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2245 break;
2246 if (win == NULL)
2247 {
2248 /* Find a previous usable window */
2249 win = curwin;
2250 do
2251 {
2252 if (win->w_buffer->b_p_bt[0] == NUL)
2253 break;
2254 if (win->w_prev == NULL)
2255 win = lastwin; /* wrap around the top */
2256 else
2257 win = win->w_prev; /* go to previous window */
2258 } while (win != curwin);
2259 }
2260 }
2261 win_goto(win);
2262
2263 /* If the location list for the window is not set, then set it
2264 * to the location list from the location window */
2265 if (win->w_llist == NULL)
2266 {
2267 win->w_llist = ll_ref;
2268 ll_ref->qf_refcount++;
2269 }
2270 }
2271 else
2272 {
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 /*
2275 * Try to find a window that shows the right buffer.
2276 * Default to the window just above the quickfix buffer.
2277 */
2278 win = curwin;
2279 altwin = NULL;
2280 for (;;)
2281 {
2282 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2283 break;
2284 if (win->w_prev == NULL)
2285 win = lastwin; /* wrap around the top */
2286 else
2287 win = win->w_prev; /* go to previous window */
2288
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002289 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 /* Didn't find it, go to the window before the quickfix
2292 * window. */
2293 if (altwin != NULL)
2294 win = altwin;
2295 else if (curwin->w_prev != NULL)
2296 win = curwin->w_prev;
2297 else
2298 win = curwin->w_next;
2299 break;
2300 }
2301
2302 /* Remember a usable window. */
2303 if (altwin == NULL && !win->w_p_pvw
2304 && win->w_buffer->b_p_bt[0] == NUL)
2305 altwin = win;
2306 }
2307
2308 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
2311 }
2312#endif
2313
2314 /*
2315 * If there is a file name,
2316 * read the wanted file if needed, and check autowrite etc.
2317 */
2318 old_curbuf = curbuf;
2319 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002320
2321 if (qf_ptr->qf_fnum != 0)
2322 {
2323 if (qf_ptr->qf_type == 1)
2324 {
2325 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2326 * set b_p_ro flag). */
2327 if (!can_abandon(curbuf, forceit))
2328 {
2329 EMSG(_(e_nowrtmsg));
2330 ok = FALSE;
2331 }
2332 else
2333 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002334 ECMD_HIDE + ECMD_SET_HELP,
2335 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002336 }
2337 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002338 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002339 int old_qf_curlist = qi->qf_curlist;
2340 int is_abort = FALSE;
2341
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002342 ok = buflist_getfile(qf_ptr->qf_fnum,
2343 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002344 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002345 {
2346 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002347 is_abort = TRUE;
2348 opened_window = FALSE;
2349 }
2350 else if (old_qf_curlist != qi->qf_curlist
2351 || !is_qf_entry_present(qi, qf_ptr))
2352 {
2353 if (qi == &ql_info)
2354 EMSG(_("E925: Current quickfix was changed"));
2355 else
2356 EMSG(_("E926: Current location list was changed"));
2357 is_abort = TRUE;
2358 }
2359
2360 if (is_abort)
2361 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002362 ok = FALSE;
2363 qi = NULL;
2364 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002365 }
2366 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002367 }
2368
2369 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
2371 /* When not switched to another buffer, still need to set pc mark */
2372 if (curbuf == old_curbuf)
2373 setpcmark();
2374
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002375 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002377 /*
2378 * Go to line with error, unless qf_lnum is 0.
2379 */
2380 i = qf_ptr->qf_lnum;
2381 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002383 if (i > curbuf->b_ml.ml_line_count)
2384 i = curbuf->b_ml.ml_line_count;
2385 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002387 if (qf_ptr->qf_col > 0)
2388 {
2389 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002390#ifdef FEAT_VIRTUALEDIT
2391 curwin->w_cursor.coladd = 0;
2392#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002393 if (qf_ptr->qf_viscol == TRUE)
2394 {
2395 /*
2396 * Check each character from the beginning of the error
2397 * line up to the error column. For each tab character
2398 * found, reduce the error column value by the length of
2399 * a tab character.
2400 */
2401 line = ml_get_curline();
2402 screen_col = 0;
2403 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2404 {
2405 if (*line == NUL)
2406 break;
2407 if (*line++ == '\t')
2408 {
2409 curwin->w_cursor.col -= 7 - (screen_col % 8);
2410 screen_col += 8 - (screen_col % 8);
2411 }
2412 else
2413 ++screen_col;
2414 }
2415 }
2416 check_cursor();
2417 }
2418 else
2419 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 }
2421 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002422 {
2423 pos_T save_cursor;
2424
2425 /* Move the cursor to the first line in the buffer */
2426 save_cursor = curwin->w_cursor;
2427 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002428 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002429 SEARCH_KEEP, NULL, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002430 curwin->w_cursor = save_cursor;
2431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
2433#ifdef FEAT_FOLDING
2434 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2435 foldOpenCursor();
2436#endif
2437 if (print_message)
2438 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002439 /* Update the screen before showing the message, unless the screen
2440 * scrolled up. */
2441 if (!msg_scrolled)
2442 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002444 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2446 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2447 /* Add the message, skipping leading whitespace and newlines. */
2448 len = (int)STRLEN(IObuff);
2449 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2450
2451 /* Output the message. Overwrite to avoid scrolling when the 'O'
2452 * flag is present in 'shortmess'; But when not jumping, print the
2453 * whole message. */
2454 i = msg_scroll;
2455 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2456 msg_scroll = TRUE;
2457 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2458 msg_scroll = FALSE;
2459 msg_attr_keep(IObuff, 0, TRUE);
2460 msg_scroll = i;
2461 }
2462 }
2463 else
2464 {
2465#ifdef FEAT_WINDOWS
2466 if (opened_window)
2467 win_close(curwin, TRUE); /* Close opened window */
2468#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002469 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {
2471 /*
2472 * Couldn't open file, so put index back where it was. This could
2473 * happen if the file was readonly and we changed something.
2474 */
2475#ifdef FEAT_WINDOWS
2476failed:
2477#endif
2478 qf_ptr = old_qf_ptr;
2479 qf_index = old_qf_index;
2480 }
2481 }
2482theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002483 if (qi != NULL)
2484 {
2485 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2486 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488#ifdef FEAT_WINDOWS
2489 if (p_swb != old_swb && opened_window)
2490 {
2491 /* Restore old 'switchbuf' value, but not when an autocommand or
2492 * modeline has changed the value. */
2493 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002496 swb_flags = old_swb_flags;
2497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 else
2499 free_string_option(old_swb);
2500 }
2501#endif
2502}
2503
2504/*
2505 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002506 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 */
2508 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002509qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002511 buf_T *buf;
2512 char_u *fname;
2513 qfline_T *qfp;
2514 int i;
2515 int idx1 = 1;
2516 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002517 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002518 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002519 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002521 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002523 if (eap->cmdidx == CMD_llist)
2524 {
2525 qi = GET_LOC_LIST(curwin);
2526 if (qi == NULL)
2527 {
2528 EMSG(_(e_loclist));
2529 return;
2530 }
2531 }
2532
2533 if (qi->qf_curlist >= qi->qf_listcount
2534 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 EMSG(_(e_quickfix));
2537 return;
2538 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002539 if (*arg == '+')
2540 {
2541 ++arg;
2542 plus = TRUE;
2543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2545 {
2546 EMSG(_(e_trailing));
2547 return;
2548 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002549 if (plus)
2550 {
2551 i = qi->qf_lists[qi->qf_curlist].qf_index;
2552 idx2 = i + idx1;
2553 idx1 = i;
2554 }
2555 else
2556 {
2557 i = qi->qf_lists[qi->qf_curlist].qf_count;
2558 if (idx1 < 0)
2559 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2560 if (idx2 < 0)
2561 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002564 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002566 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2567 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 {
2569 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2570 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002571 msg_putchar('\n');
2572 if (got_int)
2573 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002574
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002575 fname = NULL;
2576 if (qfp->qf_fnum != 0
2577 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2578 {
2579 fname = buf->b_fname;
2580 if (qfp->qf_type == 1) /* :helpgrep */
2581 fname = gettail(fname);
2582 }
2583 if (fname == NULL)
2584 sprintf((char *)IObuff, "%2d", i);
2585 else
2586 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2587 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002588 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar21020352017-06-13 17:21:04 +02002589 ? HL_ATTR(HLF_QFL) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002590 if (qfp->qf_lnum == 0)
2591 IObuff[0] = NUL;
2592 else if (qfp->qf_col == 0)
2593 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2594 else
2595 sprintf((char *)IObuff, ":%ld col %d",
2596 qfp->qf_lnum, qfp->qf_col);
2597 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2598 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002599 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002600 if (qfp->qf_pattern != NULL)
2601 {
2602 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2603 STRCAT(IObuff, ":");
2604 msg_puts(IObuff);
2605 }
2606 msg_puts((char_u *)" ");
2607
2608 /* Remove newlines and leading whitespace from the text. For an
2609 * unrecognized line keep the indent, the compiler may mark a word
2610 * with ^^^^. */
2611 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2613 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002614 msg_prt_line(IObuff, FALSE);
2615 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002617
2618 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002619 if (qfp == NULL)
2620 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002621 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 ui_breakcheck();
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624}
2625
2626/*
2627 * Remove newlines and leading whitespace from an error message.
2628 * Put the result in "buf[bufsize]".
2629 */
2630 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002631qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
2633 int i;
2634 char_u *p = text;
2635
2636 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2637 {
2638 if (*p == '\n')
2639 {
2640 buf[i] = ' ';
2641 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002642 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 break;
2644 }
2645 else
2646 buf[i] = *p++;
2647 }
2648 buf[i] = NUL;
2649}
2650
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002651 static void
2652qf_msg(qf_info_T *qi, int which, char *lead)
2653{
2654 char *title = (char *)qi->qf_lists[which].qf_title;
2655 int count = qi->qf_lists[which].qf_count;
2656 char_u buf[IOSIZE];
2657
2658 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2659 lead,
2660 which + 1,
2661 qi->qf_listcount,
2662 count);
2663
2664 if (title != NULL)
2665 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002666 size_t len = STRLEN(buf);
2667
2668 if (len < 34)
2669 {
2670 vim_memset(buf + len, ' ', 34 - len);
2671 buf[34] = NUL;
2672 }
2673 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002674 }
2675 trunc_string(buf, buf, Columns - 1, IOSIZE);
2676 msg(buf);
2677}
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679/*
2680 * ":colder [count]": Up in the quickfix stack.
2681 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002682 * ":lolder [count]": Up in the location list stack.
2683 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 */
2685 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002686qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002688 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 int count;
2690
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002691 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2692 {
2693 qi = GET_LOC_LIST(curwin);
2694 if (qi == NULL)
2695 {
2696 EMSG(_(e_loclist));
2697 return;
2698 }
2699 }
2700
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (eap->addr_count != 0)
2702 count = eap->line2;
2703 else
2704 count = 1;
2705 while (count--)
2706 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002707 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002709 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
2711 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002712 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002714 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716 else
2717 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002718 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002721 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002723 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002726 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002728 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729#endif
2730}
2731
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002732 void
2733qf_history(exarg_T *eap)
2734{
2735 qf_info_T *qi = &ql_info;
2736 int i;
2737
2738 if (eap->cmdidx == CMD_lhistory)
2739 qi = GET_LOC_LIST(curwin);
2740 if (qi == NULL || (qi->qf_listcount == 0
2741 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2742 MSG(_("No entries"));
2743 else
2744 for (i = 0; i < qi->qf_listcount; ++i)
2745 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2746}
2747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002749 * Free all the entries in the error list "idx". Note that other information
2750 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 */
2752 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002753qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002755 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002756 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002757 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002758 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002760 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002762 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002763 qfpnext = qfp->qf_next;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002764 if (qfl->qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002765 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002766 vim_free(qfp->qf_text);
2767 stop = (qfp == qfpnext);
2768 vim_free(qfp->qf_pattern);
2769 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002770 if (stop)
2771 /* Somehow qf_count may have an incorrect value, set it to 1
2772 * to avoid crashing when it's wrong.
2773 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002774 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002775 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002776 qfl->qf_start = qfpnext;
2777 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002779
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002780 qfl->qf_index = 0;
2781 qfl->qf_start = NULL;
2782 qfl->qf_last = NULL;
2783 qfl->qf_ptr = NULL;
2784 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002785
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002786 qf_clean_dir_stack(&qfl->qf_dir_stack);
2787 qfl->qf_directory = NULL;
2788 qf_clean_dir_stack(&qfl->qf_file_stack);
2789 qfl->qf_currfile = NULL;
2790 qfl->qf_multiline = FALSE;
2791 qfl->qf_multiignore = FALSE;
2792 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793}
2794
2795/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002796 * Free error list "idx". Frees all the entries in the quickfix list,
2797 * associated context information and the title.
2798 */
2799 static void
2800qf_free(qf_info_T *qi, int idx)
2801{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002802 qf_list_T *qfl = &qi->qf_lists[idx];
2803
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002804 qf_free_items(qi, idx);
2805
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002806 vim_free(qfl->qf_title);
2807 qfl->qf_title = NULL;
2808 free_tv(qfl->qf_ctx);
2809 qfl->qf_ctx = NULL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002810}
2811
2812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 * qf_mark_adjust: adjust marks
2814 */
2815 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002816qf_mark_adjust(
2817 win_T *wp,
2818 linenr_T line1,
2819 linenr_T line2,
2820 long amount,
2821 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002823 int i;
2824 qfline_T *qfp;
2825 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002826 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002827 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002828 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
Bram Moolenaarc1542742016-07-20 21:44:37 +02002830 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002831 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002832 if (wp != NULL)
2833 {
2834 if (wp->w_llist == NULL)
2835 return;
2836 qi = wp->w_llist;
2837 }
2838
2839 for (idx = 0; idx < qi->qf_listcount; ++idx)
2840 if (qi->qf_lists[idx].qf_count)
2841 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002842 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2843 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 if (qfp->qf_fnum == curbuf->b_fnum)
2845 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002846 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2848 {
2849 if (amount == MAXLNUM)
2850 qfp->qf_cleared = TRUE;
2851 else
2852 qfp->qf_lnum += amount;
2853 }
2854 else if (amount_after && qfp->qf_lnum > line2)
2855 qfp->qf_lnum += amount_after;
2856 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002857
2858 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002859 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860}
2861
2862/*
2863 * Make a nice message out of the error character and the error number:
2864 * char number message
2865 * e or E 0 " error"
2866 * w or W 0 " warning"
2867 * i or I 0 " info"
2868 * 0 0 ""
2869 * other 0 " c"
2870 * e or E n " error n"
2871 * w or W n " warning n"
2872 * i or I n " info n"
2873 * 0 n " error n"
2874 * other n " c n"
2875 * 1 x "" :helpgrep
2876 */
2877 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002878qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 static char_u buf[20];
2881 static char_u cc[3];
2882 char_u *p;
2883
2884 if (c == 'W' || c == 'w')
2885 p = (char_u *)" warning";
2886 else if (c == 'I' || c == 'i')
2887 p = (char_u *)" info";
2888 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2889 p = (char_u *)" error";
2890 else if (c == 0 || c == 1)
2891 p = (char_u *)"";
2892 else
2893 {
2894 cc[0] = ' ';
2895 cc[1] = c;
2896 cc[2] = NUL;
2897 p = cc;
2898 }
2899
2900 if (nr <= 0)
2901 return p;
2902
2903 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2904 return buf;
2905}
2906
2907#if defined(FEAT_WINDOWS) || defined(PROTO)
2908/*
2909 * ":cwindow": open the quickfix window if we have errors to display,
2910 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 * ":lwindow": open the location list window if we have locations to display,
2912 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 */
2914 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002915ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002917 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 win_T *win;
2919
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002920 if (eap->cmdidx == CMD_lwindow)
2921 {
2922 qi = GET_LOC_LIST(curwin);
2923 if (qi == NULL)
2924 return;
2925 }
2926
2927 /* Look for an existing quickfix window. */
2928 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930 /*
2931 * If a quickfix window is open but we have no errors to display,
2932 * close the window. If a quickfix window is not open, then open
2933 * it if we have errors; otherwise, leave it closed.
2934 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002935 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002936 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002937 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
2939 if (win != NULL)
2940 ex_cclose(eap);
2941 }
2942 else if (win == NULL)
2943 ex_copen(eap);
2944}
2945
2946/*
2947 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002948 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002951ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002953 win_T *win = NULL;
2954 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002956 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2957 {
2958 qi = GET_LOC_LIST(curwin);
2959 if (qi == NULL)
2960 return;
2961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002963 /* Find existing quickfix window and close it. */
2964 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 if (win != NULL)
2966 win_close(win, FALSE);
2967}
2968
2969/*
2970 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002971 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 */
2973 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002974ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002976 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002979 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002980 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002981 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002983 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2984 {
2985 qi = GET_LOC_LIST(curwin);
2986 if (qi == NULL)
2987 {
2988 EMSG(_(e_loclist));
2989 return;
2990 }
2991 }
2992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 if (eap->addr_count != 0)
2994 height = eap->line2;
2995 else
2996 height = QF_WINHEIGHT;
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#ifdef FEAT_GUI
3000 need_mouse_correct = TRUE;
3001#endif
3002
3003 /*
3004 * Find existing quickfix window, or open a new one.
3005 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003006 win = qf_find_win(qi);
3007
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003008 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003011 if (eap->addr_count != 0)
3012 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003013 if (cmdmod.split & WSP_VERT)
3014 {
3015 if (height != W_WIDTH(win))
3016 win_setwidth(height);
3017 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003018 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003019 win_setheight(height);
3020 }
3021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 else
3023 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00003024 qf_buf = qf_find_buf(qi);
3025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 /* The current window becomes the previous window afterwards. */
3027 win = curwin;
3028
Bram Moolenaar77642c02012-11-20 17:55:10 +01003029 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3030 && cmdmod.split == 0)
3031 /* Create the new window at the very bottom, except when
3032 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003033 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003034 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003036 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003038 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003040 /*
3041 * For the location list window, create a reference to the
3042 * location list from the window 'win'.
3043 */
3044 curwin->w_llist_ref = win->w_llist;
3045 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003047
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003048 if (oldwin != curwin)
3049 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003050 if (qf_buf != NULL)
3051 /* Use the existing quickfix buffer */
3052 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003053 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003054 else
3055 {
3056 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003057 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003058 /* switch off 'swapfile' */
3059 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3060 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003061 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003062 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003063 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003064#ifdef FEAT_DIFF
3065 curwin->w_p_diff = FALSE;
3066#endif
3067#ifdef FEAT_FOLDING
3068 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3069 OPT_LOCAL);
3070#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003073 /* Only set the height when still in the same tab page and there is no
3074 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003075 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 win_setheight(height);
3077 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3078 if (win_valid(win))
3079 prevwin = win;
3080 }
3081
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003082 qf_set_title_var(qi);
3083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 /*
3085 * Fill the buffer with the quickfix list.
3086 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003087 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003089 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 curwin->w_cursor.col = 0;
3091 check_cursor();
3092 update_topline(); /* scroll to show the line */
3093}
3094
3095/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003096 * Move the cursor in the quickfix window to "lnum".
3097 */
3098 static void
3099qf_win_goto(win_T *win, linenr_T lnum)
3100{
3101 win_T *old_curwin = curwin;
3102
3103 curwin = win;
3104 curbuf = win->w_buffer;
3105 curwin->w_cursor.lnum = lnum;
3106 curwin->w_cursor.col = 0;
3107#ifdef FEAT_VIRTUALEDIT
3108 curwin->w_cursor.coladd = 0;
3109#endif
3110 curwin->w_curswant = 0;
3111 update_topline(); /* scroll to show the line */
3112 redraw_later(VALID);
3113 curwin->w_redr_status = TRUE; /* update ruler */
3114 curwin = old_curwin;
3115 curbuf = curwin->w_buffer;
3116}
3117
3118/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003119 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003120 */
3121 void
3122ex_cbottom(exarg_T *eap UNUSED)
3123{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003124 qf_info_T *qi = &ql_info;
3125 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003126
Bram Moolenaar537ef082016-07-09 17:56:19 +02003127 if (eap->cmdidx == CMD_lbottom)
3128 {
3129 qi = GET_LOC_LIST(curwin);
3130 if (qi == NULL)
3131 {
3132 EMSG(_(e_loclist));
3133 return;
3134 }
3135 }
3136
3137 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003138 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3139 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3140}
3141
3142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 * Return the number of the current entry (line number in the quickfix
3144 * window).
3145 */
3146 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003147qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003149 qf_info_T *qi = &ql_info;
3150
3151 if (IS_LL_WINDOW(wp))
3152 /* In the location list window, use the referenced location list */
3153 qi = wp->w_llist_ref;
3154
3155 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
3158/*
3159 * Update the cursor position in the quickfix window to the current error.
3160 * Return TRUE if there is a quickfix window.
3161 */
3162 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003163qf_win_pos_update(
3164 qf_info_T *qi,
3165 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
3167 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003168 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 /*
3171 * Put the cursor on the current error in the quickfix window, so that
3172 * it's viewable.
3173 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003174 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 if (win != NULL
3176 && qf_index <= win->w_buffer->b_ml.ml_line_count
3177 && old_qf_index != qf_index)
3178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 if (qf_index > old_qf_index)
3180 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003181 win->w_redraw_top = old_qf_index;
3182 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 }
3184 else
3185 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003186 win->w_redraw_top = qf_index;
3187 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003189 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 }
3191 return win != NULL;
3192}
3193
3194/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003195 * Check whether the given window is displaying the specified quickfix/location
3196 * list buffer
3197 */
3198 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003199is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003200{
3201 /*
3202 * A window displaying the quickfix buffer will have the w_llist_ref field
3203 * set to NULL.
3204 * A window displaying a location list buffer will have the w_llist_ref
3205 * pointing to the location list.
3206 */
3207 if (bt_quickfix(win->w_buffer))
3208 if ((qi == &ql_info && win->w_llist_ref == NULL)
3209 || (qi != &ql_info && win->w_llist_ref == qi))
3210 return TRUE;
3211
3212 return FALSE;
3213}
3214
3215/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003216 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003217 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003218 */
3219 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003220qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003221{
3222 win_T *win;
3223
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003224 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003225 if (is_qf_win(win, qi))
3226 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003227
3228 return win;
3229}
3230
3231/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003232 * Find a quickfix buffer.
3233 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 */
3235 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003236qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003238 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003239 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
Bram Moolenaar9c102382006-05-03 21:26:49 +00003241 FOR_ALL_TAB_WINDOWS(tp, win)
3242 if (is_qf_win(win, qi))
3243 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003244
Bram Moolenaar9c102382006-05-03 21:26:49 +00003245 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246}
3247
3248/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003249 * Update the w:quickfix_title variable in the quickfix/location list window
3250 */
3251 static void
3252qf_update_win_titlevar(qf_info_T *qi)
3253{
3254 win_T *win;
3255 win_T *curwin_save;
3256
3257 if ((win = qf_find_win(qi)) != NULL)
3258 {
3259 curwin_save = curwin;
3260 curwin = win;
3261 qf_set_title_var(qi);
3262 curwin = curwin_save;
3263 }
3264}
3265
3266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 * Find the quickfix buffer. If it exists, update the contents.
3268 */
3269 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003270qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003273 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003277 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 if (buf != NULL)
3279 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003280 linenr_T old_line_count = buf->b_ml.ml_line_count;
3281
3282 if (old_last == NULL)
3283 /* set curwin/curbuf to buf and save a few things */
3284 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Bram Moolenaard823fa92016-08-12 16:29:27 +02003286 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003287
Bram Moolenaar864293a2016-06-02 13:40:04 +02003288 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003289
Bram Moolenaar864293a2016-06-02 13:40:04 +02003290 if (old_last == NULL)
3291 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003292 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003293
3294 /* restore curwin/curbuf and a few other things */
3295 aucmd_restbuf(&aco);
3296 }
3297
3298 /* Only redraw when added lines are visible. This avoids flickering
3299 * when the added lines are not visible. */
3300 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3301 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303}
3304
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003305/*
3306 * Set "w:quickfix_title" if "qi" has a title.
3307 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003308 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003309qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003310{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003311 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3312 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003313 qi->qf_lists[qi->qf_curlist].qf_title);
3314}
3315
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316/*
3317 * Fill current buffer with quickfix errors, replacing any previous contents.
3318 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003319 * If "old_last" is not NULL append the items after this one.
3320 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3321 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 */
3323 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003324qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003326 linenr_T lnum;
3327 qfline_T *qfp;
3328 buf_T *errbuf;
3329 int len;
3330 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar864293a2016-06-02 13:40:04 +02003332 if (old_last == NULL)
3333 {
3334 if (buf != curbuf)
3335 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003336 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003337 return;
3338 }
3339
3340 /* delete all existing lines */
3341 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3342 (void)ml_delete((linenr_T)1, FALSE);
3343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003346 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
3348 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003349 if (old_last == NULL)
3350 {
3351 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3352 lnum = 0;
3353 }
3354 else
3355 {
3356 qfp = old_last->qf_next;
3357 lnum = buf->b_ml.ml_line_count;
3358 }
3359 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
3361 if (qfp->qf_fnum != 0
3362 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3363 && errbuf->b_fname != NULL)
3364 {
3365 if (qfp->qf_type == 1) /* :helpgrep */
3366 STRCPY(IObuff, gettail(errbuf->b_fname));
3367 else
3368 STRCPY(IObuff, errbuf->b_fname);
3369 len = (int)STRLEN(IObuff);
3370 }
3371 else
3372 len = 0;
3373 IObuff[len++] = '|';
3374
3375 if (qfp->qf_lnum > 0)
3376 {
3377 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3378 len += (int)STRLEN(IObuff + len);
3379
3380 if (qfp->qf_col > 0)
3381 {
3382 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3383 len += (int)STRLEN(IObuff + len);
3384 }
3385
3386 sprintf((char *)IObuff + len, "%s",
3387 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3388 len += (int)STRLEN(IObuff + len);
3389 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003390 else if (qfp->qf_pattern != NULL)
3391 {
3392 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3393 len += (int)STRLEN(IObuff + len);
3394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 IObuff[len++] = '|';
3396 IObuff[len++] = ' ';
3397
3398 /* Remove newlines and leading whitespace from the text.
3399 * For an unrecognized line keep the indent, the compiler may
3400 * mark a word with ^^^^. */
3401 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3402 IObuff + len, IOSIZE - len);
3403
Bram Moolenaar864293a2016-06-02 13:40:04 +02003404 if (ml_append_buf(buf, lnum, IObuff,
3405 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003407 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003409 if (qfp == NULL)
3410 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003412
3413 if (old_last == NULL)
3414 /* Delete the empty line which is now at the end */
3415 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417
3418 /* correct cursor position */
3419 check_lnums(TRUE);
3420
Bram Moolenaar864293a2016-06-02 13:40:04 +02003421 if (old_last == NULL)
3422 {
3423 /* Set the 'filetype' to "qf" each time after filling the buffer.
3424 * This resembles reading a file into a buffer, it's more logical when
3425 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003426#ifdef FEAT_AUTOCMD
3427 ++curbuf_lock;
3428#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003429 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3430 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003433 keep_filetype = TRUE; /* don't detect 'filetype' */
3434 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003436 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003438 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003439 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003441 /* make sure it will be redrawn */
3442 redraw_curbuf_later(NOT_VALID);
3443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445 /* Restore KeyTyped, setting 'filetype' may reset it. */
3446 KeyTyped = old_KeyTyped;
3447}
3448
3449#endif /* FEAT_WINDOWS */
3450
3451/*
3452 * Return TRUE if "buf" is the quickfix buffer.
3453 */
3454 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003455bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003457 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458}
3459
3460/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003461 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3462 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 */
3464 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003465bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003467 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3468 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469}
3470
3471/*
3472 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3473 */
3474 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003475bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003477 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478}
3479
3480 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003481bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
3483 if (bt_dontwrite(buf))
3484 {
3485 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3486 return TRUE;
3487 }
3488 return FALSE;
3489}
3490
3491/*
3492 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3493 * and 'bufhidden'.
3494 */
3495 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003496buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3499 switch (buf->b_p_bh[0])
3500 {
3501 case 'u': /* "unload" */
3502 case 'w': /* "wipe" */
3503 case 'd': return FALSE; /* "delete" */
3504 case 'h': return TRUE; /* "hide" */
3505 }
3506 return (p_hid || cmdmod.hide);
3507}
3508
3509/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003510 * Return TRUE when using ":vimgrep" for ":grep".
3511 */
3512 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003513grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003514{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003515 return ((cmdidx == CMD_grep
3516 || cmdidx == CMD_lgrep
3517 || cmdidx == CMD_grepadd
3518 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003519 && STRCMP("internal",
3520 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3521}
3522
3523/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003524 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 */
3526 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003527ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003529 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003531 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003533 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003534 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003535 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003536#ifdef FEAT_AUTOCMD
3537 char_u *au_name = NULL;
3538
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003539 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3540 if (grep_internal(eap->cmdidx))
3541 {
3542 ex_vimgrep(eap);
3543 return;
3544 }
3545
Bram Moolenaar7c626922005-02-07 22:01:03 +00003546 switch (eap->cmdidx)
3547 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003548 case CMD_make: au_name = (char_u *)"make"; break;
3549 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3550 case CMD_grep: au_name = (char_u *)"grep"; break;
3551 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3552 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3553 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003554 default: break;
3555 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003556 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3557 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003558 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003559# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003560 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003561 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003562# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003563 }
3564#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003565#ifdef FEAT_MBYTE
3566 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
Bram Moolenaara6557602006-02-04 22:43:20 +00003569 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3570 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003571 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003572
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003574 fname = get_mef_name();
3575 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003577 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
3579 /*
3580 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3581 */
3582 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3583 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003584 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 cmd = alloc(len);
3586 if (cmd == NULL)
3587 return;
3588 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3589 (char *)p_shq);
3590 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003591 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 /*
3593 * Output a newline if there's something else than the :make command that
3594 * was typed (in which case the cursor is in column 0).
3595 */
3596 if (msg_col == 0)
3597 msg_didout = FALSE;
3598 msg_start();
3599 MSG_PUTS(":!");
3600 msg_outtrans(cmd); /* show what we are doing */
3601
3602 /* let the shell know if we are redirecting output or not */
3603 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3604
3605#ifdef AMIGA
3606 out_flush();
3607 /* read window status report and redraw before message */
3608 (void)char_avail();
3609#endif
3610
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003611 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003612 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3613 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003614 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003615 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003616 if (wp != NULL)
3617 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003618#ifdef FEAT_AUTOCMD
3619 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003620 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003621 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3622 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003623 if (qi->qf_curlist < qi->qf_listcount)
3624 res = qi->qf_lists[qi->qf_curlist].qf_count;
3625 else
3626 res = 0;
3627 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003628#endif
3629 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003630 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
Bram Moolenaar7c626922005-02-07 22:01:03 +00003632 mch_remove(fname);
3633 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 vim_free(cmd);
3635}
3636
3637/*
3638 * Return the name for the errorfile, in allocated memory.
3639 * Find a new unique name when 'makeef' contains "##".
3640 * Returns NULL for error.
3641 */
3642 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003643get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 char_u *p;
3646 char_u *name;
3647 static int start = -1;
3648 static int off = 0;
3649#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003650 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651#endif
3652
3653 if (*p_mef == NUL)
3654 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003655 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 if (name == NULL)
3657 EMSG(_(e_notmp));
3658 return name;
3659 }
3660
3661 for (p = p_mef; *p; ++p)
3662 if (p[0] == '#' && p[1] == '#')
3663 break;
3664
3665 if (*p == NUL)
3666 return vim_strsave(p_mef);
3667
3668 /* Keep trying until the name doesn't exist yet. */
3669 for (;;)
3670 {
3671 if (start == -1)
3672 start = mch_get_pid();
3673 else
3674 off += 19;
3675
3676 name = alloc((unsigned)STRLEN(p_mef) + 30);
3677 if (name == NULL)
3678 break;
3679 STRCPY(name, p_mef);
3680 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3681 STRCAT(name, p + 2);
3682 if (mch_getperm(name) < 0
3683#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003684 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 && mch_lstat((char *)name, &sb) < 0
3686#endif
3687 )
3688 break;
3689 vim_free(name);
3690 }
3691 return name;
3692}
3693
3694/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003695 * Returns the number of valid entries in the current quickfix/location list.
3696 */
3697 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003698qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003699{
3700 qf_info_T *qi = &ql_info;
3701 qfline_T *qfp;
3702 int i, sz = 0;
3703 int prev_fnum = 0;
3704
3705 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3706 {
3707 /* Location list */
3708 qi = GET_LOC_LIST(curwin);
3709 if (qi == NULL)
3710 return 0;
3711 }
3712
3713 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003714 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003715 ++i, qfp = qfp->qf_next)
3716 {
3717 if (qfp->qf_valid)
3718 {
3719 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3720 sz++; /* Count all valid entries */
3721 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3722 {
3723 /* Count the number of files */
3724 sz++;
3725 prev_fnum = qfp->qf_fnum;
3726 }
3727 }
3728 }
3729
3730 return sz;
3731}
3732
3733/*
3734 * Returns the current index of the quickfix/location list.
3735 * Returns 0 if there is an error.
3736 */
3737 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003738qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003739{
3740 qf_info_T *qi = &ql_info;
3741
3742 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3743 {
3744 /* Location list */
3745 qi = GET_LOC_LIST(curwin);
3746 if (qi == NULL)
3747 return 0;
3748 }
3749
3750 return qi->qf_lists[qi->qf_curlist].qf_index;
3751}
3752
3753/*
3754 * Returns the current index in the quickfix/location list (counting only valid
3755 * entries). If no valid entries are in the list, then returns 1.
3756 */
3757 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003758qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003759{
3760 qf_info_T *qi = &ql_info;
3761 qf_list_T *qfl;
3762 qfline_T *qfp;
3763 int i, eidx = 0;
3764 int prev_fnum = 0;
3765
3766 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3767 {
3768 /* Location list */
3769 qi = GET_LOC_LIST(curwin);
3770 if (qi == NULL)
3771 return 1;
3772 }
3773
3774 qfl = &qi->qf_lists[qi->qf_curlist];
3775 qfp = qfl->qf_start;
3776
3777 /* check if the list has valid errors */
3778 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3779 return 1;
3780
3781 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3782 {
3783 if (qfp->qf_valid)
3784 {
3785 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3786 {
3787 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3788 {
3789 /* Count the number of files */
3790 eidx++;
3791 prev_fnum = qfp->qf_fnum;
3792 }
3793 }
3794 else
3795 eidx++;
3796 }
3797 }
3798
3799 return eidx ? eidx : 1;
3800}
3801
3802/*
3803 * Get the 'n'th valid error entry in the quickfix or location list.
3804 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3805 * For :cdo and :ldo returns the 'n'th valid error entry.
3806 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3807 */
3808 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003809qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003810{
3811 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3812 qfline_T *qfp = qfl->qf_start;
3813 int i, eidx;
3814 int prev_fnum = 0;
3815
3816 /* check if the list has valid errors */
3817 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3818 return 1;
3819
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003820 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003821 i++, qfp = qfp->qf_next)
3822 {
3823 if (qfp->qf_valid)
3824 {
3825 if (fdo)
3826 {
3827 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3828 {
3829 /* Count the number of files */
3830 eidx++;
3831 prev_fnum = qfp->qf_fnum;
3832 }
3833 }
3834 else
3835 eidx++;
3836 }
3837
3838 if (eidx == n)
3839 break;
3840 }
3841
3842 if (i <= qfl->qf_count)
3843 return i;
3844 else
3845 return 1;
3846}
3847
3848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003850 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003851 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 */
3853 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003854ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003856 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003857 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003858
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003859 if (eap->cmdidx == CMD_ll
3860 || eap->cmdidx == CMD_lrewind
3861 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003862 || eap->cmdidx == CMD_llast
3863 || eap->cmdidx == CMD_ldo
3864 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003865 {
3866 qi = GET_LOC_LIST(curwin);
3867 if (qi == NULL)
3868 {
3869 EMSG(_(e_loclist));
3870 return;
3871 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003872 }
3873
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003874 if (eap->addr_count > 0)
3875 errornr = (int)eap->line2;
3876 else
3877 {
3878 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3879 errornr = 0;
3880 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3881 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3882 errornr = 1;
3883 else
3884 errornr = 32767;
3885 }
3886
3887 /* For cdo and ldo commands, jump to the nth valid error.
3888 * For cfdo and lfdo commands, jump to the nth valid file entry.
3889 */
3890 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3891 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3892 errornr = qf_get_nth_valid_entry(qi,
3893 eap->addr_count > 0 ? (int)eap->line1 : 1,
3894 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3895
3896 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897}
3898
3899/*
3900 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003901 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003902 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 */
3904 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003905ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003907 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003908 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003909
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003910 if (eap->cmdidx == CMD_lnext
3911 || eap->cmdidx == CMD_lNext
3912 || eap->cmdidx == CMD_lprevious
3913 || eap->cmdidx == CMD_lnfile
3914 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003915 || eap->cmdidx == CMD_lpfile
3916 || eap->cmdidx == CMD_ldo
3917 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003918 {
3919 qi = GET_LOC_LIST(curwin);
3920 if (qi == NULL)
3921 {
3922 EMSG(_(e_loclist));
3923 return;
3924 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003925 }
3926
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003927 if (eap->addr_count > 0 &&
3928 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3929 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3930 errornr = (int)eap->line2;
3931 else
3932 errornr = 1;
3933
3934 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3935 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003937 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3938 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003940 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3941 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 ? BACKWARD_FILE
3943 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003944 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945}
3946
3947/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003948 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003949 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 */
3951 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003952ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003954 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003955 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003956 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003957#ifdef FEAT_AUTOCMD
3958 char_u *au_name = NULL;
3959#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003960
3961 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003962 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003963 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003964
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003965#ifdef FEAT_AUTOCMD
3966 switch (eap->cmdidx)
3967 {
3968 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3969 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3970 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3971 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3972 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3973 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3974 default: break;
3975 }
3976 if (au_name != NULL)
3977 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3978#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003979#ifdef FEAT_MBYTE
3980 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3981#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003982#ifdef FEAT_BROWSE
3983 if (cmdmod.browse)
3984 {
3985 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3986 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3987 if (browse_file == NULL)
3988 return;
3989 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3990 vim_free(browse_file);
3991 }
3992 else
3993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003995 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003996
3997 /*
3998 * This function is used by the :cfile, :cgetfile and :caddfile
3999 * commands.
4000 * :cfile always creates a new quickfix list and jumps to the
4001 * first error.
4002 * :cgetfile creates a new quickfix list but doesn't jump to the
4003 * first error.
4004 * :caddfile adds to an existing quickfix list. If there is no
4005 * quickfix list then a new list is created.
4006 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004007 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004008 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004009 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004010 && (eap->cmdidx == CMD_cfile
4011 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004012 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004013#ifdef FEAT_AUTOCMD
4014 if (au_name != NULL)
4015 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4016#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004017 if (wp != NULL)
4018 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004019 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004020 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004021
4022 else
4023 {
4024#ifdef FEAT_AUTOCMD
4025 if (au_name != NULL)
4026 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4027#endif
4028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029}
4030
4031/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004032 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004033 * ":vimgrepadd {pattern} file(s)"
4034 * ":lvimgrep {pattern} file(s)"
4035 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004036 */
4037 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004038ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004039{
Bram Moolenaar81695252004-12-29 20:58:21 +00004040 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004041 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004042 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004043 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004044 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004045 char_u *s;
4046 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004047 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004048 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004049#ifdef FEAT_AUTOCMD
4050 qfline_T *cur_qf_start;
4051#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004052 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004053 buf_T *buf;
4054 int duplicate_name = FALSE;
4055 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004056 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004057 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004058 buf_T *first_match_buf = NULL;
4059 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004060 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004061#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4062 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004063#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004064 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004065 int flags = 0;
4066 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004067 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004068 char_u *dirname_start = NULL;
4069 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004070 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004071#ifdef FEAT_AUTOCMD
4072 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004073
4074 switch (eap->cmdidx)
4075 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004076 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4077 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4078 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004079 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004080 case CMD_grep: au_name = (char_u *)"grep"; break;
4081 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4082 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4083 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004084 default: break;
4085 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004086 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4087 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004088 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004089# ifdef FEAT_EVAL
4090 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004091 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004092# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004093 }
4094#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004095
Bram Moolenaar754b5602006-02-09 23:53:20 +00004096 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004097 || eap->cmdidx == CMD_lvimgrep
4098 || eap->cmdidx == CMD_lgrepadd
4099 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004100 {
4101 qi = ll_get_or_alloc_list(curwin);
4102 if (qi == NULL)
4103 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004104 }
4105
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004106 if (eap->addr_count > 0)
4107 tomatch = eap->line2;
4108 else
4109 tomatch = MAXLNUM;
4110
Bram Moolenaar81695252004-12-29 20:58:21 +00004111 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004112 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004113 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004114 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004115 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004116 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004117 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004118 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004119 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004120
4121 if (s != NULL && *s == NUL)
4122 {
4123 /* Pattern is empty, use last search pattern. */
4124 if (last_search_pat() == NULL)
4125 {
4126 EMSG(_(e_noprevre));
4127 goto theend;
4128 }
4129 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4130 }
4131 else
4132 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4133
Bram Moolenaar86b68352004-12-27 21:59:20 +00004134 if (regmatch.regprog == NULL)
4135 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004136 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004137 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004138
4139 p = skipwhite(p);
4140 if (*p == NUL)
4141 {
4142 EMSG(_("E683: File name missing or invalid pattern"));
4143 goto theend;
4144 }
4145
Bram Moolenaar754b5602006-02-09 23:53:20 +00004146 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004147 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004148 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004149 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004150 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004151
4152 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004153 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004154 goto theend;
4155 if (fcount == 0)
4156 {
4157 EMSG(_(e_nomatch));
4158 goto theend;
4159 }
4160
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004161 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4162 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004163 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004164 {
4165 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004166 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004167 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004168
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004169 /* Remember the current directory, because a BufRead autocommand that does
4170 * ":lcd %:p:h" changes the meaning of short path names. */
4171 mch_dirname(dirname_start, MAXPATHL);
4172
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004173#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004174 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004175 * changing the current quickfix list. */
4176 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4177#endif
4178
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004179 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004180 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004181 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004182 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004183 if (time(NULL) > seconds)
4184 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004185 /* Display the file name every second or so, show the user we are
4186 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004187 seconds = time(NULL);
4188 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004189 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004190 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004191 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004192 else
4193 {
4194 msg_outtrans(p);
4195 vim_free(p);
4196 }
4197 msg_clr_eos();
4198 msg_didout = FALSE; /* overwrite this message */
4199 msg_nowait = TRUE; /* don't wait for this message */
4200 msg_col = 0;
4201 out_flush();
4202 }
4203
Bram Moolenaar81695252004-12-29 20:58:21 +00004204 buf = buflist_findname_exp(fnames[fi]);
4205 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4206 {
4207 /* Remember that a buffer with this name already exists. */
4208 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004209 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004210 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004211
4212#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4213 /* Don't do Filetype autocommands to avoid loading syntax and
4214 * indent scripts, a great speed improvement. */
4215 save_ei = au_event_disable(",Filetype");
4216#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004217 /* Don't use modelines here, it's useless. */
4218 save_mls = p_mls;
4219 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004220
4221 /* Load file into a buffer, so that 'fileencoding' is detected,
4222 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004223 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004224
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004225 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004226#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4227 au_event_restore(save_ei);
4228#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004229 }
4230 else
4231 /* Use existing, loaded buffer. */
4232 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004233
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004234#ifdef FEAT_AUTOCMD
4235 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4236 {
4237 int idx;
4238
4239 /* Autocommands changed the quickfix list. Find the one we were
4240 * using and restore it. */
4241 for (idx = 0; idx < LISTCOUNT; ++idx)
4242 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4243 {
4244 qi->qf_curlist = idx;
4245 break;
4246 }
4247 if (idx == LISTCOUNT)
4248 {
4249 /* List cannot be found, create a new one. */
4250 qf_new_list(qi, *eap->cmdlinep);
4251 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4252 }
4253 }
4254#endif
4255
Bram Moolenaar81695252004-12-29 20:58:21 +00004256 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004257 {
4258 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004259 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004260 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004261 else
4262 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004263 /* Try for a match in all lines of the buffer.
4264 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004265 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004266 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4267 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004268 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004269 col = 0;
4270 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004271 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004272 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004273 /* Pass the buffer number so that it gets used even for a
4274 * dummy buffer, unless duplicate_name is set, then the
4275 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004276 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004277 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004278 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004279 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004280 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004281 ml_get_buf(buf,
4282 regmatch.startpos[0].lnum + lnum, FALSE),
4283 regmatch.startpos[0].lnum + lnum,
4284 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004285 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004286 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004287 0, /* nr */
4288 0, /* type */
4289 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004290 ) == FAIL)
4291 {
4292 got_int = TRUE;
4293 break;
4294 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004295 found_match = TRUE;
4296 if (--tomatch == 0)
4297 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004298 if ((flags & VGR_GLOBAL) == 0
4299 || regmatch.endpos[0].lnum > 0)
4300 break;
4301 col = regmatch.endpos[0].col
4302 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004303 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004304 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004305 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004306 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004307 if (got_int)
4308 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004309 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004310#ifdef FEAT_AUTOCMD
4311 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4312#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004313
4314 if (using_dummy)
4315 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004316 if (found_match && first_match_buf == NULL)
4317 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004318 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004319 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004320 /* Never keep a dummy buffer if there is another buffer
4321 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004322 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004323 buf = NULL;
4324 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004325 else if (!cmdmod.hide
4326 || buf->b_p_bh[0] == 'u' /* "unload" */
4327 || buf->b_p_bh[0] == 'w' /* "wipe" */
4328 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004329 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004330 /* When no match was found we don't need to remember the
4331 * buffer, wipe it out. If there was a match and it
4332 * wasn't the first one or we won't jump there: only
4333 * unload the buffer.
4334 * Ignore 'hidden' here, because it may lead to having too
4335 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004336 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004337 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004338 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004339 buf = NULL;
4340 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004341 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004342 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004343 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004344 /* Keeping the buffer, remove the dummy flag. */
4345 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004346 buf = NULL;
4347 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004348 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004349
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004350 if (buf != NULL)
4351 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004352 /* Keeping the buffer, remove the dummy flag. */
4353 buf->b_flags &= ~BF_DUMMY;
4354
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004355 /* If the buffer is still loaded we need to use the
4356 * directory we jumped to below. */
4357 if (buf == first_match_buf
4358 && target_dir == NULL
4359 && STRCMP(dirname_start, dirname_now) != 0)
4360 target_dir = vim_strsave(dirname_now);
4361
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004362 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004363 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004364 * need to be done (again). But not the window-local
4365 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004366 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004367#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004368 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4369 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004370#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004371 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004372 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004373 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004374 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004375 }
4376 }
4377
4378 FreeWild(fcount, fnames);
4379
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004380 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4381 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4382 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004383
4384#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004385 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004386#endif
4387
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004388#ifdef FEAT_AUTOCMD
4389 if (au_name != NULL)
4390 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4391 curbuf->b_fname, TRUE, curbuf);
4392#endif
4393
Bram Moolenaar86b68352004-12-27 21:59:20 +00004394 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004395 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004396 {
4397 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004398 {
4399 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004400 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004401 if (buf != curbuf)
4402 /* If we jumped to another buffer redrawing will already be
4403 * taken care of. */
4404 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004405
4406 /* Jump to the directory used after loading the buffer. */
4407 if (curbuf == first_match_buf && target_dir != NULL)
4408 {
4409 exarg_T ea;
4410
4411 ea.arg = target_dir;
4412 ea.cmdidx = CMD_lcd;
4413 ex_cd(&ea);
4414 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004415 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004416 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004417 else
4418 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004419
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004420 /* If we loaded a dummy buffer into the current window, the autocommands
4421 * may have messed up things, need to redraw and recompute folds. */
4422 if (redraw_for_dummy)
4423 {
4424#ifdef FEAT_FOLDING
4425 foldUpdateAll(curwin);
4426#else
4427 redraw_later(NOT_VALID);
4428#endif
4429 }
4430
Bram Moolenaar86b68352004-12-27 21:59:20 +00004431theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004432 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004433 vim_free(dirname_now);
4434 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004435 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004436 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004437}
4438
4439/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004440 * Restore current working directory to "dirname_start" if they differ, taking
4441 * into account whether it is set locally or globally.
4442 */
4443 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004444restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004445{
4446 char_u *dirname_now = alloc(MAXPATHL);
4447
4448 if (NULL != dirname_now)
4449 {
4450 mch_dirname(dirname_now, MAXPATHL);
4451 if (STRCMP(dirname_start, dirname_now) != 0)
4452 {
4453 /* If the directory has changed, change it back by building up an
4454 * appropriate ex command and executing it. */
4455 exarg_T ea;
4456
4457 ea.arg = dirname_start;
4458 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4459 ex_cd(&ea);
4460 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004461 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004462 }
4463}
4464
4465/*
4466 * Load file "fname" into a dummy buffer and return the buffer pointer,
4467 * placing the directory resulting from the buffer load into the
4468 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4469 * prior to calling this function. Restores directory to "dirname_start" prior
4470 * to returning, if autocmds or the 'autochdir' option have changed it.
4471 *
4472 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4473 * or wipe_dummy_buffer() later!
4474 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004475 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004476 */
4477 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004478load_dummy_buffer(
4479 char_u *fname,
4480 char_u *dirname_start, /* in: old directory */
4481 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004482{
4483 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004484 bufref_T newbufref;
4485 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004486 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004487 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004488
4489 /* Allocate a buffer without putting it in the buffer list. */
4490 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4491 if (newbuf == NULL)
4492 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004493 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004494
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004495 /* Init the options. */
4496 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4497
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004498 /* need to open the memfile before putting the buffer in a window */
4499 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004500 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004501 /* set curwin/curbuf to buf and save a few things */
4502 aucmd_prepbuf(&aco, newbuf);
4503
4504 /* Need to set the filename for autocommands. */
4505 (void)setfname(curbuf, fname, NULL, FALSE);
4506
Bram Moolenaar81695252004-12-29 20:58:21 +00004507 /* Create swap file now to avoid the ATTENTION message. */
4508 check_need_swap(TRUE);
4509
4510 /* Remove the "dummy" flag, otherwise autocommands may not
4511 * work. */
4512 curbuf->b_flags &= ~BF_DUMMY;
4513
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004514 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004515 if (readfile(fname, NULL,
4516 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4517 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004518 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004519 && !(curbuf->b_flags & BF_NEW))
4520 {
4521 failed = FALSE;
4522 if (curbuf != newbuf)
4523 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004524 /* Bloody autocommands changed the buffer! Can happen when
4525 * using netrw and editing a remote file. Use the current
4526 * buffer instead, delete the dummy one after restoring the
4527 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004528 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004529 newbuf = curbuf;
4530 }
4531 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004532
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004533 /* restore curwin/curbuf and a few other things */
4534 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004535 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4536 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004537
4538 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4539 * skip it. */
4540 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004541 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004542
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004543 /*
4544 * When autocommands/'autochdir' option changed directory: go back.
4545 * Let the caller know what the resulting dir was first, in case it is
4546 * important.
4547 */
4548 mch_dirname(resulting_dir, MAXPATHL);
4549 restore_start_dir(dirname_start);
4550
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004551 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004552 return NULL;
4553 if (failed)
4554 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004555 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004556 return NULL;
4557 }
4558 return newbuf;
4559}
4560
4561/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004562 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4563 * directory to "dirname_start" prior to returning, if autocmds or the
4564 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004565 */
4566 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004567wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004568{
4569 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004570 {
4571#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4572 cleanup_T cs;
4573
4574 /* Reset the error/interrupt/exception state here so that aborting()
4575 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4576 * work when got_int is set. */
4577 enter_cleanup(&cs);
4578#endif
4579
Bram Moolenaar81695252004-12-29 20:58:21 +00004580 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004581
4582#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4583 /* Restore the error/interrupt/exception state if not discarded by a
4584 * new aborting error, interrupt, or uncaught exception. */
4585 leave_cleanup(&cs);
4586#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004587 /* When autocommands/'autochdir' option changed directory: go back. */
4588 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004589 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004590}
4591
4592/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004593 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4594 * directory to "dirname_start" prior to returning, if autocmds or the
4595 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004596 */
4597 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004598unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004599{
4600 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004601 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004602 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004603
4604 /* When autocommands/'autochdir' option changed directory: go back. */
4605 restore_start_dir(dirname_start);
4606 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004607}
4608
Bram Moolenaar05159a02005-02-26 23:04:13 +00004609#if defined(FEAT_EVAL) || defined(PROTO)
4610/*
4611 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004612 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004613 */
4614 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004615get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004616{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004617 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004618 dict_T *dict;
4619 char_u buf[2];
4620 qfline_T *qfp;
4621 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004622 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004623
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004624 if (wp != NULL)
4625 {
4626 qi = GET_LOC_LIST(wp);
4627 if (qi == NULL)
4628 return FAIL;
4629 }
4630
Bram Moolenaard823fa92016-08-12 16:29:27 +02004631 if (qf_idx == -1)
4632 qf_idx = qi->qf_curlist;
4633
4634 if (qf_idx >= qi->qf_listcount
4635 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004636 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004637
Bram Moolenaard823fa92016-08-12 16:29:27 +02004638 qfp = qi->qf_lists[qf_idx].qf_start;
4639 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004640 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004641 /* Handle entries with a non-existing buffer number. */
4642 bufnum = qfp->qf_fnum;
4643 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4644 bufnum = 0;
4645
Bram Moolenaar05159a02005-02-26 23:04:13 +00004646 if ((dict = dict_alloc()) == NULL)
4647 return FAIL;
4648 if (list_append_dict(list, dict) == FAIL)
4649 return FAIL;
4650
4651 buf[0] = qfp->qf_type;
4652 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004653 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004654 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4655 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4656 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4657 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004658 || dict_add_nr_str(dict, "pattern", 0L,
4659 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4660 || dict_add_nr_str(dict, "text", 0L,
4661 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004662 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4663 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4664 return FAIL;
4665
4666 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004667 if (qfp == NULL)
4668 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004669 }
4670 return OK;
4671}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004672
4673/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004674 * Flags used by getqflist()/getloclist() to determine which fields to return.
4675 */
4676enum {
4677 QF_GETLIST_NONE = 0x0,
4678 QF_GETLIST_TITLE = 0x1,
4679 QF_GETLIST_ITEMS = 0x2,
4680 QF_GETLIST_NR = 0x4,
4681 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004682 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004683 QF_GETLIST_ALL = 0xFF
4684};
4685
4686/*
4687 * Return quickfix/location list details (title) as a
4688 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4689 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004690 */
4691 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004692get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4693{
4694 qf_info_T *qi = &ql_info;
4695 int status = OK;
4696 int qf_idx;
4697 dictitem_T *di;
4698 int flags = QF_GETLIST_NONE;
4699
4700 if (wp != NULL)
4701 {
4702 qi = GET_LOC_LIST(wp);
4703 if (qi == NULL)
Bram Moolenaar875feea2017-06-11 16:07:51 +02004704 {
4705 /* If querying for the size of the location list, return 0 */
4706 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL) &&
4707 (di->di_tv.v_type == VAR_STRING) &&
4708 (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4709 return dict_add_nr_str(retdict, "nr", 0, NULL);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004710 return FAIL;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004711 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004712 }
4713
4714 qf_idx = qi->qf_curlist; /* default is the current list */
4715 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4716 {
4717 /* Use the specified quickfix/location list */
4718 if (di->di_tv.v_type == VAR_NUMBER)
4719 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004720 /* for zero use the current list */
4721 if (di->di_tv.vval.v_number != 0)
4722 {
4723 qf_idx = di->di_tv.vval.v_number - 1;
4724 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4725 return FAIL;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004726 } else if (qi->qf_listcount == 0) /* stack is empty */
4727 return FAIL;
4728 flags |= QF_GETLIST_NR;
4729 } else if ((di->di_tv.v_type == VAR_STRING) &&
4730 (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4731 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004732 /* Get the last quickfix list number */
4733 if (qi->qf_listcount > 0)
4734 qf_idx = qi->qf_listcount - 1;
4735 else
4736 qf_idx = -1; /* Quickfix stack is empty */
Bram Moolenaard823fa92016-08-12 16:29:27 +02004737 flags |= QF_GETLIST_NR;
4738 }
4739 else
4740 return FAIL;
4741 }
4742
Bram Moolenaar875feea2017-06-11 16:07:51 +02004743 if (qf_idx != -1)
4744 {
4745 if (dict_find(what, (char_u *)"all", -1) != NULL)
4746 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004747
Bram Moolenaar875feea2017-06-11 16:07:51 +02004748 if (dict_find(what, (char_u *)"title", -1) != NULL)
4749 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004750
Bram Moolenaar875feea2017-06-11 16:07:51 +02004751 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4752 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004753
Bram Moolenaar875feea2017-06-11 16:07:51 +02004754 if (dict_find(what, (char_u *)"context", -1) != NULL)
4755 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004756
4757 if (dict_find(what, (char_u *)"items", -1) != NULL)
4758 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004759 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004760
Bram Moolenaard823fa92016-08-12 16:29:27 +02004761 if (flags & QF_GETLIST_TITLE)
4762 {
4763 char_u *t;
4764 t = qi->qf_lists[qf_idx].qf_title;
4765 if (t == NULL)
4766 t = (char_u *)"";
4767 status = dict_add_nr_str(retdict, "title", 0L, t);
4768 }
4769 if ((status == OK) && (flags & QF_GETLIST_NR))
4770 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4771 if ((status == OK) && (flags & QF_GETLIST_WINID))
4772 {
4773 win_T *win;
4774 win = qf_find_win(qi);
4775 if (win != NULL)
4776 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4777 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004778 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4779 {
4780 list_T *l = list_alloc();
4781 if (l != NULL)
4782 {
4783 (void)get_errorlist(wp, qf_idx, l);
4784 dict_add_list(retdict, "items", l);
4785 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004786 else
4787 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004788 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004789
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004790 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4791 {
4792 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4793 {
4794 di = dictitem_alloc((char_u *)"context");
4795 if (di != NULL)
4796 {
4797 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004798 status = dict_add(retdict, di);
4799 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004800 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004801 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004802 else
4803 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004804 }
4805 else
4806 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4807 }
4808
Bram Moolenaard823fa92016-08-12 16:29:27 +02004809 return status;
4810}
4811
4812/*
4813 * Add list of entries to quickfix/location list. Each list entry is
4814 * a dictionary with item information.
4815 */
4816 static int
4817qf_add_entries(
4818 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004819 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004820 list_T *list,
4821 char_u *title,
4822 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004823{
4824 listitem_T *li;
4825 dict_T *d;
4826 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004827 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004828 long lnum;
4829 int col, nr;
4830 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004831#ifdef FEAT_WINDOWS
4832 qfline_T *old_last = NULL;
4833#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004834 int valid, status;
4835 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004836 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004837
Bram Moolenaara3921f42017-06-04 15:30:34 +02004838 if (action == ' ' || qf_idx == qi->qf_listcount)
4839 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004840 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004841 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004842 qf_idx = qi->qf_curlist;
4843 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004844#ifdef FEAT_WINDOWS
Bram Moolenaara3921f42017-06-04 15:30:34 +02004845 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004846 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004847 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004848#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004849 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004850 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004851 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004852 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004853 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004854
4855 for (li = list->lv_first; li != NULL; li = li->li_next)
4856 {
4857 if (li->li_tv.v_type != VAR_DICT)
4858 continue; /* Skip non-dict items */
4859
4860 d = li->li_tv.vval.v_dict;
4861 if (d == NULL)
4862 continue;
4863
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004864 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004865 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4866 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4867 col = (int)get_dict_number(d, (char_u *)"col");
4868 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4869 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004870 type = get_dict_string(d, (char_u *)"type", TRUE);
4871 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4872 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004873 if (text == NULL)
4874 text = vim_strsave((char_u *)"");
4875
4876 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004877 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004878 valid = FALSE;
4879
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004880 /* Mark entries with non-existing buffer number as not valid. Give the
4881 * error message only once. */
4882 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4883 {
4884 if (!did_bufnr_emsg)
4885 {
4886 did_bufnr_emsg = TRUE;
4887 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4888 }
4889 valid = FALSE;
4890 bufnum = 0;
4891 }
4892
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02004893 /* If the 'valid' field is present it overrules the detected value. */
4894 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
4895 valid = (int)get_dict_number(d, (char_u *)"valid");
4896
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004897 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004898 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004899 NULL, /* dir */
4900 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004901 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004902 text,
4903 lnum,
4904 col,
4905 vcol, /* vis_col */
4906 pattern, /* search pattern */
4907 nr,
4908 type == NULL ? NUL : *type,
4909 valid);
4910
4911 vim_free(filename);
4912 vim_free(pattern);
4913 vim_free(text);
4914 vim_free(type);
4915
4916 if (status == FAIL)
4917 {
4918 retval = FAIL;
4919 break;
4920 }
4921 }
4922
Bram Moolenaara3921f42017-06-04 15:30:34 +02004923 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004924 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004925 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004926 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02004927 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004928 if (action != 'a')
4929 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02004930 qi->qf_lists[qf_idx].qf_ptr =
4931 qi->qf_lists[qf_idx].qf_start;
4932 if (qi->qf_lists[qf_idx].qf_count > 0)
4933 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004934 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004935
4936#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004937 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004938 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004939#endif
4940
4941 return retval;
4942}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004943
4944 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004945qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004946{
4947 dictitem_T *di;
4948 int retval = FAIL;
4949 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004950 int newlist = FALSE;
4951
4952 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4953 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004954
4955 qf_idx = qi->qf_curlist; /* default is the current list */
4956 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4957 {
4958 /* Use the specified quickfix/location list */
4959 if (di->di_tv.v_type == VAR_NUMBER)
4960 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004961 /* for zero use the current list */
4962 if (di->di_tv.vval.v_number != 0)
4963 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004964
4965 if ((action == ' ' || action == 'a') &&
4966 qf_idx == qi->qf_listcount)
4967 /*
4968 * When creating a new list, accept qf_idx pointing to the next
4969 * non-available list
4970 */
4971 newlist = TRUE;
4972 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004973 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004974 else
4975 newlist = FALSE; /* use the specified list */
Bram Moolenaar875feea2017-06-11 16:07:51 +02004976 } else if (di->di_tv.v_type == VAR_STRING &&
4977 STRCMP(di->di_tv.vval.v_string, "$") == 0 &&
4978 qi->qf_listcount > 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004979 {
Bram Moolenaar875feea2017-06-11 16:07:51 +02004980 qf_idx = qi->qf_listcount - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004981 newlist = FALSE;
4982 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004983 else
4984 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004985 }
4986
4987 if (newlist)
4988 {
4989 qf_new_list(qi, NULL);
4990 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004991 }
4992
4993 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4994 {
4995 if (di->di_tv.v_type == VAR_STRING)
4996 {
4997 vim_free(qi->qf_lists[qf_idx].qf_title);
4998 qi->qf_lists[qf_idx].qf_title =
4999 get_dict_string(what, (char_u *)"title", TRUE);
5000 if (qf_idx == qi->qf_curlist)
5001 qf_update_win_titlevar(qi);
5002 retval = OK;
5003 }
5004 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005005 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5006 {
5007 if (di->di_tv.v_type == VAR_LIST)
5008 {
5009 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5010
5011 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5012 title_save, action == ' ' ? 'a' : action);
5013 vim_free(title_save);
5014 }
5015 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005016
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005017 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5018 {
5019 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005020
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005021 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005022 ctx = alloc_tv();
5023 if (ctx != NULL)
5024 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005025 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005026 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005027 }
5028
Bram Moolenaard823fa92016-08-12 16:29:27 +02005029 return retval;
5030}
5031
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005032/*
5033 * Find the non-location list window with the specified location list.
5034 */
5035 static win_T *
5036find_win_with_ll(qf_info_T *qi)
5037{
5038 win_T *wp = NULL;
5039
5040 FOR_ALL_WINDOWS(wp)
5041 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5042 return wp;
5043
5044 return NULL;
5045}
5046
5047/*
5048 * Free the entire quickfix/location list stack.
5049 * If the quickfix/location list window is open, then clear it.
5050 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005051 static void
5052qf_free_stack(win_T *wp, qf_info_T *qi)
5053{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005054 win_T *qfwin = qf_find_win(qi);
5055 win_T *llwin = NULL;
5056 win_T *orig_wp = wp;
5057
5058 if (qfwin != NULL)
5059 {
5060 /* If the quickfix/location list window is open, then clear it */
5061 if (qi->qf_curlist < qi->qf_listcount)
5062 qf_free(qi, qi->qf_curlist);
5063 qf_update_buffer(qi, NULL);
5064 }
5065
5066 if (wp != NULL && IS_LL_WINDOW(wp))
5067 {
5068 /* If in the location list window, then use the non-location list
5069 * window with this location list (if present)
5070 */
5071 llwin = find_win_with_ll(qi);
5072 if (llwin != NULL)
5073 wp = llwin;
5074 }
5075
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005076 qf_free_all(wp);
5077 if (wp == NULL)
5078 {
5079 /* quickfix list */
5080 qi->qf_curlist = 0;
5081 qi->qf_listcount = 0;
5082 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005083 else if (IS_LL_WINDOW(orig_wp))
5084 {
5085 /* If the location list window is open, then create a new empty
5086 * location list */
5087 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005088
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005089 /* first free the list reference in the location list window */
5090 ll_free_all(&orig_wp->w_llist_ref);
5091
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005092 orig_wp->w_llist_ref = new_ll;
5093 if (llwin != NULL)
5094 {
5095 llwin->w_llist = new_ll;
5096 new_ll->qf_refcount++;
5097 }
5098 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005099}
5100
Bram Moolenaard823fa92016-08-12 16:29:27 +02005101/*
5102 * Populate the quickfix list with the items supplied in the list
5103 * of dictionaries. "title" will be copied to w:quickfix_title.
5104 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5105 */
5106 int
5107set_errorlist(
5108 win_T *wp,
5109 list_T *list,
5110 int action,
5111 char_u *title,
5112 dict_T *what)
5113{
5114 qf_info_T *qi = &ql_info;
5115 int retval = OK;
5116
5117 if (wp != NULL)
5118 {
5119 qi = ll_get_or_alloc_list(wp);
5120 if (qi == NULL)
5121 return FAIL;
5122 }
5123
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005124 if (action == 'f')
5125 {
5126 /* Free the entire quickfix or location list stack */
5127 qf_free_stack(wp, qi);
5128 }
5129 else if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005130 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005131 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005132 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005133
5134 return retval;
5135}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005136
5137 static int
5138mark_quickfix_ctx(qf_info_T *qi, int copyID)
5139{
5140 int i;
5141 int abort = FALSE;
5142 typval_T *ctx;
5143
5144 for (i = 0; i < LISTCOUNT && !abort; ++i)
5145 {
5146 ctx = qi->qf_lists[i].qf_ctx;
5147 if (ctx != NULL && ctx->v_type != VAR_NUMBER &&
5148 ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
5149 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5150 }
5151
5152 return abort;
5153}
5154
5155/*
5156 * Mark the context of the quickfix list and the location lists (if present) as
5157 * "in use". So that garabage collection doesn't free the context.
5158 */
5159 int
5160set_ref_in_quickfix(int copyID)
5161{
5162 int abort = FALSE;
5163 tabpage_T *tp;
5164 win_T *win;
5165
5166 abort = mark_quickfix_ctx(&ql_info, copyID);
5167 if (abort)
5168 return abort;
5169
5170 FOR_ALL_TAB_WINDOWS(tp, win)
5171 {
5172 if (win->w_llist != NULL)
5173 {
5174 abort = mark_quickfix_ctx(win->w_llist, copyID);
5175 if (abort)
5176 return abort;
5177 }
5178 }
5179
5180 return abort;
5181}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005182#endif
5183
Bram Moolenaar81695252004-12-29 20:58:21 +00005184/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005185 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005186 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005187 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005188 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005189 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005190 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005191 */
5192 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005193ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005194{
5195 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005196 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005197#ifdef FEAT_AUTOCMD
5198 char_u *au_name = NULL;
5199#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005200
Bram Moolenaardb552d602006-03-23 22:59:57 +00005201 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5202 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005203 {
5204 qi = ll_get_or_alloc_list(curwin);
5205 if (qi == NULL)
5206 return;
5207 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005208
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005209#ifdef FEAT_AUTOCMD
5210 switch (eap->cmdidx)
5211 {
5212 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5213 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5214 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5215 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5216 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5217 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5218 default: break;
5219 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005220 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5221 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005222 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005223# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005224 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005225 return;
5226# endif
5227 }
5228#endif
5229
Bram Moolenaar86b68352004-12-27 21:59:20 +00005230 if (*eap->arg == NUL)
5231 buf = curbuf;
5232 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5233 buf = buflist_findnr(atoi((char *)eap->arg));
5234 if (buf == NULL)
5235 EMSG(_(e_invarg));
5236 else if (buf->b_ml.ml_mfp == NULL)
5237 EMSG(_("E681: Buffer is not loaded"));
5238 else
5239 {
5240 if (eap->addr_count == 0)
5241 {
5242 eap->line1 = 1;
5243 eap->line2 = buf->b_ml.ml_line_count;
5244 }
5245 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5246 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5247 EMSG(_(e_invrange));
5248 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005249 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005250 char_u *qf_title = *eap->cmdlinep;
5251
5252 if (buf->b_sfname)
5253 {
5254 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5255 (char *)qf_title, (char *)buf->b_sfname);
5256 qf_title = IObuff;
5257 }
5258
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005259 if (qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005260 (eap->cmdidx != CMD_caddbuffer
5261 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005262 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005263 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005264 {
5265#ifdef FEAT_AUTOCMD
5266 if (au_name != NULL)
5267 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5268 curbuf->b_fname, TRUE, curbuf);
5269#endif
5270 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5271 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5272 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005273 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005274 }
5275}
5276
Bram Moolenaar1e015462005-09-25 22:16:38 +00005277#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005278/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005279 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5280 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005281 */
5282 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005283ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005284{
5285 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005286 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005287#ifdef FEAT_AUTOCMD
5288 char_u *au_name = NULL;
5289#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005290
Bram Moolenaardb552d602006-03-23 22:59:57 +00005291 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5292 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005293 {
5294 qi = ll_get_or_alloc_list(curwin);
5295 if (qi == NULL)
5296 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005297 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005298
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005299#ifdef FEAT_AUTOCMD
5300 switch (eap->cmdidx)
5301 {
5302 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5303 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5304 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5305 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5306 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5307 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5308 default: break;
5309 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005310 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5311 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005312 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005313# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005314 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005315 return;
5316# endif
5317 }
5318#endif
5319
Bram Moolenaar4770d092006-01-12 23:22:24 +00005320 /* Evaluate the expression. When the result is a string or a list we can
5321 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005322 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005323 if (tv != NULL)
5324 {
5325 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5326 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5327 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005328 if (qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005329 (eap->cmdidx != CMD_caddexpr
5330 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005331 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5332 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005333 {
5334#ifdef FEAT_AUTOCMD
5335 if (au_name != NULL)
5336 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5337 curbuf->b_fname, TRUE, curbuf);
5338#endif
5339 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5340 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5341 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005342 }
5343 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005344 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005345 free_tv(tv);
5346 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005347}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005348#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005349
5350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 * ":helpgrep {pattern}"
5352 */
5353 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005354ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356 regmatch_T regmatch;
5357 char_u *save_cpo;
5358 char_u *p;
5359 int fcount;
5360 char_u **fnames;
5361 FILE *fd;
5362 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005364#ifdef FEAT_MULTI_LANG
5365 char_u *lang;
5366#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005367 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005368 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005369 int new_qi = FALSE;
5370 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005371#ifdef FEAT_AUTOCMD
5372 char_u *au_name = NULL;
5373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005375#ifdef FEAT_MULTI_LANG
5376 /* Check for a specified language */
5377 lang = check_help_lang(eap->arg);
5378#endif
5379
Bram Moolenaar73633f82012-01-20 13:39:07 +01005380#ifdef FEAT_AUTOCMD
5381 switch (eap->cmdidx)
5382 {
5383 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5384 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5385 default: break;
5386 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005387 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5388 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005389 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005390# ifdef FEAT_EVAL
5391 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005392 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005393# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005394 }
5395#endif
5396
5397 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5398 save_cpo = p_cpo;
5399 p_cpo = empty_option;
5400
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005401 if (eap->cmdidx == CMD_lhelpgrep)
5402 {
5403 /* Find an existing help window */
5404 FOR_ALL_WINDOWS(wp)
5405 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5406 break;
5407
5408 if (wp == NULL) /* Help window not found */
5409 qi = NULL;
5410 else
5411 qi = wp->w_llist;
5412
5413 if (qi == NULL)
5414 {
5415 /* Allocate a new location list for help text matches */
5416 if ((qi = ll_new_list()) == NULL)
5417 return;
5418 new_qi = TRUE;
5419 }
5420 }
5421
Bram Moolenaaree85df32017-03-19 14:19:50 +01005422 /* Autocommands may change the list. Save it for later comparison */
5423 save_qi = qi;
5424
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5426 regmatch.rm_ic = FALSE;
5427 if (regmatch.regprog != NULL)
5428 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005429#ifdef FEAT_MBYTE
5430 vimconv_T vc;
5431
5432 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5433 * differs. */
5434 vc.vc_type = CONV_NONE;
5435 if (!enc_utf8)
5436 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5437#endif
5438
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005440 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
5442 /* Go through all directories in 'runtimepath' */
5443 p = p_rtp;
5444 while (*p != NUL && !got_int)
5445 {
5446 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5447
5448 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5449 add_pathsep(NameBuff);
5450 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5451 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5452 &fnames, EW_FILE|EW_SILENT) == OK
5453 && fcount > 0)
5454 {
5455 for (fi = 0; fi < fcount && !got_int; ++fi)
5456 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005457#ifdef FEAT_MULTI_LANG
5458 /* Skip files for a different language. */
5459 if (lang != NULL
5460 && STRNICMP(lang, fnames[fi]
5461 + STRLEN(fnames[fi]) - 3, 2) != 0
5462 && !(STRNICMP(lang, "en", 2) == 0
5463 && STRNICMP("txt", fnames[fi]
5464 + STRLEN(fnames[fi]) - 3, 3) == 0))
5465 continue;
5466#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005467 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 if (fd != NULL)
5469 {
5470 lnum = 1;
5471 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5472 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005473 char_u *line = IObuff;
5474#ifdef FEAT_MBYTE
5475 /* Convert a line if 'encoding' is not utf-8 and
5476 * the line contains a non-ASCII character. */
5477 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005478 && has_non_ascii(IObuff))
5479 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005480 line = string_convert(&vc, IObuff, NULL);
5481 if (line == NULL)
5482 line = IObuff;
5483 }
5484#endif
5485
5486 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005488 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
5490 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005491 while (l > 0 && line[l - 1] <= ' ')
5492 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005494 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005495 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 NULL, /* dir */
5497 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005498 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005499 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005501 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005502 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005503 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005504 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 0, /* nr */
5506 1, /* type */
5507 TRUE /* valid */
5508 ) == FAIL)
5509 {
5510 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005511#ifdef FEAT_MBYTE
5512 if (line != IObuff)
5513 vim_free(line);
5514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 break;
5516 }
5517 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005518#ifdef FEAT_MBYTE
5519 if (line != IObuff)
5520 vim_free(line);
5521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 ++lnum;
5523 line_breakcheck();
5524 }
5525 fclose(fd);
5526 }
5527 }
5528 FreeWild(fcount, fnames);
5529 }
5530 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005531
Bram Moolenaar473de612013-06-08 18:19:48 +02005532 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005533#ifdef FEAT_MBYTE
5534 if (vc.vc_type != CONV_NONE)
5535 convert_setup(&vc, NULL, NULL);
5536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005538 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5539 qi->qf_lists[qi->qf_curlist].qf_ptr =
5540 qi->qf_lists[qi->qf_curlist].qf_start;
5541 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005544 if (p_cpo == empty_option)
5545 p_cpo = save_cpo;
5546 else
5547 /* Darn, some plugin changed the value. */
5548 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005551 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552#endif
5553
Bram Moolenaar73633f82012-01-20 13:39:07 +01005554#ifdef FEAT_AUTOCMD
5555 if (au_name != NULL)
5556 {
5557 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5558 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005559 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005560 /* autocommands made "qi" invalid */
5561 return;
5562 }
5563#endif
5564
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005566 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005567 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005568 else
5569 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005570
5571 if (eap->cmdidx == CMD_lhelpgrep)
5572 {
5573 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005574 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005575 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5576 {
5577 if (new_qi)
5578 ll_free_all(&qi);
5579 }
5580 else if (curwin->w_llist == NULL)
5581 curwin->w_llist = qi;
5582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583}
5584
5585#endif /* FEAT_QUICKFIX */