blob: 084d82ec0eedffee081c91cce67273d453a0a6ed [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 Moolenaard12f5c12006-01-25 22:10:52 +000070} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020072/*
73 * Quickfix/Location list stack definition
74 * Contains a list of quickfix/location lists (qf_list_T)
75 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000076struct qf_info_S
77{
78 /*
79 * Count of references to this list. Used only for location lists.
80 * When a location list window reference this list, qf_refcount
81 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
82 * reaches 0, the list is freed.
83 */
84 int qf_refcount;
85 int qf_listcount; /* current number of lists */
86 int qf_curlist; /* current error list */
87 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar361c8f02016-07-02 15:41:47 +020088
89 int qf_dir_curlist; /* error list for qf_dir_stack */
90 struct dir_stack_T *qf_dir_stack;
91 char_u *qf_directory;
92 struct dir_stack_T *qf_file_stack;
93 char_u *qf_currfile;
94 int qf_multiline;
95 int qf_multiignore;
96 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000097};
98
99static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000101#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
103/*
104 * Structure used to hold the info of one part of 'errorformat'
105 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000106typedef struct efm_S efm_T;
107struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108{
109 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000110 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
112 char_u prefix; /* prefix of this format line: */
113 /* 'D' enter directory */
114 /* 'X' leave directory */
115 /* 'A' start of multi-line message */
116 /* 'E' error message */
117 /* 'W' warning message */
118 /* 'I' informational message */
119 /* 'C' continuation line */
120 /* 'Z' end of multi-line message */
121 /* 'G' general, unspecific message */
122 /* 'P' push file (partial) message */
123 /* 'Q' pop/quit file (partial) message */
124 /* 'O' overread (partial) message */
125 char_u flags; /* additional flags given in prefix */
126 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000127 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000128 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129};
130
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100131static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
132
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100133static int qf_init_ext(qf_info_T *qi, 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 +0200134static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void qf_new_list(qf_info_T *qi, char_u *qf_title);
136static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200137static 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 +0100138static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void qf_free(qf_info_T *qi, int idx);
140static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200141static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
142static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200144static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
146static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
149static int is_qf_win(win_T *win, qf_info_T *qi);
150static win_T *qf_find_win(qf_info_T *qi);
151static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200152static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200154static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static char_u *get_mef_name(void);
157static void restore_start_dir(char_u *dirname_start);
158static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
159static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
160static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
161static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163/* Quickfix window check helper macro */
164#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
165/* Location list window check helper macro */
166#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
167/*
168 * Return location list for window 'wp'
169 * For location list window, return the referenced location list
170 */
171#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200174 * Looking up a buffer can be slow if there are many. Remember the last one
175 * to make this a lot faster if there are multiple matches in the same file.
176 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200177static char_u *qf_last_bufname = NULL;
178static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200179
180/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000181 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200182 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 * Return -1 for error, number of errors for success.
184 */
185 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100186qf_init(
187 win_T *wp,
188 char_u *efile,
189 char_u *errorformat,
190 int newlist, /* TRUE: start a new error list */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100191 char_u *qf_title,
192 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000194 qf_info_T *qi = &ql_info;
195
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000196 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000197 {
198 qi = ll_get_or_alloc_list(wp);
199 if (qi == NULL)
200 return FAIL;
201 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000202
203 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200204 (linenr_T)0, (linenr_T)0,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100205 qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000206}
207
208/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200209 * Maximum number of bytes allowed per line while reading a errorfile.
210 */
211#define LINE_MAXLEN 4096
212
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200213static struct fmtpattern
214{
215 char_u convchar;
216 char *pattern;
217} fmt_pat[FMT_PATTERNS] =
218 {
219 {'f', ".\\+"}, /* only used when at end */
220 {'n', "\\d\\+"},
221 {'l', "\\d\\+"},
222 {'c', "\\d\\+"},
223 {'t', "."},
224 {'m', ".\\+"},
225 {'r', ".*"},
226 {'p', "[- .]*"},
227 {'v', "\\d\\+"},
228 {'s', ".\\+"}
229 };
230
231/*
232 * Converts a 'errorformat' string to regular expression pattern
233 */
234 static int
235efm_to_regpat(
236 char_u *efm,
237 int len,
238 efm_T *fmt_ptr,
239 char_u *regpat,
240 char_u *errmsg)
241{
242 char_u *ptr;
243 char_u *efmp;
244 char_u *srcptr;
245 int round;
246 int idx = 0;
247
248 /*
249 * Build regexp pattern from current 'errorformat' option
250 */
251 ptr = regpat;
252 *ptr++ = '^';
253 round = 0;
254 for (efmp = efm; efmp < efm + len; ++efmp)
255 {
256 if (*efmp == '%')
257 {
258 ++efmp;
259 for (idx = 0; idx < FMT_PATTERNS; ++idx)
260 if (fmt_pat[idx].convchar == *efmp)
261 break;
262 if (idx < FMT_PATTERNS)
263 {
264 if (fmt_ptr->addr[idx])
265 {
266 sprintf((char *)errmsg,
267 _("E372: Too many %%%c in format string"), *efmp);
268 EMSG(errmsg);
269 return -1;
270 }
271 if ((idx
272 && idx < 6
273 && vim_strchr((char_u *)"DXOPQ",
274 fmt_ptr->prefix) != NULL)
275 || (idx == 6
276 && vim_strchr((char_u *)"OPQ",
277 fmt_ptr->prefix) == NULL))
278 {
279 sprintf((char *)errmsg,
280 _("E373: Unexpected %%%c in format string"), *efmp);
281 EMSG(errmsg);
282 return -1;
283 }
284 fmt_ptr->addr[idx] = (char_u)++round;
285 *ptr++ = '\\';
286 *ptr++ = '(';
287#ifdef BACKSLASH_IN_FILENAME
288 if (*efmp == 'f')
289 {
290 /* Also match "c:" in the file name, even when
291 * checking for a colon next: "%f:".
292 * "\%(\a:\)\=" */
293 STRCPY(ptr, "\\%(\\a:\\)\\=");
294 ptr += 10;
295 }
296#endif
297 if (*efmp == 'f' && efmp[1] != NUL)
298 {
299 if (efmp[1] != '\\' && efmp[1] != '%')
300 {
301 /* A file name may contain spaces, but this isn't
302 * in "\f". For "%f:%l:%m" there may be a ":" in
303 * the file name. Use ".\{-1,}x" instead (x is
304 * the next character), the requirement that :999:
305 * follows should work. */
306 STRCPY(ptr, ".\\{-1,}");
307 ptr += 7;
308 }
309 else
310 {
311 /* File name followed by '\\' or '%': include as
312 * many file name chars as possible. */
313 STRCPY(ptr, "\\f\\+");
314 ptr += 4;
315 }
316 }
317 else
318 {
319 srcptr = (char_u *)fmt_pat[idx].pattern;
320 while ((*ptr = *srcptr++) != NUL)
321 ++ptr;
322 }
323 *ptr++ = '\\';
324 *ptr++ = ')';
325 }
326 else if (*efmp == '*')
327 {
328 if (*++efmp == '[' || *efmp == '\\')
329 {
330 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
331 {
332 if (efmp[1] == '^')
333 *ptr++ = *++efmp;
334 if (efmp < efm + len)
335 {
336 *ptr++ = *++efmp; /* could be ']' */
337 while (efmp < efm + len
338 && (*ptr++ = *++efmp) != ']')
339 /* skip */;
340 if (efmp == efm + len)
341 {
342 EMSG(_("E374: Missing ] in format string"));
343 return -1;
344 }
345 }
346 }
347 else if (efmp < efm + len) /* %*\D, %*\s etc. */
348 *ptr++ = *++efmp;
349 *ptr++ = '\\';
350 *ptr++ = '+';
351 }
352 else
353 {
354 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
355 sprintf((char *)errmsg,
356 _("E375: Unsupported %%%c in format string"), *efmp);
357 EMSG(errmsg);
358 return -1;
359 }
360 }
361 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
362 *ptr++ = *efmp; /* regexp magic characters */
363 else if (*efmp == '#')
364 *ptr++ = '*';
365 else if (*efmp == '>')
366 fmt_ptr->conthere = TRUE;
367 else if (efmp == efm + 1) /* analyse prefix */
368 {
369 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
370 fmt_ptr->flags = *efmp++;
371 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
372 fmt_ptr->prefix = *efmp;
373 else
374 {
375 sprintf((char *)errmsg,
376 _("E376: Invalid %%%c in format string prefix"), *efmp);
377 EMSG(errmsg);
378 return -1;
379 }
380 }
381 else
382 {
383 sprintf((char *)errmsg,
384 _("E377: Invalid %%%c in format string"), *efmp);
385 EMSG(errmsg);
386 return -1;
387 }
388 }
389 else /* copy normal character */
390 {
391 if (*efmp == '\\' && efmp + 1 < efm + len)
392 ++efmp;
393 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
394 *ptr++ = '\\'; /* escape regexp atoms */
395 if (*efmp)
396 *ptr++ = *efmp;
397 }
398 }
399 *ptr++ = '$';
400 *ptr = NUL;
401
402 return 0;
403}
404
405 static void
406free_efm_list(efm_T **efm_first)
407{
408 efm_T *efm_ptr;
409
410 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
411 {
412 *efm_first = efm_ptr->next;
413 vim_regfree(efm_ptr->prog);
414 vim_free(efm_ptr);
415 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100416 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200417}
418
419/* Parse 'errorformat' option */
420 static efm_T *
421parse_efm_option(char_u *efm)
422{
423 char_u *errmsg = NULL;
424 int errmsglen;
425 efm_T *fmt_ptr = NULL;
426 efm_T *fmt_first = NULL;
427 efm_T *fmt_last = NULL;
428 char_u *fmtstr = NULL;
429 int len;
430 int i;
431 int round;
432
433 errmsglen = CMDBUFFSIZE + 1;
434 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
435 if (errmsg == NULL)
436 goto parse_efm_end;
437
438 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200439 * Each part of the format string is copied and modified from errorformat
440 * to regex prog. Only a few % characters are allowed.
441 */
442
443 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200444 * Get some space to modify the format string into.
445 */
446 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
447 for (round = FMT_PATTERNS; round > 0; )
448 i += (int)STRLEN(fmt_pat[--round].pattern);
449#ifdef COLON_IN_FILENAME
450 i += 12; /* "%f" can become twelve chars longer */
451#else
452 i += 2; /* "%f" can become two chars longer */
453#endif
454 if ((fmtstr = alloc(i)) == NULL)
455 goto parse_efm_error;
456
457 while (efm[0] != NUL)
458 {
459 /*
460 * Allocate a new eformat structure and put it at the end of the list
461 */
462 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
463 if (fmt_ptr == NULL)
464 goto parse_efm_error;
465 if (fmt_first == NULL) /* first one */
466 fmt_first = fmt_ptr;
467 else
468 fmt_last->next = fmt_ptr;
469 fmt_last = fmt_ptr;
470
471 /*
472 * Isolate one part in the 'errorformat' option
473 */
474 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
475 if (efm[len] == '\\' && efm[len + 1] != NUL)
476 ++len;
477
478 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
479 goto parse_efm_error;
480 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
481 goto parse_efm_error;
482 /*
483 * Advance to next part
484 */
485 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
486 }
487
488 if (fmt_first == NULL) /* nothing found */
489 EMSG(_("E378: 'errorformat' contains no pattern"));
490
491 goto parse_efm_end;
492
493parse_efm_error:
494 free_efm_list(&fmt_first);
495
496parse_efm_end:
497 vim_free(fmtstr);
498 vim_free(errmsg);
499
500 return fmt_first;
501}
502
Bram Moolenaare0d37972016-07-15 22:36:01 +0200503enum {
504 QF_FAIL = 0,
505 QF_OK = 1,
506 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200507 QF_NOMEM = 3,
508 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200509};
510
511typedef struct {
512 char_u *linebuf;
513 int linelen;
514 char_u *growbuf;
515 int growbufsiz;
516 FILE *fd;
517 typval_T *tv;
518 char_u *p_str;
519 listitem_T *p_li;
520 buf_T *buf;
521 linenr_T buflnum;
522 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100523 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200524} qfstate_T;
525
526 static char_u *
527qf_grow_linebuf(qfstate_T *state, int newsz)
528{
529 /*
530 * If the line exceeds LINE_MAXLEN exclude the last
531 * byte since it's not a NL character.
532 */
533 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
534 if (state->growbuf == NULL)
535 {
536 state->growbuf = alloc(state->linelen + 1);
537 if (state->growbuf == NULL)
538 return NULL;
539 state->growbufsiz = state->linelen;
540 }
541 else if (state->linelen > state->growbufsiz)
542 {
543 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
544 if (state->growbuf == NULL)
545 return NULL;
546 state->growbufsiz = state->linelen;
547 }
548 return state->growbuf;
549}
550
551/*
552 * Get the next string (separated by newline) from state->p_str.
553 */
554 static int
555qf_get_next_str_line(qfstate_T *state)
556{
557 /* Get the next line from the supplied string */
558 char_u *p_str = state->p_str;
559 char_u *p;
560 int len;
561
562 if (*p_str == NUL) /* Reached the end of the string */
563 return QF_END_OF_INPUT;
564
565 p = vim_strchr(p_str, '\n');
566 if (p != NULL)
567 len = (int)(p - p_str) + 1;
568 else
569 len = (int)STRLEN(p_str);
570
571 if (len > IOSIZE - 2)
572 {
573 state->linebuf = qf_grow_linebuf(state, len);
574 if (state->linebuf == NULL)
575 return QF_NOMEM;
576 }
577 else
578 {
579 state->linebuf = IObuff;
580 state->linelen = len;
581 }
582 vim_strncpy(state->linebuf, p_str, state->linelen);
583
584 /*
585 * Increment using len in order to discard the rest of the
586 * line if it exceeds LINE_MAXLEN.
587 */
588 p_str += len;
589 state->p_str = p_str;
590
591 return QF_OK;
592}
593
594/*
595 * Get the next string from state->p_Li.
596 */
597 static int
598qf_get_next_list_line(qfstate_T *state)
599{
600 listitem_T *p_li = state->p_li;
601 int len;
602
603 while (p_li != NULL
604 && (p_li->li_tv.v_type != VAR_STRING
605 || p_li->li_tv.vval.v_string == NULL))
606 p_li = p_li->li_next; /* Skip non-string items */
607
608 if (p_li == NULL) /* End of the list */
609 {
610 state->p_li = NULL;
611 return QF_END_OF_INPUT;
612 }
613
614 len = (int)STRLEN(p_li->li_tv.vval.v_string);
615 if (len > IOSIZE - 2)
616 {
617 state->linebuf = qf_grow_linebuf(state, len);
618 if (state->linebuf == NULL)
619 return QF_NOMEM;
620 }
621 else
622 {
623 state->linebuf = IObuff;
624 state->linelen = len;
625 }
626
627 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
628
629 state->p_li = p_li->li_next; /* next item */
630 return QF_OK;
631}
632
633/*
634 * Get the next string from state->buf.
635 */
636 static int
637qf_get_next_buf_line(qfstate_T *state)
638{
639 char_u *p_buf = NULL;
640 int len;
641
642 /* Get the next line from the supplied buffer */
643 if (state->buflnum > state->lnumlast)
644 return QF_END_OF_INPUT;
645
646 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
647 state->buflnum += 1;
648
649 len = (int)STRLEN(p_buf);
650 if (len > IOSIZE - 2)
651 {
652 state->linebuf = qf_grow_linebuf(state, len);
653 if (state->linebuf == NULL)
654 return QF_NOMEM;
655 }
656 else
657 {
658 state->linebuf = IObuff;
659 state->linelen = len;
660 }
661 vim_strncpy(state->linebuf, p_buf, state->linelen);
662
663 return QF_OK;
664}
665
666/*
667 * Get the next string from file state->fd.
668 */
669 static int
670qf_get_next_file_line(qfstate_T *state)
671{
672 int discard;
673 int growbuflen;
674
675 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
676 return QF_END_OF_INPUT;
677
678 discard = FALSE;
679 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200680 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200681 {
682 /*
683 * The current line exceeds IObuff, continue reading using
684 * growbuf until EOL or LINE_MAXLEN bytes is read.
685 */
686 if (state->growbuf == NULL)
687 {
688 state->growbufsiz = 2 * (IOSIZE - 1);
689 state->growbuf = alloc(state->growbufsiz);
690 if (state->growbuf == NULL)
691 return QF_NOMEM;
692 }
693
694 /* Copy the read part of the line, excluding null-terminator */
695 memcpy(state->growbuf, IObuff, IOSIZE - 1);
696 growbuflen = state->linelen;
697
698 for (;;)
699 {
700 if (fgets((char *)state->growbuf + growbuflen,
701 state->growbufsiz - growbuflen, state->fd) == NULL)
702 break;
703 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
704 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200705 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200706 break;
707 if (state->growbufsiz == LINE_MAXLEN)
708 {
709 discard = TRUE;
710 break;
711 }
712
713 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
714 ? 2 * state->growbufsiz : LINE_MAXLEN;
715 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
716 if (state->growbuf == NULL)
717 return QF_NOMEM;
718 }
719
720 while (discard)
721 {
722 /*
723 * The current line is longer than LINE_MAXLEN, continue
724 * reading but discard everything until EOL or EOF is
725 * reached.
726 */
727 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
728 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200729 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200730 break;
731 }
732
733 state->linebuf = state->growbuf;
734 state->linelen = growbuflen;
735 }
736 else
737 state->linebuf = IObuff;
738
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100739#ifdef FEAT_MBYTE
740 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200741 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
742 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100743 char_u *line;
744
745 line = string_convert(&state->vc, state->linebuf, &state->linelen);
746 if (line != NULL)
747 {
748 if (state->linelen < IOSIZE)
749 {
750 STRCPY(state->linebuf, line);
751 vim_free(line);
752 }
753 else
754 {
755 vim_free(state->growbuf);
756 state->linebuf = state->growbuf = line;
757 state->growbufsiz = state->linelen < LINE_MAXLEN
758 ? state->linelen : LINE_MAXLEN;
759 }
760 }
761 }
762#endif
763
Bram Moolenaare0d37972016-07-15 22:36:01 +0200764 return QF_OK;
765}
766
767/*
768 * Get the next string from a file/buffer/list/string.
769 */
770 static int
771qf_get_nextline(qfstate_T *state)
772{
773 int status = QF_FAIL;
774
775 if (state->fd == NULL)
776 {
777 if (state->tv != NULL)
778 {
779 if (state->tv->v_type == VAR_STRING)
780 /* Get the next line from the supplied string */
781 status = qf_get_next_str_line(state);
782 else if (state->tv->v_type == VAR_LIST)
783 /* Get the next line from the supplied list */
784 status = qf_get_next_list_line(state);
785 }
786 else
787 /* Get the next line from the supplied buffer */
788 status = qf_get_next_buf_line(state);
789 }
790 else
791 /* Get the next line from the supplied file */
792 status = qf_get_next_file_line(state);
793
794 if (status != QF_OK)
795 return status;
796
797 /* remove newline/CR from the line */
798 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200799 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200800 state->linebuf[state->linelen - 1] = NUL;
801#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200802 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
803 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200805 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806
807#ifdef FEAT_MBYTE
808 remove_bom(state->linebuf);
809#endif
810
811 return QF_OK;
812}
813
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200814typedef struct {
815 char_u *namebuf;
816 char_u *errmsg;
817 int errmsglen;
818 long lnum;
819 int col;
820 char_u use_viscol;
821 char_u *pattern;
822 int enr;
823 int type;
824 int valid;
825} qffields_T;
826
827/*
828 * Parse a line and get the quickfix fields.
829 * Return the QF_ status.
830 */
831 static int
832qf_parse_line(
833 qf_info_T *qi,
834 char_u *linebuf,
835 int linelen,
836 efm_T *fmt_first,
837 qffields_T *fields)
838{
839 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200840 char_u *ptr;
841 int len;
842 int i;
843 int idx = 0;
844 char_u *tail = NULL;
845 regmatch_T regmatch;
846
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;
870 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
871 continue;
872 fields->namebuf[0] = NUL;
873 fields->pattern[0] = NUL;
874 if (!qi->qf_multiscan)
875 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 {
888 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
889 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 }
940 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
941 {
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 }
1016 qi->qf_multiscan = FALSE;
1017
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 }
1029 qi->qf_directory =
1030 qf_push_dir(fields->namebuf, &qi->qf_dir_stack, FALSE);
1031 if (qi->qf_directory == NULL)
1032 return QF_FAIL;
1033 }
1034 else if (idx == 'X') /* leave directory */
1035 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
1036 }
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)
1051 qi->qf_multiline = qi->qf_multiignore = FALSE;
1052 }
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 {
1061 qi->qf_multiline = TRUE; /* start of a multi-line message */
1062 qi->qf_multiignore = FALSE; /* reset continuation */
1063 }
1064 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1065 { /* continuation of multi-line msg */
Bram Moolenaar9b457942016-10-09 16:10:05 +02001066 if (!qi->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001067 {
Bram Moolenaar9b457942016-10-09 16:10:05 +02001068 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].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;
1072 if (*fields->errmsg && !qi->qf_multiignore)
1073 {
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)
1096 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1097 *fields->namebuf || qi->qf_directory != NULL
1098 ? fields->namebuf
1099 : qi->qf_currfile != NULL && fields->valid
1100 ? qi->qf_currfile : 0);
1101 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001102 if (idx == 'Z')
1103 qi->qf_multiline = qi->qf_multiignore = FALSE;
1104 line_breakcheck();
1105 return QF_IGNORE_LINE;
1106 }
1107 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1108 {
1109 /* global file names */
1110 fields->valid = FALSE;
1111 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1112 {
1113 if (*fields->namebuf && idx == 'P')
1114 qi->qf_currfile =
1115 qf_push_dir(fields->namebuf, &qi->qf_file_stack, TRUE);
1116 else if (idx == 'Q')
1117 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
1118 *fields->namebuf = NUL;
1119 if (tail && *tail)
1120 {
1121 STRMOVE(IObuff, skipwhite(tail));
1122 qi->qf_multiscan = TRUE;
1123 goto restofline;
1124 }
1125 }
1126 }
1127 if (fmt_ptr->flags == '-') /* generally exclude this line */
1128 {
1129 if (qi->qf_multiline)
1130 /* also exclude continuation lines */
1131 qi->qf_multiignore = TRUE;
1132 return QF_IGNORE_LINE;
1133 }
1134 }
1135
1136 return QF_OK;
1137}
1138
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001139/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001140 * Read the errorfile "efile" into memory, line by line, building the error
1141 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001142 * Alternative: when "efile" is NULL read errors from buffer "buf".
1143 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001144 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001145 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1146 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001147 * Return -1 for error, number of errors for success.
1148 */
1149 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001150qf_init_ext(
1151 qf_info_T *qi,
1152 char_u *efile,
1153 buf_T *buf,
1154 typval_T *tv,
1155 char_u *errorformat,
1156 int newlist, /* TRUE: start a new error list */
1157 linenr_T lnumfirst, /* first line number to use */
1158 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001159 char_u *qf_title,
1160 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001161{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001162 qfstate_T state;
1163 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001164#ifdef FEAT_WINDOWS
1165 qfline_T *old_last = NULL;
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001166 int adding = FALSE;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001167#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001168 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001170 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001172 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001174 /* Do not used the cached buffer, it may have been wiped out. */
1175 vim_free(qf_last_bufname);
1176 qf_last_bufname = NULL;
1177
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001178 vim_memset(&state, 0, sizeof(state));
1179 vim_memset(&fields, 0, sizeof(fields));
1180#ifdef FEAT_MBYTE
1181 state.vc.vc_type = CONV_NONE;
1182 if (enc != NULL && *enc != NUL)
1183 convert_setup(&state.vc, enc, p_enc);
1184#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001185 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1186 fields.errmsglen = CMDBUFFSIZE + 1;
1187 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1188 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1189 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1190 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 goto qf_init_end;
1192
Bram Moolenaare0d37972016-07-15 22:36:01 +02001193 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
1195 EMSG2(_(e_openerrf), efile);
1196 goto qf_init_end;
1197 }
1198
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001199 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001201 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001202#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001203 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02001204 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001205 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001206 adding = TRUE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001207 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001208 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001212 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001213 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 else
1215 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001217 /*
1218 * If we are not adding or adding to another list: clear the state.
1219 */
1220 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
1221 {
1222 qi->qf_dir_curlist = qi->qf_curlist;
1223 qf_clean_dir_stack(&qi->qf_dir_stack);
1224 qi->qf_directory = NULL;
1225 qf_clean_dir_stack(&qi->qf_file_stack);
1226 qi->qf_currfile = NULL;
1227 qi->qf_multiline = FALSE;
1228 qi->qf_multiignore = FALSE;
1229 qi->qf_multiscan = FALSE;
1230 }
1231
1232 /*
1233 * If the errorformat didn't change between calls, then reuse the
1234 * previously parsed values.
1235 */
1236 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1237 {
1238 /* free the previously parsed data */
1239 vim_free(last_efm);
1240 last_efm = NULL;
1241 free_efm_list(&fmt_first);
1242
1243 /* parse the current 'efm' */
1244 fmt_first = parse_efm_option(efm);
1245 if (fmt_first != NULL)
1246 last_efm = vim_strsave(efm);
1247 }
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
1252 /*
1253 * got_int is reset here, because it was probably set when killing the
1254 * ":make" command, but we still want to read the errorfile then.
1255 */
1256 got_int = FALSE;
1257
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001258 if (tv != NULL)
1259 {
1260 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001261 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001262 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001263 state.p_li = tv->vval.v_list->lv_first;
1264 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001265 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001266 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001267 state.buflnum = lnumfirst;
1268 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 /*
1271 * Read the lines in the error file one by one.
1272 * Try to recognize one of the error formats in each line.
1273 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001274 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001276 /* Get the next line from a file/buffer/list/string */
1277 status = qf_get_nextline(&state);
1278 if (status == QF_NOMEM) /* memory alloc failure */
1279 goto qf_init_end;
1280 if (status == QF_END_OF_INPUT) /* end of input */
1281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001283 status = qf_parse_line(qi, state.linebuf, state.linelen, fmt_first,
1284 &fields);
1285 if (status == QF_FAIL)
1286 goto error2;
1287 if (status == QF_NOMEM)
1288 goto qf_init_end;
1289 if (status == QF_IGNORE_LINE)
1290 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001292 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001293 qi->qf_curlist,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001294 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001295 (*fields.namebuf || qi->qf_directory != NULL)
1296 ? fields.namebuf
1297 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001298 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001299 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001300 fields.errmsg,
1301 fields.lnum,
1302 fields.col,
1303 fields.use_viscol,
1304 fields.pattern,
1305 fields.enr,
1306 fields.type,
1307 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 goto error2;
1309 line_breakcheck();
1310 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001311 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001313 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001315 /* no valid entry found */
1316 qi->qf_lists[qi->qf_curlist].qf_ptr =
1317 qi->qf_lists[qi->qf_curlist].qf_start;
1318 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1319 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 else
1322 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001323 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1324 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1325 qi->qf_lists[qi->qf_curlist].qf_ptr =
1326 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001328 /* return number of matches */
1329 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001330 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 EMSG(_(e_readerrf));
1333error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001334 if (!adding)
1335 {
1336 qf_free(qi, qi->qf_curlist);
1337 qi->qf_listcount--;
1338 if (qi->qf_curlist > 0)
1339 --qi->qf_curlist;
1340 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001341qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001342 if (state.fd != NULL)
1343 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001344 vim_free(fields.namebuf);
1345 vim_free(fields.errmsg);
1346 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001347 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
1349#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001350 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001352#ifdef FEAT_MBYTE
1353 if (state.vc.vc_type != CONV_NONE)
1354 convert_setup(&state.vc, NULL, NULL);
1355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
1357 return retval;
1358}
1359
Bram Moolenaarfb604092014-07-23 15:55:00 +02001360 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001361qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001362{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001363 vim_free(qi->qf_lists[qf_idx].qf_title);
1364 qi->qf_lists[qf_idx].qf_title = NULL;
1365
Bram Moolenaarfb604092014-07-23 15:55:00 +02001366 if (title != NULL)
1367 {
1368 char_u *p = alloc((int)STRLEN(title) + 2);
1369
Bram Moolenaara3921f42017-06-04 15:30:34 +02001370 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001371 if (p != NULL)
1372 sprintf((char *)p, ":%s", (char *)title);
1373 }
1374}
1375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376/*
1377 * Prepare for adding a new quickfix list.
1378 */
1379 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001380qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
1382 int i;
1383
1384 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001385 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 * the current entry. This makes it possible to browse in a tree-like
1387 * way with ":grep'.
1388 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001389 while (qi->qf_listcount > qi->qf_curlist + 1)
1390 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392 /*
1393 * When the stack is full, remove to oldest entry
1394 * Otherwise, add a new entry.
1395 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001396 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001398 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001400 qi->qf_lists[i - 1] = qi->qf_lists[i];
1401 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001404 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001405 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001406 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407}
1408
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001409/*
1410 * Free a location list
1411 */
1412 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001413ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001414{
1415 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001416 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001417
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001418 qi = *pqi;
1419 if (qi == NULL)
1420 return;
1421 *pqi = NULL; /* Remove reference to this list */
1422
1423 qi->qf_refcount--;
1424 if (qi->qf_refcount < 1)
1425 {
1426 /* No references to this location list */
1427 for (i = 0; i < qi->qf_listcount; ++i)
1428 qf_free(qi, i);
1429 vim_free(qi);
1430 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001431}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001432
1433 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001434qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001435{
1436 int i;
1437 qf_info_T *qi = &ql_info;
1438
1439 if (wp != NULL)
1440 {
1441 /* location list */
1442 ll_free_all(&wp->w_llist);
1443 ll_free_all(&wp->w_llist_ref);
1444 }
1445 else
1446 /* quickfix list */
1447 for (i = 0; i < qi->qf_listcount; ++i)
1448 qf_free(qi, i);
1449}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451/*
1452 * Add an entry to the end of the list of errors.
1453 * Returns OK or FAIL.
1454 */
1455 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001456qf_add_entry(
1457 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001458 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001459 char_u *dir, /* optional directory name */
1460 char_u *fname, /* file name or NULL */
1461 int bufnum, /* buffer number or zero */
1462 char_u *mesg, /* message */
1463 long lnum, /* line number */
1464 int col, /* column */
1465 int vis_col, /* using visual column */
1466 char_u *pattern, /* search pattern */
1467 int nr, /* error number */
1468 int type, /* type character */
1469 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001471 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001472 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001474 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001476 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001477 {
1478 buf_T *buf = buflist_findnr(bufnum);
1479
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001480 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001481 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001482 buf->b_has_qf_entry |=
1483 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001484 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001485 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001486 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1488 {
1489 vim_free(qfp);
1490 return FAIL;
1491 }
1492 qfp->qf_lnum = lnum;
1493 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001494 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001495 if (pattern == NULL || *pattern == NUL)
1496 qfp->qf_pattern = NULL;
1497 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1498 {
1499 vim_free(qfp->qf_text);
1500 vim_free(qfp);
1501 return FAIL;
1502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 qfp->qf_nr = nr;
1504 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1505 type = 0;
1506 qfp->qf_type = type;
1507 qfp->qf_valid = valid;
1508
Bram Moolenaara3921f42017-06-04 15:30:34 +02001509 lastp = &qi->qf_lists[qf_idx].qf_last;
1510 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001511 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001513 qi->qf_lists[qf_idx].qf_start = qfp;
1514 qi->qf_lists[qf_idx].qf_ptr = qfp;
1515 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001516 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518 else
1519 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001520 qfp->qf_prev = *lastp;
1521 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001523 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001525 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001526 ++qi->qf_lists[qf_idx].qf_count;
1527 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001528 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001530 qi->qf_lists[qf_idx].qf_index =
1531 qi->qf_lists[qf_idx].qf_count;
1532 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534
1535 return OK;
1536}
1537
1538/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001539 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001540 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001541 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001542ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001544 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001545
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001546 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1547 if (qi != NULL)
1548 {
1549 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1550 qi->qf_refcount++;
1551 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001552
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001553 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001554}
1555
1556/*
1557 * Return the location list for window 'wp'.
1558 * If not present, allocate a location list
1559 */
1560 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001561ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001562{
1563 if (IS_LL_WINDOW(wp))
1564 /* For a location list window, use the referenced location list */
1565 return wp->w_llist_ref;
1566
1567 /*
1568 * For a non-location list window, w_llist_ref should not point to a
1569 * location list.
1570 */
1571 ll_free_all(&wp->w_llist_ref);
1572
1573 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001574 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001575 return wp->w_llist;
1576}
1577
1578/*
1579 * Copy the location list from window "from" to window "to".
1580 */
1581 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001582copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001583{
1584 qf_info_T *qi;
1585 int idx;
1586 int i;
1587
1588 /*
1589 * When copying from a location list window, copy the referenced
1590 * location list. For other windows, copy the location list for
1591 * that window.
1592 */
1593 if (IS_LL_WINDOW(from))
1594 qi = from->w_llist_ref;
1595 else
1596 qi = from->w_llist;
1597
1598 if (qi == NULL) /* no location list to copy */
1599 return;
1600
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001601 /* allocate a new location list */
1602 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001603 return;
1604
1605 to->w_llist->qf_listcount = qi->qf_listcount;
1606
1607 /* Copy the location lists one at a time */
1608 for (idx = 0; idx < qi->qf_listcount; idx++)
1609 {
1610 qf_list_T *from_qfl;
1611 qf_list_T *to_qfl;
1612
1613 to->w_llist->qf_curlist = idx;
1614
1615 from_qfl = &qi->qf_lists[idx];
1616 to_qfl = &to->w_llist->qf_lists[idx];
1617
1618 /* Some of the fields are populated by qf_add_entry() */
1619 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1620 to_qfl->qf_count = 0;
1621 to_qfl->qf_index = 0;
1622 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001623 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001624 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001625 if (from_qfl->qf_title != NULL)
1626 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1627 else
1628 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001629 if (from_qfl->qf_ctx != NULL)
1630 {
1631 to_qfl->qf_ctx = alloc_tv();
1632 if (to_qfl->qf_ctx != NULL)
1633 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1634 }
1635 else
1636 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001637
1638 if (from_qfl->qf_count)
1639 {
1640 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001641 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001642
1643 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001644 for (i = 0, from_qfp = from_qfl->qf_start;
1645 i < from_qfl->qf_count && from_qfp != NULL;
1646 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001647 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001648 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001649 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001650 NULL,
1651 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001652 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001653 from_qfp->qf_text,
1654 from_qfp->qf_lnum,
1655 from_qfp->qf_col,
1656 from_qfp->qf_viscol,
1657 from_qfp->qf_pattern,
1658 from_qfp->qf_nr,
1659 0,
1660 from_qfp->qf_valid) == FAIL)
1661 {
1662 qf_free_all(to);
1663 return;
1664 }
1665 /*
1666 * qf_add_entry() will not set the qf_num field, as the
1667 * directory and file names are not supplied. So the qf_fnum
1668 * field is copied here.
1669 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001670 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001671 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1672 prevp->qf_type = from_qfp->qf_type; /* error type */
1673 if (from_qfl->qf_ptr == from_qfp)
1674 to_qfl->qf_ptr = prevp; /* current location */
1675 }
1676 }
1677
1678 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1679
1680 /* When no valid entries are present in the list, qf_ptr points to
1681 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001682 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001683 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001684 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001685 to_qfl->qf_index = 1;
1686 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001687 }
1688
1689 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1690}
1691
1692/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001693 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001694 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 */
1696 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001697qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
Bram Moolenaar82404332016-07-10 17:00:38 +02001699 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001700 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001701 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (fname == NULL || *fname == NUL) /* no file name */
1704 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaare60acc12011-05-10 16:41:25 +02001706#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001707 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001708#endif
1709#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001710 if (directory != NULL)
1711 slash_adjust(directory);
1712 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001713#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001714 if (directory != NULL && !vim_isAbsName(fname)
1715 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1716 {
1717 /*
1718 * Here we check if the file really exists.
1719 * This should normally be true, but if make works without
1720 * "leaving directory"-messages we might have missed a
1721 * directory change.
1722 */
1723 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001726 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001727 if (directory)
1728 ptr = concat_fnames(directory, fname, TRUE);
1729 else
1730 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001732 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001733 bufname = ptr;
1734 }
1735 else
1736 bufname = fname;
1737
1738 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001739 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001740 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001741 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001742 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001744 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001745 {
1746 vim_free(qf_last_bufname);
1747 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1748 if (bufname == ptr)
1749 qf_last_bufname = bufname;
1750 else
1751 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001752 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001753 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001754 if (buf == NULL)
1755 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001756
Bram Moolenaarc1542742016-07-20 21:44:37 +02001757 buf->b_has_qf_entry =
1758 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001759 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760}
1761
1762/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001763 * Push dirbuf onto the directory stack and return pointer to actual dir or
1764 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 */
1766 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001767qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 struct dir_stack_T *ds_new;
1770 struct dir_stack_T *ds_ptr;
1771
1772 /* allocate new stack element and hook it in */
1773 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1774 if (ds_new == NULL)
1775 return NULL;
1776
1777 ds_new->next = *stackptr;
1778 *stackptr = ds_new;
1779
1780 /* store directory on the stack */
1781 if (vim_isAbsName(dirbuf)
1782 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001783 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 (*stackptr)->dirname = vim_strsave(dirbuf);
1785 else
1786 {
1787 /* Okay we don't have an absolute path.
1788 * dirbuf must be a subdir of one of the directories on the stack.
1789 * Let's search...
1790 */
1791 ds_new = (*stackptr)->next;
1792 (*stackptr)->dirname = NULL;
1793 while (ds_new)
1794 {
1795 vim_free((*stackptr)->dirname);
1796 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1797 TRUE);
1798 if (mch_isdir((*stackptr)->dirname) == TRUE)
1799 break;
1800
1801 ds_new = ds_new->next;
1802 }
1803
1804 /* clean up all dirs we already left */
1805 while ((*stackptr)->next != ds_new)
1806 {
1807 ds_ptr = (*stackptr)->next;
1808 (*stackptr)->next = (*stackptr)->next->next;
1809 vim_free(ds_ptr->dirname);
1810 vim_free(ds_ptr);
1811 }
1812
1813 /* Nothing found -> it must be on top level */
1814 if (ds_new == NULL)
1815 {
1816 vim_free((*stackptr)->dirname);
1817 (*stackptr)->dirname = vim_strsave(dirbuf);
1818 }
1819 }
1820
1821 if ((*stackptr)->dirname != NULL)
1822 return (*stackptr)->dirname;
1823 else
1824 {
1825 ds_ptr = *stackptr;
1826 *stackptr = (*stackptr)->next;
1827 vim_free(ds_ptr);
1828 return NULL;
1829 }
1830}
1831
1832
1833/*
1834 * pop dirbuf from the directory stack and return previous directory or NULL if
1835 * stack is empty
1836 */
1837 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001838qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 struct dir_stack_T *ds_ptr;
1841
1842 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1843 * What to do if it isn't? */
1844
1845 /* pop top element and free it */
1846 if (*stackptr != NULL)
1847 {
1848 ds_ptr = *stackptr;
1849 *stackptr = (*stackptr)->next;
1850 vim_free(ds_ptr->dirname);
1851 vim_free(ds_ptr);
1852 }
1853
1854 /* return NEW top element as current dir or NULL if stack is empty*/
1855 return *stackptr ? (*stackptr)->dirname : NULL;
1856}
1857
1858/*
1859 * clean up directory stack
1860 */
1861 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001862qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
1864 struct dir_stack_T *ds_ptr;
1865
1866 while ((ds_ptr = *stackptr) != NULL)
1867 {
1868 *stackptr = (*stackptr)->next;
1869 vim_free(ds_ptr->dirname);
1870 vim_free(ds_ptr);
1871 }
1872}
1873
1874/*
1875 * Check in which directory of the directory stack the given file can be
1876 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001877 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 * Cleans up intermediate directory entries.
1879 *
1880 * TODO: How to solve the following problem?
1881 * If we have the this directory tree:
1882 * ./
1883 * ./aa
1884 * ./aa/bb
1885 * ./bb
1886 * ./bb/x.c
1887 * and make says:
1888 * making all in aa
1889 * making all in bb
1890 * x.c:9: Error
1891 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1892 * qf_guess_filepath will return NULL.
1893 */
1894 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001895qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 struct dir_stack_T *ds_ptr;
1898 struct dir_stack_T *ds_tmp;
1899 char_u *fullname;
1900
1901 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001902 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 return NULL;
1904
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001905 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 fullname = NULL;
1907 while (ds_ptr)
1908 {
1909 vim_free(fullname);
1910 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1911
1912 /* If concat_fnames failed, just go on. The worst thing that can happen
1913 * is that we delete the entire stack.
1914 */
1915 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1916 break;
1917
1918 ds_ptr = ds_ptr->next;
1919 }
1920
1921 vim_free(fullname);
1922
1923 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001924 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001926 ds_tmp = qi->qf_dir_stack->next;
1927 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 vim_free(ds_tmp->dirname);
1929 vim_free(ds_tmp);
1930 }
1931
1932 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933}
1934
1935/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001936 * When loading a file from the quickfix, the auto commands may modify it.
1937 * This may invalidate the current quickfix entry. This function checks
1938 * whether a entry is still present in the quickfix.
1939 * Similar to location list.
1940 */
1941 static int
1942is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1943{
1944 qf_list_T *qfl;
1945 qfline_T *qfp;
1946 int i;
1947
1948 qfl = &qi->qf_lists[qi->qf_curlist];
1949
1950 /* Search for the entry in the current list */
1951 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1952 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001953 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001954 break;
1955
1956 if (i == qfl->qf_count) /* Entry is not found */
1957 return FALSE;
1958
1959 return TRUE;
1960}
1961
1962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 * jump to a quickfix line
1964 * if dir == FORWARD go "errornr" valid entries forward
1965 * if dir == BACKWARD go "errornr" valid entries backward
1966 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1967 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1968 * else if "errornr" is zero, redisplay the same line
1969 * else go to entry "errornr"
1970 */
1971 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001972qf_jump(
1973 qf_info_T *qi,
1974 int dir,
1975 int errornr,
1976 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001978 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001979 qfline_T *qf_ptr;
1980 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 int qf_index;
1982 int old_qf_fnum;
1983 int old_qf_index;
1984 int prev_index;
1985 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1986 char_u *err = e_no_more_items;
1987 linenr_T i;
1988 buf_T *old_curbuf;
1989 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 colnr_T screen_col;
1991 colnr_T char_col;
1992 char_u *line;
1993#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001994 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001995 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 int opened_window = FALSE;
1997 win_T *win;
1998 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001999 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002001 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 int print_message = TRUE;
2003 int len;
2004#ifdef FEAT_FOLDING
2005 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2006#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002007 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002008 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002010 if (qi == NULL)
2011 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002012
2013 if (qi->qf_curlist >= qi->qf_listcount
2014 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
2016 EMSG(_(e_quickfix));
2017 return;
2018 }
2019
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002020 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002022 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 old_qf_index = qf_index;
2024 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
2025 {
2026 while (errornr--)
2027 {
2028 old_qf_ptr = qf_ptr;
2029 prev_index = qf_index;
2030 old_qf_fnum = qf_ptr->qf_fnum;
2031 do
2032 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002033 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 || qf_ptr->qf_next == NULL)
2035 {
2036 qf_ptr = old_qf_ptr;
2037 qf_index = prev_index;
2038 if (err != NULL)
2039 {
2040 EMSG(_(err));
2041 goto theend;
2042 }
2043 errornr = 0;
2044 break;
2045 }
2046 ++qf_index;
2047 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002048 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2049 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2051 err = NULL;
2052 }
2053 }
2054 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
2055 {
2056 while (errornr--)
2057 {
2058 old_qf_ptr = qf_ptr;
2059 prev_index = qf_index;
2060 old_qf_fnum = qf_ptr->qf_fnum;
2061 do
2062 {
2063 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
2064 {
2065 qf_ptr = old_qf_ptr;
2066 qf_index = prev_index;
2067 if (err != NULL)
2068 {
2069 EMSG(_(err));
2070 goto theend;
2071 }
2072 errornr = 0;
2073 break;
2074 }
2075 --qf_index;
2076 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2078 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2080 err = NULL;
2081 }
2082 }
2083 else if (errornr != 0) /* go to specified number */
2084 {
2085 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2086 {
2087 --qf_index;
2088 qf_ptr = qf_ptr->qf_prev;
2089 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 while (errornr > qf_index && qf_index <
2091 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 && qf_ptr->qf_next != NULL)
2093 {
2094 ++qf_index;
2095 qf_ptr = qf_ptr->qf_next;
2096 }
2097 }
2098
2099#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002100 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2101 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 /* No need to print the error message if it's visible in the error
2103 * window */
2104 print_message = FALSE;
2105
2106 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002107 * For ":helpgrep" find a help window or open one.
2108 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002109 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002110 {
2111 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002112
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002113 if (cmdmod.tab != 0)
2114 wp = NULL;
2115 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002116 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002117 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2118 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002119 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2120 win_enter(wp, TRUE);
2121 else
2122 {
2123 /*
2124 * Split off help window; put it at far top if no position
2125 * specified, the current window is vertically split and narrow.
2126 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002127 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002128 if (cmdmod.split == 0 && curwin->w_width != Columns
2129 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002130 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002131 if (qi != &ql_info)
2132 flags |= WSP_NEWLOC; /* don't copy the location list */
2133
2134 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002135 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002136 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002137
2138 if (curwin->w_height < p_hh)
2139 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002140
2141 if (qi != &ql_info) /* not a quickfix list */
2142 {
2143 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002144 curwin->w_llist = qi;
2145 qi->qf_refcount++;
2146 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002147 }
2148
2149 if (!p_im)
2150 restart_edit = 0; /* don't want insert mode in help file */
2151 }
2152
2153 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 * If currently in the quickfix window, find another window to show the
2155 * file in.
2156 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002157 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002159 win_T *usable_win_ptr = NULL;
2160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 /*
2162 * If there is no file specified, we don't know where to go.
2163 * But do advance, otherwise ":cn" gets stuck.
2164 */
2165 if (qf_ptr->qf_fnum == 0)
2166 goto theend;
2167
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002168 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002169
2170 ll_ref = curwin->w_llist_ref;
2171 if (ll_ref != NULL)
2172 {
2173 /* Find a window using the same location list that is not a
2174 * quickfix window. */
2175 FOR_ALL_WINDOWS(usable_win_ptr)
2176 if (usable_win_ptr->w_llist == ll_ref
2177 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002178 {
2179 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002180 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002181 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002182 }
2183
2184 if (!usable_win)
2185 {
2186 /* Locate a window showing a normal buffer */
2187 FOR_ALL_WINDOWS(win)
2188 if (win->w_buffer->b_p_bt[0] == NUL)
2189 {
2190 usable_win = 1;
2191 break;
2192 }
2193 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002194
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002196 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002197 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002199 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002200 {
2201 tabpage_T *tp;
2202 win_T *wp;
2203
2204 FOR_ALL_TAB_WINDOWS(tp, wp)
2205 {
2206 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2207 {
2208 goto_tabpage_win(tp, wp);
2209 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002210 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002211 }
2212 }
2213 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002214win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002215
2216 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002217 * If there is only one window and it is the quickfix window, create a
2218 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002219 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002220 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002222 flags = WSP_ABOVE;
2223 if (ll_ref != NULL)
2224 flags |= WSP_NEWLOC;
2225 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 goto failed; /* not enough room for window */
2227 opened_window = TRUE; /* close it when fail */
2228 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002229 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002230 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002231 if (ll_ref != NULL)
2232 {
2233 /* The new window should use the location list from the
2234 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002235 curwin->w_llist = ll_ref;
2236 ll_ref->qf_refcount++;
2237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 }
2239 else
2240 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002241 if (curwin->w_llist_ref != NULL)
2242 {
2243 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002244 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002245 if (win == NULL)
2246 {
2247 /* Find the window showing the selected file */
2248 FOR_ALL_WINDOWS(win)
2249 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2250 break;
2251 if (win == NULL)
2252 {
2253 /* Find a previous usable window */
2254 win = curwin;
2255 do
2256 {
2257 if (win->w_buffer->b_p_bt[0] == NUL)
2258 break;
2259 if (win->w_prev == NULL)
2260 win = lastwin; /* wrap around the top */
2261 else
2262 win = win->w_prev; /* go to previous window */
2263 } while (win != curwin);
2264 }
2265 }
2266 win_goto(win);
2267
2268 /* If the location list for the window is not set, then set it
2269 * to the location list from the location window */
2270 if (win->w_llist == NULL)
2271 {
2272 win->w_llist = ll_ref;
2273 ll_ref->qf_refcount++;
2274 }
2275 }
2276 else
2277 {
2278
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 /*
2280 * Try to find a window that shows the right buffer.
2281 * Default to the window just above the quickfix buffer.
2282 */
2283 win = curwin;
2284 altwin = NULL;
2285 for (;;)
2286 {
2287 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2288 break;
2289 if (win->w_prev == NULL)
2290 win = lastwin; /* wrap around the top */
2291 else
2292 win = win->w_prev; /* go to previous window */
2293
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002294 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 /* Didn't find it, go to the window before the quickfix
2297 * window. */
2298 if (altwin != NULL)
2299 win = altwin;
2300 else if (curwin->w_prev != NULL)
2301 win = curwin->w_prev;
2302 else
2303 win = curwin->w_next;
2304 break;
2305 }
2306
2307 /* Remember a usable window. */
2308 if (altwin == NULL && !win->w_p_pvw
2309 && win->w_buffer->b_p_bt[0] == NUL)
2310 altwin = win;
2311 }
2312
2313 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316 }
2317#endif
2318
2319 /*
2320 * If there is a file name,
2321 * read the wanted file if needed, and check autowrite etc.
2322 */
2323 old_curbuf = curbuf;
2324 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002325
2326 if (qf_ptr->qf_fnum != 0)
2327 {
2328 if (qf_ptr->qf_type == 1)
2329 {
2330 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2331 * set b_p_ro flag). */
2332 if (!can_abandon(curbuf, forceit))
2333 {
2334 EMSG(_(e_nowrtmsg));
2335 ok = FALSE;
2336 }
2337 else
2338 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002339 ECMD_HIDE + ECMD_SET_HELP,
2340 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002341 }
2342 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002343 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002344 int old_qf_curlist = qi->qf_curlist;
2345 int is_abort = FALSE;
2346
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002347 ok = buflist_getfile(qf_ptr->qf_fnum,
2348 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002349 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002350 {
2351 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002352 is_abort = TRUE;
2353 opened_window = FALSE;
2354 }
2355 else if (old_qf_curlist != qi->qf_curlist
2356 || !is_qf_entry_present(qi, qf_ptr))
2357 {
2358 if (qi == &ql_info)
2359 EMSG(_("E925: Current quickfix was changed"));
2360 else
2361 EMSG(_("E926: Current location list was changed"));
2362 is_abort = TRUE;
2363 }
2364
2365 if (is_abort)
2366 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002367 ok = FALSE;
2368 qi = NULL;
2369 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002370 }
2371 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002372 }
2373
2374 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
2376 /* When not switched to another buffer, still need to set pc mark */
2377 if (curbuf == old_curbuf)
2378 setpcmark();
2379
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002380 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002382 /*
2383 * Go to line with error, unless qf_lnum is 0.
2384 */
2385 i = qf_ptr->qf_lnum;
2386 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002388 if (i > curbuf->b_ml.ml_line_count)
2389 i = curbuf->b_ml.ml_line_count;
2390 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002392 if (qf_ptr->qf_col > 0)
2393 {
2394 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002395#ifdef FEAT_VIRTUALEDIT
2396 curwin->w_cursor.coladd = 0;
2397#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002398 if (qf_ptr->qf_viscol == TRUE)
2399 {
2400 /*
2401 * Check each character from the beginning of the error
2402 * line up to the error column. For each tab character
2403 * found, reduce the error column value by the length of
2404 * a tab character.
2405 */
2406 line = ml_get_curline();
2407 screen_col = 0;
2408 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2409 {
2410 if (*line == NUL)
2411 break;
2412 if (*line++ == '\t')
2413 {
2414 curwin->w_cursor.col -= 7 - (screen_col % 8);
2415 screen_col += 8 - (screen_col % 8);
2416 }
2417 else
2418 ++screen_col;
2419 }
2420 }
2421 check_cursor();
2422 }
2423 else
2424 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002427 {
2428 pos_T save_cursor;
2429
2430 /* Move the cursor to the first line in the buffer */
2431 save_cursor = curwin->w_cursor;
2432 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002433 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002434 SEARCH_KEEP, NULL, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002435 curwin->w_cursor = save_cursor;
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438#ifdef FEAT_FOLDING
2439 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2440 foldOpenCursor();
2441#endif
2442 if (print_message)
2443 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002444 /* Update the screen before showing the message, unless the screen
2445 * scrolled up. */
2446 if (!msg_scrolled)
2447 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002449 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2451 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2452 /* Add the message, skipping leading whitespace and newlines. */
2453 len = (int)STRLEN(IObuff);
2454 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2455
2456 /* Output the message. Overwrite to avoid scrolling when the 'O'
2457 * flag is present in 'shortmess'; But when not jumping, print the
2458 * whole message. */
2459 i = msg_scroll;
2460 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2461 msg_scroll = TRUE;
2462 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2463 msg_scroll = FALSE;
2464 msg_attr_keep(IObuff, 0, TRUE);
2465 msg_scroll = i;
2466 }
2467 }
2468 else
2469 {
2470#ifdef FEAT_WINDOWS
2471 if (opened_window)
2472 win_close(curwin, TRUE); /* Close opened window */
2473#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002474 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
2476 /*
2477 * Couldn't open file, so put index back where it was. This could
2478 * happen if the file was readonly and we changed something.
2479 */
2480#ifdef FEAT_WINDOWS
2481failed:
2482#endif
2483 qf_ptr = old_qf_ptr;
2484 qf_index = old_qf_index;
2485 }
2486 }
2487theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002488 if (qi != NULL)
2489 {
2490 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2491 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493#ifdef FEAT_WINDOWS
2494 if (p_swb != old_swb && opened_window)
2495 {
2496 /* Restore old 'switchbuf' value, but not when an autocommand or
2497 * modeline has changed the value. */
2498 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002501 swb_flags = old_swb_flags;
2502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 else
2504 free_string_option(old_swb);
2505 }
2506#endif
2507}
2508
2509/*
2510 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002511 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 */
2513 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002514qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002516 buf_T *buf;
2517 char_u *fname;
2518 qfline_T *qfp;
2519 int i;
2520 int idx1 = 1;
2521 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002522 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002523 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002524 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002526 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002528 if (eap->cmdidx == CMD_llist)
2529 {
2530 qi = GET_LOC_LIST(curwin);
2531 if (qi == NULL)
2532 {
2533 EMSG(_(e_loclist));
2534 return;
2535 }
2536 }
2537
2538 if (qi->qf_curlist >= qi->qf_listcount
2539 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
2541 EMSG(_(e_quickfix));
2542 return;
2543 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002544 if (*arg == '+')
2545 {
2546 ++arg;
2547 plus = TRUE;
2548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2550 {
2551 EMSG(_(e_trailing));
2552 return;
2553 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002554 if (plus)
2555 {
2556 i = qi->qf_lists[qi->qf_curlist].qf_index;
2557 idx2 = i + idx1;
2558 idx1 = i;
2559 }
2560 else
2561 {
2562 i = qi->qf_lists[qi->qf_curlist].qf_count;
2563 if (idx1 < 0)
2564 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2565 if (idx2 < 0)
2566 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002569 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002571 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2572 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
2574 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2575 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002576 msg_putchar('\n');
2577 if (got_int)
2578 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002579
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002580 fname = NULL;
2581 if (qfp->qf_fnum != 0
2582 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2583 {
2584 fname = buf->b_fname;
2585 if (qfp->qf_type == 1) /* :helpgrep */
2586 fname = gettail(fname);
2587 }
2588 if (fname == NULL)
2589 sprintf((char *)IObuff, "%2d", i);
2590 else
2591 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2592 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002593 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar21020352017-06-13 17:21:04 +02002594 ? HL_ATTR(HLF_QFL) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002595 if (qfp->qf_lnum == 0)
2596 IObuff[0] = NUL;
2597 else if (qfp->qf_col == 0)
2598 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2599 else
2600 sprintf((char *)IObuff, ":%ld col %d",
2601 qfp->qf_lnum, qfp->qf_col);
2602 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2603 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002604 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002605 if (qfp->qf_pattern != NULL)
2606 {
2607 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2608 STRCAT(IObuff, ":");
2609 msg_puts(IObuff);
2610 }
2611 msg_puts((char_u *)" ");
2612
2613 /* Remove newlines and leading whitespace from the text. For an
2614 * unrecognized line keep the indent, the compiler may mark a word
2615 * with ^^^^. */
2616 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2618 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002619 msg_prt_line(IObuff, FALSE);
2620 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002622
2623 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002624 if (qfp == NULL)
2625 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002626 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 ui_breakcheck();
2628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629}
2630
2631/*
2632 * Remove newlines and leading whitespace from an error message.
2633 * Put the result in "buf[bufsize]".
2634 */
2635 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002636qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638 int i;
2639 char_u *p = text;
2640
2641 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2642 {
2643 if (*p == '\n')
2644 {
2645 buf[i] = ' ';
2646 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002647 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 break;
2649 }
2650 else
2651 buf[i] = *p++;
2652 }
2653 buf[i] = NUL;
2654}
2655
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002656 static void
2657qf_msg(qf_info_T *qi, int which, char *lead)
2658{
2659 char *title = (char *)qi->qf_lists[which].qf_title;
2660 int count = qi->qf_lists[which].qf_count;
2661 char_u buf[IOSIZE];
2662
2663 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2664 lead,
2665 which + 1,
2666 qi->qf_listcount,
2667 count);
2668
2669 if (title != NULL)
2670 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002671 size_t len = STRLEN(buf);
2672
2673 if (len < 34)
2674 {
2675 vim_memset(buf + len, ' ', 34 - len);
2676 buf[34] = NUL;
2677 }
2678 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002679 }
2680 trunc_string(buf, buf, Columns - 1, IOSIZE);
2681 msg(buf);
2682}
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684/*
2685 * ":colder [count]": Up in the quickfix stack.
2686 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002687 * ":lolder [count]": Up in the location list stack.
2688 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 */
2690 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002691qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002693 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 int count;
2695
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002696 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2697 {
2698 qi = GET_LOC_LIST(curwin);
2699 if (qi == NULL)
2700 {
2701 EMSG(_(e_loclist));
2702 return;
2703 }
2704 }
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (eap->addr_count != 0)
2707 count = eap->line2;
2708 else
2709 count = 1;
2710 while (count--)
2711 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002712 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002714 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 {
2716 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002717 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002719 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721 else
2722 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002723 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
2725 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002726 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002728 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002731 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002733 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734#endif
2735}
2736
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002737 void
2738qf_history(exarg_T *eap)
2739{
2740 qf_info_T *qi = &ql_info;
2741 int i;
2742
2743 if (eap->cmdidx == CMD_lhistory)
2744 qi = GET_LOC_LIST(curwin);
2745 if (qi == NULL || (qi->qf_listcount == 0
2746 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2747 MSG(_("No entries"));
2748 else
2749 for (i = 0; i < qi->qf_listcount; ++i)
2750 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2751}
2752
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002754 * Free all the entries in the error list "idx". Note that other information
2755 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 */
2757 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002758qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002760 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002761 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002762 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002764 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002766 qfp = qi->qf_lists[idx].qf_start;
2767 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002768 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002769 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002770 vim_free(qfp->qf_text);
2771 stop = (qfp == qfpnext);
2772 vim_free(qfp->qf_pattern);
2773 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002774 if (stop)
2775 /* Somehow qf_count may have an incorrect value, set it to 1
2776 * to avoid crashing when it's wrong.
2777 * TODO: Avoid qf_count being incorrect. */
2778 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002779 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002780 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002781 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002783
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002784 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002785 qi->qf_lists[idx].qf_start = NULL;
Bram Moolenaar31bdd132017-04-15 15:22:52 +02002786 qi->qf_lists[idx].qf_last = NULL;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002787 qi->qf_lists[idx].qf_ptr = NULL;
2788 qi->qf_lists[idx].qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002789
2790 qf_clean_dir_stack(&qi->qf_dir_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002791 qi->qf_directory = NULL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002792 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002793 qi->qf_currfile = NULL;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002794 qi->qf_multiline = FALSE;
2795 qi->qf_multiignore = FALSE;
2796 qi->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797}
2798
2799/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002800 * Free error list "idx". Frees all the entries in the quickfix list,
2801 * associated context information and the title.
2802 */
2803 static void
2804qf_free(qf_info_T *qi, int idx)
2805{
2806 qf_free_items(qi, idx);
2807
2808 vim_free(qi->qf_lists[idx].qf_title);
2809 qi->qf_lists[idx].qf_title = NULL;
2810 free_tv(qi->qf_lists[idx].qf_ctx);
2811 qi->qf_lists[idx].qf_ctx = NULL;
2812}
2813
2814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 * qf_mark_adjust: adjust marks
2816 */
2817 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002818qf_mark_adjust(
2819 win_T *wp,
2820 linenr_T line1,
2821 linenr_T line2,
2822 long amount,
2823 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002825 int i;
2826 qfline_T *qfp;
2827 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002828 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002829 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002830 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
Bram Moolenaarc1542742016-07-20 21:44:37 +02002832 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002833 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002834 if (wp != NULL)
2835 {
2836 if (wp->w_llist == NULL)
2837 return;
2838 qi = wp->w_llist;
2839 }
2840
2841 for (idx = 0; idx < qi->qf_listcount; ++idx)
2842 if (qi->qf_lists[idx].qf_count)
2843 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002844 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2845 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 if (qfp->qf_fnum == curbuf->b_fnum)
2847 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002848 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2850 {
2851 if (amount == MAXLNUM)
2852 qfp->qf_cleared = TRUE;
2853 else
2854 qfp->qf_lnum += amount;
2855 }
2856 else if (amount_after && qfp->qf_lnum > line2)
2857 qfp->qf_lnum += amount_after;
2858 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002859
2860 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002861 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862}
2863
2864/*
2865 * Make a nice message out of the error character and the error number:
2866 * char number message
2867 * e or E 0 " error"
2868 * w or W 0 " warning"
2869 * i or I 0 " info"
2870 * 0 0 ""
2871 * other 0 " c"
2872 * e or E n " error n"
2873 * w or W n " warning n"
2874 * i or I n " info n"
2875 * 0 n " error n"
2876 * other n " c n"
2877 * 1 x "" :helpgrep
2878 */
2879 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002880qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
2882 static char_u buf[20];
2883 static char_u cc[3];
2884 char_u *p;
2885
2886 if (c == 'W' || c == 'w')
2887 p = (char_u *)" warning";
2888 else if (c == 'I' || c == 'i')
2889 p = (char_u *)" info";
2890 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2891 p = (char_u *)" error";
2892 else if (c == 0 || c == 1)
2893 p = (char_u *)"";
2894 else
2895 {
2896 cc[0] = ' ';
2897 cc[1] = c;
2898 cc[2] = NUL;
2899 p = cc;
2900 }
2901
2902 if (nr <= 0)
2903 return p;
2904
2905 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2906 return buf;
2907}
2908
2909#if defined(FEAT_WINDOWS) || defined(PROTO)
2910/*
2911 * ":cwindow": open the quickfix window if we have errors to display,
2912 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002913 * ":lwindow": open the location list window if we have locations to display,
2914 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 */
2916 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002917ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 win_T *win;
2921
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002922 if (eap->cmdidx == CMD_lwindow)
2923 {
2924 qi = GET_LOC_LIST(curwin);
2925 if (qi == NULL)
2926 return;
2927 }
2928
2929 /* Look for an existing quickfix window. */
2930 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 /*
2933 * If a quickfix window is open but we have no errors to display,
2934 * close the window. If a quickfix window is not open, then open
2935 * it if we have errors; otherwise, leave it closed.
2936 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002937 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002938 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002939 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
2941 if (win != NULL)
2942 ex_cclose(eap);
2943 }
2944 else if (win == NULL)
2945 ex_copen(eap);
2946}
2947
2948/*
2949 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002950 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002953ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002955 win_T *win = NULL;
2956 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002958 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2959 {
2960 qi = GET_LOC_LIST(curwin);
2961 if (qi == NULL)
2962 return;
2963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002965 /* Find existing quickfix window and close it. */
2966 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 if (win != NULL)
2968 win_close(win, FALSE);
2969}
2970
2971/*
2972 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002973 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 */
2975 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002976ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002978 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002981 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002982 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002983 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002985 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2986 {
2987 qi = GET_LOC_LIST(curwin);
2988 if (qi == NULL)
2989 {
2990 EMSG(_(e_loclist));
2991 return;
2992 }
2993 }
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 if (eap->addr_count != 0)
2996 height = eap->line2;
2997 else
2998 height = QF_WINHEIGHT;
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001#ifdef FEAT_GUI
3002 need_mouse_correct = TRUE;
3003#endif
3004
3005 /*
3006 * Find existing quickfix window, or open a new one.
3007 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003008 win = qf_find_win(qi);
3009
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003010 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003013 if (eap->addr_count != 0)
3014 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003015 if (cmdmod.split & WSP_VERT)
3016 {
3017 if (height != W_WIDTH(win))
3018 win_setwidth(height);
3019 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003020 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003021 win_setheight(height);
3022 }
3023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 else
3025 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00003026 qf_buf = qf_find_buf(qi);
3027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 /* The current window becomes the previous window afterwards. */
3029 win = curwin;
3030
Bram Moolenaar77642c02012-11-20 17:55:10 +01003031 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3032 && cmdmod.split == 0)
3033 /* Create the new window at the very bottom, except when
3034 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003035 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003036 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003038 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003040 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003042 /*
3043 * For the location list window, create a reference to the
3044 * location list from the window 'win'.
3045 */
3046 curwin->w_llist_ref = win->w_llist;
3047 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003049
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003050 if (oldwin != curwin)
3051 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003052 if (qf_buf != NULL)
3053 /* Use the existing quickfix buffer */
3054 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003055 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003056 else
3057 {
3058 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003059 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003060 /* switch off 'swapfile' */
3061 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3062 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003063 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003064 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003065 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003066#ifdef FEAT_DIFF
3067 curwin->w_p_diff = FALSE;
3068#endif
3069#ifdef FEAT_FOLDING
3070 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3071 OPT_LOCAL);
3072#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003075 /* Only set the height when still in the same tab page and there is no
3076 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003077 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 win_setheight(height);
3079 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3080 if (win_valid(win))
3081 prevwin = win;
3082 }
3083
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003084 qf_set_title_var(qi);
3085
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 /*
3087 * Fill the buffer with the quickfix list.
3088 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003089 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003091 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 curwin->w_cursor.col = 0;
3093 check_cursor();
3094 update_topline(); /* scroll to show the line */
3095}
3096
3097/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003098 * Move the cursor in the quickfix window to "lnum".
3099 */
3100 static void
3101qf_win_goto(win_T *win, linenr_T lnum)
3102{
3103 win_T *old_curwin = curwin;
3104
3105 curwin = win;
3106 curbuf = win->w_buffer;
3107 curwin->w_cursor.lnum = lnum;
3108 curwin->w_cursor.col = 0;
3109#ifdef FEAT_VIRTUALEDIT
3110 curwin->w_cursor.coladd = 0;
3111#endif
3112 curwin->w_curswant = 0;
3113 update_topline(); /* scroll to show the line */
3114 redraw_later(VALID);
3115 curwin->w_redr_status = TRUE; /* update ruler */
3116 curwin = old_curwin;
3117 curbuf = curwin->w_buffer;
3118}
3119
3120/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003121 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003122 */
3123 void
3124ex_cbottom(exarg_T *eap UNUSED)
3125{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003126 qf_info_T *qi = &ql_info;
3127 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003128
Bram Moolenaar537ef082016-07-09 17:56:19 +02003129 if (eap->cmdidx == CMD_lbottom)
3130 {
3131 qi = GET_LOC_LIST(curwin);
3132 if (qi == NULL)
3133 {
3134 EMSG(_(e_loclist));
3135 return;
3136 }
3137 }
3138
3139 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003140 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3141 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3142}
3143
3144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 * Return the number of the current entry (line number in the quickfix
3146 * window).
3147 */
3148 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003149qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003151 qf_info_T *qi = &ql_info;
3152
3153 if (IS_LL_WINDOW(wp))
3154 /* In the location list window, use the referenced location list */
3155 qi = wp->w_llist_ref;
3156
3157 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158}
3159
3160/*
3161 * Update the cursor position in the quickfix window to the current error.
3162 * Return TRUE if there is a quickfix window.
3163 */
3164 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003165qf_win_pos_update(
3166 qf_info_T *qi,
3167 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003170 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
3172 /*
3173 * Put the cursor on the current error in the quickfix window, so that
3174 * it's viewable.
3175 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003176 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 if (win != NULL
3178 && qf_index <= win->w_buffer->b_ml.ml_line_count
3179 && old_qf_index != qf_index)
3180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (qf_index > old_qf_index)
3182 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003183 win->w_redraw_top = old_qf_index;
3184 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186 else
3187 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003188 win->w_redraw_top = qf_index;
3189 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003191 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
3193 return win != NULL;
3194}
3195
3196/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003197 * Check whether the given window is displaying the specified quickfix/location
3198 * list buffer
3199 */
3200 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003201is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003202{
3203 /*
3204 * A window displaying the quickfix buffer will have the w_llist_ref field
3205 * set to NULL.
3206 * A window displaying a location list buffer will have the w_llist_ref
3207 * pointing to the location list.
3208 */
3209 if (bt_quickfix(win->w_buffer))
3210 if ((qi == &ql_info && win->w_llist_ref == NULL)
3211 || (qi != &ql_info && win->w_llist_ref == qi))
3212 return TRUE;
3213
3214 return FALSE;
3215}
3216
3217/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003218 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003219 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003220 */
3221 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003222qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003223{
3224 win_T *win;
3225
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003226 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003227 if (is_qf_win(win, qi))
3228 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003229
3230 return win;
3231}
3232
3233/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003234 * Find a quickfix buffer.
3235 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 */
3237 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003238qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003240 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003241 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
Bram Moolenaar9c102382006-05-03 21:26:49 +00003243 FOR_ALL_TAB_WINDOWS(tp, win)
3244 if (is_qf_win(win, qi))
3245 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003246
Bram Moolenaar9c102382006-05-03 21:26:49 +00003247 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248}
3249
3250/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003251 * Update the w:quickfix_title variable in the quickfix/location list window
3252 */
3253 static void
3254qf_update_win_titlevar(qf_info_T *qi)
3255{
3256 win_T *win;
3257 win_T *curwin_save;
3258
3259 if ((win = qf_find_win(qi)) != NULL)
3260 {
3261 curwin_save = curwin;
3262 curwin = win;
3263 qf_set_title_var(qi);
3264 curwin = curwin_save;
3265 }
3266}
3267
3268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 * Find the quickfix buffer. If it exists, update the contents.
3270 */
3271 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003272qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003275 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
3278 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003279 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (buf != NULL)
3281 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003282 linenr_T old_line_count = buf->b_ml.ml_line_count;
3283
3284 if (old_last == NULL)
3285 /* set curwin/curbuf to buf and save a few things */
3286 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
Bram Moolenaard823fa92016-08-12 16:29:27 +02003288 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003289
Bram Moolenaar864293a2016-06-02 13:40:04 +02003290 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003291
Bram Moolenaar864293a2016-06-02 13:40:04 +02003292 if (old_last == NULL)
3293 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003294 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003295
3296 /* restore curwin/curbuf and a few other things */
3297 aucmd_restbuf(&aco);
3298 }
3299
3300 /* Only redraw when added lines are visible. This avoids flickering
3301 * when the added lines are not visible. */
3302 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3303 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
3305}
3306
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003307/*
3308 * Set "w:quickfix_title" if "qi" has a title.
3309 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003310 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003311qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003312{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003313 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3314 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003315 qi->qf_lists[qi->qf_curlist].qf_title);
3316}
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318/*
3319 * Fill current buffer with quickfix errors, replacing any previous contents.
3320 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003321 * If "old_last" is not NULL append the items after this one.
3322 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3323 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 */
3325 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003326qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003328 linenr_T lnum;
3329 qfline_T *qfp;
3330 buf_T *errbuf;
3331 int len;
3332 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar864293a2016-06-02 13:40:04 +02003334 if (old_last == NULL)
3335 {
3336 if (buf != curbuf)
3337 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003338 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003339 return;
3340 }
3341
3342 /* delete all existing lines */
3343 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3344 (void)ml_delete((linenr_T)1, FALSE);
3345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
3347 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003348 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003351 if (old_last == NULL)
3352 {
3353 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3354 lnum = 0;
3355 }
3356 else
3357 {
3358 qfp = old_last->qf_next;
3359 lnum = buf->b_ml.ml_line_count;
3360 }
3361 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
3363 if (qfp->qf_fnum != 0
3364 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3365 && errbuf->b_fname != NULL)
3366 {
3367 if (qfp->qf_type == 1) /* :helpgrep */
3368 STRCPY(IObuff, gettail(errbuf->b_fname));
3369 else
3370 STRCPY(IObuff, errbuf->b_fname);
3371 len = (int)STRLEN(IObuff);
3372 }
3373 else
3374 len = 0;
3375 IObuff[len++] = '|';
3376
3377 if (qfp->qf_lnum > 0)
3378 {
3379 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3380 len += (int)STRLEN(IObuff + len);
3381
3382 if (qfp->qf_col > 0)
3383 {
3384 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3385 len += (int)STRLEN(IObuff + len);
3386 }
3387
3388 sprintf((char *)IObuff + len, "%s",
3389 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3390 len += (int)STRLEN(IObuff + len);
3391 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003392 else if (qfp->qf_pattern != NULL)
3393 {
3394 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3395 len += (int)STRLEN(IObuff + len);
3396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 IObuff[len++] = '|';
3398 IObuff[len++] = ' ';
3399
3400 /* Remove newlines and leading whitespace from the text.
3401 * For an unrecognized line keep the indent, the compiler may
3402 * mark a word with ^^^^. */
3403 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3404 IObuff + len, IOSIZE - len);
3405
Bram Moolenaar864293a2016-06-02 13:40:04 +02003406 if (ml_append_buf(buf, lnum, IObuff,
3407 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003409 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003411 if (qfp == NULL)
3412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003414
3415 if (old_last == NULL)
3416 /* Delete the empty line which is now at the end */
3417 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
3419
3420 /* correct cursor position */
3421 check_lnums(TRUE);
3422
Bram Moolenaar864293a2016-06-02 13:40:04 +02003423 if (old_last == NULL)
3424 {
3425 /* Set the 'filetype' to "qf" each time after filling the buffer.
3426 * This resembles reading a file into a buffer, it's more logical when
3427 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003428#ifdef FEAT_AUTOCMD
3429 ++curbuf_lock;
3430#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003431 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3432 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003435 keep_filetype = TRUE; /* don't detect 'filetype' */
3436 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003438 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003440 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003441 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003443 /* make sure it will be redrawn */
3444 redraw_curbuf_later(NOT_VALID);
3445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 /* Restore KeyTyped, setting 'filetype' may reset it. */
3448 KeyTyped = old_KeyTyped;
3449}
3450
3451#endif /* FEAT_WINDOWS */
3452
3453/*
3454 * Return TRUE if "buf" is the quickfix buffer.
3455 */
3456 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003457bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003459 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460}
3461
3462/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003463 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3464 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 */
3466 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003467bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003469 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3470 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471}
3472
3473/*
3474 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3475 */
3476 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003477bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003479 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480}
3481
3482 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003483bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484{
3485 if (bt_dontwrite(buf))
3486 {
3487 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3488 return TRUE;
3489 }
3490 return FALSE;
3491}
3492
3493/*
3494 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3495 * and 'bufhidden'.
3496 */
3497 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003498buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
3500 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3501 switch (buf->b_p_bh[0])
3502 {
3503 case 'u': /* "unload" */
3504 case 'w': /* "wipe" */
3505 case 'd': return FALSE; /* "delete" */
3506 case 'h': return TRUE; /* "hide" */
3507 }
3508 return (p_hid || cmdmod.hide);
3509}
3510
3511/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003512 * Return TRUE when using ":vimgrep" for ":grep".
3513 */
3514 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003515grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003516{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003517 return ((cmdidx == CMD_grep
3518 || cmdidx == CMD_lgrep
3519 || cmdidx == CMD_grepadd
3520 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003521 && STRCMP("internal",
3522 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3523}
3524
3525/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003526 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 */
3528 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003529ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003531 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003533 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003535 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003536 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003537 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003538#ifdef FEAT_AUTOCMD
3539 char_u *au_name = NULL;
3540
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003541 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3542 if (grep_internal(eap->cmdidx))
3543 {
3544 ex_vimgrep(eap);
3545 return;
3546 }
3547
Bram Moolenaar7c626922005-02-07 22:01:03 +00003548 switch (eap->cmdidx)
3549 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003550 case CMD_make: au_name = (char_u *)"make"; break;
3551 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3552 case CMD_grep: au_name = (char_u *)"grep"; break;
3553 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3554 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3555 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003556 default: break;
3557 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003558 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3559 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003560 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003561# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003562 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003563 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003564# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003565 }
3566#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003567#ifdef FEAT_MBYTE
3568 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
Bram Moolenaara6557602006-02-04 22:43:20 +00003571 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3572 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003573 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003576 fname = get_mef_name();
3577 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003579 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
3581 /*
3582 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3583 */
3584 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3585 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003586 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 cmd = alloc(len);
3588 if (cmd == NULL)
3589 return;
3590 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3591 (char *)p_shq);
3592 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003593 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 /*
3595 * Output a newline if there's something else than the :make command that
3596 * was typed (in which case the cursor is in column 0).
3597 */
3598 if (msg_col == 0)
3599 msg_didout = FALSE;
3600 msg_start();
3601 MSG_PUTS(":!");
3602 msg_outtrans(cmd); /* show what we are doing */
3603
3604 /* let the shell know if we are redirecting output or not */
3605 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3606
3607#ifdef AMIGA
3608 out_flush();
3609 /* read window status report and redraw before message */
3610 (void)char_avail();
3611#endif
3612
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003613 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003614 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3615 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003616 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003617 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003618 if (wp != NULL)
3619 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003620#ifdef FEAT_AUTOCMD
3621 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003622 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003623 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3624 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003625 if (qi->qf_curlist < qi->qf_listcount)
3626 res = qi->qf_lists[qi->qf_curlist].qf_count;
3627 else
3628 res = 0;
3629 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003630#endif
3631 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003632 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
Bram Moolenaar7c626922005-02-07 22:01:03 +00003634 mch_remove(fname);
3635 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 vim_free(cmd);
3637}
3638
3639/*
3640 * Return the name for the errorfile, in allocated memory.
3641 * Find a new unique name when 'makeef' contains "##".
3642 * Returns NULL for error.
3643 */
3644 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003645get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 char_u *p;
3648 char_u *name;
3649 static int start = -1;
3650 static int off = 0;
3651#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003652 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653#endif
3654
3655 if (*p_mef == NUL)
3656 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003657 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 if (name == NULL)
3659 EMSG(_(e_notmp));
3660 return name;
3661 }
3662
3663 for (p = p_mef; *p; ++p)
3664 if (p[0] == '#' && p[1] == '#')
3665 break;
3666
3667 if (*p == NUL)
3668 return vim_strsave(p_mef);
3669
3670 /* Keep trying until the name doesn't exist yet. */
3671 for (;;)
3672 {
3673 if (start == -1)
3674 start = mch_get_pid();
3675 else
3676 off += 19;
3677
3678 name = alloc((unsigned)STRLEN(p_mef) + 30);
3679 if (name == NULL)
3680 break;
3681 STRCPY(name, p_mef);
3682 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3683 STRCAT(name, p + 2);
3684 if (mch_getperm(name) < 0
3685#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003686 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 && mch_lstat((char *)name, &sb) < 0
3688#endif
3689 )
3690 break;
3691 vim_free(name);
3692 }
3693 return name;
3694}
3695
3696/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003697 * Returns the number of valid entries in the current quickfix/location list.
3698 */
3699 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003700qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003701{
3702 qf_info_T *qi = &ql_info;
3703 qfline_T *qfp;
3704 int i, sz = 0;
3705 int prev_fnum = 0;
3706
3707 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3708 {
3709 /* Location list */
3710 qi = GET_LOC_LIST(curwin);
3711 if (qi == NULL)
3712 return 0;
3713 }
3714
3715 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003716 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003717 ++i, qfp = qfp->qf_next)
3718 {
3719 if (qfp->qf_valid)
3720 {
3721 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3722 sz++; /* Count all valid entries */
3723 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3724 {
3725 /* Count the number of files */
3726 sz++;
3727 prev_fnum = qfp->qf_fnum;
3728 }
3729 }
3730 }
3731
3732 return sz;
3733}
3734
3735/*
3736 * Returns the current index of the quickfix/location list.
3737 * Returns 0 if there is an error.
3738 */
3739 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003740qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003741{
3742 qf_info_T *qi = &ql_info;
3743
3744 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3745 {
3746 /* Location list */
3747 qi = GET_LOC_LIST(curwin);
3748 if (qi == NULL)
3749 return 0;
3750 }
3751
3752 return qi->qf_lists[qi->qf_curlist].qf_index;
3753}
3754
3755/*
3756 * Returns the current index in the quickfix/location list (counting only valid
3757 * entries). If no valid entries are in the list, then returns 1.
3758 */
3759 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003760qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003761{
3762 qf_info_T *qi = &ql_info;
3763 qf_list_T *qfl;
3764 qfline_T *qfp;
3765 int i, eidx = 0;
3766 int prev_fnum = 0;
3767
3768 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3769 {
3770 /* Location list */
3771 qi = GET_LOC_LIST(curwin);
3772 if (qi == NULL)
3773 return 1;
3774 }
3775
3776 qfl = &qi->qf_lists[qi->qf_curlist];
3777 qfp = qfl->qf_start;
3778
3779 /* check if the list has valid errors */
3780 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3781 return 1;
3782
3783 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3784 {
3785 if (qfp->qf_valid)
3786 {
3787 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3788 {
3789 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3790 {
3791 /* Count the number of files */
3792 eidx++;
3793 prev_fnum = qfp->qf_fnum;
3794 }
3795 }
3796 else
3797 eidx++;
3798 }
3799 }
3800
3801 return eidx ? eidx : 1;
3802}
3803
3804/*
3805 * Get the 'n'th valid error entry in the quickfix or location list.
3806 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3807 * For :cdo and :ldo returns the 'n'th valid error entry.
3808 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3809 */
3810 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003811qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003812{
3813 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3814 qfline_T *qfp = qfl->qf_start;
3815 int i, eidx;
3816 int prev_fnum = 0;
3817
3818 /* check if the list has valid errors */
3819 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3820 return 1;
3821
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003822 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003823 i++, qfp = qfp->qf_next)
3824 {
3825 if (qfp->qf_valid)
3826 {
3827 if (fdo)
3828 {
3829 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3830 {
3831 /* Count the number of files */
3832 eidx++;
3833 prev_fnum = qfp->qf_fnum;
3834 }
3835 }
3836 else
3837 eidx++;
3838 }
3839
3840 if (eidx == n)
3841 break;
3842 }
3843
3844 if (i <= qfl->qf_count)
3845 return i;
3846 else
3847 return 1;
3848}
3849
3850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003852 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003853 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 */
3855 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003856ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003858 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003859 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003860
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003861 if (eap->cmdidx == CMD_ll
3862 || eap->cmdidx == CMD_lrewind
3863 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003864 || eap->cmdidx == CMD_llast
3865 || eap->cmdidx == CMD_ldo
3866 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003867 {
3868 qi = GET_LOC_LIST(curwin);
3869 if (qi == NULL)
3870 {
3871 EMSG(_(e_loclist));
3872 return;
3873 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003874 }
3875
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003876 if (eap->addr_count > 0)
3877 errornr = (int)eap->line2;
3878 else
3879 {
3880 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3881 errornr = 0;
3882 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3883 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3884 errornr = 1;
3885 else
3886 errornr = 32767;
3887 }
3888
3889 /* For cdo and ldo commands, jump to the nth valid error.
3890 * For cfdo and lfdo commands, jump to the nth valid file entry.
3891 */
3892 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3893 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3894 errornr = qf_get_nth_valid_entry(qi,
3895 eap->addr_count > 0 ? (int)eap->line1 : 1,
3896 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3897
3898 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899}
3900
3901/*
3902 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003903 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003904 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 */
3906 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003907ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003909 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003910 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003911
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003912 if (eap->cmdidx == CMD_lnext
3913 || eap->cmdidx == CMD_lNext
3914 || eap->cmdidx == CMD_lprevious
3915 || eap->cmdidx == CMD_lnfile
3916 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003917 || eap->cmdidx == CMD_lpfile
3918 || eap->cmdidx == CMD_ldo
3919 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003920 {
3921 qi = GET_LOC_LIST(curwin);
3922 if (qi == NULL)
3923 {
3924 EMSG(_(e_loclist));
3925 return;
3926 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003927 }
3928
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003929 if (eap->addr_count > 0 &&
3930 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3931 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3932 errornr = (int)eap->line2;
3933 else
3934 errornr = 1;
3935
3936 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3937 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003939 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3940 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003942 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3943 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 ? BACKWARD_FILE
3945 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003946 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947}
3948
3949/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003950 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003951 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 */
3953 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003954ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003956 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003957 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003958 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003959#ifdef FEAT_AUTOCMD
3960 char_u *au_name = NULL;
3961#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003962
3963 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003964 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003965 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003966
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003967#ifdef FEAT_AUTOCMD
3968 switch (eap->cmdidx)
3969 {
3970 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3971 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3972 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3973 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3974 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3975 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3976 default: break;
3977 }
3978 if (au_name != NULL)
3979 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3980#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003981#ifdef FEAT_MBYTE
3982 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3983#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003984#ifdef FEAT_BROWSE
3985 if (cmdmod.browse)
3986 {
3987 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3988 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3989 if (browse_file == NULL)
3990 return;
3991 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3992 vim_free(browse_file);
3993 }
3994 else
3995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003997 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003998
3999 /*
4000 * This function is used by the :cfile, :cgetfile and :caddfile
4001 * commands.
4002 * :cfile always creates a new quickfix list and jumps to the
4003 * first error.
4004 * :cgetfile creates a new quickfix list but doesn't jump to the
4005 * first error.
4006 * :caddfile adds to an existing quickfix list. If there is no
4007 * quickfix list then a new list is created.
4008 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004009 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004010 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004011 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004012 && (eap->cmdidx == CMD_cfile
4013 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004014 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004015#ifdef FEAT_AUTOCMD
4016 if (au_name != NULL)
4017 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4018#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004019 if (wp != NULL)
4020 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004021 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004022 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004023
4024 else
4025 {
4026#ifdef FEAT_AUTOCMD
4027 if (au_name != NULL)
4028 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4029#endif
4030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031}
4032
4033/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004034 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004035 * ":vimgrepadd {pattern} file(s)"
4036 * ":lvimgrep {pattern} file(s)"
4037 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004038 */
4039 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004040ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004041{
Bram Moolenaar81695252004-12-29 20:58:21 +00004042 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004043 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004044 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004045 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004046 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004047 char_u *s;
4048 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004049 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004050 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004051#ifdef FEAT_AUTOCMD
4052 qfline_T *cur_qf_start;
4053#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004054 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004055 buf_T *buf;
4056 int duplicate_name = FALSE;
4057 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004058 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004059 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004060 buf_T *first_match_buf = NULL;
4061 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004062 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004063#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4064 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004065#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004066 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004067 int flags = 0;
4068 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004069 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004070 char_u *dirname_start = NULL;
4071 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004072 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004073#ifdef FEAT_AUTOCMD
4074 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004075
4076 switch (eap->cmdidx)
4077 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004078 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4079 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4080 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004081 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004082 case CMD_grep: au_name = (char_u *)"grep"; break;
4083 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4084 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4085 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004086 default: break;
4087 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004088 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4089 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004090 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004091# ifdef FEAT_EVAL
4092 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004093 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004094# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004095 }
4096#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004097
Bram Moolenaar754b5602006-02-09 23:53:20 +00004098 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004099 || eap->cmdidx == CMD_lvimgrep
4100 || eap->cmdidx == CMD_lgrepadd
4101 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004102 {
4103 qi = ll_get_or_alloc_list(curwin);
4104 if (qi == NULL)
4105 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004106 }
4107
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004108 if (eap->addr_count > 0)
4109 tomatch = eap->line2;
4110 else
4111 tomatch = MAXLNUM;
4112
Bram Moolenaar81695252004-12-29 20:58:21 +00004113 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004114 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004115 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004116 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004117 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004118 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004119 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004120 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004121 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004122
4123 if (s != NULL && *s == NUL)
4124 {
4125 /* Pattern is empty, use last search pattern. */
4126 if (last_search_pat() == NULL)
4127 {
4128 EMSG(_(e_noprevre));
4129 goto theend;
4130 }
4131 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4132 }
4133 else
4134 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4135
Bram Moolenaar86b68352004-12-27 21:59:20 +00004136 if (regmatch.regprog == NULL)
4137 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004138 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004139 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004140
4141 p = skipwhite(p);
4142 if (*p == NUL)
4143 {
4144 EMSG(_("E683: File name missing or invalid pattern"));
4145 goto theend;
4146 }
4147
Bram Moolenaar754b5602006-02-09 23:53:20 +00004148 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004149 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004150 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004151 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004152 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004153
4154 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004155 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004156 goto theend;
4157 if (fcount == 0)
4158 {
4159 EMSG(_(e_nomatch));
4160 goto theend;
4161 }
4162
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004163 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4164 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004165 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004166 {
4167 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004168 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004169 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004170
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004171 /* Remember the current directory, because a BufRead autocommand that does
4172 * ":lcd %:p:h" changes the meaning of short path names. */
4173 mch_dirname(dirname_start, MAXPATHL);
4174
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004175#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004176 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004177 * changing the current quickfix list. */
4178 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4179#endif
4180
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004181 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004182 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004183 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004184 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004185 if (time(NULL) > seconds)
4186 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004187 /* Display the file name every second or so, show the user we are
4188 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004189 seconds = time(NULL);
4190 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004191 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004192 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004193 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004194 else
4195 {
4196 msg_outtrans(p);
4197 vim_free(p);
4198 }
4199 msg_clr_eos();
4200 msg_didout = FALSE; /* overwrite this message */
4201 msg_nowait = TRUE; /* don't wait for this message */
4202 msg_col = 0;
4203 out_flush();
4204 }
4205
Bram Moolenaar81695252004-12-29 20:58:21 +00004206 buf = buflist_findname_exp(fnames[fi]);
4207 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4208 {
4209 /* Remember that a buffer with this name already exists. */
4210 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004211 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004212 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004213
4214#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4215 /* Don't do Filetype autocommands to avoid loading syntax and
4216 * indent scripts, a great speed improvement. */
4217 save_ei = au_event_disable(",Filetype");
4218#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004219 /* Don't use modelines here, it's useless. */
4220 save_mls = p_mls;
4221 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004222
4223 /* Load file into a buffer, so that 'fileencoding' is detected,
4224 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004225 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004226
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004227 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004228#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4229 au_event_restore(save_ei);
4230#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004231 }
4232 else
4233 /* Use existing, loaded buffer. */
4234 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004235
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004236#ifdef FEAT_AUTOCMD
4237 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4238 {
4239 int idx;
4240
4241 /* Autocommands changed the quickfix list. Find the one we were
4242 * using and restore it. */
4243 for (idx = 0; idx < LISTCOUNT; ++idx)
4244 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4245 {
4246 qi->qf_curlist = idx;
4247 break;
4248 }
4249 if (idx == LISTCOUNT)
4250 {
4251 /* List cannot be found, create a new one. */
4252 qf_new_list(qi, *eap->cmdlinep);
4253 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4254 }
4255 }
4256#endif
4257
Bram Moolenaar81695252004-12-29 20:58:21 +00004258 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004259 {
4260 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004261 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004262 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004263 else
4264 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004265 /* Try for a match in all lines of the buffer.
4266 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004267 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004268 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4269 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004270 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004271 col = 0;
4272 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004273 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004274 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004275 /* Pass the buffer number so that it gets used even for a
4276 * dummy buffer, unless duplicate_name is set, then the
4277 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004278 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004279 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004280 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004281 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004282 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004283 ml_get_buf(buf,
4284 regmatch.startpos[0].lnum + lnum, FALSE),
4285 regmatch.startpos[0].lnum + lnum,
4286 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004287 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004288 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004289 0, /* nr */
4290 0, /* type */
4291 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004292 ) == FAIL)
4293 {
4294 got_int = TRUE;
4295 break;
4296 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004297 found_match = TRUE;
4298 if (--tomatch == 0)
4299 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004300 if ((flags & VGR_GLOBAL) == 0
4301 || regmatch.endpos[0].lnum > 0)
4302 break;
4303 col = regmatch.endpos[0].col
4304 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004305 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004306 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004307 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004308 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004309 if (got_int)
4310 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004311 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004312#ifdef FEAT_AUTOCMD
4313 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4314#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004315
4316 if (using_dummy)
4317 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004318 if (found_match && first_match_buf == NULL)
4319 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004320 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004321 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004322 /* Never keep a dummy buffer if there is another buffer
4323 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004324 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004325 buf = NULL;
4326 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004327 else if (!cmdmod.hide
4328 || buf->b_p_bh[0] == 'u' /* "unload" */
4329 || buf->b_p_bh[0] == 'w' /* "wipe" */
4330 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004331 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004332 /* When no match was found we don't need to remember the
4333 * buffer, wipe it out. If there was a match and it
4334 * wasn't the first one or we won't jump there: only
4335 * unload the buffer.
4336 * Ignore 'hidden' here, because it may lead to having too
4337 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004338 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004339 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004340 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004341 buf = NULL;
4342 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004343 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004344 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004345 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004346 /* Keeping the buffer, remove the dummy flag. */
4347 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004348 buf = NULL;
4349 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004350 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004351
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004352 if (buf != NULL)
4353 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004354 /* Keeping the buffer, remove the dummy flag. */
4355 buf->b_flags &= ~BF_DUMMY;
4356
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004357 /* If the buffer is still loaded we need to use the
4358 * directory we jumped to below. */
4359 if (buf == first_match_buf
4360 && target_dir == NULL
4361 && STRCMP(dirname_start, dirname_now) != 0)
4362 target_dir = vim_strsave(dirname_now);
4363
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004364 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004365 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004366 * need to be done (again). But not the window-local
4367 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004368 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004369#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004370 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4371 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004372#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004373 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004374 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004375 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004376 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004377 }
4378 }
4379
4380 FreeWild(fcount, fnames);
4381
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004382 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4383 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4384 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004385
4386#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004387 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004388#endif
4389
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004390#ifdef FEAT_AUTOCMD
4391 if (au_name != NULL)
4392 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4393 curbuf->b_fname, TRUE, curbuf);
4394#endif
4395
Bram Moolenaar86b68352004-12-27 21:59:20 +00004396 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004397 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004398 {
4399 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004400 {
4401 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004402 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004403 if (buf != curbuf)
4404 /* If we jumped to another buffer redrawing will already be
4405 * taken care of. */
4406 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004407
4408 /* Jump to the directory used after loading the buffer. */
4409 if (curbuf == first_match_buf && target_dir != NULL)
4410 {
4411 exarg_T ea;
4412
4413 ea.arg = target_dir;
4414 ea.cmdidx = CMD_lcd;
4415 ex_cd(&ea);
4416 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004417 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004418 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004419 else
4420 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004421
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004422 /* If we loaded a dummy buffer into the current window, the autocommands
4423 * may have messed up things, need to redraw and recompute folds. */
4424 if (redraw_for_dummy)
4425 {
4426#ifdef FEAT_FOLDING
4427 foldUpdateAll(curwin);
4428#else
4429 redraw_later(NOT_VALID);
4430#endif
4431 }
4432
Bram Moolenaar86b68352004-12-27 21:59:20 +00004433theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004434 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004435 vim_free(dirname_now);
4436 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004437 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004438 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004439}
4440
4441/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004442 * Restore current working directory to "dirname_start" if they differ, taking
4443 * into account whether it is set locally or globally.
4444 */
4445 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004446restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004447{
4448 char_u *dirname_now = alloc(MAXPATHL);
4449
4450 if (NULL != dirname_now)
4451 {
4452 mch_dirname(dirname_now, MAXPATHL);
4453 if (STRCMP(dirname_start, dirname_now) != 0)
4454 {
4455 /* If the directory has changed, change it back by building up an
4456 * appropriate ex command and executing it. */
4457 exarg_T ea;
4458
4459 ea.arg = dirname_start;
4460 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4461 ex_cd(&ea);
4462 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004463 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004464 }
4465}
4466
4467/*
4468 * Load file "fname" into a dummy buffer and return the buffer pointer,
4469 * placing the directory resulting from the buffer load into the
4470 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4471 * prior to calling this function. Restores directory to "dirname_start" prior
4472 * to returning, if autocmds or the 'autochdir' option have changed it.
4473 *
4474 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4475 * or wipe_dummy_buffer() later!
4476 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004477 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004478 */
4479 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004480load_dummy_buffer(
4481 char_u *fname,
4482 char_u *dirname_start, /* in: old directory */
4483 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004484{
4485 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004486 bufref_T newbufref;
4487 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004488 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004489 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004490
4491 /* Allocate a buffer without putting it in the buffer list. */
4492 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4493 if (newbuf == NULL)
4494 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004495 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004496
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004497 /* Init the options. */
4498 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4499
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004500 /* need to open the memfile before putting the buffer in a window */
4501 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004502 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004503 /* set curwin/curbuf to buf and save a few things */
4504 aucmd_prepbuf(&aco, newbuf);
4505
4506 /* Need to set the filename for autocommands. */
4507 (void)setfname(curbuf, fname, NULL, FALSE);
4508
Bram Moolenaar81695252004-12-29 20:58:21 +00004509 /* Create swap file now to avoid the ATTENTION message. */
4510 check_need_swap(TRUE);
4511
4512 /* Remove the "dummy" flag, otherwise autocommands may not
4513 * work. */
4514 curbuf->b_flags &= ~BF_DUMMY;
4515
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004516 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004517 if (readfile(fname, NULL,
4518 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4519 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004520 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004521 && !(curbuf->b_flags & BF_NEW))
4522 {
4523 failed = FALSE;
4524 if (curbuf != newbuf)
4525 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004526 /* Bloody autocommands changed the buffer! Can happen when
4527 * using netrw and editing a remote file. Use the current
4528 * buffer instead, delete the dummy one after restoring the
4529 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004530 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004531 newbuf = curbuf;
4532 }
4533 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004534
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004535 /* restore curwin/curbuf and a few other things */
4536 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004537 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4538 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004539
4540 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4541 * skip it. */
4542 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004543 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004544
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004545 /*
4546 * When autocommands/'autochdir' option changed directory: go back.
4547 * Let the caller know what the resulting dir was first, in case it is
4548 * important.
4549 */
4550 mch_dirname(resulting_dir, MAXPATHL);
4551 restore_start_dir(dirname_start);
4552
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004553 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004554 return NULL;
4555 if (failed)
4556 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004557 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004558 return NULL;
4559 }
4560 return newbuf;
4561}
4562
4563/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004564 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4565 * directory to "dirname_start" prior to returning, if autocmds or the
4566 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004567 */
4568 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004569wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004570{
4571 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004572 {
4573#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4574 cleanup_T cs;
4575
4576 /* Reset the error/interrupt/exception state here so that aborting()
4577 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4578 * work when got_int is set. */
4579 enter_cleanup(&cs);
4580#endif
4581
Bram Moolenaar81695252004-12-29 20:58:21 +00004582 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004583
4584#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4585 /* Restore the error/interrupt/exception state if not discarded by a
4586 * new aborting error, interrupt, or uncaught exception. */
4587 leave_cleanup(&cs);
4588#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004589 /* When autocommands/'autochdir' option changed directory: go back. */
4590 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004591 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004592}
4593
4594/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004595 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4596 * directory to "dirname_start" prior to returning, if autocmds or the
4597 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004598 */
4599 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004600unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004601{
4602 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004603 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004604 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004605
4606 /* When autocommands/'autochdir' option changed directory: go back. */
4607 restore_start_dir(dirname_start);
4608 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004609}
4610
Bram Moolenaar05159a02005-02-26 23:04:13 +00004611#if defined(FEAT_EVAL) || defined(PROTO)
4612/*
4613 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004614 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004615 */
4616 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004617get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004618{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004619 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004620 dict_T *dict;
4621 char_u buf[2];
4622 qfline_T *qfp;
4623 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004624 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004625
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004626 if (wp != NULL)
4627 {
4628 qi = GET_LOC_LIST(wp);
4629 if (qi == NULL)
4630 return FAIL;
4631 }
4632
Bram Moolenaard823fa92016-08-12 16:29:27 +02004633 if (qf_idx == -1)
4634 qf_idx = qi->qf_curlist;
4635
4636 if (qf_idx >= qi->qf_listcount
4637 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004638 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004639
Bram Moolenaard823fa92016-08-12 16:29:27 +02004640 qfp = qi->qf_lists[qf_idx].qf_start;
4641 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004642 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004643 /* Handle entries with a non-existing buffer number. */
4644 bufnum = qfp->qf_fnum;
4645 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4646 bufnum = 0;
4647
Bram Moolenaar05159a02005-02-26 23:04:13 +00004648 if ((dict = dict_alloc()) == NULL)
4649 return FAIL;
4650 if (list_append_dict(list, dict) == FAIL)
4651 return FAIL;
4652
4653 buf[0] = qfp->qf_type;
4654 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004655 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004656 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4657 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4658 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4659 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004660 || dict_add_nr_str(dict, "pattern", 0L,
4661 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4662 || dict_add_nr_str(dict, "text", 0L,
4663 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004664 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4665 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4666 return FAIL;
4667
4668 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004669 if (qfp == NULL)
4670 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004671 }
4672 return OK;
4673}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004674
4675/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004676 * Flags used by getqflist()/getloclist() to determine which fields to return.
4677 */
4678enum {
4679 QF_GETLIST_NONE = 0x0,
4680 QF_GETLIST_TITLE = 0x1,
4681 QF_GETLIST_ITEMS = 0x2,
4682 QF_GETLIST_NR = 0x4,
4683 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004684 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004685 QF_GETLIST_ALL = 0xFF
4686};
4687
4688/*
4689 * Return quickfix/location list details (title) as a
4690 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4691 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004692 */
4693 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004694get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4695{
4696 qf_info_T *qi = &ql_info;
4697 int status = OK;
4698 int qf_idx;
4699 dictitem_T *di;
4700 int flags = QF_GETLIST_NONE;
4701
4702 if (wp != NULL)
4703 {
4704 qi = GET_LOC_LIST(wp);
4705 if (qi == NULL)
Bram Moolenaar875feea2017-06-11 16:07:51 +02004706 {
4707 /* If querying for the size of the location list, return 0 */
4708 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL) &&
4709 (di->di_tv.v_type == VAR_STRING) &&
4710 (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4711 return dict_add_nr_str(retdict, "nr", 0, NULL);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004712 return FAIL;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004713 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004714 }
4715
4716 qf_idx = qi->qf_curlist; /* default is the current list */
4717 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4718 {
4719 /* Use the specified quickfix/location list */
4720 if (di->di_tv.v_type == VAR_NUMBER)
4721 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004722 /* for zero use the current list */
4723 if (di->di_tv.vval.v_number != 0)
4724 {
4725 qf_idx = di->di_tv.vval.v_number - 1;
4726 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4727 return FAIL;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004728 } else if (qi->qf_listcount == 0) /* stack is empty */
4729 return FAIL;
4730 flags |= QF_GETLIST_NR;
4731 } else if ((di->di_tv.v_type == VAR_STRING) &&
4732 (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4733 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004734 /* Get the last quickfix list number */
4735 if (qi->qf_listcount > 0)
4736 qf_idx = qi->qf_listcount - 1;
4737 else
4738 qf_idx = -1; /* Quickfix stack is empty */
Bram Moolenaard823fa92016-08-12 16:29:27 +02004739 flags |= QF_GETLIST_NR;
4740 }
4741 else
4742 return FAIL;
4743 }
4744
Bram Moolenaar875feea2017-06-11 16:07:51 +02004745 if (qf_idx != -1)
4746 {
4747 if (dict_find(what, (char_u *)"all", -1) != NULL)
4748 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004749
Bram Moolenaar875feea2017-06-11 16:07:51 +02004750 if (dict_find(what, (char_u *)"title", -1) != NULL)
4751 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004752
Bram Moolenaar875feea2017-06-11 16:07:51 +02004753 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4754 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004755
Bram Moolenaar875feea2017-06-11 16:07:51 +02004756 if (dict_find(what, (char_u *)"context", -1) != NULL)
4757 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004758
4759 if (dict_find(what, (char_u *)"items", -1) != NULL)
4760 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004761 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004762
Bram Moolenaard823fa92016-08-12 16:29:27 +02004763 if (flags & QF_GETLIST_TITLE)
4764 {
4765 char_u *t;
4766 t = qi->qf_lists[qf_idx].qf_title;
4767 if (t == NULL)
4768 t = (char_u *)"";
4769 status = dict_add_nr_str(retdict, "title", 0L, t);
4770 }
4771 if ((status == OK) && (flags & QF_GETLIST_NR))
4772 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4773 if ((status == OK) && (flags & QF_GETLIST_WINID))
4774 {
4775 win_T *win;
4776 win = qf_find_win(qi);
4777 if (win != NULL)
4778 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4779 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004780 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4781 {
4782 list_T *l = list_alloc();
4783 if (l != NULL)
4784 {
4785 (void)get_errorlist(wp, qf_idx, l);
4786 dict_add_list(retdict, "items", l);
4787 }
4788 }
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 Moolenaarbeb9cb12017-05-01 14:14:04 +02004798 if (dict_add(retdict, di) == FAIL)
4799 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004800 }
4801 }
4802 else
4803 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4804 }
4805
Bram Moolenaard823fa92016-08-12 16:29:27 +02004806 return status;
4807}
4808
4809/*
4810 * Add list of entries to quickfix/location list. Each list entry is
4811 * a dictionary with item information.
4812 */
4813 static int
4814qf_add_entries(
4815 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004816 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004817 list_T *list,
4818 char_u *title,
4819 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004820{
4821 listitem_T *li;
4822 dict_T *d;
4823 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004824 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004825 long lnum;
4826 int col, nr;
4827 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004828#ifdef FEAT_WINDOWS
4829 qfline_T *old_last = NULL;
4830#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004831 int valid, status;
4832 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004833 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004834
Bram Moolenaara3921f42017-06-04 15:30:34 +02004835 if (action == ' ' || qf_idx == qi->qf_listcount)
4836 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004837 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004838 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004839 qf_idx = qi->qf_curlist;
4840 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004841#ifdef FEAT_WINDOWS
Bram Moolenaara3921f42017-06-04 15:30:34 +02004842 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004843 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004844 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004845#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004846 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004847 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004848 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004849 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004850 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004851
4852 for (li = list->lv_first; li != NULL; li = li->li_next)
4853 {
4854 if (li->li_tv.v_type != VAR_DICT)
4855 continue; /* Skip non-dict items */
4856
4857 d = li->li_tv.vval.v_dict;
4858 if (d == NULL)
4859 continue;
4860
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004861 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004862 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4863 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4864 col = (int)get_dict_number(d, (char_u *)"col");
4865 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4866 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004867 type = get_dict_string(d, (char_u *)"type", TRUE);
4868 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4869 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004870 if (text == NULL)
4871 text = vim_strsave((char_u *)"");
4872
4873 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004874 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004875 valid = FALSE;
4876
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004877 /* Mark entries with non-existing buffer number as not valid. Give the
4878 * error message only once. */
4879 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4880 {
4881 if (!did_bufnr_emsg)
4882 {
4883 did_bufnr_emsg = TRUE;
4884 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4885 }
4886 valid = FALSE;
4887 bufnum = 0;
4888 }
4889
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02004890 /* If the 'valid' field is present it overrules the detected value. */
4891 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
4892 valid = (int)get_dict_number(d, (char_u *)"valid");
4893
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004894 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004895 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004896 NULL, /* dir */
4897 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004898 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004899 text,
4900 lnum,
4901 col,
4902 vcol, /* vis_col */
4903 pattern, /* search pattern */
4904 nr,
4905 type == NULL ? NUL : *type,
4906 valid);
4907
4908 vim_free(filename);
4909 vim_free(pattern);
4910 vim_free(text);
4911 vim_free(type);
4912
4913 if (status == FAIL)
4914 {
4915 retval = FAIL;
4916 break;
4917 }
4918 }
4919
Bram Moolenaara3921f42017-06-04 15:30:34 +02004920 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004921 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004922 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004923 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02004924 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004925 if (action != 'a')
4926 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02004927 qi->qf_lists[qf_idx].qf_ptr =
4928 qi->qf_lists[qf_idx].qf_start;
4929 if (qi->qf_lists[qf_idx].qf_count > 0)
4930 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004931 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004932
4933#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004934 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004935 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004936#endif
4937
4938 return retval;
4939}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004940
4941 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004942qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004943{
4944 dictitem_T *di;
4945 int retval = FAIL;
4946 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004947 int newlist = FALSE;
4948
4949 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4950 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004951
4952 qf_idx = qi->qf_curlist; /* default is the current list */
4953 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4954 {
4955 /* Use the specified quickfix/location list */
4956 if (di->di_tv.v_type == VAR_NUMBER)
4957 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004958 /* for zero use the current list */
4959 if (di->di_tv.vval.v_number != 0)
4960 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004961
4962 if ((action == ' ' || action == 'a') &&
4963 qf_idx == qi->qf_listcount)
4964 /*
4965 * When creating a new list, accept qf_idx pointing to the next
4966 * non-available list
4967 */
4968 newlist = TRUE;
4969 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004970 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004971 else
4972 newlist = FALSE; /* use the specified list */
Bram Moolenaar875feea2017-06-11 16:07:51 +02004973 } else if (di->di_tv.v_type == VAR_STRING &&
4974 STRCMP(di->di_tv.vval.v_string, "$") == 0 &&
4975 qi->qf_listcount > 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004976 {
Bram Moolenaar875feea2017-06-11 16:07:51 +02004977 qf_idx = qi->qf_listcount - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004978 newlist = FALSE;
4979 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004980 else
4981 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004982 }
4983
4984 if (newlist)
4985 {
4986 qf_new_list(qi, NULL);
4987 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004988 }
4989
4990 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4991 {
4992 if (di->di_tv.v_type == VAR_STRING)
4993 {
4994 vim_free(qi->qf_lists[qf_idx].qf_title);
4995 qi->qf_lists[qf_idx].qf_title =
4996 get_dict_string(what, (char_u *)"title", TRUE);
4997 if (qf_idx == qi->qf_curlist)
4998 qf_update_win_titlevar(qi);
4999 retval = OK;
5000 }
5001 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005002 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5003 {
5004 if (di->di_tv.v_type == VAR_LIST)
5005 {
5006 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5007
5008 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5009 title_save, action == ' ' ? 'a' : action);
5010 vim_free(title_save);
5011 }
5012 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005013
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005014 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5015 {
5016 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005017
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005018 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005019 ctx = alloc_tv();
5020 if (ctx != NULL)
5021 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005022 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005023 }
5024
Bram Moolenaard823fa92016-08-12 16:29:27 +02005025 return retval;
5026}
5027
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005028/*
5029 * Find the non-location list window with the specified location list.
5030 */
5031 static win_T *
5032find_win_with_ll(qf_info_T *qi)
5033{
5034 win_T *wp = NULL;
5035
5036 FOR_ALL_WINDOWS(wp)
5037 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5038 return wp;
5039
5040 return NULL;
5041}
5042
5043/*
5044 * Free the entire quickfix/location list stack.
5045 * If the quickfix/location list window is open, then clear it.
5046 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005047 static void
5048qf_free_stack(win_T *wp, qf_info_T *qi)
5049{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005050 win_T *qfwin = qf_find_win(qi);
5051 win_T *llwin = NULL;
5052 win_T *orig_wp = wp;
5053
5054 if (qfwin != NULL)
5055 {
5056 /* If the quickfix/location list window is open, then clear it */
5057 if (qi->qf_curlist < qi->qf_listcount)
5058 qf_free(qi, qi->qf_curlist);
5059 qf_update_buffer(qi, NULL);
5060 }
5061
5062 if (wp != NULL && IS_LL_WINDOW(wp))
5063 {
5064 /* If in the location list window, then use the non-location list
5065 * window with this location list (if present)
5066 */
5067 llwin = find_win_with_ll(qi);
5068 if (llwin != NULL)
5069 wp = llwin;
5070 }
5071
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005072 qf_free_all(wp);
5073 if (wp == NULL)
5074 {
5075 /* quickfix list */
5076 qi->qf_curlist = 0;
5077 qi->qf_listcount = 0;
5078 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005079 else if (IS_LL_WINDOW(orig_wp))
5080 {
5081 /* If the location list window is open, then create a new empty
5082 * location list */
5083 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005084
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005085 /* first free the list reference in the location list window */
5086 ll_free_all(&orig_wp->w_llist_ref);
5087
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005088 orig_wp->w_llist_ref = new_ll;
5089 if (llwin != NULL)
5090 {
5091 llwin->w_llist = new_ll;
5092 new_ll->qf_refcount++;
5093 }
5094 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005095}
5096
Bram Moolenaard823fa92016-08-12 16:29:27 +02005097/*
5098 * Populate the quickfix list with the items supplied in the list
5099 * of dictionaries. "title" will be copied to w:quickfix_title.
5100 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5101 */
5102 int
5103set_errorlist(
5104 win_T *wp,
5105 list_T *list,
5106 int action,
5107 char_u *title,
5108 dict_T *what)
5109{
5110 qf_info_T *qi = &ql_info;
5111 int retval = OK;
5112
5113 if (wp != NULL)
5114 {
5115 qi = ll_get_or_alloc_list(wp);
5116 if (qi == NULL)
5117 return FAIL;
5118 }
5119
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005120 if (action == 'f')
5121 {
5122 /* Free the entire quickfix or location list stack */
5123 qf_free_stack(wp, qi);
5124 }
5125 else if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005126 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005127 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005128 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005129
5130 return retval;
5131}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005132
5133 static int
5134mark_quickfix_ctx(qf_info_T *qi, int copyID)
5135{
5136 int i;
5137 int abort = FALSE;
5138 typval_T *ctx;
5139
5140 for (i = 0; i < LISTCOUNT && !abort; ++i)
5141 {
5142 ctx = qi->qf_lists[i].qf_ctx;
5143 if (ctx != NULL && ctx->v_type != VAR_NUMBER &&
5144 ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
5145 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5146 }
5147
5148 return abort;
5149}
5150
5151/*
5152 * Mark the context of the quickfix list and the location lists (if present) as
5153 * "in use". So that garabage collection doesn't free the context.
5154 */
5155 int
5156set_ref_in_quickfix(int copyID)
5157{
5158 int abort = FALSE;
5159 tabpage_T *tp;
5160 win_T *win;
5161
5162 abort = mark_quickfix_ctx(&ql_info, copyID);
5163 if (abort)
5164 return abort;
5165
5166 FOR_ALL_TAB_WINDOWS(tp, win)
5167 {
5168 if (win->w_llist != NULL)
5169 {
5170 abort = mark_quickfix_ctx(win->w_llist, copyID);
5171 if (abort)
5172 return abort;
5173 }
5174 }
5175
5176 return abort;
5177}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005178#endif
5179
Bram Moolenaar81695252004-12-29 20:58:21 +00005180/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005181 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005182 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005183 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005184 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005185 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005186 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005187 */
5188 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005189ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005190{
5191 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005192 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005193#ifdef FEAT_AUTOCMD
5194 char_u *au_name = NULL;
5195#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005196
Bram Moolenaardb552d602006-03-23 22:59:57 +00005197 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5198 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005199 {
5200 qi = ll_get_or_alloc_list(curwin);
5201 if (qi == NULL)
5202 return;
5203 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005204
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005205#ifdef FEAT_AUTOCMD
5206 switch (eap->cmdidx)
5207 {
5208 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5209 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5210 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5211 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5212 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5213 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5214 default: break;
5215 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005216 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5217 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005218 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005219# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005220 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005221 return;
5222# endif
5223 }
5224#endif
5225
Bram Moolenaar86b68352004-12-27 21:59:20 +00005226 if (*eap->arg == NUL)
5227 buf = curbuf;
5228 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5229 buf = buflist_findnr(atoi((char *)eap->arg));
5230 if (buf == NULL)
5231 EMSG(_(e_invarg));
5232 else if (buf->b_ml.ml_mfp == NULL)
5233 EMSG(_("E681: Buffer is not loaded"));
5234 else
5235 {
5236 if (eap->addr_count == 0)
5237 {
5238 eap->line1 = 1;
5239 eap->line2 = buf->b_ml.ml_line_count;
5240 }
5241 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5242 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5243 EMSG(_(e_invrange));
5244 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005245 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005246 char_u *qf_title = *eap->cmdlinep;
5247
5248 if (buf->b_sfname)
5249 {
5250 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5251 (char *)qf_title, (char *)buf->b_sfname);
5252 qf_title = IObuff;
5253 }
5254
Bram Moolenaardb552d602006-03-23 22:59:57 +00005255 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
5256 (eap->cmdidx != CMD_caddbuffer
5257 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005258 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005259 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005260 {
5261#ifdef FEAT_AUTOCMD
5262 if (au_name != NULL)
5263 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5264 curbuf->b_fname, TRUE, curbuf);
5265#endif
5266 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5267 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5268 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005269 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005270 }
5271}
5272
Bram Moolenaar1e015462005-09-25 22:16:38 +00005273#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005274/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005275 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5276 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005277 */
5278 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005279ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005280{
5281 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005282 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005283#ifdef FEAT_AUTOCMD
5284 char_u *au_name = NULL;
5285#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005286
Bram Moolenaardb552d602006-03-23 22:59:57 +00005287 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5288 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005289 {
5290 qi = ll_get_or_alloc_list(curwin);
5291 if (qi == NULL)
5292 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005293 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005294
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005295#ifdef FEAT_AUTOCMD
5296 switch (eap->cmdidx)
5297 {
5298 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5299 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5300 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5301 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5302 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5303 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5304 default: break;
5305 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005306 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5307 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005308 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005309# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005310 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005311 return;
5312# endif
5313 }
5314#endif
5315
Bram Moolenaar4770d092006-01-12 23:22:24 +00005316 /* Evaluate the expression. When the result is a string or a list we can
5317 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005318 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005319 if (tv != NULL)
5320 {
5321 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5322 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5323 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01005324 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005325 (eap->cmdidx != CMD_caddexpr
5326 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005327 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5328 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005329 {
5330#ifdef FEAT_AUTOCMD
5331 if (au_name != NULL)
5332 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5333 curbuf->b_fname, TRUE, curbuf);
5334#endif
5335 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5336 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5337 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005338 }
5339 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005340 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005341 free_tv(tv);
5342 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005343}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005344#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005345
5346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 * ":helpgrep {pattern}"
5348 */
5349 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005350ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351{
5352 regmatch_T regmatch;
5353 char_u *save_cpo;
5354 char_u *p;
5355 int fcount;
5356 char_u **fnames;
5357 FILE *fd;
5358 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005360#ifdef FEAT_MULTI_LANG
5361 char_u *lang;
5362#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005363 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005364 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005365 int new_qi = FALSE;
5366 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005367#ifdef FEAT_AUTOCMD
5368 char_u *au_name = NULL;
5369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005371#ifdef FEAT_MULTI_LANG
5372 /* Check for a specified language */
5373 lang = check_help_lang(eap->arg);
5374#endif
5375
Bram Moolenaar73633f82012-01-20 13:39:07 +01005376#ifdef FEAT_AUTOCMD
5377 switch (eap->cmdidx)
5378 {
5379 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5380 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5381 default: break;
5382 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005383 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5384 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005385 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005386# ifdef FEAT_EVAL
5387 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005388 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005389# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005390 }
5391#endif
5392
5393 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5394 save_cpo = p_cpo;
5395 p_cpo = empty_option;
5396
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005397 if (eap->cmdidx == CMD_lhelpgrep)
5398 {
5399 /* Find an existing help window */
5400 FOR_ALL_WINDOWS(wp)
5401 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5402 break;
5403
5404 if (wp == NULL) /* Help window not found */
5405 qi = NULL;
5406 else
5407 qi = wp->w_llist;
5408
5409 if (qi == NULL)
5410 {
5411 /* Allocate a new location list for help text matches */
5412 if ((qi = ll_new_list()) == NULL)
5413 return;
5414 new_qi = TRUE;
5415 }
5416 }
5417
Bram Moolenaaree85df32017-03-19 14:19:50 +01005418 /* Autocommands may change the list. Save it for later comparison */
5419 save_qi = qi;
5420
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5422 regmatch.rm_ic = FALSE;
5423 if (regmatch.regprog != NULL)
5424 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005425#ifdef FEAT_MBYTE
5426 vimconv_T vc;
5427
5428 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5429 * differs. */
5430 vc.vc_type = CONV_NONE;
5431 if (!enc_utf8)
5432 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5433#endif
5434
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005436 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
5438 /* Go through all directories in 'runtimepath' */
5439 p = p_rtp;
5440 while (*p != NUL && !got_int)
5441 {
5442 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5443
5444 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5445 add_pathsep(NameBuff);
5446 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5447 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5448 &fnames, EW_FILE|EW_SILENT) == OK
5449 && fcount > 0)
5450 {
5451 for (fi = 0; fi < fcount && !got_int; ++fi)
5452 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005453#ifdef FEAT_MULTI_LANG
5454 /* Skip files for a different language. */
5455 if (lang != NULL
5456 && STRNICMP(lang, fnames[fi]
5457 + STRLEN(fnames[fi]) - 3, 2) != 0
5458 && !(STRNICMP(lang, "en", 2) == 0
5459 && STRNICMP("txt", fnames[fi]
5460 + STRLEN(fnames[fi]) - 3, 3) == 0))
5461 continue;
5462#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005463 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 if (fd != NULL)
5465 {
5466 lnum = 1;
5467 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5468 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005469 char_u *line = IObuff;
5470#ifdef FEAT_MBYTE
5471 /* Convert a line if 'encoding' is not utf-8 and
5472 * the line contains a non-ASCII character. */
5473 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005474 && has_non_ascii(IObuff))
5475 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005476 line = string_convert(&vc, IObuff, NULL);
5477 if (line == NULL)
5478 line = IObuff;
5479 }
5480#endif
5481
5482 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005484 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
5486 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005487 while (l > 0 && line[l - 1] <= ' ')
5488 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005490 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005491 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 NULL, /* dir */
5493 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005494 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005495 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005497 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005498 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005499 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005500 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 0, /* nr */
5502 1, /* type */
5503 TRUE /* valid */
5504 ) == FAIL)
5505 {
5506 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005507#ifdef FEAT_MBYTE
5508 if (line != IObuff)
5509 vim_free(line);
5510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 break;
5512 }
5513 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005514#ifdef FEAT_MBYTE
5515 if (line != IObuff)
5516 vim_free(line);
5517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 ++lnum;
5519 line_breakcheck();
5520 }
5521 fclose(fd);
5522 }
5523 }
5524 FreeWild(fcount, fnames);
5525 }
5526 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005527
Bram Moolenaar473de612013-06-08 18:19:48 +02005528 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005529#ifdef FEAT_MBYTE
5530 if (vc.vc_type != CONV_NONE)
5531 convert_setup(&vc, NULL, NULL);
5532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005534 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5535 qi->qf_lists[qi->qf_curlist].qf_ptr =
5536 qi->qf_lists[qi->qf_curlist].qf_start;
5537 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
5539
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005540 if (p_cpo == empty_option)
5541 p_cpo = save_cpo;
5542 else
5543 /* Darn, some plugin changed the value. */
5544 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
5546#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005547 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548#endif
5549
Bram Moolenaar73633f82012-01-20 13:39:07 +01005550#ifdef FEAT_AUTOCMD
5551 if (au_name != NULL)
5552 {
5553 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5554 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005555 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005556 /* autocommands made "qi" invalid */
5557 return;
5558 }
5559#endif
5560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005562 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005563 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005564 else
5565 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005566
5567 if (eap->cmdidx == CMD_lhelpgrep)
5568 {
5569 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005570 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005571 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5572 {
5573 if (new_qi)
5574 ll_free_all(&qi);
5575 }
5576 else if (curwin->w_llist == NULL)
5577 curwin->w_llist = qi;
5578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579}
5580
5581#endif /* FEAT_QUICKFIX */