blob: 8e76cbfcdcb3c60ce955e88a3b9ed2f5552aae66 [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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
149static int is_qf_win(win_T *win, qf_info_T *qi);
150static win_T *qf_find_win(qf_info_T *qi);
151static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200152static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200154static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static char_u *get_mef_name(void);
156static void restore_start_dir(char_u *dirname_start);
157static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
158static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
159static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
160static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162/* Quickfix window check helper macro */
163#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
164/* Location list window check helper macro */
165#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
166/*
167 * Return location list for window 'wp'
168 * For location list window, return the referenced location list
169 */
170#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200173 * Looking up a buffer can be slow if there are many. Remember the last one
174 * to make this a lot faster if there are multiple matches in the same file.
175 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200176static char_u *qf_last_bufname = NULL;
177static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178
179/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000180 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200181 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 * Return -1 for error, number of errors for success.
183 */
184 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200185qf_init(win_T *wp,
186 char_u *efile,
187 char_u *errorformat,
188 int newlist, /* TRUE: start a new error list */
189 char_u *qf_title,
190 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192 qf_info_T *qi = &ql_info;
193
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000194 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000195 {
196 qi = ll_get_or_alloc_list(wp);
197 if (qi == NULL)
198 return FAIL;
199 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000200
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200201 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
202 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000203}
204
205/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200206 * Maximum number of bytes allowed per line while reading a errorfile.
207 */
208#define LINE_MAXLEN 4096
209
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200210static struct fmtpattern
211{
212 char_u convchar;
213 char *pattern;
214} fmt_pat[FMT_PATTERNS] =
215 {
216 {'f', ".\\+"}, /* only used when at end */
217 {'n', "\\d\\+"},
218 {'l', "\\d\\+"},
219 {'c', "\\d\\+"},
220 {'t', "."},
221 {'m', ".\\+"},
222 {'r', ".*"},
223 {'p', "[- .]*"},
224 {'v', "\\d\\+"},
225 {'s', ".\\+"}
226 };
227
228/*
229 * Converts a 'errorformat' string to regular expression pattern
230 */
231 static int
232efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200233 char_u *efm,
234 int len,
235 efm_T *fmt_ptr,
236 char_u *regpat,
237 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200238{
239 char_u *ptr;
240 char_u *efmp;
241 char_u *srcptr;
242 int round;
243 int idx = 0;
244
245 /*
246 * Build regexp pattern from current 'errorformat' option
247 */
248 ptr = regpat;
249 *ptr++ = '^';
250 round = 0;
251 for (efmp = efm; efmp < efm + len; ++efmp)
252 {
253 if (*efmp == '%')
254 {
255 ++efmp;
256 for (idx = 0; idx < FMT_PATTERNS; ++idx)
257 if (fmt_pat[idx].convchar == *efmp)
258 break;
259 if (idx < FMT_PATTERNS)
260 {
261 if (fmt_ptr->addr[idx])
262 {
263 sprintf((char *)errmsg,
264 _("E372: Too many %%%c in format string"), *efmp);
265 EMSG(errmsg);
266 return -1;
267 }
268 if ((idx
269 && idx < 6
270 && vim_strchr((char_u *)"DXOPQ",
271 fmt_ptr->prefix) != NULL)
272 || (idx == 6
273 && vim_strchr((char_u *)"OPQ",
274 fmt_ptr->prefix) == NULL))
275 {
276 sprintf((char *)errmsg,
277 _("E373: Unexpected %%%c in format string"), *efmp);
278 EMSG(errmsg);
279 return -1;
280 }
281 fmt_ptr->addr[idx] = (char_u)++round;
282 *ptr++ = '\\';
283 *ptr++ = '(';
284#ifdef BACKSLASH_IN_FILENAME
285 if (*efmp == 'f')
286 {
287 /* Also match "c:" in the file name, even when
288 * checking for a colon next: "%f:".
289 * "\%(\a:\)\=" */
290 STRCPY(ptr, "\\%(\\a:\\)\\=");
291 ptr += 10;
292 }
293#endif
294 if (*efmp == 'f' && efmp[1] != NUL)
295 {
296 if (efmp[1] != '\\' && efmp[1] != '%')
297 {
298 /* A file name may contain spaces, but this isn't
299 * in "\f". For "%f:%l:%m" there may be a ":" in
300 * the file name. Use ".\{-1,}x" instead (x is
301 * the next character), the requirement that :999:
302 * follows should work. */
303 STRCPY(ptr, ".\\{-1,}");
304 ptr += 7;
305 }
306 else
307 {
308 /* File name followed by '\\' or '%': include as
309 * many file name chars as possible. */
310 STRCPY(ptr, "\\f\\+");
311 ptr += 4;
312 }
313 }
314 else
315 {
316 srcptr = (char_u *)fmt_pat[idx].pattern;
317 while ((*ptr = *srcptr++) != NUL)
318 ++ptr;
319 }
320 *ptr++ = '\\';
321 *ptr++ = ')';
322 }
323 else if (*efmp == '*')
324 {
325 if (*++efmp == '[' || *efmp == '\\')
326 {
327 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
328 {
329 if (efmp[1] == '^')
330 *ptr++ = *++efmp;
331 if (efmp < efm + len)
332 {
333 *ptr++ = *++efmp; /* could be ']' */
334 while (efmp < efm + len
335 && (*ptr++ = *++efmp) != ']')
336 /* skip */;
337 if (efmp == efm + len)
338 {
339 EMSG(_("E374: Missing ] in format string"));
340 return -1;
341 }
342 }
343 }
344 else if (efmp < efm + len) /* %*\D, %*\s etc. */
345 *ptr++ = *++efmp;
346 *ptr++ = '\\';
347 *ptr++ = '+';
348 }
349 else
350 {
351 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
352 sprintf((char *)errmsg,
353 _("E375: Unsupported %%%c in format string"), *efmp);
354 EMSG(errmsg);
355 return -1;
356 }
357 }
358 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
359 *ptr++ = *efmp; /* regexp magic characters */
360 else if (*efmp == '#')
361 *ptr++ = '*';
362 else if (*efmp == '>')
363 fmt_ptr->conthere = TRUE;
364 else if (efmp == efm + 1) /* analyse prefix */
365 {
366 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
367 fmt_ptr->flags = *efmp++;
368 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
369 fmt_ptr->prefix = *efmp;
370 else
371 {
372 sprintf((char *)errmsg,
373 _("E376: Invalid %%%c in format string prefix"), *efmp);
374 EMSG(errmsg);
375 return -1;
376 }
377 }
378 else
379 {
380 sprintf((char *)errmsg,
381 _("E377: Invalid %%%c in format string"), *efmp);
382 EMSG(errmsg);
383 return -1;
384 }
385 }
386 else /* copy normal character */
387 {
388 if (*efmp == '\\' && efmp + 1 < efm + len)
389 ++efmp;
390 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
391 *ptr++ = '\\'; /* escape regexp atoms */
392 if (*efmp)
393 *ptr++ = *efmp;
394 }
395 }
396 *ptr++ = '$';
397 *ptr = NUL;
398
399 return 0;
400}
401
402 static void
403free_efm_list(efm_T **efm_first)
404{
405 efm_T *efm_ptr;
406
407 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
408 {
409 *efm_first = efm_ptr->next;
410 vim_regfree(efm_ptr->prog);
411 vim_free(efm_ptr);
412 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100413 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200414}
415
416/* Parse 'errorformat' option */
417 static efm_T *
418parse_efm_option(char_u *efm)
419{
420 char_u *errmsg = NULL;
421 int errmsglen;
422 efm_T *fmt_ptr = NULL;
423 efm_T *fmt_first = NULL;
424 efm_T *fmt_last = NULL;
425 char_u *fmtstr = NULL;
426 int len;
427 int i;
428 int round;
429
430 errmsglen = CMDBUFFSIZE + 1;
431 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
432 if (errmsg == NULL)
433 goto parse_efm_end;
434
435 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200436 * Each part of the format string is copied and modified from errorformat
437 * to regex prog. Only a few % characters are allowed.
438 */
439
440 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200441 * Get some space to modify the format string into.
442 */
443 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
444 for (round = FMT_PATTERNS; round > 0; )
445 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200446#ifdef BACKSLASH_IN_FILENAME
447 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200448#else
449 i += 2; /* "%f" can become two chars longer */
450#endif
451 if ((fmtstr = alloc(i)) == NULL)
452 goto parse_efm_error;
453
454 while (efm[0] != NUL)
455 {
456 /*
457 * Allocate a new eformat structure and put it at the end of the list
458 */
459 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
460 if (fmt_ptr == NULL)
461 goto parse_efm_error;
462 if (fmt_first == NULL) /* first one */
463 fmt_first = fmt_ptr;
464 else
465 fmt_last->next = fmt_ptr;
466 fmt_last = fmt_ptr;
467
468 /*
469 * Isolate one part in the 'errorformat' option
470 */
471 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
472 if (efm[len] == '\\' && efm[len + 1] != NUL)
473 ++len;
474
475 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
476 goto parse_efm_error;
477 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
478 goto parse_efm_error;
479 /*
480 * Advance to next part
481 */
482 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
483 }
484
485 if (fmt_first == NULL) /* nothing found */
486 EMSG(_("E378: 'errorformat' contains no pattern"));
487
488 goto parse_efm_end;
489
490parse_efm_error:
491 free_efm_list(&fmt_first);
492
493parse_efm_end:
494 vim_free(fmtstr);
495 vim_free(errmsg);
496
497 return fmt_first;
498}
499
Bram Moolenaare0d37972016-07-15 22:36:01 +0200500enum {
501 QF_FAIL = 0,
502 QF_OK = 1,
503 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200504 QF_NOMEM = 3,
505 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200506};
507
508typedef struct {
509 char_u *linebuf;
510 int linelen;
511 char_u *growbuf;
512 int growbufsiz;
513 FILE *fd;
514 typval_T *tv;
515 char_u *p_str;
516 listitem_T *p_li;
517 buf_T *buf;
518 linenr_T buflnum;
519 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100520 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200521} qfstate_T;
522
523 static char_u *
524qf_grow_linebuf(qfstate_T *state, int newsz)
525{
526 /*
527 * If the line exceeds LINE_MAXLEN exclude the last
528 * byte since it's not a NL character.
529 */
530 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
531 if (state->growbuf == NULL)
532 {
533 state->growbuf = alloc(state->linelen + 1);
534 if (state->growbuf == NULL)
535 return NULL;
536 state->growbufsiz = state->linelen;
537 }
538 else if (state->linelen > state->growbufsiz)
539 {
540 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
541 if (state->growbuf == NULL)
542 return NULL;
543 state->growbufsiz = state->linelen;
544 }
545 return state->growbuf;
546}
547
548/*
549 * Get the next string (separated by newline) from state->p_str.
550 */
551 static int
552qf_get_next_str_line(qfstate_T *state)
553{
554 /* Get the next line from the supplied string */
555 char_u *p_str = state->p_str;
556 char_u *p;
557 int len;
558
559 if (*p_str == NUL) /* Reached the end of the string */
560 return QF_END_OF_INPUT;
561
562 p = vim_strchr(p_str, '\n');
563 if (p != NULL)
564 len = (int)(p - p_str) + 1;
565 else
566 len = (int)STRLEN(p_str);
567
568 if (len > IOSIZE - 2)
569 {
570 state->linebuf = qf_grow_linebuf(state, len);
571 if (state->linebuf == NULL)
572 return QF_NOMEM;
573 }
574 else
575 {
576 state->linebuf = IObuff;
577 state->linelen = len;
578 }
579 vim_strncpy(state->linebuf, p_str, state->linelen);
580
581 /*
582 * Increment using len in order to discard the rest of the
583 * line if it exceeds LINE_MAXLEN.
584 */
585 p_str += len;
586 state->p_str = p_str;
587
588 return QF_OK;
589}
590
591/*
592 * Get the next string from state->p_Li.
593 */
594 static int
595qf_get_next_list_line(qfstate_T *state)
596{
597 listitem_T *p_li = state->p_li;
598 int len;
599
600 while (p_li != NULL
601 && (p_li->li_tv.v_type != VAR_STRING
602 || p_li->li_tv.vval.v_string == NULL))
603 p_li = p_li->li_next; /* Skip non-string items */
604
605 if (p_li == NULL) /* End of the list */
606 {
607 state->p_li = NULL;
608 return QF_END_OF_INPUT;
609 }
610
611 len = (int)STRLEN(p_li->li_tv.vval.v_string);
612 if (len > IOSIZE - 2)
613 {
614 state->linebuf = qf_grow_linebuf(state, len);
615 if (state->linebuf == NULL)
616 return QF_NOMEM;
617 }
618 else
619 {
620 state->linebuf = IObuff;
621 state->linelen = len;
622 }
623
624 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
625
626 state->p_li = p_li->li_next; /* next item */
627 return QF_OK;
628}
629
630/*
631 * Get the next string from state->buf.
632 */
633 static int
634qf_get_next_buf_line(qfstate_T *state)
635{
636 char_u *p_buf = NULL;
637 int len;
638
639 /* Get the next line from the supplied buffer */
640 if (state->buflnum > state->lnumlast)
641 return QF_END_OF_INPUT;
642
643 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
644 state->buflnum += 1;
645
646 len = (int)STRLEN(p_buf);
647 if (len > IOSIZE - 2)
648 {
649 state->linebuf = qf_grow_linebuf(state, len);
650 if (state->linebuf == NULL)
651 return QF_NOMEM;
652 }
653 else
654 {
655 state->linebuf = IObuff;
656 state->linelen = len;
657 }
658 vim_strncpy(state->linebuf, p_buf, state->linelen);
659
660 return QF_OK;
661}
662
663/*
664 * Get the next string from file state->fd.
665 */
666 static int
667qf_get_next_file_line(qfstate_T *state)
668{
669 int discard;
670 int growbuflen;
671
672 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
673 return QF_END_OF_INPUT;
674
675 discard = FALSE;
676 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200677 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200678 {
679 /*
680 * The current line exceeds IObuff, continue reading using
681 * growbuf until EOL or LINE_MAXLEN bytes is read.
682 */
683 if (state->growbuf == NULL)
684 {
685 state->growbufsiz = 2 * (IOSIZE - 1);
686 state->growbuf = alloc(state->growbufsiz);
687 if (state->growbuf == NULL)
688 return QF_NOMEM;
689 }
690
691 /* Copy the read part of the line, excluding null-terminator */
692 memcpy(state->growbuf, IObuff, IOSIZE - 1);
693 growbuflen = state->linelen;
694
695 for (;;)
696 {
697 if (fgets((char *)state->growbuf + growbuflen,
698 state->growbufsiz - growbuflen, state->fd) == NULL)
699 break;
700 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
701 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200702 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200703 break;
704 if (state->growbufsiz == LINE_MAXLEN)
705 {
706 discard = TRUE;
707 break;
708 }
709
710 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
711 ? 2 * state->growbufsiz : LINE_MAXLEN;
712 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
713 if (state->growbuf == NULL)
714 return QF_NOMEM;
715 }
716
717 while (discard)
718 {
719 /*
720 * The current line is longer than LINE_MAXLEN, continue
721 * reading but discard everything until EOL or EOF is
722 * reached.
723 */
724 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
725 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200726 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200727 break;
728 }
729
730 state->linebuf = state->growbuf;
731 state->linelen = growbuflen;
732 }
733 else
734 state->linebuf = IObuff;
735
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100736#ifdef FEAT_MBYTE
737 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200738 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
739 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100740 char_u *line;
741
742 line = string_convert(&state->vc, state->linebuf, &state->linelen);
743 if (line != NULL)
744 {
745 if (state->linelen < IOSIZE)
746 {
747 STRCPY(state->linebuf, line);
748 vim_free(line);
749 }
750 else
751 {
752 vim_free(state->growbuf);
753 state->linebuf = state->growbuf = line;
754 state->growbufsiz = state->linelen < LINE_MAXLEN
755 ? state->linelen : LINE_MAXLEN;
756 }
757 }
758 }
759#endif
760
Bram Moolenaare0d37972016-07-15 22:36:01 +0200761 return QF_OK;
762}
763
764/*
765 * Get the next string from a file/buffer/list/string.
766 */
767 static int
768qf_get_nextline(qfstate_T *state)
769{
770 int status = QF_FAIL;
771
772 if (state->fd == NULL)
773 {
774 if (state->tv != NULL)
775 {
776 if (state->tv->v_type == VAR_STRING)
777 /* Get the next line from the supplied string */
778 status = qf_get_next_str_line(state);
779 else if (state->tv->v_type == VAR_LIST)
780 /* Get the next line from the supplied list */
781 status = qf_get_next_list_line(state);
782 }
783 else
784 /* Get the next line from the supplied buffer */
785 status = qf_get_next_buf_line(state);
786 }
787 else
788 /* Get the next line from the supplied file */
789 status = qf_get_next_file_line(state);
790
791 if (status != QF_OK)
792 return status;
793
794 /* remove newline/CR from the line */
795 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200796 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200797 state->linebuf[state->linelen - 1] = NUL;
798#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200799 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
800 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200801#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200802 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200803
804#ifdef FEAT_MBYTE
805 remove_bom(state->linebuf);
806#endif
807
808 return QF_OK;
809}
810
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200811typedef struct {
812 char_u *namebuf;
813 char_u *errmsg;
814 int errmsglen;
815 long lnum;
816 int col;
817 char_u use_viscol;
818 char_u *pattern;
819 int enr;
820 int type;
821 int valid;
822} qffields_T;
823
824/*
825 * Parse a line and get the quickfix fields.
826 * Return the QF_ status.
827 */
828 static int
829qf_parse_line(
830 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200831 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200832 char_u *linebuf,
833 int linelen,
834 efm_T *fmt_first,
835 qffields_T *fields)
836{
837 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200838 char_u *ptr;
839 int len;
840 int i;
841 int idx = 0;
842 char_u *tail = NULL;
843 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200844 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200845
846 /* Always ignore case when looking for a matching error. */
847 regmatch.rm_ic = TRUE;
848
849 /* If there was no %> item start at the first pattern */
850 if (fmt_start == NULL)
851 fmt_ptr = fmt_first;
852 else
853 {
854 fmt_ptr = fmt_start;
855 fmt_start = NULL;
856 }
857
858 /*
859 * Try to match each part of 'errorformat' until we find a complete
860 * match or no match.
861 */
862 fields->valid = TRUE;
863restofline:
864 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
865 {
866 int r;
867
868 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200869 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200870 continue;
871 fields->namebuf[0] = NUL;
872 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200873 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200874 fields->errmsg[0] = NUL;
875 fields->lnum = 0;
876 fields->col = 0;
877 fields->use_viscol = FALSE;
878 fields->enr = -1;
879 fields->type = 0;
880 tail = NULL;
881
882 regmatch.regprog = fmt_ptr->prog;
883 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
884 fmt_ptr->prog = regmatch.regprog;
885 if (r)
886 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200887 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200888 continue;
889 if (vim_strchr((char_u *)"EWI", idx) != NULL)
890 fields->type = idx;
891 else
892 fields->type = 0;
893 /*
894 * Extract error message data from matched line.
895 * We check for an actual submatch, because "\[" and "\]" in
896 * the 'errorformat' may cause the wrong submatch to be used.
897 */
898 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
899 {
900 int c;
901
902 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
903 continue;
904
905 /* Expand ~/file and $HOME/file to full path. */
906 c = *regmatch.endp[i];
907 *regmatch.endp[i] = NUL;
908 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
909 *regmatch.endp[i] = c;
910
911 if (vim_strchr((char_u *)"OPQ", idx) != NULL
912 && mch_getperm(fields->namebuf) == -1)
913 continue;
914 }
915 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
916 {
917 if (regmatch.startp[i] == NULL)
918 continue;
919 fields->enr = (int)atol((char *)regmatch.startp[i]);
920 }
921 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
922 {
923 if (regmatch.startp[i] == NULL)
924 continue;
925 fields->lnum = atol((char *)regmatch.startp[i]);
926 }
927 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
928 {
929 if (regmatch.startp[i] == NULL)
930 continue;
931 fields->col = (int)atol((char *)regmatch.startp[i]);
932 }
933 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
934 {
935 if (regmatch.startp[i] == NULL)
936 continue;
937 fields->type = *regmatch.startp[i];
938 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200939 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200940 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200941 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200942 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200943 /* linelen + null terminator */
944 if ((fields->errmsg = vim_realloc(fields->errmsg,
945 linelen + 1)) == NULL)
946 return QF_NOMEM;
947 fields->errmsglen = linelen + 1;
948 }
949 vim_strncpy(fields->errmsg, linebuf, linelen);
950 }
951 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
952 {
953 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
954 continue;
955 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200956 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200957 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200958 /* len + null terminator */
959 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
960 == NULL)
961 return QF_NOMEM;
962 fields->errmsglen = len + 1;
963 }
964 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
965 }
966 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
967 {
968 if (regmatch.startp[i] == NULL)
969 continue;
970 tail = regmatch.startp[i];
971 }
972 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
973 {
974 char_u *match_ptr;
975
976 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
977 continue;
978 fields->col = 0;
979 for (match_ptr = regmatch.startp[i];
980 match_ptr != regmatch.endp[i]; ++match_ptr)
981 {
982 ++fields->col;
983 if (*match_ptr == TAB)
984 {
985 fields->col += 7;
986 fields->col -= fields->col % 8;
987 }
988 }
989 ++fields->col;
990 fields->use_viscol = TRUE;
991 }
992 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
993 {
994 if (regmatch.startp[i] == NULL)
995 continue;
996 fields->col = (int)atol((char *)regmatch.startp[i]);
997 fields->use_viscol = TRUE;
998 }
999 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1000 {
1001 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1002 continue;
1003 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1004 if (len > CMDBUFFSIZE - 5)
1005 len = CMDBUFFSIZE - 5;
1006 STRCPY(fields->pattern, "^\\V");
1007 STRNCAT(fields->pattern, regmatch.startp[i], len);
1008 fields->pattern[len + 3] = '\\';
1009 fields->pattern[len + 4] = '$';
1010 fields->pattern[len + 5] = NUL;
1011 }
1012 break;
1013 }
1014 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001015 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001016
1017 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1018 {
1019 if (fmt_ptr != NULL)
1020 {
1021 if (idx == 'D') /* enter directory */
1022 {
1023 if (*fields->namebuf == NUL)
1024 {
1025 EMSG(_("E379: Missing or empty directory name"));
1026 return QF_FAIL;
1027 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001028 qfl->qf_directory =
1029 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1030 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001031 return QF_FAIL;
1032 }
1033 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001034 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001035 }
1036 fields->namebuf[0] = NUL; /* no match found, remove file name */
1037 fields->lnum = 0; /* don't jump to this line */
1038 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001039 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001040 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001041 /* linelen + null terminator */
1042 if ((fields->errmsg = vim_realloc(fields->errmsg,
1043 linelen + 1)) == NULL)
1044 return QF_NOMEM;
1045 fields->errmsglen = linelen + 1;
1046 }
1047 /* copy whole line to error message */
1048 vim_strncpy(fields->errmsg, linebuf, linelen);
1049 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001050 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001051 }
1052 else if (fmt_ptr != NULL)
1053 {
1054 /* honor %> item */
1055 if (fmt_ptr->conthere)
1056 fmt_start = fmt_ptr;
1057
1058 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1059 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001060 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1061 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001062 }
1063 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1064 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001065 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001066 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001067 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001068
Bram Moolenaar9b457942016-10-09 16:10:05 +02001069 if (qfprev == NULL)
1070 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001071 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001072 {
1073 len = (int)STRLEN(qfprev->qf_text);
1074 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1075 == NULL)
1076 return QF_FAIL;
1077 STRCPY(ptr, qfprev->qf_text);
1078 vim_free(qfprev->qf_text);
1079 qfprev->qf_text = ptr;
1080 *(ptr += len) = '\n';
1081 STRCPY(++ptr, fields->errmsg);
1082 }
1083 if (qfprev->qf_nr == -1)
1084 qfprev->qf_nr = fields->enr;
1085 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1086 /* only printable chars allowed */
1087 qfprev->qf_type = fields->type;
1088
1089 if (!qfprev->qf_lnum)
1090 qfprev->qf_lnum = fields->lnum;
1091 if (!qfprev->qf_col)
1092 qfprev->qf_col = fields->col;
1093 qfprev->qf_viscol = fields->use_viscol;
1094 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001095 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1096 qfl->qf_directory,
1097 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001098 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001099 : qfl->qf_currfile != NULL && fields->valid
1100 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001101 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001102 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001103 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001104 line_breakcheck();
1105 return QF_IGNORE_LINE;
1106 }
1107 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1108 {
1109 /* global file names */
1110 fields->valid = FALSE;
1111 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1112 {
1113 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001114 qfl->qf_currfile =
1115 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001116 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001117 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001118 *fields->namebuf = NUL;
1119 if (tail && *tail)
1120 {
1121 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001122 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001123 goto restofline;
1124 }
1125 }
1126 }
1127 if (fmt_ptr->flags == '-') /* generally exclude this line */
1128 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001129 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001130 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001131 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001132 return QF_IGNORE_LINE;
1133 }
1134 }
1135
1136 return QF_OK;
1137}
1138
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001139/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001140 * Read the errorfile "efile" into memory, line by line, building the error
1141 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001142 * Alternative: when "efile" is NULL read errors from buffer "buf".
1143 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001144 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001145 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1146 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001147 * Return -1 for error, number of errors for success.
1148 */
1149 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001150qf_init_ext(
1151 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001152 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001153 char_u *efile,
1154 buf_T *buf,
1155 typval_T *tv,
1156 char_u *errorformat,
1157 int newlist, /* TRUE: start a new error list */
1158 linenr_T lnumfirst, /* first line number to use */
1159 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001160 char_u *qf_title,
1161 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001162{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001163 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001164 qfstate_T state;
1165 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001166 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001167 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001168 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001170 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001172 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001174 /* Do not used the cached buffer, it may have been wiped out. */
1175 vim_free(qf_last_bufname);
1176 qf_last_bufname = NULL;
1177
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001178 vim_memset(&state, 0, sizeof(state));
1179 vim_memset(&fields, 0, sizeof(fields));
1180#ifdef FEAT_MBYTE
1181 state.vc.vc_type = CONV_NONE;
1182 if (enc != NULL && *enc != NUL)
1183 convert_setup(&state.vc, enc, p_enc);
1184#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001185 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1186 fields.errmsglen = CMDBUFFSIZE + 1;
1187 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1188 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001189 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 goto qf_init_end;
1191
Bram Moolenaare0d37972016-07-15 22:36:01 +02001192 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
1194 EMSG2(_(e_openerrf), efile);
1195 goto qf_init_end;
1196 }
1197
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001198 if (newlist || qf_idx == qi->qf_listcount)
1199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001201 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001202 qf_idx = qi->qf_curlist;
1203 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001204 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001205 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001206 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001207 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001208 if (qi->qf_lists[qf_idx].qf_count > 0)
1209 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001212 qfl = &qi->qf_lists[qf_idx];
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001215 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001216 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 else
1218 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001220 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001221 * If the errorformat didn't change between calls, then reuse the
1222 * previously parsed values.
1223 */
1224 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1225 {
1226 /* free the previously parsed data */
1227 vim_free(last_efm);
1228 last_efm = NULL;
1229 free_efm_list(&fmt_first);
1230
1231 /* parse the current 'efm' */
1232 fmt_first = parse_efm_option(efm);
1233 if (fmt_first != NULL)
1234 last_efm = vim_strsave(efm);
1235 }
1236
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
1240 /*
1241 * got_int is reset here, because it was probably set when killing the
1242 * ":make" command, but we still want to read the errorfile then.
1243 */
1244 got_int = FALSE;
1245
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001246 if (tv != NULL)
1247 {
1248 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001249 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001250 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001251 state.p_li = tv->vval.v_list->lv_first;
1252 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001253 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001254 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001255 state.buflnum = lnumfirst;
1256 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 /*
1259 * Read the lines in the error file one by one.
1260 * Try to recognize one of the error formats in each line.
1261 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001262 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001264 /* Get the next line from a file/buffer/list/string */
1265 status = qf_get_nextline(&state);
1266 if (status == QF_NOMEM) /* memory alloc failure */
1267 goto qf_init_end;
1268 if (status == QF_END_OF_INPUT) /* end of input */
1269 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001271 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1272 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001273 if (status == QF_FAIL)
1274 goto error2;
1275 if (status == QF_NOMEM)
1276 goto qf_init_end;
1277 if (status == QF_IGNORE_LINE)
1278 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001280 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001281 qf_idx,
1282 qfl->qf_directory,
1283 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001284 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001285 : ((qfl->qf_currfile != NULL && fields.valid)
1286 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001287 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001288 fields.errmsg,
1289 fields.lnum,
1290 fields.col,
1291 fields.use_viscol,
1292 fields.pattern,
1293 fields.enr,
1294 fields.type,
1295 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 goto error2;
1297 line_breakcheck();
1298 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001299 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001301 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001303 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001304 qfl->qf_ptr = qfl->qf_start;
1305 qfl->qf_index = 1;
1306 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308 else
1309 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001310 qfl->qf_nonevalid = FALSE;
1311 if (qfl->qf_ptr == NULL)
1312 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001314 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001315 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001316 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318 EMSG(_(e_readerrf));
1319error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001320 if (!adding)
1321 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001322 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001323 qf_free(qi, qi->qf_curlist);
1324 qi->qf_listcount--;
1325 if (qi->qf_curlist > 0)
1326 --qi->qf_curlist;
1327 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001328qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001329 if (state.fd != NULL)
1330 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001331 vim_free(fields.namebuf);
1332 vim_free(fields.errmsg);
1333 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001334 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001336 if (qf_idx == qi->qf_curlist)
1337 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001338#ifdef FEAT_MBYTE
1339 if (state.vc.vc_type != CONV_NONE)
1340 convert_setup(&state.vc, NULL, NULL);
1341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 return retval;
1344}
1345
Bram Moolenaarfb604092014-07-23 15:55:00 +02001346 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001347qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001348{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001349 vim_free(qi->qf_lists[qf_idx].qf_title);
1350 qi->qf_lists[qf_idx].qf_title = NULL;
1351
Bram Moolenaarfb604092014-07-23 15:55:00 +02001352 if (title != NULL)
1353 {
1354 char_u *p = alloc((int)STRLEN(title) + 2);
1355
Bram Moolenaara3921f42017-06-04 15:30:34 +02001356 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001357 if (p != NULL)
1358 sprintf((char *)p, ":%s", (char *)title);
1359 }
1360}
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001363 * Prepare for adding a new quickfix list. If the current list is in the
1364 * middle of the stack, then all the following lists are freed and then
1365 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 */
1367 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001368qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
1370 int i;
1371
1372 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001373 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 * the current entry. This makes it possible to browse in a tree-like
1375 * way with ":grep'.
1376 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001377 while (qi->qf_listcount > qi->qf_curlist + 1)
1378 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
1380 /*
1381 * When the stack is full, remove to oldest entry
1382 * Otherwise, add a new entry.
1383 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001384 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001386 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001388 qi->qf_lists[i - 1] = qi->qf_lists[i];
1389 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001392 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001393 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001394 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001395 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396}
1397
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001398/*
1399 * Free a location list
1400 */
1401 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001402ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001403{
1404 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001405 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001406
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001407 qi = *pqi;
1408 if (qi == NULL)
1409 return;
1410 *pqi = NULL; /* Remove reference to this list */
1411
1412 qi->qf_refcount--;
1413 if (qi->qf_refcount < 1)
1414 {
1415 /* No references to this location list */
1416 for (i = 0; i < qi->qf_listcount; ++i)
1417 qf_free(qi, i);
1418 vim_free(qi);
1419 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001420}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001421
1422 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001423qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001424{
1425 int i;
1426 qf_info_T *qi = &ql_info;
1427
1428 if (wp != NULL)
1429 {
1430 /* location list */
1431 ll_free_all(&wp->w_llist);
1432 ll_free_all(&wp->w_llist_ref);
1433 }
1434 else
1435 /* quickfix list */
1436 for (i = 0; i < qi->qf_listcount; ++i)
1437 qf_free(qi, i);
1438}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440/*
1441 * Add an entry to the end of the list of errors.
1442 * Returns OK or FAIL.
1443 */
1444 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001445qf_add_entry(
1446 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001447 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001448 char_u *dir, /* optional directory name */
1449 char_u *fname, /* file name or NULL */
1450 int bufnum, /* buffer number or zero */
1451 char_u *mesg, /* message */
1452 long lnum, /* line number */
1453 int col, /* column */
1454 int vis_col, /* using visual column */
1455 char_u *pattern, /* search pattern */
1456 int nr, /* error number */
1457 int type, /* type character */
1458 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001460 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001461 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001463 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001465 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001466 {
1467 buf_T *buf = buflist_findnr(bufnum);
1468
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001469 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001470 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001471 buf->b_has_qf_entry |=
1472 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001473 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001474 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001475 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1477 {
1478 vim_free(qfp);
1479 return FAIL;
1480 }
1481 qfp->qf_lnum = lnum;
1482 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001483 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001484 if (pattern == NULL || *pattern == NUL)
1485 qfp->qf_pattern = NULL;
1486 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1487 {
1488 vim_free(qfp->qf_text);
1489 vim_free(qfp);
1490 return FAIL;
1491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 qfp->qf_nr = nr;
1493 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1494 type = 0;
1495 qfp->qf_type = type;
1496 qfp->qf_valid = valid;
1497
Bram Moolenaara3921f42017-06-04 15:30:34 +02001498 lastp = &qi->qf_lists[qf_idx].qf_last;
1499 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001500 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001502 qi->qf_lists[qf_idx].qf_start = qfp;
1503 qi->qf_lists[qf_idx].qf_ptr = qfp;
1504 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001505 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507 else
1508 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001509 qfp->qf_prev = *lastp;
1510 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001512 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001514 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001515 ++qi->qf_lists[qf_idx].qf_count;
1516 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001517 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001519 qi->qf_lists[qf_idx].qf_index =
1520 qi->qf_lists[qf_idx].qf_count;
1521 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 }
1523
1524 return OK;
1525}
1526
1527/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001528 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001529 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001530 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001531ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001532{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001533 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001535 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1536 if (qi != NULL)
1537 {
1538 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1539 qi->qf_refcount++;
1540 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001541
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001542 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543}
1544
1545/*
1546 * Return the location list for window 'wp'.
1547 * If not present, allocate a location list
1548 */
1549 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001550ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001551{
1552 if (IS_LL_WINDOW(wp))
1553 /* For a location list window, use the referenced location list */
1554 return wp->w_llist_ref;
1555
1556 /*
1557 * For a non-location list window, w_llist_ref should not point to a
1558 * location list.
1559 */
1560 ll_free_all(&wp->w_llist_ref);
1561
1562 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001563 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001564 return wp->w_llist;
1565}
1566
1567/*
1568 * Copy the location list from window "from" to window "to".
1569 */
1570 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001571copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001572{
1573 qf_info_T *qi;
1574 int idx;
1575 int i;
1576
1577 /*
1578 * When copying from a location list window, copy the referenced
1579 * location list. For other windows, copy the location list for
1580 * that window.
1581 */
1582 if (IS_LL_WINDOW(from))
1583 qi = from->w_llist_ref;
1584 else
1585 qi = from->w_llist;
1586
1587 if (qi == NULL) /* no location list to copy */
1588 return;
1589
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001590 /* allocate a new location list */
1591 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001592 return;
1593
1594 to->w_llist->qf_listcount = qi->qf_listcount;
1595
1596 /* Copy the location lists one at a time */
1597 for (idx = 0; idx < qi->qf_listcount; idx++)
1598 {
1599 qf_list_T *from_qfl;
1600 qf_list_T *to_qfl;
1601
1602 to->w_llist->qf_curlist = idx;
1603
1604 from_qfl = &qi->qf_lists[idx];
1605 to_qfl = &to->w_llist->qf_lists[idx];
1606
1607 /* Some of the fields are populated by qf_add_entry() */
1608 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1609 to_qfl->qf_count = 0;
1610 to_qfl->qf_index = 0;
1611 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001612 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001613 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001614 if (from_qfl->qf_title != NULL)
1615 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1616 else
1617 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001618 if (from_qfl->qf_ctx != NULL)
1619 {
1620 to_qfl->qf_ctx = alloc_tv();
1621 if (to_qfl->qf_ctx != NULL)
1622 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1623 }
1624 else
1625 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001626
1627 if (from_qfl->qf_count)
1628 {
1629 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001630 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631
1632 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001633 for (i = 0, from_qfp = from_qfl->qf_start;
1634 i < from_qfl->qf_count && from_qfp != NULL;
1635 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001636 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001637 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001638 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001639 NULL,
1640 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001641 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001642 from_qfp->qf_text,
1643 from_qfp->qf_lnum,
1644 from_qfp->qf_col,
1645 from_qfp->qf_viscol,
1646 from_qfp->qf_pattern,
1647 from_qfp->qf_nr,
1648 0,
1649 from_qfp->qf_valid) == FAIL)
1650 {
1651 qf_free_all(to);
1652 return;
1653 }
1654 /*
1655 * qf_add_entry() will not set the qf_num field, as the
1656 * directory and file names are not supplied. So the qf_fnum
1657 * field is copied here.
1658 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001659 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001660 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1661 prevp->qf_type = from_qfp->qf_type; /* error type */
1662 if (from_qfl->qf_ptr == from_qfp)
1663 to_qfl->qf_ptr = prevp; /* current location */
1664 }
1665 }
1666
1667 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1668
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001669 /* Assign a new ID for the location list */
1670 to_qfl->qf_id = ++last_qf_id;
1671
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001672 /* When no valid entries are present in the list, qf_ptr points to
1673 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001674 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001675 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001676 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001677 to_qfl->qf_index = 1;
1678 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001679 }
1680
1681 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1682}
1683
1684/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001685 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001686 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
1688 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001689qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
Bram Moolenaar82404332016-07-10 17:00:38 +02001691 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001692 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001693 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001694
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (fname == NULL || *fname == NUL) /* no file name */
1696 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
Bram Moolenaare60acc12011-05-10 16:41:25 +02001698#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001699 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001700#endif
1701#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001702 if (directory != NULL)
1703 slash_adjust(directory);
1704 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001705#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001706 if (directory != NULL && !vim_isAbsName(fname)
1707 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1708 {
1709 /*
1710 * Here we check if the file really exists.
1711 * This should normally be true, but if make works without
1712 * "leaving directory"-messages we might have missed a
1713 * directory change.
1714 */
1715 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001718 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001719 if (directory)
1720 ptr = concat_fnames(directory, fname, TRUE);
1721 else
1722 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001724 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001725 bufname = ptr;
1726 }
1727 else
1728 bufname = fname;
1729
1730 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001731 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001732 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001733 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001734 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001736 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001737 {
1738 vim_free(qf_last_bufname);
1739 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1740 if (bufname == ptr)
1741 qf_last_bufname = bufname;
1742 else
1743 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001744 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001745 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001746 if (buf == NULL)
1747 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001748
Bram Moolenaarc1542742016-07-20 21:44:37 +02001749 buf->b_has_qf_entry =
1750 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001751 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
1754/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001755 * Push dirbuf onto the directory stack and return pointer to actual dir or
1756 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 */
1758 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001759qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 struct dir_stack_T *ds_new;
1762 struct dir_stack_T *ds_ptr;
1763
1764 /* allocate new stack element and hook it in */
1765 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1766 if (ds_new == NULL)
1767 return NULL;
1768
1769 ds_new->next = *stackptr;
1770 *stackptr = ds_new;
1771
1772 /* store directory on the stack */
1773 if (vim_isAbsName(dirbuf)
1774 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001775 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 (*stackptr)->dirname = vim_strsave(dirbuf);
1777 else
1778 {
1779 /* Okay we don't have an absolute path.
1780 * dirbuf must be a subdir of one of the directories on the stack.
1781 * Let's search...
1782 */
1783 ds_new = (*stackptr)->next;
1784 (*stackptr)->dirname = NULL;
1785 while (ds_new)
1786 {
1787 vim_free((*stackptr)->dirname);
1788 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1789 TRUE);
1790 if (mch_isdir((*stackptr)->dirname) == TRUE)
1791 break;
1792
1793 ds_new = ds_new->next;
1794 }
1795
1796 /* clean up all dirs we already left */
1797 while ((*stackptr)->next != ds_new)
1798 {
1799 ds_ptr = (*stackptr)->next;
1800 (*stackptr)->next = (*stackptr)->next->next;
1801 vim_free(ds_ptr->dirname);
1802 vim_free(ds_ptr);
1803 }
1804
1805 /* Nothing found -> it must be on top level */
1806 if (ds_new == NULL)
1807 {
1808 vim_free((*stackptr)->dirname);
1809 (*stackptr)->dirname = vim_strsave(dirbuf);
1810 }
1811 }
1812
1813 if ((*stackptr)->dirname != NULL)
1814 return (*stackptr)->dirname;
1815 else
1816 {
1817 ds_ptr = *stackptr;
1818 *stackptr = (*stackptr)->next;
1819 vim_free(ds_ptr);
1820 return NULL;
1821 }
1822}
1823
1824
1825/*
1826 * pop dirbuf from the directory stack and return previous directory or NULL if
1827 * stack is empty
1828 */
1829 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001830qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 struct dir_stack_T *ds_ptr;
1833
1834 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1835 * What to do if it isn't? */
1836
1837 /* pop top element and free it */
1838 if (*stackptr != NULL)
1839 {
1840 ds_ptr = *stackptr;
1841 *stackptr = (*stackptr)->next;
1842 vim_free(ds_ptr->dirname);
1843 vim_free(ds_ptr);
1844 }
1845
1846 /* return NEW top element as current dir or NULL if stack is empty*/
1847 return *stackptr ? (*stackptr)->dirname : NULL;
1848}
1849
1850/*
1851 * clean up directory stack
1852 */
1853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001854qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 struct dir_stack_T *ds_ptr;
1857
1858 while ((ds_ptr = *stackptr) != NULL)
1859 {
1860 *stackptr = (*stackptr)->next;
1861 vim_free(ds_ptr->dirname);
1862 vim_free(ds_ptr);
1863 }
1864}
1865
1866/*
1867 * Check in which directory of the directory stack the given file can be
1868 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001869 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 * Cleans up intermediate directory entries.
1871 *
1872 * TODO: How to solve the following problem?
1873 * If we have the this directory tree:
1874 * ./
1875 * ./aa
1876 * ./aa/bb
1877 * ./bb
1878 * ./bb/x.c
1879 * and make says:
1880 * making all in aa
1881 * making all in bb
1882 * x.c:9: Error
1883 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1884 * qf_guess_filepath will return NULL.
1885 */
1886 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001887qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
1889 struct dir_stack_T *ds_ptr;
1890 struct dir_stack_T *ds_tmp;
1891 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001892 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
1894 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001895 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 return NULL;
1897
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001898 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 fullname = NULL;
1900 while (ds_ptr)
1901 {
1902 vim_free(fullname);
1903 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1904
1905 /* If concat_fnames failed, just go on. The worst thing that can happen
1906 * is that we delete the entire stack.
1907 */
1908 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1909 break;
1910
1911 ds_ptr = ds_ptr->next;
1912 }
1913
1914 vim_free(fullname);
1915
1916 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001917 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001919 ds_tmp = qfl->qf_dir_stack->next;
1920 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 vim_free(ds_tmp->dirname);
1922 vim_free(ds_tmp);
1923 }
1924
1925 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926}
1927
1928/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001929 * When loading a file from the quickfix, the auto commands may modify it.
1930 * This may invalidate the current quickfix entry. This function checks
1931 * whether a entry is still present in the quickfix.
1932 * Similar to location list.
1933 */
1934 static int
1935is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1936{
1937 qf_list_T *qfl;
1938 qfline_T *qfp;
1939 int i;
1940
1941 qfl = &qi->qf_lists[qi->qf_curlist];
1942
1943 /* Search for the entry in the current list */
1944 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1945 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001946 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001947 break;
1948
1949 if (i == qfl->qf_count) /* Entry is not found */
1950 return FALSE;
1951
1952 return TRUE;
1953}
1954
1955/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001956 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001957 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001958 */
1959 static qfline_T *
1960get_next_valid_entry(
1961 qf_info_T *qi,
1962 qfline_T *qf_ptr,
1963 int *qf_index,
1964 int dir)
1965{
1966 int idx;
1967 int old_qf_fnum;
1968
1969 idx = *qf_index;
1970 old_qf_fnum = qf_ptr->qf_fnum;
1971
1972 do
1973 {
1974 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
1975 || qf_ptr->qf_next == NULL)
1976 return NULL;
1977 ++idx;
1978 qf_ptr = qf_ptr->qf_next;
1979 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1980 && !qf_ptr->qf_valid)
1981 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1982
1983 *qf_index = idx;
1984 return qf_ptr;
1985}
1986
1987/*
1988 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001989 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001990 */
1991 static qfline_T *
1992get_prev_valid_entry(
1993 qf_info_T *qi,
1994 qfline_T *qf_ptr,
1995 int *qf_index,
1996 int dir)
1997{
1998 int idx;
1999 int old_qf_fnum;
2000
2001 idx = *qf_index;
2002 old_qf_fnum = qf_ptr->qf_fnum;
2003
2004 do
2005 {
2006 if (idx == 1 || qf_ptr->qf_prev == NULL)
2007 return NULL;
2008 --idx;
2009 qf_ptr = qf_ptr->qf_prev;
2010 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2011 && !qf_ptr->qf_valid)
2012 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2013
2014 *qf_index = idx;
2015 return qf_ptr;
2016}
2017
2018/*
2019 * Get the n'th (errornr) previous/next valid entry from the current entry in
2020 * the quickfix list.
2021 * dir == FORWARD or FORWARD_FILE: next valid entry
2022 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2023 */
2024 static qfline_T *
2025get_nth_valid_entry(
2026 qf_info_T *qi,
2027 int errornr,
2028 qfline_T *qf_ptr,
2029 int *qf_index,
2030 int dir)
2031{
2032 qfline_T *prev_qf_ptr;
2033 int prev_index;
2034 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2035 char_u *err = e_no_more_items;
2036
2037 while (errornr--)
2038 {
2039 prev_qf_ptr = qf_ptr;
2040 prev_index = *qf_index;
2041
2042 if (dir == FORWARD || dir == FORWARD_FILE)
2043 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2044 else
2045 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2046 if (qf_ptr == NULL)
2047 {
2048 qf_ptr = prev_qf_ptr;
2049 *qf_index = prev_index;
2050 if (err != NULL)
2051 {
2052 EMSG(_(err));
2053 return NULL;
2054 }
2055 break;
2056 }
2057
2058 err = NULL;
2059 }
2060
2061 return qf_ptr;
2062}
2063
2064/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002065 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002066 */
2067 static qfline_T *
2068get_nth_entry(
2069 qf_info_T *qi,
2070 int errornr,
2071 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002072 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002073{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002074 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002075
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002076 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002077 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2078 {
2079 --qf_idx;
2080 qf_ptr = qf_ptr->qf_prev;
2081 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002082 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002083 while (errornr > qf_idx &&
2084 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2085 qf_ptr->qf_next != NULL)
2086 {
2087 ++qf_idx;
2088 qf_ptr = qf_ptr->qf_next;
2089 }
2090
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002091 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002092 return qf_ptr;
2093}
2094
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002095/*
2096 * Find a help window or open one.
2097 */
2098 static int
2099jump_to_help_window(qf_info_T *qi, int *opened_window)
2100{
2101 win_T *wp;
2102 int flags;
2103
2104 if (cmdmod.tab != 0)
2105 wp = NULL;
2106 else
2107 FOR_ALL_WINDOWS(wp)
2108 if (bt_help(wp->w_buffer))
2109 break;
2110 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2111 win_enter(wp, TRUE);
2112 else
2113 {
2114 /*
2115 * Split off help window; put it at far top if no position
2116 * specified, the current window is vertically split and narrow.
2117 */
2118 flags = WSP_HELP;
2119 if (cmdmod.split == 0 && curwin->w_width != Columns
2120 && curwin->w_width < 80)
2121 flags |= WSP_TOP;
2122 if (qi != &ql_info)
2123 flags |= WSP_NEWLOC; /* don't copy the location list */
2124
2125 if (win_split(0, flags) == FAIL)
2126 return FAIL;
2127
2128 *opened_window = TRUE;
2129
2130 if (curwin->w_height < p_hh)
2131 win_setheight((int)p_hh);
2132
2133 if (qi != &ql_info) /* not a quickfix list */
2134 {
2135 /* The new window should use the supplied location list */
2136 curwin->w_llist = qi;
2137 qi->qf_refcount++;
2138 }
2139 }
2140
2141 if (!p_im)
2142 restart_edit = 0; /* don't want insert mode in help file */
2143
2144 return OK;
2145}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002146
2147/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002148 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2149 * If the file is already opened in a window, jump to it.
2150 */
2151 static int
2152qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2153{
2154 win_T *usable_win_ptr = NULL;
2155 int usable_win;
2156 qf_info_T *ll_ref;
2157 int flags;
2158 win_T *win;
2159 win_T *altwin;
2160
2161 usable_win = 0;
2162
2163 ll_ref = curwin->w_llist_ref;
2164 if (ll_ref != NULL)
2165 {
2166 /* Find a window using the same location list that is not a
2167 * quickfix window. */
2168 FOR_ALL_WINDOWS(usable_win_ptr)
2169 if (usable_win_ptr->w_llist == ll_ref
2170 && !bt_quickfix(usable_win_ptr->w_buffer))
2171 {
2172 usable_win = 1;
2173 break;
2174 }
2175 }
2176
2177 if (!usable_win)
2178 {
2179 /* Locate a window showing a normal buffer */
2180 FOR_ALL_WINDOWS(win)
2181 if (win->w_buffer->b_p_bt[0] == NUL)
2182 {
2183 usable_win = 1;
2184 break;
2185 }
2186 }
2187
2188 /*
2189 * If no usable window is found and 'switchbuf' contains "usetab"
2190 * then search in other tabs.
2191 */
2192 if (!usable_win && (swb_flags & SWB_USETAB))
2193 {
2194 tabpage_T *tp;
2195 win_T *wp;
2196
2197 FOR_ALL_TAB_WINDOWS(tp, wp)
2198 {
2199 if (wp->w_buffer->b_fnum == qf_fnum)
2200 {
2201 goto_tabpage_win(tp, wp);
2202 usable_win = 1;
2203 goto win_found;
2204 }
2205 }
2206 }
2207win_found:
2208
2209 /*
2210 * If there is only one window and it is the quickfix window, create a
2211 * new one above the quickfix window.
2212 */
2213 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2214 {
2215 flags = WSP_ABOVE;
2216 if (ll_ref != NULL)
2217 flags |= WSP_NEWLOC;
2218 if (win_split(0, flags) == FAIL)
2219 return FAIL; /* not enough room for window */
2220 *opened_window = TRUE; /* close it when fail */
2221 p_swb = empty_option; /* don't split again */
2222 swb_flags = 0;
2223 RESET_BINDING(curwin);
2224 if (ll_ref != NULL)
2225 {
2226 /* The new window should use the location list from the
2227 * location list window */
2228 curwin->w_llist = ll_ref;
2229 ll_ref->qf_refcount++;
2230 }
2231 }
2232 else
2233 {
2234 if (curwin->w_llist_ref != NULL)
2235 {
2236 /* In a location window */
2237 win = usable_win_ptr;
2238 if (win == NULL)
2239 {
2240 /* Find the window showing the selected file */
2241 FOR_ALL_WINDOWS(win)
2242 if (win->w_buffer->b_fnum == qf_fnum)
2243 break;
2244 if (win == NULL)
2245 {
2246 /* Find a previous usable window */
2247 win = curwin;
2248 do
2249 {
2250 if (win->w_buffer->b_p_bt[0] == NUL)
2251 break;
2252 if (win->w_prev == NULL)
2253 win = lastwin; /* wrap around the top */
2254 else
2255 win = win->w_prev; /* go to previous window */
2256 } while (win != curwin);
2257 }
2258 }
2259 win_goto(win);
2260
2261 /* If the location list for the window is not set, then set it
2262 * to the location list from the location window */
2263 if (win->w_llist == NULL)
2264 {
2265 win->w_llist = ll_ref;
2266 ll_ref->qf_refcount++;
2267 }
2268 }
2269 else
2270 {
2271
2272 /*
2273 * Try to find a window that shows the right buffer.
2274 * Default to the window just above the quickfix buffer.
2275 */
2276 win = curwin;
2277 altwin = NULL;
2278 for (;;)
2279 {
2280 if (win->w_buffer->b_fnum == qf_fnum)
2281 break;
2282 if (win->w_prev == NULL)
2283 win = lastwin; /* wrap around the top */
2284 else
2285 win = win->w_prev; /* go to previous window */
2286
2287 if (IS_QF_WINDOW(win))
2288 {
2289 /* Didn't find it, go to the window before the quickfix
2290 * window. */
2291 if (altwin != NULL)
2292 win = altwin;
2293 else if (curwin->w_prev != NULL)
2294 win = curwin->w_prev;
2295 else
2296 win = curwin->w_next;
2297 break;
2298 }
2299
2300 /* Remember a usable window. */
2301 if (altwin == NULL && !win->w_p_pvw
2302 && win->w_buffer->b_p_bt[0] == NUL)
2303 altwin = win;
2304 }
2305
2306 win_goto(win);
2307 }
2308 }
2309
2310 return OK;
2311}
2312
2313/*
2314 * Edit the selected file or help file.
2315 */
2316 static int
2317qf_jump_edit_buffer(
2318 qf_info_T *qi,
2319 qfline_T *qf_ptr,
2320 int forceit,
2321 win_T *oldwin,
2322 int *opened_window,
2323 int *abort)
2324{
2325 int retval = OK;
2326
2327 if (qf_ptr->qf_type == 1)
2328 {
2329 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2330 * set b_p_ro flag). */
2331 if (!can_abandon(curbuf, forceit))
2332 {
2333 no_write_message();
2334 retval = FALSE;
2335 }
2336 else
2337 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2338 ECMD_HIDE + ECMD_SET_HELP,
2339 oldwin == curwin ? curwin : NULL);
2340 }
2341 else
2342 {
2343 int old_qf_curlist = qi->qf_curlist;
2344
2345 retval = buflist_getfile(qf_ptr->qf_fnum,
2346 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
2347 if (qi != &ql_info && !win_valid_any_tab(oldwin))
2348 {
2349 EMSG(_("E924: Current window was closed"));
2350 *abort = TRUE;
2351 *opened_window = FALSE;
2352 }
2353 else if (old_qf_curlist != qi->qf_curlist
2354 || !is_qf_entry_present(qi, qf_ptr))
2355 {
2356 if (qi == &ql_info)
2357 EMSG(_("E925: Current quickfix was changed"));
2358 else
2359 EMSG(_("E926: Current location list was changed"));
2360 *abort = TRUE;
2361 }
2362
2363 if (*abort)
2364 retval = FALSE;
2365 }
2366
2367 return retval;
2368}
2369
2370/*
2371 * Goto the error line in the current file using either line/column number or a
2372 * search pattern.
2373 */
2374 static void
2375qf_jump_goto_line(
2376 linenr_T qf_lnum,
2377 int qf_col,
2378 char_u qf_viscol,
2379 char_u *qf_pattern)
2380{
2381 linenr_T i;
2382 char_u *line;
2383 colnr_T screen_col;
2384 colnr_T char_col;
2385
2386 if (qf_pattern == NULL)
2387 {
2388 /*
2389 * Go to line with error, unless qf_lnum is 0.
2390 */
2391 i = qf_lnum;
2392 if (i > 0)
2393 {
2394 if (i > curbuf->b_ml.ml_line_count)
2395 i = curbuf->b_ml.ml_line_count;
2396 curwin->w_cursor.lnum = i;
2397 }
2398 if (qf_col > 0)
2399 {
2400 curwin->w_cursor.col = qf_col - 1;
2401#ifdef FEAT_VIRTUALEDIT
2402 curwin->w_cursor.coladd = 0;
2403#endif
2404 if (qf_viscol == TRUE)
2405 {
2406 /*
2407 * Check each character from the beginning of the error
2408 * line up to the error column. For each tab character
2409 * found, reduce the error column value by the length of
2410 * a tab character.
2411 */
2412 line = ml_get_curline();
2413 screen_col = 0;
2414 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2415 {
2416 if (*line == NUL)
2417 break;
2418 if (*line++ == '\t')
2419 {
2420 curwin->w_cursor.col -= 7 - (screen_col % 8);
2421 screen_col += 8 - (screen_col % 8);
2422 }
2423 else
2424 ++screen_col;
2425 }
2426 }
2427 check_cursor();
2428 }
2429 else
2430 beginline(BL_WHITE | BL_FIX);
2431 }
2432 else
2433 {
2434 pos_T save_cursor;
2435
2436 /* Move the cursor to the first line in the buffer */
2437 save_cursor = curwin->w_cursor;
2438 curwin->w_cursor.lnum = 0;
2439 if (!do_search(NULL, '/', qf_pattern, (long)1,
2440 SEARCH_KEEP, NULL, NULL))
2441 curwin->w_cursor = save_cursor;
2442 }
2443}
2444
2445/*
2446 * Display quickfix list index and size message
2447 */
2448 static void
2449qf_jump_print_msg(
2450 qf_info_T *qi,
2451 int qf_index,
2452 qfline_T *qf_ptr,
2453 buf_T *old_curbuf,
2454 linenr_T old_lnum)
2455{
2456 linenr_T i;
2457 int len;
2458
2459 /* Update the screen before showing the message, unless the screen
2460 * scrolled up. */
2461 if (!msg_scrolled)
2462 update_topline_redraw();
2463 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2464 qi->qf_lists[qi->qf_curlist].qf_count,
2465 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2466 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2467 /* Add the message, skipping leading whitespace and newlines. */
2468 len = (int)STRLEN(IObuff);
2469 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2470
2471 /* Output the message. Overwrite to avoid scrolling when the 'O'
2472 * flag is present in 'shortmess'; But when not jumping, print the
2473 * whole message. */
2474 i = msg_scroll;
2475 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2476 msg_scroll = TRUE;
2477 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2478 msg_scroll = FALSE;
2479 msg_attr_keep(IObuff, 0, TRUE);
2480 msg_scroll = i;
2481}
2482
2483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 * jump to a quickfix line
2485 * if dir == FORWARD go "errornr" valid entries forward
2486 * if dir == BACKWARD go "errornr" valid entries backward
2487 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2488 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2489 * else if "errornr" is zero, redisplay the same line
2490 * else go to entry "errornr"
2491 */
2492 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002493qf_jump(qf_info_T *qi,
2494 int dir,
2495 int errornr,
2496 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002498 qfline_T *qf_ptr;
2499 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 buf_T *old_curbuf;
2503 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002504 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002505 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002507 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509#ifdef FEAT_FOLDING
2510 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2511#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002512 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002514 if (qi == NULL)
2515 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002516
2517 if (qi->qf_curlist >= qi->qf_listcount
2518 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
2520 EMSG(_(e_quickfix));
2521 return;
2522 }
2523
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002524 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002526 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002528 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002530 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2531 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002533 qf_ptr = old_qf_ptr;
2534 qf_index = old_qf_index;
2535 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537 }
2538 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002539 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002541 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2542 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 /* No need to print the error message if it's visible in the error
2544 * window */
2545 print_message = FALSE;
2546
2547 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002548 * For ":helpgrep" find a help window or open one.
2549 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002550 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002551 if (jump_to_help_window(qi, &opened_window) == FAIL)
2552 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002553
2554 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 * If currently in the quickfix window, find another window to show the
2556 * file in.
2557 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002558 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 /*
2561 * If there is no file specified, we don't know where to go.
2562 * But do advance, otherwise ":cn" gets stuck.
2563 */
2564 if (qf_ptr->qf_fnum == 0)
2565 goto theend;
2566
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002567 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2568 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
2571 /*
2572 * If there is a file name,
2573 * read the wanted file if needed, and check autowrite etc.
2574 */
2575 old_curbuf = curbuf;
2576 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002577
2578 if (qf_ptr->qf_fnum != 0)
2579 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002580 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002581
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002582 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2583 &opened_window, &abort);
2584 if (abort)
2585 {
2586 qi = NULL;
2587 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002588 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002589 }
2590
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002591 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
2593 /* When not switched to another buffer, still need to set pc mark */
2594 if (curbuf == old_curbuf)
2595 setpcmark();
2596
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002597 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2598 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600#ifdef FEAT_FOLDING
2601 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2602 foldOpenCursor();
2603#endif
2604 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002605 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
2607 else
2608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (opened_window)
2610 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002611 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 /*
2614 * Couldn't open file, so put index back where it was. This could
2615 * happen if the file was readonly and we changed something.
2616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 qf_ptr = old_qf_ptr;
2619 qf_index = old_qf_index;
2620 }
2621 }
2622theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002623 if (qi != NULL)
2624 {
2625 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2626 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (p_swb != old_swb && opened_window)
2629 {
2630 /* Restore old 'switchbuf' value, but not when an autocommand or
2631 * modeline has changed the value. */
2632 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002635 swb_flags = old_swb_flags;
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 else
2638 free_string_option(old_swb);
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640}
2641
2642/*
2643 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002644 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 */
2646 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002647qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002649 buf_T *buf;
2650 char_u *fname;
2651 qfline_T *qfp;
2652 int i;
2653 int idx1 = 1;
2654 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002655 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002656 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002657 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002659 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002661 if (eap->cmdidx == CMD_llist)
2662 {
2663 qi = GET_LOC_LIST(curwin);
2664 if (qi == NULL)
2665 {
2666 EMSG(_(e_loclist));
2667 return;
2668 }
2669 }
2670
2671 if (qi->qf_curlist >= qi->qf_listcount
2672 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
2674 EMSG(_(e_quickfix));
2675 return;
2676 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002677 if (*arg == '+')
2678 {
2679 ++arg;
2680 plus = TRUE;
2681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2683 {
2684 EMSG(_(e_trailing));
2685 return;
2686 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002687 if (plus)
2688 {
2689 i = qi->qf_lists[qi->qf_curlist].qf_index;
2690 idx2 = i + idx1;
2691 idx1 = i;
2692 }
2693 else
2694 {
2695 i = qi->qf_lists[qi->qf_curlist].qf_count;
2696 if (idx1 < 0)
2697 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2698 if (idx2 < 0)
2699 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002702 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002704 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2705 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
2707 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2708 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002709 msg_putchar('\n');
2710 if (got_int)
2711 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002712
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002713 fname = NULL;
2714 if (qfp->qf_fnum != 0
2715 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2716 {
2717 fname = buf->b_fname;
2718 if (qfp->qf_type == 1) /* :helpgrep */
2719 fname = gettail(fname);
2720 }
2721 if (fname == NULL)
2722 sprintf((char *)IObuff, "%2d", i);
2723 else
2724 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2725 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002726 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar21020352017-06-13 17:21:04 +02002727 ? HL_ATTR(HLF_QFL) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002728 if (qfp->qf_lnum == 0)
2729 IObuff[0] = NUL;
2730 else if (qfp->qf_col == 0)
2731 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2732 else
2733 sprintf((char *)IObuff, ":%ld col %d",
2734 qfp->qf_lnum, qfp->qf_col);
2735 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2736 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002737 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002738 if (qfp->qf_pattern != NULL)
2739 {
2740 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2741 STRCAT(IObuff, ":");
2742 msg_puts(IObuff);
2743 }
2744 msg_puts((char_u *)" ");
2745
2746 /* Remove newlines and leading whitespace from the text. For an
2747 * unrecognized line keep the indent, the compiler may mark a word
2748 * with ^^^^. */
2749 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2751 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002752 msg_prt_line(IObuff, FALSE);
2753 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002755
2756 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002757 if (qfp == NULL)
2758 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002759 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 ui_breakcheck();
2761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762}
2763
2764/*
2765 * Remove newlines and leading whitespace from an error message.
2766 * Put the result in "buf[bufsize]".
2767 */
2768 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002769qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
2771 int i;
2772 char_u *p = text;
2773
2774 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2775 {
2776 if (*p == '\n')
2777 {
2778 buf[i] = ' ';
2779 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002780 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 break;
2782 }
2783 else
2784 buf[i] = *p++;
2785 }
2786 buf[i] = NUL;
2787}
2788
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002789 static void
2790qf_msg(qf_info_T *qi, int which, char *lead)
2791{
2792 char *title = (char *)qi->qf_lists[which].qf_title;
2793 int count = qi->qf_lists[which].qf_count;
2794 char_u buf[IOSIZE];
2795
2796 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2797 lead,
2798 which + 1,
2799 qi->qf_listcount,
2800 count);
2801
2802 if (title != NULL)
2803 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002804 size_t len = STRLEN(buf);
2805
2806 if (len < 34)
2807 {
2808 vim_memset(buf + len, ' ', 34 - len);
2809 buf[34] = NUL;
2810 }
2811 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002812 }
2813 trunc_string(buf, buf, Columns - 1, IOSIZE);
2814 msg(buf);
2815}
2816
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817/*
2818 * ":colder [count]": Up in the quickfix stack.
2819 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002820 * ":lolder [count]": Up in the location list stack.
2821 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 */
2823 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002824qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002826 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 int count;
2828
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002829 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2830 {
2831 qi = GET_LOC_LIST(curwin);
2832 if (qi == NULL)
2833 {
2834 EMSG(_(e_loclist));
2835 return;
2836 }
2837 }
2838
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (eap->addr_count != 0)
2840 count = eap->line2;
2841 else
2842 count = 1;
2843 while (count--)
2844 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002845 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002847 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
2849 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002852 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
2854 else
2855 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002856 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
2858 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002859 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002861 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002864 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002865 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866}
2867
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002868 void
2869qf_history(exarg_T *eap)
2870{
2871 qf_info_T *qi = &ql_info;
2872 int i;
2873
2874 if (eap->cmdidx == CMD_lhistory)
2875 qi = GET_LOC_LIST(curwin);
2876 if (qi == NULL || (qi->qf_listcount == 0
2877 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2878 MSG(_("No entries"));
2879 else
2880 for (i = 0; i < qi->qf_listcount; ++i)
2881 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2882}
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002885 * Free all the entries in the error list "idx". Note that other information
2886 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 */
2888 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002889qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002891 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002892 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002893 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002894 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002896 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002898 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002899 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002900 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002901 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002902 vim_free(qfp->qf_text);
2903 stop = (qfp == qfpnext);
2904 vim_free(qfp->qf_pattern);
2905 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002906 if (stop)
2907 /* Somehow qf_count may have an incorrect value, set it to 1
2908 * to avoid crashing when it's wrong.
2909 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002910 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002911 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002912 qfl->qf_start = qfpnext;
2913 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002915
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002916 qfl->qf_index = 0;
2917 qfl->qf_start = NULL;
2918 qfl->qf_last = NULL;
2919 qfl->qf_ptr = NULL;
2920 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002921
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002922 qf_clean_dir_stack(&qfl->qf_dir_stack);
2923 qfl->qf_directory = NULL;
2924 qf_clean_dir_stack(&qfl->qf_file_stack);
2925 qfl->qf_currfile = NULL;
2926 qfl->qf_multiline = FALSE;
2927 qfl->qf_multiignore = FALSE;
2928 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929}
2930
2931/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002932 * Free error list "idx". Frees all the entries in the quickfix list,
2933 * associated context information and the title.
2934 */
2935 static void
2936qf_free(qf_info_T *qi, int idx)
2937{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002938 qf_list_T *qfl = &qi->qf_lists[idx];
2939
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002940 qf_free_items(qi, idx);
2941
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002942 vim_free(qfl->qf_title);
2943 qfl->qf_title = NULL;
2944 free_tv(qfl->qf_ctx);
2945 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002946 qfl->qf_id = 0;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002947}
2948
2949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 * qf_mark_adjust: adjust marks
2951 */
2952 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002953qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002954 win_T *wp,
2955 linenr_T line1,
2956 linenr_T line2,
2957 long amount,
2958 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002960 int i;
2961 qfline_T *qfp;
2962 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002963 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002964 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002965 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
Bram Moolenaarc1542742016-07-20 21:44:37 +02002967 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002968 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002969 if (wp != NULL)
2970 {
2971 if (wp->w_llist == NULL)
2972 return;
2973 qi = wp->w_llist;
2974 }
2975
2976 for (idx = 0; idx < qi->qf_listcount; ++idx)
2977 if (qi->qf_lists[idx].qf_count)
2978 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002979 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2980 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 if (qfp->qf_fnum == curbuf->b_fnum)
2982 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002983 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2985 {
2986 if (amount == MAXLNUM)
2987 qfp->qf_cleared = TRUE;
2988 else
2989 qfp->qf_lnum += amount;
2990 }
2991 else if (amount_after && qfp->qf_lnum > line2)
2992 qfp->qf_lnum += amount_after;
2993 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002994
2995 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002996 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997}
2998
2999/*
3000 * Make a nice message out of the error character and the error number:
3001 * char number message
3002 * e or E 0 " error"
3003 * w or W 0 " warning"
3004 * i or I 0 " info"
3005 * 0 0 ""
3006 * other 0 " c"
3007 * e or E n " error n"
3008 * w or W n " warning n"
3009 * i or I n " info n"
3010 * 0 n " error n"
3011 * other n " c n"
3012 * 1 x "" :helpgrep
3013 */
3014 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003015qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 static char_u buf[20];
3018 static char_u cc[3];
3019 char_u *p;
3020
3021 if (c == 'W' || c == 'w')
3022 p = (char_u *)" warning";
3023 else if (c == 'I' || c == 'i')
3024 p = (char_u *)" info";
3025 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3026 p = (char_u *)" error";
3027 else if (c == 0 || c == 1)
3028 p = (char_u *)"";
3029 else
3030 {
3031 cc[0] = ' ';
3032 cc[1] = c;
3033 cc[2] = NUL;
3034 p = cc;
3035 }
3036
3037 if (nr <= 0)
3038 return p;
3039
3040 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3041 return buf;
3042}
3043
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044/*
3045 * ":cwindow": open the quickfix window if we have errors to display,
3046 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003047 * ":lwindow": open the location list window if we have locations to display,
3048 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 */
3050 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003051ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003053 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 win_T *win;
3055
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003056 if (eap->cmdidx == CMD_lwindow)
3057 {
3058 qi = GET_LOC_LIST(curwin);
3059 if (qi == NULL)
3060 return;
3061 }
3062
3063 /* Look for an existing quickfix window. */
3064 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
3066 /*
3067 * If a quickfix window is open but we have no errors to display,
3068 * close the window. If a quickfix window is not open, then open
3069 * it if we have errors; otherwise, leave it closed.
3070 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003071 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003072 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003073 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 {
3075 if (win != NULL)
3076 ex_cclose(eap);
3077 }
3078 else if (win == NULL)
3079 ex_copen(eap);
3080}
3081
3082/*
3083 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003084 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003087ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003089 win_T *win = NULL;
3090 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003092 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3093 {
3094 qi = GET_LOC_LIST(curwin);
3095 if (qi == NULL)
3096 return;
3097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003099 /* Find existing quickfix window and close it. */
3100 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 if (win != NULL)
3102 win_close(win, FALSE);
3103}
3104
3105/*
3106 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003107 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 */
3109 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003110ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003112 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003115 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003116 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003117 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003119 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3120 {
3121 qi = GET_LOC_LIST(curwin);
3122 if (qi == NULL)
3123 {
3124 EMSG(_(e_loclist));
3125 return;
3126 }
3127 }
3128
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 if (eap->addr_count != 0)
3130 height = eap->line2;
3131 else
3132 height = QF_WINHEIGHT;
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#ifdef FEAT_GUI
3136 need_mouse_correct = TRUE;
3137#endif
3138
3139 /*
3140 * Find existing quickfix window, or open a new one.
3141 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003142 win = qf_find_win(qi);
3143
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003144 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003147 if (eap->addr_count != 0)
3148 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003149 if (cmdmod.split & WSP_VERT)
3150 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003151 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003152 win_setwidth(height);
3153 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003154 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003155 win_setheight(height);
3156 }
3157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 else
3159 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00003160 qf_buf = qf_find_buf(qi);
3161
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 /* The current window becomes the previous window afterwards. */
3163 win = curwin;
3164
Bram Moolenaar77642c02012-11-20 17:55:10 +01003165 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3166 && cmdmod.split == 0)
3167 /* Create the new window at the very bottom, except when
3168 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003169 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003170 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003172 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003174 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003176 /*
3177 * For the location list window, create a reference to the
3178 * location list from the window 'win'.
3179 */
3180 curwin->w_llist_ref = win->w_llist;
3181 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003183
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003184 if (oldwin != curwin)
3185 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003186 if (qf_buf != NULL)
3187 /* Use the existing quickfix buffer */
3188 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003189 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003190 else
3191 {
3192 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003193 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003194 /* switch off 'swapfile' */
3195 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3196 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003197 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003198 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003199 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003200#ifdef FEAT_DIFF
3201 curwin->w_p_diff = FALSE;
3202#endif
3203#ifdef FEAT_FOLDING
3204 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3205 OPT_LOCAL);
3206#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003209 /* Only set the height when still in the same tab page and there is no
3210 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003211 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 win_setheight(height);
3213 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3214 if (win_valid(win))
3215 prevwin = win;
3216 }
3217
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003218 qf_set_title_var(qi);
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 /*
3221 * Fill the buffer with the quickfix list.
3222 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003223 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003225 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 curwin->w_cursor.col = 0;
3227 check_cursor();
3228 update_topline(); /* scroll to show the line */
3229}
3230
3231/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003232 * Move the cursor in the quickfix window to "lnum".
3233 */
3234 static void
3235qf_win_goto(win_T *win, linenr_T lnum)
3236{
3237 win_T *old_curwin = curwin;
3238
3239 curwin = win;
3240 curbuf = win->w_buffer;
3241 curwin->w_cursor.lnum = lnum;
3242 curwin->w_cursor.col = 0;
3243#ifdef FEAT_VIRTUALEDIT
3244 curwin->w_cursor.coladd = 0;
3245#endif
3246 curwin->w_curswant = 0;
3247 update_topline(); /* scroll to show the line */
3248 redraw_later(VALID);
3249 curwin->w_redr_status = TRUE; /* update ruler */
3250 curwin = old_curwin;
3251 curbuf = curwin->w_buffer;
3252}
3253
3254/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003255 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003256 */
3257 void
3258ex_cbottom(exarg_T *eap UNUSED)
3259{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003260 qf_info_T *qi = &ql_info;
3261 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003262
Bram Moolenaar537ef082016-07-09 17:56:19 +02003263 if (eap->cmdidx == CMD_lbottom)
3264 {
3265 qi = GET_LOC_LIST(curwin);
3266 if (qi == NULL)
3267 {
3268 EMSG(_(e_loclist));
3269 return;
3270 }
3271 }
3272
3273 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003274 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3275 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3276}
3277
3278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 * Return the number of the current entry (line number in the quickfix
3280 * window).
3281 */
3282 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003283qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003285 qf_info_T *qi = &ql_info;
3286
3287 if (IS_LL_WINDOW(wp))
3288 /* In the location list window, use the referenced location list */
3289 qi = wp->w_llist_ref;
3290
3291 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292}
3293
3294/*
3295 * Update the cursor position in the quickfix window to the current error.
3296 * Return TRUE if there is a quickfix window.
3297 */
3298 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003299qf_win_pos_update(
3300 qf_info_T *qi,
3301 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
3303 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003304 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
3306 /*
3307 * Put the cursor on the current error in the quickfix window, so that
3308 * it's viewable.
3309 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003310 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (win != NULL
3312 && qf_index <= win->w_buffer->b_ml.ml_line_count
3313 && old_qf_index != qf_index)
3314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (qf_index > old_qf_index)
3316 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003317 win->w_redraw_top = old_qf_index;
3318 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 }
3320 else
3321 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003322 win->w_redraw_top = qf_index;
3323 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003325 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327 return win != NULL;
3328}
3329
3330/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003331 * Check whether the given window is displaying the specified quickfix/location
3332 * list buffer
3333 */
3334 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003335is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003336{
3337 /*
3338 * A window displaying the quickfix buffer will have the w_llist_ref field
3339 * set to NULL.
3340 * A window displaying a location list buffer will have the w_llist_ref
3341 * pointing to the location list.
3342 */
3343 if (bt_quickfix(win->w_buffer))
3344 if ((qi == &ql_info && win->w_llist_ref == NULL)
3345 || (qi != &ql_info && win->w_llist_ref == qi))
3346 return TRUE;
3347
3348 return FALSE;
3349}
3350
3351/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003352 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003353 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003354 */
3355 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003356qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003357{
3358 win_T *win;
3359
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003360 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003361 if (is_qf_win(win, qi))
3362 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003363
3364 return win;
3365}
3366
3367/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003368 * Find a quickfix buffer.
3369 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 */
3371 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003372qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003374 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003375 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
Bram Moolenaar9c102382006-05-03 21:26:49 +00003377 FOR_ALL_TAB_WINDOWS(tp, win)
3378 if (is_qf_win(win, qi))
3379 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003380
Bram Moolenaar9c102382006-05-03 21:26:49 +00003381 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382}
3383
3384/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003385 * Update the w:quickfix_title variable in the quickfix/location list window
3386 */
3387 static void
3388qf_update_win_titlevar(qf_info_T *qi)
3389{
3390 win_T *win;
3391 win_T *curwin_save;
3392
3393 if ((win = qf_find_win(qi)) != NULL)
3394 {
3395 curwin_save = curwin;
3396 curwin = win;
3397 qf_set_title_var(qi);
3398 curwin = curwin_save;
3399 }
3400}
3401
3402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 * Find the quickfix buffer. If it exists, update the contents.
3404 */
3405 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003406qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003409 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
3412 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003413 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (buf != NULL)
3415 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003416 linenr_T old_line_count = buf->b_ml.ml_line_count;
3417
3418 if (old_last == NULL)
3419 /* set curwin/curbuf to buf and save a few things */
3420 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaard823fa92016-08-12 16:29:27 +02003422 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003423
Bram Moolenaar864293a2016-06-02 13:40:04 +02003424 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003425 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003426
Bram Moolenaar864293a2016-06-02 13:40:04 +02003427 if (old_last == NULL)
3428 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003429 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003430
3431 /* restore curwin/curbuf and a few other things */
3432 aucmd_restbuf(&aco);
3433 }
3434
3435 /* Only redraw when added lines are visible. This avoids flickering
3436 * when the added lines are not visible. */
3437 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3438 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
3440}
3441
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003442/*
3443 * Set "w:quickfix_title" if "qi" has a title.
3444 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003445 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003446qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003447{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003448 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3449 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003450 qi->qf_lists[qi->qf_curlist].qf_title);
3451}
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453/*
3454 * Fill current buffer with quickfix errors, replacing any previous contents.
3455 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003456 * If "old_last" is not NULL append the items after this one.
3457 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3458 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 */
3460 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003461qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003463 linenr_T lnum;
3464 qfline_T *qfp;
3465 buf_T *errbuf;
3466 int len;
3467 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar864293a2016-06-02 13:40:04 +02003469 if (old_last == NULL)
3470 {
3471 if (buf != curbuf)
3472 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003473 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003474 return;
3475 }
3476
3477 /* delete all existing lines */
3478 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3479 (void)ml_delete((linenr_T)1, FALSE);
3480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
3482 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003483 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
3485 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003486 if (old_last == NULL)
3487 {
3488 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3489 lnum = 0;
3490 }
3491 else
3492 {
3493 qfp = old_last->qf_next;
3494 lnum = buf->b_ml.ml_line_count;
3495 }
3496 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 if (qfp->qf_fnum != 0
3499 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3500 && errbuf->b_fname != NULL)
3501 {
3502 if (qfp->qf_type == 1) /* :helpgrep */
3503 STRCPY(IObuff, gettail(errbuf->b_fname));
3504 else
3505 STRCPY(IObuff, errbuf->b_fname);
3506 len = (int)STRLEN(IObuff);
3507 }
3508 else
3509 len = 0;
3510 IObuff[len++] = '|';
3511
3512 if (qfp->qf_lnum > 0)
3513 {
3514 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3515 len += (int)STRLEN(IObuff + len);
3516
3517 if (qfp->qf_col > 0)
3518 {
3519 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3520 len += (int)STRLEN(IObuff + len);
3521 }
3522
3523 sprintf((char *)IObuff + len, "%s",
3524 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3525 len += (int)STRLEN(IObuff + len);
3526 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003527 else if (qfp->qf_pattern != NULL)
3528 {
3529 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3530 len += (int)STRLEN(IObuff + len);
3531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 IObuff[len++] = '|';
3533 IObuff[len++] = ' ';
3534
3535 /* Remove newlines and leading whitespace from the text.
3536 * For an unrecognized line keep the indent, the compiler may
3537 * mark a word with ^^^^. */
3538 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3539 IObuff + len, IOSIZE - len);
3540
Bram Moolenaar864293a2016-06-02 13:40:04 +02003541 if (ml_append_buf(buf, lnum, IObuff,
3542 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003544 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003546 if (qfp == NULL)
3547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003549
3550 if (old_last == NULL)
3551 /* Delete the empty line which is now at the end */
3552 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554
3555 /* correct cursor position */
3556 check_lnums(TRUE);
3557
Bram Moolenaar864293a2016-06-02 13:40:04 +02003558 if (old_last == NULL)
3559 {
3560 /* Set the 'filetype' to "qf" each time after filling the buffer.
3561 * This resembles reading a file into a buffer, it's more logical when
3562 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003563#ifdef FEAT_AUTOCMD
3564 ++curbuf_lock;
3565#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003566 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3567 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
3569#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003570 keep_filetype = TRUE; /* don't detect 'filetype' */
3571 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003573 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003575 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003576 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003578 /* make sure it will be redrawn */
3579 redraw_curbuf_later(NOT_VALID);
3580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
3582 /* Restore KeyTyped, setting 'filetype' may reset it. */
3583 KeyTyped = old_KeyTyped;
3584}
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003587 * Return TRUE when using ":vimgrep" for ":grep".
3588 */
3589 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003590grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003591{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003592 return ((cmdidx == CMD_grep
3593 || cmdidx == CMD_lgrep
3594 || cmdidx == CMD_grepadd
3595 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003596 && STRCMP("internal",
3597 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3598}
3599
3600/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003601 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 */
3603 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003604ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003606 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003608 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003610 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003611 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003612 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003613#ifdef FEAT_AUTOCMD
3614 char_u *au_name = NULL;
3615
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003616 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3617 if (grep_internal(eap->cmdidx))
3618 {
3619 ex_vimgrep(eap);
3620 return;
3621 }
3622
Bram Moolenaar7c626922005-02-07 22:01:03 +00003623 switch (eap->cmdidx)
3624 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003625 case CMD_make: au_name = (char_u *)"make"; break;
3626 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3627 case CMD_grep: au_name = (char_u *)"grep"; break;
3628 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3629 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3630 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003631 default: break;
3632 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003633 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3634 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003635 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003636# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003637 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003638 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003639# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003640 }
3641#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003642#ifdef FEAT_MBYTE
3643 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
Bram Moolenaara6557602006-02-04 22:43:20 +00003646 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3647 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003648 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003649
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003651 fname = get_mef_name();
3652 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003654 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 /*
3657 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3658 */
3659 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3660 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003661 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 cmd = alloc(len);
3663 if (cmd == NULL)
3664 return;
3665 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3666 (char *)p_shq);
3667 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003668 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 /*
3670 * Output a newline if there's something else than the :make command that
3671 * was typed (in which case the cursor is in column 0).
3672 */
3673 if (msg_col == 0)
3674 msg_didout = FALSE;
3675 msg_start();
3676 MSG_PUTS(":!");
3677 msg_outtrans(cmd); /* show what we are doing */
3678
3679 /* let the shell know if we are redirecting output or not */
3680 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3681
3682#ifdef AMIGA
3683 out_flush();
3684 /* read window status report and redraw before message */
3685 (void)char_avail();
3686#endif
3687
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003688 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003689 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3690 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003691 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003692 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003693 if (wp != NULL)
3694 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003695#ifdef FEAT_AUTOCMD
3696 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003697 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003698 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3699 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003700 if (qi->qf_curlist < qi->qf_listcount)
3701 res = qi->qf_lists[qi->qf_curlist].qf_count;
3702 else
3703 res = 0;
3704 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003705#endif
3706 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003707 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
Bram Moolenaar7c626922005-02-07 22:01:03 +00003709 mch_remove(fname);
3710 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 vim_free(cmd);
3712}
3713
3714/*
3715 * Return the name for the errorfile, in allocated memory.
3716 * Find a new unique name when 'makeef' contains "##".
3717 * Returns NULL for error.
3718 */
3719 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003720get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
3722 char_u *p;
3723 char_u *name;
3724 static int start = -1;
3725 static int off = 0;
3726#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003727 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728#endif
3729
3730 if (*p_mef == NUL)
3731 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003732 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (name == NULL)
3734 EMSG(_(e_notmp));
3735 return name;
3736 }
3737
3738 for (p = p_mef; *p; ++p)
3739 if (p[0] == '#' && p[1] == '#')
3740 break;
3741
3742 if (*p == NUL)
3743 return vim_strsave(p_mef);
3744
3745 /* Keep trying until the name doesn't exist yet. */
3746 for (;;)
3747 {
3748 if (start == -1)
3749 start = mch_get_pid();
3750 else
3751 off += 19;
3752
3753 name = alloc((unsigned)STRLEN(p_mef) + 30);
3754 if (name == NULL)
3755 break;
3756 STRCPY(name, p_mef);
3757 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3758 STRCAT(name, p + 2);
3759 if (mch_getperm(name) < 0
3760#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003761 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 && mch_lstat((char *)name, &sb) < 0
3763#endif
3764 )
3765 break;
3766 vim_free(name);
3767 }
3768 return name;
3769}
3770
3771/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003772 * Returns the number of valid entries in the current quickfix/location list.
3773 */
3774 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003775qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003776{
3777 qf_info_T *qi = &ql_info;
3778 qfline_T *qfp;
3779 int i, sz = 0;
3780 int prev_fnum = 0;
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 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003791 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003792 ++i, qfp = qfp->qf_next)
3793 {
3794 if (qfp->qf_valid)
3795 {
3796 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3797 sz++; /* Count all valid entries */
3798 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3799 {
3800 /* Count the number of files */
3801 sz++;
3802 prev_fnum = qfp->qf_fnum;
3803 }
3804 }
3805 }
3806
3807 return sz;
3808}
3809
3810/*
3811 * Returns the current index of the quickfix/location list.
3812 * Returns 0 if there is an error.
3813 */
3814 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003815qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003816{
3817 qf_info_T *qi = &ql_info;
3818
3819 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3820 {
3821 /* Location list */
3822 qi = GET_LOC_LIST(curwin);
3823 if (qi == NULL)
3824 return 0;
3825 }
3826
3827 return qi->qf_lists[qi->qf_curlist].qf_index;
3828}
3829
3830/*
3831 * Returns the current index in the quickfix/location list (counting only valid
3832 * entries). If no valid entries are in the list, then returns 1.
3833 */
3834 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003835qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003836{
3837 qf_info_T *qi = &ql_info;
3838 qf_list_T *qfl;
3839 qfline_T *qfp;
3840 int i, eidx = 0;
3841 int prev_fnum = 0;
3842
3843 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3844 {
3845 /* Location list */
3846 qi = GET_LOC_LIST(curwin);
3847 if (qi == NULL)
3848 return 1;
3849 }
3850
3851 qfl = &qi->qf_lists[qi->qf_curlist];
3852 qfp = qfl->qf_start;
3853
3854 /* check if the list has valid errors */
3855 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3856 return 1;
3857
3858 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3859 {
3860 if (qfp->qf_valid)
3861 {
3862 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3863 {
3864 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3865 {
3866 /* Count the number of files */
3867 eidx++;
3868 prev_fnum = qfp->qf_fnum;
3869 }
3870 }
3871 else
3872 eidx++;
3873 }
3874 }
3875
3876 return eidx ? eidx : 1;
3877}
3878
3879/*
3880 * Get the 'n'th valid error entry in the quickfix or location list.
3881 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3882 * For :cdo and :ldo returns the 'n'th valid error entry.
3883 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3884 */
3885 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003886qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003887{
3888 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3889 qfline_T *qfp = qfl->qf_start;
3890 int i, eidx;
3891 int prev_fnum = 0;
3892
3893 /* check if the list has valid errors */
3894 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3895 return 1;
3896
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003897 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003898 i++, qfp = qfp->qf_next)
3899 {
3900 if (qfp->qf_valid)
3901 {
3902 if (fdo)
3903 {
3904 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3905 {
3906 /* Count the number of files */
3907 eidx++;
3908 prev_fnum = qfp->qf_fnum;
3909 }
3910 }
3911 else
3912 eidx++;
3913 }
3914
3915 if (eidx == n)
3916 break;
3917 }
3918
3919 if (i <= qfl->qf_count)
3920 return i;
3921 else
3922 return 1;
3923}
3924
3925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003927 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003928 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 */
3930 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003931ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003933 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003934 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003935
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003936 if (eap->cmdidx == CMD_ll
3937 || eap->cmdidx == CMD_lrewind
3938 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003939 || eap->cmdidx == CMD_llast
3940 || eap->cmdidx == CMD_ldo
3941 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003942 {
3943 qi = GET_LOC_LIST(curwin);
3944 if (qi == NULL)
3945 {
3946 EMSG(_(e_loclist));
3947 return;
3948 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003949 }
3950
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003951 if (eap->addr_count > 0)
3952 errornr = (int)eap->line2;
3953 else
3954 {
3955 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3956 errornr = 0;
3957 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3958 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3959 errornr = 1;
3960 else
3961 errornr = 32767;
3962 }
3963
3964 /* For cdo and ldo commands, jump to the nth valid error.
3965 * For cfdo and lfdo commands, jump to the nth valid file entry.
3966 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02003967 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
3968 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003969 errornr = qf_get_nth_valid_entry(qi,
3970 eap->addr_count > 0 ? (int)eap->line1 : 1,
3971 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3972
3973 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974}
3975
3976/*
3977 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003978 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003979 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 */
3981 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003982ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003984 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003985 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003986
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003987 if (eap->cmdidx == CMD_lnext
3988 || eap->cmdidx == CMD_lNext
3989 || eap->cmdidx == CMD_lprevious
3990 || eap->cmdidx == CMD_lnfile
3991 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003992 || eap->cmdidx == CMD_lpfile
3993 || eap->cmdidx == CMD_ldo
3994 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003995 {
3996 qi = GET_LOC_LIST(curwin);
3997 if (qi == NULL)
3998 {
3999 EMSG(_(e_loclist));
4000 return;
4001 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004002 }
4003
Bram Moolenaar55b69262017-08-13 13:42:01 +02004004 if (eap->addr_count > 0
4005 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4006 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004007 errornr = (int)eap->line2;
4008 else
4009 errornr = 1;
4010
4011 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4012 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004014 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4015 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004017 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4018 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 ? BACKWARD_FILE
4020 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004021 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022}
4023
4024/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004025 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004026 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 */
4028 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004029ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004031 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004032 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004033 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004034#ifdef FEAT_AUTOCMD
4035 char_u *au_name = NULL;
4036#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004037
4038 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004039 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004040 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004041
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004042#ifdef FEAT_AUTOCMD
4043 switch (eap->cmdidx)
4044 {
4045 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4046 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4047 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4048 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4049 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4050 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4051 default: break;
4052 }
4053 if (au_name != NULL)
4054 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
4055#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004056#ifdef FEAT_MBYTE
4057 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4058#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004059#ifdef FEAT_BROWSE
4060 if (cmdmod.browse)
4061 {
4062 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4063 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4064 if (browse_file == NULL)
4065 return;
4066 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4067 vim_free(browse_file);
4068 }
4069 else
4070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004072 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004073
4074 /*
4075 * This function is used by the :cfile, :cgetfile and :caddfile
4076 * commands.
4077 * :cfile always creates a new quickfix list and jumps to the
4078 * first error.
4079 * :cgetfile creates a new quickfix list but doesn't jump to the
4080 * first error.
4081 * :caddfile adds to an existing quickfix list. If there is no
4082 * quickfix list then a new list is created.
4083 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004084 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004085 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004086 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004087 && (eap->cmdidx == CMD_cfile
4088 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004089 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004090#ifdef FEAT_AUTOCMD
4091 if (au_name != NULL)
4092 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4093#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004094 if (wp != NULL)
4095 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004096 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004097 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004098
4099 else
4100 {
4101#ifdef FEAT_AUTOCMD
4102 if (au_name != NULL)
4103 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4104#endif
4105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106}
4107
4108/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004109 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004110 * ":vimgrepadd {pattern} file(s)"
4111 * ":lvimgrep {pattern} file(s)"
4112 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004113 */
4114 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004115ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004116{
Bram Moolenaar81695252004-12-29 20:58:21 +00004117 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004118 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004119 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004120 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004121 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004122 char_u *s;
4123 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004124 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004125 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004126#ifdef FEAT_AUTOCMD
4127 qfline_T *cur_qf_start;
4128#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004129 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004130 buf_T *buf;
4131 int duplicate_name = FALSE;
4132 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004133 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004134 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004135 buf_T *first_match_buf = NULL;
4136 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004137 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004138#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4139 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004140#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004141 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004142 int flags = 0;
4143 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004144 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004145 char_u *dirname_start = NULL;
4146 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004147 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004148#ifdef FEAT_AUTOCMD
4149 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004150
4151 switch (eap->cmdidx)
4152 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004153 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4154 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4155 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004156 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004157 case CMD_grep: au_name = (char_u *)"grep"; break;
4158 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4159 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4160 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004161 default: break;
4162 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004163 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4164 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004165 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004166# ifdef FEAT_EVAL
4167 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004168 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004169# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004170 }
4171#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004172
Bram Moolenaar754b5602006-02-09 23:53:20 +00004173 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004174 || eap->cmdidx == CMD_lvimgrep
4175 || eap->cmdidx == CMD_lgrepadd
4176 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004177 {
4178 qi = ll_get_or_alloc_list(curwin);
4179 if (qi == NULL)
4180 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004181 }
4182
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004183 if (eap->addr_count > 0)
4184 tomatch = eap->line2;
4185 else
4186 tomatch = MAXLNUM;
4187
Bram Moolenaar81695252004-12-29 20:58:21 +00004188 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004189 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004190 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004191 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004192 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004193 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004194 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004195 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004196 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004197
4198 if (s != NULL && *s == NUL)
4199 {
4200 /* Pattern is empty, use last search pattern. */
4201 if (last_search_pat() == NULL)
4202 {
4203 EMSG(_(e_noprevre));
4204 goto theend;
4205 }
4206 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4207 }
4208 else
4209 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4210
Bram Moolenaar86b68352004-12-27 21:59:20 +00004211 if (regmatch.regprog == NULL)
4212 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004213 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004214 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004215
4216 p = skipwhite(p);
4217 if (*p == NUL)
4218 {
4219 EMSG(_("E683: File name missing or invalid pattern"));
4220 goto theend;
4221 }
4222
Bram Moolenaar55b69262017-08-13 13:42:01 +02004223 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4224 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004225 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004226 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004227 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004228
4229 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004230 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004231 goto theend;
4232 if (fcount == 0)
4233 {
4234 EMSG(_(e_nomatch));
4235 goto theend;
4236 }
4237
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004238 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4239 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004240 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004241 {
4242 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004243 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004244 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004245
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004246 /* Remember the current directory, because a BufRead autocommand that does
4247 * ":lcd %:p:h" changes the meaning of short path names. */
4248 mch_dirname(dirname_start, MAXPATHL);
4249
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004250#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004251 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004252 * changing the current quickfix list. */
4253 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4254#endif
4255
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004256 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004257 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004258 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004259 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004260 if (time(NULL) > seconds)
4261 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004262 /* Display the file name every second or so, show the user we are
4263 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004264 seconds = time(NULL);
4265 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004266 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004267 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004268 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004269 else
4270 {
4271 msg_outtrans(p);
4272 vim_free(p);
4273 }
4274 msg_clr_eos();
4275 msg_didout = FALSE; /* overwrite this message */
4276 msg_nowait = TRUE; /* don't wait for this message */
4277 msg_col = 0;
4278 out_flush();
4279 }
4280
Bram Moolenaar81695252004-12-29 20:58:21 +00004281 buf = buflist_findname_exp(fnames[fi]);
4282 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4283 {
4284 /* Remember that a buffer with this name already exists. */
4285 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004286 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004287 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004288
4289#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4290 /* Don't do Filetype autocommands to avoid loading syntax and
4291 * indent scripts, a great speed improvement. */
4292 save_ei = au_event_disable(",Filetype");
4293#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004294 /* Don't use modelines here, it's useless. */
4295 save_mls = p_mls;
4296 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004297
4298 /* Load file into a buffer, so that 'fileencoding' is detected,
4299 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004300 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004301
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004302 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004303#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4304 au_event_restore(save_ei);
4305#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004306 }
4307 else
4308 /* Use existing, loaded buffer. */
4309 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004310
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004311#ifdef FEAT_AUTOCMD
4312 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4313 {
4314 int idx;
4315
4316 /* Autocommands changed the quickfix list. Find the one we were
4317 * using and restore it. */
4318 for (idx = 0; idx < LISTCOUNT; ++idx)
4319 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4320 {
4321 qi->qf_curlist = idx;
4322 break;
4323 }
4324 if (idx == LISTCOUNT)
4325 {
4326 /* List cannot be found, create a new one. */
4327 qf_new_list(qi, *eap->cmdlinep);
4328 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4329 }
4330 }
4331#endif
4332
Bram Moolenaar81695252004-12-29 20:58:21 +00004333 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004334 {
4335 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004336 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004337 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004338 else
4339 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004340 /* Try for a match in all lines of the buffer.
4341 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004342 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004343 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4344 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004345 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004346 col = 0;
4347 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004348 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004349 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004350 /* Pass the buffer number so that it gets used even for a
4351 * dummy buffer, unless duplicate_name is set, then the
4352 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004353 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004354 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004355 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004356 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004357 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004358 ml_get_buf(buf,
4359 regmatch.startpos[0].lnum + lnum, FALSE),
4360 regmatch.startpos[0].lnum + lnum,
4361 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004362 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004363 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004364 0, /* nr */
4365 0, /* type */
4366 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004367 ) == FAIL)
4368 {
4369 got_int = TRUE;
4370 break;
4371 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004372 found_match = TRUE;
4373 if (--tomatch == 0)
4374 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004375 if ((flags & VGR_GLOBAL) == 0
4376 || regmatch.endpos[0].lnum > 0)
4377 break;
4378 col = regmatch.endpos[0].col
4379 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004380 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004381 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004382 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004383 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004384 if (got_int)
4385 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004386 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004387#ifdef FEAT_AUTOCMD
4388 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4389#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004390
4391 if (using_dummy)
4392 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004393 if (found_match && first_match_buf == NULL)
4394 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004395 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004396 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004397 /* Never keep a dummy buffer if there is another buffer
4398 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004399 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004400 buf = NULL;
4401 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004402 else if (!cmdmod.hide
4403 || buf->b_p_bh[0] == 'u' /* "unload" */
4404 || buf->b_p_bh[0] == 'w' /* "wipe" */
4405 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004406 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004407 /* When no match was found we don't need to remember the
4408 * buffer, wipe it out. If there was a match and it
4409 * wasn't the first one or we won't jump there: only
4410 * unload the buffer.
4411 * Ignore 'hidden' here, because it may lead to having too
4412 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004413 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004414 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004415 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004416 buf = NULL;
4417 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004418 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004419 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004420 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004421 /* Keeping the buffer, remove the dummy flag. */
4422 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004423 buf = NULL;
4424 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004425 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004426
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004427 if (buf != NULL)
4428 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004429 /* Keeping the buffer, remove the dummy flag. */
4430 buf->b_flags &= ~BF_DUMMY;
4431
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004432 /* If the buffer is still loaded we need to use the
4433 * directory we jumped to below. */
4434 if (buf == first_match_buf
4435 && target_dir == NULL
4436 && STRCMP(dirname_start, dirname_now) != 0)
4437 target_dir = vim_strsave(dirname_now);
4438
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004439 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004440 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004441 * need to be done (again). But not the window-local
4442 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004443 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004444#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004445 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4446 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004447#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004448 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004449 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004450 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004451 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004452 }
4453 }
4454
4455 FreeWild(fcount, fnames);
4456
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004457 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4458 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4459 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004460
Bram Moolenaar864293a2016-06-02 13:40:04 +02004461 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004462
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004463#ifdef FEAT_AUTOCMD
4464 if (au_name != NULL)
4465 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4466 curbuf->b_fname, TRUE, curbuf);
4467#endif
4468
Bram Moolenaar86b68352004-12-27 21:59:20 +00004469 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004470 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004471 {
4472 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004473 {
4474 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004475 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004476 if (buf != curbuf)
4477 /* If we jumped to another buffer redrawing will already be
4478 * taken care of. */
4479 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004480
4481 /* Jump to the directory used after loading the buffer. */
4482 if (curbuf == first_match_buf && target_dir != NULL)
4483 {
4484 exarg_T ea;
4485
4486 ea.arg = target_dir;
4487 ea.cmdidx = CMD_lcd;
4488 ex_cd(&ea);
4489 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004490 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004491 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004492 else
4493 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004494
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004495 /* If we loaded a dummy buffer into the current window, the autocommands
4496 * may have messed up things, need to redraw and recompute folds. */
4497 if (redraw_for_dummy)
4498 {
4499#ifdef FEAT_FOLDING
4500 foldUpdateAll(curwin);
4501#else
4502 redraw_later(NOT_VALID);
4503#endif
4504 }
4505
Bram Moolenaar86b68352004-12-27 21:59:20 +00004506theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004507 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004508 vim_free(dirname_now);
4509 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004510 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004511 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004512}
4513
4514/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004515 * Restore current working directory to "dirname_start" if they differ, taking
4516 * into account whether it is set locally or globally.
4517 */
4518 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004519restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004520{
4521 char_u *dirname_now = alloc(MAXPATHL);
4522
4523 if (NULL != dirname_now)
4524 {
4525 mch_dirname(dirname_now, MAXPATHL);
4526 if (STRCMP(dirname_start, dirname_now) != 0)
4527 {
4528 /* If the directory has changed, change it back by building up an
4529 * appropriate ex command and executing it. */
4530 exarg_T ea;
4531
4532 ea.arg = dirname_start;
4533 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4534 ex_cd(&ea);
4535 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004536 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004537 }
4538}
4539
4540/*
4541 * Load file "fname" into a dummy buffer and return the buffer pointer,
4542 * placing the directory resulting from the buffer load into the
4543 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4544 * prior to calling this function. Restores directory to "dirname_start" prior
4545 * to returning, if autocmds or the 'autochdir' option have changed it.
4546 *
4547 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4548 * or wipe_dummy_buffer() later!
4549 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004550 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004551 */
4552 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004553load_dummy_buffer(
4554 char_u *fname,
4555 char_u *dirname_start, /* in: old directory */
4556 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004557{
4558 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004559 bufref_T newbufref;
4560 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004561 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004562 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004563
4564 /* Allocate a buffer without putting it in the buffer list. */
4565 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4566 if (newbuf == NULL)
4567 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004568 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004569
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004570 /* Init the options. */
4571 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4572
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004573 /* need to open the memfile before putting the buffer in a window */
4574 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004575 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004576 /* set curwin/curbuf to buf and save a few things */
4577 aucmd_prepbuf(&aco, newbuf);
4578
4579 /* Need to set the filename for autocommands. */
4580 (void)setfname(curbuf, fname, NULL, FALSE);
4581
Bram Moolenaar81695252004-12-29 20:58:21 +00004582 /* Create swap file now to avoid the ATTENTION message. */
4583 check_need_swap(TRUE);
4584
4585 /* Remove the "dummy" flag, otherwise autocommands may not
4586 * work. */
4587 curbuf->b_flags &= ~BF_DUMMY;
4588
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004589 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004590 if (readfile(fname, NULL,
4591 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4592 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004593 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004594 && !(curbuf->b_flags & BF_NEW))
4595 {
4596 failed = FALSE;
4597 if (curbuf != newbuf)
4598 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004599 /* Bloody autocommands changed the buffer! Can happen when
4600 * using netrw and editing a remote file. Use the current
4601 * buffer instead, delete the dummy one after restoring the
4602 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004603 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004604 newbuf = curbuf;
4605 }
4606 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004607
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004608 /* restore curwin/curbuf and a few other things */
4609 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004610 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4611 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004612
4613 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4614 * skip it. */
4615 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004616 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004617
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004618 /*
4619 * When autocommands/'autochdir' option changed directory: go back.
4620 * Let the caller know what the resulting dir was first, in case it is
4621 * important.
4622 */
4623 mch_dirname(resulting_dir, MAXPATHL);
4624 restore_start_dir(dirname_start);
4625
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004626 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004627 return NULL;
4628 if (failed)
4629 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004630 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004631 return NULL;
4632 }
4633 return newbuf;
4634}
4635
4636/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004637 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4638 * directory to "dirname_start" prior to returning, if autocmds or the
4639 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004640 */
4641 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004642wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004643{
4644 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004645 {
4646#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4647 cleanup_T cs;
4648
4649 /* Reset the error/interrupt/exception state here so that aborting()
4650 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4651 * work when got_int is set. */
4652 enter_cleanup(&cs);
4653#endif
4654
Bram Moolenaar81695252004-12-29 20:58:21 +00004655 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004656
4657#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4658 /* Restore the error/interrupt/exception state if not discarded by a
4659 * new aborting error, interrupt, or uncaught exception. */
4660 leave_cleanup(&cs);
4661#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004662 /* When autocommands/'autochdir' option changed directory: go back. */
4663 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004664 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004665}
4666
4667/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004668 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4669 * directory to "dirname_start" prior to returning, if autocmds or the
4670 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004671 */
4672 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004673unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004674{
4675 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004676 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004677 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004678
4679 /* When autocommands/'autochdir' option changed directory: go back. */
4680 restore_start_dir(dirname_start);
4681 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004682}
4683
Bram Moolenaar05159a02005-02-26 23:04:13 +00004684#if defined(FEAT_EVAL) || defined(PROTO)
4685/*
4686 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004687 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004688 */
4689 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004690get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004691{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004692 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004693 dict_T *dict;
4694 char_u buf[2];
4695 qfline_T *qfp;
4696 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004697 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004698
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004699 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004700 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004701 qi = &ql_info;
4702 if (wp != NULL)
4703 {
4704 qi = GET_LOC_LIST(wp);
4705 if (qi == NULL)
4706 return FAIL;
4707 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004708 }
4709
Bram Moolenaard823fa92016-08-12 16:29:27 +02004710 if (qf_idx == -1)
4711 qf_idx = qi->qf_curlist;
4712
4713 if (qf_idx >= qi->qf_listcount
4714 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004715 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004716
Bram Moolenaard823fa92016-08-12 16:29:27 +02004717 qfp = qi->qf_lists[qf_idx].qf_start;
4718 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004719 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004720 /* Handle entries with a non-existing buffer number. */
4721 bufnum = qfp->qf_fnum;
4722 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4723 bufnum = 0;
4724
Bram Moolenaar05159a02005-02-26 23:04:13 +00004725 if ((dict = dict_alloc()) == NULL)
4726 return FAIL;
4727 if (list_append_dict(list, dict) == FAIL)
4728 return FAIL;
4729
4730 buf[0] = qfp->qf_type;
4731 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004732 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004733 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4734 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4735 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4736 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004737 || dict_add_nr_str(dict, "pattern", 0L,
4738 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4739 || dict_add_nr_str(dict, "text", 0L,
4740 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004741 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4742 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4743 return FAIL;
4744
4745 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004746 if (qfp == NULL)
4747 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004748 }
4749 return OK;
4750}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004751
4752/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004753 * Flags used by getqflist()/getloclist() to determine which fields to return.
4754 */
4755enum {
4756 QF_GETLIST_NONE = 0x0,
4757 QF_GETLIST_TITLE = 0x1,
4758 QF_GETLIST_ITEMS = 0x2,
4759 QF_GETLIST_NR = 0x4,
4760 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004761 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004762 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004763 QF_GETLIST_IDX = 0x40,
4764 QF_GETLIST_SIZE = 0x80,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004765 QF_GETLIST_ALL = 0xFF
4766};
4767
4768/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004769 * Parse text from 'di' and return the quickfix list items
4770 */
4771 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004772qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004773{
4774 int status = FAIL;
4775 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004776 char_u *errorformat = p_efm;
4777 dictitem_T *efm_di;
4778 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004779
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004780 /* Only a List value is supported */
4781 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004782 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004783 /* If errorformat is supplied then use it, otherwise use the 'efm'
4784 * option setting
4785 */
4786 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4787 {
4788 if (efm_di->di_tv.v_type != VAR_STRING ||
4789 efm_di->di_tv.vval.v_string == NULL)
4790 return FAIL;
4791 errorformat = efm_di->di_tv.vval.v_string;
4792 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004793
Bram Moolenaar36538222017-09-02 19:51:44 +02004794 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004795 if (l == NULL)
4796 return FAIL;
4797
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004798 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4799 if (qi != NULL)
4800 {
4801 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4802 qi->qf_refcount++;
4803
Bram Moolenaar36538222017-09-02 19:51:44 +02004804 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004805 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4806 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004807 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004808 qf_free(qi, 0);
4809 }
4810 free(qi);
4811 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004812 dict_add_list(retdict, "items", l);
4813 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004814 }
4815
4816 return status;
4817}
4818
4819/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004820 * Return the quickfix/location list number with the given identifier.
4821 * Returns -1 if list is not found.
4822 */
4823 static int
4824qf_id2nr(qf_info_T *qi, int_u qfid)
4825{
4826 int qf_idx;
4827
4828 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4829 if (qi->qf_lists[qf_idx].qf_id == qfid)
4830 return qf_idx;
4831 return -1;
4832}
4833
4834/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004835 * Return quickfix/location list details (title) as a
4836 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4837 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004838 */
4839 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004840qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004841{
4842 qf_info_T *qi = &ql_info;
4843 int status = OK;
4844 int qf_idx;
4845 dictitem_T *di;
4846 int flags = QF_GETLIST_NONE;
4847
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004848 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004849 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004850
Bram Moolenaard823fa92016-08-12 16:29:27 +02004851 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004852 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004853
4854 /* List is not present or is empty */
4855 if (qi == NULL || qi->qf_listcount == 0)
4856 {
4857 /* If querying for the size of the list, return 0 */
4858 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4859 && (di->di_tv.v_type == VAR_STRING)
4860 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4861 return dict_add_nr_str(retdict, "nr", 0, NULL);
4862 return FAIL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004863 }
4864
4865 qf_idx = qi->qf_curlist; /* default is the current list */
4866 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4867 {
4868 /* Use the specified quickfix/location list */
4869 if (di->di_tv.v_type == VAR_NUMBER)
4870 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004871 /* for zero use the current list */
4872 if (di->di_tv.vval.v_number != 0)
4873 {
4874 qf_idx = di->di_tv.vval.v_number - 1;
4875 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4876 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004877 }
Bram Moolenaar55b69262017-08-13 13:42:01 +02004878 }
4879 else if ((di->di_tv.v_type == VAR_STRING)
4880 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004881 /* Get the last quickfix list number */
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004882 qf_idx = qi->qf_listcount - 1;
4883 else
4884 return FAIL;
4885 flags |= QF_GETLIST_NR;
4886 }
4887
4888 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
4889 {
4890 /* Look for a list with the specified id */
4891 if (di->di_tv.v_type == VAR_NUMBER)
4892 {
4893 /* For zero, use the current list or the list specifed by 'nr' */
4894 if (di->di_tv.vval.v_number != 0)
4895 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004896 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
4897 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004898 return FAIL; /* List not found */
4899 }
4900 flags |= QF_GETLIST_ID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004901 }
4902 else
4903 return FAIL;
4904 }
4905
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004906 if (dict_find(what, (char_u *)"all", -1) != NULL)
4907 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004908
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004909 if (dict_find(what, (char_u *)"title", -1) != NULL)
4910 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004911
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004912 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4913 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004914
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004915 if (dict_find(what, (char_u *)"context", -1) != NULL)
4916 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004917
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004918 if (dict_find(what, (char_u *)"items", -1) != NULL)
4919 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004920
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004921 if (dict_find(what, (char_u *)"idx", -1) != NULL)
4922 flags |= QF_GETLIST_IDX;
4923
4924 if (dict_find(what, (char_u *)"size", -1) != NULL)
4925 flags |= QF_GETLIST_SIZE;
4926
Bram Moolenaard823fa92016-08-12 16:29:27 +02004927 if (flags & QF_GETLIST_TITLE)
4928 {
4929 char_u *t;
4930 t = qi->qf_lists[qf_idx].qf_title;
4931 if (t == NULL)
4932 t = (char_u *)"";
4933 status = dict_add_nr_str(retdict, "title", 0L, t);
4934 }
4935 if ((status == OK) && (flags & QF_GETLIST_NR))
4936 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4937 if ((status == OK) && (flags & QF_GETLIST_WINID))
4938 {
4939 win_T *win;
4940 win = qf_find_win(qi);
4941 if (win != NULL)
4942 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4943 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004944 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4945 {
4946 list_T *l = list_alloc();
4947 if (l != NULL)
4948 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004949 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004950 dict_add_list(retdict, "items", l);
4951 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004952 else
4953 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004954 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004955
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004956 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4957 {
4958 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4959 {
4960 di = dictitem_alloc((char_u *)"context");
4961 if (di != NULL)
4962 {
4963 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004964 status = dict_add(retdict, di);
4965 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004966 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004967 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004968 else
4969 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004970 }
4971 else
4972 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4973 }
4974
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004975 if ((status == OK) && (flags & QF_GETLIST_ID))
4976 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
4977 NULL);
4978
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004979 if ((status == OK) && (flags & QF_GETLIST_IDX))
4980 {
4981 int idx = qi->qf_lists[qf_idx].qf_index;
4982 if (qi->qf_lists[qf_idx].qf_count == 0)
4983 /* For empty lists, qf_index is set to 1 */
4984 idx = 0;
4985 status = dict_add_nr_str(retdict, "idx", idx, NULL);
4986 }
4987
4988 if ((status == OK) && (flags & QF_GETLIST_SIZE))
4989 status = dict_add_nr_str(retdict, "size",
4990 qi->qf_lists[qf_idx].qf_count, NULL);
4991
Bram Moolenaard823fa92016-08-12 16:29:27 +02004992 return status;
4993}
4994
4995/*
4996 * Add list of entries to quickfix/location list. Each list entry is
4997 * a dictionary with item information.
4998 */
4999 static int
5000qf_add_entries(
5001 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005002 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005003 list_T *list,
5004 char_u *title,
5005 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005006{
5007 listitem_T *li;
5008 dict_T *d;
5009 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005010 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005011 long lnum;
5012 int col, nr;
5013 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005014 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005015 int valid, status;
5016 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005017 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005018
Bram Moolenaara3921f42017-06-04 15:30:34 +02005019 if (action == ' ' || qf_idx == qi->qf_listcount)
5020 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005021 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005022 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005023 qf_idx = qi->qf_curlist;
5024 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005025 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005026 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005027 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005028 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005029 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005030 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005031 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005032 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005033
5034 for (li = list->lv_first; li != NULL; li = li->li_next)
5035 {
5036 if (li->li_tv.v_type != VAR_DICT)
5037 continue; /* Skip non-dict items */
5038
5039 d = li->li_tv.vval.v_dict;
5040 if (d == NULL)
5041 continue;
5042
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005043 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005044 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5045 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5046 col = (int)get_dict_number(d, (char_u *)"col");
5047 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5048 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005049 type = get_dict_string(d, (char_u *)"type", TRUE);
5050 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5051 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005052 if (text == NULL)
5053 text = vim_strsave((char_u *)"");
5054
5055 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005056 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005057 valid = FALSE;
5058
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005059 /* Mark entries with non-existing buffer number as not valid. Give the
5060 * error message only once. */
5061 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5062 {
5063 if (!did_bufnr_emsg)
5064 {
5065 did_bufnr_emsg = TRUE;
5066 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5067 }
5068 valid = FALSE;
5069 bufnum = 0;
5070 }
5071
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005072 /* If the 'valid' field is present it overrules the detected value. */
5073 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5074 valid = (int)get_dict_number(d, (char_u *)"valid");
5075
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005076 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005077 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005078 NULL, /* dir */
5079 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005080 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005081 text,
5082 lnum,
5083 col,
5084 vcol, /* vis_col */
5085 pattern, /* search pattern */
5086 nr,
5087 type == NULL ? NUL : *type,
5088 valid);
5089
5090 vim_free(filename);
5091 vim_free(pattern);
5092 vim_free(text);
5093 vim_free(type);
5094
5095 if (status == FAIL)
5096 {
5097 retval = FAIL;
5098 break;
5099 }
5100 }
5101
Bram Moolenaara3921f42017-06-04 15:30:34 +02005102 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005103 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005104 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005105 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005106 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005107 if (action != 'a')
5108 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005109 qi->qf_lists[qf_idx].qf_ptr =
5110 qi->qf_lists[qf_idx].qf_start;
5111 if (qi->qf_lists[qf_idx].qf_count > 0)
5112 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005113 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005114
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005115 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005116 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005117
5118 return retval;
5119}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005120
5121 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005122qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005123{
5124 dictitem_T *di;
5125 int retval = FAIL;
5126 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005127 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005128 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005129
5130 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5131 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005132
5133 qf_idx = qi->qf_curlist; /* default is the current list */
5134 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5135 {
5136 /* Use the specified quickfix/location list */
5137 if (di->di_tv.v_type == VAR_NUMBER)
5138 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005139 /* for zero use the current list */
5140 if (di->di_tv.vval.v_number != 0)
5141 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005142
Bram Moolenaar55b69262017-08-13 13:42:01 +02005143 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5144 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005145 /*
5146 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005147 * non-available list and add the new list at the end of the
5148 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005149 */
5150 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005151 qf_idx = qi->qf_listcount - 1;
5152 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005153 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005154 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005155 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005156 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005157 }
5158 else if (di->di_tv.v_type == VAR_STRING
5159 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005160 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005161 if (qi->qf_listcount > 0)
5162 qf_idx = qi->qf_listcount - 1;
5163 else if (newlist)
5164 qf_idx = 0;
5165 else
5166 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005167 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005168 else
5169 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005170 }
5171
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005172 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5173 {
5174 /* Use the quickfix/location list with the specified id */
5175 if (di->di_tv.v_type == VAR_NUMBER)
5176 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005177 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5178 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005179 return FAIL; /* List not found */
5180 }
5181 else
5182 return FAIL;
5183 }
5184
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005185 if (newlist)
5186 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005187 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005188 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005189 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005190 }
5191
5192 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5193 {
5194 if (di->di_tv.v_type == VAR_STRING)
5195 {
5196 vim_free(qi->qf_lists[qf_idx].qf_title);
5197 qi->qf_lists[qf_idx].qf_title =
5198 get_dict_string(what, (char_u *)"title", TRUE);
5199 if (qf_idx == qi->qf_curlist)
5200 qf_update_win_titlevar(qi);
5201 retval = OK;
5202 }
5203 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005204
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005205 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5206 {
5207 if (di->di_tv.v_type == VAR_LIST)
5208 {
5209 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5210
5211 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5212 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005213 if (action == 'r')
5214 {
5215 /*
5216 * When replacing the quickfix list entries using
5217 * qf_add_entries(), the title is set with a ':' prefix.
5218 * Restore the title with the saved title.
5219 */
5220 vim_free(qi->qf_lists[qf_idx].qf_title);
5221 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5222 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005223 vim_free(title_save);
5224 }
5225 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005226
Bram Moolenaar36538222017-09-02 19:51:44 +02005227 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5228 {
5229 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5230 return FAIL;
5231 errorformat = di->di_tv.vval.v_string;
5232 }
5233
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005234 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005235 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005236 /* Only a List value is supported */
5237 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005238 {
5239 if (action == 'r')
5240 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005241 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005242 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5243 retval = OK;
5244 }
5245 else
5246 return FAIL;
5247 }
5248
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005249 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5250 {
5251 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005252
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005253 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005254 ctx = alloc_tv();
5255 if (ctx != NULL)
5256 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005257 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005258 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005259 }
5260
Bram Moolenaard823fa92016-08-12 16:29:27 +02005261 return retval;
5262}
5263
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005264/*
5265 * Find the non-location list window with the specified location list.
5266 */
5267 static win_T *
5268find_win_with_ll(qf_info_T *qi)
5269{
5270 win_T *wp = NULL;
5271
5272 FOR_ALL_WINDOWS(wp)
5273 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5274 return wp;
5275
5276 return NULL;
5277}
5278
5279/*
5280 * Free the entire quickfix/location list stack.
5281 * If the quickfix/location list window is open, then clear it.
5282 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005283 static void
5284qf_free_stack(win_T *wp, qf_info_T *qi)
5285{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005286 win_T *qfwin = qf_find_win(qi);
5287 win_T *llwin = NULL;
5288 win_T *orig_wp = wp;
5289
5290 if (qfwin != NULL)
5291 {
5292 /* If the quickfix/location list window is open, then clear it */
5293 if (qi->qf_curlist < qi->qf_listcount)
5294 qf_free(qi, qi->qf_curlist);
5295 qf_update_buffer(qi, NULL);
5296 }
5297
5298 if (wp != NULL && IS_LL_WINDOW(wp))
5299 {
5300 /* If in the location list window, then use the non-location list
5301 * window with this location list (if present)
5302 */
5303 llwin = find_win_with_ll(qi);
5304 if (llwin != NULL)
5305 wp = llwin;
5306 }
5307
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005308 qf_free_all(wp);
5309 if (wp == NULL)
5310 {
5311 /* quickfix list */
5312 qi->qf_curlist = 0;
5313 qi->qf_listcount = 0;
5314 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005315 else if (IS_LL_WINDOW(orig_wp))
5316 {
5317 /* If the location list window is open, then create a new empty
5318 * location list */
5319 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005320
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005321 /* first free the list reference in the location list window */
5322 ll_free_all(&orig_wp->w_llist_ref);
5323
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005324 orig_wp->w_llist_ref = new_ll;
5325 if (llwin != NULL)
5326 {
5327 llwin->w_llist = new_ll;
5328 new_ll->qf_refcount++;
5329 }
5330 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005331}
5332
Bram Moolenaard823fa92016-08-12 16:29:27 +02005333/*
5334 * Populate the quickfix list with the items supplied in the list
5335 * of dictionaries. "title" will be copied to w:quickfix_title.
5336 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5337 */
5338 int
5339set_errorlist(
5340 win_T *wp,
5341 list_T *list,
5342 int action,
5343 char_u *title,
5344 dict_T *what)
5345{
5346 qf_info_T *qi = &ql_info;
5347 int retval = OK;
5348
5349 if (wp != NULL)
5350 {
5351 qi = ll_get_or_alloc_list(wp);
5352 if (qi == NULL)
5353 return FAIL;
5354 }
5355
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005356 if (action == 'f')
5357 {
5358 /* Free the entire quickfix or location list stack */
5359 qf_free_stack(wp, qi);
5360 }
5361 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005362 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005363 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005364 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005365
5366 return retval;
5367}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005368
5369 static int
5370mark_quickfix_ctx(qf_info_T *qi, int copyID)
5371{
5372 int i;
5373 int abort = FALSE;
5374 typval_T *ctx;
5375
5376 for (i = 0; i < LISTCOUNT && !abort; ++i)
5377 {
5378 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005379 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5380 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005381 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5382 }
5383
5384 return abort;
5385}
5386
5387/*
5388 * Mark the context of the quickfix list and the location lists (if present) as
5389 * "in use". So that garabage collection doesn't free the context.
5390 */
5391 int
5392set_ref_in_quickfix(int copyID)
5393{
5394 int abort = FALSE;
5395 tabpage_T *tp;
5396 win_T *win;
5397
5398 abort = mark_quickfix_ctx(&ql_info, copyID);
5399 if (abort)
5400 return abort;
5401
5402 FOR_ALL_TAB_WINDOWS(tp, win)
5403 {
5404 if (win->w_llist != NULL)
5405 {
5406 abort = mark_quickfix_ctx(win->w_llist, copyID);
5407 if (abort)
5408 return abort;
5409 }
5410 }
5411
5412 return abort;
5413}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005414#endif
5415
Bram Moolenaar81695252004-12-29 20:58:21 +00005416/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005417 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005418 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005419 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005420 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005421 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005422 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005423 */
5424 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005425ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005426{
5427 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005428 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005429#ifdef FEAT_AUTOCMD
5430 char_u *au_name = NULL;
5431#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005432
Bram Moolenaardb552d602006-03-23 22:59:57 +00005433 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5434 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005435 {
5436 qi = ll_get_or_alloc_list(curwin);
5437 if (qi == NULL)
5438 return;
5439 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005440
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005441#ifdef FEAT_AUTOCMD
5442 switch (eap->cmdidx)
5443 {
5444 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5445 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5446 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5447 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5448 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5449 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5450 default: break;
5451 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005452 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5453 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005454 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005455# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005456 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005457 return;
5458# endif
5459 }
5460#endif
5461
Bram Moolenaar86b68352004-12-27 21:59:20 +00005462 if (*eap->arg == NUL)
5463 buf = curbuf;
5464 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5465 buf = buflist_findnr(atoi((char *)eap->arg));
5466 if (buf == NULL)
5467 EMSG(_(e_invarg));
5468 else if (buf->b_ml.ml_mfp == NULL)
5469 EMSG(_("E681: Buffer is not loaded"));
5470 else
5471 {
5472 if (eap->addr_count == 0)
5473 {
5474 eap->line1 = 1;
5475 eap->line2 = buf->b_ml.ml_line_count;
5476 }
5477 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5478 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5479 EMSG(_(e_invrange));
5480 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005481 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005482 char_u *qf_title = *eap->cmdlinep;
5483
5484 if (buf->b_sfname)
5485 {
5486 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5487 (char *)qf_title, (char *)buf->b_sfname);
5488 qf_title = IObuff;
5489 }
5490
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005491 if (qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005492 (eap->cmdidx != CMD_caddbuffer
5493 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005494 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005495 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005496 {
5497#ifdef FEAT_AUTOCMD
5498 if (au_name != NULL)
5499 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5500 curbuf->b_fname, TRUE, curbuf);
5501#endif
5502 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5503 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5504 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005505 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005506 }
5507}
5508
Bram Moolenaar1e015462005-09-25 22:16:38 +00005509#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005510/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005511 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5512 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005513 */
5514 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005515ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005516{
5517 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005518 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005519#ifdef FEAT_AUTOCMD
5520 char_u *au_name = NULL;
5521#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005522
Bram Moolenaardb552d602006-03-23 22:59:57 +00005523 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5524 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005525 {
5526 qi = ll_get_or_alloc_list(curwin);
5527 if (qi == NULL)
5528 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005529 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005530
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005531#ifdef FEAT_AUTOCMD
5532 switch (eap->cmdidx)
5533 {
5534 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5535 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5536 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5537 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5538 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5539 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5540 default: break;
5541 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005542 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5543 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005544 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005545# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005546 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005547 return;
5548# endif
5549 }
5550#endif
5551
Bram Moolenaar4770d092006-01-12 23:22:24 +00005552 /* Evaluate the expression. When the result is a string or a list we can
5553 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005554 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005555 if (tv != NULL)
5556 {
5557 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5558 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5559 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005560 if (qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005561 (eap->cmdidx != CMD_caddexpr
5562 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005563 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5564 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005565 {
5566#ifdef FEAT_AUTOCMD
5567 if (au_name != NULL)
5568 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5569 curbuf->b_fname, TRUE, curbuf);
5570#endif
5571 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5572 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5573 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005574 }
5575 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005576 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005577 free_tv(tv);
5578 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005579}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005580#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005581
5582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 * ":helpgrep {pattern}"
5584 */
5585 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005586ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 regmatch_T regmatch;
5589 char_u *save_cpo;
5590 char_u *p;
5591 int fcount;
5592 char_u **fnames;
5593 FILE *fd;
5594 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005596#ifdef FEAT_MULTI_LANG
5597 char_u *lang;
5598#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005599 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005600 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005601 int new_qi = FALSE;
5602 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005603#ifdef FEAT_AUTOCMD
5604 char_u *au_name = NULL;
5605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005607#ifdef FEAT_MULTI_LANG
5608 /* Check for a specified language */
5609 lang = check_help_lang(eap->arg);
5610#endif
5611
Bram Moolenaar73633f82012-01-20 13:39:07 +01005612#ifdef FEAT_AUTOCMD
5613 switch (eap->cmdidx)
5614 {
5615 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5616 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5617 default: break;
5618 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005619 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5620 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005621 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005622# ifdef FEAT_EVAL
5623 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005624 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005625# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005626 }
5627#endif
5628
5629 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5630 save_cpo = p_cpo;
5631 p_cpo = empty_option;
5632
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005633 if (eap->cmdidx == CMD_lhelpgrep)
5634 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005635 /* If the current window is a help window, then use it */
5636 if (bt_help(curwin->w_buffer))
5637 wp = curwin;
5638 else
5639 /* Find an existing help window */
5640 FOR_ALL_WINDOWS(wp)
5641 if (bt_help(wp->w_buffer))
5642 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005643
5644 if (wp == NULL) /* Help window not found */
5645 qi = NULL;
5646 else
5647 qi = wp->w_llist;
5648
5649 if (qi == NULL)
5650 {
5651 /* Allocate a new location list for help text matches */
5652 if ((qi = ll_new_list()) == NULL)
5653 return;
5654 new_qi = TRUE;
5655 }
5656 }
5657
Bram Moolenaaree85df32017-03-19 14:19:50 +01005658 /* Autocommands may change the list. Save it for later comparison */
5659 save_qi = qi;
5660
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5662 regmatch.rm_ic = FALSE;
5663 if (regmatch.regprog != NULL)
5664 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005665#ifdef FEAT_MBYTE
5666 vimconv_T vc;
5667
5668 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5669 * differs. */
5670 vc.vc_type = CONV_NONE;
5671 if (!enc_utf8)
5672 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5673#endif
5674
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005676 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 /* Go through all directories in 'runtimepath' */
5679 p = p_rtp;
5680 while (*p != NUL && !got_int)
5681 {
5682 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5683
5684 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5685 add_pathsep(NameBuff);
5686 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5687 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5688 &fnames, EW_FILE|EW_SILENT) == OK
5689 && fcount > 0)
5690 {
5691 for (fi = 0; fi < fcount && !got_int; ++fi)
5692 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005693#ifdef FEAT_MULTI_LANG
5694 /* Skip files for a different language. */
5695 if (lang != NULL
5696 && STRNICMP(lang, fnames[fi]
5697 + STRLEN(fnames[fi]) - 3, 2) != 0
5698 && !(STRNICMP(lang, "en", 2) == 0
5699 && STRNICMP("txt", fnames[fi]
5700 + STRLEN(fnames[fi]) - 3, 3) == 0))
5701 continue;
5702#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005703 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (fd != NULL)
5705 {
5706 lnum = 1;
5707 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5708 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005709 char_u *line = IObuff;
5710#ifdef FEAT_MBYTE
5711 /* Convert a line if 'encoding' is not utf-8 and
5712 * the line contains a non-ASCII character. */
5713 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005714 && has_non_ascii(IObuff))
5715 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005716 line = string_convert(&vc, IObuff, NULL);
5717 if (line == NULL)
5718 line = IObuff;
5719 }
5720#endif
5721
5722 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005724 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
5726 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005727 while (l > 0 && line[l - 1] <= ' ')
5728 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005730 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005731 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 NULL, /* dir */
5733 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005734 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005735 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005737 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005738 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005739 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005740 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 0, /* nr */
5742 1, /* type */
5743 TRUE /* valid */
5744 ) == FAIL)
5745 {
5746 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005747#ifdef FEAT_MBYTE
5748 if (line != IObuff)
5749 vim_free(line);
5750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 break;
5752 }
5753 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005754#ifdef FEAT_MBYTE
5755 if (line != IObuff)
5756 vim_free(line);
5757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 ++lnum;
5759 line_breakcheck();
5760 }
5761 fclose(fd);
5762 }
5763 }
5764 FreeWild(fcount, fnames);
5765 }
5766 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005767
Bram Moolenaar473de612013-06-08 18:19:48 +02005768 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005769#ifdef FEAT_MBYTE
5770 if (vc.vc_type != CONV_NONE)
5771 convert_setup(&vc, NULL, NULL);
5772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005774 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5775 qi->qf_lists[qi->qf_curlist].qf_ptr =
5776 qi->qf_lists[qi->qf_curlist].qf_start;
5777 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
5779
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005780 if (p_cpo == empty_option)
5781 p_cpo = save_cpo;
5782 else
5783 /* Darn, some plugin changed the value. */
5784 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar864293a2016-06-02 13:40:04 +02005786 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
Bram Moolenaar73633f82012-01-20 13:39:07 +01005788#ifdef FEAT_AUTOCMD
5789 if (au_name != NULL)
5790 {
5791 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5792 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005793 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005794 /* autocommands made "qi" invalid */
5795 return;
5796 }
5797#endif
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005800 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005801 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005802 else
5803 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005804
5805 if (eap->cmdidx == CMD_lhelpgrep)
5806 {
5807 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005808 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005809 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005810 {
5811 if (new_qi)
5812 ll_free_all(&qi);
5813 }
5814 else if (curwin->w_llist == NULL)
5815 curwin->w_llist = qi;
5816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817}
5818
5819#endif /* FEAT_QUICKFIX */