blob: 05e59462830466194d803672b9f94b4907587628 [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 Moolenaarb254af32017-12-18 19:48:58 +010079 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000080} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020082/*
83 * Quickfix/Location list stack definition
84 * Contains a list of quickfix/location lists (qf_list_T)
85 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000086struct qf_info_S
87{
88 /*
89 * Count of references to this list. Used only for location lists.
90 * When a location list window reference this list, qf_refcount
91 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
92 * reaches 0, the list is freed.
93 */
94 int qf_refcount;
95 int qf_listcount; /* current number of lists */
96 int qf_curlist; /* current error list */
97 qf_list_T qf_lists[LISTCOUNT];
98};
99
100static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200101static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000103#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/*
106 * Structure used to hold the info of one part of 'errorformat'
107 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000108typedef struct efm_S efm_T;
109struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110{
111 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000112 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
114 char_u prefix; /* prefix of this format line: */
115 /* 'D' enter directory */
116 /* 'X' leave directory */
117 /* 'A' start of multi-line message */
118 /* 'E' error message */
119 /* 'W' warning message */
120 /* 'I' informational message */
121 /* 'C' continuation line */
122 /* 'Z' end of multi-line message */
123 /* 'G' general, unspecific message */
124 /* 'P' push file (partial) message */
125 /* 'Q' pop/quit file (partial) message */
126 /* 'O' overread (partial) message */
127 char_u flags; /* additional flags given in prefix */
128 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000129 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000130 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131};
132
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100133static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
134
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200135static 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 +0200136static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_new_list(qf_info_T *qi, char_u *qf_title);
138static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200139static 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 +0100140static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void qf_free(qf_info_T *qi, int idx);
142static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200143static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200144static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200146static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaar3c097222017-12-21 20:54:49 +0100147static int qflist_valid(win_T *wp, int_u qf_id);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
149static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
151static int is_qf_win(win_T *win, qf_info_T *qi);
152static win_T *qf_find_win(qf_info_T *qi);
153static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200154static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200156static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static char_u *get_mef_name(void);
158static void restore_start_dir(char_u *dirname_start);
159static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
160static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
161static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
162static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164/* Quickfix window check helper macro */
165#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
166/* Location list window check helper macro */
167#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
168/*
169 * Return location list for window 'wp'
170 * For location list window, return the referenced location list
171 */
172#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
173
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200175 * Looking up a buffer can be slow if there are many. Remember the last one
176 * to make this a lot faster if there are multiple matches in the same file.
177 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200178static char_u *qf_last_bufname = NULL;
179static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200180
Bram Moolenaar3c097222017-12-21 20:54:49 +0100181static char *e_loc_list_changed =
182 N_("E926: Current location list was changed");
183
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200184/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000185 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200186 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 * Return -1 for error, number of errors for success.
188 */
189 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200190qf_init(win_T *wp,
191 char_u *efile,
192 char_u *errorformat,
193 int newlist, /* TRUE: start a new error list */
194 char_u *qf_title,
195 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000197 qf_info_T *qi = &ql_info;
198
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000199 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000200 {
201 qi = ll_get_or_alloc_list(wp);
202 if (qi == NULL)
203 return FAIL;
204 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000205
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200206 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
207 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000208}
209
210/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200211 * Maximum number of bytes allowed per line while reading a errorfile.
212 */
213#define LINE_MAXLEN 4096
214
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200215static struct fmtpattern
216{
217 char_u convchar;
218 char *pattern;
219} fmt_pat[FMT_PATTERNS] =
220 {
221 {'f', ".\\+"}, /* only used when at end */
222 {'n', "\\d\\+"},
223 {'l', "\\d\\+"},
224 {'c', "\\d\\+"},
225 {'t', "."},
226 {'m', ".\\+"},
227 {'r', ".*"},
228 {'p', "[- .]*"},
229 {'v', "\\d\\+"},
230 {'s', ".\\+"}
231 };
232
233/*
234 * Converts a 'errorformat' string to regular expression pattern
235 */
236 static int
237efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200238 char_u *efm,
239 int len,
240 efm_T *fmt_ptr,
241 char_u *regpat,
242 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200243{
244 char_u *ptr;
245 char_u *efmp;
246 char_u *srcptr;
247 int round;
248 int idx = 0;
249
250 /*
251 * Build regexp pattern from current 'errorformat' option
252 */
253 ptr = regpat;
254 *ptr++ = '^';
255 round = 0;
256 for (efmp = efm; efmp < efm + len; ++efmp)
257 {
258 if (*efmp == '%')
259 {
260 ++efmp;
261 for (idx = 0; idx < FMT_PATTERNS; ++idx)
262 if (fmt_pat[idx].convchar == *efmp)
263 break;
264 if (idx < FMT_PATTERNS)
265 {
266 if (fmt_ptr->addr[idx])
267 {
268 sprintf((char *)errmsg,
269 _("E372: Too many %%%c in format string"), *efmp);
270 EMSG(errmsg);
271 return -1;
272 }
273 if ((idx
274 && idx < 6
275 && vim_strchr((char_u *)"DXOPQ",
276 fmt_ptr->prefix) != NULL)
277 || (idx == 6
278 && vim_strchr((char_u *)"OPQ",
279 fmt_ptr->prefix) == NULL))
280 {
281 sprintf((char *)errmsg,
282 _("E373: Unexpected %%%c in format string"), *efmp);
283 EMSG(errmsg);
284 return -1;
285 }
286 fmt_ptr->addr[idx] = (char_u)++round;
287 *ptr++ = '\\';
288 *ptr++ = '(';
289#ifdef BACKSLASH_IN_FILENAME
290 if (*efmp == 'f')
291 {
292 /* Also match "c:" in the file name, even when
293 * checking for a colon next: "%f:".
294 * "\%(\a:\)\=" */
295 STRCPY(ptr, "\\%(\\a:\\)\\=");
296 ptr += 10;
297 }
298#endif
299 if (*efmp == 'f' && efmp[1] != NUL)
300 {
301 if (efmp[1] != '\\' && efmp[1] != '%')
302 {
303 /* A file name may contain spaces, but this isn't
304 * in "\f". For "%f:%l:%m" there may be a ":" in
305 * the file name. Use ".\{-1,}x" instead (x is
306 * the next character), the requirement that :999:
307 * follows should work. */
308 STRCPY(ptr, ".\\{-1,}");
309 ptr += 7;
310 }
311 else
312 {
313 /* File name followed by '\\' or '%': include as
314 * many file name chars as possible. */
315 STRCPY(ptr, "\\f\\+");
316 ptr += 4;
317 }
318 }
319 else
320 {
321 srcptr = (char_u *)fmt_pat[idx].pattern;
322 while ((*ptr = *srcptr++) != NUL)
323 ++ptr;
324 }
325 *ptr++ = '\\';
326 *ptr++ = ')';
327 }
328 else if (*efmp == '*')
329 {
330 if (*++efmp == '[' || *efmp == '\\')
331 {
332 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
333 {
334 if (efmp[1] == '^')
335 *ptr++ = *++efmp;
336 if (efmp < efm + len)
337 {
338 *ptr++ = *++efmp; /* could be ']' */
339 while (efmp < efm + len
340 && (*ptr++ = *++efmp) != ']')
341 /* skip */;
342 if (efmp == efm + len)
343 {
344 EMSG(_("E374: Missing ] in format string"));
345 return -1;
346 }
347 }
348 }
349 else if (efmp < efm + len) /* %*\D, %*\s etc. */
350 *ptr++ = *++efmp;
351 *ptr++ = '\\';
352 *ptr++ = '+';
353 }
354 else
355 {
356 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
357 sprintf((char *)errmsg,
358 _("E375: Unsupported %%%c in format string"), *efmp);
359 EMSG(errmsg);
360 return -1;
361 }
362 }
363 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
364 *ptr++ = *efmp; /* regexp magic characters */
365 else if (*efmp == '#')
366 *ptr++ = '*';
367 else if (*efmp == '>')
368 fmt_ptr->conthere = TRUE;
369 else if (efmp == efm + 1) /* analyse prefix */
370 {
371 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
372 fmt_ptr->flags = *efmp++;
373 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
374 fmt_ptr->prefix = *efmp;
375 else
376 {
377 sprintf((char *)errmsg,
378 _("E376: Invalid %%%c in format string prefix"), *efmp);
379 EMSG(errmsg);
380 return -1;
381 }
382 }
383 else
384 {
385 sprintf((char *)errmsg,
386 _("E377: Invalid %%%c in format string"), *efmp);
387 EMSG(errmsg);
388 return -1;
389 }
390 }
391 else /* copy normal character */
392 {
393 if (*efmp == '\\' && efmp + 1 < efm + len)
394 ++efmp;
395 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
396 *ptr++ = '\\'; /* escape regexp atoms */
397 if (*efmp)
398 *ptr++ = *efmp;
399 }
400 }
401 *ptr++ = '$';
402 *ptr = NUL;
403
404 return 0;
405}
406
407 static void
408free_efm_list(efm_T **efm_first)
409{
410 efm_T *efm_ptr;
411
412 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
413 {
414 *efm_first = efm_ptr->next;
415 vim_regfree(efm_ptr->prog);
416 vim_free(efm_ptr);
417 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100418 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419}
420
421/* Parse 'errorformat' option */
422 static efm_T *
423parse_efm_option(char_u *efm)
424{
425 char_u *errmsg = NULL;
426 int errmsglen;
427 efm_T *fmt_ptr = NULL;
428 efm_T *fmt_first = NULL;
429 efm_T *fmt_last = NULL;
430 char_u *fmtstr = NULL;
431 int len;
432 int i;
433 int round;
434
435 errmsglen = CMDBUFFSIZE + 1;
436 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
437 if (errmsg == NULL)
438 goto parse_efm_end;
439
440 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200441 * Each part of the format string is copied and modified from errorformat
442 * to regex prog. Only a few % characters are allowed.
443 */
444
445 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 * Get some space to modify the format string into.
447 */
448 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
449 for (round = FMT_PATTERNS; round > 0; )
450 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200451#ifdef BACKSLASH_IN_FILENAME
452 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453#else
454 i += 2; /* "%f" can become two chars longer */
455#endif
456 if ((fmtstr = alloc(i)) == NULL)
457 goto parse_efm_error;
458
459 while (efm[0] != NUL)
460 {
461 /*
462 * Allocate a new eformat structure and put it at the end of the list
463 */
464 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
465 if (fmt_ptr == NULL)
466 goto parse_efm_error;
467 if (fmt_first == NULL) /* first one */
468 fmt_first = fmt_ptr;
469 else
470 fmt_last->next = fmt_ptr;
471 fmt_last = fmt_ptr;
472
473 /*
474 * Isolate one part in the 'errorformat' option
475 */
476 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
477 if (efm[len] == '\\' && efm[len + 1] != NUL)
478 ++len;
479
480 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
481 goto parse_efm_error;
482 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
483 goto parse_efm_error;
484 /*
485 * Advance to next part
486 */
487 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
488 }
489
490 if (fmt_first == NULL) /* nothing found */
491 EMSG(_("E378: 'errorformat' contains no pattern"));
492
493 goto parse_efm_end;
494
495parse_efm_error:
496 free_efm_list(&fmt_first);
497
498parse_efm_end:
499 vim_free(fmtstr);
500 vim_free(errmsg);
501
502 return fmt_first;
503}
504
Bram Moolenaare0d37972016-07-15 22:36:01 +0200505enum {
506 QF_FAIL = 0,
507 QF_OK = 1,
508 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200509 QF_NOMEM = 3,
510 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200511};
512
513typedef struct {
514 char_u *linebuf;
515 int linelen;
516 char_u *growbuf;
517 int growbufsiz;
518 FILE *fd;
519 typval_T *tv;
520 char_u *p_str;
521 listitem_T *p_li;
522 buf_T *buf;
523 linenr_T buflnum;
524 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100525 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200526} qfstate_T;
527
528 static char_u *
529qf_grow_linebuf(qfstate_T *state, int newsz)
530{
531 /*
532 * If the line exceeds LINE_MAXLEN exclude the last
533 * byte since it's not a NL character.
534 */
535 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
536 if (state->growbuf == NULL)
537 {
538 state->growbuf = alloc(state->linelen + 1);
539 if (state->growbuf == NULL)
540 return NULL;
541 state->growbufsiz = state->linelen;
542 }
543 else if (state->linelen > state->growbufsiz)
544 {
545 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
546 if (state->growbuf == NULL)
547 return NULL;
548 state->growbufsiz = state->linelen;
549 }
550 return state->growbuf;
551}
552
553/*
554 * Get the next string (separated by newline) from state->p_str.
555 */
556 static int
557qf_get_next_str_line(qfstate_T *state)
558{
559 /* Get the next line from the supplied string */
560 char_u *p_str = state->p_str;
561 char_u *p;
562 int len;
563
564 if (*p_str == NUL) /* Reached the end of the string */
565 return QF_END_OF_INPUT;
566
567 p = vim_strchr(p_str, '\n');
568 if (p != NULL)
569 len = (int)(p - p_str) + 1;
570 else
571 len = (int)STRLEN(p_str);
572
573 if (len > IOSIZE - 2)
574 {
575 state->linebuf = qf_grow_linebuf(state, len);
576 if (state->linebuf == NULL)
577 return QF_NOMEM;
578 }
579 else
580 {
581 state->linebuf = IObuff;
582 state->linelen = len;
583 }
584 vim_strncpy(state->linebuf, p_str, state->linelen);
585
586 /*
587 * Increment using len in order to discard the rest of the
588 * line if it exceeds LINE_MAXLEN.
589 */
590 p_str += len;
591 state->p_str = p_str;
592
593 return QF_OK;
594}
595
596/*
597 * Get the next string from state->p_Li.
598 */
599 static int
600qf_get_next_list_line(qfstate_T *state)
601{
602 listitem_T *p_li = state->p_li;
603 int len;
604
605 while (p_li != NULL
606 && (p_li->li_tv.v_type != VAR_STRING
607 || p_li->li_tv.vval.v_string == NULL))
608 p_li = p_li->li_next; /* Skip non-string items */
609
610 if (p_li == NULL) /* End of the list */
611 {
612 state->p_li = NULL;
613 return QF_END_OF_INPUT;
614 }
615
616 len = (int)STRLEN(p_li->li_tv.vval.v_string);
617 if (len > IOSIZE - 2)
618 {
619 state->linebuf = qf_grow_linebuf(state, len);
620 if (state->linebuf == NULL)
621 return QF_NOMEM;
622 }
623 else
624 {
625 state->linebuf = IObuff;
626 state->linelen = len;
627 }
628
629 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
630
631 state->p_li = p_li->li_next; /* next item */
632 return QF_OK;
633}
634
635/*
636 * Get the next string from state->buf.
637 */
638 static int
639qf_get_next_buf_line(qfstate_T *state)
640{
641 char_u *p_buf = NULL;
642 int len;
643
644 /* Get the next line from the supplied buffer */
645 if (state->buflnum > state->lnumlast)
646 return QF_END_OF_INPUT;
647
648 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
649 state->buflnum += 1;
650
651 len = (int)STRLEN(p_buf);
652 if (len > IOSIZE - 2)
653 {
654 state->linebuf = qf_grow_linebuf(state, len);
655 if (state->linebuf == NULL)
656 return QF_NOMEM;
657 }
658 else
659 {
660 state->linebuf = IObuff;
661 state->linelen = len;
662 }
663 vim_strncpy(state->linebuf, p_buf, state->linelen);
664
665 return QF_OK;
666}
667
668/*
669 * Get the next string from file state->fd.
670 */
671 static int
672qf_get_next_file_line(qfstate_T *state)
673{
674 int discard;
675 int growbuflen;
676
677 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
678 return QF_END_OF_INPUT;
679
680 discard = FALSE;
681 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200682 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200683 {
684 /*
685 * The current line exceeds IObuff, continue reading using
686 * growbuf until EOL or LINE_MAXLEN bytes is read.
687 */
688 if (state->growbuf == NULL)
689 {
690 state->growbufsiz = 2 * (IOSIZE - 1);
691 state->growbuf = alloc(state->growbufsiz);
692 if (state->growbuf == NULL)
693 return QF_NOMEM;
694 }
695
696 /* Copy the read part of the line, excluding null-terminator */
697 memcpy(state->growbuf, IObuff, IOSIZE - 1);
698 growbuflen = state->linelen;
699
700 for (;;)
701 {
702 if (fgets((char *)state->growbuf + growbuflen,
703 state->growbufsiz - growbuflen, state->fd) == NULL)
704 break;
705 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
706 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200707 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200708 break;
709 if (state->growbufsiz == LINE_MAXLEN)
710 {
711 discard = TRUE;
712 break;
713 }
714
715 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
716 ? 2 * state->growbufsiz : LINE_MAXLEN;
717 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
718 if (state->growbuf == NULL)
719 return QF_NOMEM;
720 }
721
722 while (discard)
723 {
724 /*
725 * The current line is longer than LINE_MAXLEN, continue
726 * reading but discard everything until EOL or EOF is
727 * reached.
728 */
729 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
730 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200731 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200732 break;
733 }
734
735 state->linebuf = state->growbuf;
736 state->linelen = growbuflen;
737 }
738 else
739 state->linebuf = IObuff;
740
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100741#ifdef FEAT_MBYTE
742 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200743 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
744 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100745 char_u *line;
746
747 line = string_convert(&state->vc, state->linebuf, &state->linelen);
748 if (line != NULL)
749 {
750 if (state->linelen < IOSIZE)
751 {
752 STRCPY(state->linebuf, line);
753 vim_free(line);
754 }
755 else
756 {
757 vim_free(state->growbuf);
758 state->linebuf = state->growbuf = line;
759 state->growbufsiz = state->linelen < LINE_MAXLEN
760 ? state->linelen : LINE_MAXLEN;
761 }
762 }
763 }
764#endif
765
Bram Moolenaare0d37972016-07-15 22:36:01 +0200766 return QF_OK;
767}
768
769/*
770 * Get the next string from a file/buffer/list/string.
771 */
772 static int
773qf_get_nextline(qfstate_T *state)
774{
775 int status = QF_FAIL;
776
777 if (state->fd == NULL)
778 {
779 if (state->tv != NULL)
780 {
781 if (state->tv->v_type == VAR_STRING)
782 /* Get the next line from the supplied string */
783 status = qf_get_next_str_line(state);
784 else if (state->tv->v_type == VAR_LIST)
785 /* Get the next line from the supplied list */
786 status = qf_get_next_list_line(state);
787 }
788 else
789 /* Get the next line from the supplied buffer */
790 status = qf_get_next_buf_line(state);
791 }
792 else
793 /* Get the next line from the supplied file */
794 status = qf_get_next_file_line(state);
795
796 if (status != QF_OK)
797 return status;
798
799 /* remove newline/CR from the line */
800 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200801 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802 state->linebuf[state->linelen - 1] = NUL;
803#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200804 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
805 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200807 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808
809#ifdef FEAT_MBYTE
810 remove_bom(state->linebuf);
811#endif
812
813 return QF_OK;
814}
815
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200816typedef struct {
817 char_u *namebuf;
818 char_u *errmsg;
819 int errmsglen;
820 long lnum;
821 int col;
822 char_u use_viscol;
823 char_u *pattern;
824 int enr;
825 int type;
826 int valid;
827} qffields_T;
828
829/*
830 * Parse a line and get the quickfix fields.
831 * Return the QF_ status.
832 */
833 static int
834qf_parse_line(
835 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200836 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200837 char_u *linebuf,
838 int linelen,
839 efm_T *fmt_first,
840 qffields_T *fields)
841{
842 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200843 char_u *ptr;
844 int len;
845 int i;
846 int idx = 0;
847 char_u *tail = NULL;
848 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200849 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200850
851 /* Always ignore case when looking for a matching error. */
852 regmatch.rm_ic = TRUE;
853
854 /* If there was no %> item start at the first pattern */
855 if (fmt_start == NULL)
856 fmt_ptr = fmt_first;
857 else
858 {
859 fmt_ptr = fmt_start;
860 fmt_start = NULL;
861 }
862
863 /*
864 * Try to match each part of 'errorformat' until we find a complete
865 * match or no match.
866 */
867 fields->valid = TRUE;
868restofline:
869 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
870 {
871 int r;
872
873 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200874 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200875 continue;
876 fields->namebuf[0] = NUL;
877 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200878 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200879 fields->errmsg[0] = NUL;
880 fields->lnum = 0;
881 fields->col = 0;
882 fields->use_viscol = FALSE;
883 fields->enr = -1;
884 fields->type = 0;
885 tail = NULL;
886
887 regmatch.regprog = fmt_ptr->prog;
888 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
889 fmt_ptr->prog = regmatch.regprog;
890 if (r)
891 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200892 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200893 continue;
894 if (vim_strchr((char_u *)"EWI", idx) != NULL)
895 fields->type = idx;
896 else
897 fields->type = 0;
898 /*
899 * Extract error message data from matched line.
900 * We check for an actual submatch, because "\[" and "\]" in
901 * the 'errorformat' may cause the wrong submatch to be used.
902 */
903 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
904 {
905 int c;
906
907 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
908 continue;
909
910 /* Expand ~/file and $HOME/file to full path. */
911 c = *regmatch.endp[i];
912 *regmatch.endp[i] = NUL;
913 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
914 *regmatch.endp[i] = c;
915
916 if (vim_strchr((char_u *)"OPQ", idx) != NULL
917 && mch_getperm(fields->namebuf) == -1)
918 continue;
919 }
920 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
921 {
922 if (regmatch.startp[i] == NULL)
923 continue;
924 fields->enr = (int)atol((char *)regmatch.startp[i]);
925 }
926 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
927 {
928 if (regmatch.startp[i] == NULL)
929 continue;
930 fields->lnum = atol((char *)regmatch.startp[i]);
931 }
932 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
933 {
934 if (regmatch.startp[i] == NULL)
935 continue;
936 fields->col = (int)atol((char *)regmatch.startp[i]);
937 }
938 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
939 {
940 if (regmatch.startp[i] == NULL)
941 continue;
942 fields->type = *regmatch.startp[i];
943 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200944 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200945 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200946 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200947 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200948 /* linelen + null terminator */
949 if ((fields->errmsg = vim_realloc(fields->errmsg,
950 linelen + 1)) == NULL)
951 return QF_NOMEM;
952 fields->errmsglen = linelen + 1;
953 }
954 vim_strncpy(fields->errmsg, linebuf, linelen);
955 }
956 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
957 {
958 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
959 continue;
960 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200961 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200962 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200963 /* len + null terminator */
964 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
965 == NULL)
966 return QF_NOMEM;
967 fields->errmsglen = len + 1;
968 }
969 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
970 }
971 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
972 {
973 if (regmatch.startp[i] == NULL)
974 continue;
975 tail = regmatch.startp[i];
976 }
977 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
978 {
979 char_u *match_ptr;
980
981 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
982 continue;
983 fields->col = 0;
984 for (match_ptr = regmatch.startp[i];
985 match_ptr != regmatch.endp[i]; ++match_ptr)
986 {
987 ++fields->col;
988 if (*match_ptr == TAB)
989 {
990 fields->col += 7;
991 fields->col -= fields->col % 8;
992 }
993 }
994 ++fields->col;
995 fields->use_viscol = TRUE;
996 }
997 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
998 {
999 if (regmatch.startp[i] == NULL)
1000 continue;
1001 fields->col = (int)atol((char *)regmatch.startp[i]);
1002 fields->use_viscol = TRUE;
1003 }
1004 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1005 {
1006 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1007 continue;
1008 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1009 if (len > CMDBUFFSIZE - 5)
1010 len = CMDBUFFSIZE - 5;
1011 STRCPY(fields->pattern, "^\\V");
1012 STRNCAT(fields->pattern, regmatch.startp[i], len);
1013 fields->pattern[len + 3] = '\\';
1014 fields->pattern[len + 4] = '$';
1015 fields->pattern[len + 5] = NUL;
1016 }
1017 break;
1018 }
1019 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001020 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001021
1022 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1023 {
1024 if (fmt_ptr != NULL)
1025 {
1026 if (idx == 'D') /* enter directory */
1027 {
1028 if (*fields->namebuf == NUL)
1029 {
1030 EMSG(_("E379: Missing or empty directory name"));
1031 return QF_FAIL;
1032 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001033 qfl->qf_directory =
1034 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1035 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001036 return QF_FAIL;
1037 }
1038 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001039 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001040 }
1041 fields->namebuf[0] = NUL; /* no match found, remove file name */
1042 fields->lnum = 0; /* don't jump to this line */
1043 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001044 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001045 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001046 /* linelen + null terminator */
1047 if ((fields->errmsg = vim_realloc(fields->errmsg,
1048 linelen + 1)) == NULL)
1049 return QF_NOMEM;
1050 fields->errmsglen = linelen + 1;
1051 }
1052 /* copy whole line to error message */
1053 vim_strncpy(fields->errmsg, linebuf, linelen);
1054 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001055 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001056 }
1057 else if (fmt_ptr != NULL)
1058 {
1059 /* honor %> item */
1060 if (fmt_ptr->conthere)
1061 fmt_start = fmt_ptr;
1062
1063 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1064 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001065 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1066 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001067 }
1068 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1069 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001070 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001071 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001072 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001073
Bram Moolenaar9b457942016-10-09 16:10:05 +02001074 if (qfprev == NULL)
1075 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001076 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001077 {
1078 len = (int)STRLEN(qfprev->qf_text);
1079 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1080 == NULL)
1081 return QF_FAIL;
1082 STRCPY(ptr, qfprev->qf_text);
1083 vim_free(qfprev->qf_text);
1084 qfprev->qf_text = ptr;
1085 *(ptr += len) = '\n';
1086 STRCPY(++ptr, fields->errmsg);
1087 }
1088 if (qfprev->qf_nr == -1)
1089 qfprev->qf_nr = fields->enr;
1090 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1091 /* only printable chars allowed */
1092 qfprev->qf_type = fields->type;
1093
1094 if (!qfprev->qf_lnum)
1095 qfprev->qf_lnum = fields->lnum;
1096 if (!qfprev->qf_col)
1097 qfprev->qf_col = fields->col;
1098 qfprev->qf_viscol = fields->use_viscol;
1099 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001100 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1101 qfl->qf_directory,
1102 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001103 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001104 : qfl->qf_currfile != NULL && fields->valid
1105 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001106 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001107 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001108 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001109 line_breakcheck();
1110 return QF_IGNORE_LINE;
1111 }
1112 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1113 {
1114 /* global file names */
1115 fields->valid = FALSE;
1116 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1117 {
1118 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001119 qfl->qf_currfile =
1120 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001121 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001122 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001123 *fields->namebuf = NUL;
1124 if (tail && *tail)
1125 {
1126 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001127 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001128 goto restofline;
1129 }
1130 }
1131 }
1132 if (fmt_ptr->flags == '-') /* generally exclude this line */
1133 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001134 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001135 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001136 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001137 return QF_IGNORE_LINE;
1138 }
1139 }
1140
1141 return QF_OK;
1142}
1143
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001144/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001145 * Read the errorfile "efile" into memory, line by line, building the error
1146 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001147 * Alternative: when "efile" is NULL read errors from buffer "buf".
1148 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001149 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001150 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1151 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001152 * Return -1 for error, number of errors for success.
1153 */
1154 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001155qf_init_ext(
1156 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001157 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001158 char_u *efile,
1159 buf_T *buf,
1160 typval_T *tv,
1161 char_u *errorformat,
1162 int newlist, /* TRUE: start a new error list */
1163 linenr_T lnumfirst, /* first line number to use */
1164 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001165 char_u *qf_title,
1166 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001167{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001168 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001169 qfstate_T state;
1170 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001171 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001172 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001173 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001175 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001177 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001179 /* Do not used the cached buffer, it may have been wiped out. */
1180 vim_free(qf_last_bufname);
1181 qf_last_bufname = NULL;
1182
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001183 vim_memset(&state, 0, sizeof(state));
1184 vim_memset(&fields, 0, sizeof(fields));
1185#ifdef FEAT_MBYTE
1186 state.vc.vc_type = CONV_NONE;
1187 if (enc != NULL && *enc != NUL)
1188 convert_setup(&state.vc, enc, p_enc);
1189#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001190 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1191 fields.errmsglen = CMDBUFFSIZE + 1;
1192 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1193 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001194 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 goto qf_init_end;
1196
Bram Moolenaare0d37972016-07-15 22:36:01 +02001197 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
1199 EMSG2(_(e_openerrf), efile);
1200 goto qf_init_end;
1201 }
1202
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001203 if (newlist || qf_idx == qi->qf_listcount)
1204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001206 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001207 qf_idx = qi->qf_curlist;
1208 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001209 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001210 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001211 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001212 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001213 if (qi->qf_lists[qf_idx].qf_count > 0)
1214 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001217 qfl = &qi->qf_lists[qf_idx];
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001220 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001221 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 else
1223 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001225 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001226 * If the errorformat didn't change between calls, then reuse the
1227 * previously parsed values.
1228 */
1229 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1230 {
1231 /* free the previously parsed data */
1232 vim_free(last_efm);
1233 last_efm = NULL;
1234 free_efm_list(&fmt_first);
1235
1236 /* parse the current 'efm' */
1237 fmt_first = parse_efm_option(efm);
1238 if (fmt_first != NULL)
1239 last_efm = vim_strsave(efm);
1240 }
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /*
1246 * got_int is reset here, because it was probably set when killing the
1247 * ":make" command, but we still want to read the errorfile then.
1248 */
1249 got_int = FALSE;
1250
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001251 if (tv != NULL)
1252 {
1253 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001254 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001255 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001256 state.p_li = tv->vval.v_list->lv_first;
1257 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001258 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001259 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001260 state.buflnum = lnumfirst;
1261 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /*
1264 * Read the lines in the error file one by one.
1265 * Try to recognize one of the error formats in each line.
1266 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001267 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001269 /* Get the next line from a file/buffer/list/string */
1270 status = qf_get_nextline(&state);
1271 if (status == QF_NOMEM) /* memory alloc failure */
1272 goto qf_init_end;
1273 if (status == QF_END_OF_INPUT) /* end of input */
1274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001276 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1277 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001278 if (status == QF_FAIL)
1279 goto error2;
1280 if (status == QF_NOMEM)
1281 goto qf_init_end;
1282 if (status == QF_IGNORE_LINE)
1283 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001285 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001286 qf_idx,
1287 qfl->qf_directory,
1288 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001289 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001290 : ((qfl->qf_currfile != NULL && fields.valid)
1291 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001292 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001293 fields.errmsg,
1294 fields.lnum,
1295 fields.col,
1296 fields.use_viscol,
1297 fields.pattern,
1298 fields.enr,
1299 fields.type,
1300 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 goto error2;
1302 line_breakcheck();
1303 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001304 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001306 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001308 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001309 qfl->qf_ptr = qfl->qf_start;
1310 qfl->qf_index = 1;
1311 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 else
1314 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001315 qfl->qf_nonevalid = FALSE;
1316 if (qfl->qf_ptr == NULL)
1317 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001319 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001320 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001321 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 EMSG(_(e_readerrf));
1324error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001325 if (!adding)
1326 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001327 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001328 qf_free(qi, qi->qf_curlist);
1329 qi->qf_listcount--;
1330 if (qi->qf_curlist > 0)
1331 --qi->qf_curlist;
1332 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001333qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001334 if (state.fd != NULL)
1335 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001336 vim_free(fields.namebuf);
1337 vim_free(fields.errmsg);
1338 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001339 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001341 if (qf_idx == qi->qf_curlist)
1342 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001343#ifdef FEAT_MBYTE
1344 if (state.vc.vc_type != CONV_NONE)
1345 convert_setup(&state.vc, NULL, NULL);
1346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 return retval;
1349}
1350
Bram Moolenaarfb604092014-07-23 15:55:00 +02001351 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001352qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001353{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001354 vim_free(qi->qf_lists[qf_idx].qf_title);
1355 qi->qf_lists[qf_idx].qf_title = NULL;
1356
Bram Moolenaarfb604092014-07-23 15:55:00 +02001357 if (title != NULL)
1358 {
1359 char_u *p = alloc((int)STRLEN(title) + 2);
1360
Bram Moolenaara3921f42017-06-04 15:30:34 +02001361 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001362 if (p != NULL)
1363 sprintf((char *)p, ":%s", (char *)title);
1364 }
1365}
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001368 * Prepare for adding a new quickfix list. If the current list is in the
1369 * middle of the stack, then all the following lists are freed and then
1370 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 */
1372 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001373qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
1375 int i;
1376
1377 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001378 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 * the current entry. This makes it possible to browse in a tree-like
1380 * way with ":grep'.
1381 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001382 while (qi->qf_listcount > qi->qf_curlist + 1)
1383 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
1385 /*
1386 * When the stack is full, remove to oldest entry
1387 * Otherwise, add a new entry.
1388 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001389 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001391 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001393 qi->qf_lists[i - 1] = qi->qf_lists[i];
1394 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001397 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001398 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001399 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001400 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401}
1402
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001403/*
1404 * Free a location list
1405 */
1406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001407ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001408{
1409 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001410 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001411
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001412 qi = *pqi;
1413 if (qi == NULL)
1414 return;
1415 *pqi = NULL; /* Remove reference to this list */
1416
1417 qi->qf_refcount--;
1418 if (qi->qf_refcount < 1)
1419 {
1420 /* No references to this location list */
1421 for (i = 0; i < qi->qf_listcount; ++i)
1422 qf_free(qi, i);
1423 vim_free(qi);
1424 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001425}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001426
1427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001428qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001429{
1430 int i;
1431 qf_info_T *qi = &ql_info;
1432
1433 if (wp != NULL)
1434 {
1435 /* location list */
1436 ll_free_all(&wp->w_llist);
1437 ll_free_all(&wp->w_llist_ref);
1438 }
1439 else
1440 /* quickfix list */
1441 for (i = 0; i < qi->qf_listcount; ++i)
1442 qf_free(qi, i);
1443}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445/*
1446 * Add an entry to the end of the list of errors.
1447 * Returns OK or FAIL.
1448 */
1449 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001450qf_add_entry(
1451 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001452 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001453 char_u *dir, /* optional directory name */
1454 char_u *fname, /* file name or NULL */
1455 int bufnum, /* buffer number or zero */
1456 char_u *mesg, /* message */
1457 long lnum, /* line number */
1458 int col, /* column */
1459 int vis_col, /* using visual column */
1460 char_u *pattern, /* search pattern */
1461 int nr, /* error number */
1462 int type, /* type character */
1463 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001465 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001466 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001468 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001470 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001471 {
1472 buf_T *buf = buflist_findnr(bufnum);
1473
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001474 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001475 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001476 buf->b_has_qf_entry |=
1477 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001478 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001479 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001480 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1482 {
1483 vim_free(qfp);
1484 return FAIL;
1485 }
1486 qfp->qf_lnum = lnum;
1487 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001488 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001489 if (pattern == NULL || *pattern == NUL)
1490 qfp->qf_pattern = NULL;
1491 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1492 {
1493 vim_free(qfp->qf_text);
1494 vim_free(qfp);
1495 return FAIL;
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 qfp->qf_nr = nr;
1498 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1499 type = 0;
1500 qfp->qf_type = type;
1501 qfp->qf_valid = valid;
1502
Bram Moolenaara3921f42017-06-04 15:30:34 +02001503 lastp = &qi->qf_lists[qf_idx].qf_last;
1504 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001505 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001507 qi->qf_lists[qf_idx].qf_start = qfp;
1508 qi->qf_lists[qf_idx].qf_ptr = qfp;
1509 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001510 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 else
1513 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001514 qfp->qf_prev = *lastp;
1515 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001517 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001519 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001520 ++qi->qf_lists[qf_idx].qf_count;
1521 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001524 qi->qf_lists[qf_idx].qf_index =
1525 qi->qf_lists[qf_idx].qf_count;
1526 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
1528
1529 return OK;
1530}
1531
1532/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001533 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001535 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001536ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001537{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001538 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001539
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001540 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1541 if (qi != NULL)
1542 {
1543 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1544 qi->qf_refcount++;
1545 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001546
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001547 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548}
1549
1550/*
1551 * Return the location list for window 'wp'.
1552 * If not present, allocate a location list
1553 */
1554 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001555ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001556{
1557 if (IS_LL_WINDOW(wp))
1558 /* For a location list window, use the referenced location list */
1559 return wp->w_llist_ref;
1560
1561 /*
1562 * For a non-location list window, w_llist_ref should not point to a
1563 * location list.
1564 */
1565 ll_free_all(&wp->w_llist_ref);
1566
1567 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001568 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001569 return wp->w_llist;
1570}
1571
1572/*
1573 * Copy the location list from window "from" to window "to".
1574 */
1575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001576copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001577{
1578 qf_info_T *qi;
1579 int idx;
1580 int i;
1581
1582 /*
1583 * When copying from a location list window, copy the referenced
1584 * location list. For other windows, copy the location list for
1585 * that window.
1586 */
1587 if (IS_LL_WINDOW(from))
1588 qi = from->w_llist_ref;
1589 else
1590 qi = from->w_llist;
1591
1592 if (qi == NULL) /* no location list to copy */
1593 return;
1594
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001595 /* allocate a new location list */
1596 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001597 return;
1598
1599 to->w_llist->qf_listcount = qi->qf_listcount;
1600
1601 /* Copy the location lists one at a time */
1602 for (idx = 0; idx < qi->qf_listcount; idx++)
1603 {
1604 qf_list_T *from_qfl;
1605 qf_list_T *to_qfl;
1606
1607 to->w_llist->qf_curlist = idx;
1608
1609 from_qfl = &qi->qf_lists[idx];
1610 to_qfl = &to->w_llist->qf_lists[idx];
1611
1612 /* Some of the fields are populated by qf_add_entry() */
1613 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1614 to_qfl->qf_count = 0;
1615 to_qfl->qf_index = 0;
1616 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001617 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001618 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001619 if (from_qfl->qf_title != NULL)
1620 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1621 else
1622 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001623 if (from_qfl->qf_ctx != NULL)
1624 {
1625 to_qfl->qf_ctx = alloc_tv();
1626 if (to_qfl->qf_ctx != NULL)
1627 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1628 }
1629 else
1630 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631
1632 if (from_qfl->qf_count)
1633 {
1634 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001635 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001636
1637 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001638 for (i = 0, from_qfp = from_qfl->qf_start;
1639 i < from_qfl->qf_count && from_qfp != NULL;
1640 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001641 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001642 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001643 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 NULL,
1645 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001646 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001647 from_qfp->qf_text,
1648 from_qfp->qf_lnum,
1649 from_qfp->qf_col,
1650 from_qfp->qf_viscol,
1651 from_qfp->qf_pattern,
1652 from_qfp->qf_nr,
1653 0,
1654 from_qfp->qf_valid) == FAIL)
1655 {
1656 qf_free_all(to);
1657 return;
1658 }
1659 /*
1660 * qf_add_entry() will not set the qf_num field, as the
1661 * directory and file names are not supplied. So the qf_fnum
1662 * field is copied here.
1663 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001664 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001665 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1666 prevp->qf_type = from_qfp->qf_type; /* error type */
1667 if (from_qfl->qf_ptr == from_qfp)
1668 to_qfl->qf_ptr = prevp; /* current location */
1669 }
1670 }
1671
1672 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1673
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001674 /* Assign a new ID for the location list */
1675 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001676 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001677
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001678 /* When no valid entries are present in the list, qf_ptr points to
1679 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001680 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001681 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001682 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001683 to_qfl->qf_index = 1;
1684 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001685 }
1686
1687 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1688}
1689
1690/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001691 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001692 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 */
1694 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001695qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
Bram Moolenaar82404332016-07-10 17:00:38 +02001697 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001698 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001699 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (fname == NULL || *fname == NUL) /* no file name */
1702 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaare60acc12011-05-10 16:41:25 +02001704#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001705 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001706#endif
1707#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001708 if (directory != NULL)
1709 slash_adjust(directory);
1710 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001711#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001712 if (directory != NULL && !vim_isAbsName(fname)
1713 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1714 {
1715 /*
1716 * Here we check if the file really exists.
1717 * This should normally be true, but if make works without
1718 * "leaving directory"-messages we might have missed a
1719 * directory change.
1720 */
1721 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001724 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001725 if (directory)
1726 ptr = concat_fnames(directory, fname, TRUE);
1727 else
1728 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001730 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001731 bufname = ptr;
1732 }
1733 else
1734 bufname = fname;
1735
1736 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001737 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001738 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001739 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001740 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001742 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001743 {
1744 vim_free(qf_last_bufname);
1745 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1746 if (bufname == ptr)
1747 qf_last_bufname = bufname;
1748 else
1749 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001750 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001751 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001752 if (buf == NULL)
1753 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001754
Bram Moolenaarc1542742016-07-20 21:44:37 +02001755 buf->b_has_qf_entry =
1756 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001757 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758}
1759
1760/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001761 * Push dirbuf onto the directory stack and return pointer to actual dir or
1762 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 */
1764 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001765qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 struct dir_stack_T *ds_new;
1768 struct dir_stack_T *ds_ptr;
1769
1770 /* allocate new stack element and hook it in */
1771 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1772 if (ds_new == NULL)
1773 return NULL;
1774
1775 ds_new->next = *stackptr;
1776 *stackptr = ds_new;
1777
1778 /* store directory on the stack */
1779 if (vim_isAbsName(dirbuf)
1780 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001781 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 (*stackptr)->dirname = vim_strsave(dirbuf);
1783 else
1784 {
1785 /* Okay we don't have an absolute path.
1786 * dirbuf must be a subdir of one of the directories on the stack.
1787 * Let's search...
1788 */
1789 ds_new = (*stackptr)->next;
1790 (*stackptr)->dirname = NULL;
1791 while (ds_new)
1792 {
1793 vim_free((*stackptr)->dirname);
1794 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1795 TRUE);
1796 if (mch_isdir((*stackptr)->dirname) == TRUE)
1797 break;
1798
1799 ds_new = ds_new->next;
1800 }
1801
1802 /* clean up all dirs we already left */
1803 while ((*stackptr)->next != ds_new)
1804 {
1805 ds_ptr = (*stackptr)->next;
1806 (*stackptr)->next = (*stackptr)->next->next;
1807 vim_free(ds_ptr->dirname);
1808 vim_free(ds_ptr);
1809 }
1810
1811 /* Nothing found -> it must be on top level */
1812 if (ds_new == NULL)
1813 {
1814 vim_free((*stackptr)->dirname);
1815 (*stackptr)->dirname = vim_strsave(dirbuf);
1816 }
1817 }
1818
1819 if ((*stackptr)->dirname != NULL)
1820 return (*stackptr)->dirname;
1821 else
1822 {
1823 ds_ptr = *stackptr;
1824 *stackptr = (*stackptr)->next;
1825 vim_free(ds_ptr);
1826 return NULL;
1827 }
1828}
1829
1830
1831/*
1832 * pop dirbuf from the directory stack and return previous directory or NULL if
1833 * stack is empty
1834 */
1835 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001836qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 struct dir_stack_T *ds_ptr;
1839
1840 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1841 * What to do if it isn't? */
1842
1843 /* pop top element and free it */
1844 if (*stackptr != NULL)
1845 {
1846 ds_ptr = *stackptr;
1847 *stackptr = (*stackptr)->next;
1848 vim_free(ds_ptr->dirname);
1849 vim_free(ds_ptr);
1850 }
1851
1852 /* return NEW top element as current dir or NULL if stack is empty*/
1853 return *stackptr ? (*stackptr)->dirname : NULL;
1854}
1855
1856/*
1857 * clean up directory stack
1858 */
1859 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001860qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
1862 struct dir_stack_T *ds_ptr;
1863
1864 while ((ds_ptr = *stackptr) != NULL)
1865 {
1866 *stackptr = (*stackptr)->next;
1867 vim_free(ds_ptr->dirname);
1868 vim_free(ds_ptr);
1869 }
1870}
1871
1872/*
1873 * Check in which directory of the directory stack the given file can be
1874 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001875 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 * Cleans up intermediate directory entries.
1877 *
1878 * TODO: How to solve the following problem?
1879 * If we have the this directory tree:
1880 * ./
1881 * ./aa
1882 * ./aa/bb
1883 * ./bb
1884 * ./bb/x.c
1885 * and make says:
1886 * making all in aa
1887 * making all in bb
1888 * x.c:9: Error
1889 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1890 * qf_guess_filepath will return NULL.
1891 */
1892 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001893qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 struct dir_stack_T *ds_ptr;
1896 struct dir_stack_T *ds_tmp;
1897 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001898 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001901 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 return NULL;
1903
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001904 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 fullname = NULL;
1906 while (ds_ptr)
1907 {
1908 vim_free(fullname);
1909 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1910
1911 /* If concat_fnames failed, just go on. The worst thing that can happen
1912 * is that we delete the entire stack.
1913 */
1914 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1915 break;
1916
1917 ds_ptr = ds_ptr->next;
1918 }
1919
1920 vim_free(fullname);
1921
1922 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001923 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001925 ds_tmp = qfl->qf_dir_stack->next;
1926 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 vim_free(ds_tmp->dirname);
1928 vim_free(ds_tmp);
1929 }
1930
1931 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932}
1933
1934/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01001935 * Returns TRUE if a quickfix/location list with the given identifier exists.
1936 */
1937 static int
1938qflist_valid (win_T *wp, int_u qf_id)
1939{
1940 qf_info_T *qi = &ql_info;
1941 int i;
1942
1943 if (wp != NULL)
1944 {
1945 qi = GET_LOC_LIST(wp); /* Location list */
1946 if (qi == NULL)
1947 return FALSE;
1948 }
1949
1950 for (i = 0; i < qi->qf_listcount; ++i)
1951 if (qi->qf_lists[i].qf_id == qf_id)
1952 return TRUE;
1953
1954 return FALSE;
1955}
1956
1957/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001958 * When loading a file from the quickfix, the auto commands may modify it.
1959 * This may invalidate the current quickfix entry. This function checks
1960 * whether a entry is still present in the quickfix.
1961 * Similar to location list.
1962 */
1963 static int
1964is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1965{
1966 qf_list_T *qfl;
1967 qfline_T *qfp;
1968 int i;
1969
1970 qfl = &qi->qf_lists[qi->qf_curlist];
1971
1972 /* Search for the entry in the current list */
1973 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1974 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001975 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001976 break;
1977
1978 if (i == qfl->qf_count) /* Entry is not found */
1979 return FALSE;
1980
1981 return TRUE;
1982}
1983
1984/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001985 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001986 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001987 */
1988 static qfline_T *
1989get_next_valid_entry(
1990 qf_info_T *qi,
1991 qfline_T *qf_ptr,
1992 int *qf_index,
1993 int dir)
1994{
1995 int idx;
1996 int old_qf_fnum;
1997
1998 idx = *qf_index;
1999 old_qf_fnum = qf_ptr->qf_fnum;
2000
2001 do
2002 {
2003 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2004 || qf_ptr->qf_next == NULL)
2005 return NULL;
2006 ++idx;
2007 qf_ptr = qf_ptr->qf_next;
2008 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2009 && !qf_ptr->qf_valid)
2010 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2011
2012 *qf_index = idx;
2013 return qf_ptr;
2014}
2015
2016/*
2017 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002018 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002019 */
2020 static qfline_T *
2021get_prev_valid_entry(
2022 qf_info_T *qi,
2023 qfline_T *qf_ptr,
2024 int *qf_index,
2025 int dir)
2026{
2027 int idx;
2028 int old_qf_fnum;
2029
2030 idx = *qf_index;
2031 old_qf_fnum = qf_ptr->qf_fnum;
2032
2033 do
2034 {
2035 if (idx == 1 || qf_ptr->qf_prev == NULL)
2036 return NULL;
2037 --idx;
2038 qf_ptr = qf_ptr->qf_prev;
2039 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2040 && !qf_ptr->qf_valid)
2041 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2042
2043 *qf_index = idx;
2044 return qf_ptr;
2045}
2046
2047/*
2048 * Get the n'th (errornr) previous/next valid entry from the current entry in
2049 * the quickfix list.
2050 * dir == FORWARD or FORWARD_FILE: next valid entry
2051 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2052 */
2053 static qfline_T *
2054get_nth_valid_entry(
2055 qf_info_T *qi,
2056 int errornr,
2057 qfline_T *qf_ptr,
2058 int *qf_index,
2059 int dir)
2060{
2061 qfline_T *prev_qf_ptr;
2062 int prev_index;
2063 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2064 char_u *err = e_no_more_items;
2065
2066 while (errornr--)
2067 {
2068 prev_qf_ptr = qf_ptr;
2069 prev_index = *qf_index;
2070
2071 if (dir == FORWARD || dir == FORWARD_FILE)
2072 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2073 else
2074 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2075 if (qf_ptr == NULL)
2076 {
2077 qf_ptr = prev_qf_ptr;
2078 *qf_index = prev_index;
2079 if (err != NULL)
2080 {
2081 EMSG(_(err));
2082 return NULL;
2083 }
2084 break;
2085 }
2086
2087 err = NULL;
2088 }
2089
2090 return qf_ptr;
2091}
2092
2093/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002094 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002095 */
2096 static qfline_T *
2097get_nth_entry(
2098 qf_info_T *qi,
2099 int errornr,
2100 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002101 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002102{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002103 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002104
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002105 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002106 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2107 {
2108 --qf_idx;
2109 qf_ptr = qf_ptr->qf_prev;
2110 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002111 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002112 while (errornr > qf_idx &&
2113 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2114 qf_ptr->qf_next != NULL)
2115 {
2116 ++qf_idx;
2117 qf_ptr = qf_ptr->qf_next;
2118 }
2119
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002120 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002121 return qf_ptr;
2122}
2123
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002124/*
2125 * Find a help window or open one.
2126 */
2127 static int
2128jump_to_help_window(qf_info_T *qi, int *opened_window)
2129{
2130 win_T *wp;
2131 int flags;
2132
2133 if (cmdmod.tab != 0)
2134 wp = NULL;
2135 else
2136 FOR_ALL_WINDOWS(wp)
2137 if (bt_help(wp->w_buffer))
2138 break;
2139 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2140 win_enter(wp, TRUE);
2141 else
2142 {
2143 /*
2144 * Split off help window; put it at far top if no position
2145 * specified, the current window is vertically split and narrow.
2146 */
2147 flags = WSP_HELP;
2148 if (cmdmod.split == 0 && curwin->w_width != Columns
2149 && curwin->w_width < 80)
2150 flags |= WSP_TOP;
2151 if (qi != &ql_info)
2152 flags |= WSP_NEWLOC; /* don't copy the location list */
2153
2154 if (win_split(0, flags) == FAIL)
2155 return FAIL;
2156
2157 *opened_window = TRUE;
2158
2159 if (curwin->w_height < p_hh)
2160 win_setheight((int)p_hh);
2161
2162 if (qi != &ql_info) /* not a quickfix list */
2163 {
2164 /* The new window should use the supplied location list */
2165 curwin->w_llist = qi;
2166 qi->qf_refcount++;
2167 }
2168 }
2169
2170 if (!p_im)
2171 restart_edit = 0; /* don't want insert mode in help file */
2172
2173 return OK;
2174}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002175
2176/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002177 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2178 * If the file is already opened in a window, jump to it.
2179 */
2180 static int
2181qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2182{
2183 win_T *usable_win_ptr = NULL;
2184 int usable_win;
2185 qf_info_T *ll_ref;
2186 int flags;
2187 win_T *win;
2188 win_T *altwin;
2189
2190 usable_win = 0;
2191
2192 ll_ref = curwin->w_llist_ref;
2193 if (ll_ref != NULL)
2194 {
2195 /* Find a window using the same location list that is not a
2196 * quickfix window. */
2197 FOR_ALL_WINDOWS(usable_win_ptr)
2198 if (usable_win_ptr->w_llist == ll_ref
2199 && !bt_quickfix(usable_win_ptr->w_buffer))
2200 {
2201 usable_win = 1;
2202 break;
2203 }
2204 }
2205
2206 if (!usable_win)
2207 {
2208 /* Locate a window showing a normal buffer */
2209 FOR_ALL_WINDOWS(win)
2210 if (win->w_buffer->b_p_bt[0] == NUL)
2211 {
2212 usable_win = 1;
2213 break;
2214 }
2215 }
2216
2217 /*
2218 * If no usable window is found and 'switchbuf' contains "usetab"
2219 * then search in other tabs.
2220 */
2221 if (!usable_win && (swb_flags & SWB_USETAB))
2222 {
2223 tabpage_T *tp;
2224 win_T *wp;
2225
2226 FOR_ALL_TAB_WINDOWS(tp, wp)
2227 {
2228 if (wp->w_buffer->b_fnum == qf_fnum)
2229 {
2230 goto_tabpage_win(tp, wp);
2231 usable_win = 1;
2232 goto win_found;
2233 }
2234 }
2235 }
2236win_found:
2237
2238 /*
2239 * If there is only one window and it is the quickfix window, create a
2240 * new one above the quickfix window.
2241 */
2242 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2243 {
2244 flags = WSP_ABOVE;
2245 if (ll_ref != NULL)
2246 flags |= WSP_NEWLOC;
2247 if (win_split(0, flags) == FAIL)
2248 return FAIL; /* not enough room for window */
2249 *opened_window = TRUE; /* close it when fail */
2250 p_swb = empty_option; /* don't split again */
2251 swb_flags = 0;
2252 RESET_BINDING(curwin);
2253 if (ll_ref != NULL)
2254 {
2255 /* The new window should use the location list from the
2256 * location list window */
2257 curwin->w_llist = ll_ref;
2258 ll_ref->qf_refcount++;
2259 }
2260 }
2261 else
2262 {
2263 if (curwin->w_llist_ref != NULL)
2264 {
2265 /* In a location window */
2266 win = usable_win_ptr;
2267 if (win == NULL)
2268 {
2269 /* Find the window showing the selected file */
2270 FOR_ALL_WINDOWS(win)
2271 if (win->w_buffer->b_fnum == qf_fnum)
2272 break;
2273 if (win == NULL)
2274 {
2275 /* Find a previous usable window */
2276 win = curwin;
2277 do
2278 {
2279 if (win->w_buffer->b_p_bt[0] == NUL)
2280 break;
2281 if (win->w_prev == NULL)
2282 win = lastwin; /* wrap around the top */
2283 else
2284 win = win->w_prev; /* go to previous window */
2285 } while (win != curwin);
2286 }
2287 }
2288 win_goto(win);
2289
2290 /* If the location list for the window is not set, then set it
2291 * to the location list from the location window */
2292 if (win->w_llist == NULL)
2293 {
2294 win->w_llist = ll_ref;
2295 ll_ref->qf_refcount++;
2296 }
2297 }
2298 else
2299 {
2300
2301 /*
2302 * Try to find a window that shows the right buffer.
2303 * Default to the window just above the quickfix buffer.
2304 */
2305 win = curwin;
2306 altwin = NULL;
2307 for (;;)
2308 {
2309 if (win->w_buffer->b_fnum == qf_fnum)
2310 break;
2311 if (win->w_prev == NULL)
2312 win = lastwin; /* wrap around the top */
2313 else
2314 win = win->w_prev; /* go to previous window */
2315
2316 if (IS_QF_WINDOW(win))
2317 {
2318 /* Didn't find it, go to the window before the quickfix
2319 * window. */
2320 if (altwin != NULL)
2321 win = altwin;
2322 else if (curwin->w_prev != NULL)
2323 win = curwin->w_prev;
2324 else
2325 win = curwin->w_next;
2326 break;
2327 }
2328
2329 /* Remember a usable window. */
2330 if (altwin == NULL && !win->w_p_pvw
2331 && win->w_buffer->b_p_bt[0] == NUL)
2332 altwin = win;
2333 }
2334
2335 win_goto(win);
2336 }
2337 }
2338
2339 return OK;
2340}
2341
2342/*
2343 * Edit the selected file or help file.
2344 */
2345 static int
2346qf_jump_edit_buffer(
2347 qf_info_T *qi,
2348 qfline_T *qf_ptr,
2349 int forceit,
2350 win_T *oldwin,
2351 int *opened_window,
2352 int *abort)
2353{
2354 int retval = OK;
2355
2356 if (qf_ptr->qf_type == 1)
2357 {
2358 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2359 * set b_p_ro flag). */
2360 if (!can_abandon(curbuf, forceit))
2361 {
2362 no_write_message();
2363 retval = FALSE;
2364 }
2365 else
2366 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2367 ECMD_HIDE + ECMD_SET_HELP,
2368 oldwin == curwin ? curwin : NULL);
2369 }
2370 else
2371 {
2372 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002373 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002374
2375 retval = buflist_getfile(qf_ptr->qf_fnum,
2376 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002377
2378 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002379 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002380 /*
2381 * Location list. Check whether the associated window is still
2382 * present and the list is still valid.
2383 */
2384 if (!win_valid_any_tab(oldwin))
2385 {
2386 EMSG(_("E924: Current window was closed"));
2387 *abort = TRUE;
2388 *opened_window = FALSE;
2389 }
2390 else if (!qflist_valid(oldwin, save_qfid))
2391 {
2392 EMSG(_(e_loc_list_changed));
2393 *abort = TRUE;
2394 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002395 }
2396 else if (old_qf_curlist != qi->qf_curlist
2397 || !is_qf_entry_present(qi, qf_ptr))
2398 {
2399 if (qi == &ql_info)
2400 EMSG(_("E925: Current quickfix was changed"));
2401 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002402 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002403 *abort = TRUE;
2404 }
2405
2406 if (*abort)
2407 retval = FALSE;
2408 }
2409
2410 return retval;
2411}
2412
2413/*
2414 * Goto the error line in the current file using either line/column number or a
2415 * search pattern.
2416 */
2417 static void
2418qf_jump_goto_line(
2419 linenr_T qf_lnum,
2420 int qf_col,
2421 char_u qf_viscol,
2422 char_u *qf_pattern)
2423{
2424 linenr_T i;
2425 char_u *line;
2426 colnr_T screen_col;
2427 colnr_T char_col;
2428
2429 if (qf_pattern == NULL)
2430 {
2431 /*
2432 * Go to line with error, unless qf_lnum is 0.
2433 */
2434 i = qf_lnum;
2435 if (i > 0)
2436 {
2437 if (i > curbuf->b_ml.ml_line_count)
2438 i = curbuf->b_ml.ml_line_count;
2439 curwin->w_cursor.lnum = i;
2440 }
2441 if (qf_col > 0)
2442 {
2443 curwin->w_cursor.col = qf_col - 1;
2444#ifdef FEAT_VIRTUALEDIT
2445 curwin->w_cursor.coladd = 0;
2446#endif
2447 if (qf_viscol == TRUE)
2448 {
2449 /*
2450 * Check each character from the beginning of the error
2451 * line up to the error column. For each tab character
2452 * found, reduce the error column value by the length of
2453 * a tab character.
2454 */
2455 line = ml_get_curline();
2456 screen_col = 0;
2457 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2458 {
2459 if (*line == NUL)
2460 break;
2461 if (*line++ == '\t')
2462 {
2463 curwin->w_cursor.col -= 7 - (screen_col % 8);
2464 screen_col += 8 - (screen_col % 8);
2465 }
2466 else
2467 ++screen_col;
2468 }
2469 }
2470 check_cursor();
2471 }
2472 else
2473 beginline(BL_WHITE | BL_FIX);
2474 }
2475 else
2476 {
2477 pos_T save_cursor;
2478
2479 /* Move the cursor to the first line in the buffer */
2480 save_cursor = curwin->w_cursor;
2481 curwin->w_cursor.lnum = 0;
2482 if (!do_search(NULL, '/', qf_pattern, (long)1,
2483 SEARCH_KEEP, NULL, NULL))
2484 curwin->w_cursor = save_cursor;
2485 }
2486}
2487
2488/*
2489 * Display quickfix list index and size message
2490 */
2491 static void
2492qf_jump_print_msg(
2493 qf_info_T *qi,
2494 int qf_index,
2495 qfline_T *qf_ptr,
2496 buf_T *old_curbuf,
2497 linenr_T old_lnum)
2498{
2499 linenr_T i;
2500 int len;
2501
2502 /* Update the screen before showing the message, unless the screen
2503 * scrolled up. */
2504 if (!msg_scrolled)
2505 update_topline_redraw();
2506 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2507 qi->qf_lists[qi->qf_curlist].qf_count,
2508 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2509 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2510 /* Add the message, skipping leading whitespace and newlines. */
2511 len = (int)STRLEN(IObuff);
2512 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2513
2514 /* Output the message. Overwrite to avoid scrolling when the 'O'
2515 * flag is present in 'shortmess'; But when not jumping, print the
2516 * whole message. */
2517 i = msg_scroll;
2518 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2519 msg_scroll = TRUE;
2520 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2521 msg_scroll = FALSE;
2522 msg_attr_keep(IObuff, 0, TRUE);
2523 msg_scroll = i;
2524}
2525
2526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 * jump to a quickfix line
2528 * if dir == FORWARD go "errornr" valid entries forward
2529 * if dir == BACKWARD go "errornr" valid entries backward
2530 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2531 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2532 * else if "errornr" is zero, redisplay the same line
2533 * else go to entry "errornr"
2534 */
2535 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002536qf_jump(qf_info_T *qi,
2537 int dir,
2538 int errornr,
2539 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002541 qfline_T *qf_ptr;
2542 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 buf_T *old_curbuf;
2546 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002547 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002548 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002550 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552#ifdef FEAT_FOLDING
2553 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2554#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002555 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002557 if (qi == NULL)
2558 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002559
2560 if (qi->qf_curlist >= qi->qf_listcount
2561 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 EMSG(_(e_quickfix));
2564 return;
2565 }
2566
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002567 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002569 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002571 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002573 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2574 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002576 qf_ptr = old_qf_ptr;
2577 qf_index = old_qf_index;
2578 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580 }
2581 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002582 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002584 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2585 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 /* No need to print the error message if it's visible in the error
2587 * window */
2588 print_message = FALSE;
2589
2590 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002591 * For ":helpgrep" find a help window or open one.
2592 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002593 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002594 if (jump_to_help_window(qi, &opened_window) == FAIL)
2595 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002596
2597 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 * If currently in the quickfix window, find another window to show the
2599 * file in.
2600 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002601 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
2603 /*
2604 * If there is no file specified, we don't know where to go.
2605 * But do advance, otherwise ":cn" gets stuck.
2606 */
2607 if (qf_ptr->qf_fnum == 0)
2608 goto theend;
2609
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002610 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2611 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
2614 /*
2615 * If there is a file name,
2616 * read the wanted file if needed, and check autowrite etc.
2617 */
2618 old_curbuf = curbuf;
2619 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002620
2621 if (qf_ptr->qf_fnum != 0)
2622 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002623 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002624
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002625 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2626 &opened_window, &abort);
2627 if (abort)
2628 {
2629 qi = NULL;
2630 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002631 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002632 }
2633
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002634 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 /* When not switched to another buffer, still need to set pc mark */
2637 if (curbuf == old_curbuf)
2638 setpcmark();
2639
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002640 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2641 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
2643#ifdef FEAT_FOLDING
2644 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2645 foldOpenCursor();
2646#endif
2647 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002648 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 else
2651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (opened_window)
2653 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002654 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
2656 /*
2657 * Couldn't open file, so put index back where it was. This could
2658 * happen if the file was readonly and we changed something.
2659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 qf_ptr = old_qf_ptr;
2662 qf_index = old_qf_index;
2663 }
2664 }
2665theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002666 if (qi != NULL)
2667 {
2668 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2669 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (p_swb != old_swb && opened_window)
2672 {
2673 /* Restore old 'switchbuf' value, but not when an autocommand or
2674 * modeline has changed the value. */
2675 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002676 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002678 swb_flags = old_swb_flags;
2679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 else
2681 free_string_option(old_swb);
2682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683}
2684
2685/*
2686 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002687 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
2689 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002690qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002692 buf_T *buf;
2693 char_u *fname;
2694 qfline_T *qfp;
2695 int i;
2696 int idx1 = 1;
2697 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002698 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002699 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002700 int qfFileAttr;
2701 int qfSepAttr;
2702 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002703 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002705 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002707 if (eap->cmdidx == CMD_llist)
2708 {
2709 qi = GET_LOC_LIST(curwin);
2710 if (qi == NULL)
2711 {
2712 EMSG(_(e_loclist));
2713 return;
2714 }
2715 }
2716
2717 if (qi->qf_curlist >= qi->qf_listcount
2718 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 EMSG(_(e_quickfix));
2721 return;
2722 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002723 if (*arg == '+')
2724 {
2725 ++arg;
2726 plus = TRUE;
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2729 {
2730 EMSG(_(e_trailing));
2731 return;
2732 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002733 if (plus)
2734 {
2735 i = qi->qf_lists[qi->qf_curlist].qf_index;
2736 idx2 = i + idx1;
2737 idx1 = i;
2738 }
2739 else
2740 {
2741 i = qi->qf_lists[qi->qf_curlist].qf_count;
2742 if (idx1 < 0)
2743 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2744 if (idx2 < 0)
2745 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002748 /*
2749 * Get the attributes for the different quickfix highlight items. Note
2750 * that this depends on syntax items defined in the qf.vim syntax file
2751 */
2752 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2753 if (qfFileAttr == 0)
2754 qfFileAttr = HL_ATTR(HLF_D);
2755 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2756 if (qfSepAttr == 0)
2757 qfSepAttr = HL_ATTR(HLF_D);
2758 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2759 if (qfLineAttr == 0)
2760 qfLineAttr = HL_ATTR(HLF_N);
2761
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002762 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002764 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2765 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
2767 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2768 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002769 msg_putchar('\n');
2770 if (got_int)
2771 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002772
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002773 fname = NULL;
2774 if (qfp->qf_fnum != 0
2775 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2776 {
2777 fname = buf->b_fname;
2778 if (qfp->qf_type == 1) /* :helpgrep */
2779 fname = gettail(fname);
2780 }
2781 if (fname == NULL)
2782 sprintf((char *)IObuff, "%2d", i);
2783 else
2784 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2785 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002786 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002787 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2788
2789 if (qfp->qf_lnum != 0)
2790 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002791 if (qfp->qf_lnum == 0)
2792 IObuff[0] = NUL;
2793 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002794 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002796 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002797 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002798 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002800 msg_puts_attr(IObuff, qfLineAttr);
2801 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002802 if (qfp->qf_pattern != NULL)
2803 {
2804 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002805 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002806 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002807 }
2808 msg_puts((char_u *)" ");
2809
2810 /* Remove newlines and leading whitespace from the text. For an
2811 * unrecognized line keep the indent, the compiler may mark a word
2812 * with ^^^^. */
2813 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2815 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816 msg_prt_line(IObuff, FALSE);
2817 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002819
2820 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002821 if (qfp == NULL)
2822 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002823 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 ui_breakcheck();
2825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
2827
2828/*
2829 * Remove newlines and leading whitespace from an error message.
2830 * Put the result in "buf[bufsize]".
2831 */
2832 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002833qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 int i;
2836 char_u *p = text;
2837
2838 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2839 {
2840 if (*p == '\n')
2841 {
2842 buf[i] = ' ';
2843 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002844 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 break;
2846 }
2847 else
2848 buf[i] = *p++;
2849 }
2850 buf[i] = NUL;
2851}
2852
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002853 static void
2854qf_msg(qf_info_T *qi, int which, char *lead)
2855{
2856 char *title = (char *)qi->qf_lists[which].qf_title;
2857 int count = qi->qf_lists[which].qf_count;
2858 char_u buf[IOSIZE];
2859
2860 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2861 lead,
2862 which + 1,
2863 qi->qf_listcount,
2864 count);
2865
2866 if (title != NULL)
2867 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002868 size_t len = STRLEN(buf);
2869
2870 if (len < 34)
2871 {
2872 vim_memset(buf + len, ' ', 34 - len);
2873 buf[34] = NUL;
2874 }
2875 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002876 }
2877 trunc_string(buf, buf, Columns - 1, IOSIZE);
2878 msg(buf);
2879}
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881/*
2882 * ":colder [count]": Up in the quickfix stack.
2883 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002884 * ":lolder [count]": Up in the location list stack.
2885 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 */
2887 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002888qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 int count;
2892
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002893 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2894 {
2895 qi = GET_LOC_LIST(curwin);
2896 if (qi == NULL)
2897 {
2898 EMSG(_(e_loclist));
2899 return;
2900 }
2901 }
2902
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 if (eap->addr_count != 0)
2904 count = eap->line2;
2905 else
2906 count = 1;
2907 while (count--)
2908 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002909 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
2913 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002914 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002916 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 else
2919 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002920 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002923 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002925 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002928 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002929 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930}
2931
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002932 void
2933qf_history(exarg_T *eap)
2934{
2935 qf_info_T *qi = &ql_info;
2936 int i;
2937
2938 if (eap->cmdidx == CMD_lhistory)
2939 qi = GET_LOC_LIST(curwin);
2940 if (qi == NULL || (qi->qf_listcount == 0
2941 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2942 MSG(_("No entries"));
2943 else
2944 for (i = 0; i < qi->qf_listcount; ++i)
2945 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2946}
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002949 * Free all the entries in the error list "idx". Note that other information
2950 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 */
2952 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002953qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002955 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002956 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002957 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002958 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002960 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002962 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002963 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002964 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002965 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002966 vim_free(qfp->qf_text);
2967 stop = (qfp == qfpnext);
2968 vim_free(qfp->qf_pattern);
2969 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002970 if (stop)
2971 /* Somehow qf_count may have an incorrect value, set it to 1
2972 * to avoid crashing when it's wrong.
2973 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002974 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002975 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002976 qfl->qf_start = qfpnext;
2977 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002979
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002980 qfl->qf_index = 0;
2981 qfl->qf_start = NULL;
2982 qfl->qf_last = NULL;
2983 qfl->qf_ptr = NULL;
2984 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002985
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002986 qf_clean_dir_stack(&qfl->qf_dir_stack);
2987 qfl->qf_directory = NULL;
2988 qf_clean_dir_stack(&qfl->qf_file_stack);
2989 qfl->qf_currfile = NULL;
2990 qfl->qf_multiline = FALSE;
2991 qfl->qf_multiignore = FALSE;
2992 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993}
2994
2995/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002996 * Free error list "idx". Frees all the entries in the quickfix list,
2997 * associated context information and the title.
2998 */
2999 static void
3000qf_free(qf_info_T *qi, int idx)
3001{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003002 qf_list_T *qfl = &qi->qf_lists[idx];
3003
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003004 qf_free_items(qi, idx);
3005
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003006 vim_free(qfl->qf_title);
3007 qfl->qf_title = NULL;
3008 free_tv(qfl->qf_ctx);
3009 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003010 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003011 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003012}
3013
3014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 * qf_mark_adjust: adjust marks
3016 */
3017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003018qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003019 win_T *wp,
3020 linenr_T line1,
3021 linenr_T line2,
3022 long amount,
3023 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003025 int i;
3026 qfline_T *qfp;
3027 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003028 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003029 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003030 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
Bram Moolenaarc1542742016-07-20 21:44:37 +02003032 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003033 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003034 if (wp != NULL)
3035 {
3036 if (wp->w_llist == NULL)
3037 return;
3038 qi = wp->w_llist;
3039 }
3040
3041 for (idx = 0; idx < qi->qf_listcount; ++idx)
3042 if (qi->qf_lists[idx].qf_count)
3043 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003044 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3045 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 if (qfp->qf_fnum == curbuf->b_fnum)
3047 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003048 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3050 {
3051 if (amount == MAXLNUM)
3052 qfp->qf_cleared = TRUE;
3053 else
3054 qfp->qf_lnum += amount;
3055 }
3056 else if (amount_after && qfp->qf_lnum > line2)
3057 qfp->qf_lnum += amount_after;
3058 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003059
3060 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003061 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062}
3063
3064/*
3065 * Make a nice message out of the error character and the error number:
3066 * char number message
3067 * e or E 0 " error"
3068 * w or W 0 " warning"
3069 * i or I 0 " info"
3070 * 0 0 ""
3071 * other 0 " c"
3072 * e or E n " error n"
3073 * w or W n " warning n"
3074 * i or I n " info n"
3075 * 0 n " error n"
3076 * other n " c n"
3077 * 1 x "" :helpgrep
3078 */
3079 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003080qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 static char_u buf[20];
3083 static char_u cc[3];
3084 char_u *p;
3085
3086 if (c == 'W' || c == 'w')
3087 p = (char_u *)" warning";
3088 else if (c == 'I' || c == 'i')
3089 p = (char_u *)" info";
3090 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3091 p = (char_u *)" error";
3092 else if (c == 0 || c == 1)
3093 p = (char_u *)"";
3094 else
3095 {
3096 cc[0] = ' ';
3097 cc[1] = c;
3098 cc[2] = NUL;
3099 p = cc;
3100 }
3101
3102 if (nr <= 0)
3103 return p;
3104
3105 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3106 return buf;
3107}
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109/*
3110 * ":cwindow": open the quickfix window if we have errors to display,
3111 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003112 * ":lwindow": open the location list window if we have locations to display,
3113 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 */
3115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003116ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003118 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 win_T *win;
3120
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003121 if (eap->cmdidx == CMD_lwindow)
3122 {
3123 qi = GET_LOC_LIST(curwin);
3124 if (qi == NULL)
3125 return;
3126 }
3127
3128 /* Look for an existing quickfix window. */
3129 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
3131 /*
3132 * If a quickfix window is open but we have no errors to display,
3133 * close the window. If a quickfix window is not open, then open
3134 * it if we have errors; otherwise, leave it closed.
3135 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003136 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003137 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003138 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 if (win != NULL)
3141 ex_cclose(eap);
3142 }
3143 else if (win == NULL)
3144 ex_copen(eap);
3145}
3146
3147/*
3148 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003149 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003152ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003154 win_T *win = NULL;
3155 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003157 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3158 {
3159 qi = GET_LOC_LIST(curwin);
3160 if (qi == NULL)
3161 return;
3162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003164 /* Find existing quickfix window and close it. */
3165 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (win != NULL)
3167 win_close(win, FALSE);
3168}
3169
3170/*
3171 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003172 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 */
3174 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003175ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003177 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003180 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003181 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003182 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003184 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3185 {
3186 qi = GET_LOC_LIST(curwin);
3187 if (qi == NULL)
3188 {
3189 EMSG(_(e_loclist));
3190 return;
3191 }
3192 }
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (eap->addr_count != 0)
3195 height = eap->line2;
3196 else
3197 height = QF_WINHEIGHT;
3198
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200#ifdef FEAT_GUI
3201 need_mouse_correct = TRUE;
3202#endif
3203
3204 /*
3205 * Find existing quickfix window, or open a new one.
3206 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003207 win = qf_find_win(qi);
3208
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003209 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003212 if (eap->addr_count != 0)
3213 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003214 if (cmdmod.split & WSP_VERT)
3215 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003216 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003217 win_setwidth(height);
3218 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003219 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003220 win_setheight(height);
3221 }
3222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 else
3224 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00003225 qf_buf = qf_find_buf(qi);
3226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 /* The current window becomes the previous window afterwards. */
3228 win = curwin;
3229
Bram Moolenaar77642c02012-11-20 17:55:10 +01003230 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3231 && cmdmod.split == 0)
3232 /* Create the new window at the very bottom, except when
3233 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003234 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003235 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003237 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003239 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003241 /*
3242 * For the location list window, create a reference to the
3243 * location list from the window 'win'.
3244 */
3245 curwin->w_llist_ref = win->w_llist;
3246 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003248
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003249 if (oldwin != curwin)
3250 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003251 if (qf_buf != NULL)
3252 /* Use the existing quickfix buffer */
3253 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003254 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003255 else
3256 {
3257 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003258 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003259 /* switch off 'swapfile' */
3260 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3261 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003262 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003263 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003264 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003265#ifdef FEAT_DIFF
3266 curwin->w_p_diff = FALSE;
3267#endif
3268#ifdef FEAT_FOLDING
3269 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3270 OPT_LOCAL);
3271#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003274 /* Only set the height when still in the same tab page and there is no
3275 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003276 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 win_setheight(height);
3278 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3279 if (win_valid(win))
3280 prevwin = win;
3281 }
3282
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003283 qf_set_title_var(qi);
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 /*
3286 * Fill the buffer with the quickfix list.
3287 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003288 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003290 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 curwin->w_cursor.col = 0;
3292 check_cursor();
3293 update_topline(); /* scroll to show the line */
3294}
3295
3296/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003297 * Move the cursor in the quickfix window to "lnum".
3298 */
3299 static void
3300qf_win_goto(win_T *win, linenr_T lnum)
3301{
3302 win_T *old_curwin = curwin;
3303
3304 curwin = win;
3305 curbuf = win->w_buffer;
3306 curwin->w_cursor.lnum = lnum;
3307 curwin->w_cursor.col = 0;
3308#ifdef FEAT_VIRTUALEDIT
3309 curwin->w_cursor.coladd = 0;
3310#endif
3311 curwin->w_curswant = 0;
3312 update_topline(); /* scroll to show the line */
3313 redraw_later(VALID);
3314 curwin->w_redr_status = TRUE; /* update ruler */
3315 curwin = old_curwin;
3316 curbuf = curwin->w_buffer;
3317}
3318
3319/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003320 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003321 */
3322 void
3323ex_cbottom(exarg_T *eap UNUSED)
3324{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003325 qf_info_T *qi = &ql_info;
3326 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003327
Bram Moolenaar537ef082016-07-09 17:56:19 +02003328 if (eap->cmdidx == CMD_lbottom)
3329 {
3330 qi = GET_LOC_LIST(curwin);
3331 if (qi == NULL)
3332 {
3333 EMSG(_(e_loclist));
3334 return;
3335 }
3336 }
3337
3338 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003339 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3340 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3341}
3342
3343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 * Return the number of the current entry (line number in the quickfix
3345 * window).
3346 */
3347 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003348qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003350 qf_info_T *qi = &ql_info;
3351
3352 if (IS_LL_WINDOW(wp))
3353 /* In the location list window, use the referenced location list */
3354 qi = wp->w_llist_ref;
3355
3356 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357}
3358
3359/*
3360 * Update the cursor position in the quickfix window to the current error.
3361 * Return TRUE if there is a quickfix window.
3362 */
3363 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003364qf_win_pos_update(
3365 qf_info_T *qi,
3366 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
3368 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003369 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
3371 /*
3372 * Put the cursor on the current error in the quickfix window, so that
3373 * it's viewable.
3374 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003375 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 if (win != NULL
3377 && qf_index <= win->w_buffer->b_ml.ml_line_count
3378 && old_qf_index != qf_index)
3379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 if (qf_index > old_qf_index)
3381 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003382 win->w_redraw_top = old_qf_index;
3383 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 else
3386 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003387 win->w_redraw_top = qf_index;
3388 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003390 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392 return win != NULL;
3393}
3394
3395/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003396 * Check whether the given window is displaying the specified quickfix/location
3397 * list buffer
3398 */
3399 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003400is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003401{
3402 /*
3403 * A window displaying the quickfix buffer will have the w_llist_ref field
3404 * set to NULL.
3405 * A window displaying a location list buffer will have the w_llist_ref
3406 * pointing to the location list.
3407 */
3408 if (bt_quickfix(win->w_buffer))
3409 if ((qi == &ql_info && win->w_llist_ref == NULL)
3410 || (qi != &ql_info && win->w_llist_ref == qi))
3411 return TRUE;
3412
3413 return FALSE;
3414}
3415
3416/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003417 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003418 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003419 */
3420 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003421qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003422{
3423 win_T *win;
3424
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003425 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003426 if (is_qf_win(win, qi))
3427 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003428
3429 return win;
3430}
3431
3432/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003433 * Find a quickfix buffer.
3434 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 */
3436 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003437qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003439 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003440 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
Bram Moolenaar9c102382006-05-03 21:26:49 +00003442 FOR_ALL_TAB_WINDOWS(tp, win)
3443 if (is_qf_win(win, qi))
3444 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003445
Bram Moolenaar9c102382006-05-03 21:26:49 +00003446 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447}
3448
3449/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003450 * Update the w:quickfix_title variable in the quickfix/location list window
3451 */
3452 static void
3453qf_update_win_titlevar(qf_info_T *qi)
3454{
3455 win_T *win;
3456 win_T *curwin_save;
3457
3458 if ((win = qf_find_win(qi)) != NULL)
3459 {
3460 curwin_save = curwin;
3461 curwin = win;
3462 qf_set_title_var(qi);
3463 curwin = curwin_save;
3464 }
3465}
3466
3467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 * Find the quickfix buffer. If it exists, update the contents.
3469 */
3470 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003471qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003474 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
3477 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003478 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (buf != NULL)
3480 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003481 linenr_T old_line_count = buf->b_ml.ml_line_count;
3482
3483 if (old_last == NULL)
3484 /* set curwin/curbuf to buf and save a few things */
3485 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaard823fa92016-08-12 16:29:27 +02003487 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003488
Bram Moolenaar864293a2016-06-02 13:40:04 +02003489 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003490 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003491
Bram Moolenaar864293a2016-06-02 13:40:04 +02003492 if (old_last == NULL)
3493 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003494 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003495
3496 /* restore curwin/curbuf and a few other things */
3497 aucmd_restbuf(&aco);
3498 }
3499
3500 /* Only redraw when added lines are visible. This avoids flickering
3501 * when the added lines are not visible. */
3502 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3503 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505}
3506
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003507/*
3508 * Set "w:quickfix_title" if "qi" has a title.
3509 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003510 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003511qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003512{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003513 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3514 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003515 qi->qf_lists[qi->qf_curlist].qf_title);
3516}
3517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518/*
3519 * Fill current buffer with quickfix errors, replacing any previous contents.
3520 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003521 * If "old_last" is not NULL append the items after this one.
3522 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3523 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 */
3525 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003526qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003528 linenr_T lnum;
3529 qfline_T *qfp;
3530 buf_T *errbuf;
3531 int len;
3532 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaar864293a2016-06-02 13:40:04 +02003534 if (old_last == NULL)
3535 {
3536 if (buf != curbuf)
3537 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003538 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003539 return;
3540 }
3541
3542 /* delete all existing lines */
3543 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3544 (void)ml_delete((linenr_T)1, FALSE);
3545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
3547 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003548 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003551 if (old_last == NULL)
3552 {
3553 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3554 lnum = 0;
3555 }
3556 else
3557 {
3558 qfp = old_last->qf_next;
3559 lnum = buf->b_ml.ml_line_count;
3560 }
3561 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
3563 if (qfp->qf_fnum != 0
3564 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3565 && errbuf->b_fname != NULL)
3566 {
3567 if (qfp->qf_type == 1) /* :helpgrep */
3568 STRCPY(IObuff, gettail(errbuf->b_fname));
3569 else
3570 STRCPY(IObuff, errbuf->b_fname);
3571 len = (int)STRLEN(IObuff);
3572 }
3573 else
3574 len = 0;
3575 IObuff[len++] = '|';
3576
3577 if (qfp->qf_lnum > 0)
3578 {
3579 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3580 len += (int)STRLEN(IObuff + len);
3581
3582 if (qfp->qf_col > 0)
3583 {
3584 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3585 len += (int)STRLEN(IObuff + len);
3586 }
3587
3588 sprintf((char *)IObuff + len, "%s",
3589 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3590 len += (int)STRLEN(IObuff + len);
3591 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003592 else if (qfp->qf_pattern != NULL)
3593 {
3594 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3595 len += (int)STRLEN(IObuff + len);
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 IObuff[len++] = '|';
3598 IObuff[len++] = ' ';
3599
3600 /* Remove newlines and leading whitespace from the text.
3601 * For an unrecognized line keep the indent, the compiler may
3602 * mark a word with ^^^^. */
3603 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3604 IObuff + len, IOSIZE - len);
3605
Bram Moolenaar864293a2016-06-02 13:40:04 +02003606 if (ml_append_buf(buf, lnum, IObuff,
3607 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003609 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003611 if (qfp == NULL)
3612 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003614
3615 if (old_last == NULL)
3616 /* Delete the empty line which is now at the end */
3617 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619
3620 /* correct cursor position */
3621 check_lnums(TRUE);
3622
Bram Moolenaar864293a2016-06-02 13:40:04 +02003623 if (old_last == NULL)
3624 {
3625 /* Set the 'filetype' to "qf" each time after filling the buffer.
3626 * This resembles reading a file into a buffer, it's more logical when
3627 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003628#ifdef FEAT_AUTOCMD
3629 ++curbuf_lock;
3630#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003631 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3632 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003635 keep_filetype = TRUE; /* don't detect 'filetype' */
3636 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003638 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003640 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003641 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003643 /* make sure it will be redrawn */
3644 redraw_curbuf_later(NOT_VALID);
3645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 /* Restore KeyTyped, setting 'filetype' may reset it. */
3648 KeyTyped = old_KeyTyped;
3649}
3650
Bram Moolenaarb254af32017-12-18 19:48:58 +01003651 static void
3652qf_list_changed(qf_info_T *qi, int qf_idx)
3653{
3654 qi->qf_lists[qf_idx].qf_changedtick++;
3655}
3656
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003658 * Return TRUE when using ":vimgrep" for ":grep".
3659 */
3660 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003661grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003662{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003663 return ((cmdidx == CMD_grep
3664 || cmdidx == CMD_lgrep
3665 || cmdidx == CMD_grepadd
3666 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003667 && STRCMP("internal",
3668 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3669}
3670
3671/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003672 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 */
3674 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003675ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003677 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003679 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003681 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003682 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003683 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003684#ifdef FEAT_AUTOCMD
3685 char_u *au_name = NULL;
3686
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003687 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3688 if (grep_internal(eap->cmdidx))
3689 {
3690 ex_vimgrep(eap);
3691 return;
3692 }
3693
Bram Moolenaar7c626922005-02-07 22:01:03 +00003694 switch (eap->cmdidx)
3695 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003696 case CMD_make: au_name = (char_u *)"make"; break;
3697 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3698 case CMD_grep: au_name = (char_u *)"grep"; break;
3699 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3700 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3701 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003702 default: break;
3703 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003704 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3705 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003706 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003707# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003708 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003709 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003710# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003711 }
3712#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003713#ifdef FEAT_MBYTE
3714 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
Bram Moolenaara6557602006-02-04 22:43:20 +00003717 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3718 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003719 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003722 fname = get_mef_name();
3723 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003725 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
3727 /*
3728 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3729 */
3730 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3731 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003732 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 cmd = alloc(len);
3734 if (cmd == NULL)
3735 return;
3736 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3737 (char *)p_shq);
3738 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003739 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 /*
3741 * Output a newline if there's something else than the :make command that
3742 * was typed (in which case the cursor is in column 0).
3743 */
3744 if (msg_col == 0)
3745 msg_didout = FALSE;
3746 msg_start();
3747 MSG_PUTS(":!");
3748 msg_outtrans(cmd); /* show what we are doing */
3749
3750 /* let the shell know if we are redirecting output or not */
3751 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3752
3753#ifdef AMIGA
3754 out_flush();
3755 /* read window status report and redraw before message */
3756 (void)char_avail();
3757#endif
3758
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003759 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003760 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3761 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003762 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003763 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003764 if (wp != NULL)
3765 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01003766 if (res >= 0 && qi != NULL)
3767 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003768#ifdef FEAT_AUTOCMD
3769 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003770 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003771 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3772 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003773 if (qi->qf_curlist < qi->qf_listcount)
3774 res = qi->qf_lists[qi->qf_curlist].qf_count;
3775 else
3776 res = 0;
3777 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003778#endif
3779 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003780 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
Bram Moolenaar7c626922005-02-07 22:01:03 +00003782 mch_remove(fname);
3783 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 vim_free(cmd);
3785}
3786
3787/*
3788 * Return the name for the errorfile, in allocated memory.
3789 * Find a new unique name when 'makeef' contains "##".
3790 * Returns NULL for error.
3791 */
3792 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003793get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794{
3795 char_u *p;
3796 char_u *name;
3797 static int start = -1;
3798 static int off = 0;
3799#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003800 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801#endif
3802
3803 if (*p_mef == NUL)
3804 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003805 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (name == NULL)
3807 EMSG(_(e_notmp));
3808 return name;
3809 }
3810
3811 for (p = p_mef; *p; ++p)
3812 if (p[0] == '#' && p[1] == '#')
3813 break;
3814
3815 if (*p == NUL)
3816 return vim_strsave(p_mef);
3817
3818 /* Keep trying until the name doesn't exist yet. */
3819 for (;;)
3820 {
3821 if (start == -1)
3822 start = mch_get_pid();
3823 else
3824 off += 19;
3825
3826 name = alloc((unsigned)STRLEN(p_mef) + 30);
3827 if (name == NULL)
3828 break;
3829 STRCPY(name, p_mef);
3830 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3831 STRCAT(name, p + 2);
3832 if (mch_getperm(name) < 0
3833#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003834 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 && mch_lstat((char *)name, &sb) < 0
3836#endif
3837 )
3838 break;
3839 vim_free(name);
3840 }
3841 return name;
3842}
3843
3844/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003845 * Returns the number of valid entries in the current quickfix/location list.
3846 */
3847 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003848qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003849{
3850 qf_info_T *qi = &ql_info;
3851 qfline_T *qfp;
3852 int i, sz = 0;
3853 int prev_fnum = 0;
3854
3855 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3856 {
3857 /* Location list */
3858 qi = GET_LOC_LIST(curwin);
3859 if (qi == NULL)
3860 return 0;
3861 }
3862
3863 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003864 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003865 ++i, qfp = qfp->qf_next)
3866 {
3867 if (qfp->qf_valid)
3868 {
3869 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3870 sz++; /* Count all valid entries */
3871 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3872 {
3873 /* Count the number of files */
3874 sz++;
3875 prev_fnum = qfp->qf_fnum;
3876 }
3877 }
3878 }
3879
3880 return sz;
3881}
3882
3883/*
3884 * Returns the current index of the quickfix/location list.
3885 * Returns 0 if there is an error.
3886 */
3887 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003888qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003889{
3890 qf_info_T *qi = &ql_info;
3891
3892 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3893 {
3894 /* Location list */
3895 qi = GET_LOC_LIST(curwin);
3896 if (qi == NULL)
3897 return 0;
3898 }
3899
3900 return qi->qf_lists[qi->qf_curlist].qf_index;
3901}
3902
3903/*
3904 * Returns the current index in the quickfix/location list (counting only valid
3905 * entries). If no valid entries are in the list, then returns 1.
3906 */
3907 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003908qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003909{
3910 qf_info_T *qi = &ql_info;
3911 qf_list_T *qfl;
3912 qfline_T *qfp;
3913 int i, eidx = 0;
3914 int prev_fnum = 0;
3915
3916 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3917 {
3918 /* Location list */
3919 qi = GET_LOC_LIST(curwin);
3920 if (qi == NULL)
3921 return 1;
3922 }
3923
3924 qfl = &qi->qf_lists[qi->qf_curlist];
3925 qfp = qfl->qf_start;
3926
3927 /* check if the list has valid errors */
3928 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3929 return 1;
3930
3931 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3932 {
3933 if (qfp->qf_valid)
3934 {
3935 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3936 {
3937 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3938 {
3939 /* Count the number of files */
3940 eidx++;
3941 prev_fnum = qfp->qf_fnum;
3942 }
3943 }
3944 else
3945 eidx++;
3946 }
3947 }
3948
3949 return eidx ? eidx : 1;
3950}
3951
3952/*
3953 * Get the 'n'th valid error entry in the quickfix or location list.
3954 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3955 * For :cdo and :ldo returns the 'n'th valid error entry.
3956 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3957 */
3958 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003959qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003960{
3961 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3962 qfline_T *qfp = qfl->qf_start;
3963 int i, eidx;
3964 int prev_fnum = 0;
3965
3966 /* check if the list has valid errors */
3967 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3968 return 1;
3969
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003970 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003971 i++, qfp = qfp->qf_next)
3972 {
3973 if (qfp->qf_valid)
3974 {
3975 if (fdo)
3976 {
3977 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3978 {
3979 /* Count the number of files */
3980 eidx++;
3981 prev_fnum = qfp->qf_fnum;
3982 }
3983 }
3984 else
3985 eidx++;
3986 }
3987
3988 if (eidx == n)
3989 break;
3990 }
3991
3992 if (i <= qfl->qf_count)
3993 return i;
3994 else
3995 return 1;
3996}
3997
3998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004000 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004001 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 */
4003 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004004ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004006 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004007 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004008
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004009 if (eap->cmdidx == CMD_ll
4010 || eap->cmdidx == CMD_lrewind
4011 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004012 || eap->cmdidx == CMD_llast
4013 || eap->cmdidx == CMD_ldo
4014 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004015 {
4016 qi = GET_LOC_LIST(curwin);
4017 if (qi == NULL)
4018 {
4019 EMSG(_(e_loclist));
4020 return;
4021 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004022 }
4023
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004024 if (eap->addr_count > 0)
4025 errornr = (int)eap->line2;
4026 else
4027 {
4028 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4029 errornr = 0;
4030 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4031 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4032 errornr = 1;
4033 else
4034 errornr = 32767;
4035 }
4036
4037 /* For cdo and ldo commands, jump to the nth valid error.
4038 * For cfdo and lfdo commands, jump to the nth valid file entry.
4039 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004040 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4041 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004042 errornr = qf_get_nth_valid_entry(qi,
4043 eap->addr_count > 0 ? (int)eap->line1 : 1,
4044 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4045
4046 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047}
4048
4049/*
4050 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004051 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004052 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 */
4054 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004055ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004057 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004058 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004059
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004060 if (eap->cmdidx == CMD_lnext
4061 || eap->cmdidx == CMD_lNext
4062 || eap->cmdidx == CMD_lprevious
4063 || eap->cmdidx == CMD_lnfile
4064 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004065 || eap->cmdidx == CMD_lpfile
4066 || eap->cmdidx == CMD_ldo
4067 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004068 {
4069 qi = GET_LOC_LIST(curwin);
4070 if (qi == NULL)
4071 {
4072 EMSG(_(e_loclist));
4073 return;
4074 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004075 }
4076
Bram Moolenaar55b69262017-08-13 13:42:01 +02004077 if (eap->addr_count > 0
4078 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4079 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004080 errornr = (int)eap->line2;
4081 else
4082 errornr = 1;
4083
4084 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4085 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004087 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4088 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004090 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4091 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 ? BACKWARD_FILE
4093 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004094 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095}
4096
4097/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004098 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004099 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 */
4101 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004102ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004104 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004105 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004106 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004107#ifdef FEAT_AUTOCMD
4108 char_u *au_name = NULL;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004109 int save_qfid;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004110#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004111 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004112
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004113#ifdef FEAT_AUTOCMD
4114 switch (eap->cmdidx)
4115 {
4116 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4117 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4118 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4119 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4120 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4121 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4122 default: break;
4123 }
4124 if (au_name != NULL)
4125 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
4126#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004127#ifdef FEAT_MBYTE
4128 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4129#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004130#ifdef FEAT_BROWSE
4131 if (cmdmod.browse)
4132 {
4133 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4134 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4135 if (browse_file == NULL)
4136 return;
4137 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4138 vim_free(browse_file);
4139 }
4140 else
4141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004143 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004144
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004145 if (eap->cmdidx == CMD_lfile
4146 || eap->cmdidx == CMD_lgetfile
4147 || eap->cmdidx == CMD_laddfile)
4148 wp = curwin;
4149
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004150 /*
4151 * This function is used by the :cfile, :cgetfile and :caddfile
4152 * commands.
4153 * :cfile always creates a new quickfix list and jumps to the
4154 * first error.
4155 * :cgetfile creates a new quickfix list but doesn't jump to the
4156 * first error.
4157 * :caddfile adds to an existing quickfix list. If there is no
4158 * quickfix list then a new list is created.
4159 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004160 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4161 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004162 if (wp != NULL)
4163 qi = GET_LOC_LIST(wp);
4164 if (res >= 0 && qi != NULL)
4165 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004166#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004167 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004168 if (au_name != NULL)
4169 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004170 /*
4171 * Autocmd might have freed the quickfix/location list. Check whether it is
4172 * still valid
4173 */
4174 if (!qflist_valid(wp, save_qfid))
4175 return;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004176#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004177 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
4178 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004179 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181}
4182
4183/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004184 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004185 * ":vimgrepadd {pattern} file(s)"
4186 * ":lvimgrep {pattern} file(s)"
4187 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004188 */
4189 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004190ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004191{
Bram Moolenaar81695252004-12-29 20:58:21 +00004192 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004193 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004194 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004195 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004196 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004197 char_u *s;
4198 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004199 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004200 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004201 int loclist_cmd = FALSE;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004202#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004203 int_u save_qfid;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004204 qfline_T *cur_qf_start;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004205 win_T *wp;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004206#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004207 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004208 buf_T *buf;
4209 int duplicate_name = FALSE;
4210 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004211 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004212 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004213 buf_T *first_match_buf = NULL;
4214 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004215 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004216#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4217 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004218#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004219 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004220 int flags = 0;
4221 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004222 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004223 char_u *dirname_start = NULL;
4224 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004225 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004226#ifdef FEAT_AUTOCMD
4227 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004228
4229 switch (eap->cmdidx)
4230 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004231 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4232 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4233 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004234 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004235 case CMD_grep: au_name = (char_u *)"grep"; break;
4236 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4237 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4238 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004239 default: break;
4240 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004241 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4242 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004243 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004244# ifdef FEAT_EVAL
4245 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004246 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004247# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004248 }
4249#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004250
Bram Moolenaar754b5602006-02-09 23:53:20 +00004251 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004252 || eap->cmdidx == CMD_lvimgrep
4253 || eap->cmdidx == CMD_lgrepadd
4254 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004255 {
4256 qi = ll_get_or_alloc_list(curwin);
4257 if (qi == NULL)
4258 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004259 loclist_cmd = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00004260 }
4261
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004262 if (eap->addr_count > 0)
4263 tomatch = eap->line2;
4264 else
4265 tomatch = MAXLNUM;
4266
Bram Moolenaar81695252004-12-29 20:58:21 +00004267 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004268 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004269 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004270 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004271 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004272 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004273 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004274 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004275 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004276
4277 if (s != NULL && *s == NUL)
4278 {
4279 /* Pattern is empty, use last search pattern. */
4280 if (last_search_pat() == NULL)
4281 {
4282 EMSG(_(e_noprevre));
4283 goto theend;
4284 }
4285 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4286 }
4287 else
4288 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4289
Bram Moolenaar86b68352004-12-27 21:59:20 +00004290 if (regmatch.regprog == NULL)
4291 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004292 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004293 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004294
4295 p = skipwhite(p);
4296 if (*p == NUL)
4297 {
4298 EMSG(_("E683: File name missing or invalid pattern"));
4299 goto theend;
4300 }
4301
Bram Moolenaar55b69262017-08-13 13:42:01 +02004302 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4303 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004304 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004305 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004306 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004307
4308 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004309 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004310 goto theend;
4311 if (fcount == 0)
4312 {
4313 EMSG(_(e_nomatch));
4314 goto theend;
4315 }
4316
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004317 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4318 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004319 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004320 {
4321 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004322 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004323 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004324
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004325 /* Remember the current directory, because a BufRead autocommand that does
4326 * ":lcd %:p:h" changes the meaning of short path names. */
4327 mch_dirname(dirname_start, MAXPATHL);
4328
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004329#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004330 /* Remember the current values of the quickfix list and qf_start, so that
4331 * we can check for autocommands changing the current quickfix list. */
4332 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004333 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4334#endif
4335
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004336 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004337 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004338 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004339 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004340 if (time(NULL) > seconds)
4341 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004342 /* Display the file name every second or so, show the user we are
4343 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004344 seconds = time(NULL);
4345 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004346 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004347 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004348 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004349 else
4350 {
4351 msg_outtrans(p);
4352 vim_free(p);
4353 }
4354 msg_clr_eos();
4355 msg_didout = FALSE; /* overwrite this message */
4356 msg_nowait = TRUE; /* don't wait for this message */
4357 msg_col = 0;
4358 out_flush();
4359 }
4360
Bram Moolenaar81695252004-12-29 20:58:21 +00004361 buf = buflist_findname_exp(fnames[fi]);
4362 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4363 {
4364 /* Remember that a buffer with this name already exists. */
4365 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004366 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004367 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004368
4369#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4370 /* Don't do Filetype autocommands to avoid loading syntax and
4371 * indent scripts, a great speed improvement. */
4372 save_ei = au_event_disable(",Filetype");
4373#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004374 /* Don't use modelines here, it's useless. */
4375 save_mls = p_mls;
4376 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004377
4378 /* Load file into a buffer, so that 'fileencoding' is detected,
4379 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004380 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004381
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004382 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004383#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4384 au_event_restore(save_ei);
4385#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004386 }
4387 else
4388 /* Use existing, loaded buffer. */
4389 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004390
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004391#ifdef FEAT_AUTOCMD
Bram Moolenaar3c097222017-12-21 20:54:49 +01004392 if (loclist_cmd)
4393 {
4394 /*
4395 * Verify that the location list is still valid. An autocmd might
4396 * have freed the location list.
4397 */
4398 if (!qflist_valid(curwin, save_qfid))
4399 {
4400 EMSG(_(e_loc_list_changed));
4401 goto theend;
4402 }
4403 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004404 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4405 {
4406 int idx;
4407
4408 /* Autocommands changed the quickfix list. Find the one we were
4409 * using and restore it. */
4410 for (idx = 0; idx < LISTCOUNT; ++idx)
4411 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4412 {
4413 qi->qf_curlist = idx;
4414 break;
4415 }
4416 if (idx == LISTCOUNT)
4417 {
4418 /* List cannot be found, create a new one. */
4419 qf_new_list(qi, *eap->cmdlinep);
4420 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4421 }
4422 }
4423#endif
4424
Bram Moolenaar81695252004-12-29 20:58:21 +00004425 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004426 {
4427 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004428 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004429 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004430 else
4431 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004432 /* Try for a match in all lines of the buffer.
4433 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004434 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004435 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4436 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004437 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004438 col = 0;
4439 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004440 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004441 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004442 /* Pass the buffer number so that it gets used even for a
4443 * dummy buffer, unless duplicate_name is set, then the
4444 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004445 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004446 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004447 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004448 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004449 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004450 ml_get_buf(buf,
4451 regmatch.startpos[0].lnum + lnum, FALSE),
4452 regmatch.startpos[0].lnum + lnum,
4453 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004454 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004455 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004456 0, /* nr */
4457 0, /* type */
4458 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004459 ) == FAIL)
4460 {
4461 got_int = TRUE;
4462 break;
4463 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004464 found_match = TRUE;
4465 if (--tomatch == 0)
4466 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004467 if ((flags & VGR_GLOBAL) == 0
4468 || regmatch.endpos[0].lnum > 0)
4469 break;
4470 col = regmatch.endpos[0].col
4471 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004472 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004473 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004474 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004475 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004476 if (got_int)
4477 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004478 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004479#ifdef FEAT_AUTOCMD
4480 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4481#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004482
4483 if (using_dummy)
4484 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004485 if (found_match && first_match_buf == NULL)
4486 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004487 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004488 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004489 /* Never keep a dummy buffer if there is another buffer
4490 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004491 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004492 buf = NULL;
4493 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004494 else if (!cmdmod.hide
4495 || buf->b_p_bh[0] == 'u' /* "unload" */
4496 || buf->b_p_bh[0] == 'w' /* "wipe" */
4497 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004498 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004499 /* When no match was found we don't need to remember the
4500 * buffer, wipe it out. If there was a match and it
4501 * wasn't the first one or we won't jump there: only
4502 * unload the buffer.
4503 * Ignore 'hidden' here, because it may lead to having too
4504 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004505 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004506 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004507 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004508 buf = NULL;
4509 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004510 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004511 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004512 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004513 /* Keeping the buffer, remove the dummy flag. */
4514 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004515 buf = NULL;
4516 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004517 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004518
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004519 if (buf != NULL)
4520 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004521 /* Keeping the buffer, remove the dummy flag. */
4522 buf->b_flags &= ~BF_DUMMY;
4523
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004524 /* If the buffer is still loaded we need to use the
4525 * directory we jumped to below. */
4526 if (buf == first_match_buf
4527 && target_dir == NULL
4528 && STRCMP(dirname_start, dirname_now) != 0)
4529 target_dir = vim_strsave(dirname_now);
4530
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004531 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004532 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004533 * need to be done (again). But not the window-local
4534 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004535 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004536#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004537 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4538 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004539#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004540 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004541 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004542 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004543 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004544 }
4545 }
4546
4547 FreeWild(fcount, fnames);
4548
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004549 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4550 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4551 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004552 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004553
Bram Moolenaar864293a2016-06-02 13:40:04 +02004554 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004555
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004556#ifdef FEAT_AUTOCMD
4557 if (au_name != NULL)
4558 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4559 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004560 /*
4561 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4562 * is still valid.
4563 */
4564 wp = loclist_cmd ? curwin : NULL;
4565 if (!qflist_valid(wp, save_qfid))
4566 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004567#endif
4568
Bram Moolenaar86b68352004-12-27 21:59:20 +00004569 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004570 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004571 {
4572 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004573 {
4574 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004575 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004576 if (buf != curbuf)
4577 /* If we jumped to another buffer redrawing will already be
4578 * taken care of. */
4579 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004580
4581 /* Jump to the directory used after loading the buffer. */
4582 if (curbuf == first_match_buf && target_dir != NULL)
4583 {
4584 exarg_T ea;
4585
4586 ea.arg = target_dir;
4587 ea.cmdidx = CMD_lcd;
4588 ex_cd(&ea);
4589 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004590 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004591 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004592 else
4593 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004594
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004595 /* If we loaded a dummy buffer into the current window, the autocommands
4596 * may have messed up things, need to redraw and recompute folds. */
4597 if (redraw_for_dummy)
4598 {
4599#ifdef FEAT_FOLDING
4600 foldUpdateAll(curwin);
4601#else
4602 redraw_later(NOT_VALID);
4603#endif
4604 }
4605
Bram Moolenaar86b68352004-12-27 21:59:20 +00004606theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004607 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004608 vim_free(dirname_now);
4609 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004610 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004611 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004612}
4613
4614/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004615 * Restore current working directory to "dirname_start" if they differ, taking
4616 * into account whether it is set locally or globally.
4617 */
4618 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004619restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004620{
4621 char_u *dirname_now = alloc(MAXPATHL);
4622
4623 if (NULL != dirname_now)
4624 {
4625 mch_dirname(dirname_now, MAXPATHL);
4626 if (STRCMP(dirname_start, dirname_now) != 0)
4627 {
4628 /* If the directory has changed, change it back by building up an
4629 * appropriate ex command and executing it. */
4630 exarg_T ea;
4631
4632 ea.arg = dirname_start;
4633 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4634 ex_cd(&ea);
4635 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004636 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004637 }
4638}
4639
4640/*
4641 * Load file "fname" into a dummy buffer and return the buffer pointer,
4642 * placing the directory resulting from the buffer load into the
4643 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4644 * prior to calling this function. Restores directory to "dirname_start" prior
4645 * to returning, if autocmds or the 'autochdir' option have changed it.
4646 *
4647 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4648 * or wipe_dummy_buffer() later!
4649 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004650 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004651 */
4652 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004653load_dummy_buffer(
4654 char_u *fname,
4655 char_u *dirname_start, /* in: old directory */
4656 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004657{
4658 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004659 bufref_T newbufref;
4660 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004661 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004662 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004663 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004664
4665 /* Allocate a buffer without putting it in the buffer list. */
4666 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4667 if (newbuf == NULL)
4668 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004669 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004670
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004671 /* Init the options. */
4672 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4673
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004674 /* need to open the memfile before putting the buffer in a window */
4675 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004676 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004677 /* Make sure this buffer isn't wiped out by auto commands. */
4678 ++newbuf->b_locked;
4679
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004680 /* set curwin/curbuf to buf and save a few things */
4681 aucmd_prepbuf(&aco, newbuf);
4682
4683 /* Need to set the filename for autocommands. */
4684 (void)setfname(curbuf, fname, NULL, FALSE);
4685
Bram Moolenaar81695252004-12-29 20:58:21 +00004686 /* Create swap file now to avoid the ATTENTION message. */
4687 check_need_swap(TRUE);
4688
4689 /* Remove the "dummy" flag, otherwise autocommands may not
4690 * work. */
4691 curbuf->b_flags &= ~BF_DUMMY;
4692
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004693 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004694 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004695 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004696 NULL, READ_NEW | READ_DUMMY);
4697 --newbuf->b_locked;
4698 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004699 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004700 && !(curbuf->b_flags & BF_NEW))
4701 {
4702 failed = FALSE;
4703 if (curbuf != newbuf)
4704 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004705 /* Bloody autocommands changed the buffer! Can happen when
4706 * using netrw and editing a remote file. Use the current
4707 * buffer instead, delete the dummy one after restoring the
4708 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004709 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004710 newbuf = curbuf;
4711 }
4712 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004713
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004714 /* restore curwin/curbuf and a few other things */
4715 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004716 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4717 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004718
4719 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4720 * skip it. */
4721 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004722 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004723
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004724 /*
4725 * When autocommands/'autochdir' option changed directory: go back.
4726 * Let the caller know what the resulting dir was first, in case it is
4727 * important.
4728 */
4729 mch_dirname(resulting_dir, MAXPATHL);
4730 restore_start_dir(dirname_start);
4731
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004732 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004733 return NULL;
4734 if (failed)
4735 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004736 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004737 return NULL;
4738 }
4739 return newbuf;
4740}
4741
4742/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004743 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4744 * directory to "dirname_start" prior to returning, if autocmds or the
4745 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004746 */
4747 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004748wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004749{
4750 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004751 {
4752#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4753 cleanup_T cs;
4754
4755 /* Reset the error/interrupt/exception state here so that aborting()
4756 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4757 * work when got_int is set. */
4758 enter_cleanup(&cs);
4759#endif
4760
Bram Moolenaar81695252004-12-29 20:58:21 +00004761 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004762
4763#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4764 /* Restore the error/interrupt/exception state if not discarded by a
4765 * new aborting error, interrupt, or uncaught exception. */
4766 leave_cleanup(&cs);
4767#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004768 /* When autocommands/'autochdir' option changed directory: go back. */
4769 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004770 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004771}
4772
4773/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004774 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4775 * directory to "dirname_start" prior to returning, if autocmds or the
4776 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004777 */
4778 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004779unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004780{
4781 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004782 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004783 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004784
4785 /* When autocommands/'autochdir' option changed directory: go back. */
4786 restore_start_dir(dirname_start);
4787 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004788}
4789
Bram Moolenaar05159a02005-02-26 23:04:13 +00004790#if defined(FEAT_EVAL) || defined(PROTO)
4791/*
4792 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004793 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004794 */
4795 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004796get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004797{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004798 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004799 dict_T *dict;
4800 char_u buf[2];
4801 qfline_T *qfp;
4802 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004803 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004804
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004805 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004806 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004807 qi = &ql_info;
4808 if (wp != NULL)
4809 {
4810 qi = GET_LOC_LIST(wp);
4811 if (qi == NULL)
4812 return FAIL;
4813 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004814 }
4815
Bram Moolenaard823fa92016-08-12 16:29:27 +02004816 if (qf_idx == -1)
4817 qf_idx = qi->qf_curlist;
4818
4819 if (qf_idx >= qi->qf_listcount
4820 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004821 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004822
Bram Moolenaard823fa92016-08-12 16:29:27 +02004823 qfp = qi->qf_lists[qf_idx].qf_start;
4824 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004825 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004826 /* Handle entries with a non-existing buffer number. */
4827 bufnum = qfp->qf_fnum;
4828 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4829 bufnum = 0;
4830
Bram Moolenaar05159a02005-02-26 23:04:13 +00004831 if ((dict = dict_alloc()) == NULL)
4832 return FAIL;
4833 if (list_append_dict(list, dict) == FAIL)
4834 return FAIL;
4835
4836 buf[0] = qfp->qf_type;
4837 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004838 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004839 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4840 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4841 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4842 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004843 || dict_add_nr_str(dict, "pattern", 0L,
4844 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4845 || dict_add_nr_str(dict, "text", 0L,
4846 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004847 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4848 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4849 return FAIL;
4850
4851 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004852 if (qfp == NULL)
4853 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004854 }
4855 return OK;
4856}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004857
4858/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004859 * Flags used by getqflist()/getloclist() to determine which fields to return.
4860 */
4861enum {
4862 QF_GETLIST_NONE = 0x0,
4863 QF_GETLIST_TITLE = 0x1,
4864 QF_GETLIST_ITEMS = 0x2,
4865 QF_GETLIST_NR = 0x4,
4866 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004867 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004868 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004869 QF_GETLIST_IDX = 0x40,
4870 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01004871 QF_GETLIST_TICK = 0x100,
4872 QF_GETLIST_ALL = 0x1FF
Bram Moolenaard823fa92016-08-12 16:29:27 +02004873};
4874
4875/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004876 * Parse text from 'di' and return the quickfix list items
4877 */
4878 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004879qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004880{
4881 int status = FAIL;
4882 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004883 char_u *errorformat = p_efm;
4884 dictitem_T *efm_di;
4885 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004886
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004887 /* Only a List value is supported */
4888 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004889 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004890 /* If errorformat is supplied then use it, otherwise use the 'efm'
4891 * option setting
4892 */
4893 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4894 {
4895 if (efm_di->di_tv.v_type != VAR_STRING ||
4896 efm_di->di_tv.vval.v_string == NULL)
4897 return FAIL;
4898 errorformat = efm_di->di_tv.vval.v_string;
4899 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004900
Bram Moolenaar36538222017-09-02 19:51:44 +02004901 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004902 if (l == NULL)
4903 return FAIL;
4904
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004905 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4906 if (qi != NULL)
4907 {
4908 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4909 qi->qf_refcount++;
4910
Bram Moolenaar36538222017-09-02 19:51:44 +02004911 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004912 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4913 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004914 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004915 qf_free(qi, 0);
4916 }
4917 free(qi);
4918 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004919 dict_add_list(retdict, "items", l);
4920 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004921 }
4922
4923 return status;
4924}
4925
4926/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004927 * Return the quickfix/location list number with the given identifier.
4928 * Returns -1 if list is not found.
4929 */
4930 static int
4931qf_id2nr(qf_info_T *qi, int_u qfid)
4932{
4933 int qf_idx;
4934
4935 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4936 if (qi->qf_lists[qf_idx].qf_id == qfid)
4937 return qf_idx;
4938 return -1;
4939}
4940
4941/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004942 * Return quickfix/location list details (title) as a
4943 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4944 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004945 */
4946 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004947qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004948{
4949 qf_info_T *qi = &ql_info;
4950 int status = OK;
4951 int qf_idx;
4952 dictitem_T *di;
4953 int flags = QF_GETLIST_NONE;
4954
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004955 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004956 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004957
Bram Moolenaard823fa92016-08-12 16:29:27 +02004958 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004959 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004960
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004961 if (dict_find(what, (char_u *)"all", -1) != NULL)
4962 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004963
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004964 if (dict_find(what, (char_u *)"title", -1) != NULL)
4965 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004966
Bram Moolenaara6d48492017-12-12 22:45:31 +01004967 if (dict_find(what, (char_u *)"nr", -1) != NULL)
4968 flags |= QF_GETLIST_NR;
4969
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004970 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4971 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004972
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004973 if (dict_find(what, (char_u *)"context", -1) != NULL)
4974 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004975
Bram Moolenaara6d48492017-12-12 22:45:31 +01004976 if (dict_find(what, (char_u *)"id", -1) != NULL)
4977 flags |= QF_GETLIST_ID;
4978
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004979 if (dict_find(what, (char_u *)"items", -1) != NULL)
4980 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004981
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004982 if (dict_find(what, (char_u *)"idx", -1) != NULL)
4983 flags |= QF_GETLIST_IDX;
4984
4985 if (dict_find(what, (char_u *)"size", -1) != NULL)
4986 flags |= QF_GETLIST_SIZE;
4987
Bram Moolenaarb254af32017-12-18 19:48:58 +01004988 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
4989 flags |= QF_GETLIST_TICK;
4990
Bram Moolenaara6d48492017-12-12 22:45:31 +01004991 if (qi != NULL && qi->qf_listcount != 0)
4992 {
4993 qf_idx = qi->qf_curlist; /* default is the current list */
4994 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4995 {
4996 /* Use the specified quickfix/location list */
4997 if (di->di_tv.v_type == VAR_NUMBER)
4998 {
4999 /* for zero use the current list */
5000 if (di->di_tv.vval.v_number != 0)
5001 {
5002 qf_idx = di->di_tv.vval.v_number - 1;
5003 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
5004 qf_idx = -1;
5005 }
5006 }
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005007 else if (di->di_tv.v_type == VAR_STRING
5008 && di->di_tv.vval.v_string != NULL
5009 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005010 /* Get the last quickfix list number */
5011 qf_idx = qi->qf_listcount - 1;
5012 else
5013 qf_idx = -1;
5014 flags |= QF_GETLIST_NR;
5015 }
5016
5017 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5018 {
5019 /* Look for a list with the specified id */
5020 if (di->di_tv.v_type == VAR_NUMBER)
5021 {
5022 /*
5023 * For zero, use the current list or the list specifed by 'nr'
5024 */
5025 if (di->di_tv.vval.v_number != 0)
5026 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5027 flags |= QF_GETLIST_ID;
5028 }
5029 else
5030 qf_idx = -1;
5031 }
5032 }
5033
5034 /* List is not present or is empty */
5035 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
5036 {
5037 if (flags & QF_GETLIST_TITLE)
5038 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5039 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5040 {
5041 list_T *l = list_alloc();
5042 if (l != NULL)
5043 status = dict_add_list(retdict, "items", l);
5044 else
5045 status = FAIL;
5046 }
5047 if ((status == OK) && (flags & QF_GETLIST_NR))
5048 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5049 if ((status == OK) && (flags & QF_GETLIST_WINID))
5050 status = dict_add_nr_str(retdict, "winid", 0L, NULL);
5051 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5052 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5053 if ((status == OK) && (flags & QF_GETLIST_ID))
5054 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5055 if ((status == OK) && (flags & QF_GETLIST_IDX))
5056 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5057 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5058 status = dict_add_nr_str(retdict, "size", 0L, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005059 if ((status == OK) && (flags & QF_GETLIST_TICK))
5060 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005061
5062 return status;
5063 }
5064
Bram Moolenaard823fa92016-08-12 16:29:27 +02005065 if (flags & QF_GETLIST_TITLE)
5066 {
5067 char_u *t;
5068 t = qi->qf_lists[qf_idx].qf_title;
5069 if (t == NULL)
5070 t = (char_u *)"";
5071 status = dict_add_nr_str(retdict, "title", 0L, t);
5072 }
5073 if ((status == OK) && (flags & QF_GETLIST_NR))
5074 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5075 if ((status == OK) && (flags & QF_GETLIST_WINID))
5076 {
5077 win_T *win;
Bram Moolenaar74240d32017-12-10 15:26:15 +01005078 int win_id = 0;
5079
Bram Moolenaard823fa92016-08-12 16:29:27 +02005080 win = qf_find_win(qi);
5081 if (win != NULL)
Bram Moolenaar74240d32017-12-10 15:26:15 +01005082 win_id = win->w_id;
5083 status = dict_add_nr_str(retdict, "winid", win_id, NULL);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005084 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005085 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5086 {
5087 list_T *l = list_alloc();
5088 if (l != NULL)
5089 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005090 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005091 dict_add_list(retdict, "items", l);
5092 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005093 else
5094 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005095 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005096
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005097 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5098 {
5099 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5100 {
5101 di = dictitem_alloc((char_u *)"context");
5102 if (di != NULL)
5103 {
5104 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005105 status = dict_add(retdict, di);
5106 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02005107 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005108 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005109 else
5110 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005111 }
5112 else
5113 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5114 }
5115
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005116 if ((status == OK) && (flags & QF_GETLIST_ID))
5117 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5118 NULL);
5119
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005120 if ((status == OK) && (flags & QF_GETLIST_IDX))
5121 {
5122 int idx = qi->qf_lists[qf_idx].qf_index;
5123 if (qi->qf_lists[qf_idx].qf_count == 0)
5124 /* For empty lists, qf_index is set to 1 */
5125 idx = 0;
5126 status = dict_add_nr_str(retdict, "idx", idx, NULL);
5127 }
5128
5129 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5130 status = dict_add_nr_str(retdict, "size",
5131 qi->qf_lists[qf_idx].qf_count, NULL);
5132
Bram Moolenaarb254af32017-12-18 19:48:58 +01005133 if ((status == OK) && (flags & QF_GETLIST_TICK))
5134 status = dict_add_nr_str(retdict, "changedtick",
5135 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5136
Bram Moolenaard823fa92016-08-12 16:29:27 +02005137 return status;
5138}
5139
5140/*
5141 * Add list of entries to quickfix/location list. Each list entry is
5142 * a dictionary with item information.
5143 */
5144 static int
5145qf_add_entries(
5146 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005147 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005148 list_T *list,
5149 char_u *title,
5150 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005151{
5152 listitem_T *li;
5153 dict_T *d;
5154 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005155 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005156 long lnum;
5157 int col, nr;
5158 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005159 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005160 int valid, status;
5161 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005162 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005163
Bram Moolenaara3921f42017-06-04 15:30:34 +02005164 if (action == ' ' || qf_idx == qi->qf_listcount)
5165 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005166 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005167 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005168 qf_idx = qi->qf_curlist;
5169 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005170 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005171 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005172 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005173 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005174 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005175 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005176 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005177 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005178
5179 for (li = list->lv_first; li != NULL; li = li->li_next)
5180 {
5181 if (li->li_tv.v_type != VAR_DICT)
5182 continue; /* Skip non-dict items */
5183
5184 d = li->li_tv.vval.v_dict;
5185 if (d == NULL)
5186 continue;
5187
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005188 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005189 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5190 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5191 col = (int)get_dict_number(d, (char_u *)"col");
5192 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5193 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005194 type = get_dict_string(d, (char_u *)"type", TRUE);
5195 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5196 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005197 if (text == NULL)
5198 text = vim_strsave((char_u *)"");
5199
5200 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005201 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005202 valid = FALSE;
5203
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005204 /* Mark entries with non-existing buffer number as not valid. Give the
5205 * error message only once. */
5206 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5207 {
5208 if (!did_bufnr_emsg)
5209 {
5210 did_bufnr_emsg = TRUE;
5211 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5212 }
5213 valid = FALSE;
5214 bufnum = 0;
5215 }
5216
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005217 /* If the 'valid' field is present it overrules the detected value. */
5218 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5219 valid = (int)get_dict_number(d, (char_u *)"valid");
5220
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005221 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005222 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005223 NULL, /* dir */
5224 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005225 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005226 text,
5227 lnum,
5228 col,
5229 vcol, /* vis_col */
5230 pattern, /* search pattern */
5231 nr,
5232 type == NULL ? NUL : *type,
5233 valid);
5234
5235 vim_free(filename);
5236 vim_free(pattern);
5237 vim_free(text);
5238 vim_free(type);
5239
5240 if (status == FAIL)
5241 {
5242 retval = FAIL;
5243 break;
5244 }
5245 }
5246
Bram Moolenaara3921f42017-06-04 15:30:34 +02005247 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005248 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005249 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005250 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005251 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005252 if (action != 'a')
5253 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005254 qi->qf_lists[qf_idx].qf_ptr =
5255 qi->qf_lists[qf_idx].qf_start;
5256 if (qi->qf_lists[qf_idx].qf_count > 0)
5257 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005258 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005259
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005260 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005261 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005262
5263 return retval;
5264}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005265
5266 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005267qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005268{
5269 dictitem_T *di;
5270 int retval = FAIL;
5271 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005272 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005273 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005274
5275 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5276 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005277
5278 qf_idx = qi->qf_curlist; /* default is the current list */
5279 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5280 {
5281 /* Use the specified quickfix/location list */
5282 if (di->di_tv.v_type == VAR_NUMBER)
5283 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005284 /* for zero use the current list */
5285 if (di->di_tv.vval.v_number != 0)
5286 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005287
Bram Moolenaar55b69262017-08-13 13:42:01 +02005288 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5289 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005290 /*
5291 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005292 * non-available list and add the new list at the end of the
5293 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005294 */
5295 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005296 qf_idx = qi->qf_listcount - 1;
5297 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005298 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005299 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005300 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005301 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005302 }
5303 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01005304 && di->di_tv.vval.v_string != NULL
5305 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005306 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005307 if (qi->qf_listcount > 0)
5308 qf_idx = qi->qf_listcount - 1;
5309 else if (newlist)
5310 qf_idx = 0;
5311 else
5312 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005313 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005314 else
5315 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005316 }
5317
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005318 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5319 {
5320 /* Use the quickfix/location list with the specified id */
5321 if (di->di_tv.v_type == VAR_NUMBER)
5322 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005323 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5324 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005325 return FAIL; /* List not found */
5326 }
5327 else
5328 return FAIL;
5329 }
5330
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005331 if (newlist)
5332 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005333 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005334 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005335 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005336 }
5337
5338 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5339 {
5340 if (di->di_tv.v_type == VAR_STRING)
5341 {
5342 vim_free(qi->qf_lists[qf_idx].qf_title);
5343 qi->qf_lists[qf_idx].qf_title =
5344 get_dict_string(what, (char_u *)"title", TRUE);
5345 if (qf_idx == qi->qf_curlist)
5346 qf_update_win_titlevar(qi);
5347 retval = OK;
5348 }
5349 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005350
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005351 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5352 {
5353 if (di->di_tv.v_type == VAR_LIST)
5354 {
5355 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5356
5357 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5358 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005359 if (action == 'r')
5360 {
5361 /*
5362 * When replacing the quickfix list entries using
5363 * qf_add_entries(), the title is set with a ':' prefix.
5364 * Restore the title with the saved title.
5365 */
5366 vim_free(qi->qf_lists[qf_idx].qf_title);
5367 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5368 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005369 vim_free(title_save);
5370 }
5371 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005372
Bram Moolenaar36538222017-09-02 19:51:44 +02005373 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5374 {
5375 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5376 return FAIL;
5377 errorformat = di->di_tv.vval.v_string;
5378 }
5379
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005380 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005381 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005382 /* Only a List value is supported */
5383 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005384 {
5385 if (action == 'r')
5386 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005387 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005388 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5389 retval = OK;
5390 }
5391 else
5392 return FAIL;
5393 }
5394
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005395 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5396 {
5397 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005398
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005399 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005400 ctx = alloc_tv();
5401 if (ctx != NULL)
5402 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005403 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005404 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005405 }
5406
Bram Moolenaarb254af32017-12-18 19:48:58 +01005407 if (retval == OK)
5408 qf_list_changed(qi, qf_idx);
5409
Bram Moolenaard823fa92016-08-12 16:29:27 +02005410 return retval;
5411}
5412
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005413/*
5414 * Find the non-location list window with the specified location list.
5415 */
5416 static win_T *
5417find_win_with_ll(qf_info_T *qi)
5418{
5419 win_T *wp = NULL;
5420
5421 FOR_ALL_WINDOWS(wp)
5422 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5423 return wp;
5424
5425 return NULL;
5426}
5427
5428/*
5429 * Free the entire quickfix/location list stack.
5430 * If the quickfix/location list window is open, then clear it.
5431 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005432 static void
5433qf_free_stack(win_T *wp, qf_info_T *qi)
5434{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005435 win_T *qfwin = qf_find_win(qi);
5436 win_T *llwin = NULL;
5437 win_T *orig_wp = wp;
5438
5439 if (qfwin != NULL)
5440 {
5441 /* If the quickfix/location list window is open, then clear it */
5442 if (qi->qf_curlist < qi->qf_listcount)
5443 qf_free(qi, qi->qf_curlist);
5444 qf_update_buffer(qi, NULL);
5445 }
5446
5447 if (wp != NULL && IS_LL_WINDOW(wp))
5448 {
5449 /* If in the location list window, then use the non-location list
5450 * window with this location list (if present)
5451 */
5452 llwin = find_win_with_ll(qi);
5453 if (llwin != NULL)
5454 wp = llwin;
5455 }
5456
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005457 qf_free_all(wp);
5458 if (wp == NULL)
5459 {
5460 /* quickfix list */
5461 qi->qf_curlist = 0;
5462 qi->qf_listcount = 0;
5463 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005464 else if (IS_LL_WINDOW(orig_wp))
5465 {
5466 /* If the location list window is open, then create a new empty
5467 * location list */
5468 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005469
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005470 /* first free the list reference in the location list window */
5471 ll_free_all(&orig_wp->w_llist_ref);
5472
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005473 orig_wp->w_llist_ref = new_ll;
5474 if (llwin != NULL)
5475 {
5476 llwin->w_llist = new_ll;
5477 new_ll->qf_refcount++;
5478 }
5479 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005480}
5481
Bram Moolenaard823fa92016-08-12 16:29:27 +02005482/*
5483 * Populate the quickfix list with the items supplied in the list
5484 * of dictionaries. "title" will be copied to w:quickfix_title.
5485 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5486 */
5487 int
5488set_errorlist(
5489 win_T *wp,
5490 list_T *list,
5491 int action,
5492 char_u *title,
5493 dict_T *what)
5494{
5495 qf_info_T *qi = &ql_info;
5496 int retval = OK;
5497
5498 if (wp != NULL)
5499 {
5500 qi = ll_get_or_alloc_list(wp);
5501 if (qi == NULL)
5502 return FAIL;
5503 }
5504
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005505 if (action == 'f')
5506 {
5507 /* Free the entire quickfix or location list stack */
5508 qf_free_stack(wp, qi);
5509 }
5510 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005511 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005512 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005513 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005514 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005515 if (retval == OK)
5516 qf_list_changed(qi, qi->qf_curlist);
5517 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005518
5519 return retval;
5520}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005521
5522 static int
5523mark_quickfix_ctx(qf_info_T *qi, int copyID)
5524{
5525 int i;
5526 int abort = FALSE;
5527 typval_T *ctx;
5528
5529 for (i = 0; i < LISTCOUNT && !abort; ++i)
5530 {
5531 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005532 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5533 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005534 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5535 }
5536
5537 return abort;
5538}
5539
5540/*
5541 * Mark the context of the quickfix list and the location lists (if present) as
5542 * "in use". So that garabage collection doesn't free the context.
5543 */
5544 int
5545set_ref_in_quickfix(int copyID)
5546{
5547 int abort = FALSE;
5548 tabpage_T *tp;
5549 win_T *win;
5550
5551 abort = mark_quickfix_ctx(&ql_info, copyID);
5552 if (abort)
5553 return abort;
5554
5555 FOR_ALL_TAB_WINDOWS(tp, win)
5556 {
5557 if (win->w_llist != NULL)
5558 {
5559 abort = mark_quickfix_ctx(win->w_llist, copyID);
5560 if (abort)
5561 return abort;
5562 }
Bram Moolenaar12237442017-12-19 12:38:52 +01005563 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
5564 {
5565 /* In a location list window and none of the other windows is
5566 * referring to this location list. Mark the location list
5567 * context as still in use.
5568 */
5569 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
5570 if (abort)
5571 return abort;
5572 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005573 }
5574
5575 return abort;
5576}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005577#endif
5578
Bram Moolenaar81695252004-12-29 20:58:21 +00005579/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005580 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005581 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005582 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005583 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005584 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005585 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005586 */
5587 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005588ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005589{
5590 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005591 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005592#ifdef FEAT_AUTOCMD
5593 char_u *au_name = NULL;
5594#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005595 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005596
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005597#ifdef FEAT_AUTOCMD
5598 switch (eap->cmdidx)
5599 {
5600 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5601 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5602 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5603 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5604 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5605 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5606 default: break;
5607 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005608 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5609 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005610 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005611# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005612 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005613 return;
5614# endif
5615 }
5616#endif
5617
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005618 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005619 if (eap->cmdidx == CMD_lbuffer
5620 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01005621 || eap->cmdidx == CMD_laddbuffer)
5622 {
5623 qi = ll_get_or_alloc_list(curwin);
5624 if (qi == NULL)
5625 return;
5626 }
5627
Bram Moolenaar86b68352004-12-27 21:59:20 +00005628 if (*eap->arg == NUL)
5629 buf = curbuf;
5630 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5631 buf = buflist_findnr(atoi((char *)eap->arg));
5632 if (buf == NULL)
5633 EMSG(_(e_invarg));
5634 else if (buf->b_ml.ml_mfp == NULL)
5635 EMSG(_("E681: Buffer is not loaded"));
5636 else
5637 {
5638 if (eap->addr_count == 0)
5639 {
5640 eap->line1 = 1;
5641 eap->line2 = buf->b_ml.ml_line_count;
5642 }
5643 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5644 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5645 EMSG(_(e_invrange));
5646 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005647 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005648 char_u *qf_title = *eap->cmdlinep;
5649
5650 if (buf->b_sfname)
5651 {
5652 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5653 (char *)qf_title, (char *)buf->b_sfname);
5654 qf_title = IObuff;
5655 }
5656
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005657 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005658 (eap->cmdidx != CMD_caddbuffer
5659 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005660 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005661 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005662 if (res >= 0)
5663 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005664#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005665 if (au_name != NULL)
5666 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5667 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005668#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005669 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5670 eap->cmdidx == CMD_lbuffer))
5671 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005672 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005673 }
5674}
5675
Bram Moolenaar1e015462005-09-25 22:16:38 +00005676#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005677/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005678 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5679 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005680 */
5681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005682ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005683{
5684 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005685 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005686#ifdef FEAT_AUTOCMD
5687 char_u *au_name = NULL;
5688#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005689 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005690
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005691#ifdef FEAT_AUTOCMD
5692 switch (eap->cmdidx)
5693 {
5694 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5695 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5696 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5697 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5698 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5699 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5700 default: break;
5701 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005702 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5703 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005704 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005705# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005706 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005707 return;
5708# endif
5709 }
5710#endif
5711
Bram Moolenaar3c097222017-12-21 20:54:49 +01005712 if (eap->cmdidx == CMD_lexpr
5713 || eap->cmdidx == CMD_lgetexpr
5714 || eap->cmdidx == CMD_laddexpr)
5715 {
5716 qi = ll_get_or_alloc_list(curwin);
5717 if (qi == NULL)
5718 return;
5719 }
5720
Bram Moolenaar4770d092006-01-12 23:22:24 +00005721 /* Evaluate the expression. When the result is a string or a list we can
5722 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005723 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005724 if (tv != NULL)
5725 {
5726 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5727 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5728 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005729 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005730 (eap->cmdidx != CMD_caddexpr
5731 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005732 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005733 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005734 if (res >= 0)
5735 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005736#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005737 if (au_name != NULL)
5738 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5739 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005740#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005741 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5742 eap->cmdidx == CMD_lexpr))
5743 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005744 }
5745 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005746 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005747 free_tv(tv);
5748 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005749}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005750#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005751
5752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 * ":helpgrep {pattern}"
5754 */
5755 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005756ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
5758 regmatch_T regmatch;
5759 char_u *save_cpo;
5760 char_u *p;
5761 int fcount;
5762 char_u **fnames;
5763 FILE *fd;
5764 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005766#ifdef FEAT_MULTI_LANG
5767 char_u *lang;
5768#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005769 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005770 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005771 int new_qi = FALSE;
5772 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005773#ifdef FEAT_AUTOCMD
5774 char_u *au_name = NULL;
5775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005777#ifdef FEAT_MULTI_LANG
5778 /* Check for a specified language */
5779 lang = check_help_lang(eap->arg);
5780#endif
5781
Bram Moolenaar73633f82012-01-20 13:39:07 +01005782#ifdef FEAT_AUTOCMD
5783 switch (eap->cmdidx)
5784 {
5785 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5786 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5787 default: break;
5788 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005789 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5790 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005791 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005792# ifdef FEAT_EVAL
5793 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005794 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005795# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005796 }
5797#endif
5798
5799 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5800 save_cpo = p_cpo;
5801 p_cpo = empty_option;
5802
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005803 if (eap->cmdidx == CMD_lhelpgrep)
5804 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005805 /* If the current window is a help window, then use it */
5806 if (bt_help(curwin->w_buffer))
5807 wp = curwin;
5808 else
5809 /* Find an existing help window */
5810 FOR_ALL_WINDOWS(wp)
5811 if (bt_help(wp->w_buffer))
5812 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005813
5814 if (wp == NULL) /* Help window not found */
5815 qi = NULL;
5816 else
5817 qi = wp->w_llist;
5818
5819 if (qi == NULL)
5820 {
5821 /* Allocate a new location list for help text matches */
5822 if ((qi = ll_new_list()) == NULL)
5823 return;
5824 new_qi = TRUE;
5825 }
5826 }
5827
Bram Moolenaaree85df32017-03-19 14:19:50 +01005828 /* Autocommands may change the list. Save it for later comparison */
5829 save_qi = qi;
5830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5832 regmatch.rm_ic = FALSE;
5833 if (regmatch.regprog != NULL)
5834 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005835#ifdef FEAT_MBYTE
5836 vimconv_T vc;
5837
5838 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5839 * differs. */
5840 vc.vc_type = CONV_NONE;
5841 if (!enc_utf8)
5842 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5843#endif
5844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005846 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
5848 /* Go through all directories in 'runtimepath' */
5849 p = p_rtp;
5850 while (*p != NUL && !got_int)
5851 {
5852 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5853
5854 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5855 add_pathsep(NameBuff);
5856 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5857 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5858 &fnames, EW_FILE|EW_SILENT) == OK
5859 && fcount > 0)
5860 {
5861 for (fi = 0; fi < fcount && !got_int; ++fi)
5862 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005863#ifdef FEAT_MULTI_LANG
5864 /* Skip files for a different language. */
5865 if (lang != NULL
5866 && STRNICMP(lang, fnames[fi]
5867 + STRLEN(fnames[fi]) - 3, 2) != 0
5868 && !(STRNICMP(lang, "en", 2) == 0
5869 && STRNICMP("txt", fnames[fi]
5870 + STRLEN(fnames[fi]) - 3, 3) == 0))
5871 continue;
5872#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005873 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 if (fd != NULL)
5875 {
5876 lnum = 1;
5877 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5878 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005879 char_u *line = IObuff;
5880#ifdef FEAT_MBYTE
5881 /* Convert a line if 'encoding' is not utf-8 and
5882 * the line contains a non-ASCII character. */
5883 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005884 && has_non_ascii(IObuff))
5885 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005886 line = string_convert(&vc, IObuff, NULL);
5887 if (line == NULL)
5888 line = IObuff;
5889 }
5890#endif
5891
5892 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005894 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
5896 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005897 while (l > 0 && line[l - 1] <= ' ')
5898 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005900 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005901 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 NULL, /* dir */
5903 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005904 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005905 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005907 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005908 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005909 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005910 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 0, /* nr */
5912 1, /* type */
5913 TRUE /* valid */
5914 ) == FAIL)
5915 {
5916 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005917#ifdef FEAT_MBYTE
5918 if (line != IObuff)
5919 vim_free(line);
5920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 break;
5922 }
5923 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005924#ifdef FEAT_MBYTE
5925 if (line != IObuff)
5926 vim_free(line);
5927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 ++lnum;
5929 line_breakcheck();
5930 }
5931 fclose(fd);
5932 }
5933 }
5934 FreeWild(fcount, fnames);
5935 }
5936 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005937
Bram Moolenaar473de612013-06-08 18:19:48 +02005938 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005939#ifdef FEAT_MBYTE
5940 if (vc.vc_type != CONV_NONE)
5941 convert_setup(&vc, NULL, NULL);
5942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005944 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5945 qi->qf_lists[qi->qf_curlist].qf_ptr =
5946 qi->qf_lists[qi->qf_curlist].qf_start;
5947 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 }
5949
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005950 if (p_cpo == empty_option)
5951 p_cpo = save_cpo;
5952 else
5953 /* Darn, some plugin changed the value. */
5954 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
Bram Moolenaarb254af32017-12-18 19:48:58 +01005956 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005957 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
Bram Moolenaar73633f82012-01-20 13:39:07 +01005959#ifdef FEAT_AUTOCMD
5960 if (au_name != NULL)
5961 {
5962 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5963 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005964 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005965 /* autocommands made "qi" invalid */
5966 return;
5967 }
5968#endif
5969
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005971 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005972 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005973 else
5974 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005975
5976 if (eap->cmdidx == CMD_lhelpgrep)
5977 {
5978 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005979 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005980 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005981 {
5982 if (new_qi)
5983 ll_free_all(&qi);
5984 }
5985 else if (curwin->w_llist == NULL)
5986 curwin->w_llist = qi;
5987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988}
5989
5990#endif /* FEAT_QUICKFIX */