blob: 39eb5b63a29628181b2ae7802971027ba8972b4b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020050/*
51 * Quickfix/Location list definition
52 * Contains a list of entries (qfline_T). qf_start points to the first entry
53 * and qf_last points to the last entry. qf_count contains the list size.
54 *
55 * Usually the list contains one or more entries. But an empty list can be
56 * created using setqflist()/setloclist() with a title and/or user context
57 * information and entries can be added later using setqflist()/setloclist().
58 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000059typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000060{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020061 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000062 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020063 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020065 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 int qf_index; /* current index in the error list */
67 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020068 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020069 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020070 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020071
72 struct dir_stack_T *qf_dir_stack;
73 char_u *qf_directory;
74 struct dir_stack_T *qf_file_stack;
75 char_u *qf_currfile;
76 int qf_multiline;
77 int qf_multiignore;
78 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000079} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020081/*
82 * Quickfix/Location list stack definition
83 * Contains a list of quickfix/location lists (qf_list_T)
84 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000085struct qf_info_S
86{
87 /*
88 * Count of references to this list. Used only for location lists.
89 * When a location list window reference this list, qf_refcount
90 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
91 * reaches 0, the list is freed.
92 */
93 int qf_refcount;
94 int qf_listcount; /* current number of lists */
95 int qf_curlist; /* current error list */
96 qf_list_T qf_lists[LISTCOUNT];
97};
98
99static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200100static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000102#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
104/*
105 * Structure used to hold the info of one part of 'errorformat'
106 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000107typedef struct efm_S efm_T;
108struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109{
110 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000111 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
113 char_u prefix; /* prefix of this format line: */
114 /* 'D' enter directory */
115 /* 'X' leave directory */
116 /* 'A' start of multi-line message */
117 /* 'E' error message */
118 /* 'W' warning message */
119 /* 'I' informational message */
120 /* 'C' continuation line */
121 /* 'Z' end of multi-line message */
122 /* 'G' general, unspecific message */
123 /* 'P' push file (partial) message */
124 /* 'Q' pop/quit file (partial) message */
125 /* 'O' overread (partial) message */
126 char_u flags; /* additional flags given in prefix */
127 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000128 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000129 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130};
131
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100132static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
133
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200134static int qf_init_ext(qf_info_T *qi, int qf_idx, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title, char_u *enc);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200135static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void qf_new_list(qf_info_T *qi, char_u *qf_title);
137static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200138static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void qf_free(qf_info_T *qi, int idx);
141static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200142static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200143static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200145static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
147static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
150static int is_qf_win(win_T *win, qf_info_T *qi);
151static win_T *qf_find_win(qf_info_T *qi);
152static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200153static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200155static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static char_u *get_mef_name(void);
158static void restore_start_dir(char_u *dirname_start);
159static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
160static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
161static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
162static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164/* Quickfix window check helper macro */
165#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
166/* Location list window check helper macro */
167#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
168/*
169 * Return location list for window 'wp'
170 * For location list window, return the referenced location list
171 */
172#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
173
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200175 * Looking up a buffer can be slow if there are many. Remember the last one
176 * to make this a lot faster if there are multiple matches in the same file.
177 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200178static char_u *qf_last_bufname = NULL;
179static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200180
181/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200183 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 * Return -1 for error, number of errors for success.
185 */
186 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200187qf_init(win_T *wp,
188 char_u *efile,
189 char_u *errorformat,
190 int newlist, /* TRUE: start a new error list */
191 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
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200203 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
204 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000205}
206
207/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200208 * Maximum number of bytes allowed per line while reading a errorfile.
209 */
210#define LINE_MAXLEN 4096
211
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200212static struct fmtpattern
213{
214 char_u convchar;
215 char *pattern;
216} fmt_pat[FMT_PATTERNS] =
217 {
218 {'f', ".\\+"}, /* only used when at end */
219 {'n', "\\d\\+"},
220 {'l', "\\d\\+"},
221 {'c', "\\d\\+"},
222 {'t', "."},
223 {'m', ".\\+"},
224 {'r', ".*"},
225 {'p', "[- .]*"},
226 {'v', "\\d\\+"},
227 {'s', ".\\+"}
228 };
229
230/*
231 * Converts a 'errorformat' string to regular expression pattern
232 */
233 static int
234efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200235 char_u *efm,
236 int len,
237 efm_T *fmt_ptr,
238 char_u *regpat,
239 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200240{
241 char_u *ptr;
242 char_u *efmp;
243 char_u *srcptr;
244 int round;
245 int idx = 0;
246
247 /*
248 * Build regexp pattern from current 'errorformat' option
249 */
250 ptr = regpat;
251 *ptr++ = '^';
252 round = 0;
253 for (efmp = efm; efmp < efm + len; ++efmp)
254 {
255 if (*efmp == '%')
256 {
257 ++efmp;
258 for (idx = 0; idx < FMT_PATTERNS; ++idx)
259 if (fmt_pat[idx].convchar == *efmp)
260 break;
261 if (idx < FMT_PATTERNS)
262 {
263 if (fmt_ptr->addr[idx])
264 {
265 sprintf((char *)errmsg,
266 _("E372: Too many %%%c in format string"), *efmp);
267 EMSG(errmsg);
268 return -1;
269 }
270 if ((idx
271 && idx < 6
272 && vim_strchr((char_u *)"DXOPQ",
273 fmt_ptr->prefix) != NULL)
274 || (idx == 6
275 && vim_strchr((char_u *)"OPQ",
276 fmt_ptr->prefix) == NULL))
277 {
278 sprintf((char *)errmsg,
279 _("E373: Unexpected %%%c in format string"), *efmp);
280 EMSG(errmsg);
281 return -1;
282 }
283 fmt_ptr->addr[idx] = (char_u)++round;
284 *ptr++ = '\\';
285 *ptr++ = '(';
286#ifdef BACKSLASH_IN_FILENAME
287 if (*efmp == 'f')
288 {
289 /* Also match "c:" in the file name, even when
290 * checking for a colon next: "%f:".
291 * "\%(\a:\)\=" */
292 STRCPY(ptr, "\\%(\\a:\\)\\=");
293 ptr += 10;
294 }
295#endif
296 if (*efmp == 'f' && efmp[1] != NUL)
297 {
298 if (efmp[1] != '\\' && efmp[1] != '%')
299 {
300 /* A file name may contain spaces, but this isn't
301 * in "\f". For "%f:%l:%m" there may be a ":" in
302 * the file name. Use ".\{-1,}x" instead (x is
303 * the next character), the requirement that :999:
304 * follows should work. */
305 STRCPY(ptr, ".\\{-1,}");
306 ptr += 7;
307 }
308 else
309 {
310 /* File name followed by '\\' or '%': include as
311 * many file name chars as possible. */
312 STRCPY(ptr, "\\f\\+");
313 ptr += 4;
314 }
315 }
316 else
317 {
318 srcptr = (char_u *)fmt_pat[idx].pattern;
319 while ((*ptr = *srcptr++) != NUL)
320 ++ptr;
321 }
322 *ptr++ = '\\';
323 *ptr++ = ')';
324 }
325 else if (*efmp == '*')
326 {
327 if (*++efmp == '[' || *efmp == '\\')
328 {
329 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
330 {
331 if (efmp[1] == '^')
332 *ptr++ = *++efmp;
333 if (efmp < efm + len)
334 {
335 *ptr++ = *++efmp; /* could be ']' */
336 while (efmp < efm + len
337 && (*ptr++ = *++efmp) != ']')
338 /* skip */;
339 if (efmp == efm + len)
340 {
341 EMSG(_("E374: Missing ] in format string"));
342 return -1;
343 }
344 }
345 }
346 else if (efmp < efm + len) /* %*\D, %*\s etc. */
347 *ptr++ = *++efmp;
348 *ptr++ = '\\';
349 *ptr++ = '+';
350 }
351 else
352 {
353 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
354 sprintf((char *)errmsg,
355 _("E375: Unsupported %%%c in format string"), *efmp);
356 EMSG(errmsg);
357 return -1;
358 }
359 }
360 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
361 *ptr++ = *efmp; /* regexp magic characters */
362 else if (*efmp == '#')
363 *ptr++ = '*';
364 else if (*efmp == '>')
365 fmt_ptr->conthere = TRUE;
366 else if (efmp == efm + 1) /* analyse prefix */
367 {
368 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
369 fmt_ptr->flags = *efmp++;
370 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
371 fmt_ptr->prefix = *efmp;
372 else
373 {
374 sprintf((char *)errmsg,
375 _("E376: Invalid %%%c in format string prefix"), *efmp);
376 EMSG(errmsg);
377 return -1;
378 }
379 }
380 else
381 {
382 sprintf((char *)errmsg,
383 _("E377: Invalid %%%c in format string"), *efmp);
384 EMSG(errmsg);
385 return -1;
386 }
387 }
388 else /* copy normal character */
389 {
390 if (*efmp == '\\' && efmp + 1 < efm + len)
391 ++efmp;
392 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
393 *ptr++ = '\\'; /* escape regexp atoms */
394 if (*efmp)
395 *ptr++ = *efmp;
396 }
397 }
398 *ptr++ = '$';
399 *ptr = NUL;
400
401 return 0;
402}
403
404 static void
405free_efm_list(efm_T **efm_first)
406{
407 efm_T *efm_ptr;
408
409 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
410 {
411 *efm_first = efm_ptr->next;
412 vim_regfree(efm_ptr->prog);
413 vim_free(efm_ptr);
414 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100415 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200416}
417
418/* Parse 'errorformat' option */
419 static efm_T *
420parse_efm_option(char_u *efm)
421{
422 char_u *errmsg = NULL;
423 int errmsglen;
424 efm_T *fmt_ptr = NULL;
425 efm_T *fmt_first = NULL;
426 efm_T *fmt_last = NULL;
427 char_u *fmtstr = NULL;
428 int len;
429 int i;
430 int round;
431
432 errmsglen = CMDBUFFSIZE + 1;
433 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
434 if (errmsg == NULL)
435 goto parse_efm_end;
436
437 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200438 * Each part of the format string is copied and modified from errorformat
439 * to regex prog. Only a few % characters are allowed.
440 */
441
442 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 * Get some space to modify the format string into.
444 */
445 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
446 for (round = FMT_PATTERNS; round > 0; )
447 i += (int)STRLEN(fmt_pat[--round].pattern);
448#ifdef COLON_IN_FILENAME
449 i += 12; /* "%f" can become twelve chars longer */
450#else
451 i += 2; /* "%f" can become two chars longer */
452#endif
453 if ((fmtstr = alloc(i)) == NULL)
454 goto parse_efm_error;
455
456 while (efm[0] != NUL)
457 {
458 /*
459 * Allocate a new eformat structure and put it at the end of the list
460 */
461 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
462 if (fmt_ptr == NULL)
463 goto parse_efm_error;
464 if (fmt_first == NULL) /* first one */
465 fmt_first = fmt_ptr;
466 else
467 fmt_last->next = fmt_ptr;
468 fmt_last = fmt_ptr;
469
470 /*
471 * Isolate one part in the 'errorformat' option
472 */
473 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
474 if (efm[len] == '\\' && efm[len + 1] != NUL)
475 ++len;
476
477 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
478 goto parse_efm_error;
479 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
480 goto parse_efm_error;
481 /*
482 * Advance to next part
483 */
484 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
485 }
486
487 if (fmt_first == NULL) /* nothing found */
488 EMSG(_("E378: 'errorformat' contains no pattern"));
489
490 goto parse_efm_end;
491
492parse_efm_error:
493 free_efm_list(&fmt_first);
494
495parse_efm_end:
496 vim_free(fmtstr);
497 vim_free(errmsg);
498
499 return fmt_first;
500}
501
Bram Moolenaare0d37972016-07-15 22:36:01 +0200502enum {
503 QF_FAIL = 0,
504 QF_OK = 1,
505 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200506 QF_NOMEM = 3,
507 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200508};
509
510typedef struct {
511 char_u *linebuf;
512 int linelen;
513 char_u *growbuf;
514 int growbufsiz;
515 FILE *fd;
516 typval_T *tv;
517 char_u *p_str;
518 listitem_T *p_li;
519 buf_T *buf;
520 linenr_T buflnum;
521 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100522 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200523} qfstate_T;
524
525 static char_u *
526qf_grow_linebuf(qfstate_T *state, int newsz)
527{
528 /*
529 * If the line exceeds LINE_MAXLEN exclude the last
530 * byte since it's not a NL character.
531 */
532 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
533 if (state->growbuf == NULL)
534 {
535 state->growbuf = alloc(state->linelen + 1);
536 if (state->growbuf == NULL)
537 return NULL;
538 state->growbufsiz = state->linelen;
539 }
540 else if (state->linelen > state->growbufsiz)
541 {
542 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
543 if (state->growbuf == NULL)
544 return NULL;
545 state->growbufsiz = state->linelen;
546 }
547 return state->growbuf;
548}
549
550/*
551 * Get the next string (separated by newline) from state->p_str.
552 */
553 static int
554qf_get_next_str_line(qfstate_T *state)
555{
556 /* Get the next line from the supplied string */
557 char_u *p_str = state->p_str;
558 char_u *p;
559 int len;
560
561 if (*p_str == NUL) /* Reached the end of the string */
562 return QF_END_OF_INPUT;
563
564 p = vim_strchr(p_str, '\n');
565 if (p != NULL)
566 len = (int)(p - p_str) + 1;
567 else
568 len = (int)STRLEN(p_str);
569
570 if (len > IOSIZE - 2)
571 {
572 state->linebuf = qf_grow_linebuf(state, len);
573 if (state->linebuf == NULL)
574 return QF_NOMEM;
575 }
576 else
577 {
578 state->linebuf = IObuff;
579 state->linelen = len;
580 }
581 vim_strncpy(state->linebuf, p_str, state->linelen);
582
583 /*
584 * Increment using len in order to discard the rest of the
585 * line if it exceeds LINE_MAXLEN.
586 */
587 p_str += len;
588 state->p_str = p_str;
589
590 return QF_OK;
591}
592
593/*
594 * Get the next string from state->p_Li.
595 */
596 static int
597qf_get_next_list_line(qfstate_T *state)
598{
599 listitem_T *p_li = state->p_li;
600 int len;
601
602 while (p_li != NULL
603 && (p_li->li_tv.v_type != VAR_STRING
604 || p_li->li_tv.vval.v_string == NULL))
605 p_li = p_li->li_next; /* Skip non-string items */
606
607 if (p_li == NULL) /* End of the list */
608 {
609 state->p_li = NULL;
610 return QF_END_OF_INPUT;
611 }
612
613 len = (int)STRLEN(p_li->li_tv.vval.v_string);
614 if (len > IOSIZE - 2)
615 {
616 state->linebuf = qf_grow_linebuf(state, len);
617 if (state->linebuf == NULL)
618 return QF_NOMEM;
619 }
620 else
621 {
622 state->linebuf = IObuff;
623 state->linelen = len;
624 }
625
626 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
627
628 state->p_li = p_li->li_next; /* next item */
629 return QF_OK;
630}
631
632/*
633 * Get the next string from state->buf.
634 */
635 static int
636qf_get_next_buf_line(qfstate_T *state)
637{
638 char_u *p_buf = NULL;
639 int len;
640
641 /* Get the next line from the supplied buffer */
642 if (state->buflnum > state->lnumlast)
643 return QF_END_OF_INPUT;
644
645 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
646 state->buflnum += 1;
647
648 len = (int)STRLEN(p_buf);
649 if (len > IOSIZE - 2)
650 {
651 state->linebuf = qf_grow_linebuf(state, len);
652 if (state->linebuf == NULL)
653 return QF_NOMEM;
654 }
655 else
656 {
657 state->linebuf = IObuff;
658 state->linelen = len;
659 }
660 vim_strncpy(state->linebuf, p_buf, state->linelen);
661
662 return QF_OK;
663}
664
665/*
666 * Get the next string from file state->fd.
667 */
668 static int
669qf_get_next_file_line(qfstate_T *state)
670{
671 int discard;
672 int growbuflen;
673
674 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
675 return QF_END_OF_INPUT;
676
677 discard = FALSE;
678 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200679 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200680 {
681 /*
682 * The current line exceeds IObuff, continue reading using
683 * growbuf until EOL or LINE_MAXLEN bytes is read.
684 */
685 if (state->growbuf == NULL)
686 {
687 state->growbufsiz = 2 * (IOSIZE - 1);
688 state->growbuf = alloc(state->growbufsiz);
689 if (state->growbuf == NULL)
690 return QF_NOMEM;
691 }
692
693 /* Copy the read part of the line, excluding null-terminator */
694 memcpy(state->growbuf, IObuff, IOSIZE - 1);
695 growbuflen = state->linelen;
696
697 for (;;)
698 {
699 if (fgets((char *)state->growbuf + growbuflen,
700 state->growbufsiz - growbuflen, state->fd) == NULL)
701 break;
702 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
703 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200704 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200705 break;
706 if (state->growbufsiz == LINE_MAXLEN)
707 {
708 discard = TRUE;
709 break;
710 }
711
712 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
713 ? 2 * state->growbufsiz : LINE_MAXLEN;
714 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
715 if (state->growbuf == NULL)
716 return QF_NOMEM;
717 }
718
719 while (discard)
720 {
721 /*
722 * The current line is longer than LINE_MAXLEN, continue
723 * reading but discard everything until EOL or EOF is
724 * reached.
725 */
726 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
727 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200728 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200729 break;
730 }
731
732 state->linebuf = state->growbuf;
733 state->linelen = growbuflen;
734 }
735 else
736 state->linebuf = IObuff;
737
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100738#ifdef FEAT_MBYTE
739 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200740 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
741 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100742 char_u *line;
743
744 line = string_convert(&state->vc, state->linebuf, &state->linelen);
745 if (line != NULL)
746 {
747 if (state->linelen < IOSIZE)
748 {
749 STRCPY(state->linebuf, line);
750 vim_free(line);
751 }
752 else
753 {
754 vim_free(state->growbuf);
755 state->linebuf = state->growbuf = line;
756 state->growbufsiz = state->linelen < LINE_MAXLEN
757 ? state->linelen : LINE_MAXLEN;
758 }
759 }
760 }
761#endif
762
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 return QF_OK;
764}
765
766/*
767 * Get the next string from a file/buffer/list/string.
768 */
769 static int
770qf_get_nextline(qfstate_T *state)
771{
772 int status = QF_FAIL;
773
774 if (state->fd == NULL)
775 {
776 if (state->tv != NULL)
777 {
778 if (state->tv->v_type == VAR_STRING)
779 /* Get the next line from the supplied string */
780 status = qf_get_next_str_line(state);
781 else if (state->tv->v_type == VAR_LIST)
782 /* Get the next line from the supplied list */
783 status = qf_get_next_list_line(state);
784 }
785 else
786 /* Get the next line from the supplied buffer */
787 status = qf_get_next_buf_line(state);
788 }
789 else
790 /* Get the next line from the supplied file */
791 status = qf_get_next_file_line(state);
792
793 if (status != QF_OK)
794 return status;
795
796 /* remove newline/CR from the line */
797 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200798 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799 state->linebuf[state->linelen - 1] = NUL;
800#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200801 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
802 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200803#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200804 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200805
806#ifdef FEAT_MBYTE
807 remove_bom(state->linebuf);
808#endif
809
810 return QF_OK;
811}
812
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200813typedef struct {
814 char_u *namebuf;
815 char_u *errmsg;
816 int errmsglen;
817 long lnum;
818 int col;
819 char_u use_viscol;
820 char_u *pattern;
821 int enr;
822 int type;
823 int valid;
824} qffields_T;
825
826/*
827 * Parse a line and get the quickfix fields.
828 * Return the QF_ status.
829 */
830 static int
831qf_parse_line(
832 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200833 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200834 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;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200846 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200847
848 /* Always ignore case when looking for a matching error. */
849 regmatch.rm_ic = TRUE;
850
851 /* If there was no %> item start at the first pattern */
852 if (fmt_start == NULL)
853 fmt_ptr = fmt_first;
854 else
855 {
856 fmt_ptr = fmt_start;
857 fmt_start = NULL;
858 }
859
860 /*
861 * Try to match each part of 'errorformat' until we find a complete
862 * match or no match.
863 */
864 fields->valid = TRUE;
865restofline:
866 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
867 {
868 int r;
869
870 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200871 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200872 continue;
873 fields->namebuf[0] = NUL;
874 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200875 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200876 fields->errmsg[0] = NUL;
877 fields->lnum = 0;
878 fields->col = 0;
879 fields->use_viscol = FALSE;
880 fields->enr = -1;
881 fields->type = 0;
882 tail = NULL;
883
884 regmatch.regprog = fmt_ptr->prog;
885 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
886 fmt_ptr->prog = regmatch.regprog;
887 if (r)
888 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200889 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200890 continue;
891 if (vim_strchr((char_u *)"EWI", idx) != NULL)
892 fields->type = idx;
893 else
894 fields->type = 0;
895 /*
896 * Extract error message data from matched line.
897 * We check for an actual submatch, because "\[" and "\]" in
898 * the 'errorformat' may cause the wrong submatch to be used.
899 */
900 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
901 {
902 int c;
903
904 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
905 continue;
906
907 /* Expand ~/file and $HOME/file to full path. */
908 c = *regmatch.endp[i];
909 *regmatch.endp[i] = NUL;
910 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
911 *regmatch.endp[i] = c;
912
913 if (vim_strchr((char_u *)"OPQ", idx) != NULL
914 && mch_getperm(fields->namebuf) == -1)
915 continue;
916 }
917 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
918 {
919 if (regmatch.startp[i] == NULL)
920 continue;
921 fields->enr = (int)atol((char *)regmatch.startp[i]);
922 }
923 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
924 {
925 if (regmatch.startp[i] == NULL)
926 continue;
927 fields->lnum = atol((char *)regmatch.startp[i]);
928 }
929 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
930 {
931 if (regmatch.startp[i] == NULL)
932 continue;
933 fields->col = (int)atol((char *)regmatch.startp[i]);
934 }
935 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
936 {
937 if (regmatch.startp[i] == NULL)
938 continue;
939 fields->type = *regmatch.startp[i];
940 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200941 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200942 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200943 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200944 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200945 /* linelen + null terminator */
946 if ((fields->errmsg = vim_realloc(fields->errmsg,
947 linelen + 1)) == NULL)
948 return QF_NOMEM;
949 fields->errmsglen = linelen + 1;
950 }
951 vim_strncpy(fields->errmsg, linebuf, linelen);
952 }
953 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
954 {
955 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
956 continue;
957 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200958 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200959 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200960 /* len + null terminator */
961 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
962 == NULL)
963 return QF_NOMEM;
964 fields->errmsglen = len + 1;
965 }
966 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
967 }
968 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
969 {
970 if (regmatch.startp[i] == NULL)
971 continue;
972 tail = regmatch.startp[i];
973 }
974 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
975 {
976 char_u *match_ptr;
977
978 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
979 continue;
980 fields->col = 0;
981 for (match_ptr = regmatch.startp[i];
982 match_ptr != regmatch.endp[i]; ++match_ptr)
983 {
984 ++fields->col;
985 if (*match_ptr == TAB)
986 {
987 fields->col += 7;
988 fields->col -= fields->col % 8;
989 }
990 }
991 ++fields->col;
992 fields->use_viscol = TRUE;
993 }
994 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
995 {
996 if (regmatch.startp[i] == NULL)
997 continue;
998 fields->col = (int)atol((char *)regmatch.startp[i]);
999 fields->use_viscol = TRUE;
1000 }
1001 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1002 {
1003 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1004 continue;
1005 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1006 if (len > CMDBUFFSIZE - 5)
1007 len = CMDBUFFSIZE - 5;
1008 STRCPY(fields->pattern, "^\\V");
1009 STRNCAT(fields->pattern, regmatch.startp[i], len);
1010 fields->pattern[len + 3] = '\\';
1011 fields->pattern[len + 4] = '$';
1012 fields->pattern[len + 5] = NUL;
1013 }
1014 break;
1015 }
1016 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001017 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001018
1019 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1020 {
1021 if (fmt_ptr != NULL)
1022 {
1023 if (idx == 'D') /* enter directory */
1024 {
1025 if (*fields->namebuf == NUL)
1026 {
1027 EMSG(_("E379: Missing or empty directory name"));
1028 return QF_FAIL;
1029 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001030 qfl->qf_directory =
1031 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1032 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001033 return QF_FAIL;
1034 }
1035 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001036 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001037 }
1038 fields->namebuf[0] = NUL; /* no match found, remove file name */
1039 fields->lnum = 0; /* don't jump to this line */
1040 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001041 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001042 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001043 /* linelen + null terminator */
1044 if ((fields->errmsg = vim_realloc(fields->errmsg,
1045 linelen + 1)) == NULL)
1046 return QF_NOMEM;
1047 fields->errmsglen = linelen + 1;
1048 }
1049 /* copy whole line to error message */
1050 vim_strncpy(fields->errmsg, linebuf, linelen);
1051 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001052 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001053 }
1054 else if (fmt_ptr != NULL)
1055 {
1056 /* honor %> item */
1057 if (fmt_ptr->conthere)
1058 fmt_start = fmt_ptr;
1059
1060 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1061 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001062 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1063 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001064 }
1065 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1066 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001067 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001068 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001069 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001070
Bram Moolenaar9b457942016-10-09 16:10:05 +02001071 if (qfprev == NULL)
1072 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001073 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001074 {
1075 len = (int)STRLEN(qfprev->qf_text);
1076 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1077 == NULL)
1078 return QF_FAIL;
1079 STRCPY(ptr, qfprev->qf_text);
1080 vim_free(qfprev->qf_text);
1081 qfprev->qf_text = ptr;
1082 *(ptr += len) = '\n';
1083 STRCPY(++ptr, fields->errmsg);
1084 }
1085 if (qfprev->qf_nr == -1)
1086 qfprev->qf_nr = fields->enr;
1087 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1088 /* only printable chars allowed */
1089 qfprev->qf_type = fields->type;
1090
1091 if (!qfprev->qf_lnum)
1092 qfprev->qf_lnum = fields->lnum;
1093 if (!qfprev->qf_col)
1094 qfprev->qf_col = fields->col;
1095 qfprev->qf_viscol = fields->use_viscol;
1096 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001097 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1098 qfl->qf_directory,
1099 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001100 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001101 : qfl->qf_currfile != NULL && fields->valid
1102 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001103 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001104 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001105 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001106 line_breakcheck();
1107 return QF_IGNORE_LINE;
1108 }
1109 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1110 {
1111 /* global file names */
1112 fields->valid = FALSE;
1113 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1114 {
1115 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001116 qfl->qf_currfile =
1117 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001118 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001119 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001120 *fields->namebuf = NUL;
1121 if (tail && *tail)
1122 {
1123 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001124 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001125 goto restofline;
1126 }
1127 }
1128 }
1129 if (fmt_ptr->flags == '-') /* generally exclude this line */
1130 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001131 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001132 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001133 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001134 return QF_IGNORE_LINE;
1135 }
1136 }
1137
1138 return QF_OK;
1139}
1140
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001141/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001142 * Read the errorfile "efile" into memory, line by line, building the error
1143 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001144 * Alternative: when "efile" is NULL read errors from buffer "buf".
1145 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001146 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001147 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1148 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001149 * Return -1 for error, number of errors for success.
1150 */
1151 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001152qf_init_ext(
1153 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001154 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001155 char_u *efile,
1156 buf_T *buf,
1157 typval_T *tv,
1158 char_u *errorformat,
1159 int newlist, /* TRUE: start a new error list */
1160 linenr_T lnumfirst, /* first line number to use */
1161 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001162 char_u *qf_title,
1163 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001164{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001165 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001166 qfstate_T state;
1167 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001168#ifdef FEAT_WINDOWS
1169 qfline_T *old_last = NULL;
1170#endif
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001171 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001172 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001174 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001176 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001178 /* Do not used the cached buffer, it may have been wiped out. */
1179 vim_free(qf_last_bufname);
1180 qf_last_bufname = NULL;
1181
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001182 vim_memset(&state, 0, sizeof(state));
1183 vim_memset(&fields, 0, sizeof(fields));
1184#ifdef FEAT_MBYTE
1185 state.vc.vc_type = CONV_NONE;
1186 if (enc != NULL && *enc != NUL)
1187 convert_setup(&state.vc, enc, p_enc);
1188#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001189 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1190 fields.errmsglen = CMDBUFFSIZE + 1;
1191 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1192 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001193 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 goto qf_init_end;
1195
Bram Moolenaare0d37972016-07-15 22:36:01 +02001196 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
1198 EMSG2(_(e_openerrf), efile);
1199 goto qf_init_end;
1200 }
1201
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001202 if (newlist || qf_idx == qi->qf_listcount)
1203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001205 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001206 qf_idx = qi->qf_curlist;
1207 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001208 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001209 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001210 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001211 adding = TRUE;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001212#ifdef FEAT_WINDOWS
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001213 if (qi->qf_lists[qf_idx].qf_count > 0)
1214 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001215#endif
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001218 qfl = &qi->qf_lists[qf_idx];
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001221 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001222 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 else
1224 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001226 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001227 * If the errorformat didn't change between calls, then reuse the
1228 * previously parsed values.
1229 */
1230 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1231 {
1232 /* free the previously parsed data */
1233 vim_free(last_efm);
1234 last_efm = NULL;
1235 free_efm_list(&fmt_first);
1236
1237 /* parse the current 'efm' */
1238 fmt_first = parse_efm_option(efm);
1239 if (fmt_first != NULL)
1240 last_efm = vim_strsave(efm);
1241 }
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
1246 /*
1247 * got_int is reset here, because it was probably set when killing the
1248 * ":make" command, but we still want to read the errorfile then.
1249 */
1250 got_int = FALSE;
1251
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001252 if (tv != NULL)
1253 {
1254 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001255 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001256 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001257 state.p_li = tv->vval.v_list->lv_first;
1258 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001259 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001260 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001261 state.buflnum = lnumfirst;
1262 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /*
1265 * Read the lines in the error file one by one.
1266 * Try to recognize one of the error formats in each line.
1267 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001268 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001270 /* Get the next line from a file/buffer/list/string */
1271 status = qf_get_nextline(&state);
1272 if (status == QF_NOMEM) /* memory alloc failure */
1273 goto qf_init_end;
1274 if (status == QF_END_OF_INPUT) /* end of input */
1275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001277 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1278 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001279 if (status == QF_FAIL)
1280 goto error2;
1281 if (status == QF_NOMEM)
1282 goto qf_init_end;
1283 if (status == QF_IGNORE_LINE)
1284 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001286 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001287 qf_idx,
1288 qfl->qf_directory,
1289 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001290 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001291 : ((qfl->qf_currfile != NULL && fields.valid)
1292 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001293 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001294 fields.errmsg,
1295 fields.lnum,
1296 fields.col,
1297 fields.use_viscol,
1298 fields.pattern,
1299 fields.enr,
1300 fields.type,
1301 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 goto error2;
1303 line_breakcheck();
1304 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001305 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001307 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001309 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001310 qfl->qf_ptr = qfl->qf_start;
1311 qfl->qf_index = 1;
1312 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 else
1315 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001316 qfl->qf_nonevalid = FALSE;
1317 if (qfl->qf_ptr == NULL)
1318 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001320 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001321 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001322 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324 EMSG(_(e_readerrf));
1325error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001326 if (!adding)
1327 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001328 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001329 qf_free(qi, qi->qf_curlist);
1330 qi->qf_listcount--;
1331 if (qi->qf_curlist > 0)
1332 --qi->qf_curlist;
1333 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001334qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001335 if (state.fd != NULL)
1336 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001337 vim_free(fields.namebuf);
1338 vim_free(fields.errmsg);
1339 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001340 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342#ifdef FEAT_WINDOWS
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001343 if (qf_idx == qi->qf_curlist)
1344 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001346#ifdef FEAT_MBYTE
1347 if (state.vc.vc_type != CONV_NONE)
1348 convert_setup(&state.vc, NULL, NULL);
1349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
1351 return retval;
1352}
1353
Bram Moolenaarfb604092014-07-23 15:55:00 +02001354 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001355qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001356{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001357 vim_free(qi->qf_lists[qf_idx].qf_title);
1358 qi->qf_lists[qf_idx].qf_title = NULL;
1359
Bram Moolenaarfb604092014-07-23 15:55:00 +02001360 if (title != NULL)
1361 {
1362 char_u *p = alloc((int)STRLEN(title) + 2);
1363
Bram Moolenaara3921f42017-06-04 15:30:34 +02001364 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001365 if (p != NULL)
1366 sprintf((char *)p, ":%s", (char *)title);
1367 }
1368}
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001371 * Prepare for adding a new quickfix list. If the current list is in the
1372 * middle of the stack, then all the following lists are freed and then
1373 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 */
1375 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001376qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 int i;
1379
1380 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001381 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 * the current entry. This makes it possible to browse in a tree-like
1383 * way with ":grep'.
1384 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001385 while (qi->qf_listcount > qi->qf_curlist + 1)
1386 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
1388 /*
1389 * When the stack is full, remove to oldest entry
1390 * Otherwise, add a new entry.
1391 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001392 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001394 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001396 qi->qf_lists[i - 1] = qi->qf_lists[i];
1397 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001400 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001401 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001402 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001403 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404}
1405
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001406/*
1407 * Free a location list
1408 */
1409 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001410ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001411{
1412 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001413 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001414
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001415 qi = *pqi;
1416 if (qi == NULL)
1417 return;
1418 *pqi = NULL; /* Remove reference to this list */
1419
1420 qi->qf_refcount--;
1421 if (qi->qf_refcount < 1)
1422 {
1423 /* No references to this location list */
1424 for (i = 0; i < qi->qf_listcount; ++i)
1425 qf_free(qi, i);
1426 vim_free(qi);
1427 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001428}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001429
1430 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001431qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001432{
1433 int i;
1434 qf_info_T *qi = &ql_info;
1435
1436 if (wp != NULL)
1437 {
1438 /* location list */
1439 ll_free_all(&wp->w_llist);
1440 ll_free_all(&wp->w_llist_ref);
1441 }
1442 else
1443 /* quickfix list */
1444 for (i = 0; i < qi->qf_listcount; ++i)
1445 qf_free(qi, i);
1446}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001447
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448/*
1449 * Add an entry to the end of the list of errors.
1450 * Returns OK or FAIL.
1451 */
1452 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001453qf_add_entry(
1454 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001455 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001456 char_u *dir, /* optional directory name */
1457 char_u *fname, /* file name or NULL */
1458 int bufnum, /* buffer number or zero */
1459 char_u *mesg, /* message */
1460 long lnum, /* line number */
1461 int col, /* column */
1462 int vis_col, /* using visual column */
1463 char_u *pattern, /* search pattern */
1464 int nr, /* error number */
1465 int type, /* type character */
1466 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001468 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001469 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001471 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001473 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001474 {
1475 buf_T *buf = buflist_findnr(bufnum);
1476
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001477 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001478 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001479 buf->b_has_qf_entry |=
1480 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001481 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001482 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001483 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1485 {
1486 vim_free(qfp);
1487 return FAIL;
1488 }
1489 qfp->qf_lnum = lnum;
1490 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001491 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001492 if (pattern == NULL || *pattern == NUL)
1493 qfp->qf_pattern = NULL;
1494 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1495 {
1496 vim_free(qfp->qf_text);
1497 vim_free(qfp);
1498 return FAIL;
1499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 qfp->qf_nr = nr;
1501 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1502 type = 0;
1503 qfp->qf_type = type;
1504 qfp->qf_valid = valid;
1505
Bram Moolenaara3921f42017-06-04 15:30:34 +02001506 lastp = &qi->qf_lists[qf_idx].qf_last;
1507 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001508 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001510 qi->qf_lists[qf_idx].qf_start = qfp;
1511 qi->qf_lists[qf_idx].qf_ptr = qfp;
1512 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001513 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 }
1515 else
1516 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001517 qfp->qf_prev = *lastp;
1518 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001520 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001522 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001523 ++qi->qf_lists[qf_idx].qf_count;
1524 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001525 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001527 qi->qf_lists[qf_idx].qf_index =
1528 qi->qf_lists[qf_idx].qf_count;
1529 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 }
1531
1532 return OK;
1533}
1534
1535/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001536 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001537 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001538 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001539ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001540{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001541 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001542
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001543 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1544 if (qi != NULL)
1545 {
1546 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1547 qi->qf_refcount++;
1548 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001549
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001550 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001551}
1552
1553/*
1554 * Return the location list for window 'wp'.
1555 * If not present, allocate a location list
1556 */
1557 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001558ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001559{
1560 if (IS_LL_WINDOW(wp))
1561 /* For a location list window, use the referenced location list */
1562 return wp->w_llist_ref;
1563
1564 /*
1565 * For a non-location list window, w_llist_ref should not point to a
1566 * location list.
1567 */
1568 ll_free_all(&wp->w_llist_ref);
1569
1570 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001571 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001572 return wp->w_llist;
1573}
1574
1575/*
1576 * Copy the location list from window "from" to window "to".
1577 */
1578 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001579copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001580{
1581 qf_info_T *qi;
1582 int idx;
1583 int i;
1584
1585 /*
1586 * When copying from a location list window, copy the referenced
1587 * location list. For other windows, copy the location list for
1588 * that window.
1589 */
1590 if (IS_LL_WINDOW(from))
1591 qi = from->w_llist_ref;
1592 else
1593 qi = from->w_llist;
1594
1595 if (qi == NULL) /* no location list to copy */
1596 return;
1597
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001598 /* allocate a new location list */
1599 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001600 return;
1601
1602 to->w_llist->qf_listcount = qi->qf_listcount;
1603
1604 /* Copy the location lists one at a time */
1605 for (idx = 0; idx < qi->qf_listcount; idx++)
1606 {
1607 qf_list_T *from_qfl;
1608 qf_list_T *to_qfl;
1609
1610 to->w_llist->qf_curlist = idx;
1611
1612 from_qfl = &qi->qf_lists[idx];
1613 to_qfl = &to->w_llist->qf_lists[idx];
1614
1615 /* Some of the fields are populated by qf_add_entry() */
1616 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1617 to_qfl->qf_count = 0;
1618 to_qfl->qf_index = 0;
1619 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001620 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001621 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001622 if (from_qfl->qf_title != NULL)
1623 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1624 else
1625 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001626 if (from_qfl->qf_ctx != NULL)
1627 {
1628 to_qfl->qf_ctx = alloc_tv();
1629 if (to_qfl->qf_ctx != NULL)
1630 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1631 }
1632 else
1633 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001634
1635 if (from_qfl->qf_count)
1636 {
1637 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001638 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001639
1640 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001641 for (i = 0, from_qfp = from_qfl->qf_start;
1642 i < from_qfl->qf_count && from_qfp != NULL;
1643 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001645 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001646 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001647 NULL,
1648 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001649 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001650 from_qfp->qf_text,
1651 from_qfp->qf_lnum,
1652 from_qfp->qf_col,
1653 from_qfp->qf_viscol,
1654 from_qfp->qf_pattern,
1655 from_qfp->qf_nr,
1656 0,
1657 from_qfp->qf_valid) == FAIL)
1658 {
1659 qf_free_all(to);
1660 return;
1661 }
1662 /*
1663 * qf_add_entry() will not set the qf_num field, as the
1664 * directory and file names are not supplied. So the qf_fnum
1665 * field is copied here.
1666 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001667 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001668 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1669 prevp->qf_type = from_qfp->qf_type; /* error type */
1670 if (from_qfl->qf_ptr == from_qfp)
1671 to_qfl->qf_ptr = prevp; /* current location */
1672 }
1673 }
1674
1675 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1676
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001677 /* Assign a new ID for the location list */
1678 to_qfl->qf_id = ++last_qf_id;
1679
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001680 /* 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 Moolenaara7df8c72017-07-19 13:23:06 +02001697qf_get_fnum(qf_info_T *qi, int qf_idx, 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 Moolenaara7df8c72017-07-19 13:23:06 +02001726 directory = qf_guess_filepath(qi, qf_idx, 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 Moolenaara7df8c72017-07-19 13:23:06 +02001895qf_guess_filepath(qf_info_T *qi, int qf_idx, 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;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001900 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001903 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 return NULL;
1905
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001906 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 fullname = NULL;
1908 while (ds_ptr)
1909 {
1910 vim_free(fullname);
1911 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1912
1913 /* If concat_fnames failed, just go on. The worst thing that can happen
1914 * is that we delete the entire stack.
1915 */
1916 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1917 break;
1918
1919 ds_ptr = ds_ptr->next;
1920 }
1921
1922 vim_free(fullname);
1923
1924 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001925 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001927 ds_tmp = qfl->qf_dir_stack->next;
1928 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 vim_free(ds_tmp->dirname);
1930 vim_free(ds_tmp);
1931 }
1932
1933 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934}
1935
1936/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001937 * When loading a file from the quickfix, the auto commands may modify it.
1938 * This may invalidate the current quickfix entry. This function checks
1939 * whether a entry is still present in the quickfix.
1940 * Similar to location list.
1941 */
1942 static int
1943is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1944{
1945 qf_list_T *qfl;
1946 qfline_T *qfp;
1947 int i;
1948
1949 qfl = &qi->qf_lists[qi->qf_curlist];
1950
1951 /* Search for the entry in the current list */
1952 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1953 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001954 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001955 break;
1956
1957 if (i == qfl->qf_count) /* Entry is not found */
1958 return FALSE;
1959
1960 return TRUE;
1961}
1962
1963/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001964 * Get the next valid entry in the current quickfix/location list. The search
1965 * starts from the current entry. If next_file is TRUE, then return the next
1966 * valid entry in the next file in the list. Returns NULL on failure.
1967 */
1968 static qfline_T *
1969get_next_valid_entry(
1970 qf_info_T *qi,
1971 qfline_T *qf_ptr,
1972 int *qf_index,
1973 int dir)
1974{
1975 int idx;
1976 int old_qf_fnum;
1977
1978 idx = *qf_index;
1979 old_qf_fnum = qf_ptr->qf_fnum;
1980
1981 do
1982 {
1983 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
1984 || qf_ptr->qf_next == NULL)
1985 return NULL;
1986 ++idx;
1987 qf_ptr = qf_ptr->qf_next;
1988 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1989 && !qf_ptr->qf_valid)
1990 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1991
1992 *qf_index = idx;
1993 return qf_ptr;
1994}
1995
1996/*
1997 * Get the previous valid entry in the current quickfix/location list. The
1998 * search starts from the current entry. If prev_file is TRUE, then return the
1999 * previous valid entry in the previous file in the list. Returns NULL on
2000 * failure.
2001 */
2002 static qfline_T *
2003get_prev_valid_entry(
2004 qf_info_T *qi,
2005 qfline_T *qf_ptr,
2006 int *qf_index,
2007 int dir)
2008{
2009 int idx;
2010 int old_qf_fnum;
2011
2012 idx = *qf_index;
2013 old_qf_fnum = qf_ptr->qf_fnum;
2014
2015 do
2016 {
2017 if (idx == 1 || qf_ptr->qf_prev == NULL)
2018 return NULL;
2019 --idx;
2020 qf_ptr = qf_ptr->qf_prev;
2021 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2022 && !qf_ptr->qf_valid)
2023 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2024
2025 *qf_index = idx;
2026 return qf_ptr;
2027}
2028
2029/*
2030 * Get the n'th (errornr) previous/next valid entry from the current entry in
2031 * the quickfix list.
2032 * dir == FORWARD or FORWARD_FILE: next valid entry
2033 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2034 */
2035 static qfline_T *
2036get_nth_valid_entry(
2037 qf_info_T *qi,
2038 int errornr,
2039 qfline_T *qf_ptr,
2040 int *qf_index,
2041 int dir)
2042{
2043 qfline_T *prev_qf_ptr;
2044 int prev_index;
2045 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2046 char_u *err = e_no_more_items;
2047
2048 while (errornr--)
2049 {
2050 prev_qf_ptr = qf_ptr;
2051 prev_index = *qf_index;
2052
2053 if (dir == FORWARD || dir == FORWARD_FILE)
2054 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2055 else
2056 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2057 if (qf_ptr == NULL)
2058 {
2059 qf_ptr = prev_qf_ptr;
2060 *qf_index = prev_index;
2061 if (err != NULL)
2062 {
2063 EMSG(_(err));
2064 return NULL;
2065 }
2066 break;
2067 }
2068
2069 err = NULL;
2070 }
2071
2072 return qf_ptr;
2073}
2074
2075/*
2076 * Get n'th quickfix entry
2077 */
2078 static qfline_T *
2079get_nth_entry(
2080 qf_info_T *qi,
2081 int errornr,
2082 qfline_T *qf_ptr,
2083 int *qf_index)
2084{
2085 int qf_idx = *qf_index;
2086
2087 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2088 {
2089 --qf_idx;
2090 qf_ptr = qf_ptr->qf_prev;
2091 }
2092 while (errornr > qf_idx &&
2093 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2094 qf_ptr->qf_next != NULL)
2095 {
2096 ++qf_idx;
2097 qf_ptr = qf_ptr->qf_next;
2098 }
2099
2100 *qf_index = qf_idx;
2101 return qf_ptr;
2102}
2103
2104#ifdef FEAT_WINDOWS
2105/*
2106 * Find a help window or open one.
2107 */
2108 static int
2109jump_to_help_window(qf_info_T *qi, int *opened_window)
2110{
2111 win_T *wp;
2112 int flags;
2113
2114 if (cmdmod.tab != 0)
2115 wp = NULL;
2116 else
2117 FOR_ALL_WINDOWS(wp)
2118 if (bt_help(wp->w_buffer))
2119 break;
2120 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2121 win_enter(wp, TRUE);
2122 else
2123 {
2124 /*
2125 * Split off help window; put it at far top if no position
2126 * specified, the current window is vertically split and narrow.
2127 */
2128 flags = WSP_HELP;
2129 if (cmdmod.split == 0 && curwin->w_width != Columns
2130 && curwin->w_width < 80)
2131 flags |= WSP_TOP;
2132 if (qi != &ql_info)
2133 flags |= WSP_NEWLOC; /* don't copy the location list */
2134
2135 if (win_split(0, flags) == FAIL)
2136 return FAIL;
2137
2138 *opened_window = TRUE;
2139
2140 if (curwin->w_height < p_hh)
2141 win_setheight((int)p_hh);
2142
2143 if (qi != &ql_info) /* not a quickfix list */
2144 {
2145 /* The new window should use the supplied location list */
2146 curwin->w_llist = qi;
2147 qi->qf_refcount++;
2148 }
2149 }
2150
2151 if (!p_im)
2152 restart_edit = 0; /* don't want insert mode in help file */
2153
2154 return OK;
2155}
2156#endif
2157
2158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 * jump to a quickfix line
2160 * if dir == FORWARD go "errornr" valid entries forward
2161 * if dir == BACKWARD go "errornr" valid entries backward
2162 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2163 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2164 * else if "errornr" is zero, redisplay the same line
2165 * else go to entry "errornr"
2166 */
2167 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002168qf_jump(qf_info_T *qi,
2169 int dir,
2170 int errornr,
2171 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002173 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002174 qfline_T *qf_ptr;
2175 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 linenr_T i;
2179 buf_T *old_curbuf;
2180 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 colnr_T screen_col;
2182 colnr_T char_col;
2183 char_u *line;
2184#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002185 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002186 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 int opened_window = FALSE;
2188 win_T *win;
2189 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002190 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002192 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 int print_message = TRUE;
2194 int len;
2195#ifdef FEAT_FOLDING
2196 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2197#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002198 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002199 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002201 if (qi == NULL)
2202 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002203
2204 if (qi->qf_curlist >= qi->qf_listcount
2205 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
2207 EMSG(_(e_quickfix));
2208 return;
2209 }
2210
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002211 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002213 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002215 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002217 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2218 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002220 qf_ptr = old_qf_ptr;
2221 qf_index = old_qf_index;
2222 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
2224 }
2225 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002226 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002229 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2230 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 /* No need to print the error message if it's visible in the error
2232 * window */
2233 print_message = FALSE;
2234
2235 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002236 * For ":helpgrep" find a help window or open one.
2237 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002238 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002239 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002240 if (jump_to_help_window(qi, &opened_window) == FAIL)
2241 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002242 }
2243
2244 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 * If currently in the quickfix window, find another window to show the
2246 * file in.
2247 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002248 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002250 win_T *usable_win_ptr = NULL;
2251
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 /*
2253 * If there is no file specified, we don't know where to go.
2254 * But do advance, otherwise ":cn" gets stuck.
2255 */
2256 if (qf_ptr->qf_fnum == 0)
2257 goto theend;
2258
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002259 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002260
2261 ll_ref = curwin->w_llist_ref;
2262 if (ll_ref != NULL)
2263 {
2264 /* Find a window using the same location list that is not a
2265 * quickfix window. */
2266 FOR_ALL_WINDOWS(usable_win_ptr)
2267 if (usable_win_ptr->w_llist == ll_ref
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02002268 && !bt_quickfix(usable_win_ptr->w_buffer))
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002269 {
2270 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002271 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002272 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002273 }
2274
2275 if (!usable_win)
2276 {
2277 /* Locate a window showing a normal buffer */
2278 FOR_ALL_WINDOWS(win)
2279 if (win->w_buffer->b_p_bt[0] == NUL)
2280 {
2281 usable_win = 1;
2282 break;
2283 }
2284 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002285
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002287 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002288 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002290 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002291 {
2292 tabpage_T *tp;
2293 win_T *wp;
2294
2295 FOR_ALL_TAB_WINDOWS(tp, wp)
2296 {
2297 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2298 {
2299 goto_tabpage_win(tp, wp);
2300 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002301 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002302 }
2303 }
2304 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002305win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002306
2307 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002308 * If there is only one window and it is the quickfix window, create a
2309 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002310 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002311 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002313 flags = WSP_ABOVE;
2314 if (ll_ref != NULL)
2315 flags |= WSP_NEWLOC;
2316 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 goto failed; /* not enough room for window */
2318 opened_window = TRUE; /* close it when fail */
2319 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002320 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002321 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002322 if (ll_ref != NULL)
2323 {
2324 /* The new window should use the location list from the
2325 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002326 curwin->w_llist = ll_ref;
2327 ll_ref->qf_refcount++;
2328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
2330 else
2331 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002332 if (curwin->w_llist_ref != NULL)
2333 {
2334 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002335 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002336 if (win == NULL)
2337 {
2338 /* Find the window showing the selected file */
2339 FOR_ALL_WINDOWS(win)
2340 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2341 break;
2342 if (win == NULL)
2343 {
2344 /* Find a previous usable window */
2345 win = curwin;
2346 do
2347 {
2348 if (win->w_buffer->b_p_bt[0] == NUL)
2349 break;
2350 if (win->w_prev == NULL)
2351 win = lastwin; /* wrap around the top */
2352 else
2353 win = win->w_prev; /* go to previous window */
2354 } while (win != curwin);
2355 }
2356 }
2357 win_goto(win);
2358
2359 /* If the location list for the window is not set, then set it
2360 * to the location list from the location window */
2361 if (win->w_llist == NULL)
2362 {
2363 win->w_llist = ll_ref;
2364 ll_ref->qf_refcount++;
2365 }
2366 }
2367 else
2368 {
2369
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 /*
2371 * Try to find a window that shows the right buffer.
2372 * Default to the window just above the quickfix buffer.
2373 */
2374 win = curwin;
2375 altwin = NULL;
2376 for (;;)
2377 {
2378 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2379 break;
2380 if (win->w_prev == NULL)
2381 win = lastwin; /* wrap around the top */
2382 else
2383 win = win->w_prev; /* go to previous window */
2384
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002385 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
2387 /* Didn't find it, go to the window before the quickfix
2388 * window. */
2389 if (altwin != NULL)
2390 win = altwin;
2391 else if (curwin->w_prev != NULL)
2392 win = curwin->w_prev;
2393 else
2394 win = curwin->w_next;
2395 break;
2396 }
2397
2398 /* Remember a usable window. */
2399 if (altwin == NULL && !win->w_p_pvw
2400 && win->w_buffer->b_p_bt[0] == NUL)
2401 altwin = win;
2402 }
2403
2404 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407 }
2408#endif
2409
2410 /*
2411 * If there is a file name,
2412 * read the wanted file if needed, and check autowrite etc.
2413 */
2414 old_curbuf = curbuf;
2415 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002416
2417 if (qf_ptr->qf_fnum != 0)
2418 {
2419 if (qf_ptr->qf_type == 1)
2420 {
2421 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2422 * set b_p_ro flag). */
2423 if (!can_abandon(curbuf, forceit))
2424 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002425 no_write_message();
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002426 ok = FALSE;
2427 }
2428 else
2429 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002430 ECMD_HIDE + ECMD_SET_HELP,
2431 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002432 }
2433 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002434 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002435 int old_qf_curlist = qi->qf_curlist;
2436 int is_abort = FALSE;
2437
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002438 ok = buflist_getfile(qf_ptr->qf_fnum,
2439 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002440 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002441 {
2442 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002443 is_abort = TRUE;
2444 opened_window = FALSE;
2445 }
2446 else if (old_qf_curlist != qi->qf_curlist
2447 || !is_qf_entry_present(qi, qf_ptr))
2448 {
2449 if (qi == &ql_info)
2450 EMSG(_("E925: Current quickfix was changed"));
2451 else
2452 EMSG(_("E926: Current location list was changed"));
2453 is_abort = TRUE;
2454 }
2455
2456 if (is_abort)
2457 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002458 ok = FALSE;
2459 qi = NULL;
2460 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002461 }
2462 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002463 }
2464
2465 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
2467 /* When not switched to another buffer, still need to set pc mark */
2468 if (curbuf == old_curbuf)
2469 setpcmark();
2470
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002471 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002473 /*
2474 * Go to line with error, unless qf_lnum is 0.
2475 */
2476 i = qf_ptr->qf_lnum;
2477 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002479 if (i > curbuf->b_ml.ml_line_count)
2480 i = curbuf->b_ml.ml_line_count;
2481 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002483 if (qf_ptr->qf_col > 0)
2484 {
2485 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002486#ifdef FEAT_VIRTUALEDIT
2487 curwin->w_cursor.coladd = 0;
2488#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002489 if (qf_ptr->qf_viscol == TRUE)
2490 {
2491 /*
2492 * Check each character from the beginning of the error
2493 * line up to the error column. For each tab character
2494 * found, reduce the error column value by the length of
2495 * a tab character.
2496 */
2497 line = ml_get_curline();
2498 screen_col = 0;
2499 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2500 {
2501 if (*line == NUL)
2502 break;
2503 if (*line++ == '\t')
2504 {
2505 curwin->w_cursor.col -= 7 - (screen_col % 8);
2506 screen_col += 8 - (screen_col % 8);
2507 }
2508 else
2509 ++screen_col;
2510 }
2511 }
2512 check_cursor();
2513 }
2514 else
2515 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002518 {
2519 pos_T save_cursor;
2520
2521 /* Move the cursor to the first line in the buffer */
2522 save_cursor = curwin->w_cursor;
2523 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002524 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002525 SEARCH_KEEP, NULL, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002526 curwin->w_cursor = save_cursor;
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529#ifdef FEAT_FOLDING
2530 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2531 foldOpenCursor();
2532#endif
2533 if (print_message)
2534 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002535 /* Update the screen before showing the message, unless the screen
2536 * scrolled up. */
2537 if (!msg_scrolled)
2538 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002540 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2542 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2543 /* Add the message, skipping leading whitespace and newlines. */
2544 len = (int)STRLEN(IObuff);
2545 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2546
2547 /* Output the message. Overwrite to avoid scrolling when the 'O'
2548 * flag is present in 'shortmess'; But when not jumping, print the
2549 * whole message. */
2550 i = msg_scroll;
2551 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2552 msg_scroll = TRUE;
2553 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2554 msg_scroll = FALSE;
2555 msg_attr_keep(IObuff, 0, TRUE);
2556 msg_scroll = i;
2557 }
2558 }
2559 else
2560 {
2561#ifdef FEAT_WINDOWS
2562 if (opened_window)
2563 win_close(curwin, TRUE); /* Close opened window */
2564#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002565 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
2567 /*
2568 * Couldn't open file, so put index back where it was. This could
2569 * happen if the file was readonly and we changed something.
2570 */
2571#ifdef FEAT_WINDOWS
2572failed:
2573#endif
2574 qf_ptr = old_qf_ptr;
2575 qf_index = old_qf_index;
2576 }
2577 }
2578theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002579 if (qi != NULL)
2580 {
2581 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2582 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584#ifdef FEAT_WINDOWS
2585 if (p_swb != old_swb && opened_window)
2586 {
2587 /* Restore old 'switchbuf' value, but not when an autocommand or
2588 * modeline has changed the value. */
2589 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002592 swb_flags = old_swb_flags;
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 else
2595 free_string_option(old_swb);
2596 }
2597#endif
2598}
2599
2600/*
2601 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002602 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 */
2604 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002605qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002607 buf_T *buf;
2608 char_u *fname;
2609 qfline_T *qfp;
2610 int i;
2611 int idx1 = 1;
2612 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002613 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002614 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002615 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002617 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002619 if (eap->cmdidx == CMD_llist)
2620 {
2621 qi = GET_LOC_LIST(curwin);
2622 if (qi == NULL)
2623 {
2624 EMSG(_(e_loclist));
2625 return;
2626 }
2627 }
2628
2629 if (qi->qf_curlist >= qi->qf_listcount
2630 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 EMSG(_(e_quickfix));
2633 return;
2634 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002635 if (*arg == '+')
2636 {
2637 ++arg;
2638 plus = TRUE;
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2641 {
2642 EMSG(_(e_trailing));
2643 return;
2644 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002645 if (plus)
2646 {
2647 i = qi->qf_lists[qi->qf_curlist].qf_index;
2648 idx2 = i + idx1;
2649 idx1 = i;
2650 }
2651 else
2652 {
2653 i = qi->qf_lists[qi->qf_curlist].qf_count;
2654 if (idx1 < 0)
2655 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2656 if (idx2 < 0)
2657 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002660 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002662 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2663 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
2665 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2666 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002667 msg_putchar('\n');
2668 if (got_int)
2669 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002670
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002671 fname = NULL;
2672 if (qfp->qf_fnum != 0
2673 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2674 {
2675 fname = buf->b_fname;
2676 if (qfp->qf_type == 1) /* :helpgrep */
2677 fname = gettail(fname);
2678 }
2679 if (fname == NULL)
2680 sprintf((char *)IObuff, "%2d", i);
2681 else
2682 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2683 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002684 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar21020352017-06-13 17:21:04 +02002685 ? HL_ATTR(HLF_QFL) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002686 if (qfp->qf_lnum == 0)
2687 IObuff[0] = NUL;
2688 else if (qfp->qf_col == 0)
2689 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2690 else
2691 sprintf((char *)IObuff, ":%ld col %d",
2692 qfp->qf_lnum, qfp->qf_col);
2693 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2694 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002695 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002696 if (qfp->qf_pattern != NULL)
2697 {
2698 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2699 STRCAT(IObuff, ":");
2700 msg_puts(IObuff);
2701 }
2702 msg_puts((char_u *)" ");
2703
2704 /* Remove newlines and leading whitespace from the text. For an
2705 * unrecognized line keep the indent, the compiler may mark a word
2706 * with ^^^^. */
2707 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2709 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002710 msg_prt_line(IObuff, FALSE);
2711 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002713
2714 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002715 if (qfp == NULL)
2716 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002717 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 ui_breakcheck();
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720}
2721
2722/*
2723 * Remove newlines and leading whitespace from an error message.
2724 * Put the result in "buf[bufsize]".
2725 */
2726 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002727qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 int i;
2730 char_u *p = text;
2731
2732 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2733 {
2734 if (*p == '\n')
2735 {
2736 buf[i] = ' ';
2737 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002738 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 break;
2740 }
2741 else
2742 buf[i] = *p++;
2743 }
2744 buf[i] = NUL;
2745}
2746
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002747 static void
2748qf_msg(qf_info_T *qi, int which, char *lead)
2749{
2750 char *title = (char *)qi->qf_lists[which].qf_title;
2751 int count = qi->qf_lists[which].qf_count;
2752 char_u buf[IOSIZE];
2753
2754 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2755 lead,
2756 which + 1,
2757 qi->qf_listcount,
2758 count);
2759
2760 if (title != NULL)
2761 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002762 size_t len = STRLEN(buf);
2763
2764 if (len < 34)
2765 {
2766 vim_memset(buf + len, ' ', 34 - len);
2767 buf[34] = NUL;
2768 }
2769 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002770 }
2771 trunc_string(buf, buf, Columns - 1, IOSIZE);
2772 msg(buf);
2773}
2774
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775/*
2776 * ":colder [count]": Up in the quickfix stack.
2777 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002778 * ":lolder [count]": Up in the location list stack.
2779 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 */
2781 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002782qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002784 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 int count;
2786
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002787 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2788 {
2789 qi = GET_LOC_LIST(curwin);
2790 if (qi == NULL)
2791 {
2792 EMSG(_(e_loclist));
2793 return;
2794 }
2795 }
2796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (eap->addr_count != 0)
2798 count = eap->line2;
2799 else
2800 count = 1;
2801 while (count--)
2802 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002803 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002805 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
2807 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002810 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812 else
2813 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002814 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
2816 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002819 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 }
2821 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002822 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002824 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825#endif
2826}
2827
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002828 void
2829qf_history(exarg_T *eap)
2830{
2831 qf_info_T *qi = &ql_info;
2832 int i;
2833
2834 if (eap->cmdidx == CMD_lhistory)
2835 qi = GET_LOC_LIST(curwin);
2836 if (qi == NULL || (qi->qf_listcount == 0
2837 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2838 MSG(_("No entries"));
2839 else
2840 for (i = 0; i < qi->qf_listcount; ++i)
2841 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2842}
2843
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002845 * Free all the entries in the error list "idx". Note that other information
2846 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 */
2848 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002849qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002851 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002852 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002853 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002854 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002856 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002858 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002859 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002860 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002861 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002862 vim_free(qfp->qf_text);
2863 stop = (qfp == qfpnext);
2864 vim_free(qfp->qf_pattern);
2865 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002866 if (stop)
2867 /* Somehow qf_count may have an incorrect value, set it to 1
2868 * to avoid crashing when it's wrong.
2869 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002870 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002871 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002872 qfl->qf_start = qfpnext;
2873 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002875
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002876 qfl->qf_index = 0;
2877 qfl->qf_start = NULL;
2878 qfl->qf_last = NULL;
2879 qfl->qf_ptr = NULL;
2880 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002881
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002882 qf_clean_dir_stack(&qfl->qf_dir_stack);
2883 qfl->qf_directory = NULL;
2884 qf_clean_dir_stack(&qfl->qf_file_stack);
2885 qfl->qf_currfile = NULL;
2886 qfl->qf_multiline = FALSE;
2887 qfl->qf_multiignore = FALSE;
2888 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889}
2890
2891/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002892 * Free error list "idx". Frees all the entries in the quickfix list,
2893 * associated context information and the title.
2894 */
2895 static void
2896qf_free(qf_info_T *qi, int idx)
2897{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002898 qf_list_T *qfl = &qi->qf_lists[idx];
2899
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002900 qf_free_items(qi, idx);
2901
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002902 vim_free(qfl->qf_title);
2903 qfl->qf_title = NULL;
2904 free_tv(qfl->qf_ctx);
2905 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002906 qfl->qf_id = 0;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002907}
2908
2909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 * qf_mark_adjust: adjust marks
2911 */
2912 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002913qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002914 win_T *wp,
2915 linenr_T line1,
2916 linenr_T line2,
2917 long amount,
2918 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002920 int i;
2921 qfline_T *qfp;
2922 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002923 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002924 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002925 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaarc1542742016-07-20 21:44:37 +02002927 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002928 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002929 if (wp != NULL)
2930 {
2931 if (wp->w_llist == NULL)
2932 return;
2933 qi = wp->w_llist;
2934 }
2935
2936 for (idx = 0; idx < qi->qf_listcount; ++idx)
2937 if (qi->qf_lists[idx].qf_count)
2938 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002939 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2940 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 if (qfp->qf_fnum == curbuf->b_fnum)
2942 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002943 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2945 {
2946 if (amount == MAXLNUM)
2947 qfp->qf_cleared = TRUE;
2948 else
2949 qfp->qf_lnum += amount;
2950 }
2951 else if (amount_after && qfp->qf_lnum > line2)
2952 qfp->qf_lnum += amount_after;
2953 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002954
2955 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002956 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957}
2958
2959/*
2960 * Make a nice message out of the error character and the error number:
2961 * char number message
2962 * e or E 0 " error"
2963 * w or W 0 " warning"
2964 * i or I 0 " info"
2965 * 0 0 ""
2966 * other 0 " c"
2967 * e or E n " error n"
2968 * w or W n " warning n"
2969 * i or I n " info n"
2970 * 0 n " error n"
2971 * other n " c n"
2972 * 1 x "" :helpgrep
2973 */
2974 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002975qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 static char_u buf[20];
2978 static char_u cc[3];
2979 char_u *p;
2980
2981 if (c == 'W' || c == 'w')
2982 p = (char_u *)" warning";
2983 else if (c == 'I' || c == 'i')
2984 p = (char_u *)" info";
2985 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2986 p = (char_u *)" error";
2987 else if (c == 0 || c == 1)
2988 p = (char_u *)"";
2989 else
2990 {
2991 cc[0] = ' ';
2992 cc[1] = c;
2993 cc[2] = NUL;
2994 p = cc;
2995 }
2996
2997 if (nr <= 0)
2998 return p;
2999
3000 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3001 return buf;
3002}
3003
3004#if defined(FEAT_WINDOWS) || defined(PROTO)
3005/*
3006 * ":cwindow": open the quickfix window if we have errors to display,
3007 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003008 * ":lwindow": open the location list window if we have locations to display,
3009 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 */
3011 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003012ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003014 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 win_T *win;
3016
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003017 if (eap->cmdidx == CMD_lwindow)
3018 {
3019 qi = GET_LOC_LIST(curwin);
3020 if (qi == NULL)
3021 return;
3022 }
3023
3024 /* Look for an existing quickfix window. */
3025 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
3027 /*
3028 * If a quickfix window is open but we have no errors to display,
3029 * close the window. If a quickfix window is not open, then open
3030 * it if we have errors; otherwise, leave it closed.
3031 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003032 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003033 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003034 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
3036 if (win != NULL)
3037 ex_cclose(eap);
3038 }
3039 else if (win == NULL)
3040 ex_copen(eap);
3041}
3042
3043/*
3044 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003045 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003048ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003050 win_T *win = NULL;
3051 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003053 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3054 {
3055 qi = GET_LOC_LIST(curwin);
3056 if (qi == NULL)
3057 return;
3058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003060 /* Find existing quickfix window and close it. */
3061 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 if (win != NULL)
3063 win_close(win, FALSE);
3064}
3065
3066/*
3067 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003068 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 */
3070 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003071ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003073 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003076 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003077 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003078 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003080 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3081 {
3082 qi = GET_LOC_LIST(curwin);
3083 if (qi == NULL)
3084 {
3085 EMSG(_(e_loclist));
3086 return;
3087 }
3088 }
3089
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 if (eap->addr_count != 0)
3091 height = eap->line2;
3092 else
3093 height = QF_WINHEIGHT;
3094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096#ifdef FEAT_GUI
3097 need_mouse_correct = TRUE;
3098#endif
3099
3100 /*
3101 * Find existing quickfix window, or open a new one.
3102 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003103 win = qf_find_win(qi);
3104
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003105 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003106 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003108 if (eap->addr_count != 0)
3109 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003110 if (cmdmod.split & WSP_VERT)
3111 {
3112 if (height != W_WIDTH(win))
3113 win_setwidth(height);
3114 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003115 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003116 win_setheight(height);
3117 }
3118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 else
3120 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00003121 qf_buf = qf_find_buf(qi);
3122
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 /* The current window becomes the previous window afterwards. */
3124 win = curwin;
3125
Bram Moolenaar77642c02012-11-20 17:55:10 +01003126 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3127 && cmdmod.split == 0)
3128 /* Create the new window at the very bottom, except when
3129 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003130 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003131 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003133 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003135 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003137 /*
3138 * For the location list window, create a reference to the
3139 * location list from the window 'win'.
3140 */
3141 curwin->w_llist_ref = win->w_llist;
3142 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003144
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003145 if (oldwin != curwin)
3146 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003147 if (qf_buf != NULL)
3148 /* Use the existing quickfix buffer */
3149 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003150 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003151 else
3152 {
3153 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003154 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003155 /* switch off 'swapfile' */
3156 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3157 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003158 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003159 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003160 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003161#ifdef FEAT_DIFF
3162 curwin->w_p_diff = FALSE;
3163#endif
3164#ifdef FEAT_FOLDING
3165 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3166 OPT_LOCAL);
3167#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003170 /* Only set the height when still in the same tab page and there is no
3171 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003172 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 win_setheight(height);
3174 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3175 if (win_valid(win))
3176 prevwin = win;
3177 }
3178
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003179 qf_set_title_var(qi);
3180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 /*
3182 * Fill the buffer with the quickfix list.
3183 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003184 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003186 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 curwin->w_cursor.col = 0;
3188 check_cursor();
3189 update_topline(); /* scroll to show the line */
3190}
3191
3192/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003193 * Move the cursor in the quickfix window to "lnum".
3194 */
3195 static void
3196qf_win_goto(win_T *win, linenr_T lnum)
3197{
3198 win_T *old_curwin = curwin;
3199
3200 curwin = win;
3201 curbuf = win->w_buffer;
3202 curwin->w_cursor.lnum = lnum;
3203 curwin->w_cursor.col = 0;
3204#ifdef FEAT_VIRTUALEDIT
3205 curwin->w_cursor.coladd = 0;
3206#endif
3207 curwin->w_curswant = 0;
3208 update_topline(); /* scroll to show the line */
3209 redraw_later(VALID);
3210 curwin->w_redr_status = TRUE; /* update ruler */
3211 curwin = old_curwin;
3212 curbuf = curwin->w_buffer;
3213}
3214
3215/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003216 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003217 */
3218 void
3219ex_cbottom(exarg_T *eap UNUSED)
3220{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003221 qf_info_T *qi = &ql_info;
3222 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003223
Bram Moolenaar537ef082016-07-09 17:56:19 +02003224 if (eap->cmdidx == CMD_lbottom)
3225 {
3226 qi = GET_LOC_LIST(curwin);
3227 if (qi == NULL)
3228 {
3229 EMSG(_(e_loclist));
3230 return;
3231 }
3232 }
3233
3234 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003235 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3236 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3237}
3238
3239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * Return the number of the current entry (line number in the quickfix
3241 * window).
3242 */
3243 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003244qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003246 qf_info_T *qi = &ql_info;
3247
3248 if (IS_LL_WINDOW(wp))
3249 /* In the location list window, use the referenced location list */
3250 qi = wp->w_llist_ref;
3251
3252 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253}
3254
3255/*
3256 * Update the cursor position in the quickfix window to the current error.
3257 * Return TRUE if there is a quickfix window.
3258 */
3259 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003260qf_win_pos_update(
3261 qf_info_T *qi,
3262 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003265 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266
3267 /*
3268 * Put the cursor on the current error in the quickfix window, so that
3269 * it's viewable.
3270 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003271 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 if (win != NULL
3273 && qf_index <= win->w_buffer->b_ml.ml_line_count
3274 && old_qf_index != qf_index)
3275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (qf_index > old_qf_index)
3277 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003278 win->w_redraw_top = old_qf_index;
3279 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281 else
3282 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003283 win->w_redraw_top = qf_index;
3284 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003286 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288 return win != NULL;
3289}
3290
3291/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003292 * Check whether the given window is displaying the specified quickfix/location
3293 * list buffer
3294 */
3295 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003296is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003297{
3298 /*
3299 * A window displaying the quickfix buffer will have the w_llist_ref field
3300 * set to NULL.
3301 * A window displaying a location list buffer will have the w_llist_ref
3302 * pointing to the location list.
3303 */
3304 if (bt_quickfix(win->w_buffer))
3305 if ((qi == &ql_info && win->w_llist_ref == NULL)
3306 || (qi != &ql_info && win->w_llist_ref == qi))
3307 return TRUE;
3308
3309 return FALSE;
3310}
3311
3312/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003313 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003314 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003315 */
3316 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003317qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003318{
3319 win_T *win;
3320
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003321 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003322 if (is_qf_win(win, qi))
3323 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003324
3325 return win;
3326}
3327
3328/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003329 * Find a quickfix buffer.
3330 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 */
3332 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003333qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003335 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003336 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaar9c102382006-05-03 21:26:49 +00003338 FOR_ALL_TAB_WINDOWS(tp, win)
3339 if (is_qf_win(win, qi))
3340 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003341
Bram Moolenaar9c102382006-05-03 21:26:49 +00003342 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343}
3344
3345/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003346 * Update the w:quickfix_title variable in the quickfix/location list window
3347 */
3348 static void
3349qf_update_win_titlevar(qf_info_T *qi)
3350{
3351 win_T *win;
3352 win_T *curwin_save;
3353
3354 if ((win = qf_find_win(qi)) != NULL)
3355 {
3356 curwin_save = curwin;
3357 curwin = win;
3358 qf_set_title_var(qi);
3359 curwin = curwin_save;
3360 }
3361}
3362
3363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 * Find the quickfix buffer. If it exists, update the contents.
3365 */
3366 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003367qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003370 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372
3373 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003374 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (buf != NULL)
3376 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003377 linenr_T old_line_count = buf->b_ml.ml_line_count;
3378
3379 if (old_last == NULL)
3380 /* set curwin/curbuf to buf and save a few things */
3381 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaard823fa92016-08-12 16:29:27 +02003383 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003384
Bram Moolenaar864293a2016-06-02 13:40:04 +02003385 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003386 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003387
Bram Moolenaar864293a2016-06-02 13:40:04 +02003388 if (old_last == NULL)
3389 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003390 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003391
3392 /* restore curwin/curbuf and a few other things */
3393 aucmd_restbuf(&aco);
3394 }
3395
3396 /* Only redraw when added lines are visible. This avoids flickering
3397 * when the added lines are not visible. */
3398 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3399 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
3401}
3402
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003403/*
3404 * Set "w:quickfix_title" if "qi" has a title.
3405 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003407qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003408{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003409 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3410 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003411 qi->qf_lists[qi->qf_curlist].qf_title);
3412}
3413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414/*
3415 * Fill current buffer with quickfix errors, replacing any previous contents.
3416 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003417 * If "old_last" is not NULL append the items after this one.
3418 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3419 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 */
3421 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003422qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003424 linenr_T lnum;
3425 qfline_T *qfp;
3426 buf_T *errbuf;
3427 int len;
3428 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
Bram Moolenaar864293a2016-06-02 13:40:04 +02003430 if (old_last == NULL)
3431 {
3432 if (buf != curbuf)
3433 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003434 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003435 return;
3436 }
3437
3438 /* delete all existing lines */
3439 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3440 (void)ml_delete((linenr_T)1, FALSE);
3441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003444 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003447 if (old_last == NULL)
3448 {
3449 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3450 lnum = 0;
3451 }
3452 else
3453 {
3454 qfp = old_last->qf_next;
3455 lnum = buf->b_ml.ml_line_count;
3456 }
3457 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
3459 if (qfp->qf_fnum != 0
3460 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3461 && errbuf->b_fname != NULL)
3462 {
3463 if (qfp->qf_type == 1) /* :helpgrep */
3464 STRCPY(IObuff, gettail(errbuf->b_fname));
3465 else
3466 STRCPY(IObuff, errbuf->b_fname);
3467 len = (int)STRLEN(IObuff);
3468 }
3469 else
3470 len = 0;
3471 IObuff[len++] = '|';
3472
3473 if (qfp->qf_lnum > 0)
3474 {
3475 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3476 len += (int)STRLEN(IObuff + len);
3477
3478 if (qfp->qf_col > 0)
3479 {
3480 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3481 len += (int)STRLEN(IObuff + len);
3482 }
3483
3484 sprintf((char *)IObuff + len, "%s",
3485 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3486 len += (int)STRLEN(IObuff + len);
3487 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003488 else if (qfp->qf_pattern != NULL)
3489 {
3490 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3491 len += (int)STRLEN(IObuff + len);
3492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 IObuff[len++] = '|';
3494 IObuff[len++] = ' ';
3495
3496 /* Remove newlines and leading whitespace from the text.
3497 * For an unrecognized line keep the indent, the compiler may
3498 * mark a word with ^^^^. */
3499 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3500 IObuff + len, IOSIZE - len);
3501
Bram Moolenaar864293a2016-06-02 13:40:04 +02003502 if (ml_append_buf(buf, lnum, IObuff,
3503 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003505 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003507 if (qfp == NULL)
3508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003510
3511 if (old_last == NULL)
3512 /* Delete the empty line which is now at the end */
3513 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515
3516 /* correct cursor position */
3517 check_lnums(TRUE);
3518
Bram Moolenaar864293a2016-06-02 13:40:04 +02003519 if (old_last == NULL)
3520 {
3521 /* Set the 'filetype' to "qf" each time after filling the buffer.
3522 * This resembles reading a file into a buffer, it's more logical when
3523 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003524#ifdef FEAT_AUTOCMD
3525 ++curbuf_lock;
3526#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003527 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3528 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003531 keep_filetype = TRUE; /* don't detect 'filetype' */
3532 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003534 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003536 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003537 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003539 /* make sure it will be redrawn */
3540 redraw_curbuf_later(NOT_VALID);
3541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
3543 /* Restore KeyTyped, setting 'filetype' may reset it. */
3544 KeyTyped = old_KeyTyped;
3545}
3546
3547#endif /* FEAT_WINDOWS */
3548
3549/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003550 * Return TRUE when using ":vimgrep" for ":grep".
3551 */
3552 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003553grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003554{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003555 return ((cmdidx == CMD_grep
3556 || cmdidx == CMD_lgrep
3557 || cmdidx == CMD_grepadd
3558 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003559 && STRCMP("internal",
3560 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3561}
3562
3563/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003564 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 */
3566 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003567ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003569 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003571 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003573 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003574 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003575 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003576#ifdef FEAT_AUTOCMD
3577 char_u *au_name = NULL;
3578
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003579 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3580 if (grep_internal(eap->cmdidx))
3581 {
3582 ex_vimgrep(eap);
3583 return;
3584 }
3585
Bram Moolenaar7c626922005-02-07 22:01:03 +00003586 switch (eap->cmdidx)
3587 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003588 case CMD_make: au_name = (char_u *)"make"; break;
3589 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3590 case CMD_grep: au_name = (char_u *)"grep"; break;
3591 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3592 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3593 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003594 default: break;
3595 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003596 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3597 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003598 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003599# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003600 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003601 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003602# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003603 }
3604#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003605#ifdef FEAT_MBYTE
3606 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaara6557602006-02-04 22:43:20 +00003609 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3610 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003611 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003614 fname = get_mef_name();
3615 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003617 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
3619 /*
3620 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3621 */
3622 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3623 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003624 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 cmd = alloc(len);
3626 if (cmd == NULL)
3627 return;
3628 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3629 (char *)p_shq);
3630 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003631 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 /*
3633 * Output a newline if there's something else than the :make command that
3634 * was typed (in which case the cursor is in column 0).
3635 */
3636 if (msg_col == 0)
3637 msg_didout = FALSE;
3638 msg_start();
3639 MSG_PUTS(":!");
3640 msg_outtrans(cmd); /* show what we are doing */
3641
3642 /* let the shell know if we are redirecting output or not */
3643 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3644
3645#ifdef AMIGA
3646 out_flush();
3647 /* read window status report and redraw before message */
3648 (void)char_avail();
3649#endif
3650
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003651 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003652 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3653 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003654 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003655 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003656 if (wp != NULL)
3657 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003658#ifdef FEAT_AUTOCMD
3659 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003660 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003661 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3662 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003663 if (qi->qf_curlist < qi->qf_listcount)
3664 res = qi->qf_lists[qi->qf_curlist].qf_count;
3665 else
3666 res = 0;
3667 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003668#endif
3669 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003670 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
Bram Moolenaar7c626922005-02-07 22:01:03 +00003672 mch_remove(fname);
3673 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 vim_free(cmd);
3675}
3676
3677/*
3678 * Return the name for the errorfile, in allocated memory.
3679 * Find a new unique name when 'makeef' contains "##".
3680 * Returns NULL for error.
3681 */
3682 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003683get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684{
3685 char_u *p;
3686 char_u *name;
3687 static int start = -1;
3688 static int off = 0;
3689#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003690 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691#endif
3692
3693 if (*p_mef == NUL)
3694 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003695 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (name == NULL)
3697 EMSG(_(e_notmp));
3698 return name;
3699 }
3700
3701 for (p = p_mef; *p; ++p)
3702 if (p[0] == '#' && p[1] == '#')
3703 break;
3704
3705 if (*p == NUL)
3706 return vim_strsave(p_mef);
3707
3708 /* Keep trying until the name doesn't exist yet. */
3709 for (;;)
3710 {
3711 if (start == -1)
3712 start = mch_get_pid();
3713 else
3714 off += 19;
3715
3716 name = alloc((unsigned)STRLEN(p_mef) + 30);
3717 if (name == NULL)
3718 break;
3719 STRCPY(name, p_mef);
3720 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3721 STRCAT(name, p + 2);
3722 if (mch_getperm(name) < 0
3723#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003724 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 && mch_lstat((char *)name, &sb) < 0
3726#endif
3727 )
3728 break;
3729 vim_free(name);
3730 }
3731 return name;
3732}
3733
3734/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003735 * Returns the number of valid entries in the current quickfix/location list.
3736 */
3737 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003738qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003739{
3740 qf_info_T *qi = &ql_info;
3741 qfline_T *qfp;
3742 int i, sz = 0;
3743 int prev_fnum = 0;
3744
3745 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3746 {
3747 /* Location list */
3748 qi = GET_LOC_LIST(curwin);
3749 if (qi == NULL)
3750 return 0;
3751 }
3752
3753 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003754 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003755 ++i, qfp = qfp->qf_next)
3756 {
3757 if (qfp->qf_valid)
3758 {
3759 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3760 sz++; /* Count all valid entries */
3761 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3762 {
3763 /* Count the number of files */
3764 sz++;
3765 prev_fnum = qfp->qf_fnum;
3766 }
3767 }
3768 }
3769
3770 return sz;
3771}
3772
3773/*
3774 * Returns the current index of the quickfix/location list.
3775 * Returns 0 if there is an error.
3776 */
3777 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003778qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003779{
3780 qf_info_T *qi = &ql_info;
3781
3782 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3783 {
3784 /* Location list */
3785 qi = GET_LOC_LIST(curwin);
3786 if (qi == NULL)
3787 return 0;
3788 }
3789
3790 return qi->qf_lists[qi->qf_curlist].qf_index;
3791}
3792
3793/*
3794 * Returns the current index in the quickfix/location list (counting only valid
3795 * entries). If no valid entries are in the list, then returns 1.
3796 */
3797 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003798qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003799{
3800 qf_info_T *qi = &ql_info;
3801 qf_list_T *qfl;
3802 qfline_T *qfp;
3803 int i, eidx = 0;
3804 int prev_fnum = 0;
3805
3806 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3807 {
3808 /* Location list */
3809 qi = GET_LOC_LIST(curwin);
3810 if (qi == NULL)
3811 return 1;
3812 }
3813
3814 qfl = &qi->qf_lists[qi->qf_curlist];
3815 qfp = qfl->qf_start;
3816
3817 /* check if the list has valid errors */
3818 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3819 return 1;
3820
3821 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3822 {
3823 if (qfp->qf_valid)
3824 {
3825 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3826 {
3827 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3828 {
3829 /* Count the number of files */
3830 eidx++;
3831 prev_fnum = qfp->qf_fnum;
3832 }
3833 }
3834 else
3835 eidx++;
3836 }
3837 }
3838
3839 return eidx ? eidx : 1;
3840}
3841
3842/*
3843 * Get the 'n'th valid error entry in the quickfix or location list.
3844 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3845 * For :cdo and :ldo returns the 'n'th valid error entry.
3846 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3847 */
3848 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003849qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003850{
3851 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3852 qfline_T *qfp = qfl->qf_start;
3853 int i, eidx;
3854 int prev_fnum = 0;
3855
3856 /* check if the list has valid errors */
3857 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3858 return 1;
3859
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003860 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003861 i++, qfp = qfp->qf_next)
3862 {
3863 if (qfp->qf_valid)
3864 {
3865 if (fdo)
3866 {
3867 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3868 {
3869 /* Count the number of files */
3870 eidx++;
3871 prev_fnum = qfp->qf_fnum;
3872 }
3873 }
3874 else
3875 eidx++;
3876 }
3877
3878 if (eidx == n)
3879 break;
3880 }
3881
3882 if (i <= qfl->qf_count)
3883 return i;
3884 else
3885 return 1;
3886}
3887
3888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003890 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003891 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 */
3893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003894ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003896 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003897 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003898
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003899 if (eap->cmdidx == CMD_ll
3900 || eap->cmdidx == CMD_lrewind
3901 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003902 || eap->cmdidx == CMD_llast
3903 || eap->cmdidx == CMD_ldo
3904 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003905 {
3906 qi = GET_LOC_LIST(curwin);
3907 if (qi == NULL)
3908 {
3909 EMSG(_(e_loclist));
3910 return;
3911 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003912 }
3913
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003914 if (eap->addr_count > 0)
3915 errornr = (int)eap->line2;
3916 else
3917 {
3918 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3919 errornr = 0;
3920 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3921 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3922 errornr = 1;
3923 else
3924 errornr = 32767;
3925 }
3926
3927 /* For cdo and ldo commands, jump to the nth valid error.
3928 * For cfdo and lfdo commands, jump to the nth valid file entry.
3929 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02003930 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
3931 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003932 errornr = qf_get_nth_valid_entry(qi,
3933 eap->addr_count > 0 ? (int)eap->line1 : 1,
3934 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3935
3936 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937}
3938
3939/*
3940 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003941 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003942 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 */
3944 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003945ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003947 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003948 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003949
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003950 if (eap->cmdidx == CMD_lnext
3951 || eap->cmdidx == CMD_lNext
3952 || eap->cmdidx == CMD_lprevious
3953 || eap->cmdidx == CMD_lnfile
3954 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003955 || eap->cmdidx == CMD_lpfile
3956 || eap->cmdidx == CMD_ldo
3957 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003958 {
3959 qi = GET_LOC_LIST(curwin);
3960 if (qi == NULL)
3961 {
3962 EMSG(_(e_loclist));
3963 return;
3964 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003965 }
3966
Bram Moolenaar55b69262017-08-13 13:42:01 +02003967 if (eap->addr_count > 0
3968 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
3969 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003970 errornr = (int)eap->line2;
3971 else
3972 errornr = 1;
3973
3974 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3975 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003977 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3978 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003980 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3981 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 ? BACKWARD_FILE
3983 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003984 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985}
3986
3987/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003988 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003989 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 */
3991 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003992ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003994 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003995 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003996 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003997#ifdef FEAT_AUTOCMD
3998 char_u *au_name = NULL;
3999#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004000
4001 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004002 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004003 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004004
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004005#ifdef FEAT_AUTOCMD
4006 switch (eap->cmdidx)
4007 {
4008 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4009 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4010 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4011 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4012 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4013 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4014 default: break;
4015 }
4016 if (au_name != NULL)
4017 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
4018#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004019#ifdef FEAT_MBYTE
4020 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4021#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004022#ifdef FEAT_BROWSE
4023 if (cmdmod.browse)
4024 {
4025 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4026 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4027 if (browse_file == NULL)
4028 return;
4029 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4030 vim_free(browse_file);
4031 }
4032 else
4033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004035 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004036
4037 /*
4038 * This function is used by the :cfile, :cgetfile and :caddfile
4039 * commands.
4040 * :cfile always creates a new quickfix list and jumps to the
4041 * first error.
4042 * :cgetfile creates a new quickfix list but doesn't jump to the
4043 * first error.
4044 * :caddfile adds to an existing quickfix list. If there is no
4045 * quickfix list then a new list is created.
4046 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004047 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004048 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004049 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004050 && (eap->cmdidx == CMD_cfile
4051 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004052 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004053#ifdef FEAT_AUTOCMD
4054 if (au_name != NULL)
4055 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4056#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004057 if (wp != NULL)
4058 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004059 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004060 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004061
4062 else
4063 {
4064#ifdef FEAT_AUTOCMD
4065 if (au_name != NULL)
4066 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4067#endif
4068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069}
4070
4071/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004072 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004073 * ":vimgrepadd {pattern} file(s)"
4074 * ":lvimgrep {pattern} file(s)"
4075 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004076 */
4077 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004078ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004079{
Bram Moolenaar81695252004-12-29 20:58:21 +00004080 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004081 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004082 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004083 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004084 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004085 char_u *s;
4086 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004087 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004088 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004089#ifdef FEAT_AUTOCMD
4090 qfline_T *cur_qf_start;
4091#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004092 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004093 buf_T *buf;
4094 int duplicate_name = FALSE;
4095 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004096 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004097 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004098 buf_T *first_match_buf = NULL;
4099 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004100 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004101#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4102 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004103#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004104 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004105 int flags = 0;
4106 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004107 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004108 char_u *dirname_start = NULL;
4109 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004110 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004111#ifdef FEAT_AUTOCMD
4112 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004113
4114 switch (eap->cmdidx)
4115 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004116 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4117 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4118 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004119 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004120 case CMD_grep: au_name = (char_u *)"grep"; break;
4121 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4122 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4123 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004124 default: break;
4125 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004126 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4127 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004128 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004129# ifdef FEAT_EVAL
4130 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004131 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004132# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004133 }
4134#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004135
Bram Moolenaar754b5602006-02-09 23:53:20 +00004136 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004137 || eap->cmdidx == CMD_lvimgrep
4138 || eap->cmdidx == CMD_lgrepadd
4139 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004140 {
4141 qi = ll_get_or_alloc_list(curwin);
4142 if (qi == NULL)
4143 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004144 }
4145
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004146 if (eap->addr_count > 0)
4147 tomatch = eap->line2;
4148 else
4149 tomatch = MAXLNUM;
4150
Bram Moolenaar81695252004-12-29 20:58:21 +00004151 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004152 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004153 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004154 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004155 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004156 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004157 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004158 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004159 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004160
4161 if (s != NULL && *s == NUL)
4162 {
4163 /* Pattern is empty, use last search pattern. */
4164 if (last_search_pat() == NULL)
4165 {
4166 EMSG(_(e_noprevre));
4167 goto theend;
4168 }
4169 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4170 }
4171 else
4172 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4173
Bram Moolenaar86b68352004-12-27 21:59:20 +00004174 if (regmatch.regprog == NULL)
4175 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004176 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004177 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004178
4179 p = skipwhite(p);
4180 if (*p == NUL)
4181 {
4182 EMSG(_("E683: File name missing or invalid pattern"));
4183 goto theend;
4184 }
4185
Bram Moolenaar55b69262017-08-13 13:42:01 +02004186 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4187 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004188 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004189 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004190 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004191
4192 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004193 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004194 goto theend;
4195 if (fcount == 0)
4196 {
4197 EMSG(_(e_nomatch));
4198 goto theend;
4199 }
4200
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004201 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4202 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004203 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004204 {
4205 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004206 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004207 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004208
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004209 /* Remember the current directory, because a BufRead autocommand that does
4210 * ":lcd %:p:h" changes the meaning of short path names. */
4211 mch_dirname(dirname_start, MAXPATHL);
4212
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004213#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004214 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004215 * changing the current quickfix list. */
4216 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4217#endif
4218
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004219 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004220 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004221 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004222 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004223 if (time(NULL) > seconds)
4224 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004225 /* Display the file name every second or so, show the user we are
4226 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004227 seconds = time(NULL);
4228 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004229 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004230 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004231 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004232 else
4233 {
4234 msg_outtrans(p);
4235 vim_free(p);
4236 }
4237 msg_clr_eos();
4238 msg_didout = FALSE; /* overwrite this message */
4239 msg_nowait = TRUE; /* don't wait for this message */
4240 msg_col = 0;
4241 out_flush();
4242 }
4243
Bram Moolenaar81695252004-12-29 20:58:21 +00004244 buf = buflist_findname_exp(fnames[fi]);
4245 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4246 {
4247 /* Remember that a buffer with this name already exists. */
4248 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004249 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004250 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004251
4252#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4253 /* Don't do Filetype autocommands to avoid loading syntax and
4254 * indent scripts, a great speed improvement. */
4255 save_ei = au_event_disable(",Filetype");
4256#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004257 /* Don't use modelines here, it's useless. */
4258 save_mls = p_mls;
4259 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004260
4261 /* Load file into a buffer, so that 'fileencoding' is detected,
4262 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004263 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004264
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004265 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004266#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4267 au_event_restore(save_ei);
4268#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004269 }
4270 else
4271 /* Use existing, loaded buffer. */
4272 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004273
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004274#ifdef FEAT_AUTOCMD
4275 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4276 {
4277 int idx;
4278
4279 /* Autocommands changed the quickfix list. Find the one we were
4280 * using and restore it. */
4281 for (idx = 0; idx < LISTCOUNT; ++idx)
4282 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4283 {
4284 qi->qf_curlist = idx;
4285 break;
4286 }
4287 if (idx == LISTCOUNT)
4288 {
4289 /* List cannot be found, create a new one. */
4290 qf_new_list(qi, *eap->cmdlinep);
4291 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4292 }
4293 }
4294#endif
4295
Bram Moolenaar81695252004-12-29 20:58:21 +00004296 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004297 {
4298 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004299 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004300 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004301 else
4302 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004303 /* Try for a match in all lines of the buffer.
4304 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004305 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004306 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4307 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004308 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004309 col = 0;
4310 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004311 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004312 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004313 /* Pass the buffer number so that it gets used even for a
4314 * dummy buffer, unless duplicate_name is set, then the
4315 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004316 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004317 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004318 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004319 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004320 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004321 ml_get_buf(buf,
4322 regmatch.startpos[0].lnum + lnum, FALSE),
4323 regmatch.startpos[0].lnum + lnum,
4324 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004325 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004326 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004327 0, /* nr */
4328 0, /* type */
4329 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004330 ) == FAIL)
4331 {
4332 got_int = TRUE;
4333 break;
4334 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004335 found_match = TRUE;
4336 if (--tomatch == 0)
4337 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004338 if ((flags & VGR_GLOBAL) == 0
4339 || regmatch.endpos[0].lnum > 0)
4340 break;
4341 col = regmatch.endpos[0].col
4342 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004343 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004344 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004345 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004346 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004347 if (got_int)
4348 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004349 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004350#ifdef FEAT_AUTOCMD
4351 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4352#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004353
4354 if (using_dummy)
4355 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004356 if (found_match && first_match_buf == NULL)
4357 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004358 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004359 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004360 /* Never keep a dummy buffer if there is another buffer
4361 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004362 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004363 buf = NULL;
4364 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004365 else if (!cmdmod.hide
4366 || buf->b_p_bh[0] == 'u' /* "unload" */
4367 || buf->b_p_bh[0] == 'w' /* "wipe" */
4368 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004369 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004370 /* When no match was found we don't need to remember the
4371 * buffer, wipe it out. If there was a match and it
4372 * wasn't the first one or we won't jump there: only
4373 * unload the buffer.
4374 * Ignore 'hidden' here, because it may lead to having too
4375 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004376 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004377 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004378 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004379 buf = NULL;
4380 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004381 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004382 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004383 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004384 /* Keeping the buffer, remove the dummy flag. */
4385 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004386 buf = NULL;
4387 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004388 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004389
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004390 if (buf != NULL)
4391 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004392 /* Keeping the buffer, remove the dummy flag. */
4393 buf->b_flags &= ~BF_DUMMY;
4394
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004395 /* If the buffer is still loaded we need to use the
4396 * directory we jumped to below. */
4397 if (buf == first_match_buf
4398 && target_dir == NULL
4399 && STRCMP(dirname_start, dirname_now) != 0)
4400 target_dir = vim_strsave(dirname_now);
4401
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004402 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004403 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004404 * need to be done (again). But not the window-local
4405 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004406 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004407#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004408 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4409 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004410#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004411 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004412 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004413 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004414 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004415 }
4416 }
4417
4418 FreeWild(fcount, fnames);
4419
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004420 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4421 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4422 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004423
4424#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004425 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004426#endif
4427
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004428#ifdef FEAT_AUTOCMD
4429 if (au_name != NULL)
4430 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4431 curbuf->b_fname, TRUE, curbuf);
4432#endif
4433
Bram Moolenaar86b68352004-12-27 21:59:20 +00004434 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004435 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004436 {
4437 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004438 {
4439 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004440 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004441 if (buf != curbuf)
4442 /* If we jumped to another buffer redrawing will already be
4443 * taken care of. */
4444 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004445
4446 /* Jump to the directory used after loading the buffer. */
4447 if (curbuf == first_match_buf && target_dir != NULL)
4448 {
4449 exarg_T ea;
4450
4451 ea.arg = target_dir;
4452 ea.cmdidx = CMD_lcd;
4453 ex_cd(&ea);
4454 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004455 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004456 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004457 else
4458 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004459
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004460 /* If we loaded a dummy buffer into the current window, the autocommands
4461 * may have messed up things, need to redraw and recompute folds. */
4462 if (redraw_for_dummy)
4463 {
4464#ifdef FEAT_FOLDING
4465 foldUpdateAll(curwin);
4466#else
4467 redraw_later(NOT_VALID);
4468#endif
4469 }
4470
Bram Moolenaar86b68352004-12-27 21:59:20 +00004471theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004472 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004473 vim_free(dirname_now);
4474 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004475 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004476 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004477}
4478
4479/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004480 * Restore current working directory to "dirname_start" if they differ, taking
4481 * into account whether it is set locally or globally.
4482 */
4483 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004484restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004485{
4486 char_u *dirname_now = alloc(MAXPATHL);
4487
4488 if (NULL != dirname_now)
4489 {
4490 mch_dirname(dirname_now, MAXPATHL);
4491 if (STRCMP(dirname_start, dirname_now) != 0)
4492 {
4493 /* If the directory has changed, change it back by building up an
4494 * appropriate ex command and executing it. */
4495 exarg_T ea;
4496
4497 ea.arg = dirname_start;
4498 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4499 ex_cd(&ea);
4500 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004501 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004502 }
4503}
4504
4505/*
4506 * Load file "fname" into a dummy buffer and return the buffer pointer,
4507 * placing the directory resulting from the buffer load into the
4508 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4509 * prior to calling this function. Restores directory to "dirname_start" prior
4510 * to returning, if autocmds or the 'autochdir' option have changed it.
4511 *
4512 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4513 * or wipe_dummy_buffer() later!
4514 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004515 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004516 */
4517 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004518load_dummy_buffer(
4519 char_u *fname,
4520 char_u *dirname_start, /* in: old directory */
4521 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004522{
4523 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004524 bufref_T newbufref;
4525 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004526 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004527 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004528
4529 /* Allocate a buffer without putting it in the buffer list. */
4530 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4531 if (newbuf == NULL)
4532 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004533 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004534
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004535 /* Init the options. */
4536 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4537
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004538 /* need to open the memfile before putting the buffer in a window */
4539 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004540 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004541 /* set curwin/curbuf to buf and save a few things */
4542 aucmd_prepbuf(&aco, newbuf);
4543
4544 /* Need to set the filename for autocommands. */
4545 (void)setfname(curbuf, fname, NULL, FALSE);
4546
Bram Moolenaar81695252004-12-29 20:58:21 +00004547 /* Create swap file now to avoid the ATTENTION message. */
4548 check_need_swap(TRUE);
4549
4550 /* Remove the "dummy" flag, otherwise autocommands may not
4551 * work. */
4552 curbuf->b_flags &= ~BF_DUMMY;
4553
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004554 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004555 if (readfile(fname, NULL,
4556 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4557 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004558 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004559 && !(curbuf->b_flags & BF_NEW))
4560 {
4561 failed = FALSE;
4562 if (curbuf != newbuf)
4563 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004564 /* Bloody autocommands changed the buffer! Can happen when
4565 * using netrw and editing a remote file. Use the current
4566 * buffer instead, delete the dummy one after restoring the
4567 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004568 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004569 newbuf = curbuf;
4570 }
4571 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004572
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004573 /* restore curwin/curbuf and a few other things */
4574 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004575 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4576 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004577
4578 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4579 * skip it. */
4580 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004581 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004582
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004583 /*
4584 * When autocommands/'autochdir' option changed directory: go back.
4585 * Let the caller know what the resulting dir was first, in case it is
4586 * important.
4587 */
4588 mch_dirname(resulting_dir, MAXPATHL);
4589 restore_start_dir(dirname_start);
4590
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004591 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004592 return NULL;
4593 if (failed)
4594 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004595 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004596 return NULL;
4597 }
4598 return newbuf;
4599}
4600
4601/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004602 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4603 * directory to "dirname_start" prior to returning, if autocmds or the
4604 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004605 */
4606 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004607wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004608{
4609 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004610 {
4611#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4612 cleanup_T cs;
4613
4614 /* Reset the error/interrupt/exception state here so that aborting()
4615 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4616 * work when got_int is set. */
4617 enter_cleanup(&cs);
4618#endif
4619
Bram Moolenaar81695252004-12-29 20:58:21 +00004620 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004621
4622#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4623 /* Restore the error/interrupt/exception state if not discarded by a
4624 * new aborting error, interrupt, or uncaught exception. */
4625 leave_cleanup(&cs);
4626#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004627 /* When autocommands/'autochdir' option changed directory: go back. */
4628 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004629 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004630}
4631
4632/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004633 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4634 * directory to "dirname_start" prior to returning, if autocmds or the
4635 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004636 */
4637 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004638unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004639{
4640 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004641 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004642 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004643
4644 /* When autocommands/'autochdir' option changed directory: go back. */
4645 restore_start_dir(dirname_start);
4646 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004647}
4648
Bram Moolenaar05159a02005-02-26 23:04:13 +00004649#if defined(FEAT_EVAL) || defined(PROTO)
4650/*
4651 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004652 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004653 */
4654 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004655get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004656{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004657 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004658 dict_T *dict;
4659 char_u buf[2];
4660 qfline_T *qfp;
4661 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004662 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004663
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004664 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004665 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004666 qi = &ql_info;
4667 if (wp != NULL)
4668 {
4669 qi = GET_LOC_LIST(wp);
4670 if (qi == NULL)
4671 return FAIL;
4672 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004673 }
4674
Bram Moolenaard823fa92016-08-12 16:29:27 +02004675 if (qf_idx == -1)
4676 qf_idx = qi->qf_curlist;
4677
4678 if (qf_idx >= qi->qf_listcount
4679 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004680 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004681
Bram Moolenaard823fa92016-08-12 16:29:27 +02004682 qfp = qi->qf_lists[qf_idx].qf_start;
4683 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004684 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004685 /* Handle entries with a non-existing buffer number. */
4686 bufnum = qfp->qf_fnum;
4687 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4688 bufnum = 0;
4689
Bram Moolenaar05159a02005-02-26 23:04:13 +00004690 if ((dict = dict_alloc()) == NULL)
4691 return FAIL;
4692 if (list_append_dict(list, dict) == FAIL)
4693 return FAIL;
4694
4695 buf[0] = qfp->qf_type;
4696 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004697 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004698 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4699 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4700 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4701 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004702 || dict_add_nr_str(dict, "pattern", 0L,
4703 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4704 || dict_add_nr_str(dict, "text", 0L,
4705 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004706 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4707 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4708 return FAIL;
4709
4710 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004711 if (qfp == NULL)
4712 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004713 }
4714 return OK;
4715}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004716
4717/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004718 * Flags used by getqflist()/getloclist() to determine which fields to return.
4719 */
4720enum {
4721 QF_GETLIST_NONE = 0x0,
4722 QF_GETLIST_TITLE = 0x1,
4723 QF_GETLIST_ITEMS = 0x2,
4724 QF_GETLIST_NR = 0x4,
4725 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004726 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004727 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004728 QF_GETLIST_IDX = 0x40,
4729 QF_GETLIST_SIZE = 0x80,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004730 QF_GETLIST_ALL = 0xFF
4731};
4732
4733/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004734 * Parse text from 'di' and return the quickfix list items
4735 */
4736 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004737qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004738{
4739 int status = FAIL;
4740 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004741 char_u *errorformat = p_efm;
4742 dictitem_T *efm_di;
4743 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004744
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004745 /* Only a List value is supported */
4746 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004747 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004748 /* If errorformat is supplied then use it, otherwise use the 'efm'
4749 * option setting
4750 */
4751 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4752 {
4753 if (efm_di->di_tv.v_type != VAR_STRING ||
4754 efm_di->di_tv.vval.v_string == NULL)
4755 return FAIL;
4756 errorformat = efm_di->di_tv.vval.v_string;
4757 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004758
Bram Moolenaar36538222017-09-02 19:51:44 +02004759 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004760 if (l == NULL)
4761 return FAIL;
4762
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004763 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4764 if (qi != NULL)
4765 {
4766 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4767 qi->qf_refcount++;
4768
Bram Moolenaar36538222017-09-02 19:51:44 +02004769 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004770 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4771 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004772 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004773 qf_free(qi, 0);
4774 }
4775 free(qi);
4776 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004777 dict_add_list(retdict, "items", l);
4778 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004779 }
4780
4781 return status;
4782}
4783
4784/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004785 * Return the quickfix/location list number with the given identifier.
4786 * Returns -1 if list is not found.
4787 */
4788 static int
4789qf_id2nr(qf_info_T *qi, int_u qfid)
4790{
4791 int qf_idx;
4792
4793 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4794 if (qi->qf_lists[qf_idx].qf_id == qfid)
4795 return qf_idx;
4796 return -1;
4797}
4798
4799/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004800 * Return quickfix/location list details (title) as a
4801 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4802 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004803 */
4804 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004805qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004806{
4807 qf_info_T *qi = &ql_info;
4808 int status = OK;
4809 int qf_idx;
4810 dictitem_T *di;
4811 int flags = QF_GETLIST_NONE;
4812
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004813 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004814 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004815
Bram Moolenaard823fa92016-08-12 16:29:27 +02004816 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004817 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004818
4819 /* List is not present or is empty */
4820 if (qi == NULL || qi->qf_listcount == 0)
4821 {
4822 /* If querying for the size of the list, return 0 */
4823 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4824 && (di->di_tv.v_type == VAR_STRING)
4825 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4826 return dict_add_nr_str(retdict, "nr", 0, NULL);
4827 return FAIL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004828 }
4829
4830 qf_idx = qi->qf_curlist; /* default is the current list */
4831 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4832 {
4833 /* Use the specified quickfix/location list */
4834 if (di->di_tv.v_type == VAR_NUMBER)
4835 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004836 /* for zero use the current list */
4837 if (di->di_tv.vval.v_number != 0)
4838 {
4839 qf_idx = di->di_tv.vval.v_number - 1;
4840 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4841 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004842 }
Bram Moolenaar55b69262017-08-13 13:42:01 +02004843 }
4844 else if ((di->di_tv.v_type == VAR_STRING)
4845 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004846 /* Get the last quickfix list number */
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004847 qf_idx = qi->qf_listcount - 1;
4848 else
4849 return FAIL;
4850 flags |= QF_GETLIST_NR;
4851 }
4852
4853 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
4854 {
4855 /* Look for a list with the specified id */
4856 if (di->di_tv.v_type == VAR_NUMBER)
4857 {
4858 /* For zero, use the current list or the list specifed by 'nr' */
4859 if (di->di_tv.vval.v_number != 0)
4860 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004861 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
4862 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004863 return FAIL; /* List not found */
4864 }
4865 flags |= QF_GETLIST_ID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004866 }
4867 else
4868 return FAIL;
4869 }
4870
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004871 if (dict_find(what, (char_u *)"all", -1) != NULL)
4872 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004873
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004874 if (dict_find(what, (char_u *)"title", -1) != NULL)
4875 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004876
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004877 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4878 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004879
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004880 if (dict_find(what, (char_u *)"context", -1) != NULL)
4881 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004882
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004883 if (dict_find(what, (char_u *)"items", -1) != NULL)
4884 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004885
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004886 if (dict_find(what, (char_u *)"idx", -1) != NULL)
4887 flags |= QF_GETLIST_IDX;
4888
4889 if (dict_find(what, (char_u *)"size", -1) != NULL)
4890 flags |= QF_GETLIST_SIZE;
4891
Bram Moolenaard823fa92016-08-12 16:29:27 +02004892 if (flags & QF_GETLIST_TITLE)
4893 {
4894 char_u *t;
4895 t = qi->qf_lists[qf_idx].qf_title;
4896 if (t == NULL)
4897 t = (char_u *)"";
4898 status = dict_add_nr_str(retdict, "title", 0L, t);
4899 }
4900 if ((status == OK) && (flags & QF_GETLIST_NR))
4901 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4902 if ((status == OK) && (flags & QF_GETLIST_WINID))
4903 {
4904 win_T *win;
4905 win = qf_find_win(qi);
4906 if (win != NULL)
4907 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4908 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004909 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4910 {
4911 list_T *l = list_alloc();
4912 if (l != NULL)
4913 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004914 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004915 dict_add_list(retdict, "items", l);
4916 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004917 else
4918 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004919 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004920
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004921 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4922 {
4923 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4924 {
4925 di = dictitem_alloc((char_u *)"context");
4926 if (di != NULL)
4927 {
4928 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004929 status = dict_add(retdict, di);
4930 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004931 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004932 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004933 else
4934 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004935 }
4936 else
4937 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4938 }
4939
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004940 if ((status == OK) && (flags & QF_GETLIST_ID))
4941 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
4942 NULL);
4943
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004944 if ((status == OK) && (flags & QF_GETLIST_IDX))
4945 {
4946 int idx = qi->qf_lists[qf_idx].qf_index;
4947 if (qi->qf_lists[qf_idx].qf_count == 0)
4948 /* For empty lists, qf_index is set to 1 */
4949 idx = 0;
4950 status = dict_add_nr_str(retdict, "idx", idx, NULL);
4951 }
4952
4953 if ((status == OK) && (flags & QF_GETLIST_SIZE))
4954 status = dict_add_nr_str(retdict, "size",
4955 qi->qf_lists[qf_idx].qf_count, NULL);
4956
Bram Moolenaard823fa92016-08-12 16:29:27 +02004957 return status;
4958}
4959
4960/*
4961 * Add list of entries to quickfix/location list. Each list entry is
4962 * a dictionary with item information.
4963 */
4964 static int
4965qf_add_entries(
4966 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004967 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004968 list_T *list,
4969 char_u *title,
4970 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004971{
4972 listitem_T *li;
4973 dict_T *d;
4974 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004975 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004976 long lnum;
4977 int col, nr;
4978 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004979#ifdef FEAT_WINDOWS
4980 qfline_T *old_last = NULL;
4981#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004982 int valid, status;
4983 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004984 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004985
Bram Moolenaara3921f42017-06-04 15:30:34 +02004986 if (action == ' ' || qf_idx == qi->qf_listcount)
4987 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004988 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004989 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004990 qf_idx = qi->qf_curlist;
4991 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004992#ifdef FEAT_WINDOWS
Bram Moolenaara3921f42017-06-04 15:30:34 +02004993 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004994 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004995 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004996#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004997 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004998 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004999 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005000 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005001 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005002
5003 for (li = list->lv_first; li != NULL; li = li->li_next)
5004 {
5005 if (li->li_tv.v_type != VAR_DICT)
5006 continue; /* Skip non-dict items */
5007
5008 d = li->li_tv.vval.v_dict;
5009 if (d == NULL)
5010 continue;
5011
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005012 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005013 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5014 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5015 col = (int)get_dict_number(d, (char_u *)"col");
5016 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5017 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005018 type = get_dict_string(d, (char_u *)"type", TRUE);
5019 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5020 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005021 if (text == NULL)
5022 text = vim_strsave((char_u *)"");
5023
5024 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005025 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005026 valid = FALSE;
5027
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005028 /* Mark entries with non-existing buffer number as not valid. Give the
5029 * error message only once. */
5030 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5031 {
5032 if (!did_bufnr_emsg)
5033 {
5034 did_bufnr_emsg = TRUE;
5035 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5036 }
5037 valid = FALSE;
5038 bufnum = 0;
5039 }
5040
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005041 /* If the 'valid' field is present it overrules the detected value. */
5042 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5043 valid = (int)get_dict_number(d, (char_u *)"valid");
5044
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005045 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005046 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005047 NULL, /* dir */
5048 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005049 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005050 text,
5051 lnum,
5052 col,
5053 vcol, /* vis_col */
5054 pattern, /* search pattern */
5055 nr,
5056 type == NULL ? NUL : *type,
5057 valid);
5058
5059 vim_free(filename);
5060 vim_free(pattern);
5061 vim_free(text);
5062 vim_free(type);
5063
5064 if (status == FAIL)
5065 {
5066 retval = FAIL;
5067 break;
5068 }
5069 }
5070
Bram Moolenaara3921f42017-06-04 15:30:34 +02005071 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005072 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005073 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005074 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005075 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005076 if (action != 'a')
5077 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005078 qi->qf_lists[qf_idx].qf_ptr =
5079 qi->qf_lists[qf_idx].qf_start;
5080 if (qi->qf_lists[qf_idx].qf_count > 0)
5081 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005082 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005083
5084#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005085 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005086 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005087#endif
5088
5089 return retval;
5090}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005091
5092 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005093qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005094{
5095 dictitem_T *di;
5096 int retval = FAIL;
5097 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005098 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005099 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005100
5101 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5102 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005103
5104 qf_idx = qi->qf_curlist; /* default is the current list */
5105 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5106 {
5107 /* Use the specified quickfix/location list */
5108 if (di->di_tv.v_type == VAR_NUMBER)
5109 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005110 /* for zero use the current list */
5111 if (di->di_tv.vval.v_number != 0)
5112 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005113
Bram Moolenaar55b69262017-08-13 13:42:01 +02005114 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5115 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005116 /*
5117 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005118 * non-available list and add the new list at the end of the
5119 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005120 */
5121 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005122 qf_idx = qi->qf_listcount - 1;
5123 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005124 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005125 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005126 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005127 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005128 }
5129 else if (di->di_tv.v_type == VAR_STRING
5130 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005131 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005132 if (qi->qf_listcount > 0)
5133 qf_idx = qi->qf_listcount - 1;
5134 else if (newlist)
5135 qf_idx = 0;
5136 else
5137 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005138 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005139 else
5140 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005141 }
5142
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005143 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5144 {
5145 /* Use the quickfix/location list with the specified id */
5146 if (di->di_tv.v_type == VAR_NUMBER)
5147 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005148 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5149 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005150 return FAIL; /* List not found */
5151 }
5152 else
5153 return FAIL;
5154 }
5155
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005156 if (newlist)
5157 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005158 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005159 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005160 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005161 }
5162
5163 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5164 {
5165 if (di->di_tv.v_type == VAR_STRING)
5166 {
5167 vim_free(qi->qf_lists[qf_idx].qf_title);
5168 qi->qf_lists[qf_idx].qf_title =
5169 get_dict_string(what, (char_u *)"title", TRUE);
5170 if (qf_idx == qi->qf_curlist)
5171 qf_update_win_titlevar(qi);
5172 retval = OK;
5173 }
5174 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005175
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005176 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5177 {
5178 if (di->di_tv.v_type == VAR_LIST)
5179 {
5180 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5181
5182 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5183 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005184 if (action == 'r')
5185 {
5186 /*
5187 * When replacing the quickfix list entries using
5188 * qf_add_entries(), the title is set with a ':' prefix.
5189 * Restore the title with the saved title.
5190 */
5191 vim_free(qi->qf_lists[qf_idx].qf_title);
5192 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5193 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005194 vim_free(title_save);
5195 }
5196 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005197
Bram Moolenaar36538222017-09-02 19:51:44 +02005198 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5199 {
5200 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5201 return FAIL;
5202 errorformat = di->di_tv.vval.v_string;
5203 }
5204
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005205 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005206 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005207 /* Only a List value is supported */
5208 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005209 {
5210 if (action == 'r')
5211 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005212 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005213 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5214 retval = OK;
5215 }
5216 else
5217 return FAIL;
5218 }
5219
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005220 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5221 {
5222 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005223
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005224 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005225 ctx = alloc_tv();
5226 if (ctx != NULL)
5227 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005228 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005229 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005230 }
5231
Bram Moolenaard823fa92016-08-12 16:29:27 +02005232 return retval;
5233}
5234
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005235/*
5236 * Find the non-location list window with the specified location list.
5237 */
5238 static win_T *
5239find_win_with_ll(qf_info_T *qi)
5240{
5241 win_T *wp = NULL;
5242
5243 FOR_ALL_WINDOWS(wp)
5244 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5245 return wp;
5246
5247 return NULL;
5248}
5249
5250/*
5251 * Free the entire quickfix/location list stack.
5252 * If the quickfix/location list window is open, then clear it.
5253 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005254 static void
5255qf_free_stack(win_T *wp, qf_info_T *qi)
5256{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005257 win_T *qfwin = qf_find_win(qi);
5258 win_T *llwin = NULL;
5259 win_T *orig_wp = wp;
5260
5261 if (qfwin != NULL)
5262 {
5263 /* If the quickfix/location list window is open, then clear it */
5264 if (qi->qf_curlist < qi->qf_listcount)
5265 qf_free(qi, qi->qf_curlist);
5266 qf_update_buffer(qi, NULL);
5267 }
5268
5269 if (wp != NULL && IS_LL_WINDOW(wp))
5270 {
5271 /* If in the location list window, then use the non-location list
5272 * window with this location list (if present)
5273 */
5274 llwin = find_win_with_ll(qi);
5275 if (llwin != NULL)
5276 wp = llwin;
5277 }
5278
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005279 qf_free_all(wp);
5280 if (wp == NULL)
5281 {
5282 /* quickfix list */
5283 qi->qf_curlist = 0;
5284 qi->qf_listcount = 0;
5285 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005286 else if (IS_LL_WINDOW(orig_wp))
5287 {
5288 /* If the location list window is open, then create a new empty
5289 * location list */
5290 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005291
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005292 /* first free the list reference in the location list window */
5293 ll_free_all(&orig_wp->w_llist_ref);
5294
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005295 orig_wp->w_llist_ref = new_ll;
5296 if (llwin != NULL)
5297 {
5298 llwin->w_llist = new_ll;
5299 new_ll->qf_refcount++;
5300 }
5301 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005302}
5303
Bram Moolenaard823fa92016-08-12 16:29:27 +02005304/*
5305 * Populate the quickfix list with the items supplied in the list
5306 * of dictionaries. "title" will be copied to w:quickfix_title.
5307 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5308 */
5309 int
5310set_errorlist(
5311 win_T *wp,
5312 list_T *list,
5313 int action,
5314 char_u *title,
5315 dict_T *what)
5316{
5317 qf_info_T *qi = &ql_info;
5318 int retval = OK;
5319
5320 if (wp != NULL)
5321 {
5322 qi = ll_get_or_alloc_list(wp);
5323 if (qi == NULL)
5324 return FAIL;
5325 }
5326
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005327 if (action == 'f')
5328 {
5329 /* Free the entire quickfix or location list stack */
5330 qf_free_stack(wp, qi);
5331 }
5332 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005333 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005334 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005335 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005336
5337 return retval;
5338}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005339
5340 static int
5341mark_quickfix_ctx(qf_info_T *qi, int copyID)
5342{
5343 int i;
5344 int abort = FALSE;
5345 typval_T *ctx;
5346
5347 for (i = 0; i < LISTCOUNT && !abort; ++i)
5348 {
5349 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005350 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5351 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005352 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5353 }
5354
5355 return abort;
5356}
5357
5358/*
5359 * Mark the context of the quickfix list and the location lists (if present) as
5360 * "in use". So that garabage collection doesn't free the context.
5361 */
5362 int
5363set_ref_in_quickfix(int copyID)
5364{
5365 int abort = FALSE;
5366 tabpage_T *tp;
5367 win_T *win;
5368
5369 abort = mark_quickfix_ctx(&ql_info, copyID);
5370 if (abort)
5371 return abort;
5372
5373 FOR_ALL_TAB_WINDOWS(tp, win)
5374 {
5375 if (win->w_llist != NULL)
5376 {
5377 abort = mark_quickfix_ctx(win->w_llist, copyID);
5378 if (abort)
5379 return abort;
5380 }
5381 }
5382
5383 return abort;
5384}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005385#endif
5386
Bram Moolenaar81695252004-12-29 20:58:21 +00005387/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005388 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005389 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005390 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005391 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005392 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005393 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005394 */
5395 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005396ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005397{
5398 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005399 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005400#ifdef FEAT_AUTOCMD
5401 char_u *au_name = NULL;
5402#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005403
Bram Moolenaardb552d602006-03-23 22:59:57 +00005404 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5405 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005406 {
5407 qi = ll_get_or_alloc_list(curwin);
5408 if (qi == NULL)
5409 return;
5410 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005411
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005412#ifdef FEAT_AUTOCMD
5413 switch (eap->cmdidx)
5414 {
5415 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5416 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5417 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5418 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5419 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5420 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5421 default: break;
5422 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005423 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5424 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005425 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005426# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005427 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005428 return;
5429# endif
5430 }
5431#endif
5432
Bram Moolenaar86b68352004-12-27 21:59:20 +00005433 if (*eap->arg == NUL)
5434 buf = curbuf;
5435 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5436 buf = buflist_findnr(atoi((char *)eap->arg));
5437 if (buf == NULL)
5438 EMSG(_(e_invarg));
5439 else if (buf->b_ml.ml_mfp == NULL)
5440 EMSG(_("E681: Buffer is not loaded"));
5441 else
5442 {
5443 if (eap->addr_count == 0)
5444 {
5445 eap->line1 = 1;
5446 eap->line2 = buf->b_ml.ml_line_count;
5447 }
5448 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5449 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5450 EMSG(_(e_invrange));
5451 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005452 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005453 char_u *qf_title = *eap->cmdlinep;
5454
5455 if (buf->b_sfname)
5456 {
5457 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5458 (char *)qf_title, (char *)buf->b_sfname);
5459 qf_title = IObuff;
5460 }
5461
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005462 if (qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005463 (eap->cmdidx != CMD_caddbuffer
5464 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005465 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005466 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005467 {
5468#ifdef FEAT_AUTOCMD
5469 if (au_name != NULL)
5470 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5471 curbuf->b_fname, TRUE, curbuf);
5472#endif
5473 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5474 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5475 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005476 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005477 }
5478}
5479
Bram Moolenaar1e015462005-09-25 22:16:38 +00005480#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005481/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005482 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5483 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005484 */
5485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005486ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005487{
5488 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005489 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005490#ifdef FEAT_AUTOCMD
5491 char_u *au_name = NULL;
5492#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005493
Bram Moolenaardb552d602006-03-23 22:59:57 +00005494 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5495 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005496 {
5497 qi = ll_get_or_alloc_list(curwin);
5498 if (qi == NULL)
5499 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005500 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005501
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005502#ifdef FEAT_AUTOCMD
5503 switch (eap->cmdidx)
5504 {
5505 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5506 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5507 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5508 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5509 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5510 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5511 default: break;
5512 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005513 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5514 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005515 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005516# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005517 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005518 return;
5519# endif
5520 }
5521#endif
5522
Bram Moolenaar4770d092006-01-12 23:22:24 +00005523 /* Evaluate the expression. When the result is a string or a list we can
5524 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005525 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005526 if (tv != NULL)
5527 {
5528 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5529 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5530 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005531 if (qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005532 (eap->cmdidx != CMD_caddexpr
5533 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005534 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5535 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005536 {
5537#ifdef FEAT_AUTOCMD
5538 if (au_name != NULL)
5539 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5540 curbuf->b_fname, TRUE, curbuf);
5541#endif
5542 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5543 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5544 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005545 }
5546 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005547 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005548 free_tv(tv);
5549 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005550}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005551#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005552
5553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 * ":helpgrep {pattern}"
5555 */
5556 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005557ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 regmatch_T regmatch;
5560 char_u *save_cpo;
5561 char_u *p;
5562 int fcount;
5563 char_u **fnames;
5564 FILE *fd;
5565 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005567#ifdef FEAT_MULTI_LANG
5568 char_u *lang;
5569#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005570 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005571 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005572 int new_qi = FALSE;
5573 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005574#ifdef FEAT_AUTOCMD
5575 char_u *au_name = NULL;
5576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005578#ifdef FEAT_MULTI_LANG
5579 /* Check for a specified language */
5580 lang = check_help_lang(eap->arg);
5581#endif
5582
Bram Moolenaar73633f82012-01-20 13:39:07 +01005583#ifdef FEAT_AUTOCMD
5584 switch (eap->cmdidx)
5585 {
5586 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5587 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5588 default: break;
5589 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005590 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5591 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005592 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005593# ifdef FEAT_EVAL
5594 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005595 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005596# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005597 }
5598#endif
5599
5600 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5601 save_cpo = p_cpo;
5602 p_cpo = empty_option;
5603
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005604 if (eap->cmdidx == CMD_lhelpgrep)
5605 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005606 /* If the current window is a help window, then use it */
5607 if (bt_help(curwin->w_buffer))
5608 wp = curwin;
5609 else
5610 /* Find an existing help window */
5611 FOR_ALL_WINDOWS(wp)
5612 if (bt_help(wp->w_buffer))
5613 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005614
5615 if (wp == NULL) /* Help window not found */
5616 qi = NULL;
5617 else
5618 qi = wp->w_llist;
5619
5620 if (qi == NULL)
5621 {
5622 /* Allocate a new location list for help text matches */
5623 if ((qi = ll_new_list()) == NULL)
5624 return;
5625 new_qi = TRUE;
5626 }
5627 }
5628
Bram Moolenaaree85df32017-03-19 14:19:50 +01005629 /* Autocommands may change the list. Save it for later comparison */
5630 save_qi = qi;
5631
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5633 regmatch.rm_ic = FALSE;
5634 if (regmatch.regprog != NULL)
5635 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005636#ifdef FEAT_MBYTE
5637 vimconv_T vc;
5638
5639 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5640 * differs. */
5641 vc.vc_type = CONV_NONE;
5642 if (!enc_utf8)
5643 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5644#endif
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005647 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 /* Go through all directories in 'runtimepath' */
5650 p = p_rtp;
5651 while (*p != NUL && !got_int)
5652 {
5653 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5654
5655 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5656 add_pathsep(NameBuff);
5657 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5658 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5659 &fnames, EW_FILE|EW_SILENT) == OK
5660 && fcount > 0)
5661 {
5662 for (fi = 0; fi < fcount && !got_int; ++fi)
5663 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005664#ifdef FEAT_MULTI_LANG
5665 /* Skip files for a different language. */
5666 if (lang != NULL
5667 && STRNICMP(lang, fnames[fi]
5668 + STRLEN(fnames[fi]) - 3, 2) != 0
5669 && !(STRNICMP(lang, "en", 2) == 0
5670 && STRNICMP("txt", fnames[fi]
5671 + STRLEN(fnames[fi]) - 3, 3) == 0))
5672 continue;
5673#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005674 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 if (fd != NULL)
5676 {
5677 lnum = 1;
5678 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5679 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005680 char_u *line = IObuff;
5681#ifdef FEAT_MBYTE
5682 /* Convert a line if 'encoding' is not utf-8 and
5683 * the line contains a non-ASCII character. */
5684 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005685 && has_non_ascii(IObuff))
5686 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005687 line = string_convert(&vc, IObuff, NULL);
5688 if (line == NULL)
5689 line = IObuff;
5690 }
5691#endif
5692
5693 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005695 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
5697 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005698 while (l > 0 && line[l - 1] <= ' ')
5699 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005701 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005702 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 NULL, /* dir */
5704 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005705 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005706 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005708 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005709 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005710 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005711 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 0, /* nr */
5713 1, /* type */
5714 TRUE /* valid */
5715 ) == FAIL)
5716 {
5717 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005718#ifdef FEAT_MBYTE
5719 if (line != IObuff)
5720 vim_free(line);
5721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 break;
5723 }
5724 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005725#ifdef FEAT_MBYTE
5726 if (line != IObuff)
5727 vim_free(line);
5728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 ++lnum;
5730 line_breakcheck();
5731 }
5732 fclose(fd);
5733 }
5734 }
5735 FreeWild(fcount, fnames);
5736 }
5737 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005738
Bram Moolenaar473de612013-06-08 18:19:48 +02005739 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005740#ifdef FEAT_MBYTE
5741 if (vc.vc_type != CONV_NONE)
5742 convert_setup(&vc, NULL, NULL);
5743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005745 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5746 qi->qf_lists[qi->qf_curlist].qf_ptr =
5747 qi->qf_lists[qi->qf_curlist].qf_start;
5748 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 }
5750
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005751 if (p_cpo == empty_option)
5752 p_cpo = save_cpo;
5753 else
5754 /* Darn, some plugin changed the value. */
5755 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756
5757#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005758 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759#endif
5760
Bram Moolenaar73633f82012-01-20 13:39:07 +01005761#ifdef FEAT_AUTOCMD
5762 if (au_name != NULL)
5763 {
5764 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5765 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005766 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005767 /* autocommands made "qi" invalid */
5768 return;
5769 }
5770#endif
5771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005773 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005774 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005775 else
5776 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005777
5778 if (eap->cmdidx == CMD_lhelpgrep)
5779 {
5780 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005781 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005782 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005783 {
5784 if (new_qi)
5785 ll_free_all(&qi);
5786 }
5787 else if (curwin->w_llist == NULL)
5788 curwin->w_llist = qi;
5789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790}
5791
5792#endif /* FEAT_QUICKFIX */