blob: 54ee9285d3760ea7ed9d5a2b6e21acc0a5df9a9f [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 */
Bram Moolenaard76ce852018-05-01 15:02:04 +020036 char_u *qf_module; /* module name for this error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000037 char_u *qf_pattern; /* search pattern for the error */
38 char_u *qf_text; /* description of the error */
39 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
40 char_u qf_cleared; /* set to TRUE if line has been deleted */
41 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000043 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020052/*
53 * Quickfix/Location list definition
54 * Contains a list of entries (qfline_T). qf_start points to the first entry
55 * and qf_last points to the last entry. qf_count contains the list size.
56 *
57 * Usually the list contains one or more entries. But an empty list can be
58 * created using setqflist()/setloclist() with a title and/or user context
59 * information and entries can be added later using setqflist()/setloclist().
60 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000062{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020063 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020065 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000068 int qf_index; /* current index in the error list */
69 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020070 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020071 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020072 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020073
74 struct dir_stack_T *qf_dir_stack;
75 char_u *qf_directory;
76 struct dir_stack_T *qf_file_stack;
77 char_u *qf_currfile;
78 int qf_multiline;
79 int qf_multiignore;
80 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010081 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000082} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020084/*
85 * Quickfix/Location list stack definition
86 * Contains a list of quickfix/location lists (qf_list_T)
87 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000088struct qf_info_S
89{
90 /*
91 * Count of references to this list. Used only for location lists.
92 * When a location list window reference this list, qf_refcount
93 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
94 * reaches 0, the list is freed.
95 */
96 int qf_refcount;
97 int qf_listcount; /* current number of lists */
98 int qf_curlist; /* current error list */
99 qf_list_T qf_lists[LISTCOUNT];
100};
101
102static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200103static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaard76ce852018-05-01 15:02:04 +0200105#define FMT_PATTERNS 11 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107/*
108 * Structure used to hold the info of one part of 'errorformat'
109 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000110typedef struct efm_S efm_T;
111struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112{
113 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
116 char_u prefix; /* prefix of this format line: */
117 /* 'D' enter directory */
118 /* 'X' leave directory */
119 /* 'A' start of multi-line message */
120 /* 'E' error message */
121 /* 'W' warning message */
122 /* 'I' informational message */
123 /* 'C' continuation line */
124 /* 'Z' end of multi-line message */
125 /* 'G' general, unspecific message */
126 /* 'P' push file (partial) message */
127 /* 'Q' pop/quit file (partial) message */
128 /* 'O' overread (partial) message */
129 char_u flags; /* additional flags given in prefix */
130 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000131 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000132 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133};
134
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100135static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
136
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200137static 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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200139static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
140static 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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static win_T *qf_find_win(qf_info_T *qi);
150static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200151static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200153static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
156static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
157static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
158static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000160/* Quickfix window check helper macro */
161#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
162/* Location list window check helper macro */
163#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
164/*
165 * Return location list for window 'wp'
166 * For location list window, return the referenced location list
167 */
168#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200171 * Looking up a buffer can be slow if there are many. Remember the last one
172 * to make this a lot faster if there are multiple matches in the same file.
173 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200174static char_u *qf_last_bufname = NULL;
175static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200176
Bram Moolenaar3c097222017-12-21 20:54:49 +0100177static char *e_loc_list_changed =
178 N_("E926: Current location list was changed");
179
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200180/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000181 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200182 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 * Return -1 for error, number of errors for success.
184 */
185 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200186qf_init(win_T *wp,
187 char_u *efile,
188 char_u *errorformat,
189 int newlist, /* TRUE: start a new error list */
190 char_u *qf_title,
191 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000193 qf_info_T *qi = &ql_info;
194
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000195 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000196 {
197 qi = ll_get_or_alloc_list(wp);
198 if (qi == NULL)
199 return FAIL;
200 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000201
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200202 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
203 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000204}
205
206/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200207 * Maximum number of bytes allowed per line while reading a errorfile.
208 */
209#define LINE_MAXLEN 4096
210
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200211static struct fmtpattern
212{
213 char_u convchar;
214 char *pattern;
215} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200216 {
217 {'f', ".\\+"}, /* only used when at end */
218 {'n', "\\d\\+"},
219 {'l', "\\d\\+"},
220 {'c', "\\d\\+"},
221 {'t', "."},
222 {'m', ".\\+"},
223 {'r', ".*"},
224 {'p', "[- .]*"},
225 {'v', "\\d\\+"},
226 {'s', ".\\+"},
227 {'o', ".\\+"}
228 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200229
230/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200231 * Convert an errorformat pattern to a regular expression pattern.
232 * See fmt_pat definition above for the list of supported patterns.
233 */
234 static char_u *
235fmtpat_to_regpat(
236 char_u *efmp,
237 efm_T *fmt_ptr,
238 int idx,
239 int round,
240 char_u *ptr,
241 char_u *errmsg)
242{
243 char_u *srcptr;
244
245 if (fmt_ptr->addr[idx])
246 {
247 /* Each errorformat pattern can occur only once */
248 sprintf((char *)errmsg,
249 _("E372: Too many %%%c in format string"), *efmp);
250 EMSG(errmsg);
251 return NULL;
252 }
253 if ((idx && idx < 6
254 && vim_strchr((char_u *)"DXOPQ", fmt_ptr->prefix) != NULL)
255 || (idx == 6
256 && vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL))
257 {
258 sprintf((char *)errmsg,
259 _("E373: Unexpected %%%c in format string"), *efmp);
260 EMSG(errmsg);
261 return NULL;
262 }
263 fmt_ptr->addr[idx] = (char_u)++round;
264 *ptr++ = '\\';
265 *ptr++ = '(';
266#ifdef BACKSLASH_IN_FILENAME
267 if (*efmp == 'f')
268 {
269 /* Also match "c:" in the file name, even when
270 * checking for a colon next: "%f:".
271 * "\%(\a:\)\=" */
272 STRCPY(ptr, "\\%(\\a:\\)\\=");
273 ptr += 10;
274 }
275#endif
276 if (*efmp == 'f' && efmp[1] != NUL)
277 {
278 if (efmp[1] != '\\' && efmp[1] != '%')
279 {
280 /* A file name may contain spaces, but this isn't
281 * in "\f". For "%f:%l:%m" there may be a ":" in
282 * the file name. Use ".\{-1,}x" instead (x is
283 * the next character), the requirement that :999:
284 * follows should work. */
285 STRCPY(ptr, ".\\{-1,}");
286 ptr += 7;
287 }
288 else
289 {
290 /* File name followed by '\\' or '%': include as
291 * many file name chars as possible. */
292 STRCPY(ptr, "\\f\\+");
293 ptr += 4;
294 }
295 }
296 else
297 {
298 srcptr = (char_u *)fmt_pat[idx].pattern;
299 while ((*ptr = *srcptr++) != NUL)
300 ++ptr;
301 }
302 *ptr++ = '\\';
303 *ptr++ = ')';
304
305 return ptr;
306}
307
308/*
309 * Convert a scanf like format in 'errorformat' to a regular expression.
310 */
311 static char_u *
312scanf_fmt_to_regpat(
313 char_u *efm,
314 int len,
315 char_u **pefmp,
316 char_u *ptr,
317 char_u *errmsg)
318{
319 char_u *efmp = *pefmp;
320
321 if (*++efmp == '[' || *efmp == '\\')
322 {
323 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
324 {
325 if (efmp[1] == '^')
326 *ptr++ = *++efmp;
327 if (efmp < efm + len)
328 {
329 *ptr++ = *++efmp; /* could be ']' */
330 while (efmp < efm + len
331 && (*ptr++ = *++efmp) != ']')
332 /* skip */;
333 if (efmp == efm + len)
334 {
335 EMSG(_("E374: Missing ] in format string"));
336 return NULL;
337 }
338 }
339 }
340 else if (efmp < efm + len) /* %*\D, %*\s etc. */
341 *ptr++ = *++efmp;
342 *ptr++ = '\\';
343 *ptr++ = '+';
344 }
345 else
346 {
347 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
348 sprintf((char *)errmsg,
349 _("E375: Unsupported %%%c in format string"), *efmp);
350 EMSG(errmsg);
351 return NULL;
352 }
353
354 *pefmp = efmp;
355
356 return ptr;
357}
358
359/*
360 * Analyze/parse an errorformat prefix.
361 */
362 static int
363efm_analyze_prefix(char_u **pefmp, efm_T *fmt_ptr, char_u *errmsg)
364{
365 char_u *efmp = *pefmp;
366
367 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
368 fmt_ptr->flags = *efmp++;
369 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
370 fmt_ptr->prefix = *efmp;
371 else
372 {
373 sprintf((char *)errmsg,
374 _("E376: Invalid %%%c in format string prefix"), *efmp);
375 EMSG(errmsg);
376 return FAIL;
377 }
378
379 *pefmp = efmp;
380
381 return OK;
382}
383
384/*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200385 * Converts a 'errorformat' string to regular expression pattern
386 */
387 static int
388efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200389 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200390 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200391 efm_T *fmt_ptr,
392 char_u *regpat,
393 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200394{
395 char_u *ptr;
396 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200397 int round;
398 int idx = 0;
399
400 /*
401 * Build regexp pattern from current 'errorformat' option
402 */
403 ptr = regpat;
404 *ptr++ = '^';
405 round = 0;
406 for (efmp = efm; efmp < efm + len; ++efmp)
407 {
408 if (*efmp == '%')
409 {
410 ++efmp;
411 for (idx = 0; idx < FMT_PATTERNS; ++idx)
412 if (fmt_pat[idx].convchar == *efmp)
413 break;
414 if (idx < FMT_PATTERNS)
415 {
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200416 ptr = fmtpat_to_regpat(efmp, fmt_ptr, idx, round, ptr,
417 errmsg);
418 if (ptr == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419 return -1;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200420 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200421 }
422 else if (*efmp == '*')
423 {
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200424 ptr = scanf_fmt_to_regpat(efm, len, &efmp, ptr, errmsg);
425 if (ptr == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200426 return -1;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200427 }
428 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
429 *ptr++ = *efmp; /* regexp magic characters */
430 else if (*efmp == '#')
431 *ptr++ = '*';
432 else if (*efmp == '>')
433 fmt_ptr->conthere = TRUE;
434 else if (efmp == efm + 1) /* analyse prefix */
435 {
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200436 if (efm_analyze_prefix(&efmp, fmt_ptr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200437 return -1;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200438 }
439 else
440 {
441 sprintf((char *)errmsg,
442 _("E377: Invalid %%%c in format string"), *efmp);
443 EMSG(errmsg);
444 return -1;
445 }
446 }
447 else /* copy normal character */
448 {
449 if (*efmp == '\\' && efmp + 1 < efm + len)
450 ++efmp;
451 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
452 *ptr++ = '\\'; /* escape regexp atoms */
453 if (*efmp)
454 *ptr++ = *efmp;
455 }
456 }
457 *ptr++ = '$';
458 *ptr = NUL;
459
460 return 0;
461}
462
463 static void
464free_efm_list(efm_T **efm_first)
465{
466 efm_T *efm_ptr;
467
468 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
469 {
470 *efm_first = efm_ptr->next;
471 vim_regfree(efm_ptr->prog);
472 vim_free(efm_ptr);
473 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100474 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200475}
476
477/* Parse 'errorformat' option */
478 static efm_T *
479parse_efm_option(char_u *efm)
480{
481 char_u *errmsg = NULL;
482 int errmsglen;
483 efm_T *fmt_ptr = NULL;
484 efm_T *fmt_first = NULL;
485 efm_T *fmt_last = NULL;
486 char_u *fmtstr = NULL;
487 int len;
488 int i;
489 int round;
490
491 errmsglen = CMDBUFFSIZE + 1;
492 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
493 if (errmsg == NULL)
494 goto parse_efm_end;
495
496 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200497 * Each part of the format string is copied and modified from errorformat
498 * to regex prog. Only a few % characters are allowed.
499 */
500
501 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200502 * Get some space to modify the format string into.
503 */
504 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
505 for (round = FMT_PATTERNS; round > 0; )
506 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200507#ifdef BACKSLASH_IN_FILENAME
508 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200509#else
510 i += 2; /* "%f" can become two chars longer */
511#endif
512 if ((fmtstr = alloc(i)) == NULL)
513 goto parse_efm_error;
514
515 while (efm[0] != NUL)
516 {
517 /*
518 * Allocate a new eformat structure and put it at the end of the list
519 */
520 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
521 if (fmt_ptr == NULL)
522 goto parse_efm_error;
523 if (fmt_first == NULL) /* first one */
524 fmt_first = fmt_ptr;
525 else
526 fmt_last->next = fmt_ptr;
527 fmt_last = fmt_ptr;
528
529 /*
530 * Isolate one part in the 'errorformat' option
531 */
532 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
533 if (efm[len] == '\\' && efm[len + 1] != NUL)
534 ++len;
535
536 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
537 goto parse_efm_error;
538 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
539 goto parse_efm_error;
540 /*
541 * Advance to next part
542 */
543 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
544 }
545
546 if (fmt_first == NULL) /* nothing found */
547 EMSG(_("E378: 'errorformat' contains no pattern"));
548
549 goto parse_efm_end;
550
551parse_efm_error:
552 free_efm_list(&fmt_first);
553
554parse_efm_end:
555 vim_free(fmtstr);
556 vim_free(errmsg);
557
558 return fmt_first;
559}
560
Bram Moolenaare0d37972016-07-15 22:36:01 +0200561enum {
562 QF_FAIL = 0,
563 QF_OK = 1,
564 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200565 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200566 QF_IGNORE_LINE = 4,
567 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200568};
569
570typedef struct {
571 char_u *linebuf;
572 int linelen;
573 char_u *growbuf;
574 int growbufsiz;
575 FILE *fd;
576 typval_T *tv;
577 char_u *p_str;
578 listitem_T *p_li;
579 buf_T *buf;
580 linenr_T buflnum;
581 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100582 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200583} qfstate_T;
584
585 static char_u *
586qf_grow_linebuf(qfstate_T *state, int newsz)
587{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200588 char_u *p;
589
Bram Moolenaare0d37972016-07-15 22:36:01 +0200590 /*
591 * If the line exceeds LINE_MAXLEN exclude the last
592 * byte since it's not a NL character.
593 */
594 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
595 if (state->growbuf == NULL)
596 {
597 state->growbuf = alloc(state->linelen + 1);
598 if (state->growbuf == NULL)
599 return NULL;
600 state->growbufsiz = state->linelen;
601 }
602 else if (state->linelen > state->growbufsiz)
603 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200604 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200605 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200606 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200607 state->growbufsiz = state->linelen;
608 }
609 return state->growbuf;
610}
611
612/*
613 * Get the next string (separated by newline) from state->p_str.
614 */
615 static int
616qf_get_next_str_line(qfstate_T *state)
617{
618 /* Get the next line from the supplied string */
619 char_u *p_str = state->p_str;
620 char_u *p;
621 int len;
622
623 if (*p_str == NUL) /* Reached the end of the string */
624 return QF_END_OF_INPUT;
625
626 p = vim_strchr(p_str, '\n');
627 if (p != NULL)
628 len = (int)(p - p_str) + 1;
629 else
630 len = (int)STRLEN(p_str);
631
632 if (len > IOSIZE - 2)
633 {
634 state->linebuf = qf_grow_linebuf(state, len);
635 if (state->linebuf == NULL)
636 return QF_NOMEM;
637 }
638 else
639 {
640 state->linebuf = IObuff;
641 state->linelen = len;
642 }
643 vim_strncpy(state->linebuf, p_str, state->linelen);
644
645 /*
646 * Increment using len in order to discard the rest of the
647 * line if it exceeds LINE_MAXLEN.
648 */
649 p_str += len;
650 state->p_str = p_str;
651
652 return QF_OK;
653}
654
655/*
656 * Get the next string from state->p_Li.
657 */
658 static int
659qf_get_next_list_line(qfstate_T *state)
660{
661 listitem_T *p_li = state->p_li;
662 int len;
663
664 while (p_li != NULL
665 && (p_li->li_tv.v_type != VAR_STRING
666 || p_li->li_tv.vval.v_string == NULL))
667 p_li = p_li->li_next; /* Skip non-string items */
668
669 if (p_li == NULL) /* End of the list */
670 {
671 state->p_li = NULL;
672 return QF_END_OF_INPUT;
673 }
674
675 len = (int)STRLEN(p_li->li_tv.vval.v_string);
676 if (len > IOSIZE - 2)
677 {
678 state->linebuf = qf_grow_linebuf(state, len);
679 if (state->linebuf == NULL)
680 return QF_NOMEM;
681 }
682 else
683 {
684 state->linebuf = IObuff;
685 state->linelen = len;
686 }
687
688 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
689
690 state->p_li = p_li->li_next; /* next item */
691 return QF_OK;
692}
693
694/*
695 * Get the next string from state->buf.
696 */
697 static int
698qf_get_next_buf_line(qfstate_T *state)
699{
700 char_u *p_buf = NULL;
701 int len;
702
703 /* Get the next line from the supplied buffer */
704 if (state->buflnum > state->lnumlast)
705 return QF_END_OF_INPUT;
706
707 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
708 state->buflnum += 1;
709
710 len = (int)STRLEN(p_buf);
711 if (len > IOSIZE - 2)
712 {
713 state->linebuf = qf_grow_linebuf(state, len);
714 if (state->linebuf == NULL)
715 return QF_NOMEM;
716 }
717 else
718 {
719 state->linebuf = IObuff;
720 state->linelen = len;
721 }
722 vim_strncpy(state->linebuf, p_buf, state->linelen);
723
724 return QF_OK;
725}
726
727/*
728 * Get the next string from file state->fd.
729 */
730 static int
731qf_get_next_file_line(qfstate_T *state)
732{
733 int discard;
734 int growbuflen;
735
736 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
737 return QF_END_OF_INPUT;
738
739 discard = FALSE;
740 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200741 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200742 {
743 /*
744 * The current line exceeds IObuff, continue reading using
745 * growbuf until EOL or LINE_MAXLEN bytes is read.
746 */
747 if (state->growbuf == NULL)
748 {
749 state->growbufsiz = 2 * (IOSIZE - 1);
750 state->growbuf = alloc(state->growbufsiz);
751 if (state->growbuf == NULL)
752 return QF_NOMEM;
753 }
754
755 /* Copy the read part of the line, excluding null-terminator */
756 memcpy(state->growbuf, IObuff, IOSIZE - 1);
757 growbuflen = state->linelen;
758
759 for (;;)
760 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200761 char_u *p;
762
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 if (fgets((char *)state->growbuf + growbuflen,
764 state->growbufsiz - growbuflen, state->fd) == NULL)
765 break;
766 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
767 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200768 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200769 break;
770 if (state->growbufsiz == LINE_MAXLEN)
771 {
772 discard = TRUE;
773 break;
774 }
775
776 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
777 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200778 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200779 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200780 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200781 }
782
783 while (discard)
784 {
785 /*
786 * The current line is longer than LINE_MAXLEN, continue
787 * reading but discard everything until EOL or EOF is
788 * reached.
789 */
790 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
791 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200792 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200793 break;
794 }
795
796 state->linebuf = state->growbuf;
797 state->linelen = growbuflen;
798 }
799 else
800 state->linebuf = IObuff;
801
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100802#ifdef FEAT_MBYTE
803 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200804 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
805 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100806 char_u *line;
807
808 line = string_convert(&state->vc, state->linebuf, &state->linelen);
809 if (line != NULL)
810 {
811 if (state->linelen < IOSIZE)
812 {
813 STRCPY(state->linebuf, line);
814 vim_free(line);
815 }
816 else
817 {
818 vim_free(state->growbuf);
819 state->linebuf = state->growbuf = line;
820 state->growbufsiz = state->linelen < LINE_MAXLEN
821 ? state->linelen : LINE_MAXLEN;
822 }
823 }
824 }
825#endif
826
Bram Moolenaare0d37972016-07-15 22:36:01 +0200827 return QF_OK;
828}
829
830/*
831 * Get the next string from a file/buffer/list/string.
832 */
833 static int
834qf_get_nextline(qfstate_T *state)
835{
836 int status = QF_FAIL;
837
838 if (state->fd == NULL)
839 {
840 if (state->tv != NULL)
841 {
842 if (state->tv->v_type == VAR_STRING)
843 /* Get the next line from the supplied string */
844 status = qf_get_next_str_line(state);
845 else if (state->tv->v_type == VAR_LIST)
846 /* Get the next line from the supplied list */
847 status = qf_get_next_list_line(state);
848 }
849 else
850 /* Get the next line from the supplied buffer */
851 status = qf_get_next_buf_line(state);
852 }
853 else
854 /* Get the next line from the supplied file */
855 status = qf_get_next_file_line(state);
856
857 if (status != QF_OK)
858 return status;
859
860 /* remove newline/CR from the line */
861 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200862 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200863 state->linebuf[state->linelen - 1] = NUL;
864#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200865 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
866 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200867#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200868 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200869
870#ifdef FEAT_MBYTE
871 remove_bom(state->linebuf);
872#endif
873
874 return QF_OK;
875}
876
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200877typedef struct {
878 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200879 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200880 char_u *errmsg;
881 int errmsglen;
882 long lnum;
883 int col;
884 char_u use_viscol;
885 char_u *pattern;
886 int enr;
887 int type;
888 int valid;
889} qffields_T;
890
891/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200892 * Parse the error format matches in 'regmatch' and set the values in 'fields'.
893 * fmt_ptr contains the 'efm' format specifiers/prefixes that have a match.
894 * Returns QF_OK if all the matches are successfully parsed. On failure,
895 * returns QF_FAIL or QF_NOMEM.
896 */
897 static int
898qf_parse_match(
899 char_u *linebuf,
900 int linelen,
901 efm_T *fmt_ptr,
902 regmatch_T *regmatch,
903 qffields_T *fields,
904 int qf_multiline,
905 int qf_multiscan,
906 char_u **tail)
907{
908 char_u *p;
909 int idx = fmt_ptr->prefix;
910 int i;
911 int len;
912
913 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
914 return QF_FAIL;
915 if (vim_strchr((char_u *)"EWI", idx) != NULL)
916 fields->type = idx;
917 else
918 fields->type = 0;
919 /*
920 * Extract error message data from matched line.
921 * We check for an actual submatch, because "\[" and "\]" in
922 * the 'errorformat' may cause the wrong submatch to be used.
923 */
924 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
925 {
926 int c;
927
928 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
929 return QF_FAIL;
930
931 /* Expand ~/file and $HOME/file to full path. */
932 c = *regmatch->endp[i];
933 *regmatch->endp[i] = NUL;
934 expand_env(regmatch->startp[i], fields->namebuf, CMDBUFFSIZE);
935 *regmatch->endp[i] = c;
936
937 if (vim_strchr((char_u *)"OPQ", idx) != NULL
938 && mch_getperm(fields->namebuf) == -1)
939 return QF_FAIL;
940 }
941 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
942 {
943 if (regmatch->startp[i] == NULL)
944 return QF_FAIL;
945 fields->enr = (int)atol((char *)regmatch->startp[i]);
946 }
947 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
948 {
949 if (regmatch->startp[i] == NULL)
950 return QF_FAIL;
951 fields->lnum = atol((char *)regmatch->startp[i]);
952 }
953 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
954 {
955 if (regmatch->startp[i] == NULL)
956 return QF_FAIL;
957 fields->col = (int)atol((char *)regmatch->startp[i]);
958 }
959 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
960 {
961 if (regmatch->startp[i] == NULL)
962 return QF_FAIL;
963 fields->type = *regmatch->startp[i];
964 }
965 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
966 {
967 if (linelen >= fields->errmsglen)
968 {
969 /* linelen + null terminator */
970 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
971 return QF_NOMEM;
972 fields->errmsg = p;
973 fields->errmsglen = linelen + 1;
974 }
975 vim_strncpy(fields->errmsg, linebuf, linelen);
976 }
977 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
978 {
979 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
980 return QF_FAIL;
981 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
982 if (len >= fields->errmsglen)
983 {
984 /* len + null terminator */
985 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
986 return QF_NOMEM;
987 fields->errmsg = p;
988 fields->errmsglen = len + 1;
989 }
990 vim_strncpy(fields->errmsg, regmatch->startp[i], len);
991 }
992 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
993 {
994 if (regmatch->startp[i] == NULL)
995 return QF_FAIL;
996 *tail = regmatch->startp[i];
997 }
998 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
999 {
1000 char_u *match_ptr;
1001
1002 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
1003 return QF_FAIL;
1004 fields->col = 0;
1005 for (match_ptr = regmatch->startp[i];
1006 match_ptr != regmatch->endp[i]; ++match_ptr)
1007 {
1008 ++fields->col;
1009 if (*match_ptr == TAB)
1010 {
1011 fields->col += 7;
1012 fields->col -= fields->col % 8;
1013 }
1014 }
1015 ++fields->col;
1016 fields->use_viscol = TRUE;
1017 }
1018 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
1019 {
1020 if (regmatch->startp[i] == NULL)
1021 return QF_FAIL;
1022 fields->col = (int)atol((char *)regmatch->startp[i]);
1023 fields->use_viscol = TRUE;
1024 }
1025 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1026 {
1027 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
1028 return QF_FAIL;
1029 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
1030 if (len > CMDBUFFSIZE - 5)
1031 len = CMDBUFFSIZE - 5;
1032 STRCPY(fields->pattern, "^\\V");
1033 STRNCAT(fields->pattern, regmatch->startp[i], len);
1034 fields->pattern[len + 3] = '\\';
1035 fields->pattern[len + 4] = '$';
1036 fields->pattern[len + 5] = NUL;
1037 }
1038 if ((i = (int)fmt_ptr->addr[10]) > 0) /* %o */
1039 {
1040 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
1041 return QF_FAIL;
1042 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
1043 if (len > CMDBUFFSIZE)
1044 len = CMDBUFFSIZE;
1045 STRNCAT(fields->module, regmatch->startp[i], len);
1046 }
1047
1048 return QF_OK;
1049}
1050
1051/*
1052 * Parse an error line in 'linebuf' using a single error format string in
1053 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1054 * Returns QF_OK if the efm format matches completely and the fields are
1055 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1056 */
1057 static int
1058qf_parse_get_fields(
1059 char_u *linebuf,
1060 int linelen,
1061 efm_T *fmt_ptr,
1062 qffields_T *fields,
1063 int qf_multiline,
1064 int qf_multiscan,
1065 char_u **tail)
1066{
1067 regmatch_T regmatch;
1068 int status = QF_FAIL;
1069 int r;
1070
1071 if (qf_multiscan &&
1072 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1073 return QF_FAIL;
1074
1075 fields->namebuf[0] = NUL;
1076 fields->module[0] = NUL;
1077 fields->pattern[0] = NUL;
1078 if (!qf_multiscan)
1079 fields->errmsg[0] = NUL;
1080 fields->lnum = 0;
1081 fields->col = 0;
1082 fields->use_viscol = FALSE;
1083 fields->enr = -1;
1084 fields->type = 0;
1085 *tail = NULL;
1086
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001087 /* Always ignore case when looking for a matching error. */
1088 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001089 regmatch.regprog = fmt_ptr->prog;
1090 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1091 fmt_ptr->prog = regmatch.regprog;
1092 if (r)
1093 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1094 fields, qf_multiline, qf_multiscan, tail);
1095
1096 return status;
1097}
1098
1099/*
1100 * Parse directory error format prefixes (%D and %X).
1101 * Push and pop directories from the directory stack when scanning directory
1102 * names.
1103 */
1104 static int
1105qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1106{
1107 if (idx == 'D') /* enter directory */
1108 {
1109 if (*fields->namebuf == NUL)
1110 {
1111 EMSG(_("E379: Missing or empty directory name"));
1112 return QF_FAIL;
1113 }
1114 qfl->qf_directory =
1115 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1116 if (qfl->qf_directory == NULL)
1117 return QF_FAIL;
1118 }
1119 else if (idx == 'X') /* leave directory */
1120 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1121
1122 return QF_OK;
1123}
1124
1125/*
1126 * Parse global file name error format prefixes (%O, %P and %Q).
1127 */
1128 static int
1129qf_parse_file_pfx(
1130 int idx,
1131 qffields_T *fields,
1132 qf_list_T *qfl,
1133 char_u *tail)
1134{
1135 fields->valid = FALSE;
1136 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1137 {
1138 if (*fields->namebuf && idx == 'P')
1139 qfl->qf_currfile =
1140 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1141 else if (idx == 'Q')
1142 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1143 *fields->namebuf = NUL;
1144 if (tail && *tail)
1145 {
1146 STRMOVE(IObuff, skipwhite(tail));
1147 qfl->qf_multiscan = TRUE;
1148 return QF_MULTISCAN;
1149 }
1150 }
1151
1152 return QF_OK;
1153}
1154
1155/*
1156 * Parse a non-error line (a line which doesn't match any of the error
1157 * format in 'efm').
1158 */
1159 static int
1160qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1161{
1162 char_u *p;
1163
1164 fields->namebuf[0] = NUL; /* no match found, remove file name */
1165 fields->lnum = 0; /* don't jump to this line */
1166 fields->valid = FALSE;
1167 if (linelen >= fields->errmsglen)
1168 {
1169 /* linelen + null terminator */
1170 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1171 return QF_NOMEM;
1172 fields->errmsg = p;
1173 fields->errmsglen = linelen + 1;
1174 }
1175 /* copy whole line to error message */
1176 vim_strncpy(fields->errmsg, linebuf, linelen);
1177
1178 return QF_OK;
1179}
1180
1181/*
1182 * Parse multi-line error format prefixes (%C and %Z)
1183 */
1184 static int
1185qf_parse_multiline_pfx(
1186 qf_info_T *qi,
1187 int qf_idx,
1188 int idx,
1189 qf_list_T *qfl,
1190 qffields_T *fields)
1191{
1192 char_u *ptr;
1193 int len;
1194
1195 if (!qfl->qf_multiignore)
1196 {
1197 qfline_T *qfprev = qfl->qf_last;
1198
1199 if (qfprev == NULL)
1200 return QF_FAIL;
1201 if (*fields->errmsg && !qfl->qf_multiignore)
1202 {
1203 len = (int)STRLEN(qfprev->qf_text);
1204 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1205 == NULL)
1206 return QF_FAIL;
1207 STRCPY(ptr, qfprev->qf_text);
1208 vim_free(qfprev->qf_text);
1209 qfprev->qf_text = ptr;
1210 *(ptr += len) = '\n';
1211 STRCPY(++ptr, fields->errmsg);
1212 }
1213 if (qfprev->qf_nr == -1)
1214 qfprev->qf_nr = fields->enr;
1215 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1216 /* only printable chars allowed */
1217 qfprev->qf_type = fields->type;
1218
1219 if (!qfprev->qf_lnum)
1220 qfprev->qf_lnum = fields->lnum;
1221 if (!qfprev->qf_col)
1222 qfprev->qf_col = fields->col;
1223 qfprev->qf_viscol = fields->use_viscol;
1224 if (!qfprev->qf_fnum)
1225 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1226 qfl->qf_directory,
1227 *fields->namebuf || qfl->qf_directory != NULL
1228 ? fields->namebuf
1229 : qfl->qf_currfile != NULL && fields->valid
1230 ? qfl->qf_currfile : 0);
1231 }
1232 if (idx == 'Z')
1233 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1234 line_breakcheck();
1235
1236 return QF_IGNORE_LINE;
1237}
1238
1239/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001240 * Parse a line and get the quickfix fields.
1241 * Return the QF_ status.
1242 */
1243 static int
1244qf_parse_line(
1245 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001246 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001247 char_u *linebuf,
1248 int linelen,
1249 efm_T *fmt_first,
1250 qffields_T *fields)
1251{
1252 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001253 int idx = 0;
1254 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001255 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001256 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001257
Bram Moolenaare333e792018-04-08 13:27:39 +02001258restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001259 /* If there was no %> item start at the first pattern */
1260 if (fmt_start == NULL)
1261 fmt_ptr = fmt_first;
1262 else
1263 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001264 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001265 fmt_ptr = fmt_start;
1266 fmt_start = NULL;
1267 }
1268
1269 /*
1270 * Try to match each part of 'errorformat' until we find a complete
1271 * match or no match.
1272 */
1273 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001274 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1275 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001276 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001277 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1278 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1279 if (status == QF_NOMEM)
1280 return status;
1281 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001282 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001283 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001284 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001285
1286 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1287 {
1288 if (fmt_ptr != NULL)
1289 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001290 /* 'D' and 'X' directory specifiers */
1291 status = qf_parse_dir_pfx(idx, fields, qfl);
1292 if (status != QF_OK)
1293 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001294 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001295
1296 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1297 if (status != QF_OK)
1298 return status;
1299
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001300 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001301 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001302 }
1303 else if (fmt_ptr != NULL)
1304 {
1305 /* honor %> item */
1306 if (fmt_ptr->conthere)
1307 fmt_start = fmt_ptr;
1308
1309 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1310 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001311 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1312 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001313 }
1314 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1315 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001316 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1317 if (status != QF_OK)
1318 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001319 }
1320 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001321 { /* global file names */
1322 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1323 if (status == QF_MULTISCAN)
1324 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001325 }
1326 if (fmt_ptr->flags == '-') /* generally exclude this line */
1327 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001328 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001329 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001330 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001331 return QF_IGNORE_LINE;
1332 }
1333 }
1334
1335 return QF_OK;
1336}
1337
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001338/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001339 * Read the errorfile "efile" into memory, line by line, building the error
1340 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001341 * Alternative: when "efile" is NULL read errors from buffer "buf".
1342 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001343 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001344 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1345 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001346 * Return -1 for error, number of errors for success.
1347 */
1348 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001349qf_init_ext(
1350 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001351 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001352 char_u *efile,
1353 buf_T *buf,
1354 typval_T *tv,
1355 char_u *errorformat,
1356 int newlist, /* TRUE: start a new error list */
1357 linenr_T lnumfirst, /* first line number to use */
1358 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001359 char_u *qf_title,
1360 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001361{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001362 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001363 qfstate_T state;
1364 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001365 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001366 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001367 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001369 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001371 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001373 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001374 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001375
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001376 vim_memset(&state, 0, sizeof(state));
1377 vim_memset(&fields, 0, sizeof(fields));
1378#ifdef FEAT_MBYTE
1379 state.vc.vc_type = CONV_NONE;
1380 if (enc != NULL && *enc != NUL)
1381 convert_setup(&state.vc, enc, p_enc);
1382#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001383 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaard76ce852018-05-01 15:02:04 +02001384 fields.module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001385 fields.errmsglen = CMDBUFFSIZE + 1;
1386 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1387 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02001388 if (fields.namebuf == NULL || fields.errmsg == NULL
Bram Moolenaard76ce852018-05-01 15:02:04 +02001389 || fields.pattern == NULL || fields.module == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 goto qf_init_end;
1391
Bram Moolenaare0d37972016-07-15 22:36:01 +02001392 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 {
1394 EMSG2(_(e_openerrf), efile);
1395 goto qf_init_end;
1396 }
1397
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001398 if (newlist || qf_idx == qi->qf_listcount)
1399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001401 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001402 qf_idx = qi->qf_curlist;
1403 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001404 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001405 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001406 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001407 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001408 if (qi->qf_lists[qf_idx].qf_count > 0)
1409 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001412 qfl = &qi->qf_lists[qf_idx];
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001415 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001416 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 else
1418 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001420 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001421 * If the errorformat didn't change between calls, then reuse the
1422 * previously parsed values.
1423 */
1424 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1425 {
1426 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001427 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001428 free_efm_list(&fmt_first);
1429
1430 /* parse the current 'efm' */
1431 fmt_first = parse_efm_option(efm);
1432 if (fmt_first != NULL)
1433 last_efm = vim_strsave(efm);
1434 }
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
1439 /*
1440 * got_int is reset here, because it was probably set when killing the
1441 * ":make" command, but we still want to read the errorfile then.
1442 */
1443 got_int = FALSE;
1444
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001445 if (tv != NULL)
1446 {
1447 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001448 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001449 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001450 state.p_li = tv->vval.v_list->lv_first;
1451 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001452 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001453 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001454 state.buflnum = lnumfirst;
1455 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 /*
1458 * Read the lines in the error file one by one.
1459 * Try to recognize one of the error formats in each line.
1460 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001461 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001463 /* Get the next line from a file/buffer/list/string */
1464 status = qf_get_nextline(&state);
1465 if (status == QF_NOMEM) /* memory alloc failure */
1466 goto qf_init_end;
1467 if (status == QF_END_OF_INPUT) /* end of input */
1468 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001470 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1471 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001472 if (status == QF_FAIL)
1473 goto error2;
1474 if (status == QF_NOMEM)
1475 goto qf_init_end;
1476 if (status == QF_IGNORE_LINE)
1477 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001479 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001480 qf_idx,
1481 qfl->qf_directory,
1482 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001483 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001484 : ((qfl->qf_currfile != NULL && fields.valid)
1485 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001486 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001487 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001488 fields.errmsg,
1489 fields.lnum,
1490 fields.col,
1491 fields.use_viscol,
1492 fields.pattern,
1493 fields.enr,
1494 fields.type,
1495 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 goto error2;
1497 line_breakcheck();
1498 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001499 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001501 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001503 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001504 qfl->qf_ptr = qfl->qf_start;
1505 qfl->qf_index = 1;
1506 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508 else
1509 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001510 qfl->qf_nonevalid = FALSE;
1511 if (qfl->qf_ptr == NULL)
1512 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001514 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001515 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001516 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518 EMSG(_(e_readerrf));
1519error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001520 if (!adding)
1521 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001522 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001523 qf_free(qi, qi->qf_curlist);
1524 qi->qf_listcount--;
1525 if (qi->qf_curlist > 0)
1526 --qi->qf_curlist;
1527 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001528qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001529 if (state.fd != NULL)
1530 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001531 vim_free(fields.namebuf);
Bram Moolenaard76ce852018-05-01 15:02:04 +02001532 vim_free(fields.module);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001533 vim_free(fields.errmsg);
1534 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001535 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001537 if (qf_idx == qi->qf_curlist)
1538 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001539#ifdef FEAT_MBYTE
1540 if (state.vc.vc_type != CONV_NONE)
1541 convert_setup(&state.vc, NULL, NULL);
1542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544 return retval;
1545}
1546
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001547/*
1548 * Set the title of the specified quickfix list. Frees the previous title.
1549 * Prepends ':' to the title.
1550 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001551 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001552qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001553{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001554 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001555
Bram Moolenaarfb604092014-07-23 15:55:00 +02001556 if (title != NULL)
1557 {
1558 char_u *p = alloc((int)STRLEN(title) + 2);
1559
Bram Moolenaara3921f42017-06-04 15:30:34 +02001560 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001561 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001562 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001563 }
1564}
1565
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001567 * The title of a quickfix/location list is set, by default, to the command
1568 * that created the quickfix list with the ":" prefix.
1569 * Create a quickfix list title string by prepending ":" to a user command.
1570 * Returns a pointer to a static buffer with the title.
1571 */
1572 static char_u *
1573qf_cmdtitle(char_u *cmd)
1574{
1575 static char_u qftitle_str[IOSIZE];
1576
1577 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1578 return qftitle_str;
1579}
1580
1581/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001582 * Prepare for adding a new quickfix list. If the current list is in the
1583 * middle of the stack, then all the following lists are freed and then
1584 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 */
1586 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001587qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
1589 int i;
1590
1591 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001592 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 * the current entry. This makes it possible to browse in a tree-like
1594 * way with ":grep'.
1595 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001596 while (qi->qf_listcount > qi->qf_curlist + 1)
1597 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
1599 /*
1600 * When the stack is full, remove to oldest entry
1601 * Otherwise, add a new entry.
1602 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001603 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001605 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001607 qi->qf_lists[i - 1] = qi->qf_lists[i];
1608 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001611 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001612 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001613 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001614 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615}
1616
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001617/*
1618 * Free a location list
1619 */
1620 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001621ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001622{
1623 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001624 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001625
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001626 qi = *pqi;
1627 if (qi == NULL)
1628 return;
1629 *pqi = NULL; /* Remove reference to this list */
1630
1631 qi->qf_refcount--;
1632 if (qi->qf_refcount < 1)
1633 {
1634 /* No references to this location list */
1635 for (i = 0; i < qi->qf_listcount; ++i)
1636 qf_free(qi, i);
1637 vim_free(qi);
1638 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001639}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001640
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001641/*
1642 * Free all the quickfix/location lists in the stack.
1643 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001645qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001646{
1647 int i;
1648 qf_info_T *qi = &ql_info;
1649
1650 if (wp != NULL)
1651 {
1652 /* location list */
1653 ll_free_all(&wp->w_llist);
1654 ll_free_all(&wp->w_llist_ref);
1655 }
1656 else
1657 /* quickfix list */
1658 for (i = 0; i < qi->qf_listcount; ++i)
1659 qf_free(qi, i);
1660}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662/*
1663 * Add an entry to the end of the list of errors.
1664 * Returns OK or FAIL.
1665 */
1666 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001667qf_add_entry(
1668 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001669 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001670 char_u *dir, /* optional directory name */
1671 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001672 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001673 int bufnum, /* buffer number or zero */
1674 char_u *mesg, /* message */
1675 long lnum, /* line number */
1676 int col, /* column */
1677 int vis_col, /* using visual column */
1678 char_u *pattern, /* search pattern */
1679 int nr, /* error number */
1680 int type, /* type character */
1681 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001683 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001684 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001686 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001688 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001689 {
1690 buf_T *buf = buflist_findnr(bufnum);
1691
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001692 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001693 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001694 buf->b_has_qf_entry |=
1695 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001696 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001697 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001698 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1700 {
1701 vim_free(qfp);
1702 return FAIL;
1703 }
1704 qfp->qf_lnum = lnum;
1705 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001706 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001707 if (pattern == NULL || *pattern == NUL)
1708 qfp->qf_pattern = NULL;
1709 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1710 {
1711 vim_free(qfp->qf_text);
1712 vim_free(qfp);
1713 return FAIL;
1714 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001715 if (module == NULL || *module == NUL)
1716 qfp->qf_module = NULL;
1717 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1718 {
1719 vim_free(qfp->qf_text);
1720 vim_free(qfp->qf_pattern);
1721 vim_free(qfp);
1722 return FAIL;
1723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 qfp->qf_nr = nr;
1725 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1726 type = 0;
1727 qfp->qf_type = type;
1728 qfp->qf_valid = valid;
1729
Bram Moolenaara3921f42017-06-04 15:30:34 +02001730 lastp = &qi->qf_lists[qf_idx].qf_last;
1731 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001732 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001734 qi->qf_lists[qf_idx].qf_start = qfp;
1735 qi->qf_lists[qf_idx].qf_ptr = qfp;
1736 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001737 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
1739 else
1740 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001741 qfp->qf_prev = *lastp;
1742 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001744 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001746 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001747 ++qi->qf_lists[qf_idx].qf_count;
1748 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001749 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001751 qi->qf_lists[qf_idx].qf_index =
1752 qi->qf_lists[qf_idx].qf_count;
1753 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
1755
1756 return OK;
1757}
1758
1759/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001760 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001761 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001762 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001763ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001764{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001765 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001766
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001767 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1768 if (qi != NULL)
1769 {
1770 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1771 qi->qf_refcount++;
1772 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001773
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001774 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001775}
1776
1777/*
1778 * Return the location list for window 'wp'.
1779 * If not present, allocate a location list
1780 */
1781 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001782ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001783{
1784 if (IS_LL_WINDOW(wp))
1785 /* For a location list window, use the referenced location list */
1786 return wp->w_llist_ref;
1787
1788 /*
1789 * For a non-location list window, w_llist_ref should not point to a
1790 * location list.
1791 */
1792 ll_free_all(&wp->w_llist_ref);
1793
1794 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001795 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001796 return wp->w_llist;
1797}
1798
1799/*
1800 * Copy the location list from window "from" to window "to".
1801 */
1802 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001803copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001804{
1805 qf_info_T *qi;
1806 int idx;
1807 int i;
1808
1809 /*
1810 * When copying from a location list window, copy the referenced
1811 * location list. For other windows, copy the location list for
1812 * that window.
1813 */
1814 if (IS_LL_WINDOW(from))
1815 qi = from->w_llist_ref;
1816 else
1817 qi = from->w_llist;
1818
1819 if (qi == NULL) /* no location list to copy */
1820 return;
1821
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001822 /* allocate a new location list */
1823 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001824 return;
1825
1826 to->w_llist->qf_listcount = qi->qf_listcount;
1827
1828 /* Copy the location lists one at a time */
1829 for (idx = 0; idx < qi->qf_listcount; idx++)
1830 {
1831 qf_list_T *from_qfl;
1832 qf_list_T *to_qfl;
1833
1834 to->w_llist->qf_curlist = idx;
1835
1836 from_qfl = &qi->qf_lists[idx];
1837 to_qfl = &to->w_llist->qf_lists[idx];
1838
1839 /* Some of the fields are populated by qf_add_entry() */
1840 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1841 to_qfl->qf_count = 0;
1842 to_qfl->qf_index = 0;
1843 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001844 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001845 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001846 if (from_qfl->qf_title != NULL)
1847 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1848 else
1849 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001850 if (from_qfl->qf_ctx != NULL)
1851 {
1852 to_qfl->qf_ctx = alloc_tv();
1853 if (to_qfl->qf_ctx != NULL)
1854 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1855 }
1856 else
1857 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001858
1859 if (from_qfl->qf_count)
1860 {
1861 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001862 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001863
1864 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001865 for (i = 0, from_qfp = from_qfl->qf_start;
1866 i < from_qfl->qf_count && from_qfp != NULL;
1867 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001868 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001869 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001870 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001871 NULL,
1872 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02001873 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001874 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001875 from_qfp->qf_text,
1876 from_qfp->qf_lnum,
1877 from_qfp->qf_col,
1878 from_qfp->qf_viscol,
1879 from_qfp->qf_pattern,
1880 from_qfp->qf_nr,
1881 0,
1882 from_qfp->qf_valid) == FAIL)
1883 {
1884 qf_free_all(to);
1885 return;
1886 }
1887 /*
1888 * qf_add_entry() will not set the qf_num field, as the
1889 * directory and file names are not supplied. So the qf_fnum
1890 * field is copied here.
1891 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001892 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001893 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1894 prevp->qf_type = from_qfp->qf_type; /* error type */
1895 if (from_qfl->qf_ptr == from_qfp)
1896 to_qfl->qf_ptr = prevp; /* current location */
1897 }
1898 }
1899
1900 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1901
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001902 /* Assign a new ID for the location list */
1903 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001904 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001905
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001906 /* When no valid entries are present in the list, qf_ptr points to
1907 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001908 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001909 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001910 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001911 to_qfl->qf_index = 1;
1912 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001913 }
1914
1915 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1916}
1917
1918/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001919 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001920 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 */
1922 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001923qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924{
Bram Moolenaar82404332016-07-10 17:00:38 +02001925 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001926 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001927 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001928
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 if (fname == NULL || *fname == NUL) /* no file name */
1930 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
Bram Moolenaare60acc12011-05-10 16:41:25 +02001932#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001933 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001934#endif
1935#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001936 if (directory != NULL)
1937 slash_adjust(directory);
1938 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001939#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001940 if (directory != NULL && !vim_isAbsName(fname)
1941 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1942 {
1943 /*
1944 * Here we check if the file really exists.
1945 * This should normally be true, but if make works without
1946 * "leaving directory"-messages we might have missed a
1947 * directory change.
1948 */
1949 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001952 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001953 if (directory)
1954 ptr = concat_fnames(directory, fname, TRUE);
1955 else
1956 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001958 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001959 bufname = ptr;
1960 }
1961 else
1962 bufname = fname;
1963
1964 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001965 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001966 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001967 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001968 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001970 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001971 {
1972 vim_free(qf_last_bufname);
1973 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1974 if (bufname == ptr)
1975 qf_last_bufname = bufname;
1976 else
1977 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001978 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001979 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001980 if (buf == NULL)
1981 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001982
Bram Moolenaarc1542742016-07-20 21:44:37 +02001983 buf->b_has_qf_entry =
1984 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001985 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986}
1987
1988/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001989 * Push dirbuf onto the directory stack and return pointer to actual dir or
1990 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 */
1992 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001993qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994{
1995 struct dir_stack_T *ds_new;
1996 struct dir_stack_T *ds_ptr;
1997
1998 /* allocate new stack element and hook it in */
1999 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2000 if (ds_new == NULL)
2001 return NULL;
2002
2003 ds_new->next = *stackptr;
2004 *stackptr = ds_new;
2005
2006 /* store directory on the stack */
2007 if (vim_isAbsName(dirbuf)
2008 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002009 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 (*stackptr)->dirname = vim_strsave(dirbuf);
2011 else
2012 {
2013 /* Okay we don't have an absolute path.
2014 * dirbuf must be a subdir of one of the directories on the stack.
2015 * Let's search...
2016 */
2017 ds_new = (*stackptr)->next;
2018 (*stackptr)->dirname = NULL;
2019 while (ds_new)
2020 {
2021 vim_free((*stackptr)->dirname);
2022 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2023 TRUE);
2024 if (mch_isdir((*stackptr)->dirname) == TRUE)
2025 break;
2026
2027 ds_new = ds_new->next;
2028 }
2029
2030 /* clean up all dirs we already left */
2031 while ((*stackptr)->next != ds_new)
2032 {
2033 ds_ptr = (*stackptr)->next;
2034 (*stackptr)->next = (*stackptr)->next->next;
2035 vim_free(ds_ptr->dirname);
2036 vim_free(ds_ptr);
2037 }
2038
2039 /* Nothing found -> it must be on top level */
2040 if (ds_new == NULL)
2041 {
2042 vim_free((*stackptr)->dirname);
2043 (*stackptr)->dirname = vim_strsave(dirbuf);
2044 }
2045 }
2046
2047 if ((*stackptr)->dirname != NULL)
2048 return (*stackptr)->dirname;
2049 else
2050 {
2051 ds_ptr = *stackptr;
2052 *stackptr = (*stackptr)->next;
2053 vim_free(ds_ptr);
2054 return NULL;
2055 }
2056}
2057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058/*
2059 * pop dirbuf from the directory stack and return previous directory or NULL if
2060 * stack is empty
2061 */
2062 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002063qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064{
2065 struct dir_stack_T *ds_ptr;
2066
2067 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2068 * What to do if it isn't? */
2069
2070 /* pop top element and free it */
2071 if (*stackptr != NULL)
2072 {
2073 ds_ptr = *stackptr;
2074 *stackptr = (*stackptr)->next;
2075 vim_free(ds_ptr->dirname);
2076 vim_free(ds_ptr);
2077 }
2078
2079 /* return NEW top element as current dir or NULL if stack is empty*/
2080 return *stackptr ? (*stackptr)->dirname : NULL;
2081}
2082
2083/*
2084 * clean up directory stack
2085 */
2086 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002087qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
2089 struct dir_stack_T *ds_ptr;
2090
2091 while ((ds_ptr = *stackptr) != NULL)
2092 {
2093 *stackptr = (*stackptr)->next;
2094 vim_free(ds_ptr->dirname);
2095 vim_free(ds_ptr);
2096 }
2097}
2098
2099/*
2100 * Check in which directory of the directory stack the given file can be
2101 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002102 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 * Cleans up intermediate directory entries.
2104 *
2105 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002106 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 * ./
2108 * ./aa
2109 * ./aa/bb
2110 * ./bb
2111 * ./bb/x.c
2112 * and make says:
2113 * making all in aa
2114 * making all in bb
2115 * x.c:9: Error
2116 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2117 * qf_guess_filepath will return NULL.
2118 */
2119 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002120qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
2122 struct dir_stack_T *ds_ptr;
2123 struct dir_stack_T *ds_tmp;
2124 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002125 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
2127 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002128 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 return NULL;
2130
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002131 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 fullname = NULL;
2133 while (ds_ptr)
2134 {
2135 vim_free(fullname);
2136 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2137
2138 /* If concat_fnames failed, just go on. The worst thing that can happen
2139 * is that we delete the entire stack.
2140 */
2141 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2142 break;
2143
2144 ds_ptr = ds_ptr->next;
2145 }
2146
2147 vim_free(fullname);
2148
2149 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002150 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002152 ds_tmp = qfl->qf_dir_stack->next;
2153 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 vim_free(ds_tmp->dirname);
2155 vim_free(ds_tmp);
2156 }
2157
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002158 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159}
2160
2161/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002162 * Returns TRUE if a quickfix/location list with the given identifier exists.
2163 */
2164 static int
2165qflist_valid (win_T *wp, int_u qf_id)
2166{
2167 qf_info_T *qi = &ql_info;
2168 int i;
2169
2170 if (wp != NULL)
2171 {
2172 qi = GET_LOC_LIST(wp); /* Location list */
2173 if (qi == NULL)
2174 return FALSE;
2175 }
2176
2177 for (i = 0; i < qi->qf_listcount; ++i)
2178 if (qi->qf_lists[i].qf_id == qf_id)
2179 return TRUE;
2180
2181 return FALSE;
2182}
2183
2184/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002185 * When loading a file from the quickfix, the auto commands may modify it.
2186 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002187 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002188 * Similar to location list.
2189 */
2190 static int
2191is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
2192{
2193 qf_list_T *qfl;
2194 qfline_T *qfp;
2195 int i;
2196
2197 qfl = &qi->qf_lists[qi->qf_curlist];
2198
2199 /* Search for the entry in the current list */
2200 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2201 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002202 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002203 break;
2204
2205 if (i == qfl->qf_count) /* Entry is not found */
2206 return FALSE;
2207
2208 return TRUE;
2209}
2210
2211/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002212 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002213 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002214 */
2215 static qfline_T *
2216get_next_valid_entry(
2217 qf_info_T *qi,
2218 qfline_T *qf_ptr,
2219 int *qf_index,
2220 int dir)
2221{
2222 int idx;
2223 int old_qf_fnum;
2224
2225 idx = *qf_index;
2226 old_qf_fnum = qf_ptr->qf_fnum;
2227
2228 do
2229 {
2230 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2231 || qf_ptr->qf_next == NULL)
2232 return NULL;
2233 ++idx;
2234 qf_ptr = qf_ptr->qf_next;
2235 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2236 && !qf_ptr->qf_valid)
2237 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2238
2239 *qf_index = idx;
2240 return qf_ptr;
2241}
2242
2243/*
2244 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002245 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002246 */
2247 static qfline_T *
2248get_prev_valid_entry(
2249 qf_info_T *qi,
2250 qfline_T *qf_ptr,
2251 int *qf_index,
2252 int dir)
2253{
2254 int idx;
2255 int old_qf_fnum;
2256
2257 idx = *qf_index;
2258 old_qf_fnum = qf_ptr->qf_fnum;
2259
2260 do
2261 {
2262 if (idx == 1 || qf_ptr->qf_prev == NULL)
2263 return NULL;
2264 --idx;
2265 qf_ptr = qf_ptr->qf_prev;
2266 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2267 && !qf_ptr->qf_valid)
2268 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2269
2270 *qf_index = idx;
2271 return qf_ptr;
2272}
2273
2274/*
2275 * Get the n'th (errornr) previous/next valid entry from the current entry in
2276 * the quickfix list.
2277 * dir == FORWARD or FORWARD_FILE: next valid entry
2278 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2279 */
2280 static qfline_T *
2281get_nth_valid_entry(
2282 qf_info_T *qi,
2283 int errornr,
2284 qfline_T *qf_ptr,
2285 int *qf_index,
2286 int dir)
2287{
2288 qfline_T *prev_qf_ptr;
2289 int prev_index;
2290 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2291 char_u *err = e_no_more_items;
2292
2293 while (errornr--)
2294 {
2295 prev_qf_ptr = qf_ptr;
2296 prev_index = *qf_index;
2297
2298 if (dir == FORWARD || dir == FORWARD_FILE)
2299 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2300 else
2301 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2302 if (qf_ptr == NULL)
2303 {
2304 qf_ptr = prev_qf_ptr;
2305 *qf_index = prev_index;
2306 if (err != NULL)
2307 {
2308 EMSG(_(err));
2309 return NULL;
2310 }
2311 break;
2312 }
2313
2314 err = NULL;
2315 }
2316
2317 return qf_ptr;
2318}
2319
2320/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002321 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002322 */
2323 static qfline_T *
2324get_nth_entry(
2325 qf_info_T *qi,
2326 int errornr,
2327 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002328 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002329{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002330 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002331
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002332 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002333 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2334 {
2335 --qf_idx;
2336 qf_ptr = qf_ptr->qf_prev;
2337 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002338 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002339 while (errornr > qf_idx &&
2340 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2341 qf_ptr->qf_next != NULL)
2342 {
2343 ++qf_idx;
2344 qf_ptr = qf_ptr->qf_next;
2345 }
2346
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002347 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002348 return qf_ptr;
2349}
2350
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002351/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002352 * Find a window displaying a Vim help file.
2353 */
2354 static win_T *
2355qf_find_help_win(void)
2356{
2357 win_T *wp;
2358
2359 FOR_ALL_WINDOWS(wp)
2360 if (bt_help(wp->w_buffer))
2361 return wp;
2362
2363 return NULL;
2364}
2365
2366/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002367 * Find a help window or open one.
2368 */
2369 static int
2370jump_to_help_window(qf_info_T *qi, int *opened_window)
2371{
2372 win_T *wp;
2373 int flags;
2374
2375 if (cmdmod.tab != 0)
2376 wp = NULL;
2377 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002378 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002379 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2380 win_enter(wp, TRUE);
2381 else
2382 {
2383 /*
2384 * Split off help window; put it at far top if no position
2385 * specified, the current window is vertically split and narrow.
2386 */
2387 flags = WSP_HELP;
2388 if (cmdmod.split == 0 && curwin->w_width != Columns
2389 && curwin->w_width < 80)
2390 flags |= WSP_TOP;
2391 if (qi != &ql_info)
2392 flags |= WSP_NEWLOC; /* don't copy the location list */
2393
2394 if (win_split(0, flags) == FAIL)
2395 return FAIL;
2396
2397 *opened_window = TRUE;
2398
2399 if (curwin->w_height < p_hh)
2400 win_setheight((int)p_hh);
2401
2402 if (qi != &ql_info) /* not a quickfix list */
2403 {
2404 /* The new window should use the supplied location list */
2405 curwin->w_llist = qi;
2406 qi->qf_refcount++;
2407 }
2408 }
2409
2410 if (!p_im)
2411 restart_edit = 0; /* don't want insert mode in help file */
2412
2413 return OK;
2414}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002415
2416/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002417 * Find a non-quickfix window using the given location list.
2418 * Returns NULL if a matching window is not found.
2419 */
2420 static win_T *
2421qf_find_win_with_loclist(qf_info_T *ll)
2422{
2423 win_T *wp;
2424
2425 FOR_ALL_WINDOWS(wp)
2426 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2427 return wp;
2428
2429 return NULL;
2430}
2431
2432/*
2433 * Find a window containing a normal buffer
2434 */
2435 static win_T *
2436qf_find_win_with_normal_buf(void)
2437{
2438 win_T *wp;
2439
2440 FOR_ALL_WINDOWS(wp)
2441 if (wp->w_buffer->b_p_bt[0] == NUL)
2442 return wp;
2443
2444 return NULL;
2445}
2446
2447/*
2448 * Go to a window in any tabpage containing the specified file. Returns TRUE
2449 * if successfully jumped to the window. Otherwise returns FALSE.
2450 */
2451 static int
2452qf_goto_tabwin_with_file(int fnum)
2453{
2454 tabpage_T *tp;
2455 win_T *wp;
2456
2457 FOR_ALL_TAB_WINDOWS(tp, wp)
2458 if (wp->w_buffer->b_fnum == fnum)
2459 {
2460 goto_tabpage_win(tp, wp);
2461 return TRUE;
2462 }
2463
2464 return FALSE;
2465}
2466
2467/*
2468 * Create a new window to show a file above the quickfix window. Called when
2469 * only the quickfix window is present.
2470 */
2471 static int
2472qf_open_new_file_win(qf_info_T *ll_ref)
2473{
2474 int flags;
2475
2476 flags = WSP_ABOVE;
2477 if (ll_ref != NULL)
2478 flags |= WSP_NEWLOC;
2479 if (win_split(0, flags) == FAIL)
2480 return FAIL; /* not enough room for window */
2481 p_swb = empty_option; /* don't split again */
2482 swb_flags = 0;
2483 RESET_BINDING(curwin);
2484 if (ll_ref != NULL)
2485 {
2486 /* The new window should use the location list from the
2487 * location list window */
2488 curwin->w_llist = ll_ref;
2489 ll_ref->qf_refcount++;
2490 }
2491 return OK;
2492}
2493
2494/*
2495 * Go to a window that shows the right buffer. If the window is not found, go
2496 * to the window just above the location list window. This is used for opening
2497 * a file from a location window and not from a quickfix window. If some usable
2498 * window is previously found, then it is supplied in 'use_win'.
2499 */
2500 static void
2501qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2502{
2503 win_T *win = use_win;
2504
2505 if (win == NULL)
2506 {
2507 /* Find the window showing the selected file */
2508 FOR_ALL_WINDOWS(win)
2509 if (win->w_buffer->b_fnum == qf_fnum)
2510 break;
2511 if (win == NULL)
2512 {
2513 /* Find a previous usable window */
2514 win = curwin;
2515 do
2516 {
2517 if (win->w_buffer->b_p_bt[0] == NUL)
2518 break;
2519 if (win->w_prev == NULL)
2520 win = lastwin; /* wrap around the top */
2521 else
2522 win = win->w_prev; /* go to previous window */
2523 } while (win != curwin);
2524 }
2525 }
2526 win_goto(win);
2527
2528 /* If the location list for the window is not set, then set it
2529 * to the location list from the location window */
2530 if (win->w_llist == NULL)
2531 {
2532 win->w_llist = ll_ref;
2533 ll_ref->qf_refcount++;
2534 }
2535}
2536
2537/*
2538 * Go to a window that shows the specified file. If a window is not found, go
2539 * to the window just above the quickfix window. This is used for opening a
2540 * file from a quickfix window and not from a location window.
2541 */
2542 static void
2543qf_goto_win_with_qfl_file(int qf_fnum)
2544{
2545 win_T *win;
2546 win_T *altwin;
2547
2548 win = curwin;
2549 altwin = NULL;
2550 for (;;)
2551 {
2552 if (win->w_buffer->b_fnum == qf_fnum)
2553 break;
2554 if (win->w_prev == NULL)
2555 win = lastwin; /* wrap around the top */
2556 else
2557 win = win->w_prev; /* go to previous window */
2558
2559 if (IS_QF_WINDOW(win))
2560 {
2561 /* Didn't find it, go to the window before the quickfix
2562 * window. */
2563 if (altwin != NULL)
2564 win = altwin;
2565 else if (curwin->w_prev != NULL)
2566 win = curwin->w_prev;
2567 else
2568 win = curwin->w_next;
2569 break;
2570 }
2571
2572 /* Remember a usable window. */
2573 if (altwin == NULL && !win->w_p_pvw
2574 && win->w_buffer->b_p_bt[0] == NUL)
2575 altwin = win;
2576 }
2577
2578 win_goto(win);
2579}
2580
2581/*
2582 * Find a suitable window for opening a file (qf_fnum) from the
2583 * quickfix/location list and jump to it. If the file is already opened in a
2584 * window, jump to it. Otherwise open a new window to display the file. This is
2585 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002586 */
2587 static int
2588qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2589{
2590 win_T *usable_win_ptr = NULL;
2591 int usable_win;
2592 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002593 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002594
2595 usable_win = 0;
2596
2597 ll_ref = curwin->w_llist_ref;
2598 if (ll_ref != NULL)
2599 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002600 /* Find a non-quickfix window with this location list */
2601 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2602 if (usable_win_ptr != NULL)
2603 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002604 }
2605
2606 if (!usable_win)
2607 {
2608 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002609 win = qf_find_win_with_normal_buf();
2610 if (win != NULL)
2611 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002612 }
2613
2614 /*
2615 * If no usable window is found and 'switchbuf' contains "usetab"
2616 * then search in other tabs.
2617 */
2618 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002619 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002620
2621 /*
2622 * If there is only one window and it is the quickfix window, create a
2623 * new one above the quickfix window.
2624 */
2625 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2626 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002627 if (qf_open_new_file_win(ll_ref) != OK)
2628 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002629 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002630 }
2631 else
2632 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002633 if (curwin->w_llist_ref != NULL) /* In a location window */
2634 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2635 else /* In a quickfix window */
2636 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002637 }
2638
2639 return OK;
2640}
2641
2642/*
2643 * Edit the selected file or help file.
2644 */
2645 static int
2646qf_jump_edit_buffer(
2647 qf_info_T *qi,
2648 qfline_T *qf_ptr,
2649 int forceit,
2650 win_T *oldwin,
2651 int *opened_window,
2652 int *abort)
2653{
2654 int retval = OK;
2655
2656 if (qf_ptr->qf_type == 1)
2657 {
2658 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2659 * set b_p_ro flag). */
2660 if (!can_abandon(curbuf, forceit))
2661 {
2662 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002663 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002664 }
2665 else
2666 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2667 ECMD_HIDE + ECMD_SET_HELP,
2668 oldwin == curwin ? curwin : NULL);
2669 }
2670 else
2671 {
2672 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002673 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002674
2675 retval = buflist_getfile(qf_ptr->qf_fnum,
2676 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002677
2678 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002679 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002680 /*
2681 * Location list. Check whether the associated window is still
2682 * present and the list is still valid.
2683 */
2684 if (!win_valid_any_tab(oldwin))
2685 {
2686 EMSG(_("E924: Current window was closed"));
2687 *abort = TRUE;
2688 *opened_window = FALSE;
2689 }
2690 else if (!qflist_valid(oldwin, save_qfid))
2691 {
2692 EMSG(_(e_loc_list_changed));
2693 *abort = TRUE;
2694 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002695 }
2696 else if (old_qf_curlist != qi->qf_curlist
2697 || !is_qf_entry_present(qi, qf_ptr))
2698 {
2699 if (qi == &ql_info)
2700 EMSG(_("E925: Current quickfix was changed"));
2701 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002702 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002703 *abort = TRUE;
2704 }
2705
2706 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002707 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002708 }
2709
2710 return retval;
2711}
2712
2713/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002714 * Go to the error line in the current file using either line/column number or
2715 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002716 */
2717 static void
2718qf_jump_goto_line(
2719 linenr_T qf_lnum,
2720 int qf_col,
2721 char_u qf_viscol,
2722 char_u *qf_pattern)
2723{
2724 linenr_T i;
2725 char_u *line;
2726 colnr_T screen_col;
2727 colnr_T char_col;
2728
2729 if (qf_pattern == NULL)
2730 {
2731 /*
2732 * Go to line with error, unless qf_lnum is 0.
2733 */
2734 i = qf_lnum;
2735 if (i > 0)
2736 {
2737 if (i > curbuf->b_ml.ml_line_count)
2738 i = curbuf->b_ml.ml_line_count;
2739 curwin->w_cursor.lnum = i;
2740 }
2741 if (qf_col > 0)
2742 {
2743 curwin->w_cursor.col = qf_col - 1;
2744#ifdef FEAT_VIRTUALEDIT
2745 curwin->w_cursor.coladd = 0;
2746#endif
2747 if (qf_viscol == TRUE)
2748 {
2749 /*
2750 * Check each character from the beginning of the error
2751 * line up to the error column. For each tab character
2752 * found, reduce the error column value by the length of
2753 * a tab character.
2754 */
2755 line = ml_get_curline();
2756 screen_col = 0;
2757 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2758 {
2759 if (*line == NUL)
2760 break;
2761 if (*line++ == '\t')
2762 {
2763 curwin->w_cursor.col -= 7 - (screen_col % 8);
2764 screen_col += 8 - (screen_col % 8);
2765 }
2766 else
2767 ++screen_col;
2768 }
2769 }
2770 check_cursor();
2771 }
2772 else
2773 beginline(BL_WHITE | BL_FIX);
2774 }
2775 else
2776 {
2777 pos_T save_cursor;
2778
2779 /* Move the cursor to the first line in the buffer */
2780 save_cursor = curwin->w_cursor;
2781 curwin->w_cursor.lnum = 0;
2782 if (!do_search(NULL, '/', qf_pattern, (long)1,
2783 SEARCH_KEEP, NULL, NULL))
2784 curwin->w_cursor = save_cursor;
2785 }
2786}
2787
2788/*
2789 * Display quickfix list index and size message
2790 */
2791 static void
2792qf_jump_print_msg(
2793 qf_info_T *qi,
2794 int qf_index,
2795 qfline_T *qf_ptr,
2796 buf_T *old_curbuf,
2797 linenr_T old_lnum)
2798{
2799 linenr_T i;
2800 int len;
2801
2802 /* Update the screen before showing the message, unless the screen
2803 * scrolled up. */
2804 if (!msg_scrolled)
2805 update_topline_redraw();
2806 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2807 qi->qf_lists[qi->qf_curlist].qf_count,
2808 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2809 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2810 /* Add the message, skipping leading whitespace and newlines. */
2811 len = (int)STRLEN(IObuff);
2812 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2813
2814 /* Output the message. Overwrite to avoid scrolling when the 'O'
2815 * flag is present in 'shortmess'; But when not jumping, print the
2816 * whole message. */
2817 i = msg_scroll;
2818 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2819 msg_scroll = TRUE;
2820 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2821 msg_scroll = FALSE;
2822 msg_attr_keep(IObuff, 0, TRUE);
2823 msg_scroll = i;
2824}
2825
2826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 * jump to a quickfix line
2828 * if dir == FORWARD go "errornr" valid entries forward
2829 * if dir == BACKWARD go "errornr" valid entries backward
2830 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2831 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2832 * else if "errornr" is zero, redisplay the same line
2833 * else go to entry "errornr"
2834 */
2835 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002836qf_jump(qf_info_T *qi,
2837 int dir,
2838 int errornr,
2839 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002841 qfline_T *qf_ptr;
2842 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 buf_T *old_curbuf;
2846 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002847 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002848 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002850 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852#ifdef FEAT_FOLDING
2853 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2854#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002855 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002857 if (qi == NULL)
2858 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002859
2860 if (qi->qf_curlist >= qi->qf_listcount
2861 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
2863 EMSG(_(e_quickfix));
2864 return;
2865 }
2866
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002867 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002869 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002871 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002873 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2874 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002876 qf_ptr = old_qf_ptr;
2877 qf_index = old_qf_index;
2878 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 }
2880 }
2881 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002882 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002884 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2885 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 /* No need to print the error message if it's visible in the error
2887 * window */
2888 print_message = FALSE;
2889
2890 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002891 * For ":helpgrep" find a help window or open one.
2892 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002893 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002894 if (jump_to_help_window(qi, &opened_window) == FAIL)
2895 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002896
2897 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 * If currently in the quickfix window, find another window to show the
2899 * file in.
2900 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002901 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
2903 /*
2904 * If there is no file specified, we don't know where to go.
2905 * But do advance, otherwise ":cn" gets stuck.
2906 */
2907 if (qf_ptr->qf_fnum == 0)
2908 goto theend;
2909
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002910 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2911 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914 /*
2915 * If there is a file name,
2916 * read the wanted file if needed, and check autowrite etc.
2917 */
2918 old_curbuf = curbuf;
2919 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002920
2921 if (qf_ptr->qf_fnum != 0)
2922 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002923 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002924
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002925 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2926 &opened_window, &abort);
2927 if (abort)
2928 {
2929 qi = NULL;
2930 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002931 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002932 }
2933
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002934 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
2936 /* When not switched to another buffer, still need to set pc mark */
2937 if (curbuf == old_curbuf)
2938 setpcmark();
2939
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002940 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2941 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
2943#ifdef FEAT_FOLDING
2944 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2945 foldOpenCursor();
2946#endif
2947 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002948 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
2950 else
2951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (opened_window)
2953 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002954 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
2956 /*
2957 * Couldn't open file, so put index back where it was. This could
2958 * happen if the file was readonly and we changed something.
2959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 qf_ptr = old_qf_ptr;
2962 qf_index = old_qf_index;
2963 }
2964 }
2965theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002966 if (qi != NULL)
2967 {
2968 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2969 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (p_swb != old_swb && opened_window)
2972 {
2973 /* Restore old 'switchbuf' value, but not when an autocommand or
2974 * modeline has changed the value. */
2975 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002978 swb_flags = old_swb_flags;
2979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 else
2981 free_string_option(old_swb);
2982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983}
2984
2985/*
2986 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002987 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 */
2989 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002990qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002992 buf_T *buf;
2993 char_u *fname;
2994 qfline_T *qfp;
2995 int i;
2996 int idx1 = 1;
2997 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002998 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002999 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003000 int qfFileAttr;
3001 int qfSepAttr;
3002 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003003 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003005 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003007 if (eap->cmdidx == CMD_llist)
3008 {
3009 qi = GET_LOC_LIST(curwin);
3010 if (qi == NULL)
3011 {
3012 EMSG(_(e_loclist));
3013 return;
3014 }
3015 }
3016
3017 if (qi->qf_curlist >= qi->qf_listcount
3018 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 {
3020 EMSG(_(e_quickfix));
3021 return;
3022 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003023 if (*arg == '+')
3024 {
3025 ++arg;
3026 plus = TRUE;
3027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3029 {
3030 EMSG(_(e_trailing));
3031 return;
3032 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003033 if (plus)
3034 {
3035 i = qi->qf_lists[qi->qf_curlist].qf_index;
3036 idx2 = i + idx1;
3037 idx1 = i;
3038 }
3039 else
3040 {
3041 i = qi->qf_lists[qi->qf_curlist].qf_count;
3042 if (idx1 < 0)
3043 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3044 if (idx2 < 0)
3045 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
Bram Moolenaara796d462018-05-01 14:30:36 +02003048 /* Shorten all the file names, so that it is easy to read */
3049 shorten_fnames(FALSE);
3050
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003051 /*
3052 * Get the attributes for the different quickfix highlight items. Note
3053 * that this depends on syntax items defined in the qf.vim syntax file
3054 */
3055 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3056 if (qfFileAttr == 0)
3057 qfFileAttr = HL_ATTR(HLF_D);
3058 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3059 if (qfSepAttr == 0)
3060 qfSepAttr = HL_ATTR(HLF_D);
3061 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3062 if (qfLineAttr == 0)
3063 qfLineAttr = HL_ATTR(HLF_N);
3064
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003065 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003067 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3068 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 {
3070 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3071 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003072 msg_putchar('\n');
3073 if (got_int)
3074 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003075
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003076 fname = NULL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02003077 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3078 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", i, (char *)qfp->qf_module);
3079 else {
3080 if (qfp->qf_fnum != 0
3081 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3082 {
3083 fname = buf->b_fname;
3084 if (qfp->qf_type == 1) /* :helpgrep */
3085 fname = gettail(fname);
3086 }
3087 if (fname == NULL)
3088 sprintf((char *)IObuff, "%2d", i);
3089 else
3090 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3091 i, (char *)fname);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003092 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003093 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003094 ? HL_ATTR(HLF_QFL) : qfFileAttr);
3095
3096 if (qfp->qf_lnum != 0)
3097 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003098 if (qfp->qf_lnum == 0)
3099 IObuff[0] = NUL;
3100 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003101 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003102 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003103 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003104 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003105 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003106 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003107 msg_puts_attr(IObuff, qfLineAttr);
3108 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003109 if (qfp->qf_pattern != NULL)
3110 {
3111 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003112 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003113 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003114 }
3115 msg_puts((char_u *)" ");
3116
3117 /* Remove newlines and leading whitespace from the text. For an
3118 * unrecognized line keep the indent, the compiler may mark a word
3119 * with ^^^^. */
3120 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3122 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003123 msg_prt_line(IObuff, FALSE);
3124 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003126
3127 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003128 if (qfp == NULL)
3129 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003130 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 ui_breakcheck();
3132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133}
3134
3135/*
3136 * Remove newlines and leading whitespace from an error message.
3137 * Put the result in "buf[bufsize]".
3138 */
3139 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003140qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 int i;
3143 char_u *p = text;
3144
3145 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3146 {
3147 if (*p == '\n')
3148 {
3149 buf[i] = ' ';
3150 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003151 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 break;
3153 }
3154 else
3155 buf[i] = *p++;
3156 }
3157 buf[i] = NUL;
3158}
3159
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003160/*
3161 * Display information (list number, list size and the title) about a
3162 * quickfix/location list.
3163 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003164 static void
3165qf_msg(qf_info_T *qi, int which, char *lead)
3166{
3167 char *title = (char *)qi->qf_lists[which].qf_title;
3168 int count = qi->qf_lists[which].qf_count;
3169 char_u buf[IOSIZE];
3170
3171 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3172 lead,
3173 which + 1,
3174 qi->qf_listcount,
3175 count);
3176
3177 if (title != NULL)
3178 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003179 size_t len = STRLEN(buf);
3180
3181 if (len < 34)
3182 {
3183 vim_memset(buf + len, ' ', 34 - len);
3184 buf[34] = NUL;
3185 }
3186 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003187 }
3188 trunc_string(buf, buf, Columns - 1, IOSIZE);
3189 msg(buf);
3190}
3191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192/*
3193 * ":colder [count]": Up in the quickfix stack.
3194 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003195 * ":lolder [count]": Up in the location list stack.
3196 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 */
3198 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003199qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003201 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 int count;
3203
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003204 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
3205 {
3206 qi = GET_LOC_LIST(curwin);
3207 if (qi == NULL)
3208 {
3209 EMSG(_(e_loclist));
3210 return;
3211 }
3212 }
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 if (eap->addr_count != 0)
3215 count = eap->line2;
3216 else
3217 count = 1;
3218 while (count--)
3219 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003220 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003222 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
3224 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003225 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003227 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229 else
3230 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003231 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
3233 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003236 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 }
3238 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003239 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003240 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241}
3242
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003243/*
3244 * Display the information about all the quickfix/location lists in the stack
3245 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003246 void
3247qf_history(exarg_T *eap)
3248{
3249 qf_info_T *qi = &ql_info;
3250 int i;
3251
3252 if (eap->cmdidx == CMD_lhistory)
3253 qi = GET_LOC_LIST(curwin);
3254 if (qi == NULL || (qi->qf_listcount == 0
3255 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
3256 MSG(_("No entries"));
3257 else
3258 for (i = 0; i < qi->qf_listcount; ++i)
3259 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3260}
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003263 * Free all the entries in the error list "idx". Note that other information
3264 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 */
3266 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003267qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003269 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003270 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003271 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003272 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003274 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003276 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003277 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003278 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003279 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003280 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003281 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003282 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003283 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003284 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003285 if (stop)
3286 /* Somehow qf_count may have an incorrect value, set it to 1
3287 * to avoid crashing when it's wrong.
3288 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003289 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003290 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003291 qfl->qf_start = qfpnext;
3292 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003294
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003295 qfl->qf_index = 0;
3296 qfl->qf_start = NULL;
3297 qfl->qf_last = NULL;
3298 qfl->qf_ptr = NULL;
3299 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003300
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003301 qf_clean_dir_stack(&qfl->qf_dir_stack);
3302 qfl->qf_directory = NULL;
3303 qf_clean_dir_stack(&qfl->qf_file_stack);
3304 qfl->qf_currfile = NULL;
3305 qfl->qf_multiline = FALSE;
3306 qfl->qf_multiignore = FALSE;
3307 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308}
3309
3310/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003311 * Free error list "idx". Frees all the entries in the quickfix list,
3312 * associated context information and the title.
3313 */
3314 static void
3315qf_free(qf_info_T *qi, int idx)
3316{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003317 qf_list_T *qfl = &qi->qf_lists[idx];
3318
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003319 qf_free_items(qi, idx);
3320
Bram Moolenaard23a8232018-02-10 18:45:26 +01003321 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003322 free_tv(qfl->qf_ctx);
3323 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003324 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003325 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003326}
3327
3328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 * qf_mark_adjust: adjust marks
3330 */
3331 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003332qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003333 win_T *wp,
3334 linenr_T line1,
3335 linenr_T line2,
3336 long amount,
3337 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003339 int i;
3340 qfline_T *qfp;
3341 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003342 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003343 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003344 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
Bram Moolenaarc1542742016-07-20 21:44:37 +02003346 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003347 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003348 if (wp != NULL)
3349 {
3350 if (wp->w_llist == NULL)
3351 return;
3352 qi = wp->w_llist;
3353 }
3354
3355 for (idx = 0; idx < qi->qf_listcount; ++idx)
3356 if (qi->qf_lists[idx].qf_count)
3357 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003358 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3359 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (qfp->qf_fnum == curbuf->b_fnum)
3361 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003362 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3364 {
3365 if (amount == MAXLNUM)
3366 qfp->qf_cleared = TRUE;
3367 else
3368 qfp->qf_lnum += amount;
3369 }
3370 else if (amount_after && qfp->qf_lnum > line2)
3371 qfp->qf_lnum += amount_after;
3372 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003373
3374 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003375 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376}
3377
3378/*
3379 * Make a nice message out of the error character and the error number:
3380 * char number message
3381 * e or E 0 " error"
3382 * w or W 0 " warning"
3383 * i or I 0 " info"
3384 * 0 0 ""
3385 * other 0 " c"
3386 * e or E n " error n"
3387 * w or W n " warning n"
3388 * i or I n " info n"
3389 * 0 n " error n"
3390 * other n " c n"
3391 * 1 x "" :helpgrep
3392 */
3393 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003394qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
3396 static char_u buf[20];
3397 static char_u cc[3];
3398 char_u *p;
3399
3400 if (c == 'W' || c == 'w')
3401 p = (char_u *)" warning";
3402 else if (c == 'I' || c == 'i')
3403 p = (char_u *)" info";
3404 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3405 p = (char_u *)" error";
3406 else if (c == 0 || c == 1)
3407 p = (char_u *)"";
3408 else
3409 {
3410 cc[0] = ' ';
3411 cc[1] = c;
3412 cc[2] = NUL;
3413 p = cc;
3414 }
3415
3416 if (nr <= 0)
3417 return p;
3418
3419 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3420 return buf;
3421}
3422
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423/*
3424 * ":cwindow": open the quickfix window if we have errors to display,
3425 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003426 * ":lwindow": open the location list window if we have locations to display,
3427 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 */
3429 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003430ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003432 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 win_T *win;
3434
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003435 if (eap->cmdidx == CMD_lwindow)
3436 {
3437 qi = GET_LOC_LIST(curwin);
3438 if (qi == NULL)
3439 return;
3440 }
3441
3442 /* Look for an existing quickfix window. */
3443 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445 /*
3446 * If a quickfix window is open but we have no errors to display,
3447 * close the window. If a quickfix window is not open, then open
3448 * it if we have errors; otherwise, leave it closed.
3449 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003450 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003451 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003452 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 if (win != NULL)
3455 ex_cclose(eap);
3456 }
3457 else if (win == NULL)
3458 ex_copen(eap);
3459}
3460
3461/*
3462 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003463 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003466ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003468 win_T *win = NULL;
3469 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003471 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3472 {
3473 qi = GET_LOC_LIST(curwin);
3474 if (qi == NULL)
3475 return;
3476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003478 /* Find existing quickfix window and close it. */
3479 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (win != NULL)
3481 win_close(win, FALSE);
3482}
3483
3484/*
3485 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003486 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 */
3488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003489ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003491 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003494 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003495 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003496 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003498 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3499 {
3500 qi = GET_LOC_LIST(curwin);
3501 if (qi == NULL)
3502 {
3503 EMSG(_(e_loclist));
3504 return;
3505 }
3506 }
3507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (eap->addr_count != 0)
3509 height = eap->line2;
3510 else
3511 height = QF_WINHEIGHT;
3512
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514#ifdef FEAT_GUI
3515 need_mouse_correct = TRUE;
3516#endif
3517
3518 /*
3519 * Find existing quickfix window, or open a new one.
3520 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003521 win = qf_find_win(qi);
3522
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003523 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003526 if (eap->addr_count != 0)
3527 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003528 if (cmdmod.split & WSP_VERT)
3529 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003530 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003531 win_setwidth(height);
3532 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003533 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003534 win_setheight(height);
3535 }
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else
3538 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003539 int flags = 0;
3540
Bram Moolenaar9c102382006-05-03 21:26:49 +00003541 qf_buf = qf_find_buf(qi);
3542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* The current window becomes the previous window afterwards. */
3544 win = curwin;
3545
Bram Moolenaar77642c02012-11-20 17:55:10 +01003546 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3547 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003548 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003549 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003550 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003551 /* Default is to open the window below the current window */
3552 if (cmdmod.split == 0)
3553 flags = WSP_BELOW;
3554 flags |= WSP_NEWLOC;
3555 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003557 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003559 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003561 /*
3562 * For the location list window, create a reference to the
3563 * location list from the window 'win'.
3564 */
3565 curwin->w_llist_ref = win->w_llist;
3566 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003568
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003569 if (oldwin != curwin)
3570 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003571 if (qf_buf != NULL)
3572 /* Use the existing quickfix buffer */
3573 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003574 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003575 else
3576 {
3577 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003578 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003579 /* switch off 'swapfile' */
3580 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3581 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003582 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003583 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003584 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003585#ifdef FEAT_DIFF
3586 curwin->w_p_diff = FALSE;
3587#endif
3588#ifdef FEAT_FOLDING
3589 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3590 OPT_LOCAL);
3591#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003594 /* Only set the height when still in the same tab page and there is no
3595 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003596 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 win_setheight(height);
3598 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3599 if (win_valid(win))
3600 prevwin = win;
3601 }
3602
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003603 qf_set_title_var(qi);
3604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 /*
3606 * Fill the buffer with the quickfix list.
3607 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003608 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003610 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 curwin->w_cursor.col = 0;
3612 check_cursor();
3613 update_topline(); /* scroll to show the line */
3614}
3615
3616/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003617 * Move the cursor in the quickfix window to "lnum".
3618 */
3619 static void
3620qf_win_goto(win_T *win, linenr_T lnum)
3621{
3622 win_T *old_curwin = curwin;
3623
3624 curwin = win;
3625 curbuf = win->w_buffer;
3626 curwin->w_cursor.lnum = lnum;
3627 curwin->w_cursor.col = 0;
3628#ifdef FEAT_VIRTUALEDIT
3629 curwin->w_cursor.coladd = 0;
3630#endif
3631 curwin->w_curswant = 0;
3632 update_topline(); /* scroll to show the line */
3633 redraw_later(VALID);
3634 curwin->w_redr_status = TRUE; /* update ruler */
3635 curwin = old_curwin;
3636 curbuf = curwin->w_buffer;
3637}
3638
3639/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003640 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003641 */
3642 void
3643ex_cbottom(exarg_T *eap UNUSED)
3644{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003645 qf_info_T *qi = &ql_info;
3646 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003647
Bram Moolenaar537ef082016-07-09 17:56:19 +02003648 if (eap->cmdidx == CMD_lbottom)
3649 {
3650 qi = GET_LOC_LIST(curwin);
3651 if (qi == NULL)
3652 {
3653 EMSG(_(e_loclist));
3654 return;
3655 }
3656 }
3657
3658 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003659 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3660 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3661}
3662
3663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 * Return the number of the current entry (line number in the quickfix
3665 * window).
3666 */
3667 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003668qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003670 qf_info_T *qi = &ql_info;
3671
3672 if (IS_LL_WINDOW(wp))
3673 /* In the location list window, use the referenced location list */
3674 qi = wp->w_llist_ref;
3675
3676 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677}
3678
3679/*
3680 * Update the cursor position in the quickfix window to the current error.
3681 * Return TRUE if there is a quickfix window.
3682 */
3683 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003684qf_win_pos_update(
3685 qf_info_T *qi,
3686 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003689 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
3691 /*
3692 * Put the cursor on the current error in the quickfix window, so that
3693 * it's viewable.
3694 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003695 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (win != NULL
3697 && qf_index <= win->w_buffer->b_ml.ml_line_count
3698 && old_qf_index != qf_index)
3699 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 if (qf_index > old_qf_index)
3701 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003702 win->w_redraw_top = old_qf_index;
3703 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705 else
3706 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003707 win->w_redraw_top = qf_index;
3708 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003710 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712 return win != NULL;
3713}
3714
3715/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003716 * Check whether the given window is displaying the specified quickfix/location
3717 * list buffer
3718 */
3719 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003720is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003721{
3722 /*
3723 * A window displaying the quickfix buffer will have the w_llist_ref field
3724 * set to NULL.
3725 * A window displaying a location list buffer will have the w_llist_ref
3726 * pointing to the location list.
3727 */
3728 if (bt_quickfix(win->w_buffer))
3729 if ((qi == &ql_info && win->w_llist_ref == NULL)
3730 || (qi != &ql_info && win->w_llist_ref == qi))
3731 return TRUE;
3732
3733 return FALSE;
3734}
3735
3736/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003737 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003738 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003739 */
3740 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003741qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003742{
3743 win_T *win;
3744
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003745 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003746 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003747 return win;
3748 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003749}
3750
3751/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003752 * Find a quickfix buffer.
3753 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 */
3755 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003756qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003758 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003759 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760
Bram Moolenaar9c102382006-05-03 21:26:49 +00003761 FOR_ALL_TAB_WINDOWS(tp, win)
3762 if (is_qf_win(win, qi))
3763 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003764
Bram Moolenaar9c102382006-05-03 21:26:49 +00003765 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766}
3767
3768/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003769 * Update the w:quickfix_title variable in the quickfix/location list window
3770 */
3771 static void
3772qf_update_win_titlevar(qf_info_T *qi)
3773{
3774 win_T *win;
3775 win_T *curwin_save;
3776
3777 if ((win = qf_find_win(qi)) != NULL)
3778 {
3779 curwin_save = curwin;
3780 curwin = win;
3781 qf_set_title_var(qi);
3782 curwin = curwin_save;
3783 }
3784}
3785
3786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 * Find the quickfix buffer. If it exists, update the contents.
3788 */
3789 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003790qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
3792 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003793 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
3796 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003797 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (buf != NULL)
3799 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003800 linenr_T old_line_count = buf->b_ml.ml_line_count;
3801
3802 if (old_last == NULL)
3803 /* set curwin/curbuf to buf and save a few things */
3804 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805
Bram Moolenaard823fa92016-08-12 16:29:27 +02003806 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003807
Bram Moolenaar864293a2016-06-02 13:40:04 +02003808 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003809 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003810
Bram Moolenaar864293a2016-06-02 13:40:04 +02003811 if (old_last == NULL)
3812 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003813 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003814
3815 /* restore curwin/curbuf and a few other things */
3816 aucmd_restbuf(&aco);
3817 }
3818
3819 /* Only redraw when added lines are visible. This avoids flickering
3820 * when the added lines are not visible. */
3821 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3822 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
3824}
3825
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003826/*
3827 * Set "w:quickfix_title" if "qi" has a title.
3828 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003829 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003830qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003831{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003832 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3833 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003834 qi->qf_lists[qi->qf_curlist].qf_title);
3835}
3836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837/*
3838 * Fill current buffer with quickfix errors, replacing any previous contents.
3839 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003840 * If "old_last" is not NULL append the items after this one.
3841 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3842 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 */
3844 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003845qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003847 linenr_T lnum;
3848 qfline_T *qfp;
3849 buf_T *errbuf;
3850 int len;
3851 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar864293a2016-06-02 13:40:04 +02003853 if (old_last == NULL)
3854 {
3855 if (buf != curbuf)
3856 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003857 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003858 return;
3859 }
3860
3861 /* delete all existing lines */
3862 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3863 (void)ml_delete((linenr_T)1, FALSE);
3864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003867 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003869 char_u dirname[MAXPATHL];
3870
3871 *dirname = NUL;
3872
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003874 if (old_last == NULL)
3875 {
3876 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3877 lnum = 0;
3878 }
3879 else
3880 {
3881 qfp = old_last->qf_next;
3882 lnum = buf->b_ml.ml_line_count;
3883 }
3884 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003886 if (qfp->qf_module != NULL)
3887 {
3888 STRCPY(IObuff, qfp->qf_module);
3889 len = (int)STRLEN(IObuff);
3890 }
3891 else if (qfp->qf_fnum != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3893 && errbuf->b_fname != NULL)
3894 {
3895 if (qfp->qf_type == 1) /* :helpgrep */
3896 STRCPY(IObuff, gettail(errbuf->b_fname));
3897 else
Bram Moolenaara796d462018-05-01 14:30:36 +02003898 {
3899 /* shorten the file name if not done already */
3900 if (errbuf->b_sfname == NULL
3901 || mch_isFullName(errbuf->b_sfname))
3902 {
3903 if (*dirname == NUL)
3904 mch_dirname(dirname, MAXPATHL);
3905 shorten_buf_fname(errbuf, dirname, FALSE);
3906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 STRCPY(IObuff, errbuf->b_fname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 len = (int)STRLEN(IObuff);
3910 }
3911 else
3912 len = 0;
3913 IObuff[len++] = '|';
3914
3915 if (qfp->qf_lnum > 0)
3916 {
3917 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3918 len += (int)STRLEN(IObuff + len);
3919
3920 if (qfp->qf_col > 0)
3921 {
3922 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3923 len += (int)STRLEN(IObuff + len);
3924 }
3925
3926 sprintf((char *)IObuff + len, "%s",
3927 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3928 len += (int)STRLEN(IObuff + len);
3929 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003930 else if (qfp->qf_pattern != NULL)
3931 {
3932 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3933 len += (int)STRLEN(IObuff + len);
3934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 IObuff[len++] = '|';
3936 IObuff[len++] = ' ';
3937
3938 /* Remove newlines and leading whitespace from the text.
3939 * For an unrecognized line keep the indent, the compiler may
3940 * mark a word with ^^^^. */
3941 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3942 IObuff + len, IOSIZE - len);
3943
Bram Moolenaar864293a2016-06-02 13:40:04 +02003944 if (ml_append_buf(buf, lnum, IObuff,
3945 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003947 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003949 if (qfp == NULL)
3950 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003952
3953 if (old_last == NULL)
3954 /* Delete the empty line which is now at the end */
3955 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
3957
3958 /* correct cursor position */
3959 check_lnums(TRUE);
3960
Bram Moolenaar864293a2016-06-02 13:40:04 +02003961 if (old_last == NULL)
3962 {
3963 /* Set the 'filetype' to "qf" each time after filling the buffer.
3964 * This resembles reading a file into a buffer, it's more logical when
3965 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003966 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003967 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3968 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
Bram Moolenaar864293a2016-06-02 13:40:04 +02003970 keep_filetype = TRUE; /* don't detect 'filetype' */
3971 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003973 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003975 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003976 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003977
Bram Moolenaar864293a2016-06-02 13:40:04 +02003978 /* make sure it will be redrawn */
3979 redraw_curbuf_later(NOT_VALID);
3980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
3982 /* Restore KeyTyped, setting 'filetype' may reset it. */
3983 KeyTyped = old_KeyTyped;
3984}
3985
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003986/*
3987 * For every change made to the quickfix list, update the changed tick.
3988 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01003989 static void
3990qf_list_changed(qf_info_T *qi, int qf_idx)
3991{
3992 qi->qf_lists[qf_idx].qf_changedtick++;
3993}
3994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003996 * Return TRUE when using ":vimgrep" for ":grep".
3997 */
3998 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003999grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004000{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004001 return ((cmdidx == CMD_grep
4002 || cmdidx == CMD_lgrep
4003 || cmdidx == CMD_grepadd
4004 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004005 && STRCMP("internal",
4006 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4007}
4008
4009/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004010 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 */
4012 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004013ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004015 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004017 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004019 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004020 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004021 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004022 char_u *au_name = NULL;
4023
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004024 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4025 if (grep_internal(eap->cmdidx))
4026 {
4027 ex_vimgrep(eap);
4028 return;
4029 }
4030
Bram Moolenaar7c626922005-02-07 22:01:03 +00004031 switch (eap->cmdidx)
4032 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004033 case CMD_make: au_name = (char_u *)"make"; break;
4034 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4035 case CMD_grep: au_name = (char_u *)"grep"; break;
4036 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4037 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4038 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004039 default: break;
4040 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004041 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4042 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004043 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004044#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004045 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004046 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004047#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004048 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004049#ifdef FEAT_MBYTE
4050 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
Bram Moolenaara6557602006-02-04 22:43:20 +00004053 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
4054 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004055 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004056
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004058 fname = get_mef_name();
4059 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004061 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 /*
4064 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4065 */
4066 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4067 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004068 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 cmd = alloc(len);
4070 if (cmd == NULL)
4071 return;
4072 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4073 (char *)p_shq);
4074 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004075 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 /*
4077 * Output a newline if there's something else than the :make command that
4078 * was typed (in which case the cursor is in column 0).
4079 */
4080 if (msg_col == 0)
4081 msg_didout = FALSE;
4082 msg_start();
4083 MSG_PUTS(":!");
4084 msg_outtrans(cmd); /* show what we are doing */
4085
4086 /* let the shell know if we are redirecting output or not */
4087 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4088
4089#ifdef AMIGA
4090 out_flush();
4091 /* read window status report and redraw before message */
4092 (void)char_avail();
4093#endif
4094
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004095 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004096 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4097 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004098 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004099 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004100 if (wp != NULL)
4101 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004102 if (res >= 0 && qi != NULL)
4103 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004104 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004105 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004106 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4107 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004108 if (qi != NULL && qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004109 res = qi->qf_lists[qi->qf_curlist].qf_count;
4110 else
4111 res = 0;
4112 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004113 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004114 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
Bram Moolenaar7c626922005-02-07 22:01:03 +00004116 mch_remove(fname);
4117 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 vim_free(cmd);
4119}
4120
4121/*
4122 * Return the name for the errorfile, in allocated memory.
4123 * Find a new unique name when 'makeef' contains "##".
4124 * Returns NULL for error.
4125 */
4126 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004127get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 char_u *p;
4130 char_u *name;
4131 static int start = -1;
4132 static int off = 0;
4133#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004134 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135#endif
4136
4137 if (*p_mef == NUL)
4138 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004139 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 if (name == NULL)
4141 EMSG(_(e_notmp));
4142 return name;
4143 }
4144
4145 for (p = p_mef; *p; ++p)
4146 if (p[0] == '#' && p[1] == '#')
4147 break;
4148
4149 if (*p == NUL)
4150 return vim_strsave(p_mef);
4151
4152 /* Keep trying until the name doesn't exist yet. */
4153 for (;;)
4154 {
4155 if (start == -1)
4156 start = mch_get_pid();
4157 else
4158 off += 19;
4159
4160 name = alloc((unsigned)STRLEN(p_mef) + 30);
4161 if (name == NULL)
4162 break;
4163 STRCPY(name, p_mef);
4164 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4165 STRCAT(name, p + 2);
4166 if (mch_getperm(name) < 0
4167#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004168 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 && mch_lstat((char *)name, &sb) < 0
4170#endif
4171 )
4172 break;
4173 vim_free(name);
4174 }
4175 return name;
4176}
4177
4178/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004179 * Returns the number of valid entries in the current quickfix/location list.
4180 */
4181 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004182qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004183{
4184 qf_info_T *qi = &ql_info;
4185 qfline_T *qfp;
4186 int i, sz = 0;
4187 int prev_fnum = 0;
4188
4189 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4190 {
4191 /* Location list */
4192 qi = GET_LOC_LIST(curwin);
4193 if (qi == NULL)
4194 return 0;
4195 }
4196
4197 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004198 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004199 ++i, qfp = qfp->qf_next)
4200 {
4201 if (qfp->qf_valid)
4202 {
4203 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4204 sz++; /* Count all valid entries */
4205 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4206 {
4207 /* Count the number of files */
4208 sz++;
4209 prev_fnum = qfp->qf_fnum;
4210 }
4211 }
4212 }
4213
4214 return sz;
4215}
4216
4217/*
4218 * Returns the current index of the quickfix/location list.
4219 * Returns 0 if there is an error.
4220 */
4221 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004222qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004223{
4224 qf_info_T *qi = &ql_info;
4225
4226 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4227 {
4228 /* Location list */
4229 qi = GET_LOC_LIST(curwin);
4230 if (qi == NULL)
4231 return 0;
4232 }
4233
4234 return qi->qf_lists[qi->qf_curlist].qf_index;
4235}
4236
4237/*
4238 * Returns the current index in the quickfix/location list (counting only valid
4239 * entries). If no valid entries are in the list, then returns 1.
4240 */
4241 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004242qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004243{
4244 qf_info_T *qi = &ql_info;
4245 qf_list_T *qfl;
4246 qfline_T *qfp;
4247 int i, eidx = 0;
4248 int prev_fnum = 0;
4249
4250 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4251 {
4252 /* Location list */
4253 qi = GET_LOC_LIST(curwin);
4254 if (qi == NULL)
4255 return 1;
4256 }
4257
4258 qfl = &qi->qf_lists[qi->qf_curlist];
4259 qfp = qfl->qf_start;
4260
4261 /* check if the list has valid errors */
4262 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4263 return 1;
4264
4265 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4266 {
4267 if (qfp->qf_valid)
4268 {
4269 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4270 {
4271 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4272 {
4273 /* Count the number of files */
4274 eidx++;
4275 prev_fnum = qfp->qf_fnum;
4276 }
4277 }
4278 else
4279 eidx++;
4280 }
4281 }
4282
4283 return eidx ? eidx : 1;
4284}
4285
4286/*
4287 * Get the 'n'th valid error entry in the quickfix or location list.
4288 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4289 * For :cdo and :ldo returns the 'n'th valid error entry.
4290 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4291 */
4292 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004293qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004294{
4295 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4296 qfline_T *qfp = qfl->qf_start;
4297 int i, eidx;
4298 int prev_fnum = 0;
4299
4300 /* check if the list has valid errors */
4301 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4302 return 1;
4303
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004304 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004305 i++, qfp = qfp->qf_next)
4306 {
4307 if (qfp->qf_valid)
4308 {
4309 if (fdo)
4310 {
4311 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4312 {
4313 /* Count the number of files */
4314 eidx++;
4315 prev_fnum = qfp->qf_fnum;
4316 }
4317 }
4318 else
4319 eidx++;
4320 }
4321
4322 if (eidx == n)
4323 break;
4324 }
4325
4326 if (i <= qfl->qf_count)
4327 return i;
4328 else
4329 return 1;
4330}
4331
4332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004334 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004335 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 */
4337 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004338ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004340 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004341 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004342
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004343 if (eap->cmdidx == CMD_ll
4344 || eap->cmdidx == CMD_lrewind
4345 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004346 || eap->cmdidx == CMD_llast
4347 || eap->cmdidx == CMD_ldo
4348 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004349 {
4350 qi = GET_LOC_LIST(curwin);
4351 if (qi == NULL)
4352 {
4353 EMSG(_(e_loclist));
4354 return;
4355 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004356 }
4357
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004358 if (eap->addr_count > 0)
4359 errornr = (int)eap->line2;
4360 else
4361 {
4362 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4363 errornr = 0;
4364 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4365 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4366 errornr = 1;
4367 else
4368 errornr = 32767;
4369 }
4370
4371 /* For cdo and ldo commands, jump to the nth valid error.
4372 * For cfdo and lfdo commands, jump to the nth valid file entry.
4373 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004374 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4375 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004376 errornr = qf_get_nth_valid_entry(qi,
4377 eap->addr_count > 0 ? (int)eap->line1 : 1,
4378 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4379
4380 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381}
4382
4383/*
4384 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004385 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004386 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 */
4388 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004389ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004391 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004392 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004393
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004394 if (eap->cmdidx == CMD_lnext
4395 || eap->cmdidx == CMD_lNext
4396 || eap->cmdidx == CMD_lprevious
4397 || eap->cmdidx == CMD_lnfile
4398 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004399 || eap->cmdidx == CMD_lpfile
4400 || eap->cmdidx == CMD_ldo
4401 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004402 {
4403 qi = GET_LOC_LIST(curwin);
4404 if (qi == NULL)
4405 {
4406 EMSG(_(e_loclist));
4407 return;
4408 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004409 }
4410
Bram Moolenaar55b69262017-08-13 13:42:01 +02004411 if (eap->addr_count > 0
4412 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4413 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004414 errornr = (int)eap->line2;
4415 else
4416 errornr = 1;
4417
4418 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4419 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004421 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4422 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004424 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4425 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 ? BACKWARD_FILE
4427 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004428 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429}
4430
4431/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004432 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004433 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 */
4435 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004436ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004438 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004439 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004440 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004441 char_u *au_name = NULL;
Bram Moolenaarfc6f16b2018-03-06 17:43:22 +01004442 int save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004443 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004444
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004445 switch (eap->cmdidx)
4446 {
4447 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4448 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4449 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4450 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4451 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4452 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4453 default: break;
4454 }
4455 if (au_name != NULL)
4456 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004457#ifdef FEAT_MBYTE
4458 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4459#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004460#ifdef FEAT_BROWSE
4461 if (cmdmod.browse)
4462 {
4463 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004464 NULL, NULL,
4465 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004466 if (browse_file == NULL)
4467 return;
4468 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4469 vim_free(browse_file);
4470 }
4471 else
4472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004474 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004475
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004476 if (eap->cmdidx == CMD_lfile
4477 || eap->cmdidx == CMD_lgetfile
4478 || eap->cmdidx == CMD_laddfile)
4479 wp = curwin;
4480
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004481 /*
4482 * This function is used by the :cfile, :cgetfile and :caddfile
4483 * commands.
4484 * :cfile always creates a new quickfix list and jumps to the
4485 * first error.
4486 * :cgetfile creates a new quickfix list but doesn't jump to the
4487 * first error.
4488 * :caddfile adds to an existing quickfix list. If there is no
4489 * quickfix list then a new list is created.
4490 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004491 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004492 && eap->cmdidx != CMD_laddfile),
4493 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004494 if (wp != NULL)
4495 qi = GET_LOC_LIST(wp);
4496 if (res >= 0 && qi != NULL)
4497 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004498 if (qi != NULL)
4499 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004500 if (au_name != NULL)
4501 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004502
4503 /* An autocmd might have freed the quickfix/location list. Check whether it
4504 * is still valid. */
4505 if (qi != NULL && !qflist_valid(wp, save_qfid))
Bram Moolenaar3c097222017-12-21 20:54:49 +01004506 return;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004507 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004508 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509}
4510
4511/*
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004512 * Return the quickfix/location list number with the given identifier.
4513 * Returns -1 if list is not found.
4514 */
4515 static int
4516qf_id2nr(qf_info_T *qi, int_u qfid)
4517{
4518 int qf_idx;
4519
4520 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4521 if (qi->qf_lists[qf_idx].qf_id == qfid)
4522 return qf_idx;
Bram Moolenaar29ce4092018-04-28 21:56:44 +02004523 return INVALID_QFIDX;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004524}
4525
4526/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004527 * Return the vimgrep autocmd name.
4528 */
4529 static char_u *
4530vgr_get_auname(cmdidx_T cmdidx)
4531{
4532 switch (cmdidx)
4533 {
4534 case CMD_vimgrep: return (char_u *)"vimgrep";
4535 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4536 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4537 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4538 case CMD_grep: return (char_u *)"grep";
4539 case CMD_lgrep: return (char_u *)"lgrep";
4540 case CMD_grepadd: return (char_u *)"grepadd";
4541 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4542 default: return NULL;
4543 }
4544}
4545
4546/*
4547 * Initialize the regmatch used by vimgrep for pattern "s".
4548 */
4549 static void
4550vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4551{
4552 /* Get the search pattern: either white-separated or enclosed in // */
4553 regmatch->regprog = NULL;
4554
4555 if (s == NULL || *s == NUL)
4556 {
4557 /* Pattern is empty, use last search pattern. */
4558 if (last_search_pat() == NULL)
4559 {
4560 EMSG(_(e_noprevre));
4561 return;
4562 }
4563 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4564 }
4565 else
4566 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4567
4568 regmatch->rmm_ic = p_ic;
4569 regmatch->rmm_maxcol = 0;
4570}
4571
4572/*
4573 * Display a file name when vimgrep is running.
4574 */
4575 static void
4576vgr_display_fname(char_u *fname)
4577{
4578 char_u *p;
4579
4580 msg_start();
4581 p = msg_strtrunc(fname, TRUE);
4582 if (p == NULL)
4583 msg_outtrans(fname);
4584 else
4585 {
4586 msg_outtrans(p);
4587 vim_free(p);
4588 }
4589 msg_clr_eos();
4590 msg_didout = FALSE; /* overwrite this message */
4591 msg_nowait = TRUE; /* don't wait for this message */
4592 msg_col = 0;
4593 out_flush();
4594}
4595
4596/*
4597 * Load a dummy buffer to search for a pattern using vimgrep.
4598 */
4599 static buf_T *
4600vgr_load_dummy_buf(
4601 char_u *fname,
4602 char_u *dirname_start,
4603 char_u *dirname_now)
4604{
4605 int save_mls;
4606#if defined(FEAT_SYN_HL)
4607 char_u *save_ei = NULL;
4608#endif
4609 buf_T *buf;
4610
4611#if defined(FEAT_SYN_HL)
4612 /* Don't do Filetype autocommands to avoid loading syntax and
4613 * indent scripts, a great speed improvement. */
4614 save_ei = au_event_disable(",Filetype");
4615#endif
4616 /* Don't use modelines here, it's useless. */
4617 save_mls = p_mls;
4618 p_mls = 0;
4619
4620 /* Load file into a buffer, so that 'fileencoding' is detected,
4621 * autocommands applied, etc. */
4622 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4623
4624 p_mls = save_mls;
4625#if defined(FEAT_SYN_HL)
4626 au_event_restore(save_ei);
4627#endif
4628
4629 return buf;
4630}
4631
4632/*
4633 * Check whether a quickfix/location list valid. Autocmds may remove or change
4634 * a quickfix list when vimgrep is running. If the list is not found, create a
4635 * new list.
4636 */
4637 static int
4638vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004639 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004640 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004641 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004642 char_u *title)
4643{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004644 /* Verify that the quickfix/location list was not freed by an autocmd */
4645 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004646 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004647 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004648 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004649 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004650 EMSG(_(e_loc_list_changed));
4651 return FALSE;
4652 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004653 else
4654 {
4655 /* Quickfix list is not found, create a new one. */
4656 qf_new_list(qi, title);
4657 return TRUE;
4658 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004659 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004660
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004661 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004662 /* Autocommands changed the quickfix list. Find the one we were
4663 * using and restore it. */
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004664 qi->qf_curlist = qf_id2nr(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004665
4666 return TRUE;
4667}
4668
4669/*
4670 * Search for a pattern in all the lines in a buffer and add the matching lines
4671 * to a quickfix list.
4672 */
4673 static int
4674vgr_match_buflines(
4675 qf_info_T *qi,
4676 char_u *fname,
4677 buf_T *buf,
4678 regmmatch_T *regmatch,
4679 long tomatch,
4680 int duplicate_name,
4681 int flags)
4682{
4683 int found_match = FALSE;
4684 long lnum;
4685 colnr_T col;
4686
4687 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
4688 {
4689 col = 0;
4690 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
4691 col, NULL, NULL) > 0)
4692 {
4693 /* Pass the buffer number so that it gets used even for a
4694 * dummy buffer, unless duplicate_name is set, then the
4695 * buffer will be wiped out below. */
4696 if (qf_add_entry(qi,
4697 qi->qf_curlist,
4698 NULL, /* dir */
4699 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02004700 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004701 duplicate_name ? 0 : buf->b_fnum,
4702 ml_get_buf(buf,
4703 regmatch->startpos[0].lnum + lnum, FALSE),
4704 regmatch->startpos[0].lnum + lnum,
4705 regmatch->startpos[0].col + 1,
4706 FALSE, /* vis_col */
4707 NULL, /* search pattern */
4708 0, /* nr */
4709 0, /* type */
4710 TRUE /* valid */
4711 ) == FAIL)
4712 {
4713 got_int = TRUE;
4714 break;
4715 }
4716 found_match = TRUE;
4717 if (--tomatch == 0)
4718 break;
4719 if ((flags & VGR_GLOBAL) == 0
4720 || regmatch->endpos[0].lnum > 0)
4721 break;
4722 col = regmatch->endpos[0].col
4723 + (col == regmatch->endpos[0].col);
4724 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
4725 break;
4726 }
4727 line_breakcheck();
4728 if (got_int)
4729 break;
4730 }
4731
4732 return found_match;
4733}
4734
4735/*
4736 * Jump to the first match and update the directory.
4737 */
4738 static void
4739vgr_jump_to_match(
4740 qf_info_T *qi,
4741 int forceit,
4742 int *redraw_for_dummy,
4743 buf_T *first_match_buf,
4744 char_u *target_dir)
4745{
4746 buf_T *buf;
4747
4748 buf = curbuf;
4749 qf_jump(qi, 0, 0, forceit);
4750 if (buf != curbuf)
4751 /* If we jumped to another buffer redrawing will already be
4752 * taken care of. */
4753 *redraw_for_dummy = FALSE;
4754
4755 /* Jump to the directory used after loading the buffer. */
4756 if (curbuf == first_match_buf && target_dir != NULL)
4757 {
4758 exarg_T ea;
4759
4760 ea.arg = target_dir;
4761 ea.cmdidx = CMD_lcd;
4762 ex_cd(&ea);
4763 }
4764}
4765
4766/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004767 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004768 * ":vimgrepadd {pattern} file(s)"
4769 * ":lvimgrep {pattern} file(s)"
4770 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004771 */
4772 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004773ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004774{
Bram Moolenaar81695252004-12-29 20:58:21 +00004775 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004776 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004777 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004778 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004779 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004780 char_u *s;
4781 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004782 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004783 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004784 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004785 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004786 buf_T *buf;
4787 int duplicate_name = FALSE;
4788 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004789 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004790 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004791 buf_T *first_match_buf = NULL;
4792 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004793 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004794 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004795 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004796 char_u *dirname_start = NULL;
4797 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004798 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004799 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004800
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004801 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01004802 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4803 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004804 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004805#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004806 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004807 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004808#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004809 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004810
Bram Moolenaar754b5602006-02-09 23:53:20 +00004811 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004812 || eap->cmdidx == CMD_lvimgrep
4813 || eap->cmdidx == CMD_lgrepadd
4814 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004815 {
4816 qi = ll_get_or_alloc_list(curwin);
4817 if (qi == NULL)
4818 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004819 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004820 }
4821
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004822 if (eap->addr_count > 0)
4823 tomatch = eap->line2;
4824 else
4825 tomatch = MAXLNUM;
4826
Bram Moolenaar81695252004-12-29 20:58:21 +00004827 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004828 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004829 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00004830 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004831 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004832 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004833 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004834 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004835 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004836
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004837 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004838 if (regmatch.regprog == NULL)
4839 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004840
4841 p = skipwhite(p);
4842 if (*p == NUL)
4843 {
4844 EMSG(_("E683: File name missing or invalid pattern"));
4845 goto theend;
4846 }
4847
Bram Moolenaar55b69262017-08-13 13:42:01 +02004848 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004849 && eap->cmdidx != CMD_vimgrepadd
4850 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004851 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004852 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004853 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00004854
4855 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004856 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004857 goto theend;
4858 if (fcount == 0)
4859 {
4860 EMSG(_(e_nomatch));
4861 goto theend;
4862 }
4863
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004864 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4865 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004866 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004867 {
4868 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004869 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004870 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004871
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004872 /* Remember the current directory, because a BufRead autocommand that does
4873 * ":lcd %:p:h" changes the meaning of short path names. */
4874 mch_dirname(dirname_start, MAXPATHL);
4875
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004876 /* Remember the current quickfix list identifier, so that we can check for
4877 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004878 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004879
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004880 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004881 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004882 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004883 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004884 if (time(NULL) > seconds)
4885 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004886 /* Display the file name every second or so, show the user we are
4887 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004888 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004889 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004890 }
4891
Bram Moolenaar81695252004-12-29 20:58:21 +00004892 buf = buflist_findname_exp(fnames[fi]);
4893 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4894 {
4895 /* Remember that a buffer with this name already exists. */
4896 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004897 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004898 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004899
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004900 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00004901 }
4902 else
4903 /* Use existing, loaded buffer. */
4904 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004905
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004906 /* Check whether the quickfix list is still valid. When loading a
4907 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004908 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004909 {
4910 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004911 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004912 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004913 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004914
Bram Moolenaar81695252004-12-29 20:58:21 +00004915 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004916 {
4917 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004918 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004919 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004920 else
4921 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004922 /* Try for a match in all lines of the buffer.
4923 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004924 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
4925 tomatch, duplicate_name, flags);
4926
Bram Moolenaar81695252004-12-29 20:58:21 +00004927 if (using_dummy)
4928 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004929 if (found_match && first_match_buf == NULL)
4930 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004931 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004932 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004933 /* Never keep a dummy buffer if there is another buffer
4934 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004935 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004936 buf = NULL;
4937 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004938 else if (!cmdmod.hide
4939 || buf->b_p_bh[0] == 'u' /* "unload" */
4940 || buf->b_p_bh[0] == 'w' /* "wipe" */
4941 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004942 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004943 /* When no match was found we don't need to remember the
4944 * buffer, wipe it out. If there was a match and it
4945 * wasn't the first one or we won't jump there: only
4946 * unload the buffer.
4947 * Ignore 'hidden' here, because it may lead to having too
4948 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004949 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004950 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004951 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004952 buf = NULL;
4953 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004954 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004955 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004956 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004957 /* Keeping the buffer, remove the dummy flag. */
4958 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004959 buf = NULL;
4960 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004961 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004962
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004963 if (buf != NULL)
4964 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004965 /* Keeping the buffer, remove the dummy flag. */
4966 buf->b_flags &= ~BF_DUMMY;
4967
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004968 /* If the buffer is still loaded we need to use the
4969 * directory we jumped to below. */
4970 if (buf == first_match_buf
4971 && target_dir == NULL
4972 && STRCMP(dirname_start, dirname_now) != 0)
4973 target_dir = vim_strsave(dirname_now);
4974
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004975 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004976 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004977 * need to be done (again). But not the window-local
4978 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004979 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004980#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004981 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4982 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004983#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004984 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004985 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004986 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004987 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004988 }
4989 }
4990
4991 FreeWild(fcount, fnames);
4992
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004993 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4994 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4995 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004996 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004997
Bram Moolenaar864293a2016-06-02 13:40:04 +02004998 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004999
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005000 if (au_name != NULL)
5001 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5002 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005003 /*
5004 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5005 * is still valid.
5006 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005007 if (!qflist_valid(wp, save_qfid))
5008 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005009
Bram Moolenaar86b68352004-12-27 21:59:20 +00005010 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005011 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005012 {
5013 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005014 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5015 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005016 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005017 else
5018 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005019
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005020 /* If we loaded a dummy buffer into the current window, the autocommands
5021 * may have messed up things, need to redraw and recompute folds. */
5022 if (redraw_for_dummy)
5023 {
5024#ifdef FEAT_FOLDING
5025 foldUpdateAll(curwin);
5026#else
5027 redraw_later(NOT_VALID);
5028#endif
5029 }
5030
Bram Moolenaar86b68352004-12-27 21:59:20 +00005031theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005032 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005033 vim_free(dirname_now);
5034 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005035 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005036 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005037}
5038
5039/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005040 * Restore current working directory to "dirname_start" if they differ, taking
5041 * into account whether it is set locally or globally.
5042 */
5043 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005044restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005045{
5046 char_u *dirname_now = alloc(MAXPATHL);
5047
5048 if (NULL != dirname_now)
5049 {
5050 mch_dirname(dirname_now, MAXPATHL);
5051 if (STRCMP(dirname_start, dirname_now) != 0)
5052 {
5053 /* If the directory has changed, change it back by building up an
5054 * appropriate ex command and executing it. */
5055 exarg_T ea;
5056
5057 ea.arg = dirname_start;
5058 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5059 ex_cd(&ea);
5060 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005061 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005062 }
5063}
5064
5065/*
5066 * Load file "fname" into a dummy buffer and return the buffer pointer,
5067 * placing the directory resulting from the buffer load into the
5068 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5069 * prior to calling this function. Restores directory to "dirname_start" prior
5070 * to returning, if autocmds or the 'autochdir' option have changed it.
5071 *
5072 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5073 * or wipe_dummy_buffer() later!
5074 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005075 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005076 */
5077 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005078load_dummy_buffer(
5079 char_u *fname,
5080 char_u *dirname_start, /* in: old directory */
5081 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005082{
5083 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005084 bufref_T newbufref;
5085 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005086 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005087 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005088 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005089
5090 /* Allocate a buffer without putting it in the buffer list. */
5091 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5092 if (newbuf == NULL)
5093 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005094 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005095
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005096 /* Init the options. */
5097 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5098
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005099 /* need to open the memfile before putting the buffer in a window */
5100 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005101 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005102 /* Make sure this buffer isn't wiped out by auto commands. */
5103 ++newbuf->b_locked;
5104
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005105 /* set curwin/curbuf to buf and save a few things */
5106 aucmd_prepbuf(&aco, newbuf);
5107
5108 /* Need to set the filename for autocommands. */
5109 (void)setfname(curbuf, fname, NULL, FALSE);
5110
Bram Moolenaar81695252004-12-29 20:58:21 +00005111 /* Create swap file now to avoid the ATTENTION message. */
5112 check_need_swap(TRUE);
5113
5114 /* Remove the "dummy" flag, otherwise autocommands may not
5115 * work. */
5116 curbuf->b_flags &= ~BF_DUMMY;
5117
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005118 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005119 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005120 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005121 NULL, READ_NEW | READ_DUMMY);
5122 --newbuf->b_locked;
5123 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005124 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005125 && !(curbuf->b_flags & BF_NEW))
5126 {
5127 failed = FALSE;
5128 if (curbuf != newbuf)
5129 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005130 /* Bloody autocommands changed the buffer! Can happen when
5131 * using netrw and editing a remote file. Use the current
5132 * buffer instead, delete the dummy one after restoring the
5133 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005134 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005135 newbuf = curbuf;
5136 }
5137 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005138
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005139 /* restore curwin/curbuf and a few other things */
5140 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005141 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5142 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005143
5144 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5145 * skip it. */
5146 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005147 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005148
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005149 /*
5150 * When autocommands/'autochdir' option changed directory: go back.
5151 * Let the caller know what the resulting dir was first, in case it is
5152 * important.
5153 */
5154 mch_dirname(resulting_dir, MAXPATHL);
5155 restore_start_dir(dirname_start);
5156
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005157 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005158 return NULL;
5159 if (failed)
5160 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005161 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005162 return NULL;
5163 }
5164 return newbuf;
5165}
5166
5167/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005168 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5169 * directory to "dirname_start" prior to returning, if autocmds or the
5170 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005171 */
5172 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005173wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005174{
5175 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005176 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005177#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005178 cleanup_T cs;
5179
5180 /* Reset the error/interrupt/exception state here so that aborting()
5181 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5182 * work when got_int is set. */
5183 enter_cleanup(&cs);
5184#endif
5185
Bram Moolenaar81695252004-12-29 20:58:21 +00005186 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005187
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005188#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005189 /* Restore the error/interrupt/exception state if not discarded by a
5190 * new aborting error, interrupt, or uncaught exception. */
5191 leave_cleanup(&cs);
5192#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005193 /* When autocommands/'autochdir' option changed directory: go back. */
5194 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005195 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005196}
5197
5198/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005199 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5200 * directory to "dirname_start" prior to returning, if autocmds or the
5201 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005202 */
5203 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005204unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005205{
5206 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005207 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005208 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005209
5210 /* When autocommands/'autochdir' option changed directory: go back. */
5211 restore_start_dir(dirname_start);
5212 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005213}
5214
Bram Moolenaar05159a02005-02-26 23:04:13 +00005215#if defined(FEAT_EVAL) || defined(PROTO)
5216/*
5217 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005218 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005219 */
5220 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005221get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005222{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005223 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005224 dict_T *dict;
5225 char_u buf[2];
5226 qfline_T *qfp;
5227 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005228 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005229
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005230 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005231 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005232 qi = &ql_info;
5233 if (wp != NULL)
5234 {
5235 qi = GET_LOC_LIST(wp);
5236 if (qi == NULL)
5237 return FAIL;
5238 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005239 }
5240
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005241 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005242 qf_idx = qi->qf_curlist;
5243
5244 if (qf_idx >= qi->qf_listcount
5245 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005246 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005247
Bram Moolenaard823fa92016-08-12 16:29:27 +02005248 qfp = qi->qf_lists[qf_idx].qf_start;
5249 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005250 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005251 /* Handle entries with a non-existing buffer number. */
5252 bufnum = qfp->qf_fnum;
5253 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5254 bufnum = 0;
5255
Bram Moolenaar05159a02005-02-26 23:04:13 +00005256 if ((dict = dict_alloc()) == NULL)
5257 return FAIL;
5258 if (list_append_dict(list, dict) == FAIL)
5259 return FAIL;
5260
5261 buf[0] = qfp->qf_type;
5262 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005263 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00005264 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
5265 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
5266 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
5267 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaard76ce852018-05-01 15:02:04 +02005268 || dict_add_nr_str(dict, "module", 0L,
5269 qfp->qf_module == NULL ? (char_u *)"" : qfp->qf_module) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00005270 || dict_add_nr_str(dict, "pattern", 0L,
5271 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
5272 || dict_add_nr_str(dict, "text", 0L,
5273 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00005274 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
5275 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
5276 return FAIL;
5277
5278 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005279 if (qfp == NULL)
5280 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005281 }
5282 return OK;
5283}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005284
5285/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005286 * Flags used by getqflist()/getloclist() to determine which fields to return.
5287 */
5288enum {
5289 QF_GETLIST_NONE = 0x0,
5290 QF_GETLIST_TITLE = 0x1,
5291 QF_GETLIST_ITEMS = 0x2,
5292 QF_GETLIST_NR = 0x4,
5293 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005294 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005295 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005296 QF_GETLIST_IDX = 0x40,
5297 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005298 QF_GETLIST_TICK = 0x100,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005299 QF_GETLIST_ALL = 0x1FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005300};
5301
5302/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005303 * Parse text from 'di' and return the quickfix list items.
5304 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005305 */
5306 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005307qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005308{
5309 int status = FAIL;
5310 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005311 char_u *errorformat = p_efm;
5312 dictitem_T *efm_di;
5313 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005314
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005315 /* Only a List value is supported */
5316 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005317 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005318 /* If errorformat is supplied then use it, otherwise use the 'efm'
5319 * option setting
5320 */
5321 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5322 {
5323 if (efm_di->di_tv.v_type != VAR_STRING ||
5324 efm_di->di_tv.vval.v_string == NULL)
5325 return FAIL;
5326 errorformat = efm_di->di_tv.vval.v_string;
5327 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005328
Bram Moolenaar36538222017-09-02 19:51:44 +02005329 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005330 if (l == NULL)
5331 return FAIL;
5332
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005333 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
5334 if (qi != NULL)
5335 {
5336 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
5337 qi->qf_refcount++;
5338
Bram Moolenaar36538222017-09-02 19:51:44 +02005339 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005340 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5341 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005342 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005343 qf_free(qi, 0);
5344 }
5345 free(qi);
5346 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005347 dict_add_list(retdict, "items", l);
5348 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005349 }
5350
5351 return status;
5352}
5353
5354/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005355 * Return the quickfix/location list window identifier in the current tabpage.
5356 */
5357 static int
5358qf_winid(qf_info_T *qi)
5359{
5360 win_T *win;
5361
5362 /* The quickfix window can be opened even if the quickfix list is not set
5363 * using ":copen". This is not true for location lists. */
5364 if (qi == NULL)
5365 return 0;
5366 win = qf_find_win(qi);
5367 if (win != NULL)
5368 return win->w_id;
5369 return 0;
5370}
5371
5372/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005373 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005374 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005375 static int
5376qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005377{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005378 int flags = QF_GETLIST_NONE;
5379
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005380 if (dict_find(what, (char_u *)"all", -1) != NULL)
5381 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005382
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005383 if (dict_find(what, (char_u *)"title", -1) != NULL)
5384 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005385
Bram Moolenaara6d48492017-12-12 22:45:31 +01005386 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5387 flags |= QF_GETLIST_NR;
5388
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005389 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5390 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005391
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005392 if (dict_find(what, (char_u *)"context", -1) != NULL)
5393 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005394
Bram Moolenaara6d48492017-12-12 22:45:31 +01005395 if (dict_find(what, (char_u *)"id", -1) != NULL)
5396 flags |= QF_GETLIST_ID;
5397
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005398 if (dict_find(what, (char_u *)"items", -1) != NULL)
5399 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005400
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005401 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5402 flags |= QF_GETLIST_IDX;
5403
5404 if (dict_find(what, (char_u *)"size", -1) != NULL)
5405 flags |= QF_GETLIST_SIZE;
5406
Bram Moolenaarb254af32017-12-18 19:48:58 +01005407 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5408 flags |= QF_GETLIST_TICK;
5409
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005410 return flags;
5411}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005412
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005413/*
5414 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5415 * If 'nr' and 'id' are not present in 'what' then return the current
5416 * quickfix list index.
5417 * If 'nr' is zero then return the current quickfix list index.
5418 * If 'nr' is '$' then return the last quickfix list index.
5419 * If 'id' is present then return the index of the quickfix list with that id.
5420 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5421 * Return -1, if quickfix list is not present or if the stack is empty.
5422 */
5423 static int
5424qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5425{
5426 int qf_idx;
5427 dictitem_T *di;
5428
5429 qf_idx = qi->qf_curlist; /* default is the current list */
5430 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5431 {
5432 /* Use the specified quickfix/location list */
5433 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005434 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005435 /* for zero use the current list */
5436 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005437 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005438 qf_idx = di->di_tv.vval.v_number - 1;
5439 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005440 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005441 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005442 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005443 else if (di->di_tv.v_type == VAR_STRING
5444 && di->di_tv.vval.v_string != NULL
5445 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5446 /* Get the last quickfix list number */
5447 qf_idx = qi->qf_listcount - 1;
5448 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005449 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005450 }
5451
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005452 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5453 {
5454 /* Look for a list with the specified id */
5455 if (di->di_tv.v_type == VAR_NUMBER)
5456 {
5457 /*
5458 * For zero, use the current list or the list specified by 'nr'
5459 */
5460 if (di->di_tv.vval.v_number != 0)
5461 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5462 }
5463 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005464 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005465 }
5466
5467 return qf_idx;
5468}
5469
5470/*
5471 * Return default values for quickfix list properties in retdict.
5472 */
5473 static int
5474qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5475{
5476 int status = OK;
5477
5478 if (flags & QF_GETLIST_TITLE)
5479 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5480 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5481 {
5482 list_T *l = list_alloc();
5483 if (l != NULL)
5484 status = dict_add_list(retdict, "items", l);
5485 else
5486 status = FAIL;
5487 }
5488 if ((status == OK) && (flags & QF_GETLIST_NR))
5489 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5490 if ((status == OK) && (flags & QF_GETLIST_WINID))
5491 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
5492 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5493 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5494 if ((status == OK) && (flags & QF_GETLIST_ID))
5495 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5496 if ((status == OK) && (flags & QF_GETLIST_IDX))
5497 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5498 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5499 status = dict_add_nr_str(retdict, "size", 0L, NULL);
5500 if ((status == OK) && (flags & QF_GETLIST_TICK))
5501 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
5502
5503 return status;
5504}
5505
5506/*
5507 * Return the quickfix list title as 'title' in retdict
5508 */
5509 static int
5510qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5511{
5512 char_u *t;
5513
5514 t = qi->qf_lists[qf_idx].qf_title;
5515 if (t == NULL)
5516 t = (char_u *)"";
5517 return dict_add_nr_str(retdict, "title", 0L, t);
5518}
5519
5520/*
5521 * Return the quickfix list items/entries as 'items' in retdict
5522 */
5523 static int
5524qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5525{
5526 int status = OK;
5527 list_T *l = list_alloc();
5528 if (l != NULL)
5529 {
5530 (void)get_errorlist(qi, NULL, qf_idx, l);
5531 dict_add_list(retdict, "items", l);
5532 }
5533 else
5534 status = FAIL;
5535
5536 return status;
5537}
5538
5539/*
5540 * Return the quickfix list context (if any) as 'context' in retdict.
5541 */
5542 static int
5543qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5544{
5545 int status;
5546 dictitem_T *di;
5547
5548 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5549 {
5550 di = dictitem_alloc((char_u *)"context");
5551 if (di != NULL)
5552 {
5553 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5554 status = dict_add(retdict, di);
5555 if (status == FAIL)
5556 dictitem_free(di);
5557 }
5558 else
5559 status = FAIL;
5560 }
5561 else
5562 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5563
5564 return status;
5565}
5566
5567/*
5568 * Return the quickfix list index as 'idx' in retdict
5569 */
5570 static int
5571qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5572{
5573 int idx = qi->qf_lists[qf_idx].qf_index;
5574 if (qi->qf_lists[qf_idx].qf_count == 0)
5575 /* For empty lists, qf_index is set to 1 */
5576 idx = 0;
5577 return dict_add_nr_str(retdict, "idx", idx, NULL);
5578}
5579
5580/*
5581 * Return quickfix/location list details (title) as a
5582 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5583 * then current list is used. Otherwise the specified list is used.
5584 */
5585 int
5586qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5587{
5588 qf_info_T *qi = &ql_info;
5589 int status = OK;
5590 int qf_idx;
5591 dictitem_T *di;
5592 int flags = QF_GETLIST_NONE;
5593
5594 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5595 return qf_get_list_from_lines(what, di, retdict);
5596
5597 if (wp != NULL)
5598 qi = GET_LOC_LIST(wp);
5599
5600 flags = qf_getprop_keys2flags(what);
5601
5602 if (qi != NULL && qi->qf_listcount != 0)
5603 qf_idx = qf_getprop_qfidx(qi, what);
5604
Bram Moolenaara6d48492017-12-12 22:45:31 +01005605 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005606 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005607 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005608
Bram Moolenaard823fa92016-08-12 16:29:27 +02005609 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005610 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005611 if ((status == OK) && (flags & QF_GETLIST_NR))
5612 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5613 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005614 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005615 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005616 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005617 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005618 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005619 if ((status == OK) && (flags & QF_GETLIST_ID))
5620 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5621 NULL);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005622 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005623 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005624 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5625 status = dict_add_nr_str(retdict, "size",
5626 qi->qf_lists[qf_idx].qf_count, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005627 if ((status == OK) && (flags & QF_GETLIST_TICK))
5628 status = dict_add_nr_str(retdict, "changedtick",
5629 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5630
Bram Moolenaard823fa92016-08-12 16:29:27 +02005631 return status;
5632}
5633
5634/*
5635 * Add list of entries to quickfix/location list. Each list entry is
5636 * a dictionary with item information.
5637 */
5638 static int
5639qf_add_entries(
5640 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005641 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005642 list_T *list,
5643 char_u *title,
5644 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005645{
5646 listitem_T *li;
5647 dict_T *d;
Bram Moolenaard76ce852018-05-01 15:02:04 +02005648 char_u *filename, *module, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005649 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005650 long lnum;
5651 int col, nr;
5652 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005653 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005654 int valid, status;
5655 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005656 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005657
Bram Moolenaara3921f42017-06-04 15:30:34 +02005658 if (action == ' ' || qf_idx == qi->qf_listcount)
5659 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005660 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005661 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005662 qf_idx = qi->qf_curlist;
5663 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005664 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005665 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005666 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005667 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005668 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005669 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005670 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005671 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005672
5673 for (li = list->lv_first; li != NULL; li = li->li_next)
5674 {
5675 if (li->li_tv.v_type != VAR_DICT)
5676 continue; /* Skip non-dict items */
5677
5678 d = li->li_tv.vval.v_dict;
5679 if (d == NULL)
5680 continue;
5681
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005682 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaard76ce852018-05-01 15:02:04 +02005683 module = get_dict_string(d, (char_u *)"module", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005684 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5685 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5686 col = (int)get_dict_number(d, (char_u *)"col");
5687 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5688 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005689 type = get_dict_string(d, (char_u *)"type", TRUE);
5690 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5691 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005692 if (text == NULL)
5693 text = vim_strsave((char_u *)"");
5694
5695 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005696 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005697 valid = FALSE;
5698
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005699 /* Mark entries with non-existing buffer number as not valid. Give the
5700 * error message only once. */
5701 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5702 {
5703 if (!did_bufnr_emsg)
5704 {
5705 did_bufnr_emsg = TRUE;
5706 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5707 }
5708 valid = FALSE;
5709 bufnum = 0;
5710 }
5711
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005712 /* If the 'valid' field is present it overrules the detected value. */
5713 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5714 valid = (int)get_dict_number(d, (char_u *)"valid");
5715
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005716 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005717 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005718 NULL, /* dir */
5719 filename,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005720 module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005721 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005722 text,
5723 lnum,
5724 col,
5725 vcol, /* vis_col */
5726 pattern, /* search pattern */
5727 nr,
5728 type == NULL ? NUL : *type,
5729 valid);
5730
5731 vim_free(filename);
Bram Moolenaard76ce852018-05-01 15:02:04 +02005732 vim_free(module);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005733 vim_free(pattern);
5734 vim_free(text);
5735 vim_free(type);
5736
5737 if (status == FAIL)
5738 {
5739 retval = FAIL;
5740 break;
5741 }
5742 }
5743
Bram Moolenaara3921f42017-06-04 15:30:34 +02005744 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005745 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005746 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005747 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005748 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005749 if (action != 'a')
5750 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005751 qi->qf_lists[qf_idx].qf_ptr =
5752 qi->qf_lists[qf_idx].qf_start;
5753 if (qi->qf_lists[qf_idx].qf_count > 0)
5754 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005755 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005756
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005757 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005758 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005759
5760 return retval;
5761}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005762
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005763/*
5764 * Get the quickfix list index from 'nr' or 'id'
5765 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02005766 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005767qf_setprop_get_qfidx(
5768 qf_info_T *qi,
5769 dict_T *what,
5770 int action,
5771 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005772{
5773 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005774 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005775
Bram Moolenaard823fa92016-08-12 16:29:27 +02005776 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5777 {
5778 /* Use the specified quickfix/location list */
5779 if (di->di_tv.v_type == VAR_NUMBER)
5780 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005781 /* for zero use the current list */
5782 if (di->di_tv.vval.v_number != 0)
5783 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005784
Bram Moolenaar55b69262017-08-13 13:42:01 +02005785 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5786 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005787 /*
5788 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005789 * non-available list and add the new list at the end of the
5790 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005791 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005792 *newlist = TRUE;
5793 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005794 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005795 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005796 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005797 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005798 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005799 }
5800 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005801 && di->di_tv.vval.v_string != NULL
5802 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005803 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005804 if (qi->qf_listcount > 0)
5805 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005806 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02005807 qf_idx = 0;
5808 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005809 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005810 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005811 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005812 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005813 }
5814
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005815 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005816 {
5817 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005818 if (di->di_tv.v_type != VAR_NUMBER)
5819 return INVALID_QFIDX;
5820
5821 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005822 }
5823
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005824 return qf_idx;
5825}
5826
5827/*
5828 * Set the quickfix list title.
5829 */
5830 static int
5831qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
5832{
5833 if (di->di_tv.v_type != VAR_STRING)
5834 return FAIL;
5835
5836 vim_free(qi->qf_lists[qf_idx].qf_title);
5837 qi->qf_lists[qf_idx].qf_title =
5838 get_dict_string(what, (char_u *)"title", TRUE);
5839 if (qf_idx == qi->qf_curlist)
5840 qf_update_win_titlevar(qi);
5841
5842 return OK;
5843}
5844
5845/*
5846 * Set quickfix list items/entries.
5847 */
5848 static int
5849qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
5850{
5851 int retval = FAIL;
5852 char_u *title_save;
5853
5854 if (di->di_tv.v_type != VAR_LIST)
5855 return FAIL;
5856
5857 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5858 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5859 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005860 vim_free(title_save);
5861
5862 return retval;
5863}
5864
5865/*
5866 * Set quickfix list items/entries from a list of lines.
5867 */
5868 static int
5869qf_setprop_items_from_lines(
5870 qf_info_T *qi,
5871 int qf_idx,
5872 dict_T *what,
5873 dictitem_T *di,
5874 int action)
5875{
5876 char_u *errorformat = p_efm;
5877 dictitem_T *efm_di;
5878 int retval = FAIL;
5879
5880 /* Use the user supplied errorformat settings (if present) */
5881 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5882 {
5883 if (efm_di->di_tv.v_type != VAR_STRING ||
5884 efm_di->di_tv.vval.v_string == NULL)
5885 return FAIL;
5886 errorformat = efm_di->di_tv.vval.v_string;
5887 }
5888
5889 /* Only a List value is supported */
5890 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
5891 return FAIL;
5892
5893 if (action == 'r')
5894 qf_free_items(qi, qf_idx);
5895 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
5896 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5897 retval = OK;
5898
5899 return retval;
5900}
5901
5902/*
5903 * Set quickfix list context.
5904 */
5905 static int
5906qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
5907{
5908 typval_T *ctx;
5909
5910 free_tv(qi->qf_lists[qf_idx].qf_ctx);
5911 ctx = alloc_tv();
5912 if (ctx != NULL)
5913 copy_tv(&di->di_tv, ctx);
5914 qi->qf_lists[qf_idx].qf_ctx = ctx;
5915
5916 return OK;
5917}
5918
5919/*
5920 * Set quickfix/location list properties (title, items, context).
5921 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02005922 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005923 */
5924 static int
5925qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
5926{
5927 dictitem_T *di;
5928 int retval = FAIL;
5929 int qf_idx;
5930 int newlist = FALSE;
5931
5932 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5933 newlist = TRUE;
5934
5935 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
5936 if (qf_idx == INVALID_QFIDX) /* List not found */
5937 return FAIL;
5938
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005939 if (newlist)
5940 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005941 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005942 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005943 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005944 }
5945
5946 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005947 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005948 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005949 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005950 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005951 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005952 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005953 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005954
Bram Moolenaarb254af32017-12-18 19:48:58 +01005955 if (retval == OK)
5956 qf_list_changed(qi, qf_idx);
5957
Bram Moolenaard823fa92016-08-12 16:29:27 +02005958 return retval;
5959}
5960
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005961/*
5962 * Find the non-location list window with the specified location list.
5963 */
5964 static win_T *
5965find_win_with_ll(qf_info_T *qi)
5966{
5967 win_T *wp = NULL;
5968
5969 FOR_ALL_WINDOWS(wp)
5970 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5971 return wp;
5972
5973 return NULL;
5974}
5975
5976/*
5977 * Free the entire quickfix/location list stack.
5978 * If the quickfix/location list window is open, then clear it.
5979 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005980 static void
5981qf_free_stack(win_T *wp, qf_info_T *qi)
5982{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005983 win_T *qfwin = qf_find_win(qi);
5984 win_T *llwin = NULL;
5985 win_T *orig_wp = wp;
5986
5987 if (qfwin != NULL)
5988 {
5989 /* If the quickfix/location list window is open, then clear it */
5990 if (qi->qf_curlist < qi->qf_listcount)
5991 qf_free(qi, qi->qf_curlist);
5992 qf_update_buffer(qi, NULL);
5993 }
5994
5995 if (wp != NULL && IS_LL_WINDOW(wp))
5996 {
5997 /* If in the location list window, then use the non-location list
5998 * window with this location list (if present)
5999 */
6000 llwin = find_win_with_ll(qi);
6001 if (llwin != NULL)
6002 wp = llwin;
6003 }
6004
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006005 qf_free_all(wp);
6006 if (wp == NULL)
6007 {
6008 /* quickfix list */
6009 qi->qf_curlist = 0;
6010 qi->qf_listcount = 0;
6011 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006012 else if (IS_LL_WINDOW(orig_wp))
6013 {
6014 /* If the location list window is open, then create a new empty
6015 * location list */
6016 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006017
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006018 /* first free the list reference in the location list window */
6019 ll_free_all(&orig_wp->w_llist_ref);
6020
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006021 orig_wp->w_llist_ref = new_ll;
6022 if (llwin != NULL)
6023 {
6024 llwin->w_llist = new_ll;
6025 new_ll->qf_refcount++;
6026 }
6027 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006028}
6029
Bram Moolenaard823fa92016-08-12 16:29:27 +02006030/*
6031 * Populate the quickfix list with the items supplied in the list
6032 * of dictionaries. "title" will be copied to w:quickfix_title.
6033 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6034 */
6035 int
6036set_errorlist(
6037 win_T *wp,
6038 list_T *list,
6039 int action,
6040 char_u *title,
6041 dict_T *what)
6042{
6043 qf_info_T *qi = &ql_info;
6044 int retval = OK;
6045
6046 if (wp != NULL)
6047 {
6048 qi = ll_get_or_alloc_list(wp);
6049 if (qi == NULL)
6050 return FAIL;
6051 }
6052
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006053 if (action == 'f')
6054 {
6055 /* Free the entire quickfix or location list stack */
6056 qf_free_stack(wp, qi);
6057 }
6058 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006059 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006060 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006061 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006062 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006063 if (retval == OK)
6064 qf_list_changed(qi, qi->qf_curlist);
6065 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006066
6067 return retval;
6068}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006069
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006070/*
6071 * Mark the context as in use for all the lists in a quickfix stack.
6072 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006073 static int
6074mark_quickfix_ctx(qf_info_T *qi, int copyID)
6075{
6076 int i;
6077 int abort = FALSE;
6078 typval_T *ctx;
6079
6080 for (i = 0; i < LISTCOUNT && !abort; ++i)
6081 {
6082 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006083 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6084 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006085 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6086 }
6087
6088 return abort;
6089}
6090
6091/*
6092 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006093 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006094 */
6095 int
6096set_ref_in_quickfix(int copyID)
6097{
6098 int abort = FALSE;
6099 tabpage_T *tp;
6100 win_T *win;
6101
6102 abort = mark_quickfix_ctx(&ql_info, copyID);
6103 if (abort)
6104 return abort;
6105
6106 FOR_ALL_TAB_WINDOWS(tp, win)
6107 {
6108 if (win->w_llist != NULL)
6109 {
6110 abort = mark_quickfix_ctx(win->w_llist, copyID);
6111 if (abort)
6112 return abort;
6113 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006114 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6115 {
6116 /* In a location list window and none of the other windows is
6117 * referring to this location list. Mark the location list
6118 * context as still in use.
6119 */
6120 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6121 if (abort)
6122 return abort;
6123 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006124 }
6125
6126 return abort;
6127}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006128#endif
6129
Bram Moolenaar81695252004-12-29 20:58:21 +00006130/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006131 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006132 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006133 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006134 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006135 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006136 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006137 */
6138 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006139ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006140{
6141 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006142 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006143 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006144 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006145
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006146 switch (eap->cmdidx)
6147 {
6148 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6149 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6150 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6151 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6152 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6153 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6154 default: break;
6155 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006156 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6157 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006158 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006159#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006160 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006161 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006162#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006163 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006164
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006165 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01006166 if (eap->cmdidx == CMD_lbuffer
6167 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006168 || eap->cmdidx == CMD_laddbuffer)
6169 {
6170 qi = ll_get_or_alloc_list(curwin);
6171 if (qi == NULL)
6172 return;
6173 }
6174
Bram Moolenaar86b68352004-12-27 21:59:20 +00006175 if (*eap->arg == NUL)
6176 buf = curbuf;
6177 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6178 buf = buflist_findnr(atoi((char *)eap->arg));
6179 if (buf == NULL)
6180 EMSG(_(e_invarg));
6181 else if (buf->b_ml.ml_mfp == NULL)
6182 EMSG(_("E681: Buffer is not loaded"));
6183 else
6184 {
6185 if (eap->addr_count == 0)
6186 {
6187 eap->line1 = 1;
6188 eap->line2 = buf->b_ml.ml_line_count;
6189 }
6190 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6191 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6192 EMSG(_(e_invrange));
6193 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006194 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006195 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006196
6197 if (buf->b_sfname)
6198 {
6199 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6200 (char *)qf_title, (char *)buf->b_sfname);
6201 qf_title = IObuff;
6202 }
6203
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006204 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006205 (eap->cmdidx != CMD_caddbuffer
6206 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006207 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006208 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006209 if (res >= 0)
6210 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006211 if (au_name != NULL)
6212 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6213 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006214 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
6215 eap->cmdidx == CMD_lbuffer))
6216 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006217 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006218 }
6219}
6220
Bram Moolenaar1e015462005-09-25 22:16:38 +00006221#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006222/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006223 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6224 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006225 */
6226 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006227ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006228{
6229 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006230 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006231 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006232 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006233
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006234 switch (eap->cmdidx)
6235 {
6236 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6237 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6238 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6239 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6240 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6241 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6242 default: break;
6243 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006244 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6245 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006246 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006247#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006248 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006249 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006250#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006251 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006252
Bram Moolenaar3c097222017-12-21 20:54:49 +01006253 if (eap->cmdidx == CMD_lexpr
6254 || eap->cmdidx == CMD_lgetexpr
6255 || eap->cmdidx == CMD_laddexpr)
6256 {
6257 qi = ll_get_or_alloc_list(curwin);
6258 if (qi == NULL)
6259 return;
6260 }
6261
Bram Moolenaar4770d092006-01-12 23:22:24 +00006262 /* Evaluate the expression. When the result is a string or a list we can
6263 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006264 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006265 if (tv != NULL)
6266 {
6267 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6268 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6269 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006270 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006271 (eap->cmdidx != CMD_caddexpr
6272 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006273 (linenr_T)0, (linenr_T)0,
6274 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006275 if (res >= 0)
6276 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006277 if (au_name != NULL)
6278 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6279 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006280 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
6281 eap->cmdidx == CMD_lexpr))
6282 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006283 }
6284 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006285 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006286 free_tv(tv);
6287 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006288}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006289#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006290
6291/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006292 * Get the location list for ":lhelpgrep"
6293 */
6294 static qf_info_T *
6295hgr_get_ll(int *new_ll)
6296{
6297 win_T *wp;
6298 qf_info_T *qi;
6299
6300 /* If the current window is a help window, then use it */
6301 if (bt_help(curwin->w_buffer))
6302 wp = curwin;
6303 else
6304 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006305 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006306
6307 if (wp == NULL) /* Help window not found */
6308 qi = NULL;
6309 else
6310 qi = wp->w_llist;
6311
6312 if (qi == NULL)
6313 {
6314 /* Allocate a new location list for help text matches */
6315 if ((qi = ll_new_list()) == NULL)
6316 return NULL;
6317 *new_ll = TRUE;
6318 }
6319
6320 return qi;
6321}
6322
6323/*
6324 * Search for a pattern in a help file.
6325 */
6326 static void
6327hgr_search_file(
6328 qf_info_T *qi,
6329 char_u *fname,
6330#ifdef FEAT_MBYTE
6331 vimconv_T *p_vc,
6332#endif
6333 regmatch_T *p_regmatch)
6334{
6335 FILE *fd;
6336 long lnum;
6337
6338 fd = mch_fopen((char *)fname, "r");
6339 if (fd == NULL)
6340 return;
6341
6342 lnum = 1;
6343 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6344 {
6345 char_u *line = IObuff;
6346#ifdef FEAT_MBYTE
6347 /* Convert a line if 'encoding' is not utf-8 and
6348 * the line contains a non-ASCII character. */
6349 if (p_vc->vc_type != CONV_NONE
6350 && has_non_ascii(IObuff))
6351 {
6352 line = string_convert(p_vc, IObuff, NULL);
6353 if (line == NULL)
6354 line = IObuff;
6355 }
6356#endif
6357
6358 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6359 {
6360 int l = (int)STRLEN(line);
6361
6362 /* remove trailing CR, LF, spaces, etc. */
6363 while (l > 0 && line[l - 1] <= ' ')
6364 line[--l] = NUL;
6365
6366 if (qf_add_entry(qi,
6367 qi->qf_curlist,
6368 NULL, /* dir */
6369 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006370 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006371 0,
6372 line,
6373 lnum,
6374 (int)(p_regmatch->startp[0] - line)
6375 + 1, /* col */
6376 FALSE, /* vis_col */
6377 NULL, /* search pattern */
6378 0, /* nr */
6379 1, /* type */
6380 TRUE /* valid */
6381 ) == FAIL)
6382 {
6383 got_int = TRUE;
6384#ifdef FEAT_MBYTE
6385 if (line != IObuff)
6386 vim_free(line);
6387#endif
6388 break;
6389 }
6390 }
6391#ifdef FEAT_MBYTE
6392 if (line != IObuff)
6393 vim_free(line);
6394#endif
6395 ++lnum;
6396 line_breakcheck();
6397 }
6398 fclose(fd);
6399}
6400
6401/*
6402 * Search for a pattern in all the help files in the doc directory under
6403 * the given directory.
6404 */
6405 static void
6406hgr_search_files_in_dir(
6407 qf_info_T *qi,
6408 char_u *dirname,
6409 regmatch_T *p_regmatch
6410#ifdef FEAT_MBYTE
6411 , vimconv_T *p_vc
6412#endif
6413#ifdef FEAT_MULTI_LANG
6414 , char_u *lang
6415#endif
6416 )
6417{
6418 int fcount;
6419 char_u **fnames;
6420 int fi;
6421
6422 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6423 add_pathsep(dirname);
6424 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6425 if (gen_expand_wildcards(1, &dirname, &fcount,
6426 &fnames, EW_FILE|EW_SILENT) == OK
6427 && fcount > 0)
6428 {
6429 for (fi = 0; fi < fcount && !got_int; ++fi)
6430 {
6431#ifdef FEAT_MULTI_LANG
6432 /* Skip files for a different language. */
6433 if (lang != NULL
6434 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006435 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006436 && !(STRNICMP(lang, "en", 2) == 0
6437 && STRNICMP("txt", fnames[fi]
6438 + STRLEN(fnames[fi]) - 3, 3) == 0))
6439 continue;
6440#endif
6441
6442 hgr_search_file(qi, fnames[fi],
6443#ifdef FEAT_MBYTE
6444 p_vc,
6445#endif
6446 p_regmatch);
6447 }
6448 FreeWild(fcount, fnames);
6449 }
6450}
6451
6452/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006453 * Search for a pattern in all the help files in the 'runtimepath'
6454 * and add the matches to a quickfix list.
6455 * 'arg' is the language specifier. If supplied, then only matches in the
6456 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006457 */
6458 static void
6459hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6460{
6461 char_u *p;
6462#ifdef FEAT_MULTI_LANG
6463 char_u *lang;
6464#endif
6465
6466#ifdef FEAT_MBYTE
6467 vimconv_T vc;
6468
6469 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6470 * differs. */
6471 vc.vc_type = CONV_NONE;
6472 if (!enc_utf8)
6473 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6474#endif
6475
6476#ifdef FEAT_MULTI_LANG
6477 /* Check for a specified language */
6478 lang = check_help_lang(arg);
6479#endif
6480
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006481 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006482 p = p_rtp;
6483 while (*p != NUL && !got_int)
6484 {
6485 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6486
6487 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6488#ifdef FEAT_MBYTE
6489 , &vc
6490#endif
6491#ifdef FEAT_MULTI_LANG
6492 , lang
6493#endif
6494 );
6495 }
6496
6497#ifdef FEAT_MBYTE
6498 if (vc.vc_type != CONV_NONE)
6499 convert_setup(&vc, NULL, NULL);
6500#endif
6501}
6502
6503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 * ":helpgrep {pattern}"
6505 */
6506 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006507ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
6509 regmatch_T regmatch;
6510 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006511 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006512 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006513 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
Bram Moolenaar73633f82012-01-20 13:39:07 +01006515 switch (eap->cmdidx)
6516 {
6517 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6518 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6519 default: break;
6520 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006521 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6522 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006523 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006524#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006525 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006526 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006527#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006528 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006529
6530 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6531 save_cpo = p_cpo;
6532 p_cpo = empty_option;
6533
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006534 if (eap->cmdidx == CMD_lhelpgrep)
6535 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006536 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006537 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006538 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006539 }
6540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6542 regmatch.rm_ic = FALSE;
6543 if (regmatch.regprog != NULL)
6544 {
6545 /* create a new quickfix list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006546 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006548 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006549
Bram Moolenaar473de612013-06-08 18:19:48 +02006550 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006552 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6553 qi->qf_lists[qi->qf_curlist].qf_ptr =
6554 qi->qf_lists[qi->qf_curlist].qf_start;
6555 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006558 if (p_cpo == empty_option)
6559 p_cpo = save_cpo;
6560 else
6561 /* Darn, some plugin changed the value. */
6562 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
Bram Moolenaarb254af32017-12-18 19:48:58 +01006564 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006565 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaar73633f82012-01-20 13:39:07 +01006567 if (au_name != NULL)
6568 {
6569 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6570 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006571 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006572 /* autocommands made "qi" invalid */
6573 return;
6574 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006575
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006577 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006578 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006579 else
6580 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006581
6582 if (eap->cmdidx == CMD_lhelpgrep)
6583 {
6584 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006585 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006586 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006587 {
6588 if (new_qi)
6589 ll_free_all(&qi);
6590 }
6591 else if (curwin->w_llist == NULL)
6592 curwin->w_llist = qi;
6593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594}
6595
6596#endif /* FEAT_QUICKFIX */