blob: 40b6fe93d692c012d67894889529daa9eefc745a [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. */
3428 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3429 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003432 keep_filetype = TRUE; /* don't detect 'filetype' */
3433 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003435 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003437 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003439 /* make sure it will be redrawn */
3440 redraw_curbuf_later(NOT_VALID);
3441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443 /* Restore KeyTyped, setting 'filetype' may reset it. */
3444 KeyTyped = old_KeyTyped;
3445}
3446
3447#endif /* FEAT_WINDOWS */
3448
3449/*
3450 * Return TRUE if "buf" is the quickfix buffer.
3451 */
3452 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003453bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003455 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456}
3457
3458/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003459 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3460 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 */
3462 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003463bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003465 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3466 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467}
3468
3469/*
3470 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3471 */
3472 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003473bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003475 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476}
3477
3478 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003479bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481 if (bt_dontwrite(buf))
3482 {
3483 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3484 return TRUE;
3485 }
3486 return FALSE;
3487}
3488
3489/*
3490 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3491 * and 'bufhidden'.
3492 */
3493 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003494buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3497 switch (buf->b_p_bh[0])
3498 {
3499 case 'u': /* "unload" */
3500 case 'w': /* "wipe" */
3501 case 'd': return FALSE; /* "delete" */
3502 case 'h': return TRUE; /* "hide" */
3503 }
3504 return (p_hid || cmdmod.hide);
3505}
3506
3507/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003508 * Return TRUE when using ":vimgrep" for ":grep".
3509 */
3510 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003511grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003512{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003513 return ((cmdidx == CMD_grep
3514 || cmdidx == CMD_lgrep
3515 || cmdidx == CMD_grepadd
3516 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003517 && STRCMP("internal",
3518 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3519}
3520
3521/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003522 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 */
3524 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003525ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003527 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003529 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003531 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003532 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003533 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003534#ifdef FEAT_AUTOCMD
3535 char_u *au_name = NULL;
3536
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003537 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3538 if (grep_internal(eap->cmdidx))
3539 {
3540 ex_vimgrep(eap);
3541 return;
3542 }
3543
Bram Moolenaar7c626922005-02-07 22:01:03 +00003544 switch (eap->cmdidx)
3545 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003546 case CMD_make: au_name = (char_u *)"make"; break;
3547 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3548 case CMD_grep: au_name = (char_u *)"grep"; break;
3549 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3550 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3551 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003552 default: break;
3553 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003554 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3555 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003556 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003557# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003558 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003559 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003560# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003561 }
3562#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003563#ifdef FEAT_MBYTE
3564 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
Bram Moolenaara6557602006-02-04 22:43:20 +00003567 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3568 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003569 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003572 fname = get_mef_name();
3573 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003575 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 /*
3578 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3579 */
3580 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3581 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003582 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 cmd = alloc(len);
3584 if (cmd == NULL)
3585 return;
3586 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3587 (char *)p_shq);
3588 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003589 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 /*
3591 * Output a newline if there's something else than the :make command that
3592 * was typed (in which case the cursor is in column 0).
3593 */
3594 if (msg_col == 0)
3595 msg_didout = FALSE;
3596 msg_start();
3597 MSG_PUTS(":!");
3598 msg_outtrans(cmd); /* show what we are doing */
3599
3600 /* let the shell know if we are redirecting output or not */
3601 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3602
3603#ifdef AMIGA
3604 out_flush();
3605 /* read window status report and redraw before message */
3606 (void)char_avail();
3607#endif
3608
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003609 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003610 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3611 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003612 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003613 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003614 if (wp != NULL)
3615 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003616#ifdef FEAT_AUTOCMD
3617 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003618 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003619 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3620 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003621 if (qi->qf_curlist < qi->qf_listcount)
3622 res = qi->qf_lists[qi->qf_curlist].qf_count;
3623 else
3624 res = 0;
3625 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003626#endif
3627 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003628 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaar7c626922005-02-07 22:01:03 +00003630 mch_remove(fname);
3631 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 vim_free(cmd);
3633}
3634
3635/*
3636 * Return the name for the errorfile, in allocated memory.
3637 * Find a new unique name when 'makeef' contains "##".
3638 * Returns NULL for error.
3639 */
3640 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003641get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
3643 char_u *p;
3644 char_u *name;
3645 static int start = -1;
3646 static int off = 0;
3647#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003648 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649#endif
3650
3651 if (*p_mef == NUL)
3652 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003653 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (name == NULL)
3655 EMSG(_(e_notmp));
3656 return name;
3657 }
3658
3659 for (p = p_mef; *p; ++p)
3660 if (p[0] == '#' && p[1] == '#')
3661 break;
3662
3663 if (*p == NUL)
3664 return vim_strsave(p_mef);
3665
3666 /* Keep trying until the name doesn't exist yet. */
3667 for (;;)
3668 {
3669 if (start == -1)
3670 start = mch_get_pid();
3671 else
3672 off += 19;
3673
3674 name = alloc((unsigned)STRLEN(p_mef) + 30);
3675 if (name == NULL)
3676 break;
3677 STRCPY(name, p_mef);
3678 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3679 STRCAT(name, p + 2);
3680 if (mch_getperm(name) < 0
3681#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003682 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 && mch_lstat((char *)name, &sb) < 0
3684#endif
3685 )
3686 break;
3687 vim_free(name);
3688 }
3689 return name;
3690}
3691
3692/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003693 * Returns the number of valid entries in the current quickfix/location list.
3694 */
3695 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003696qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003697{
3698 qf_info_T *qi = &ql_info;
3699 qfline_T *qfp;
3700 int i, sz = 0;
3701 int prev_fnum = 0;
3702
3703 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3704 {
3705 /* Location list */
3706 qi = GET_LOC_LIST(curwin);
3707 if (qi == NULL)
3708 return 0;
3709 }
3710
3711 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003712 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003713 ++i, qfp = qfp->qf_next)
3714 {
3715 if (qfp->qf_valid)
3716 {
3717 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3718 sz++; /* Count all valid entries */
3719 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3720 {
3721 /* Count the number of files */
3722 sz++;
3723 prev_fnum = qfp->qf_fnum;
3724 }
3725 }
3726 }
3727
3728 return sz;
3729}
3730
3731/*
3732 * Returns the current index of the quickfix/location list.
3733 * Returns 0 if there is an error.
3734 */
3735 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003736qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003737{
3738 qf_info_T *qi = &ql_info;
3739
3740 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3741 {
3742 /* Location list */
3743 qi = GET_LOC_LIST(curwin);
3744 if (qi == NULL)
3745 return 0;
3746 }
3747
3748 return qi->qf_lists[qi->qf_curlist].qf_index;
3749}
3750
3751/*
3752 * Returns the current index in the quickfix/location list (counting only valid
3753 * entries). If no valid entries are in the list, then returns 1.
3754 */
3755 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003756qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003757{
3758 qf_info_T *qi = &ql_info;
3759 qf_list_T *qfl;
3760 qfline_T *qfp;
3761 int i, eidx = 0;
3762 int prev_fnum = 0;
3763
3764 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3765 {
3766 /* Location list */
3767 qi = GET_LOC_LIST(curwin);
3768 if (qi == NULL)
3769 return 1;
3770 }
3771
3772 qfl = &qi->qf_lists[qi->qf_curlist];
3773 qfp = qfl->qf_start;
3774
3775 /* check if the list has valid errors */
3776 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3777 return 1;
3778
3779 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3780 {
3781 if (qfp->qf_valid)
3782 {
3783 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3784 {
3785 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3786 {
3787 /* Count the number of files */
3788 eidx++;
3789 prev_fnum = qfp->qf_fnum;
3790 }
3791 }
3792 else
3793 eidx++;
3794 }
3795 }
3796
3797 return eidx ? eidx : 1;
3798}
3799
3800/*
3801 * Get the 'n'th valid error entry in the quickfix or location list.
3802 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3803 * For :cdo and :ldo returns the 'n'th valid error entry.
3804 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3805 */
3806 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003807qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003808{
3809 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3810 qfline_T *qfp = qfl->qf_start;
3811 int i, eidx;
3812 int prev_fnum = 0;
3813
3814 /* check if the list has valid errors */
3815 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3816 return 1;
3817
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003818 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003819 i++, qfp = qfp->qf_next)
3820 {
3821 if (qfp->qf_valid)
3822 {
3823 if (fdo)
3824 {
3825 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3826 {
3827 /* Count the number of files */
3828 eidx++;
3829 prev_fnum = qfp->qf_fnum;
3830 }
3831 }
3832 else
3833 eidx++;
3834 }
3835
3836 if (eidx == n)
3837 break;
3838 }
3839
3840 if (i <= qfl->qf_count)
3841 return i;
3842 else
3843 return 1;
3844}
3845
3846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003848 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003849 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 */
3851 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003852ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003854 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003855 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003856
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003857 if (eap->cmdidx == CMD_ll
3858 || eap->cmdidx == CMD_lrewind
3859 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003860 || eap->cmdidx == CMD_llast
3861 || eap->cmdidx == CMD_ldo
3862 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003863 {
3864 qi = GET_LOC_LIST(curwin);
3865 if (qi == NULL)
3866 {
3867 EMSG(_(e_loclist));
3868 return;
3869 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003870 }
3871
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003872 if (eap->addr_count > 0)
3873 errornr = (int)eap->line2;
3874 else
3875 {
3876 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3877 errornr = 0;
3878 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3879 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3880 errornr = 1;
3881 else
3882 errornr = 32767;
3883 }
3884
3885 /* For cdo and ldo commands, jump to the nth valid error.
3886 * For cfdo and lfdo commands, jump to the nth valid file entry.
3887 */
3888 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3889 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3890 errornr = qf_get_nth_valid_entry(qi,
3891 eap->addr_count > 0 ? (int)eap->line1 : 1,
3892 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3893
3894 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895}
3896
3897/*
3898 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003899 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003900 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 */
3902 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003903ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003905 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003906 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003907
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003908 if (eap->cmdidx == CMD_lnext
3909 || eap->cmdidx == CMD_lNext
3910 || eap->cmdidx == CMD_lprevious
3911 || eap->cmdidx == CMD_lnfile
3912 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003913 || eap->cmdidx == CMD_lpfile
3914 || eap->cmdidx == CMD_ldo
3915 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003916 {
3917 qi = GET_LOC_LIST(curwin);
3918 if (qi == NULL)
3919 {
3920 EMSG(_(e_loclist));
3921 return;
3922 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003923 }
3924
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003925 if (eap->addr_count > 0 &&
3926 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3927 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3928 errornr = (int)eap->line2;
3929 else
3930 errornr = 1;
3931
3932 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3933 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003935 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3936 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003938 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3939 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 ? BACKWARD_FILE
3941 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003942 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943}
3944
3945/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003946 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003947 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 */
3949 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003950ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003952 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003953 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003954 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003955#ifdef FEAT_AUTOCMD
3956 char_u *au_name = NULL;
3957#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003958
3959 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003960 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003961 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003962
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003963#ifdef FEAT_AUTOCMD
3964 switch (eap->cmdidx)
3965 {
3966 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3967 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3968 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3969 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3970 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3971 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3972 default: break;
3973 }
3974 if (au_name != NULL)
3975 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3976#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003977#ifdef FEAT_MBYTE
3978 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3979#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003980#ifdef FEAT_BROWSE
3981 if (cmdmod.browse)
3982 {
3983 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3984 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3985 if (browse_file == NULL)
3986 return;
3987 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3988 vim_free(browse_file);
3989 }
3990 else
3991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003993 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003994
3995 /*
3996 * This function is used by the :cfile, :cgetfile and :caddfile
3997 * commands.
3998 * :cfile always creates a new quickfix list and jumps to the
3999 * first error.
4000 * :cgetfile creates a new quickfix list but doesn't jump to the
4001 * first error.
4002 * :caddfile adds to an existing quickfix list. If there is no
4003 * quickfix list then a new list is created.
4004 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004005 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004006 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004007 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004008 && (eap->cmdidx == CMD_cfile
4009 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004010 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004011#ifdef FEAT_AUTOCMD
4012 if (au_name != NULL)
4013 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4014#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004015 if (wp != NULL)
4016 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004017 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004018 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004019
4020 else
4021 {
4022#ifdef FEAT_AUTOCMD
4023 if (au_name != NULL)
4024 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4025#endif
4026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027}
4028
4029/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004030 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004031 * ":vimgrepadd {pattern} file(s)"
4032 * ":lvimgrep {pattern} file(s)"
4033 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004034 */
4035 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004036ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004037{
Bram Moolenaar81695252004-12-29 20:58:21 +00004038 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004039 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004040 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004041 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004042 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004043 char_u *s;
4044 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004045 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004046 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004047#ifdef FEAT_AUTOCMD
4048 qfline_T *cur_qf_start;
4049#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004050 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004051 buf_T *buf;
4052 int duplicate_name = FALSE;
4053 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004054 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004055 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004056 buf_T *first_match_buf = NULL;
4057 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004058 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004059#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4060 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004061#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004062 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004063 int flags = 0;
4064 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004065 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004066 char_u *dirname_start = NULL;
4067 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004068 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004069#ifdef FEAT_AUTOCMD
4070 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004071
4072 switch (eap->cmdidx)
4073 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004074 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4075 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4076 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004077 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004078 case CMD_grep: au_name = (char_u *)"grep"; break;
4079 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4080 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4081 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004082 default: break;
4083 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004084 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4085 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004086 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004087# ifdef FEAT_EVAL
4088 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004089 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004090# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004091 }
4092#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004093
Bram Moolenaar754b5602006-02-09 23:53:20 +00004094 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004095 || eap->cmdidx == CMD_lvimgrep
4096 || eap->cmdidx == CMD_lgrepadd
4097 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004098 {
4099 qi = ll_get_or_alloc_list(curwin);
4100 if (qi == NULL)
4101 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004102 }
4103
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004104 if (eap->addr_count > 0)
4105 tomatch = eap->line2;
4106 else
4107 tomatch = MAXLNUM;
4108
Bram Moolenaar81695252004-12-29 20:58:21 +00004109 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004110 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004111 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004112 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004113 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004114 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004115 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004116 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004117 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004118
4119 if (s != NULL && *s == NUL)
4120 {
4121 /* Pattern is empty, use last search pattern. */
4122 if (last_search_pat() == NULL)
4123 {
4124 EMSG(_(e_noprevre));
4125 goto theend;
4126 }
4127 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4128 }
4129 else
4130 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4131
Bram Moolenaar86b68352004-12-27 21:59:20 +00004132 if (regmatch.regprog == NULL)
4133 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004134 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004135 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004136
4137 p = skipwhite(p);
4138 if (*p == NUL)
4139 {
4140 EMSG(_("E683: File name missing or invalid pattern"));
4141 goto theend;
4142 }
4143
Bram Moolenaar754b5602006-02-09 23:53:20 +00004144 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004145 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004146 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004147 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004148 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004149
4150 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004151 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004152 goto theend;
4153 if (fcount == 0)
4154 {
4155 EMSG(_(e_nomatch));
4156 goto theend;
4157 }
4158
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004159 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4160 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004161 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004162 {
4163 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004164 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004165 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004166
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004167 /* Remember the current directory, because a BufRead autocommand that does
4168 * ":lcd %:p:h" changes the meaning of short path names. */
4169 mch_dirname(dirname_start, MAXPATHL);
4170
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004171#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004172 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004173 * changing the current quickfix list. */
4174 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4175#endif
4176
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004177 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004178 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004179 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004180 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004181 if (time(NULL) > seconds)
4182 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004183 /* Display the file name every second or so, show the user we are
4184 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004185 seconds = time(NULL);
4186 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004187 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004188 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004189 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004190 else
4191 {
4192 msg_outtrans(p);
4193 vim_free(p);
4194 }
4195 msg_clr_eos();
4196 msg_didout = FALSE; /* overwrite this message */
4197 msg_nowait = TRUE; /* don't wait for this message */
4198 msg_col = 0;
4199 out_flush();
4200 }
4201
Bram Moolenaar81695252004-12-29 20:58:21 +00004202 buf = buflist_findname_exp(fnames[fi]);
4203 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4204 {
4205 /* Remember that a buffer with this name already exists. */
4206 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004207 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004208 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004209
4210#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4211 /* Don't do Filetype autocommands to avoid loading syntax and
4212 * indent scripts, a great speed improvement. */
4213 save_ei = au_event_disable(",Filetype");
4214#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004215 /* Don't use modelines here, it's useless. */
4216 save_mls = p_mls;
4217 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004218
4219 /* Load file into a buffer, so that 'fileencoding' is detected,
4220 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004221 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004222
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004223 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004224#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4225 au_event_restore(save_ei);
4226#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004227 }
4228 else
4229 /* Use existing, loaded buffer. */
4230 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004231
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004232#ifdef FEAT_AUTOCMD
4233 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4234 {
4235 int idx;
4236
4237 /* Autocommands changed the quickfix list. Find the one we were
4238 * using and restore it. */
4239 for (idx = 0; idx < LISTCOUNT; ++idx)
4240 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4241 {
4242 qi->qf_curlist = idx;
4243 break;
4244 }
4245 if (idx == LISTCOUNT)
4246 {
4247 /* List cannot be found, create a new one. */
4248 qf_new_list(qi, *eap->cmdlinep);
4249 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4250 }
4251 }
4252#endif
4253
Bram Moolenaar81695252004-12-29 20:58:21 +00004254 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004255 {
4256 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004257 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004258 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004259 else
4260 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004261 /* Try for a match in all lines of the buffer.
4262 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004263 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004264 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4265 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004266 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004267 col = 0;
4268 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004269 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004270 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004271 /* Pass the buffer number so that it gets used even for a
4272 * dummy buffer, unless duplicate_name is set, then the
4273 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004274 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004275 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004276 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004277 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004278 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004279 ml_get_buf(buf,
4280 regmatch.startpos[0].lnum + lnum, FALSE),
4281 regmatch.startpos[0].lnum + lnum,
4282 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004283 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004284 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004285 0, /* nr */
4286 0, /* type */
4287 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004288 ) == FAIL)
4289 {
4290 got_int = TRUE;
4291 break;
4292 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004293 found_match = TRUE;
4294 if (--tomatch == 0)
4295 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004296 if ((flags & VGR_GLOBAL) == 0
4297 || regmatch.endpos[0].lnum > 0)
4298 break;
4299 col = regmatch.endpos[0].col
4300 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004301 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004302 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004303 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004304 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004305 if (got_int)
4306 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004307 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004308#ifdef FEAT_AUTOCMD
4309 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4310#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004311
4312 if (using_dummy)
4313 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004314 if (found_match && first_match_buf == NULL)
4315 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004316 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004317 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004318 /* Never keep a dummy buffer if there is another buffer
4319 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004320 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004321 buf = NULL;
4322 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004323 else if (!cmdmod.hide
4324 || buf->b_p_bh[0] == 'u' /* "unload" */
4325 || buf->b_p_bh[0] == 'w' /* "wipe" */
4326 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004327 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004328 /* When no match was found we don't need to remember the
4329 * buffer, wipe it out. If there was a match and it
4330 * wasn't the first one or we won't jump there: only
4331 * unload the buffer.
4332 * Ignore 'hidden' here, because it may lead to having too
4333 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004334 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004335 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004336 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004337 buf = NULL;
4338 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004339 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004340 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004341 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004342 /* Keeping the buffer, remove the dummy flag. */
4343 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004344 buf = NULL;
4345 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004346 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004347
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004348 if (buf != NULL)
4349 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004350 /* Keeping the buffer, remove the dummy flag. */
4351 buf->b_flags &= ~BF_DUMMY;
4352
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004353 /* If the buffer is still loaded we need to use the
4354 * directory we jumped to below. */
4355 if (buf == first_match_buf
4356 && target_dir == NULL
4357 && STRCMP(dirname_start, dirname_now) != 0)
4358 target_dir = vim_strsave(dirname_now);
4359
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004360 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004361 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004362 * need to be done (again). But not the window-local
4363 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004364 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004365#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004366 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4367 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004368#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004369 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004370 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004371 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004372 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004373 }
4374 }
4375
4376 FreeWild(fcount, fnames);
4377
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004378 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4379 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4380 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004381
4382#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004383 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004384#endif
4385
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004386#ifdef FEAT_AUTOCMD
4387 if (au_name != NULL)
4388 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4389 curbuf->b_fname, TRUE, curbuf);
4390#endif
4391
Bram Moolenaar86b68352004-12-27 21:59:20 +00004392 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004393 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004394 {
4395 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004396 {
4397 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004398 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004399 if (buf != curbuf)
4400 /* If we jumped to another buffer redrawing will already be
4401 * taken care of. */
4402 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004403
4404 /* Jump to the directory used after loading the buffer. */
4405 if (curbuf == first_match_buf && target_dir != NULL)
4406 {
4407 exarg_T ea;
4408
4409 ea.arg = target_dir;
4410 ea.cmdidx = CMD_lcd;
4411 ex_cd(&ea);
4412 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004413 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004414 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004415 else
4416 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004417
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004418 /* If we loaded a dummy buffer into the current window, the autocommands
4419 * may have messed up things, need to redraw and recompute folds. */
4420 if (redraw_for_dummy)
4421 {
4422#ifdef FEAT_FOLDING
4423 foldUpdateAll(curwin);
4424#else
4425 redraw_later(NOT_VALID);
4426#endif
4427 }
4428
Bram Moolenaar86b68352004-12-27 21:59:20 +00004429theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004430 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004431 vim_free(dirname_now);
4432 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004433 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004434 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004435}
4436
4437/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004438 * Restore current working directory to "dirname_start" if they differ, taking
4439 * into account whether it is set locally or globally.
4440 */
4441 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004442restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004443{
4444 char_u *dirname_now = alloc(MAXPATHL);
4445
4446 if (NULL != dirname_now)
4447 {
4448 mch_dirname(dirname_now, MAXPATHL);
4449 if (STRCMP(dirname_start, dirname_now) != 0)
4450 {
4451 /* If the directory has changed, change it back by building up an
4452 * appropriate ex command and executing it. */
4453 exarg_T ea;
4454
4455 ea.arg = dirname_start;
4456 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4457 ex_cd(&ea);
4458 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004459 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004460 }
4461}
4462
4463/*
4464 * Load file "fname" into a dummy buffer and return the buffer pointer,
4465 * placing the directory resulting from the buffer load into the
4466 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4467 * prior to calling this function. Restores directory to "dirname_start" prior
4468 * to returning, if autocmds or the 'autochdir' option have changed it.
4469 *
4470 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4471 * or wipe_dummy_buffer() later!
4472 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004473 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004474 */
4475 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004476load_dummy_buffer(
4477 char_u *fname,
4478 char_u *dirname_start, /* in: old directory */
4479 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004480{
4481 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004482 bufref_T newbufref;
4483 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004484 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004485 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004486
4487 /* Allocate a buffer without putting it in the buffer list. */
4488 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4489 if (newbuf == NULL)
4490 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004491 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004492
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004493 /* Init the options. */
4494 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4495
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004496 /* need to open the memfile before putting the buffer in a window */
4497 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004498 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004499 /* set curwin/curbuf to buf and save a few things */
4500 aucmd_prepbuf(&aco, newbuf);
4501
4502 /* Need to set the filename for autocommands. */
4503 (void)setfname(curbuf, fname, NULL, FALSE);
4504
Bram Moolenaar81695252004-12-29 20:58:21 +00004505 /* Create swap file now to avoid the ATTENTION message. */
4506 check_need_swap(TRUE);
4507
4508 /* Remove the "dummy" flag, otherwise autocommands may not
4509 * work. */
4510 curbuf->b_flags &= ~BF_DUMMY;
4511
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004512 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004513 if (readfile(fname, NULL,
4514 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4515 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004516 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004517 && !(curbuf->b_flags & BF_NEW))
4518 {
4519 failed = FALSE;
4520 if (curbuf != newbuf)
4521 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004522 /* Bloody autocommands changed the buffer! Can happen when
4523 * using netrw and editing a remote file. Use the current
4524 * buffer instead, delete the dummy one after restoring the
4525 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004526 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004527 newbuf = curbuf;
4528 }
4529 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004530
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004531 /* restore curwin/curbuf and a few other things */
4532 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004533 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4534 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004535
4536 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4537 * skip it. */
4538 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004539 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004540
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004541 /*
4542 * When autocommands/'autochdir' option changed directory: go back.
4543 * Let the caller know what the resulting dir was first, in case it is
4544 * important.
4545 */
4546 mch_dirname(resulting_dir, MAXPATHL);
4547 restore_start_dir(dirname_start);
4548
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004549 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004550 return NULL;
4551 if (failed)
4552 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004553 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004554 return NULL;
4555 }
4556 return newbuf;
4557}
4558
4559/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004560 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4561 * directory to "dirname_start" prior to returning, if autocmds or the
4562 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004563 */
4564 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004565wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004566{
4567 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004568 {
4569#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4570 cleanup_T cs;
4571
4572 /* Reset the error/interrupt/exception state here so that aborting()
4573 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4574 * work when got_int is set. */
4575 enter_cleanup(&cs);
4576#endif
4577
Bram Moolenaar81695252004-12-29 20:58:21 +00004578 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004579
4580#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4581 /* Restore the error/interrupt/exception state if not discarded by a
4582 * new aborting error, interrupt, or uncaught exception. */
4583 leave_cleanup(&cs);
4584#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004585 /* When autocommands/'autochdir' option changed directory: go back. */
4586 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004587 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004588}
4589
4590/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004591 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4592 * directory to "dirname_start" prior to returning, if autocmds or the
4593 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004594 */
4595 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004596unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004597{
4598 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004599 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004600 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004601
4602 /* When autocommands/'autochdir' option changed directory: go back. */
4603 restore_start_dir(dirname_start);
4604 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004605}
4606
Bram Moolenaar05159a02005-02-26 23:04:13 +00004607#if defined(FEAT_EVAL) || defined(PROTO)
4608/*
4609 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004610 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004611 */
4612 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004613get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004614{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004615 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004616 dict_T *dict;
4617 char_u buf[2];
4618 qfline_T *qfp;
4619 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004620 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004621
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004622 if (wp != NULL)
4623 {
4624 qi = GET_LOC_LIST(wp);
4625 if (qi == NULL)
4626 return FAIL;
4627 }
4628
Bram Moolenaard823fa92016-08-12 16:29:27 +02004629 if (qf_idx == -1)
4630 qf_idx = qi->qf_curlist;
4631
4632 if (qf_idx >= qi->qf_listcount
4633 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004634 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004635
Bram Moolenaard823fa92016-08-12 16:29:27 +02004636 qfp = qi->qf_lists[qf_idx].qf_start;
4637 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004638 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004639 /* Handle entries with a non-existing buffer number. */
4640 bufnum = qfp->qf_fnum;
4641 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4642 bufnum = 0;
4643
Bram Moolenaar05159a02005-02-26 23:04:13 +00004644 if ((dict = dict_alloc()) == NULL)
4645 return FAIL;
4646 if (list_append_dict(list, dict) == FAIL)
4647 return FAIL;
4648
4649 buf[0] = qfp->qf_type;
4650 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004651 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004652 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4653 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4654 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4655 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004656 || dict_add_nr_str(dict, "pattern", 0L,
4657 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4658 || dict_add_nr_str(dict, "text", 0L,
4659 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004660 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4661 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4662 return FAIL;
4663
4664 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004665 if (qfp == NULL)
4666 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004667 }
4668 return OK;
4669}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004670
4671/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004672 * Flags used by getqflist()/getloclist() to determine which fields to return.
4673 */
4674enum {
4675 QF_GETLIST_NONE = 0x0,
4676 QF_GETLIST_TITLE = 0x1,
4677 QF_GETLIST_ITEMS = 0x2,
4678 QF_GETLIST_NR = 0x4,
4679 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004680 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004681 QF_GETLIST_ALL = 0xFF
4682};
4683
4684/*
4685 * Return quickfix/location list details (title) as a
4686 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4687 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004688 */
4689 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004690get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4691{
4692 qf_info_T *qi = &ql_info;
4693 int status = OK;
4694 int qf_idx;
4695 dictitem_T *di;
4696 int flags = QF_GETLIST_NONE;
4697
4698 if (wp != NULL)
4699 {
4700 qi = GET_LOC_LIST(wp);
4701 if (qi == NULL)
Bram Moolenaar875feea2017-06-11 16:07:51 +02004702 {
4703 /* If querying for the size of the location list, return 0 */
4704 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL) &&
4705 (di->di_tv.v_type == VAR_STRING) &&
4706 (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4707 return dict_add_nr_str(retdict, "nr", 0, NULL);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004708 return FAIL;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004709 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004710 }
4711
4712 qf_idx = qi->qf_curlist; /* default is the current list */
4713 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4714 {
4715 /* Use the specified quickfix/location list */
4716 if (di->di_tv.v_type == VAR_NUMBER)
4717 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004718 /* for zero use the current list */
4719 if (di->di_tv.vval.v_number != 0)
4720 {
4721 qf_idx = di->di_tv.vval.v_number - 1;
4722 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4723 return FAIL;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004724 } else if (qi->qf_listcount == 0) /* stack is empty */
4725 return FAIL;
4726 flags |= QF_GETLIST_NR;
4727 } else if ((di->di_tv.v_type == VAR_STRING) &&
4728 (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4729 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004730 /* Get the last quickfix list number */
4731 if (qi->qf_listcount > 0)
4732 qf_idx = qi->qf_listcount - 1;
4733 else
4734 qf_idx = -1; /* Quickfix stack is empty */
Bram Moolenaard823fa92016-08-12 16:29:27 +02004735 flags |= QF_GETLIST_NR;
4736 }
4737 else
4738 return FAIL;
4739 }
4740
Bram Moolenaar875feea2017-06-11 16:07:51 +02004741 if (qf_idx != -1)
4742 {
4743 if (dict_find(what, (char_u *)"all", -1) != NULL)
4744 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004745
Bram Moolenaar875feea2017-06-11 16:07:51 +02004746 if (dict_find(what, (char_u *)"title", -1) != NULL)
4747 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004748
Bram Moolenaar875feea2017-06-11 16:07:51 +02004749 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4750 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004751
Bram Moolenaar875feea2017-06-11 16:07:51 +02004752 if (dict_find(what, (char_u *)"context", -1) != NULL)
4753 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004754
4755 if (dict_find(what, (char_u *)"items", -1) != NULL)
4756 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004757 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004758
Bram Moolenaard823fa92016-08-12 16:29:27 +02004759 if (flags & QF_GETLIST_TITLE)
4760 {
4761 char_u *t;
4762 t = qi->qf_lists[qf_idx].qf_title;
4763 if (t == NULL)
4764 t = (char_u *)"";
4765 status = dict_add_nr_str(retdict, "title", 0L, t);
4766 }
4767 if ((status == OK) && (flags & QF_GETLIST_NR))
4768 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4769 if ((status == OK) && (flags & QF_GETLIST_WINID))
4770 {
4771 win_T *win;
4772 win = qf_find_win(qi);
4773 if (win != NULL)
4774 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4775 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004776 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4777 {
4778 list_T *l = list_alloc();
4779 if (l != NULL)
4780 {
4781 (void)get_errorlist(wp, qf_idx, l);
4782 dict_add_list(retdict, "items", l);
4783 }
4784 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004785
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004786 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4787 {
4788 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4789 {
4790 di = dictitem_alloc((char_u *)"context");
4791 if (di != NULL)
4792 {
4793 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004794 if (dict_add(retdict, di) == FAIL)
4795 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004796 }
4797 }
4798 else
4799 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4800 }
4801
Bram Moolenaard823fa92016-08-12 16:29:27 +02004802 return status;
4803}
4804
4805/*
4806 * Add list of entries to quickfix/location list. Each list entry is
4807 * a dictionary with item information.
4808 */
4809 static int
4810qf_add_entries(
4811 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004812 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004813 list_T *list,
4814 char_u *title,
4815 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004816{
4817 listitem_T *li;
4818 dict_T *d;
4819 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004820 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004821 long lnum;
4822 int col, nr;
4823 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004824#ifdef FEAT_WINDOWS
4825 qfline_T *old_last = NULL;
4826#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004827 int valid, status;
4828 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004829 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004830
Bram Moolenaara3921f42017-06-04 15:30:34 +02004831 if (action == ' ' || qf_idx == qi->qf_listcount)
4832 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004833 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004834 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004835 qf_idx = qi->qf_curlist;
4836 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004837#ifdef FEAT_WINDOWS
Bram Moolenaara3921f42017-06-04 15:30:34 +02004838 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004839 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004840 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004841#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004842 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004843 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004844 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004845 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004846 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004847
4848 for (li = list->lv_first; li != NULL; li = li->li_next)
4849 {
4850 if (li->li_tv.v_type != VAR_DICT)
4851 continue; /* Skip non-dict items */
4852
4853 d = li->li_tv.vval.v_dict;
4854 if (d == NULL)
4855 continue;
4856
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004857 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004858 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4859 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4860 col = (int)get_dict_number(d, (char_u *)"col");
4861 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4862 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004863 type = get_dict_string(d, (char_u *)"type", TRUE);
4864 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4865 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004866 if (text == NULL)
4867 text = vim_strsave((char_u *)"");
4868
4869 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004870 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004871 valid = FALSE;
4872
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004873 /* Mark entries with non-existing buffer number as not valid. Give the
4874 * error message only once. */
4875 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4876 {
4877 if (!did_bufnr_emsg)
4878 {
4879 did_bufnr_emsg = TRUE;
4880 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4881 }
4882 valid = FALSE;
4883 bufnum = 0;
4884 }
4885
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02004886 /* If the 'valid' field is present it overrules the detected value. */
4887 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
4888 valid = (int)get_dict_number(d, (char_u *)"valid");
4889
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004890 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004891 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004892 NULL, /* dir */
4893 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004894 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004895 text,
4896 lnum,
4897 col,
4898 vcol, /* vis_col */
4899 pattern, /* search pattern */
4900 nr,
4901 type == NULL ? NUL : *type,
4902 valid);
4903
4904 vim_free(filename);
4905 vim_free(pattern);
4906 vim_free(text);
4907 vim_free(type);
4908
4909 if (status == FAIL)
4910 {
4911 retval = FAIL;
4912 break;
4913 }
4914 }
4915
Bram Moolenaara3921f42017-06-04 15:30:34 +02004916 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004917 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004918 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004919 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02004920 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004921 if (action != 'a')
4922 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02004923 qi->qf_lists[qf_idx].qf_ptr =
4924 qi->qf_lists[qf_idx].qf_start;
4925 if (qi->qf_lists[qf_idx].qf_count > 0)
4926 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004927 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004928
4929#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004930 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004931 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004932#endif
4933
4934 return retval;
4935}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004936
4937 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004938qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004939{
4940 dictitem_T *di;
4941 int retval = FAIL;
4942 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004943 int newlist = FALSE;
4944
4945 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4946 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004947
4948 qf_idx = qi->qf_curlist; /* default is the current list */
4949 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4950 {
4951 /* Use the specified quickfix/location list */
4952 if (di->di_tv.v_type == VAR_NUMBER)
4953 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004954 /* for zero use the current list */
4955 if (di->di_tv.vval.v_number != 0)
4956 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004957
4958 if ((action == ' ' || action == 'a') &&
4959 qf_idx == qi->qf_listcount)
4960 /*
4961 * When creating a new list, accept qf_idx pointing to the next
4962 * non-available list
4963 */
4964 newlist = TRUE;
4965 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004966 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004967 else
4968 newlist = FALSE; /* use the specified list */
Bram Moolenaar875feea2017-06-11 16:07:51 +02004969 } else if (di->di_tv.v_type == VAR_STRING &&
4970 STRCMP(di->di_tv.vval.v_string, "$") == 0 &&
4971 qi->qf_listcount > 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004972 {
Bram Moolenaar875feea2017-06-11 16:07:51 +02004973 qf_idx = qi->qf_listcount - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004974 newlist = FALSE;
4975 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004976 else
4977 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004978 }
4979
4980 if (newlist)
4981 {
4982 qf_new_list(qi, NULL);
4983 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004984 }
4985
4986 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4987 {
4988 if (di->di_tv.v_type == VAR_STRING)
4989 {
4990 vim_free(qi->qf_lists[qf_idx].qf_title);
4991 qi->qf_lists[qf_idx].qf_title =
4992 get_dict_string(what, (char_u *)"title", TRUE);
4993 if (qf_idx == qi->qf_curlist)
4994 qf_update_win_titlevar(qi);
4995 retval = OK;
4996 }
4997 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004998 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
4999 {
5000 if (di->di_tv.v_type == VAR_LIST)
5001 {
5002 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5003
5004 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5005 title_save, action == ' ' ? 'a' : action);
5006 vim_free(title_save);
5007 }
5008 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005009
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005010 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5011 {
5012 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005013
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005014 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005015 ctx = alloc_tv();
5016 if (ctx != NULL)
5017 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005018 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005019 }
5020
Bram Moolenaard823fa92016-08-12 16:29:27 +02005021 return retval;
5022}
5023
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005024/*
5025 * Find the non-location list window with the specified location list.
5026 */
5027 static win_T *
5028find_win_with_ll(qf_info_T *qi)
5029{
5030 win_T *wp = NULL;
5031
5032 FOR_ALL_WINDOWS(wp)
5033 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5034 return wp;
5035
5036 return NULL;
5037}
5038
5039/*
5040 * Free the entire quickfix/location list stack.
5041 * If the quickfix/location list window is open, then clear it.
5042 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005043 static void
5044qf_free_stack(win_T *wp, qf_info_T *qi)
5045{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005046 win_T *qfwin = qf_find_win(qi);
5047 win_T *llwin = NULL;
5048 win_T *orig_wp = wp;
5049
5050 if (qfwin != NULL)
5051 {
5052 /* If the quickfix/location list window is open, then clear it */
5053 if (qi->qf_curlist < qi->qf_listcount)
5054 qf_free(qi, qi->qf_curlist);
5055 qf_update_buffer(qi, NULL);
5056 }
5057
5058 if (wp != NULL && IS_LL_WINDOW(wp))
5059 {
5060 /* If in the location list window, then use the non-location list
5061 * window with this location list (if present)
5062 */
5063 llwin = find_win_with_ll(qi);
5064 if (llwin != NULL)
5065 wp = llwin;
5066 }
5067
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005068 qf_free_all(wp);
5069 if (wp == NULL)
5070 {
5071 /* quickfix list */
5072 qi->qf_curlist = 0;
5073 qi->qf_listcount = 0;
5074 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005075 else if (IS_LL_WINDOW(orig_wp))
5076 {
5077 /* If the location list window is open, then create a new empty
5078 * location list */
5079 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005080
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005081 /* first free the list reference in the location list window */
5082 ll_free_all(&orig_wp->w_llist_ref);
5083
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005084 orig_wp->w_llist_ref = new_ll;
5085 if (llwin != NULL)
5086 {
5087 llwin->w_llist = new_ll;
5088 new_ll->qf_refcount++;
5089 }
5090 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005091}
5092
Bram Moolenaard823fa92016-08-12 16:29:27 +02005093/*
5094 * Populate the quickfix list with the items supplied in the list
5095 * of dictionaries. "title" will be copied to w:quickfix_title.
5096 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5097 */
5098 int
5099set_errorlist(
5100 win_T *wp,
5101 list_T *list,
5102 int action,
5103 char_u *title,
5104 dict_T *what)
5105{
5106 qf_info_T *qi = &ql_info;
5107 int retval = OK;
5108
5109 if (wp != NULL)
5110 {
5111 qi = ll_get_or_alloc_list(wp);
5112 if (qi == NULL)
5113 return FAIL;
5114 }
5115
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005116 if (action == 'f')
5117 {
5118 /* Free the entire quickfix or location list stack */
5119 qf_free_stack(wp, qi);
5120 }
5121 else if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005122 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005123 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005124 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005125
5126 return retval;
5127}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005128
5129 static int
5130mark_quickfix_ctx(qf_info_T *qi, int copyID)
5131{
5132 int i;
5133 int abort = FALSE;
5134 typval_T *ctx;
5135
5136 for (i = 0; i < LISTCOUNT && !abort; ++i)
5137 {
5138 ctx = qi->qf_lists[i].qf_ctx;
5139 if (ctx != NULL && ctx->v_type != VAR_NUMBER &&
5140 ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
5141 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5142 }
5143
5144 return abort;
5145}
5146
5147/*
5148 * Mark the context of the quickfix list and the location lists (if present) as
5149 * "in use". So that garabage collection doesn't free the context.
5150 */
5151 int
5152set_ref_in_quickfix(int copyID)
5153{
5154 int abort = FALSE;
5155 tabpage_T *tp;
5156 win_T *win;
5157
5158 abort = mark_quickfix_ctx(&ql_info, copyID);
5159 if (abort)
5160 return abort;
5161
5162 FOR_ALL_TAB_WINDOWS(tp, win)
5163 {
5164 if (win->w_llist != NULL)
5165 {
5166 abort = mark_quickfix_ctx(win->w_llist, copyID);
5167 if (abort)
5168 return abort;
5169 }
5170 }
5171
5172 return abort;
5173}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005174#endif
5175
Bram Moolenaar81695252004-12-29 20:58:21 +00005176/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005177 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005178 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005179 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005180 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005181 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005182 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005183 */
5184 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005185ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005186{
5187 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005188 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005189#ifdef FEAT_AUTOCMD
5190 char_u *au_name = NULL;
5191#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005192
Bram Moolenaardb552d602006-03-23 22:59:57 +00005193 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5194 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005195 {
5196 qi = ll_get_or_alloc_list(curwin);
5197 if (qi == NULL)
5198 return;
5199 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005200
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005201#ifdef FEAT_AUTOCMD
5202 switch (eap->cmdidx)
5203 {
5204 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5205 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5206 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5207 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5208 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5209 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5210 default: break;
5211 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005212 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5213 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005214 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005215# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005216 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005217 return;
5218# endif
5219 }
5220#endif
5221
Bram Moolenaar86b68352004-12-27 21:59:20 +00005222 if (*eap->arg == NUL)
5223 buf = curbuf;
5224 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5225 buf = buflist_findnr(atoi((char *)eap->arg));
5226 if (buf == NULL)
5227 EMSG(_(e_invarg));
5228 else if (buf->b_ml.ml_mfp == NULL)
5229 EMSG(_("E681: Buffer is not loaded"));
5230 else
5231 {
5232 if (eap->addr_count == 0)
5233 {
5234 eap->line1 = 1;
5235 eap->line2 = buf->b_ml.ml_line_count;
5236 }
5237 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5238 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5239 EMSG(_(e_invrange));
5240 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005241 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005242 char_u *qf_title = *eap->cmdlinep;
5243
5244 if (buf->b_sfname)
5245 {
5246 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5247 (char *)qf_title, (char *)buf->b_sfname);
5248 qf_title = IObuff;
5249 }
5250
Bram Moolenaardb552d602006-03-23 22:59:57 +00005251 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
5252 (eap->cmdidx != CMD_caddbuffer
5253 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005254 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005255 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005256 {
5257#ifdef FEAT_AUTOCMD
5258 if (au_name != NULL)
5259 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5260 curbuf->b_fname, TRUE, curbuf);
5261#endif
5262 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5263 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5264 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005265 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005266 }
5267}
5268
Bram Moolenaar1e015462005-09-25 22:16:38 +00005269#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005270/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005271 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5272 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005273 */
5274 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005275ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005276{
5277 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005278 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005279#ifdef FEAT_AUTOCMD
5280 char_u *au_name = NULL;
5281#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005282
Bram Moolenaardb552d602006-03-23 22:59:57 +00005283 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5284 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005285 {
5286 qi = ll_get_or_alloc_list(curwin);
5287 if (qi == NULL)
5288 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005289 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005290
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005291#ifdef FEAT_AUTOCMD
5292 switch (eap->cmdidx)
5293 {
5294 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5295 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5296 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5297 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5298 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5299 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5300 default: break;
5301 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005302 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5303 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005304 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005305# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005306 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005307 return;
5308# endif
5309 }
5310#endif
5311
Bram Moolenaar4770d092006-01-12 23:22:24 +00005312 /* Evaluate the expression. When the result is a string or a list we can
5313 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005314 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005315 if (tv != NULL)
5316 {
5317 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5318 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5319 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01005320 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005321 (eap->cmdidx != CMD_caddexpr
5322 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005323 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5324 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005325 {
5326#ifdef FEAT_AUTOCMD
5327 if (au_name != NULL)
5328 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5329 curbuf->b_fname, TRUE, curbuf);
5330#endif
5331 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5332 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5333 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005334 }
5335 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005336 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005337 free_tv(tv);
5338 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005339}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005340#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005341
5342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 * ":helpgrep {pattern}"
5344 */
5345 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005346ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
5348 regmatch_T regmatch;
5349 char_u *save_cpo;
5350 char_u *p;
5351 int fcount;
5352 char_u **fnames;
5353 FILE *fd;
5354 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005356#ifdef FEAT_MULTI_LANG
5357 char_u *lang;
5358#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005359 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005360 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005361 int new_qi = FALSE;
5362 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005363#ifdef FEAT_AUTOCMD
5364 char_u *au_name = NULL;
5365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005367#ifdef FEAT_MULTI_LANG
5368 /* Check for a specified language */
5369 lang = check_help_lang(eap->arg);
5370#endif
5371
Bram Moolenaar73633f82012-01-20 13:39:07 +01005372#ifdef FEAT_AUTOCMD
5373 switch (eap->cmdidx)
5374 {
5375 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5376 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5377 default: break;
5378 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005379 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5380 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005381 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005382# ifdef FEAT_EVAL
5383 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005384 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005385# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005386 }
5387#endif
5388
5389 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5390 save_cpo = p_cpo;
5391 p_cpo = empty_option;
5392
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005393 if (eap->cmdidx == CMD_lhelpgrep)
5394 {
5395 /* Find an existing help window */
5396 FOR_ALL_WINDOWS(wp)
5397 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5398 break;
5399
5400 if (wp == NULL) /* Help window not found */
5401 qi = NULL;
5402 else
5403 qi = wp->w_llist;
5404
5405 if (qi == NULL)
5406 {
5407 /* Allocate a new location list for help text matches */
5408 if ((qi = ll_new_list()) == NULL)
5409 return;
5410 new_qi = TRUE;
5411 }
5412 }
5413
Bram Moolenaaree85df32017-03-19 14:19:50 +01005414 /* Autocommands may change the list. Save it for later comparison */
5415 save_qi = qi;
5416
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5418 regmatch.rm_ic = FALSE;
5419 if (regmatch.regprog != NULL)
5420 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005421#ifdef FEAT_MBYTE
5422 vimconv_T vc;
5423
5424 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5425 * differs. */
5426 vc.vc_type = CONV_NONE;
5427 if (!enc_utf8)
5428 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5429#endif
5430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005432 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433
5434 /* Go through all directories in 'runtimepath' */
5435 p = p_rtp;
5436 while (*p != NUL && !got_int)
5437 {
5438 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5439
5440 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5441 add_pathsep(NameBuff);
5442 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5443 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5444 &fnames, EW_FILE|EW_SILENT) == OK
5445 && fcount > 0)
5446 {
5447 for (fi = 0; fi < fcount && !got_int; ++fi)
5448 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005449#ifdef FEAT_MULTI_LANG
5450 /* Skip files for a different language. */
5451 if (lang != NULL
5452 && STRNICMP(lang, fnames[fi]
5453 + STRLEN(fnames[fi]) - 3, 2) != 0
5454 && !(STRNICMP(lang, "en", 2) == 0
5455 && STRNICMP("txt", fnames[fi]
5456 + STRLEN(fnames[fi]) - 3, 3) == 0))
5457 continue;
5458#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005459 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 if (fd != NULL)
5461 {
5462 lnum = 1;
5463 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5464 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005465 char_u *line = IObuff;
5466#ifdef FEAT_MBYTE
5467 /* Convert a line if 'encoding' is not utf-8 and
5468 * the line contains a non-ASCII character. */
5469 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005470 && has_non_ascii(IObuff))
5471 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005472 line = string_convert(&vc, IObuff, NULL);
5473 if (line == NULL)
5474 line = IObuff;
5475 }
5476#endif
5477
5478 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005480 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
5482 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005483 while (l > 0 && line[l - 1] <= ' ')
5484 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005486 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005487 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 NULL, /* dir */
5489 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005490 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005491 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005493 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005494 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005495 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005496 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 0, /* nr */
5498 1, /* type */
5499 TRUE /* valid */
5500 ) == FAIL)
5501 {
5502 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005503#ifdef FEAT_MBYTE
5504 if (line != IObuff)
5505 vim_free(line);
5506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 break;
5508 }
5509 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005510#ifdef FEAT_MBYTE
5511 if (line != IObuff)
5512 vim_free(line);
5513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 ++lnum;
5515 line_breakcheck();
5516 }
5517 fclose(fd);
5518 }
5519 }
5520 FreeWild(fcount, fnames);
5521 }
5522 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005523
Bram Moolenaar473de612013-06-08 18:19:48 +02005524 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005525#ifdef FEAT_MBYTE
5526 if (vc.vc_type != CONV_NONE)
5527 convert_setup(&vc, NULL, NULL);
5528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005530 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5531 qi->qf_lists[qi->qf_curlist].qf_ptr =
5532 qi->qf_lists[qi->qf_curlist].qf_start;
5533 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005536 if (p_cpo == empty_option)
5537 p_cpo = save_cpo;
5538 else
5539 /* Darn, some plugin changed the value. */
5540 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
5542#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005543 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544#endif
5545
Bram Moolenaar73633f82012-01-20 13:39:07 +01005546#ifdef FEAT_AUTOCMD
5547 if (au_name != NULL)
5548 {
5549 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5550 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005551 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005552 /* autocommands made "qi" invalid */
5553 return;
5554 }
5555#endif
5556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005558 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005559 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005560 else
5561 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005562
5563 if (eap->cmdidx == CMD_lhelpgrep)
5564 {
5565 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005566 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005567 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5568 {
5569 if (new_qi)
5570 ll_free_all(&qi);
5571 }
5572 else if (curwin->w_llist == NULL)
5573 curwin->w_llist = qi;
5574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575}
5576
5577#endif /* FEAT_QUICKFIX */