blob: 9e21191e705f1faeceda6f340a28e68363866227 [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/*
231 * Converts a 'errorformat' string to regular expression pattern
232 */
233 static int
234efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200235 char_u *efm,
236 int len,
237 efm_T *fmt_ptr,
238 char_u *regpat,
239 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200240{
241 char_u *ptr;
242 char_u *efmp;
243 char_u *srcptr;
244 int round;
245 int idx = 0;
246
247 /*
248 * Build regexp pattern from current 'errorformat' option
249 */
250 ptr = regpat;
251 *ptr++ = '^';
252 round = 0;
253 for (efmp = efm; efmp < efm + len; ++efmp)
254 {
255 if (*efmp == '%')
256 {
257 ++efmp;
258 for (idx = 0; idx < FMT_PATTERNS; ++idx)
259 if (fmt_pat[idx].convchar == *efmp)
260 break;
261 if (idx < FMT_PATTERNS)
262 {
263 if (fmt_ptr->addr[idx])
264 {
265 sprintf((char *)errmsg,
266 _("E372: Too many %%%c in format string"), *efmp);
267 EMSG(errmsg);
268 return -1;
269 }
270 if ((idx
271 && idx < 6
272 && vim_strchr((char_u *)"DXOPQ",
273 fmt_ptr->prefix) != NULL)
274 || (idx == 6
275 && vim_strchr((char_u *)"OPQ",
276 fmt_ptr->prefix) == NULL))
277 {
278 sprintf((char *)errmsg,
279 _("E373: Unexpected %%%c in format string"), *efmp);
280 EMSG(errmsg);
281 return -1;
282 }
283 fmt_ptr->addr[idx] = (char_u)++round;
284 *ptr++ = '\\';
285 *ptr++ = '(';
286#ifdef BACKSLASH_IN_FILENAME
287 if (*efmp == 'f')
288 {
289 /* Also match "c:" in the file name, even when
290 * checking for a colon next: "%f:".
291 * "\%(\a:\)\=" */
292 STRCPY(ptr, "\\%(\\a:\\)\\=");
293 ptr += 10;
294 }
295#endif
296 if (*efmp == 'f' && efmp[1] != NUL)
297 {
298 if (efmp[1] != '\\' && efmp[1] != '%')
299 {
300 /* A file name may contain spaces, but this isn't
301 * in "\f". For "%f:%l:%m" there may be a ":" in
302 * the file name. Use ".\{-1,}x" instead (x is
303 * the next character), the requirement that :999:
304 * follows should work. */
305 STRCPY(ptr, ".\\{-1,}");
306 ptr += 7;
307 }
308 else
309 {
310 /* File name followed by '\\' or '%': include as
311 * many file name chars as possible. */
312 STRCPY(ptr, "\\f\\+");
313 ptr += 4;
314 }
315 }
316 else
317 {
318 srcptr = (char_u *)fmt_pat[idx].pattern;
319 while ((*ptr = *srcptr++) != NUL)
320 ++ptr;
321 }
322 *ptr++ = '\\';
323 *ptr++ = ')';
324 }
325 else if (*efmp == '*')
326 {
327 if (*++efmp == '[' || *efmp == '\\')
328 {
329 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
330 {
331 if (efmp[1] == '^')
332 *ptr++ = *++efmp;
333 if (efmp < efm + len)
334 {
335 *ptr++ = *++efmp; /* could be ']' */
336 while (efmp < efm + len
337 && (*ptr++ = *++efmp) != ']')
338 /* skip */;
339 if (efmp == efm + len)
340 {
341 EMSG(_("E374: Missing ] in format string"));
342 return -1;
343 }
344 }
345 }
346 else if (efmp < efm + len) /* %*\D, %*\s etc. */
347 *ptr++ = *++efmp;
348 *ptr++ = '\\';
349 *ptr++ = '+';
350 }
351 else
352 {
353 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
354 sprintf((char *)errmsg,
355 _("E375: Unsupported %%%c in format string"), *efmp);
356 EMSG(errmsg);
357 return -1;
358 }
359 }
360 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
361 *ptr++ = *efmp; /* regexp magic characters */
362 else if (*efmp == '#')
363 *ptr++ = '*';
364 else if (*efmp == '>')
365 fmt_ptr->conthere = TRUE;
366 else if (efmp == efm + 1) /* analyse prefix */
367 {
368 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
369 fmt_ptr->flags = *efmp++;
370 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
371 fmt_ptr->prefix = *efmp;
372 else
373 {
374 sprintf((char *)errmsg,
375 _("E376: Invalid %%%c in format string prefix"), *efmp);
376 EMSG(errmsg);
377 return -1;
378 }
379 }
380 else
381 {
382 sprintf((char *)errmsg,
383 _("E377: Invalid %%%c in format string"), *efmp);
384 EMSG(errmsg);
385 return -1;
386 }
387 }
388 else /* copy normal character */
389 {
390 if (*efmp == '\\' && efmp + 1 < efm + len)
391 ++efmp;
392 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
393 *ptr++ = '\\'; /* escape regexp atoms */
394 if (*efmp)
395 *ptr++ = *efmp;
396 }
397 }
398 *ptr++ = '$';
399 *ptr = NUL;
400
401 return 0;
402}
403
404 static void
405free_efm_list(efm_T **efm_first)
406{
407 efm_T *efm_ptr;
408
409 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
410 {
411 *efm_first = efm_ptr->next;
412 vim_regfree(efm_ptr->prog);
413 vim_free(efm_ptr);
414 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100415 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200416}
417
418/* Parse 'errorformat' option */
419 static efm_T *
420parse_efm_option(char_u *efm)
421{
422 char_u *errmsg = NULL;
423 int errmsglen;
424 efm_T *fmt_ptr = NULL;
425 efm_T *fmt_first = NULL;
426 efm_T *fmt_last = NULL;
427 char_u *fmtstr = NULL;
428 int len;
429 int i;
430 int round;
431
432 errmsglen = CMDBUFFSIZE + 1;
433 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
434 if (errmsg == NULL)
435 goto parse_efm_end;
436
437 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200438 * Each part of the format string is copied and modified from errorformat
439 * to regex prog. Only a few % characters are allowed.
440 */
441
442 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 * Get some space to modify the format string into.
444 */
445 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
446 for (round = FMT_PATTERNS; round > 0; )
447 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200448#ifdef BACKSLASH_IN_FILENAME
449 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200450#else
451 i += 2; /* "%f" can become two chars longer */
452#endif
453 if ((fmtstr = alloc(i)) == NULL)
454 goto parse_efm_error;
455
456 while (efm[0] != NUL)
457 {
458 /*
459 * Allocate a new eformat structure and put it at the end of the list
460 */
461 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
462 if (fmt_ptr == NULL)
463 goto parse_efm_error;
464 if (fmt_first == NULL) /* first one */
465 fmt_first = fmt_ptr;
466 else
467 fmt_last->next = fmt_ptr;
468 fmt_last = fmt_ptr;
469
470 /*
471 * Isolate one part in the 'errorformat' option
472 */
473 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
474 if (efm[len] == '\\' && efm[len + 1] != NUL)
475 ++len;
476
477 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
478 goto parse_efm_error;
479 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
480 goto parse_efm_error;
481 /*
482 * Advance to next part
483 */
484 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
485 }
486
487 if (fmt_first == NULL) /* nothing found */
488 EMSG(_("E378: 'errorformat' contains no pattern"));
489
490 goto parse_efm_end;
491
492parse_efm_error:
493 free_efm_list(&fmt_first);
494
495parse_efm_end:
496 vim_free(fmtstr);
497 vim_free(errmsg);
498
499 return fmt_first;
500}
501
Bram Moolenaare0d37972016-07-15 22:36:01 +0200502enum {
503 QF_FAIL = 0,
504 QF_OK = 1,
505 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200506 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200507 QF_IGNORE_LINE = 4,
508 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200509};
510
511typedef struct {
512 char_u *linebuf;
513 int linelen;
514 char_u *growbuf;
515 int growbufsiz;
516 FILE *fd;
517 typval_T *tv;
518 char_u *p_str;
519 listitem_T *p_li;
520 buf_T *buf;
521 linenr_T buflnum;
522 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100523 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200524} qfstate_T;
525
526 static char_u *
527qf_grow_linebuf(qfstate_T *state, int newsz)
528{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200529 char_u *p;
530
Bram Moolenaare0d37972016-07-15 22:36:01 +0200531 /*
532 * If the line exceeds LINE_MAXLEN exclude the last
533 * byte since it's not a NL character.
534 */
535 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
536 if (state->growbuf == NULL)
537 {
538 state->growbuf = alloc(state->linelen + 1);
539 if (state->growbuf == NULL)
540 return NULL;
541 state->growbufsiz = state->linelen;
542 }
543 else if (state->linelen > state->growbufsiz)
544 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200545 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200546 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200547 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200548 state->growbufsiz = state->linelen;
549 }
550 return state->growbuf;
551}
552
553/*
554 * Get the next string (separated by newline) from state->p_str.
555 */
556 static int
557qf_get_next_str_line(qfstate_T *state)
558{
559 /* Get the next line from the supplied string */
560 char_u *p_str = state->p_str;
561 char_u *p;
562 int len;
563
564 if (*p_str == NUL) /* Reached the end of the string */
565 return QF_END_OF_INPUT;
566
567 p = vim_strchr(p_str, '\n');
568 if (p != NULL)
569 len = (int)(p - p_str) + 1;
570 else
571 len = (int)STRLEN(p_str);
572
573 if (len > IOSIZE - 2)
574 {
575 state->linebuf = qf_grow_linebuf(state, len);
576 if (state->linebuf == NULL)
577 return QF_NOMEM;
578 }
579 else
580 {
581 state->linebuf = IObuff;
582 state->linelen = len;
583 }
584 vim_strncpy(state->linebuf, p_str, state->linelen);
585
586 /*
587 * Increment using len in order to discard the rest of the
588 * line if it exceeds LINE_MAXLEN.
589 */
590 p_str += len;
591 state->p_str = p_str;
592
593 return QF_OK;
594}
595
596/*
597 * Get the next string from state->p_Li.
598 */
599 static int
600qf_get_next_list_line(qfstate_T *state)
601{
602 listitem_T *p_li = state->p_li;
603 int len;
604
605 while (p_li != NULL
606 && (p_li->li_tv.v_type != VAR_STRING
607 || p_li->li_tv.vval.v_string == NULL))
608 p_li = p_li->li_next; /* Skip non-string items */
609
610 if (p_li == NULL) /* End of the list */
611 {
612 state->p_li = NULL;
613 return QF_END_OF_INPUT;
614 }
615
616 len = (int)STRLEN(p_li->li_tv.vval.v_string);
617 if (len > IOSIZE - 2)
618 {
619 state->linebuf = qf_grow_linebuf(state, len);
620 if (state->linebuf == NULL)
621 return QF_NOMEM;
622 }
623 else
624 {
625 state->linebuf = IObuff;
626 state->linelen = len;
627 }
628
629 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
630
631 state->p_li = p_li->li_next; /* next item */
632 return QF_OK;
633}
634
635/*
636 * Get the next string from state->buf.
637 */
638 static int
639qf_get_next_buf_line(qfstate_T *state)
640{
641 char_u *p_buf = NULL;
642 int len;
643
644 /* Get the next line from the supplied buffer */
645 if (state->buflnum > state->lnumlast)
646 return QF_END_OF_INPUT;
647
648 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
649 state->buflnum += 1;
650
651 len = (int)STRLEN(p_buf);
652 if (len > IOSIZE - 2)
653 {
654 state->linebuf = qf_grow_linebuf(state, len);
655 if (state->linebuf == NULL)
656 return QF_NOMEM;
657 }
658 else
659 {
660 state->linebuf = IObuff;
661 state->linelen = len;
662 }
663 vim_strncpy(state->linebuf, p_buf, state->linelen);
664
665 return QF_OK;
666}
667
668/*
669 * Get the next string from file state->fd.
670 */
671 static int
672qf_get_next_file_line(qfstate_T *state)
673{
674 int discard;
675 int growbuflen;
676
677 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
678 return QF_END_OF_INPUT;
679
680 discard = FALSE;
681 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200682 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200683 {
684 /*
685 * The current line exceeds IObuff, continue reading using
686 * growbuf until EOL or LINE_MAXLEN bytes is read.
687 */
688 if (state->growbuf == NULL)
689 {
690 state->growbufsiz = 2 * (IOSIZE - 1);
691 state->growbuf = alloc(state->growbufsiz);
692 if (state->growbuf == NULL)
693 return QF_NOMEM;
694 }
695
696 /* Copy the read part of the line, excluding null-terminator */
697 memcpy(state->growbuf, IObuff, IOSIZE - 1);
698 growbuflen = state->linelen;
699
700 for (;;)
701 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200702 char_u *p;
703
Bram Moolenaare0d37972016-07-15 22:36:01 +0200704 if (fgets((char *)state->growbuf + growbuflen,
705 state->growbufsiz - growbuflen, state->fd) == NULL)
706 break;
707 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
708 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200709 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200710 break;
711 if (state->growbufsiz == LINE_MAXLEN)
712 {
713 discard = TRUE;
714 break;
715 }
716
717 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
718 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200719 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200720 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200721 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200722 }
723
724 while (discard)
725 {
726 /*
727 * The current line is longer than LINE_MAXLEN, continue
728 * reading but discard everything until EOL or EOF is
729 * reached.
730 */
731 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
732 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200733 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200734 break;
735 }
736
737 state->linebuf = state->growbuf;
738 state->linelen = growbuflen;
739 }
740 else
741 state->linebuf = IObuff;
742
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100743#ifdef FEAT_MBYTE
744 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200745 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
746 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100747 char_u *line;
748
749 line = string_convert(&state->vc, state->linebuf, &state->linelen);
750 if (line != NULL)
751 {
752 if (state->linelen < IOSIZE)
753 {
754 STRCPY(state->linebuf, line);
755 vim_free(line);
756 }
757 else
758 {
759 vim_free(state->growbuf);
760 state->linebuf = state->growbuf = line;
761 state->growbufsiz = state->linelen < LINE_MAXLEN
762 ? state->linelen : LINE_MAXLEN;
763 }
764 }
765 }
766#endif
767
Bram Moolenaare0d37972016-07-15 22:36:01 +0200768 return QF_OK;
769}
770
771/*
772 * Get the next string from a file/buffer/list/string.
773 */
774 static int
775qf_get_nextline(qfstate_T *state)
776{
777 int status = QF_FAIL;
778
779 if (state->fd == NULL)
780 {
781 if (state->tv != NULL)
782 {
783 if (state->tv->v_type == VAR_STRING)
784 /* Get the next line from the supplied string */
785 status = qf_get_next_str_line(state);
786 else if (state->tv->v_type == VAR_LIST)
787 /* Get the next line from the supplied list */
788 status = qf_get_next_list_line(state);
789 }
790 else
791 /* Get the next line from the supplied buffer */
792 status = qf_get_next_buf_line(state);
793 }
794 else
795 /* Get the next line from the supplied file */
796 status = qf_get_next_file_line(state);
797
798 if (status != QF_OK)
799 return status;
800
801 /* remove newline/CR from the line */
802 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200803 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804 state->linebuf[state->linelen - 1] = NUL;
805#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200806 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
807 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200809 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200810
811#ifdef FEAT_MBYTE
812 remove_bom(state->linebuf);
813#endif
814
815 return QF_OK;
816}
817
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200818typedef struct {
819 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200820 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200821 char_u *errmsg;
822 int errmsglen;
823 long lnum;
824 int col;
825 char_u use_viscol;
826 char_u *pattern;
827 int enr;
828 int type;
829 int valid;
830} qffields_T;
831
832/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200833 * Parse the error format matches in 'regmatch' and set the values in 'fields'.
834 * fmt_ptr contains the 'efm' format specifiers/prefixes that have a match.
835 * Returns QF_OK if all the matches are successfully parsed. On failure,
836 * returns QF_FAIL or QF_NOMEM.
837 */
838 static int
839qf_parse_match(
840 char_u *linebuf,
841 int linelen,
842 efm_T *fmt_ptr,
843 regmatch_T *regmatch,
844 qffields_T *fields,
845 int qf_multiline,
846 int qf_multiscan,
847 char_u **tail)
848{
849 char_u *p;
850 int idx = fmt_ptr->prefix;
851 int i;
852 int len;
853
854 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
855 return QF_FAIL;
856 if (vim_strchr((char_u *)"EWI", idx) != NULL)
857 fields->type = idx;
858 else
859 fields->type = 0;
860 /*
861 * Extract error message data from matched line.
862 * We check for an actual submatch, because "\[" and "\]" in
863 * the 'errorformat' may cause the wrong submatch to be used.
864 */
865 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
866 {
867 int c;
868
869 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
870 return QF_FAIL;
871
872 /* Expand ~/file and $HOME/file to full path. */
873 c = *regmatch->endp[i];
874 *regmatch->endp[i] = NUL;
875 expand_env(regmatch->startp[i], fields->namebuf, CMDBUFFSIZE);
876 *regmatch->endp[i] = c;
877
878 if (vim_strchr((char_u *)"OPQ", idx) != NULL
879 && mch_getperm(fields->namebuf) == -1)
880 return QF_FAIL;
881 }
882 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
883 {
884 if (regmatch->startp[i] == NULL)
885 return QF_FAIL;
886 fields->enr = (int)atol((char *)regmatch->startp[i]);
887 }
888 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
889 {
890 if (regmatch->startp[i] == NULL)
891 return QF_FAIL;
892 fields->lnum = atol((char *)regmatch->startp[i]);
893 }
894 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
895 {
896 if (regmatch->startp[i] == NULL)
897 return QF_FAIL;
898 fields->col = (int)atol((char *)regmatch->startp[i]);
899 }
900 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
901 {
902 if (regmatch->startp[i] == NULL)
903 return QF_FAIL;
904 fields->type = *regmatch->startp[i];
905 }
906 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
907 {
908 if (linelen >= fields->errmsglen)
909 {
910 /* linelen + null terminator */
911 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
912 return QF_NOMEM;
913 fields->errmsg = p;
914 fields->errmsglen = linelen + 1;
915 }
916 vim_strncpy(fields->errmsg, linebuf, linelen);
917 }
918 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
919 {
920 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
921 return QF_FAIL;
922 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
923 if (len >= fields->errmsglen)
924 {
925 /* len + null terminator */
926 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
927 return QF_NOMEM;
928 fields->errmsg = p;
929 fields->errmsglen = len + 1;
930 }
931 vim_strncpy(fields->errmsg, regmatch->startp[i], len);
932 }
933 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
934 {
935 if (regmatch->startp[i] == NULL)
936 return QF_FAIL;
937 *tail = regmatch->startp[i];
938 }
939 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
940 {
941 char_u *match_ptr;
942
943 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
944 return QF_FAIL;
945 fields->col = 0;
946 for (match_ptr = regmatch->startp[i];
947 match_ptr != regmatch->endp[i]; ++match_ptr)
948 {
949 ++fields->col;
950 if (*match_ptr == TAB)
951 {
952 fields->col += 7;
953 fields->col -= fields->col % 8;
954 }
955 }
956 ++fields->col;
957 fields->use_viscol = TRUE;
958 }
959 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
960 {
961 if (regmatch->startp[i] == NULL)
962 return QF_FAIL;
963 fields->col = (int)atol((char *)regmatch->startp[i]);
964 fields->use_viscol = TRUE;
965 }
966 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
967 {
968 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
969 return QF_FAIL;
970 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
971 if (len > CMDBUFFSIZE - 5)
972 len = CMDBUFFSIZE - 5;
973 STRCPY(fields->pattern, "^\\V");
974 STRNCAT(fields->pattern, regmatch->startp[i], len);
975 fields->pattern[len + 3] = '\\';
976 fields->pattern[len + 4] = '$';
977 fields->pattern[len + 5] = NUL;
978 }
979 if ((i = (int)fmt_ptr->addr[10]) > 0) /* %o */
980 {
981 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
982 return QF_FAIL;
983 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
984 if (len > CMDBUFFSIZE)
985 len = CMDBUFFSIZE;
986 STRNCAT(fields->module, regmatch->startp[i], len);
987 }
988
989 return QF_OK;
990}
991
992/*
993 * Parse an error line in 'linebuf' using a single error format string in
994 * 'fmt_ptr->prog' and return the matching values in 'fields'.
995 * Returns QF_OK if the efm format matches completely and the fields are
996 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
997 */
998 static int
999qf_parse_get_fields(
1000 char_u *linebuf,
1001 int linelen,
1002 efm_T *fmt_ptr,
1003 qffields_T *fields,
1004 int qf_multiline,
1005 int qf_multiscan,
1006 char_u **tail)
1007{
1008 regmatch_T regmatch;
1009 int status = QF_FAIL;
1010 int r;
1011
1012 if (qf_multiscan &&
1013 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1014 return QF_FAIL;
1015
1016 fields->namebuf[0] = NUL;
1017 fields->module[0] = NUL;
1018 fields->pattern[0] = NUL;
1019 if (!qf_multiscan)
1020 fields->errmsg[0] = NUL;
1021 fields->lnum = 0;
1022 fields->col = 0;
1023 fields->use_viscol = FALSE;
1024 fields->enr = -1;
1025 fields->type = 0;
1026 *tail = NULL;
1027
1028 regmatch.regprog = fmt_ptr->prog;
1029 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1030 fmt_ptr->prog = regmatch.regprog;
1031 if (r)
1032 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1033 fields, qf_multiline, qf_multiscan, tail);
1034
1035 return status;
1036}
1037
1038/*
1039 * Parse directory error format prefixes (%D and %X).
1040 * Push and pop directories from the directory stack when scanning directory
1041 * names.
1042 */
1043 static int
1044qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1045{
1046 if (idx == 'D') /* enter directory */
1047 {
1048 if (*fields->namebuf == NUL)
1049 {
1050 EMSG(_("E379: Missing or empty directory name"));
1051 return QF_FAIL;
1052 }
1053 qfl->qf_directory =
1054 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1055 if (qfl->qf_directory == NULL)
1056 return QF_FAIL;
1057 }
1058 else if (idx == 'X') /* leave directory */
1059 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1060
1061 return QF_OK;
1062}
1063
1064/*
1065 * Parse global file name error format prefixes (%O, %P and %Q).
1066 */
1067 static int
1068qf_parse_file_pfx(
1069 int idx,
1070 qffields_T *fields,
1071 qf_list_T *qfl,
1072 char_u *tail)
1073{
1074 fields->valid = FALSE;
1075 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1076 {
1077 if (*fields->namebuf && idx == 'P')
1078 qfl->qf_currfile =
1079 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1080 else if (idx == 'Q')
1081 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1082 *fields->namebuf = NUL;
1083 if (tail && *tail)
1084 {
1085 STRMOVE(IObuff, skipwhite(tail));
1086 qfl->qf_multiscan = TRUE;
1087 return QF_MULTISCAN;
1088 }
1089 }
1090
1091 return QF_OK;
1092}
1093
1094/*
1095 * Parse a non-error line (a line which doesn't match any of the error
1096 * format in 'efm').
1097 */
1098 static int
1099qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1100{
1101 char_u *p;
1102
1103 fields->namebuf[0] = NUL; /* no match found, remove file name */
1104 fields->lnum = 0; /* don't jump to this line */
1105 fields->valid = FALSE;
1106 if (linelen >= fields->errmsglen)
1107 {
1108 /* linelen + null terminator */
1109 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1110 return QF_NOMEM;
1111 fields->errmsg = p;
1112 fields->errmsglen = linelen + 1;
1113 }
1114 /* copy whole line to error message */
1115 vim_strncpy(fields->errmsg, linebuf, linelen);
1116
1117 return QF_OK;
1118}
1119
1120/*
1121 * Parse multi-line error format prefixes (%C and %Z)
1122 */
1123 static int
1124qf_parse_multiline_pfx(
1125 qf_info_T *qi,
1126 int qf_idx,
1127 int idx,
1128 qf_list_T *qfl,
1129 qffields_T *fields)
1130{
1131 char_u *ptr;
1132 int len;
1133
1134 if (!qfl->qf_multiignore)
1135 {
1136 qfline_T *qfprev = qfl->qf_last;
1137
1138 if (qfprev == NULL)
1139 return QF_FAIL;
1140 if (*fields->errmsg && !qfl->qf_multiignore)
1141 {
1142 len = (int)STRLEN(qfprev->qf_text);
1143 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1144 == NULL)
1145 return QF_FAIL;
1146 STRCPY(ptr, qfprev->qf_text);
1147 vim_free(qfprev->qf_text);
1148 qfprev->qf_text = ptr;
1149 *(ptr += len) = '\n';
1150 STRCPY(++ptr, fields->errmsg);
1151 }
1152 if (qfprev->qf_nr == -1)
1153 qfprev->qf_nr = fields->enr;
1154 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1155 /* only printable chars allowed */
1156 qfprev->qf_type = fields->type;
1157
1158 if (!qfprev->qf_lnum)
1159 qfprev->qf_lnum = fields->lnum;
1160 if (!qfprev->qf_col)
1161 qfprev->qf_col = fields->col;
1162 qfprev->qf_viscol = fields->use_viscol;
1163 if (!qfprev->qf_fnum)
1164 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1165 qfl->qf_directory,
1166 *fields->namebuf || qfl->qf_directory != NULL
1167 ? fields->namebuf
1168 : qfl->qf_currfile != NULL && fields->valid
1169 ? qfl->qf_currfile : 0);
1170 }
1171 if (idx == 'Z')
1172 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1173 line_breakcheck();
1174
1175 return QF_IGNORE_LINE;
1176}
1177
1178/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001179 * Parse a line and get the quickfix fields.
1180 * Return the QF_ status.
1181 */
1182 static int
1183qf_parse_line(
1184 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001185 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001186 char_u *linebuf,
1187 int linelen,
1188 efm_T *fmt_first,
1189 qffields_T *fields)
1190{
1191 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001192 int idx = 0;
1193 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001194 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001195 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001196
Bram Moolenaare333e792018-04-08 13:27:39 +02001197restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001198 /* If there was no %> item start at the first pattern */
1199 if (fmt_start == NULL)
1200 fmt_ptr = fmt_first;
1201 else
1202 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001203 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001204 fmt_ptr = fmt_start;
1205 fmt_start = NULL;
1206 }
1207
1208 /*
1209 * Try to match each part of 'errorformat' until we find a complete
1210 * match or no match.
1211 */
1212 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001213 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1214 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001215 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001216 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1217 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1218 if (status == QF_NOMEM)
1219 return status;
1220 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001221 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001222 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001223 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001224
1225 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1226 {
1227 if (fmt_ptr != NULL)
1228 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001229 /* 'D' and 'X' directory specifiers */
1230 status = qf_parse_dir_pfx(idx, fields, qfl);
1231 if (status != QF_OK)
1232 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001233 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001234
1235 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1236 if (status != QF_OK)
1237 return status;
1238
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001239 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001240 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001241 }
1242 else if (fmt_ptr != NULL)
1243 {
1244 /* honor %> item */
1245 if (fmt_ptr->conthere)
1246 fmt_start = fmt_ptr;
1247
1248 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1249 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001250 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1251 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001252 }
1253 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1254 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001255 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1256 if (status != QF_OK)
1257 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001258 }
1259 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001260 { /* global file names */
1261 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1262 if (status == QF_MULTISCAN)
1263 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001264 }
1265 if (fmt_ptr->flags == '-') /* generally exclude this line */
1266 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001267 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001268 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001269 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001270 return QF_IGNORE_LINE;
1271 }
1272 }
1273
1274 return QF_OK;
1275}
1276
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001277/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001278 * Read the errorfile "efile" into memory, line by line, building the error
1279 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001280 * Alternative: when "efile" is NULL read errors from buffer "buf".
1281 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001282 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001283 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1284 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001285 * Return -1 for error, number of errors for success.
1286 */
1287 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001288qf_init_ext(
1289 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001290 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001291 char_u *efile,
1292 buf_T *buf,
1293 typval_T *tv,
1294 char_u *errorformat,
1295 int newlist, /* TRUE: start a new error list */
1296 linenr_T lnumfirst, /* first line number to use */
1297 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001298 char_u *qf_title,
1299 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001300{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001301 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001302 qfstate_T state;
1303 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001304 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001305 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001306 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001308 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001310 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001312 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001313 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001314
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001315 vim_memset(&state, 0, sizeof(state));
1316 vim_memset(&fields, 0, sizeof(fields));
1317#ifdef FEAT_MBYTE
1318 state.vc.vc_type = CONV_NONE;
1319 if (enc != NULL && *enc != NUL)
1320 convert_setup(&state.vc, enc, p_enc);
1321#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001322 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaard76ce852018-05-01 15:02:04 +02001323 fields.module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001324 fields.errmsglen = CMDBUFFSIZE + 1;
1325 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1326 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02001327 if (fields.namebuf == NULL || fields.errmsg == NULL
Bram Moolenaard76ce852018-05-01 15:02:04 +02001328 || fields.pattern == NULL || fields.module == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 goto qf_init_end;
1330
Bram Moolenaare0d37972016-07-15 22:36:01 +02001331 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
1333 EMSG2(_(e_openerrf), efile);
1334 goto qf_init_end;
1335 }
1336
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001337 if (newlist || qf_idx == qi->qf_listcount)
1338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001340 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001341 qf_idx = qi->qf_curlist;
1342 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001343 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001344 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001345 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001346 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001347 if (qi->qf_lists[qf_idx].qf_count > 0)
1348 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001351 qfl = &qi->qf_lists[qf_idx];
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001354 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001355 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 else
1357 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001359 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001360 * If the errorformat didn't change between calls, then reuse the
1361 * previously parsed values.
1362 */
1363 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1364 {
1365 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001366 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001367 free_efm_list(&fmt_first);
1368
1369 /* parse the current 'efm' */
1370 fmt_first = parse_efm_option(efm);
1371 if (fmt_first != NULL)
1372 last_efm = vim_strsave(efm);
1373 }
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
1378 /*
1379 * got_int is reset here, because it was probably set when killing the
1380 * ":make" command, but we still want to read the errorfile then.
1381 */
1382 got_int = FALSE;
1383
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001384 if (tv != NULL)
1385 {
1386 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001387 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001388 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001389 state.p_li = tv->vval.v_list->lv_first;
1390 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001391 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001392 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001393 state.buflnum = lnumfirst;
1394 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 /*
1397 * Read the lines in the error file one by one.
1398 * Try to recognize one of the error formats in each line.
1399 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001400 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001402 /* Get the next line from a file/buffer/list/string */
1403 status = qf_get_nextline(&state);
1404 if (status == QF_NOMEM) /* memory alloc failure */
1405 goto qf_init_end;
1406 if (status == QF_END_OF_INPUT) /* end of input */
1407 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001409 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1410 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001411 if (status == QF_FAIL)
1412 goto error2;
1413 if (status == QF_NOMEM)
1414 goto qf_init_end;
1415 if (status == QF_IGNORE_LINE)
1416 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001418 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001419 qf_idx,
1420 qfl->qf_directory,
1421 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001422 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001423 : ((qfl->qf_currfile != NULL && fields.valid)
1424 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001425 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001426 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001427 fields.errmsg,
1428 fields.lnum,
1429 fields.col,
1430 fields.use_viscol,
1431 fields.pattern,
1432 fields.enr,
1433 fields.type,
1434 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 goto error2;
1436 line_breakcheck();
1437 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001438 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001440 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001442 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001443 qfl->qf_ptr = qfl->qf_start;
1444 qfl->qf_index = 1;
1445 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447 else
1448 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001449 qfl->qf_nonevalid = FALSE;
1450 if (qfl->qf_ptr == NULL)
1451 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001453 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001454 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001455 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457 EMSG(_(e_readerrf));
1458error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001459 if (!adding)
1460 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001461 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001462 qf_free(qi, qi->qf_curlist);
1463 qi->qf_listcount--;
1464 if (qi->qf_curlist > 0)
1465 --qi->qf_curlist;
1466 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001467qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001468 if (state.fd != NULL)
1469 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001470 vim_free(fields.namebuf);
Bram Moolenaard76ce852018-05-01 15:02:04 +02001471 vim_free(fields.module);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001472 vim_free(fields.errmsg);
1473 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001474 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001476 if (qf_idx == qi->qf_curlist)
1477 qf_update_buffer(qi, old_last);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001478#ifdef FEAT_MBYTE
1479 if (state.vc.vc_type != CONV_NONE)
1480 convert_setup(&state.vc, NULL, NULL);
1481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
1483 return retval;
1484}
1485
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001486/*
1487 * Set the title of the specified quickfix list. Frees the previous title.
1488 * Prepends ':' to the title.
1489 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001490 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001491qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001492{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001493 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001494
Bram Moolenaarfb604092014-07-23 15:55:00 +02001495 if (title != NULL)
1496 {
1497 char_u *p = alloc((int)STRLEN(title) + 2);
1498
Bram Moolenaara3921f42017-06-04 15:30:34 +02001499 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001500 if (p != NULL)
1501 sprintf((char *)p, ":%s", (char *)title);
1502 }
1503}
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001506 * Prepare for adding a new quickfix list. If the current list is in the
1507 * middle of the stack, then all the following lists are freed and then
1508 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 */
1510 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001511qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 int i;
1514
1515 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001516 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 * the current entry. This makes it possible to browse in a tree-like
1518 * way with ":grep'.
1519 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001520 while (qi->qf_listcount > qi->qf_curlist + 1)
1521 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
1523 /*
1524 * When the stack is full, remove to oldest entry
1525 * Otherwise, add a new entry.
1526 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001527 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001529 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001531 qi->qf_lists[i - 1] = qi->qf_lists[i];
1532 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001535 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001536 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001537 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001538 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539}
1540
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001541/*
1542 * Free a location list
1543 */
1544 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001545ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001546{
1547 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001549
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001550 qi = *pqi;
1551 if (qi == NULL)
1552 return;
1553 *pqi = NULL; /* Remove reference to this list */
1554
1555 qi->qf_refcount--;
1556 if (qi->qf_refcount < 1)
1557 {
1558 /* No references to this location list */
1559 for (i = 0; i < qi->qf_listcount; ++i)
1560 qf_free(qi, i);
1561 vim_free(qi);
1562 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001563}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001564
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001565/*
1566 * Free all the quickfix/location lists in the stack.
1567 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001568 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001569qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001570{
1571 int i;
1572 qf_info_T *qi = &ql_info;
1573
1574 if (wp != NULL)
1575 {
1576 /* location list */
1577 ll_free_all(&wp->w_llist);
1578 ll_free_all(&wp->w_llist_ref);
1579 }
1580 else
1581 /* quickfix list */
1582 for (i = 0; i < qi->qf_listcount; ++i)
1583 qf_free(qi, i);
1584}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586/*
1587 * Add an entry to the end of the list of errors.
1588 * Returns OK or FAIL.
1589 */
1590 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001591qf_add_entry(
1592 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001593 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001594 char_u *dir, /* optional directory name */
1595 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001596 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001597 int bufnum, /* buffer number or zero */
1598 char_u *mesg, /* message */
1599 long lnum, /* line number */
1600 int col, /* column */
1601 int vis_col, /* using visual column */
1602 char_u *pattern, /* search pattern */
1603 int nr, /* error number */
1604 int type, /* type character */
1605 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001607 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001608 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001610 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001612 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001613 {
1614 buf_T *buf = buflist_findnr(bufnum);
1615
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001616 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001617 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001618 buf->b_has_qf_entry |=
1619 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001620 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001621 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001622 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1624 {
1625 vim_free(qfp);
1626 return FAIL;
1627 }
1628 qfp->qf_lnum = lnum;
1629 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001630 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001631 if (pattern == NULL || *pattern == NUL)
1632 qfp->qf_pattern = NULL;
1633 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1634 {
1635 vim_free(qfp->qf_text);
1636 vim_free(qfp);
1637 return FAIL;
1638 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001639 if (module == NULL || *module == NUL)
1640 qfp->qf_module = NULL;
1641 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1642 {
1643 vim_free(qfp->qf_text);
1644 vim_free(qfp->qf_pattern);
1645 vim_free(qfp);
1646 return FAIL;
1647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 qfp->qf_nr = nr;
1649 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1650 type = 0;
1651 qfp->qf_type = type;
1652 qfp->qf_valid = valid;
1653
Bram Moolenaara3921f42017-06-04 15:30:34 +02001654 lastp = &qi->qf_lists[qf_idx].qf_last;
1655 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001656 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001658 qi->qf_lists[qf_idx].qf_start = qfp;
1659 qi->qf_lists[qf_idx].qf_ptr = qfp;
1660 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001661 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 else
1664 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001665 qfp->qf_prev = *lastp;
1666 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001668 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001670 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001671 ++qi->qf_lists[qf_idx].qf_count;
1672 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001673 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001675 qi->qf_lists[qf_idx].qf_index =
1676 qi->qf_lists[qf_idx].qf_count;
1677 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679
1680 return OK;
1681}
1682
1683/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001684 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001685 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001686 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001687ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001688{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001689 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001690
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001691 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1692 if (qi != NULL)
1693 {
1694 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1695 qi->qf_refcount++;
1696 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001697
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001698 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001699}
1700
1701/*
1702 * Return the location list for window 'wp'.
1703 * If not present, allocate a location list
1704 */
1705 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001706ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001707{
1708 if (IS_LL_WINDOW(wp))
1709 /* For a location list window, use the referenced location list */
1710 return wp->w_llist_ref;
1711
1712 /*
1713 * For a non-location list window, w_llist_ref should not point to a
1714 * location list.
1715 */
1716 ll_free_all(&wp->w_llist_ref);
1717
1718 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001719 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001720 return wp->w_llist;
1721}
1722
1723/*
1724 * Copy the location list from window "from" to window "to".
1725 */
1726 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001727copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001728{
1729 qf_info_T *qi;
1730 int idx;
1731 int i;
1732
1733 /*
1734 * When copying from a location list window, copy the referenced
1735 * location list. For other windows, copy the location list for
1736 * that window.
1737 */
1738 if (IS_LL_WINDOW(from))
1739 qi = from->w_llist_ref;
1740 else
1741 qi = from->w_llist;
1742
1743 if (qi == NULL) /* no location list to copy */
1744 return;
1745
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001746 /* allocate a new location list */
1747 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001748 return;
1749
1750 to->w_llist->qf_listcount = qi->qf_listcount;
1751
1752 /* Copy the location lists one at a time */
1753 for (idx = 0; idx < qi->qf_listcount; idx++)
1754 {
1755 qf_list_T *from_qfl;
1756 qf_list_T *to_qfl;
1757
1758 to->w_llist->qf_curlist = idx;
1759
1760 from_qfl = &qi->qf_lists[idx];
1761 to_qfl = &to->w_llist->qf_lists[idx];
1762
1763 /* Some of the fields are populated by qf_add_entry() */
1764 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1765 to_qfl->qf_count = 0;
1766 to_qfl->qf_index = 0;
1767 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001768 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001769 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001770 if (from_qfl->qf_title != NULL)
1771 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1772 else
1773 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001774 if (from_qfl->qf_ctx != NULL)
1775 {
1776 to_qfl->qf_ctx = alloc_tv();
1777 if (to_qfl->qf_ctx != NULL)
1778 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1779 }
1780 else
1781 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001782
1783 if (from_qfl->qf_count)
1784 {
1785 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001786 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001787
1788 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001789 for (i = 0, from_qfp = from_qfl->qf_start;
1790 i < from_qfl->qf_count && from_qfp != NULL;
1791 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001792 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001793 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001794 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001795 NULL,
1796 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02001797 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001798 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001799 from_qfp->qf_text,
1800 from_qfp->qf_lnum,
1801 from_qfp->qf_col,
1802 from_qfp->qf_viscol,
1803 from_qfp->qf_pattern,
1804 from_qfp->qf_nr,
1805 0,
1806 from_qfp->qf_valid) == FAIL)
1807 {
1808 qf_free_all(to);
1809 return;
1810 }
1811 /*
1812 * qf_add_entry() will not set the qf_num field, as the
1813 * directory and file names are not supplied. So the qf_fnum
1814 * field is copied here.
1815 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001816 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001817 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1818 prevp->qf_type = from_qfp->qf_type; /* error type */
1819 if (from_qfl->qf_ptr == from_qfp)
1820 to_qfl->qf_ptr = prevp; /* current location */
1821 }
1822 }
1823
1824 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1825
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001826 /* Assign a new ID for the location list */
1827 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001828 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001829
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001830 /* When no valid entries are present in the list, qf_ptr points to
1831 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001832 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001833 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001834 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001835 to_qfl->qf_index = 1;
1836 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001837 }
1838
1839 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1840}
1841
1842/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001843 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001844 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001847qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
Bram Moolenaar82404332016-07-10 17:00:38 +02001849 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001850 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001851 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (fname == NULL || *fname == NUL) /* no file name */
1854 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaare60acc12011-05-10 16:41:25 +02001856#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001857 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001858#endif
1859#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001860 if (directory != NULL)
1861 slash_adjust(directory);
1862 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001863#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001864 if (directory != NULL && !vim_isAbsName(fname)
1865 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1866 {
1867 /*
1868 * Here we check if the file really exists.
1869 * This should normally be true, but if make works without
1870 * "leaving directory"-messages we might have missed a
1871 * directory change.
1872 */
1873 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001876 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001877 if (directory)
1878 ptr = concat_fnames(directory, fname, TRUE);
1879 else
1880 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001882 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001883 bufname = ptr;
1884 }
1885 else
1886 bufname = fname;
1887
1888 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001889 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001890 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001891 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001892 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001894 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001895 {
1896 vim_free(qf_last_bufname);
1897 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1898 if (bufname == ptr)
1899 qf_last_bufname = bufname;
1900 else
1901 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001902 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001903 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001904 if (buf == NULL)
1905 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001906
Bram Moolenaarc1542742016-07-20 21:44:37 +02001907 buf->b_has_qf_entry =
1908 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001909 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910}
1911
1912/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001913 * Push dirbuf onto the directory stack and return pointer to actual dir or
1914 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 */
1916 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001917qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918{
1919 struct dir_stack_T *ds_new;
1920 struct dir_stack_T *ds_ptr;
1921
1922 /* allocate new stack element and hook it in */
1923 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1924 if (ds_new == NULL)
1925 return NULL;
1926
1927 ds_new->next = *stackptr;
1928 *stackptr = ds_new;
1929
1930 /* store directory on the stack */
1931 if (vim_isAbsName(dirbuf)
1932 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001933 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 (*stackptr)->dirname = vim_strsave(dirbuf);
1935 else
1936 {
1937 /* Okay we don't have an absolute path.
1938 * dirbuf must be a subdir of one of the directories on the stack.
1939 * Let's search...
1940 */
1941 ds_new = (*stackptr)->next;
1942 (*stackptr)->dirname = NULL;
1943 while (ds_new)
1944 {
1945 vim_free((*stackptr)->dirname);
1946 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1947 TRUE);
1948 if (mch_isdir((*stackptr)->dirname) == TRUE)
1949 break;
1950
1951 ds_new = ds_new->next;
1952 }
1953
1954 /* clean up all dirs we already left */
1955 while ((*stackptr)->next != ds_new)
1956 {
1957 ds_ptr = (*stackptr)->next;
1958 (*stackptr)->next = (*stackptr)->next->next;
1959 vim_free(ds_ptr->dirname);
1960 vim_free(ds_ptr);
1961 }
1962
1963 /* Nothing found -> it must be on top level */
1964 if (ds_new == NULL)
1965 {
1966 vim_free((*stackptr)->dirname);
1967 (*stackptr)->dirname = vim_strsave(dirbuf);
1968 }
1969 }
1970
1971 if ((*stackptr)->dirname != NULL)
1972 return (*stackptr)->dirname;
1973 else
1974 {
1975 ds_ptr = *stackptr;
1976 *stackptr = (*stackptr)->next;
1977 vim_free(ds_ptr);
1978 return NULL;
1979 }
1980}
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/*
1983 * pop dirbuf from the directory stack and return previous directory or NULL if
1984 * stack is empty
1985 */
1986 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001987qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 struct dir_stack_T *ds_ptr;
1990
1991 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1992 * What to do if it isn't? */
1993
1994 /* pop top element and free it */
1995 if (*stackptr != NULL)
1996 {
1997 ds_ptr = *stackptr;
1998 *stackptr = (*stackptr)->next;
1999 vim_free(ds_ptr->dirname);
2000 vim_free(ds_ptr);
2001 }
2002
2003 /* return NEW top element as current dir or NULL if stack is empty*/
2004 return *stackptr ? (*stackptr)->dirname : NULL;
2005}
2006
2007/*
2008 * clean up directory stack
2009 */
2010 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002011qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
2013 struct dir_stack_T *ds_ptr;
2014
2015 while ((ds_ptr = *stackptr) != NULL)
2016 {
2017 *stackptr = (*stackptr)->next;
2018 vim_free(ds_ptr->dirname);
2019 vim_free(ds_ptr);
2020 }
2021}
2022
2023/*
2024 * Check in which directory of the directory stack the given file can be
2025 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002026 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 * Cleans up intermediate directory entries.
2028 *
2029 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002030 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 * ./
2032 * ./aa
2033 * ./aa/bb
2034 * ./bb
2035 * ./bb/x.c
2036 * and make says:
2037 * making all in aa
2038 * making all in bb
2039 * x.c:9: Error
2040 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2041 * qf_guess_filepath will return NULL.
2042 */
2043 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002044qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 struct dir_stack_T *ds_ptr;
2047 struct dir_stack_T *ds_tmp;
2048 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002049 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002052 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 return NULL;
2054
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002055 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 fullname = NULL;
2057 while (ds_ptr)
2058 {
2059 vim_free(fullname);
2060 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2061
2062 /* If concat_fnames failed, just go on. The worst thing that can happen
2063 * is that we delete the entire stack.
2064 */
2065 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2066 break;
2067
2068 ds_ptr = ds_ptr->next;
2069 }
2070
2071 vim_free(fullname);
2072
2073 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002074 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002076 ds_tmp = qfl->qf_dir_stack->next;
2077 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 vim_free(ds_tmp->dirname);
2079 vim_free(ds_tmp);
2080 }
2081
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002082 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083}
2084
2085/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002086 * Returns TRUE if a quickfix/location list with the given identifier exists.
2087 */
2088 static int
2089qflist_valid (win_T *wp, int_u qf_id)
2090{
2091 qf_info_T *qi = &ql_info;
2092 int i;
2093
2094 if (wp != NULL)
2095 {
2096 qi = GET_LOC_LIST(wp); /* Location list */
2097 if (qi == NULL)
2098 return FALSE;
2099 }
2100
2101 for (i = 0; i < qi->qf_listcount; ++i)
2102 if (qi->qf_lists[i].qf_id == qf_id)
2103 return TRUE;
2104
2105 return FALSE;
2106}
2107
2108/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002109 * When loading a file from the quickfix, the auto commands may modify it.
2110 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002111 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002112 * Similar to location list.
2113 */
2114 static int
2115is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
2116{
2117 qf_list_T *qfl;
2118 qfline_T *qfp;
2119 int i;
2120
2121 qfl = &qi->qf_lists[qi->qf_curlist];
2122
2123 /* Search for the entry in the current list */
2124 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2125 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002126 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002127 break;
2128
2129 if (i == qfl->qf_count) /* Entry is not found */
2130 return FALSE;
2131
2132 return TRUE;
2133}
2134
2135/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002136 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002137 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002138 */
2139 static qfline_T *
2140get_next_valid_entry(
2141 qf_info_T *qi,
2142 qfline_T *qf_ptr,
2143 int *qf_index,
2144 int dir)
2145{
2146 int idx;
2147 int old_qf_fnum;
2148
2149 idx = *qf_index;
2150 old_qf_fnum = qf_ptr->qf_fnum;
2151
2152 do
2153 {
2154 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2155 || qf_ptr->qf_next == NULL)
2156 return NULL;
2157 ++idx;
2158 qf_ptr = qf_ptr->qf_next;
2159 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2160 && !qf_ptr->qf_valid)
2161 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2162
2163 *qf_index = idx;
2164 return qf_ptr;
2165}
2166
2167/*
2168 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002169 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002170 */
2171 static qfline_T *
2172get_prev_valid_entry(
2173 qf_info_T *qi,
2174 qfline_T *qf_ptr,
2175 int *qf_index,
2176 int dir)
2177{
2178 int idx;
2179 int old_qf_fnum;
2180
2181 idx = *qf_index;
2182 old_qf_fnum = qf_ptr->qf_fnum;
2183
2184 do
2185 {
2186 if (idx == 1 || qf_ptr->qf_prev == NULL)
2187 return NULL;
2188 --idx;
2189 qf_ptr = qf_ptr->qf_prev;
2190 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2191 && !qf_ptr->qf_valid)
2192 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2193
2194 *qf_index = idx;
2195 return qf_ptr;
2196}
2197
2198/*
2199 * Get the n'th (errornr) previous/next valid entry from the current entry in
2200 * the quickfix list.
2201 * dir == FORWARD or FORWARD_FILE: next valid entry
2202 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2203 */
2204 static qfline_T *
2205get_nth_valid_entry(
2206 qf_info_T *qi,
2207 int errornr,
2208 qfline_T *qf_ptr,
2209 int *qf_index,
2210 int dir)
2211{
2212 qfline_T *prev_qf_ptr;
2213 int prev_index;
2214 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2215 char_u *err = e_no_more_items;
2216
2217 while (errornr--)
2218 {
2219 prev_qf_ptr = qf_ptr;
2220 prev_index = *qf_index;
2221
2222 if (dir == FORWARD || dir == FORWARD_FILE)
2223 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2224 else
2225 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2226 if (qf_ptr == NULL)
2227 {
2228 qf_ptr = prev_qf_ptr;
2229 *qf_index = prev_index;
2230 if (err != NULL)
2231 {
2232 EMSG(_(err));
2233 return NULL;
2234 }
2235 break;
2236 }
2237
2238 err = NULL;
2239 }
2240
2241 return qf_ptr;
2242}
2243
2244/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002245 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002246 */
2247 static qfline_T *
2248get_nth_entry(
2249 qf_info_T *qi,
2250 int errornr,
2251 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002252 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002253{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002254 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002255
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002256 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002257 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2258 {
2259 --qf_idx;
2260 qf_ptr = qf_ptr->qf_prev;
2261 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002262 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002263 while (errornr > qf_idx &&
2264 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2265 qf_ptr->qf_next != NULL)
2266 {
2267 ++qf_idx;
2268 qf_ptr = qf_ptr->qf_next;
2269 }
2270
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002271 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002272 return qf_ptr;
2273}
2274
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002275/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002276 * Find a window displaying a Vim help file.
2277 */
2278 static win_T *
2279qf_find_help_win(void)
2280{
2281 win_T *wp;
2282
2283 FOR_ALL_WINDOWS(wp)
2284 if (bt_help(wp->w_buffer))
2285 return wp;
2286
2287 return NULL;
2288}
2289
2290/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002291 * Find a help window or open one.
2292 */
2293 static int
2294jump_to_help_window(qf_info_T *qi, int *opened_window)
2295{
2296 win_T *wp;
2297 int flags;
2298
2299 if (cmdmod.tab != 0)
2300 wp = NULL;
2301 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002302 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002303 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2304 win_enter(wp, TRUE);
2305 else
2306 {
2307 /*
2308 * Split off help window; put it at far top if no position
2309 * specified, the current window is vertically split and narrow.
2310 */
2311 flags = WSP_HELP;
2312 if (cmdmod.split == 0 && curwin->w_width != Columns
2313 && curwin->w_width < 80)
2314 flags |= WSP_TOP;
2315 if (qi != &ql_info)
2316 flags |= WSP_NEWLOC; /* don't copy the location list */
2317
2318 if (win_split(0, flags) == FAIL)
2319 return FAIL;
2320
2321 *opened_window = TRUE;
2322
2323 if (curwin->w_height < p_hh)
2324 win_setheight((int)p_hh);
2325
2326 if (qi != &ql_info) /* not a quickfix list */
2327 {
2328 /* The new window should use the supplied location list */
2329 curwin->w_llist = qi;
2330 qi->qf_refcount++;
2331 }
2332 }
2333
2334 if (!p_im)
2335 restart_edit = 0; /* don't want insert mode in help file */
2336
2337 return OK;
2338}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002339
2340/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002341 * Find a non-quickfix window using the given location list.
2342 * Returns NULL if a matching window is not found.
2343 */
2344 static win_T *
2345qf_find_win_with_loclist(qf_info_T *ll)
2346{
2347 win_T *wp;
2348
2349 FOR_ALL_WINDOWS(wp)
2350 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2351 return wp;
2352
2353 return NULL;
2354}
2355
2356/*
2357 * Find a window containing a normal buffer
2358 */
2359 static win_T *
2360qf_find_win_with_normal_buf(void)
2361{
2362 win_T *wp;
2363
2364 FOR_ALL_WINDOWS(wp)
2365 if (wp->w_buffer->b_p_bt[0] == NUL)
2366 return wp;
2367
2368 return NULL;
2369}
2370
2371/*
2372 * Go to a window in any tabpage containing the specified file. Returns TRUE
2373 * if successfully jumped to the window. Otherwise returns FALSE.
2374 */
2375 static int
2376qf_goto_tabwin_with_file(int fnum)
2377{
2378 tabpage_T *tp;
2379 win_T *wp;
2380
2381 FOR_ALL_TAB_WINDOWS(tp, wp)
2382 if (wp->w_buffer->b_fnum == fnum)
2383 {
2384 goto_tabpage_win(tp, wp);
2385 return TRUE;
2386 }
2387
2388 return FALSE;
2389}
2390
2391/*
2392 * Create a new window to show a file above the quickfix window. Called when
2393 * only the quickfix window is present.
2394 */
2395 static int
2396qf_open_new_file_win(qf_info_T *ll_ref)
2397{
2398 int flags;
2399
2400 flags = WSP_ABOVE;
2401 if (ll_ref != NULL)
2402 flags |= WSP_NEWLOC;
2403 if (win_split(0, flags) == FAIL)
2404 return FAIL; /* not enough room for window */
2405 p_swb = empty_option; /* don't split again */
2406 swb_flags = 0;
2407 RESET_BINDING(curwin);
2408 if (ll_ref != NULL)
2409 {
2410 /* The new window should use the location list from the
2411 * location list window */
2412 curwin->w_llist = ll_ref;
2413 ll_ref->qf_refcount++;
2414 }
2415 return OK;
2416}
2417
2418/*
2419 * Go to a window that shows the right buffer. If the window is not found, go
2420 * to the window just above the location list window. This is used for opening
2421 * a file from a location window and not from a quickfix window. If some usable
2422 * window is previously found, then it is supplied in 'use_win'.
2423 */
2424 static void
2425qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2426{
2427 win_T *win = use_win;
2428
2429 if (win == NULL)
2430 {
2431 /* Find the window showing the selected file */
2432 FOR_ALL_WINDOWS(win)
2433 if (win->w_buffer->b_fnum == qf_fnum)
2434 break;
2435 if (win == NULL)
2436 {
2437 /* Find a previous usable window */
2438 win = curwin;
2439 do
2440 {
2441 if (win->w_buffer->b_p_bt[0] == NUL)
2442 break;
2443 if (win->w_prev == NULL)
2444 win = lastwin; /* wrap around the top */
2445 else
2446 win = win->w_prev; /* go to previous window */
2447 } while (win != curwin);
2448 }
2449 }
2450 win_goto(win);
2451
2452 /* If the location list for the window is not set, then set it
2453 * to the location list from the location window */
2454 if (win->w_llist == NULL)
2455 {
2456 win->w_llist = ll_ref;
2457 ll_ref->qf_refcount++;
2458 }
2459}
2460
2461/*
2462 * Go to a window that shows the specified file. If a window is not found, go
2463 * to the window just above the quickfix window. This is used for opening a
2464 * file from a quickfix window and not from a location window.
2465 */
2466 static void
2467qf_goto_win_with_qfl_file(int qf_fnum)
2468{
2469 win_T *win;
2470 win_T *altwin;
2471
2472 win = curwin;
2473 altwin = NULL;
2474 for (;;)
2475 {
2476 if (win->w_buffer->b_fnum == qf_fnum)
2477 break;
2478 if (win->w_prev == NULL)
2479 win = lastwin; /* wrap around the top */
2480 else
2481 win = win->w_prev; /* go to previous window */
2482
2483 if (IS_QF_WINDOW(win))
2484 {
2485 /* Didn't find it, go to the window before the quickfix
2486 * window. */
2487 if (altwin != NULL)
2488 win = altwin;
2489 else if (curwin->w_prev != NULL)
2490 win = curwin->w_prev;
2491 else
2492 win = curwin->w_next;
2493 break;
2494 }
2495
2496 /* Remember a usable window. */
2497 if (altwin == NULL && !win->w_p_pvw
2498 && win->w_buffer->b_p_bt[0] == NUL)
2499 altwin = win;
2500 }
2501
2502 win_goto(win);
2503}
2504
2505/*
2506 * Find a suitable window for opening a file (qf_fnum) from the
2507 * quickfix/location list and jump to it. If the file is already opened in a
2508 * window, jump to it. Otherwise open a new window to display the file. This is
2509 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002510 */
2511 static int
2512qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2513{
2514 win_T *usable_win_ptr = NULL;
2515 int usable_win;
2516 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002517 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002518
2519 usable_win = 0;
2520
2521 ll_ref = curwin->w_llist_ref;
2522 if (ll_ref != NULL)
2523 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002524 /* Find a non-quickfix window with this location list */
2525 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2526 if (usable_win_ptr != NULL)
2527 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002528 }
2529
2530 if (!usable_win)
2531 {
2532 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002533 win = qf_find_win_with_normal_buf();
2534 if (win != NULL)
2535 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002536 }
2537
2538 /*
2539 * If no usable window is found and 'switchbuf' contains "usetab"
2540 * then search in other tabs.
2541 */
2542 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002543 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002544
2545 /*
2546 * If there is only one window and it is the quickfix window, create a
2547 * new one above the quickfix window.
2548 */
2549 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2550 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002551 if (qf_open_new_file_win(ll_ref) != OK)
2552 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002553 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002554 }
2555 else
2556 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002557 if (curwin->w_llist_ref != NULL) /* In a location window */
2558 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2559 else /* In a quickfix window */
2560 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002561 }
2562
2563 return OK;
2564}
2565
2566/*
2567 * Edit the selected file or help file.
2568 */
2569 static int
2570qf_jump_edit_buffer(
2571 qf_info_T *qi,
2572 qfline_T *qf_ptr,
2573 int forceit,
2574 win_T *oldwin,
2575 int *opened_window,
2576 int *abort)
2577{
2578 int retval = OK;
2579
2580 if (qf_ptr->qf_type == 1)
2581 {
2582 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2583 * set b_p_ro flag). */
2584 if (!can_abandon(curbuf, forceit))
2585 {
2586 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002587 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002588 }
2589 else
2590 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2591 ECMD_HIDE + ECMD_SET_HELP,
2592 oldwin == curwin ? curwin : NULL);
2593 }
2594 else
2595 {
2596 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002597 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002598
2599 retval = buflist_getfile(qf_ptr->qf_fnum,
2600 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002601
2602 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002603 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002604 /*
2605 * Location list. Check whether the associated window is still
2606 * present and the list is still valid.
2607 */
2608 if (!win_valid_any_tab(oldwin))
2609 {
2610 EMSG(_("E924: Current window was closed"));
2611 *abort = TRUE;
2612 *opened_window = FALSE;
2613 }
2614 else if (!qflist_valid(oldwin, save_qfid))
2615 {
2616 EMSG(_(e_loc_list_changed));
2617 *abort = TRUE;
2618 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002619 }
2620 else if (old_qf_curlist != qi->qf_curlist
2621 || !is_qf_entry_present(qi, qf_ptr))
2622 {
2623 if (qi == &ql_info)
2624 EMSG(_("E925: Current quickfix was changed"));
2625 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002626 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002627 *abort = TRUE;
2628 }
2629
2630 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002631 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002632 }
2633
2634 return retval;
2635}
2636
2637/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002638 * Go to the error line in the current file using either line/column number or
2639 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002640 */
2641 static void
2642qf_jump_goto_line(
2643 linenr_T qf_lnum,
2644 int qf_col,
2645 char_u qf_viscol,
2646 char_u *qf_pattern)
2647{
2648 linenr_T i;
2649 char_u *line;
2650 colnr_T screen_col;
2651 colnr_T char_col;
2652
2653 if (qf_pattern == NULL)
2654 {
2655 /*
2656 * Go to line with error, unless qf_lnum is 0.
2657 */
2658 i = qf_lnum;
2659 if (i > 0)
2660 {
2661 if (i > curbuf->b_ml.ml_line_count)
2662 i = curbuf->b_ml.ml_line_count;
2663 curwin->w_cursor.lnum = i;
2664 }
2665 if (qf_col > 0)
2666 {
2667 curwin->w_cursor.col = qf_col - 1;
2668#ifdef FEAT_VIRTUALEDIT
2669 curwin->w_cursor.coladd = 0;
2670#endif
2671 if (qf_viscol == TRUE)
2672 {
2673 /*
2674 * Check each character from the beginning of the error
2675 * line up to the error column. For each tab character
2676 * found, reduce the error column value by the length of
2677 * a tab character.
2678 */
2679 line = ml_get_curline();
2680 screen_col = 0;
2681 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2682 {
2683 if (*line == NUL)
2684 break;
2685 if (*line++ == '\t')
2686 {
2687 curwin->w_cursor.col -= 7 - (screen_col % 8);
2688 screen_col += 8 - (screen_col % 8);
2689 }
2690 else
2691 ++screen_col;
2692 }
2693 }
2694 check_cursor();
2695 }
2696 else
2697 beginline(BL_WHITE | BL_FIX);
2698 }
2699 else
2700 {
2701 pos_T save_cursor;
2702
2703 /* Move the cursor to the first line in the buffer */
2704 save_cursor = curwin->w_cursor;
2705 curwin->w_cursor.lnum = 0;
2706 if (!do_search(NULL, '/', qf_pattern, (long)1,
2707 SEARCH_KEEP, NULL, NULL))
2708 curwin->w_cursor = save_cursor;
2709 }
2710}
2711
2712/*
2713 * Display quickfix list index and size message
2714 */
2715 static void
2716qf_jump_print_msg(
2717 qf_info_T *qi,
2718 int qf_index,
2719 qfline_T *qf_ptr,
2720 buf_T *old_curbuf,
2721 linenr_T old_lnum)
2722{
2723 linenr_T i;
2724 int len;
2725
2726 /* Update the screen before showing the message, unless the screen
2727 * scrolled up. */
2728 if (!msg_scrolled)
2729 update_topline_redraw();
2730 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2731 qi->qf_lists[qi->qf_curlist].qf_count,
2732 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2733 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2734 /* Add the message, skipping leading whitespace and newlines. */
2735 len = (int)STRLEN(IObuff);
2736 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2737
2738 /* Output the message. Overwrite to avoid scrolling when the 'O'
2739 * flag is present in 'shortmess'; But when not jumping, print the
2740 * whole message. */
2741 i = msg_scroll;
2742 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2743 msg_scroll = TRUE;
2744 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2745 msg_scroll = FALSE;
2746 msg_attr_keep(IObuff, 0, TRUE);
2747 msg_scroll = i;
2748}
2749
2750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 * jump to a quickfix line
2752 * if dir == FORWARD go "errornr" valid entries forward
2753 * if dir == BACKWARD go "errornr" valid entries backward
2754 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2755 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2756 * else if "errornr" is zero, redisplay the same line
2757 * else go to entry "errornr"
2758 */
2759 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002760qf_jump(qf_info_T *qi,
2761 int dir,
2762 int errornr,
2763 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002765 qfline_T *qf_ptr;
2766 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 buf_T *old_curbuf;
2770 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002771 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002772 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002774 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776#ifdef FEAT_FOLDING
2777 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2778#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002779 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002781 if (qi == NULL)
2782 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783
2784 if (qi->qf_curlist >= qi->qf_listcount
2785 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
2787 EMSG(_(e_quickfix));
2788 return;
2789 }
2790
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002791 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002793 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002795 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002797 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2798 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002800 qf_ptr = old_qf_ptr;
2801 qf_index = old_qf_index;
2802 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 }
2805 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002806 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002808 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2809 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 /* No need to print the error message if it's visible in the error
2811 * window */
2812 print_message = FALSE;
2813
2814 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002815 * For ":helpgrep" find a help window or open one.
2816 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002817 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002818 if (jump_to_help_window(qi, &opened_window) == FAIL)
2819 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002820
2821 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 * If currently in the quickfix window, find another window to show the
2823 * file in.
2824 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002825 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 {
2827 /*
2828 * If there is no file specified, we don't know where to go.
2829 * But do advance, otherwise ":cn" gets stuck.
2830 */
2831 if (qf_ptr->qf_fnum == 0)
2832 goto theend;
2833
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002834 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2835 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 /*
2839 * If there is a file name,
2840 * read the wanted file if needed, and check autowrite etc.
2841 */
2842 old_curbuf = curbuf;
2843 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002844
2845 if (qf_ptr->qf_fnum != 0)
2846 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002847 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002848
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002849 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2850 &opened_window, &abort);
2851 if (abort)
2852 {
2853 qi = NULL;
2854 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002855 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002856 }
2857
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002858 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
2860 /* When not switched to another buffer, still need to set pc mark */
2861 if (curbuf == old_curbuf)
2862 setpcmark();
2863
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002864 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2865 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
2867#ifdef FEAT_FOLDING
2868 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2869 foldOpenCursor();
2870#endif
2871 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002872 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
2874 else
2875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (opened_window)
2877 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002878 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
2880 /*
2881 * Couldn't open file, so put index back where it was. This could
2882 * happen if the file was readonly and we changed something.
2883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 qf_ptr = old_qf_ptr;
2886 qf_index = old_qf_index;
2887 }
2888 }
2889theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002890 if (qi != NULL)
2891 {
2892 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2893 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 if (p_swb != old_swb && opened_window)
2896 {
2897 /* Restore old 'switchbuf' value, but not when an autocommand or
2898 * modeline has changed the value. */
2899 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002902 swb_flags = old_swb_flags;
2903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 else
2905 free_string_option(old_swb);
2906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907}
2908
2909/*
2910 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 */
2913 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002914qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002916 buf_T *buf;
2917 char_u *fname;
2918 qfline_T *qfp;
2919 int i;
2920 int idx1 = 1;
2921 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002922 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002923 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002924 int qfFileAttr;
2925 int qfSepAttr;
2926 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002927 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002929 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002931 if (eap->cmdidx == CMD_llist)
2932 {
2933 qi = GET_LOC_LIST(curwin);
2934 if (qi == NULL)
2935 {
2936 EMSG(_(e_loclist));
2937 return;
2938 }
2939 }
2940
2941 if (qi->qf_curlist >= qi->qf_listcount
2942 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
2944 EMSG(_(e_quickfix));
2945 return;
2946 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002947 if (*arg == '+')
2948 {
2949 ++arg;
2950 plus = TRUE;
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2953 {
2954 EMSG(_(e_trailing));
2955 return;
2956 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002957 if (plus)
2958 {
2959 i = qi->qf_lists[qi->qf_curlist].qf_index;
2960 idx2 = i + idx1;
2961 idx1 = i;
2962 }
2963 else
2964 {
2965 i = qi->qf_lists[qi->qf_curlist].qf_count;
2966 if (idx1 < 0)
2967 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2968 if (idx2 < 0)
2969 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
Bram Moolenaara796d462018-05-01 14:30:36 +02002972 /* Shorten all the file names, so that it is easy to read */
2973 shorten_fnames(FALSE);
2974
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002975 /*
2976 * Get the attributes for the different quickfix highlight items. Note
2977 * that this depends on syntax items defined in the qf.vim syntax file
2978 */
2979 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2980 if (qfFileAttr == 0)
2981 qfFileAttr = HL_ATTR(HLF_D);
2982 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2983 if (qfSepAttr == 0)
2984 qfSepAttr = HL_ATTR(HLF_D);
2985 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2986 if (qfLineAttr == 0)
2987 qfLineAttr = HL_ATTR(HLF_N);
2988
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002989 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002991 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2992 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 {
2994 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2995 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002996 msg_putchar('\n');
2997 if (got_int)
2998 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002999
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003000 fname = NULL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02003001 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3002 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", i, (char *)qfp->qf_module);
3003 else {
3004 if (qfp->qf_fnum != 0
3005 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3006 {
3007 fname = buf->b_fname;
3008 if (qfp->qf_type == 1) /* :helpgrep */
3009 fname = gettail(fname);
3010 }
3011 if (fname == NULL)
3012 sprintf((char *)IObuff, "%2d", i);
3013 else
3014 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3015 i, (char *)fname);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003016 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003017 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003018 ? HL_ATTR(HLF_QFL) : qfFileAttr);
3019
3020 if (qfp->qf_lnum != 0)
3021 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003022 if (qfp->qf_lnum == 0)
3023 IObuff[0] = NUL;
3024 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003025 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003026 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003027 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003028 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003029 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003030 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003031 msg_puts_attr(IObuff, qfLineAttr);
3032 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003033 if (qfp->qf_pattern != NULL)
3034 {
3035 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003036 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003037 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003038 }
3039 msg_puts((char_u *)" ");
3040
3041 /* Remove newlines and leading whitespace from the text. For an
3042 * unrecognized line keep the indent, the compiler may mark a word
3043 * with ^^^^. */
3044 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3046 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003047 msg_prt_line(IObuff, FALSE);
3048 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003050
3051 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003052 if (qfp == NULL)
3053 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003054 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 ui_breakcheck();
3056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057}
3058
3059/*
3060 * Remove newlines and leading whitespace from an error message.
3061 * Put the result in "buf[bufsize]".
3062 */
3063 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003064qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
3066 int i;
3067 char_u *p = text;
3068
3069 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3070 {
3071 if (*p == '\n')
3072 {
3073 buf[i] = ' ';
3074 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003075 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 break;
3077 }
3078 else
3079 buf[i] = *p++;
3080 }
3081 buf[i] = NUL;
3082}
3083
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003084/*
3085 * Display information (list number, list size and the title) about a
3086 * quickfix/location list.
3087 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003088 static void
3089qf_msg(qf_info_T *qi, int which, char *lead)
3090{
3091 char *title = (char *)qi->qf_lists[which].qf_title;
3092 int count = qi->qf_lists[which].qf_count;
3093 char_u buf[IOSIZE];
3094
3095 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3096 lead,
3097 which + 1,
3098 qi->qf_listcount,
3099 count);
3100
3101 if (title != NULL)
3102 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003103 size_t len = STRLEN(buf);
3104
3105 if (len < 34)
3106 {
3107 vim_memset(buf + len, ' ', 34 - len);
3108 buf[34] = NUL;
3109 }
3110 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003111 }
3112 trunc_string(buf, buf, Columns - 1, IOSIZE);
3113 msg(buf);
3114}
3115
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116/*
3117 * ":colder [count]": Up in the quickfix stack.
3118 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003119 * ":lolder [count]": Up in the location list stack.
3120 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 */
3122 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003123qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003125 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 int count;
3127
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003128 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
3129 {
3130 qi = GET_LOC_LIST(curwin);
3131 if (qi == NULL)
3132 {
3133 EMSG(_(e_loclist));
3134 return;
3135 }
3136 }
3137
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (eap->addr_count != 0)
3139 count = eap->line2;
3140 else
3141 count = 1;
3142 while (count--)
3143 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003144 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003146 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003149 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003151 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 }
3153 else
3154 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003155 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
3157 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003158 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003160 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
3162 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003163 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003164 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165}
3166
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003167/*
3168 * Display the information about all the quickfix/location lists in the stack
3169 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003170 void
3171qf_history(exarg_T *eap)
3172{
3173 qf_info_T *qi = &ql_info;
3174 int i;
3175
3176 if (eap->cmdidx == CMD_lhistory)
3177 qi = GET_LOC_LIST(curwin);
3178 if (qi == NULL || (qi->qf_listcount == 0
3179 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
3180 MSG(_("No entries"));
3181 else
3182 for (i = 0; i < qi->qf_listcount; ++i)
3183 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3184}
3185
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003187 * Free all the entries in the error list "idx". Note that other information
3188 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 */
3190 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003191qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003193 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003194 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003195 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003196 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003198 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003200 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003201 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003202 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003203 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003204 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003205 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003206 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003207 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003208 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003209 if (stop)
3210 /* Somehow qf_count may have an incorrect value, set it to 1
3211 * to avoid crashing when it's wrong.
3212 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003213 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003214 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003215 qfl->qf_start = qfpnext;
3216 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003218
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003219 qfl->qf_index = 0;
3220 qfl->qf_start = NULL;
3221 qfl->qf_last = NULL;
3222 qfl->qf_ptr = NULL;
3223 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003224
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003225 qf_clean_dir_stack(&qfl->qf_dir_stack);
3226 qfl->qf_directory = NULL;
3227 qf_clean_dir_stack(&qfl->qf_file_stack);
3228 qfl->qf_currfile = NULL;
3229 qfl->qf_multiline = FALSE;
3230 qfl->qf_multiignore = FALSE;
3231 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232}
3233
3234/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003235 * Free error list "idx". Frees all the entries in the quickfix list,
3236 * associated context information and the title.
3237 */
3238 static void
3239qf_free(qf_info_T *qi, int idx)
3240{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003241 qf_list_T *qfl = &qi->qf_lists[idx];
3242
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003243 qf_free_items(qi, idx);
3244
Bram Moolenaard23a8232018-02-10 18:45:26 +01003245 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003246 free_tv(qfl->qf_ctx);
3247 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003248 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003249 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003250}
3251
3252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 * qf_mark_adjust: adjust marks
3254 */
3255 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003256qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003257 win_T *wp,
3258 linenr_T line1,
3259 linenr_T line2,
3260 long amount,
3261 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003263 int i;
3264 qfline_T *qfp;
3265 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003266 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003267 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003268 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaarc1542742016-07-20 21:44:37 +02003270 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003271 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003272 if (wp != NULL)
3273 {
3274 if (wp->w_llist == NULL)
3275 return;
3276 qi = wp->w_llist;
3277 }
3278
3279 for (idx = 0; idx < qi->qf_listcount; ++idx)
3280 if (qi->qf_lists[idx].qf_count)
3281 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003282 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3283 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 if (qfp->qf_fnum == curbuf->b_fnum)
3285 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003286 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3288 {
3289 if (amount == MAXLNUM)
3290 qfp->qf_cleared = TRUE;
3291 else
3292 qfp->qf_lnum += amount;
3293 }
3294 else if (amount_after && qfp->qf_lnum > line2)
3295 qfp->qf_lnum += amount_after;
3296 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003297
3298 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003299 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300}
3301
3302/*
3303 * Make a nice message out of the error character and the error number:
3304 * char number message
3305 * e or E 0 " error"
3306 * w or W 0 " warning"
3307 * i or I 0 " info"
3308 * 0 0 ""
3309 * other 0 " c"
3310 * e or E n " error n"
3311 * w or W n " warning n"
3312 * i or I n " info n"
3313 * 0 n " error n"
3314 * other n " c n"
3315 * 1 x "" :helpgrep
3316 */
3317 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003318qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 static char_u buf[20];
3321 static char_u cc[3];
3322 char_u *p;
3323
3324 if (c == 'W' || c == 'w')
3325 p = (char_u *)" warning";
3326 else if (c == 'I' || c == 'i')
3327 p = (char_u *)" info";
3328 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3329 p = (char_u *)" error";
3330 else if (c == 0 || c == 1)
3331 p = (char_u *)"";
3332 else
3333 {
3334 cc[0] = ' ';
3335 cc[1] = c;
3336 cc[2] = NUL;
3337 p = cc;
3338 }
3339
3340 if (nr <= 0)
3341 return p;
3342
3343 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3344 return buf;
3345}
3346
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347/*
3348 * ":cwindow": open the quickfix window if we have errors to display,
3349 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003350 * ":lwindow": open the location list window if we have locations to display,
3351 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 */
3353 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003354ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003356 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 win_T *win;
3358
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003359 if (eap->cmdidx == CMD_lwindow)
3360 {
3361 qi = GET_LOC_LIST(curwin);
3362 if (qi == NULL)
3363 return;
3364 }
3365
3366 /* Look for an existing quickfix window. */
3367 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369 /*
3370 * If a quickfix window is open but we have no errors to display,
3371 * close the window. If a quickfix window is not open, then open
3372 * it if we have errors; otherwise, leave it closed.
3373 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003374 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003375 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003376 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 if (win != NULL)
3379 ex_cclose(eap);
3380 }
3381 else if (win == NULL)
3382 ex_copen(eap);
3383}
3384
3385/*
3386 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003387 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003390ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003392 win_T *win = NULL;
3393 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003395 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3396 {
3397 qi = GET_LOC_LIST(curwin);
3398 if (qi == NULL)
3399 return;
3400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003402 /* Find existing quickfix window and close it. */
3403 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (win != NULL)
3405 win_close(win, FALSE);
3406}
3407
3408/*
3409 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003410 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 */
3412 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003413ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003415 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003418 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003419 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003420 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003422 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3423 {
3424 qi = GET_LOC_LIST(curwin);
3425 if (qi == NULL)
3426 {
3427 EMSG(_(e_loclist));
3428 return;
3429 }
3430 }
3431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (eap->addr_count != 0)
3433 height = eap->line2;
3434 else
3435 height = QF_WINHEIGHT;
3436
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438#ifdef FEAT_GUI
3439 need_mouse_correct = TRUE;
3440#endif
3441
3442 /*
3443 * Find existing quickfix window, or open a new one.
3444 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003445 win = qf_find_win(qi);
3446
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003447 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003450 if (eap->addr_count != 0)
3451 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003452 if (cmdmod.split & WSP_VERT)
3453 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003454 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003455 win_setwidth(height);
3456 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003457 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003458 win_setheight(height);
3459 }
3460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 else
3462 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003463 int flags = 0;
3464
Bram Moolenaar9c102382006-05-03 21:26:49 +00003465 qf_buf = qf_find_buf(qi);
3466
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 /* The current window becomes the previous window afterwards. */
3468 win = curwin;
3469
Bram Moolenaar77642c02012-11-20 17:55:10 +01003470 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3471 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003472 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003473 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003474 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003475 /* Default is to open the window below the current window */
3476 if (cmdmod.split == 0)
3477 flags = WSP_BELOW;
3478 flags |= WSP_NEWLOC;
3479 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003481 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003483 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003485 /*
3486 * For the location list window, create a reference to the
3487 * location list from the window 'win'.
3488 */
3489 curwin->w_llist_ref = win->w_llist;
3490 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003492
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003493 if (oldwin != curwin)
3494 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003495 if (qf_buf != NULL)
3496 /* Use the existing quickfix buffer */
3497 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003498 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003499 else
3500 {
3501 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003502 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003503 /* switch off 'swapfile' */
3504 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3505 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003506 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003507 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003508 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003509#ifdef FEAT_DIFF
3510 curwin->w_p_diff = FALSE;
3511#endif
3512#ifdef FEAT_FOLDING
3513 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3514 OPT_LOCAL);
3515#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003518 /* Only set the height when still in the same tab page and there is no
3519 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003520 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 win_setheight(height);
3522 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3523 if (win_valid(win))
3524 prevwin = win;
3525 }
3526
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003527 qf_set_title_var(qi);
3528
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 /*
3530 * Fill the buffer with the quickfix list.
3531 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003532 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003534 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 curwin->w_cursor.col = 0;
3536 check_cursor();
3537 update_topline(); /* scroll to show the line */
3538}
3539
3540/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003541 * Move the cursor in the quickfix window to "lnum".
3542 */
3543 static void
3544qf_win_goto(win_T *win, linenr_T lnum)
3545{
3546 win_T *old_curwin = curwin;
3547
3548 curwin = win;
3549 curbuf = win->w_buffer;
3550 curwin->w_cursor.lnum = lnum;
3551 curwin->w_cursor.col = 0;
3552#ifdef FEAT_VIRTUALEDIT
3553 curwin->w_cursor.coladd = 0;
3554#endif
3555 curwin->w_curswant = 0;
3556 update_topline(); /* scroll to show the line */
3557 redraw_later(VALID);
3558 curwin->w_redr_status = TRUE; /* update ruler */
3559 curwin = old_curwin;
3560 curbuf = curwin->w_buffer;
3561}
3562
3563/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003564 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003565 */
3566 void
3567ex_cbottom(exarg_T *eap UNUSED)
3568{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003569 qf_info_T *qi = &ql_info;
3570 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003571
Bram Moolenaar537ef082016-07-09 17:56:19 +02003572 if (eap->cmdidx == CMD_lbottom)
3573 {
3574 qi = GET_LOC_LIST(curwin);
3575 if (qi == NULL)
3576 {
3577 EMSG(_(e_loclist));
3578 return;
3579 }
3580 }
3581
3582 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003583 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3584 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3585}
3586
3587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 * Return the number of the current entry (line number in the quickfix
3589 * window).
3590 */
3591 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003592qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003594 qf_info_T *qi = &ql_info;
3595
3596 if (IS_LL_WINDOW(wp))
3597 /* In the location list window, use the referenced location list */
3598 qi = wp->w_llist_ref;
3599
3600 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601}
3602
3603/*
3604 * Update the cursor position in the quickfix window to the current error.
3605 * Return TRUE if there is a quickfix window.
3606 */
3607 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003608qf_win_pos_update(
3609 qf_info_T *qi,
3610 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
3612 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003613 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
3615 /*
3616 * Put the cursor on the current error in the quickfix window, so that
3617 * it's viewable.
3618 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003619 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 if (win != NULL
3621 && qf_index <= win->w_buffer->b_ml.ml_line_count
3622 && old_qf_index != qf_index)
3623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (qf_index > old_qf_index)
3625 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003626 win->w_redraw_top = old_qf_index;
3627 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629 else
3630 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003631 win->w_redraw_top = qf_index;
3632 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003634 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
3636 return win != NULL;
3637}
3638
3639/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003640 * Check whether the given window is displaying the specified quickfix/location
3641 * list buffer
3642 */
3643 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003644is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003645{
3646 /*
3647 * A window displaying the quickfix buffer will have the w_llist_ref field
3648 * set to NULL.
3649 * A window displaying a location list buffer will have the w_llist_ref
3650 * pointing to the location list.
3651 */
3652 if (bt_quickfix(win->w_buffer))
3653 if ((qi == &ql_info && win->w_llist_ref == NULL)
3654 || (qi != &ql_info && win->w_llist_ref == qi))
3655 return TRUE;
3656
3657 return FALSE;
3658}
3659
3660/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003661 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003662 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003663 */
3664 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003665qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003666{
3667 win_T *win;
3668
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003669 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003670 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003671 return win;
3672 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003673}
3674
3675/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003676 * Find a quickfix buffer.
3677 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 */
3679 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003680qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003682 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003683 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
Bram Moolenaar9c102382006-05-03 21:26:49 +00003685 FOR_ALL_TAB_WINDOWS(tp, win)
3686 if (is_qf_win(win, qi))
3687 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003688
Bram Moolenaar9c102382006-05-03 21:26:49 +00003689 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690}
3691
3692/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003693 * Update the w:quickfix_title variable in the quickfix/location list window
3694 */
3695 static void
3696qf_update_win_titlevar(qf_info_T *qi)
3697{
3698 win_T *win;
3699 win_T *curwin_save;
3700
3701 if ((win = qf_find_win(qi)) != NULL)
3702 {
3703 curwin_save = curwin;
3704 curwin = win;
3705 qf_set_title_var(qi);
3706 curwin = curwin_save;
3707 }
3708}
3709
3710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 * Find the quickfix buffer. If it exists, update the contents.
3712 */
3713 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003714qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003717 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
3720 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003721 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (buf != NULL)
3723 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003724 linenr_T old_line_count = buf->b_ml.ml_line_count;
3725
3726 if (old_last == NULL)
3727 /* set curwin/curbuf to buf and save a few things */
3728 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
Bram Moolenaard823fa92016-08-12 16:29:27 +02003730 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003731
Bram Moolenaar864293a2016-06-02 13:40:04 +02003732 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003733 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003734
Bram Moolenaar864293a2016-06-02 13:40:04 +02003735 if (old_last == NULL)
3736 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003737 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003738
3739 /* restore curwin/curbuf and a few other things */
3740 aucmd_restbuf(&aco);
3741 }
3742
3743 /* Only redraw when added lines are visible. This avoids flickering
3744 * when the added lines are not visible. */
3745 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3746 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748}
3749
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003750/*
3751 * Set "w:quickfix_title" if "qi" has a title.
3752 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003754qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003755{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003756 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3757 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003758 qi->qf_lists[qi->qf_curlist].qf_title);
3759}
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761/*
3762 * Fill current buffer with quickfix errors, replacing any previous contents.
3763 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003764 * If "old_last" is not NULL append the items after this one.
3765 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3766 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 */
3768 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003769qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003771 linenr_T lnum;
3772 qfline_T *qfp;
3773 buf_T *errbuf;
3774 int len;
3775 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
Bram Moolenaar864293a2016-06-02 13:40:04 +02003777 if (old_last == NULL)
3778 {
3779 if (buf != curbuf)
3780 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003781 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003782 return;
3783 }
3784
3785 /* delete all existing lines */
3786 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3787 (void)ml_delete((linenr_T)1, FALSE);
3788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
3790 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003791 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003793 char_u dirname[MAXPATHL];
3794
3795 *dirname = NUL;
3796
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003798 if (old_last == NULL)
3799 {
3800 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3801 lnum = 0;
3802 }
3803 else
3804 {
3805 qfp = old_last->qf_next;
3806 lnum = buf->b_ml.ml_line_count;
3807 }
3808 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003810 if (qfp->qf_module != NULL)
3811 {
3812 STRCPY(IObuff, qfp->qf_module);
3813 len = (int)STRLEN(IObuff);
3814 }
3815 else if (qfp->qf_fnum != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3817 && errbuf->b_fname != NULL)
3818 {
3819 if (qfp->qf_type == 1) /* :helpgrep */
3820 STRCPY(IObuff, gettail(errbuf->b_fname));
3821 else
Bram Moolenaara796d462018-05-01 14:30:36 +02003822 {
3823 /* shorten the file name if not done already */
3824 if (errbuf->b_sfname == NULL
3825 || mch_isFullName(errbuf->b_sfname))
3826 {
3827 if (*dirname == NUL)
3828 mch_dirname(dirname, MAXPATHL);
3829 shorten_buf_fname(errbuf, dirname, FALSE);
3830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 STRCPY(IObuff, errbuf->b_fname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 len = (int)STRLEN(IObuff);
3834 }
3835 else
3836 len = 0;
3837 IObuff[len++] = '|';
3838
3839 if (qfp->qf_lnum > 0)
3840 {
3841 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3842 len += (int)STRLEN(IObuff + len);
3843
3844 if (qfp->qf_col > 0)
3845 {
3846 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3847 len += (int)STRLEN(IObuff + len);
3848 }
3849
3850 sprintf((char *)IObuff + len, "%s",
3851 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3852 len += (int)STRLEN(IObuff + len);
3853 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003854 else if (qfp->qf_pattern != NULL)
3855 {
3856 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3857 len += (int)STRLEN(IObuff + len);
3858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 IObuff[len++] = '|';
3860 IObuff[len++] = ' ';
3861
3862 /* Remove newlines and leading whitespace from the text.
3863 * For an unrecognized line keep the indent, the compiler may
3864 * mark a word with ^^^^. */
3865 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3866 IObuff + len, IOSIZE - len);
3867
Bram Moolenaar864293a2016-06-02 13:40:04 +02003868 if (ml_append_buf(buf, lnum, IObuff,
3869 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003871 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003873 if (qfp == NULL)
3874 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003876
3877 if (old_last == NULL)
3878 /* Delete the empty line which is now at the end */
3879 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881
3882 /* correct cursor position */
3883 check_lnums(TRUE);
3884
Bram Moolenaar864293a2016-06-02 13:40:04 +02003885 if (old_last == NULL)
3886 {
3887 /* Set the 'filetype' to "qf" each time after filling the buffer.
3888 * This resembles reading a file into a buffer, it's more logical when
3889 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003890 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003891 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3892 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
Bram Moolenaar864293a2016-06-02 13:40:04 +02003894 keep_filetype = TRUE; /* don't detect 'filetype' */
3895 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003897 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003899 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003900 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003901
Bram Moolenaar864293a2016-06-02 13:40:04 +02003902 /* make sure it will be redrawn */
3903 redraw_curbuf_later(NOT_VALID);
3904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
3906 /* Restore KeyTyped, setting 'filetype' may reset it. */
3907 KeyTyped = old_KeyTyped;
3908}
3909
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003910/*
3911 * For every change made to the quickfix list, update the changed tick.
3912 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01003913 static void
3914qf_list_changed(qf_info_T *qi, int qf_idx)
3915{
3916 qi->qf_lists[qf_idx].qf_changedtick++;
3917}
3918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003920 * Return TRUE when using ":vimgrep" for ":grep".
3921 */
3922 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003923grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003924{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003925 return ((cmdidx == CMD_grep
3926 || cmdidx == CMD_lgrep
3927 || cmdidx == CMD_grepadd
3928 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003929 && STRCMP("internal",
3930 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3931}
3932
3933/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003934 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 */
3936 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003937ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003939 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003941 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003943 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003944 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003945 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003946 char_u *au_name = NULL;
3947
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003948 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3949 if (grep_internal(eap->cmdidx))
3950 {
3951 ex_vimgrep(eap);
3952 return;
3953 }
3954
Bram Moolenaar7c626922005-02-07 22:01:03 +00003955 switch (eap->cmdidx)
3956 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003957 case CMD_make: au_name = (char_u *)"make"; break;
3958 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3959 case CMD_grep: au_name = (char_u *)"grep"; break;
3960 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3961 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3962 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003963 default: break;
3964 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003965 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3966 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003967 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003968#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003969 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003970 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003971#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003972 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003973#ifdef FEAT_MBYTE
3974 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
Bram Moolenaara6557602006-02-04 22:43:20 +00003977 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3978 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003979 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003982 fname = get_mef_name();
3983 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003985 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
3987 /*
3988 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3989 */
3990 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3991 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003992 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 cmd = alloc(len);
3994 if (cmd == NULL)
3995 return;
3996 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3997 (char *)p_shq);
3998 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003999 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 /*
4001 * Output a newline if there's something else than the :make command that
4002 * was typed (in which case the cursor is in column 0).
4003 */
4004 if (msg_col == 0)
4005 msg_didout = FALSE;
4006 msg_start();
4007 MSG_PUTS(":!");
4008 msg_outtrans(cmd); /* show what we are doing */
4009
4010 /* let the shell know if we are redirecting output or not */
4011 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4012
4013#ifdef AMIGA
4014 out_flush();
4015 /* read window status report and redraw before message */
4016 (void)char_avail();
4017#endif
4018
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004019 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004020 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4021 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004022 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004023 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004024 if (wp != NULL)
4025 qi = GET_LOC_LIST(wp);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004026 if (res >= 0 && qi != NULL)
4027 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004028 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004029 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004030 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4031 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004032 if (qi != NULL && qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004033 res = qi->qf_lists[qi->qf_curlist].qf_count;
4034 else
4035 res = 0;
4036 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004037 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004038 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
Bram Moolenaar7c626922005-02-07 22:01:03 +00004040 mch_remove(fname);
4041 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 vim_free(cmd);
4043}
4044
4045/*
4046 * Return the name for the errorfile, in allocated memory.
4047 * Find a new unique name when 'makeef' contains "##".
4048 * Returns NULL for error.
4049 */
4050 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004051get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052{
4053 char_u *p;
4054 char_u *name;
4055 static int start = -1;
4056 static int off = 0;
4057#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004058 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059#endif
4060
4061 if (*p_mef == NUL)
4062 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004063 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 if (name == NULL)
4065 EMSG(_(e_notmp));
4066 return name;
4067 }
4068
4069 for (p = p_mef; *p; ++p)
4070 if (p[0] == '#' && p[1] == '#')
4071 break;
4072
4073 if (*p == NUL)
4074 return vim_strsave(p_mef);
4075
4076 /* Keep trying until the name doesn't exist yet. */
4077 for (;;)
4078 {
4079 if (start == -1)
4080 start = mch_get_pid();
4081 else
4082 off += 19;
4083
4084 name = alloc((unsigned)STRLEN(p_mef) + 30);
4085 if (name == NULL)
4086 break;
4087 STRCPY(name, p_mef);
4088 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4089 STRCAT(name, p + 2);
4090 if (mch_getperm(name) < 0
4091#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004092 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 && mch_lstat((char *)name, &sb) < 0
4094#endif
4095 )
4096 break;
4097 vim_free(name);
4098 }
4099 return name;
4100}
4101
4102/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004103 * Returns the number of valid entries in the current quickfix/location list.
4104 */
4105 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004106qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004107{
4108 qf_info_T *qi = &ql_info;
4109 qfline_T *qfp;
4110 int i, sz = 0;
4111 int prev_fnum = 0;
4112
4113 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4114 {
4115 /* Location list */
4116 qi = GET_LOC_LIST(curwin);
4117 if (qi == NULL)
4118 return 0;
4119 }
4120
4121 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004122 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004123 ++i, qfp = qfp->qf_next)
4124 {
4125 if (qfp->qf_valid)
4126 {
4127 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4128 sz++; /* Count all valid entries */
4129 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4130 {
4131 /* Count the number of files */
4132 sz++;
4133 prev_fnum = qfp->qf_fnum;
4134 }
4135 }
4136 }
4137
4138 return sz;
4139}
4140
4141/*
4142 * Returns the current index of the quickfix/location list.
4143 * Returns 0 if there is an error.
4144 */
4145 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004146qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004147{
4148 qf_info_T *qi = &ql_info;
4149
4150 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4151 {
4152 /* Location list */
4153 qi = GET_LOC_LIST(curwin);
4154 if (qi == NULL)
4155 return 0;
4156 }
4157
4158 return qi->qf_lists[qi->qf_curlist].qf_index;
4159}
4160
4161/*
4162 * Returns the current index in the quickfix/location list (counting only valid
4163 * entries). If no valid entries are in the list, then returns 1.
4164 */
4165 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004166qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004167{
4168 qf_info_T *qi = &ql_info;
4169 qf_list_T *qfl;
4170 qfline_T *qfp;
4171 int i, eidx = 0;
4172 int prev_fnum = 0;
4173
4174 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4175 {
4176 /* Location list */
4177 qi = GET_LOC_LIST(curwin);
4178 if (qi == NULL)
4179 return 1;
4180 }
4181
4182 qfl = &qi->qf_lists[qi->qf_curlist];
4183 qfp = qfl->qf_start;
4184
4185 /* check if the list has valid errors */
4186 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4187 return 1;
4188
4189 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4190 {
4191 if (qfp->qf_valid)
4192 {
4193 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4194 {
4195 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4196 {
4197 /* Count the number of files */
4198 eidx++;
4199 prev_fnum = qfp->qf_fnum;
4200 }
4201 }
4202 else
4203 eidx++;
4204 }
4205 }
4206
4207 return eidx ? eidx : 1;
4208}
4209
4210/*
4211 * Get the 'n'th valid error entry in the quickfix or location list.
4212 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4213 * For :cdo and :ldo returns the 'n'th valid error entry.
4214 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4215 */
4216 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004217qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004218{
4219 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4220 qfline_T *qfp = qfl->qf_start;
4221 int i, eidx;
4222 int prev_fnum = 0;
4223
4224 /* check if the list has valid errors */
4225 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4226 return 1;
4227
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004228 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004229 i++, qfp = qfp->qf_next)
4230 {
4231 if (qfp->qf_valid)
4232 {
4233 if (fdo)
4234 {
4235 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4236 {
4237 /* Count the number of files */
4238 eidx++;
4239 prev_fnum = qfp->qf_fnum;
4240 }
4241 }
4242 else
4243 eidx++;
4244 }
4245
4246 if (eidx == n)
4247 break;
4248 }
4249
4250 if (i <= qfl->qf_count)
4251 return i;
4252 else
4253 return 1;
4254}
4255
4256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004258 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004259 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 */
4261 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004262ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004264 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004265 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004266
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004267 if (eap->cmdidx == CMD_ll
4268 || eap->cmdidx == CMD_lrewind
4269 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004270 || eap->cmdidx == CMD_llast
4271 || eap->cmdidx == CMD_ldo
4272 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004273 {
4274 qi = GET_LOC_LIST(curwin);
4275 if (qi == NULL)
4276 {
4277 EMSG(_(e_loclist));
4278 return;
4279 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004280 }
4281
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004282 if (eap->addr_count > 0)
4283 errornr = (int)eap->line2;
4284 else
4285 {
4286 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4287 errornr = 0;
4288 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4289 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4290 errornr = 1;
4291 else
4292 errornr = 32767;
4293 }
4294
4295 /* For cdo and ldo commands, jump to the nth valid error.
4296 * For cfdo and lfdo commands, jump to the nth valid file entry.
4297 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004298 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4299 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004300 errornr = qf_get_nth_valid_entry(qi,
4301 eap->addr_count > 0 ? (int)eap->line1 : 1,
4302 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4303
4304 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305}
4306
4307/*
4308 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004309 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004310 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 */
4312 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004313ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004315 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004316 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004317
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004318 if (eap->cmdidx == CMD_lnext
4319 || eap->cmdidx == CMD_lNext
4320 || eap->cmdidx == CMD_lprevious
4321 || eap->cmdidx == CMD_lnfile
4322 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004323 || eap->cmdidx == CMD_lpfile
4324 || eap->cmdidx == CMD_ldo
4325 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004326 {
4327 qi = GET_LOC_LIST(curwin);
4328 if (qi == NULL)
4329 {
4330 EMSG(_(e_loclist));
4331 return;
4332 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004333 }
4334
Bram Moolenaar55b69262017-08-13 13:42:01 +02004335 if (eap->addr_count > 0
4336 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4337 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004338 errornr = (int)eap->line2;
4339 else
4340 errornr = 1;
4341
4342 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4343 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004345 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4346 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004348 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4349 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 ? BACKWARD_FILE
4351 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004352 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353}
4354
4355/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004356 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004357 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 */
4359 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004360ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004362 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004363 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004364 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004365 char_u *au_name = NULL;
Bram Moolenaarfc6f16b2018-03-06 17:43:22 +01004366 int save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004367 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004368
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004369 switch (eap->cmdidx)
4370 {
4371 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4372 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4373 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4374 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4375 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4376 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4377 default: break;
4378 }
4379 if (au_name != NULL)
4380 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004381#ifdef FEAT_MBYTE
4382 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4383#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004384#ifdef FEAT_BROWSE
4385 if (cmdmod.browse)
4386 {
4387 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004388 NULL, NULL,
4389 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004390 if (browse_file == NULL)
4391 return;
4392 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4393 vim_free(browse_file);
4394 }
4395 else
4396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004398 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004399
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004400 if (eap->cmdidx == CMD_lfile
4401 || eap->cmdidx == CMD_lgetfile
4402 || eap->cmdidx == CMD_laddfile)
4403 wp = curwin;
4404
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004405 /*
4406 * This function is used by the :cfile, :cgetfile and :caddfile
4407 * commands.
4408 * :cfile always creates a new quickfix list and jumps to the
4409 * first error.
4410 * :cgetfile creates a new quickfix list but doesn't jump to the
4411 * first error.
4412 * :caddfile adds to an existing quickfix list. If there is no
4413 * quickfix list then a new list is created.
4414 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004415 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4416 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004417 if (wp != NULL)
4418 qi = GET_LOC_LIST(wp);
4419 if (res >= 0 && qi != NULL)
4420 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004421 if (qi != NULL)
4422 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004423 if (au_name != NULL)
4424 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004425
4426 /* An autocmd might have freed the quickfix/location list. Check whether it
4427 * is still valid. */
4428 if (qi != NULL && !qflist_valid(wp, save_qfid))
Bram Moolenaar3c097222017-12-21 20:54:49 +01004429 return;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004430 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004431 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432}
4433
4434/*
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004435 * Return the quickfix/location list number with the given identifier.
4436 * Returns -1 if list is not found.
4437 */
4438 static int
4439qf_id2nr(qf_info_T *qi, int_u qfid)
4440{
4441 int qf_idx;
4442
4443 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4444 if (qi->qf_lists[qf_idx].qf_id == qfid)
4445 return qf_idx;
Bram Moolenaar29ce4092018-04-28 21:56:44 +02004446 return INVALID_QFIDX;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004447}
4448
4449/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004450 * Return the vimgrep autocmd name.
4451 */
4452 static char_u *
4453vgr_get_auname(cmdidx_T cmdidx)
4454{
4455 switch (cmdidx)
4456 {
4457 case CMD_vimgrep: return (char_u *)"vimgrep";
4458 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4459 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4460 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4461 case CMD_grep: return (char_u *)"grep";
4462 case CMD_lgrep: return (char_u *)"lgrep";
4463 case CMD_grepadd: return (char_u *)"grepadd";
4464 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4465 default: return NULL;
4466 }
4467}
4468
4469/*
4470 * Initialize the regmatch used by vimgrep for pattern "s".
4471 */
4472 static void
4473vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4474{
4475 /* Get the search pattern: either white-separated or enclosed in // */
4476 regmatch->regprog = NULL;
4477
4478 if (s == NULL || *s == NUL)
4479 {
4480 /* Pattern is empty, use last search pattern. */
4481 if (last_search_pat() == NULL)
4482 {
4483 EMSG(_(e_noprevre));
4484 return;
4485 }
4486 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4487 }
4488 else
4489 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4490
4491 regmatch->rmm_ic = p_ic;
4492 regmatch->rmm_maxcol = 0;
4493}
4494
4495/*
4496 * Display a file name when vimgrep is running.
4497 */
4498 static void
4499vgr_display_fname(char_u *fname)
4500{
4501 char_u *p;
4502
4503 msg_start();
4504 p = msg_strtrunc(fname, TRUE);
4505 if (p == NULL)
4506 msg_outtrans(fname);
4507 else
4508 {
4509 msg_outtrans(p);
4510 vim_free(p);
4511 }
4512 msg_clr_eos();
4513 msg_didout = FALSE; /* overwrite this message */
4514 msg_nowait = TRUE; /* don't wait for this message */
4515 msg_col = 0;
4516 out_flush();
4517}
4518
4519/*
4520 * Load a dummy buffer to search for a pattern using vimgrep.
4521 */
4522 static buf_T *
4523vgr_load_dummy_buf(
4524 char_u *fname,
4525 char_u *dirname_start,
4526 char_u *dirname_now)
4527{
4528 int save_mls;
4529#if defined(FEAT_SYN_HL)
4530 char_u *save_ei = NULL;
4531#endif
4532 buf_T *buf;
4533
4534#if defined(FEAT_SYN_HL)
4535 /* Don't do Filetype autocommands to avoid loading syntax and
4536 * indent scripts, a great speed improvement. */
4537 save_ei = au_event_disable(",Filetype");
4538#endif
4539 /* Don't use modelines here, it's useless. */
4540 save_mls = p_mls;
4541 p_mls = 0;
4542
4543 /* Load file into a buffer, so that 'fileencoding' is detected,
4544 * autocommands applied, etc. */
4545 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4546
4547 p_mls = save_mls;
4548#if defined(FEAT_SYN_HL)
4549 au_event_restore(save_ei);
4550#endif
4551
4552 return buf;
4553}
4554
4555/*
4556 * Check whether a quickfix/location list valid. Autocmds may remove or change
4557 * a quickfix list when vimgrep is running. If the list is not found, create a
4558 * new list.
4559 */
4560 static int
4561vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004562 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004563 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004564 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004565 char_u *title)
4566{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004567 /* Verify that the quickfix/location list was not freed by an autocmd */
4568 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004569 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004570 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004571 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004572 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004573 EMSG(_(e_loc_list_changed));
4574 return FALSE;
4575 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004576 else
4577 {
4578 /* Quickfix list is not found, create a new one. */
4579 qf_new_list(qi, title);
4580 return TRUE;
4581 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004582 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004583
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004584 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004585 /* Autocommands changed the quickfix list. Find the one we were
4586 * using and restore it. */
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004587 qi->qf_curlist = qf_id2nr(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004588
4589 return TRUE;
4590}
4591
4592/*
4593 * Search for a pattern in all the lines in a buffer and add the matching lines
4594 * to a quickfix list.
4595 */
4596 static int
4597vgr_match_buflines(
4598 qf_info_T *qi,
4599 char_u *fname,
4600 buf_T *buf,
4601 regmmatch_T *regmatch,
4602 long tomatch,
4603 int duplicate_name,
4604 int flags)
4605{
4606 int found_match = FALSE;
4607 long lnum;
4608 colnr_T col;
4609
4610 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
4611 {
4612 col = 0;
4613 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
4614 col, NULL, NULL) > 0)
4615 {
4616 /* Pass the buffer number so that it gets used even for a
4617 * dummy buffer, unless duplicate_name is set, then the
4618 * buffer will be wiped out below. */
4619 if (qf_add_entry(qi,
4620 qi->qf_curlist,
4621 NULL, /* dir */
4622 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02004623 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004624 duplicate_name ? 0 : buf->b_fnum,
4625 ml_get_buf(buf,
4626 regmatch->startpos[0].lnum + lnum, FALSE),
4627 regmatch->startpos[0].lnum + lnum,
4628 regmatch->startpos[0].col + 1,
4629 FALSE, /* vis_col */
4630 NULL, /* search pattern */
4631 0, /* nr */
4632 0, /* type */
4633 TRUE /* valid */
4634 ) == FAIL)
4635 {
4636 got_int = TRUE;
4637 break;
4638 }
4639 found_match = TRUE;
4640 if (--tomatch == 0)
4641 break;
4642 if ((flags & VGR_GLOBAL) == 0
4643 || regmatch->endpos[0].lnum > 0)
4644 break;
4645 col = regmatch->endpos[0].col
4646 + (col == regmatch->endpos[0].col);
4647 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
4648 break;
4649 }
4650 line_breakcheck();
4651 if (got_int)
4652 break;
4653 }
4654
4655 return found_match;
4656}
4657
4658/*
4659 * Jump to the first match and update the directory.
4660 */
4661 static void
4662vgr_jump_to_match(
4663 qf_info_T *qi,
4664 int forceit,
4665 int *redraw_for_dummy,
4666 buf_T *first_match_buf,
4667 char_u *target_dir)
4668{
4669 buf_T *buf;
4670
4671 buf = curbuf;
4672 qf_jump(qi, 0, 0, forceit);
4673 if (buf != curbuf)
4674 /* If we jumped to another buffer redrawing will already be
4675 * taken care of. */
4676 *redraw_for_dummy = FALSE;
4677
4678 /* Jump to the directory used after loading the buffer. */
4679 if (curbuf == first_match_buf && target_dir != NULL)
4680 {
4681 exarg_T ea;
4682
4683 ea.arg = target_dir;
4684 ea.cmdidx = CMD_lcd;
4685 ex_cd(&ea);
4686 }
4687}
4688
4689/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004690 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004691 * ":vimgrepadd {pattern} file(s)"
4692 * ":lvimgrep {pattern} file(s)"
4693 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004694 */
4695 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004696ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004697{
Bram Moolenaar81695252004-12-29 20:58:21 +00004698 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004699 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004700 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004701 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004702 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004703 char_u *s;
4704 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004705 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004706 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004707 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004708 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004709 buf_T *buf;
4710 int duplicate_name = FALSE;
4711 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004712 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004713 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004714 buf_T *first_match_buf = NULL;
4715 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004716 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004717 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004718 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004719 char_u *dirname_start = NULL;
4720 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004721 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004722 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004723
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004724 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01004725 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4726 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004727 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004728#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004729 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004730 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004731#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004732 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004733
Bram Moolenaar754b5602006-02-09 23:53:20 +00004734 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004735 || eap->cmdidx == CMD_lvimgrep
4736 || eap->cmdidx == CMD_lgrepadd
4737 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004738 {
4739 qi = ll_get_or_alloc_list(curwin);
4740 if (qi == NULL)
4741 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004742 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004743 }
4744
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004745 if (eap->addr_count > 0)
4746 tomatch = eap->line2;
4747 else
4748 tomatch = MAXLNUM;
4749
Bram Moolenaar81695252004-12-29 20:58:21 +00004750 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004751 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004752 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004753 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004754 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004755 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004756 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004757 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004758 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004759
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004760 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004761 if (regmatch.regprog == NULL)
4762 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004763
4764 p = skipwhite(p);
4765 if (*p == NUL)
4766 {
4767 EMSG(_("E683: File name missing or invalid pattern"));
4768 goto theend;
4769 }
4770
Bram Moolenaar55b69262017-08-13 13:42:01 +02004771 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004772 && eap->cmdidx != CMD_vimgrepadd
4773 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004774 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004775 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004776 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004777
4778 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004779 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004780 goto theend;
4781 if (fcount == 0)
4782 {
4783 EMSG(_(e_nomatch));
4784 goto theend;
4785 }
4786
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004787 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4788 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004789 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004790 {
4791 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004792 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004793 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004794
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004795 /* Remember the current directory, because a BufRead autocommand that does
4796 * ":lcd %:p:h" changes the meaning of short path names. */
4797 mch_dirname(dirname_start, MAXPATHL);
4798
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004799 /* Remember the current quickfix list identifier, so that we can check for
4800 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004801 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004802
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004803 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004804 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004805 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004806 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004807 if (time(NULL) > seconds)
4808 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004809 /* Display the file name every second or so, show the user we are
4810 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004811 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004812 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004813 }
4814
Bram Moolenaar81695252004-12-29 20:58:21 +00004815 buf = buflist_findname_exp(fnames[fi]);
4816 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4817 {
4818 /* Remember that a buffer with this name already exists. */
4819 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004820 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004821 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004822
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004823 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00004824 }
4825 else
4826 /* Use existing, loaded buffer. */
4827 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004828
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004829 /* Check whether the quickfix list is still valid. When loading a
4830 * buffer above, autocommands might have changed the quickfix list. */
4831 if (!vgr_qflist_valid(wp, qi, save_qfid, *eap->cmdlinep))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004832 {
4833 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004834 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02004835 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004836 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004837
Bram Moolenaar81695252004-12-29 20:58:21 +00004838 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004839 {
4840 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004841 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004842 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004843 else
4844 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004845 /* Try for a match in all lines of the buffer.
4846 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004847 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
4848 tomatch, duplicate_name, flags);
4849
Bram Moolenaar81695252004-12-29 20:58:21 +00004850 if (using_dummy)
4851 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004852 if (found_match && first_match_buf == NULL)
4853 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004854 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004855 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004856 /* Never keep a dummy buffer if there is another buffer
4857 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004858 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004859 buf = NULL;
4860 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004861 else if (!cmdmod.hide
4862 || buf->b_p_bh[0] == 'u' /* "unload" */
4863 || buf->b_p_bh[0] == 'w' /* "wipe" */
4864 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004865 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004866 /* When no match was found we don't need to remember the
4867 * buffer, wipe it out. If there was a match and it
4868 * wasn't the first one or we won't jump there: only
4869 * unload the buffer.
4870 * Ignore 'hidden' here, because it may lead to having too
4871 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004872 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004873 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004874 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004875 buf = NULL;
4876 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004877 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004878 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004879 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004880 /* Keeping the buffer, remove the dummy flag. */
4881 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004882 buf = NULL;
4883 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004884 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004885
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004886 if (buf != NULL)
4887 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004888 /* Keeping the buffer, remove the dummy flag. */
4889 buf->b_flags &= ~BF_DUMMY;
4890
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004891 /* If the buffer is still loaded we need to use the
4892 * directory we jumped to below. */
4893 if (buf == first_match_buf
4894 && target_dir == NULL
4895 && STRCMP(dirname_start, dirname_now) != 0)
4896 target_dir = vim_strsave(dirname_now);
4897
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004898 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004899 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004900 * need to be done (again). But not the window-local
4901 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004902 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004903#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004904 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4905 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004906#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004907 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004908 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004909 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004910 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004911 }
4912 }
4913
4914 FreeWild(fcount, fnames);
4915
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004916 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4917 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4918 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004919 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004920
Bram Moolenaar864293a2016-06-02 13:40:04 +02004921 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004922
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004923 if (au_name != NULL)
4924 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4925 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01004926 /*
4927 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
4928 * is still valid.
4929 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01004930 if (!qflist_valid(wp, save_qfid))
4931 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004932
Bram Moolenaar86b68352004-12-27 21:59:20 +00004933 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004934 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004935 {
4936 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004937 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
4938 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004939 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004940 else
4941 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004942
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004943 /* If we loaded a dummy buffer into the current window, the autocommands
4944 * may have messed up things, need to redraw and recompute folds. */
4945 if (redraw_for_dummy)
4946 {
4947#ifdef FEAT_FOLDING
4948 foldUpdateAll(curwin);
4949#else
4950 redraw_later(NOT_VALID);
4951#endif
4952 }
4953
Bram Moolenaar86b68352004-12-27 21:59:20 +00004954theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004955 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004956 vim_free(dirname_now);
4957 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004958 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004959 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004960}
4961
4962/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004963 * Restore current working directory to "dirname_start" if they differ, taking
4964 * into account whether it is set locally or globally.
4965 */
4966 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004967restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004968{
4969 char_u *dirname_now = alloc(MAXPATHL);
4970
4971 if (NULL != dirname_now)
4972 {
4973 mch_dirname(dirname_now, MAXPATHL);
4974 if (STRCMP(dirname_start, dirname_now) != 0)
4975 {
4976 /* If the directory has changed, change it back by building up an
4977 * appropriate ex command and executing it. */
4978 exarg_T ea;
4979
4980 ea.arg = dirname_start;
4981 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4982 ex_cd(&ea);
4983 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004984 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004985 }
4986}
4987
4988/*
4989 * Load file "fname" into a dummy buffer and return the buffer pointer,
4990 * placing the directory resulting from the buffer load into the
4991 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4992 * prior to calling this function. Restores directory to "dirname_start" prior
4993 * to returning, if autocmds or the 'autochdir' option have changed it.
4994 *
4995 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4996 * or wipe_dummy_buffer() later!
4997 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004998 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004999 */
5000 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005001load_dummy_buffer(
5002 char_u *fname,
5003 char_u *dirname_start, /* in: old directory */
5004 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005005{
5006 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005007 bufref_T newbufref;
5008 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005009 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005010 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005011 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005012
5013 /* Allocate a buffer without putting it in the buffer list. */
5014 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5015 if (newbuf == NULL)
5016 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005017 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005018
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005019 /* Init the options. */
5020 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5021
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005022 /* need to open the memfile before putting the buffer in a window */
5023 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005024 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005025 /* Make sure this buffer isn't wiped out by auto commands. */
5026 ++newbuf->b_locked;
5027
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005028 /* set curwin/curbuf to buf and save a few things */
5029 aucmd_prepbuf(&aco, newbuf);
5030
5031 /* Need to set the filename for autocommands. */
5032 (void)setfname(curbuf, fname, NULL, FALSE);
5033
Bram Moolenaar81695252004-12-29 20:58:21 +00005034 /* Create swap file now to avoid the ATTENTION message. */
5035 check_need_swap(TRUE);
5036
5037 /* Remove the "dummy" flag, otherwise autocommands may not
5038 * work. */
5039 curbuf->b_flags &= ~BF_DUMMY;
5040
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005041 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005042 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005043 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005044 NULL, READ_NEW | READ_DUMMY);
5045 --newbuf->b_locked;
5046 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005047 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005048 && !(curbuf->b_flags & BF_NEW))
5049 {
5050 failed = FALSE;
5051 if (curbuf != newbuf)
5052 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005053 /* Bloody autocommands changed the buffer! Can happen when
5054 * using netrw and editing a remote file. Use the current
5055 * buffer instead, delete the dummy one after restoring the
5056 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005057 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005058 newbuf = curbuf;
5059 }
5060 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005061
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005062 /* restore curwin/curbuf and a few other things */
5063 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005064 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5065 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005066
5067 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5068 * skip it. */
5069 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005070 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005071
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005072 /*
5073 * When autocommands/'autochdir' option changed directory: go back.
5074 * Let the caller know what the resulting dir was first, in case it is
5075 * important.
5076 */
5077 mch_dirname(resulting_dir, MAXPATHL);
5078 restore_start_dir(dirname_start);
5079
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005080 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005081 return NULL;
5082 if (failed)
5083 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005084 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005085 return NULL;
5086 }
5087 return newbuf;
5088}
5089
5090/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005091 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5092 * directory to "dirname_start" prior to returning, if autocmds or the
5093 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005094 */
5095 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005096wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005097{
5098 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005099 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005100#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005101 cleanup_T cs;
5102
5103 /* Reset the error/interrupt/exception state here so that aborting()
5104 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5105 * work when got_int is set. */
5106 enter_cleanup(&cs);
5107#endif
5108
Bram Moolenaar81695252004-12-29 20:58:21 +00005109 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005110
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005111#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005112 /* Restore the error/interrupt/exception state if not discarded by a
5113 * new aborting error, interrupt, or uncaught exception. */
5114 leave_cleanup(&cs);
5115#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005116 /* When autocommands/'autochdir' option changed directory: go back. */
5117 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005118 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005119}
5120
5121/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005122 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5123 * directory to "dirname_start" prior to returning, if autocmds or the
5124 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005125 */
5126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005127unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005128{
5129 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005130 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005131 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005132
5133 /* When autocommands/'autochdir' option changed directory: go back. */
5134 restore_start_dir(dirname_start);
5135 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005136}
5137
Bram Moolenaar05159a02005-02-26 23:04:13 +00005138#if defined(FEAT_EVAL) || defined(PROTO)
5139/*
5140 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005141 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005142 */
5143 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005144get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005145{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005146 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005147 dict_T *dict;
5148 char_u buf[2];
5149 qfline_T *qfp;
5150 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005151 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005152
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005153 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005154 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005155 qi = &ql_info;
5156 if (wp != NULL)
5157 {
5158 qi = GET_LOC_LIST(wp);
5159 if (qi == NULL)
5160 return FAIL;
5161 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005162 }
5163
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005164 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005165 qf_idx = qi->qf_curlist;
5166
5167 if (qf_idx >= qi->qf_listcount
5168 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005169 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005170
Bram Moolenaard823fa92016-08-12 16:29:27 +02005171 qfp = qi->qf_lists[qf_idx].qf_start;
5172 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005173 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005174 /* Handle entries with a non-existing buffer number. */
5175 bufnum = qfp->qf_fnum;
5176 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5177 bufnum = 0;
5178
Bram Moolenaar05159a02005-02-26 23:04:13 +00005179 if ((dict = dict_alloc()) == NULL)
5180 return FAIL;
5181 if (list_append_dict(list, dict) == FAIL)
5182 return FAIL;
5183
5184 buf[0] = qfp->qf_type;
5185 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005186 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00005187 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
5188 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
5189 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
5190 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaard76ce852018-05-01 15:02:04 +02005191 || dict_add_nr_str(dict, "module", 0L,
5192 qfp->qf_module == NULL ? (char_u *)"" : qfp->qf_module) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00005193 || dict_add_nr_str(dict, "pattern", 0L,
5194 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
5195 || dict_add_nr_str(dict, "text", 0L,
5196 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00005197 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
5198 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
5199 return FAIL;
5200
5201 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005202 if (qfp == NULL)
5203 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005204 }
5205 return OK;
5206}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005207
5208/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005209 * Flags used by getqflist()/getloclist() to determine which fields to return.
5210 */
5211enum {
5212 QF_GETLIST_NONE = 0x0,
5213 QF_GETLIST_TITLE = 0x1,
5214 QF_GETLIST_ITEMS = 0x2,
5215 QF_GETLIST_NR = 0x4,
5216 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005217 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005218 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005219 QF_GETLIST_IDX = 0x40,
5220 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005221 QF_GETLIST_TICK = 0x100,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005222 QF_GETLIST_ALL = 0x1FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005223};
5224
5225/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005226 * Parse text from 'di' and return the quickfix list items.
5227 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005228 */
5229 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005230qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005231{
5232 int status = FAIL;
5233 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005234 char_u *errorformat = p_efm;
5235 dictitem_T *efm_di;
5236 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005237
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005238 /* Only a List value is supported */
5239 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005240 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005241 /* If errorformat is supplied then use it, otherwise use the 'efm'
5242 * option setting
5243 */
5244 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5245 {
5246 if (efm_di->di_tv.v_type != VAR_STRING ||
5247 efm_di->di_tv.vval.v_string == NULL)
5248 return FAIL;
5249 errorformat = efm_di->di_tv.vval.v_string;
5250 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005251
Bram Moolenaar36538222017-09-02 19:51:44 +02005252 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005253 if (l == NULL)
5254 return FAIL;
5255
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005256 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
5257 if (qi != NULL)
5258 {
5259 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
5260 qi->qf_refcount++;
5261
Bram Moolenaar36538222017-09-02 19:51:44 +02005262 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005263 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5264 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005265 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005266 qf_free(qi, 0);
5267 }
5268 free(qi);
5269 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005270 dict_add_list(retdict, "items", l);
5271 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005272 }
5273
5274 return status;
5275}
5276
5277/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005278 * Return the quickfix/location list window identifier in the current tabpage.
5279 */
5280 static int
5281qf_winid(qf_info_T *qi)
5282{
5283 win_T *win;
5284
5285 /* The quickfix window can be opened even if the quickfix list is not set
5286 * using ":copen". This is not true for location lists. */
5287 if (qi == NULL)
5288 return 0;
5289 win = qf_find_win(qi);
5290 if (win != NULL)
5291 return win->w_id;
5292 return 0;
5293}
5294
5295/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005296 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005297 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005298 static int
5299qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005300{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005301 int flags = QF_GETLIST_NONE;
5302
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005303 if (dict_find(what, (char_u *)"all", -1) != NULL)
5304 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005305
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005306 if (dict_find(what, (char_u *)"title", -1) != NULL)
5307 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005308
Bram Moolenaara6d48492017-12-12 22:45:31 +01005309 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5310 flags |= QF_GETLIST_NR;
5311
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005312 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5313 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005314
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005315 if (dict_find(what, (char_u *)"context", -1) != NULL)
5316 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005317
Bram Moolenaara6d48492017-12-12 22:45:31 +01005318 if (dict_find(what, (char_u *)"id", -1) != NULL)
5319 flags |= QF_GETLIST_ID;
5320
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005321 if (dict_find(what, (char_u *)"items", -1) != NULL)
5322 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005323
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005324 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5325 flags |= QF_GETLIST_IDX;
5326
5327 if (dict_find(what, (char_u *)"size", -1) != NULL)
5328 flags |= QF_GETLIST_SIZE;
5329
Bram Moolenaarb254af32017-12-18 19:48:58 +01005330 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5331 flags |= QF_GETLIST_TICK;
5332
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005333 return flags;
5334}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005335
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005336/*
5337 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5338 * If 'nr' and 'id' are not present in 'what' then return the current
5339 * quickfix list index.
5340 * If 'nr' is zero then return the current quickfix list index.
5341 * If 'nr' is '$' then return the last quickfix list index.
5342 * If 'id' is present then return the index of the quickfix list with that id.
5343 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5344 * Return -1, if quickfix list is not present or if the stack is empty.
5345 */
5346 static int
5347qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5348{
5349 int qf_idx;
5350 dictitem_T *di;
5351
5352 qf_idx = qi->qf_curlist; /* default is the current list */
5353 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5354 {
5355 /* Use the specified quickfix/location list */
5356 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005357 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005358 /* for zero use the current list */
5359 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005360 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005361 qf_idx = di->di_tv.vval.v_number - 1;
5362 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005363 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005364 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005365 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005366 else if (di->di_tv.v_type == VAR_STRING
5367 && di->di_tv.vval.v_string != NULL
5368 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5369 /* Get the last quickfix list number */
5370 qf_idx = qi->qf_listcount - 1;
5371 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005372 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005373 }
5374
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005375 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5376 {
5377 /* Look for a list with the specified id */
5378 if (di->di_tv.v_type == VAR_NUMBER)
5379 {
5380 /*
5381 * For zero, use the current list or the list specified by 'nr'
5382 */
5383 if (di->di_tv.vval.v_number != 0)
5384 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5385 }
5386 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005387 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005388 }
5389
5390 return qf_idx;
5391}
5392
5393/*
5394 * Return default values for quickfix list properties in retdict.
5395 */
5396 static int
5397qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5398{
5399 int status = OK;
5400
5401 if (flags & QF_GETLIST_TITLE)
5402 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5403 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5404 {
5405 list_T *l = list_alloc();
5406 if (l != NULL)
5407 status = dict_add_list(retdict, "items", l);
5408 else
5409 status = FAIL;
5410 }
5411 if ((status == OK) && (flags & QF_GETLIST_NR))
5412 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5413 if ((status == OK) && (flags & QF_GETLIST_WINID))
5414 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
5415 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5416 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5417 if ((status == OK) && (flags & QF_GETLIST_ID))
5418 status = dict_add_nr_str(retdict, "id", 0L, NULL);
5419 if ((status == OK) && (flags & QF_GETLIST_IDX))
5420 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5421 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5422 status = dict_add_nr_str(retdict, "size", 0L, NULL);
5423 if ((status == OK) && (flags & QF_GETLIST_TICK))
5424 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
5425
5426 return status;
5427}
5428
5429/*
5430 * Return the quickfix list title as 'title' in retdict
5431 */
5432 static int
5433qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5434{
5435 char_u *t;
5436
5437 t = qi->qf_lists[qf_idx].qf_title;
5438 if (t == NULL)
5439 t = (char_u *)"";
5440 return dict_add_nr_str(retdict, "title", 0L, t);
5441}
5442
5443/*
5444 * Return the quickfix list items/entries as 'items' in retdict
5445 */
5446 static int
5447qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5448{
5449 int status = OK;
5450 list_T *l = list_alloc();
5451 if (l != NULL)
5452 {
5453 (void)get_errorlist(qi, NULL, qf_idx, l);
5454 dict_add_list(retdict, "items", l);
5455 }
5456 else
5457 status = FAIL;
5458
5459 return status;
5460}
5461
5462/*
5463 * Return the quickfix list context (if any) as 'context' in retdict.
5464 */
5465 static int
5466qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5467{
5468 int status;
5469 dictitem_T *di;
5470
5471 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5472 {
5473 di = dictitem_alloc((char_u *)"context");
5474 if (di != NULL)
5475 {
5476 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5477 status = dict_add(retdict, di);
5478 if (status == FAIL)
5479 dictitem_free(di);
5480 }
5481 else
5482 status = FAIL;
5483 }
5484 else
5485 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5486
5487 return status;
5488}
5489
5490/*
5491 * Return the quickfix list index as 'idx' in retdict
5492 */
5493 static int
5494qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5495{
5496 int idx = qi->qf_lists[qf_idx].qf_index;
5497 if (qi->qf_lists[qf_idx].qf_count == 0)
5498 /* For empty lists, qf_index is set to 1 */
5499 idx = 0;
5500 return dict_add_nr_str(retdict, "idx", idx, NULL);
5501}
5502
5503/*
5504 * Return quickfix/location list details (title) as a
5505 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5506 * then current list is used. Otherwise the specified list is used.
5507 */
5508 int
5509qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5510{
5511 qf_info_T *qi = &ql_info;
5512 int status = OK;
5513 int qf_idx;
5514 dictitem_T *di;
5515 int flags = QF_GETLIST_NONE;
5516
5517 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5518 return qf_get_list_from_lines(what, di, retdict);
5519
5520 if (wp != NULL)
5521 qi = GET_LOC_LIST(wp);
5522
5523 flags = qf_getprop_keys2flags(what);
5524
5525 if (qi != NULL && qi->qf_listcount != 0)
5526 qf_idx = qf_getprop_qfidx(qi, what);
5527
Bram Moolenaara6d48492017-12-12 22:45:31 +01005528 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005529 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005530 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005531
Bram Moolenaard823fa92016-08-12 16:29:27 +02005532 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005533 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005534 if ((status == OK) && (flags & QF_GETLIST_NR))
5535 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5536 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005537 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005538 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005539 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005540 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005541 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005542 if ((status == OK) && (flags & QF_GETLIST_ID))
5543 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5544 NULL);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005545 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005546 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005547 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5548 status = dict_add_nr_str(retdict, "size",
5549 qi->qf_lists[qf_idx].qf_count, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005550 if ((status == OK) && (flags & QF_GETLIST_TICK))
5551 status = dict_add_nr_str(retdict, "changedtick",
5552 qi->qf_lists[qf_idx].qf_changedtick, NULL);
5553
Bram Moolenaard823fa92016-08-12 16:29:27 +02005554 return status;
5555}
5556
5557/*
5558 * Add list of entries to quickfix/location list. Each list entry is
5559 * a dictionary with item information.
5560 */
5561 static int
5562qf_add_entries(
5563 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005564 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005565 list_T *list,
5566 char_u *title,
5567 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005568{
5569 listitem_T *li;
5570 dict_T *d;
Bram Moolenaard76ce852018-05-01 15:02:04 +02005571 char_u *filename, *module, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005572 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005573 long lnum;
5574 int col, nr;
5575 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005576 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005577 int valid, status;
5578 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005579 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005580
Bram Moolenaara3921f42017-06-04 15:30:34 +02005581 if (action == ' ' || qf_idx == qi->qf_listcount)
5582 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005583 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005584 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005585 qf_idx = qi->qf_curlist;
5586 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005587 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005588 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005589 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005590 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005591 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005592 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005593 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005594 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005595
5596 for (li = list->lv_first; li != NULL; li = li->li_next)
5597 {
5598 if (li->li_tv.v_type != VAR_DICT)
5599 continue; /* Skip non-dict items */
5600
5601 d = li->li_tv.vval.v_dict;
5602 if (d == NULL)
5603 continue;
5604
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005605 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaard76ce852018-05-01 15:02:04 +02005606 module = get_dict_string(d, (char_u *)"module", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005607 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5608 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5609 col = (int)get_dict_number(d, (char_u *)"col");
5610 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5611 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005612 type = get_dict_string(d, (char_u *)"type", TRUE);
5613 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5614 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005615 if (text == NULL)
5616 text = vim_strsave((char_u *)"");
5617
5618 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005619 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005620 valid = FALSE;
5621
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005622 /* Mark entries with non-existing buffer number as not valid. Give the
5623 * error message only once. */
5624 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5625 {
5626 if (!did_bufnr_emsg)
5627 {
5628 did_bufnr_emsg = TRUE;
5629 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5630 }
5631 valid = FALSE;
5632 bufnum = 0;
5633 }
5634
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005635 /* If the 'valid' field is present it overrules the detected value. */
5636 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5637 valid = (int)get_dict_number(d, (char_u *)"valid");
5638
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005639 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005640 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005641 NULL, /* dir */
5642 filename,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005643 module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005644 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005645 text,
5646 lnum,
5647 col,
5648 vcol, /* vis_col */
5649 pattern, /* search pattern */
5650 nr,
5651 type == NULL ? NUL : *type,
5652 valid);
5653
5654 vim_free(filename);
Bram Moolenaard76ce852018-05-01 15:02:04 +02005655 vim_free(module);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005656 vim_free(pattern);
5657 vim_free(text);
5658 vim_free(type);
5659
5660 if (status == FAIL)
5661 {
5662 retval = FAIL;
5663 break;
5664 }
5665 }
5666
Bram Moolenaara3921f42017-06-04 15:30:34 +02005667 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005668 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005669 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005670 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005671 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005672 if (action != 'a')
5673 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005674 qi->qf_lists[qf_idx].qf_ptr =
5675 qi->qf_lists[qf_idx].qf_start;
5676 if (qi->qf_lists[qf_idx].qf_count > 0)
5677 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005678 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005679
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005680 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005681 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005682
5683 return retval;
5684}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005685
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005686/*
5687 * Get the quickfix list index from 'nr' or 'id'
5688 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02005689 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005690qf_setprop_get_qfidx(
5691 qf_info_T *qi,
5692 dict_T *what,
5693 int action,
5694 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005695{
5696 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005697 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005698
Bram Moolenaard823fa92016-08-12 16:29:27 +02005699 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5700 {
5701 /* Use the specified quickfix/location list */
5702 if (di->di_tv.v_type == VAR_NUMBER)
5703 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005704 /* for zero use the current list */
5705 if (di->di_tv.vval.v_number != 0)
5706 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005707
Bram Moolenaar55b69262017-08-13 13:42:01 +02005708 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5709 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005710 /*
5711 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005712 * non-available list and add the new list at the end of the
5713 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005714 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005715 *newlist = TRUE;
5716 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005717 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005718 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005719 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005720 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005721 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005722 }
5723 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005724 && di->di_tv.vval.v_string != NULL
5725 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005726 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005727 if (qi->qf_listcount > 0)
5728 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005729 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02005730 qf_idx = 0;
5731 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005732 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005733 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005734 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005735 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005736 }
5737
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005738 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005739 {
5740 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005741 if (di->di_tv.v_type != VAR_NUMBER)
5742 return INVALID_QFIDX;
5743
5744 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005745 }
5746
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005747 return qf_idx;
5748}
5749
5750/*
5751 * Set the quickfix list title.
5752 */
5753 static int
5754qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
5755{
5756 if (di->di_tv.v_type != VAR_STRING)
5757 return FAIL;
5758
5759 vim_free(qi->qf_lists[qf_idx].qf_title);
5760 qi->qf_lists[qf_idx].qf_title =
5761 get_dict_string(what, (char_u *)"title", TRUE);
5762 if (qf_idx == qi->qf_curlist)
5763 qf_update_win_titlevar(qi);
5764
5765 return OK;
5766}
5767
5768/*
5769 * Set quickfix list items/entries.
5770 */
5771 static int
5772qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
5773{
5774 int retval = FAIL;
5775 char_u *title_save;
5776
5777 if (di->di_tv.v_type != VAR_LIST)
5778 return FAIL;
5779
5780 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5781 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5782 title_save, action == ' ' ? 'a' : action);
5783 if (action == 'r')
5784 {
5785 /*
5786 * When replacing the quickfix list entries using
5787 * qf_add_entries(), the title is set with a ':' prefix.
5788 * Restore the title with the saved title.
5789 */
5790 vim_free(qi->qf_lists[qf_idx].qf_title);
5791 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5792 }
5793 vim_free(title_save);
5794
5795 return retval;
5796}
5797
5798/*
5799 * Set quickfix list items/entries from a list of lines.
5800 */
5801 static int
5802qf_setprop_items_from_lines(
5803 qf_info_T *qi,
5804 int qf_idx,
5805 dict_T *what,
5806 dictitem_T *di,
5807 int action)
5808{
5809 char_u *errorformat = p_efm;
5810 dictitem_T *efm_di;
5811 int retval = FAIL;
5812
5813 /* Use the user supplied errorformat settings (if present) */
5814 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5815 {
5816 if (efm_di->di_tv.v_type != VAR_STRING ||
5817 efm_di->di_tv.vval.v_string == NULL)
5818 return FAIL;
5819 errorformat = efm_di->di_tv.vval.v_string;
5820 }
5821
5822 /* Only a List value is supported */
5823 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
5824 return FAIL;
5825
5826 if (action == 'r')
5827 qf_free_items(qi, qf_idx);
5828 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
5829 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5830 retval = OK;
5831
5832 return retval;
5833}
5834
5835/*
5836 * Set quickfix list context.
5837 */
5838 static int
5839qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
5840{
5841 typval_T *ctx;
5842
5843 free_tv(qi->qf_lists[qf_idx].qf_ctx);
5844 ctx = alloc_tv();
5845 if (ctx != NULL)
5846 copy_tv(&di->di_tv, ctx);
5847 qi->qf_lists[qf_idx].qf_ctx = ctx;
5848
5849 return OK;
5850}
5851
5852/*
5853 * Set quickfix/location list properties (title, items, context).
5854 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02005855 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005856 */
5857 static int
5858qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
5859{
5860 dictitem_T *di;
5861 int retval = FAIL;
5862 int qf_idx;
5863 int newlist = FALSE;
5864
5865 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5866 newlist = TRUE;
5867
5868 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
5869 if (qf_idx == INVALID_QFIDX) /* List not found */
5870 return FAIL;
5871
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005872 if (newlist)
5873 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005874 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005875 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005876 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005877 }
5878
5879 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005880 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005881 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005882 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005883 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005884 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005885 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005886 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005887
Bram Moolenaarb254af32017-12-18 19:48:58 +01005888 if (retval == OK)
5889 qf_list_changed(qi, qf_idx);
5890
Bram Moolenaard823fa92016-08-12 16:29:27 +02005891 return retval;
5892}
5893
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005894/*
5895 * Find the non-location list window with the specified location list.
5896 */
5897 static win_T *
5898find_win_with_ll(qf_info_T *qi)
5899{
5900 win_T *wp = NULL;
5901
5902 FOR_ALL_WINDOWS(wp)
5903 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5904 return wp;
5905
5906 return NULL;
5907}
5908
5909/*
5910 * Free the entire quickfix/location list stack.
5911 * If the quickfix/location list window is open, then clear it.
5912 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005913 static void
5914qf_free_stack(win_T *wp, qf_info_T *qi)
5915{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005916 win_T *qfwin = qf_find_win(qi);
5917 win_T *llwin = NULL;
5918 win_T *orig_wp = wp;
5919
5920 if (qfwin != NULL)
5921 {
5922 /* If the quickfix/location list window is open, then clear it */
5923 if (qi->qf_curlist < qi->qf_listcount)
5924 qf_free(qi, qi->qf_curlist);
5925 qf_update_buffer(qi, NULL);
5926 }
5927
5928 if (wp != NULL && IS_LL_WINDOW(wp))
5929 {
5930 /* If in the location list window, then use the non-location list
5931 * window with this location list (if present)
5932 */
5933 llwin = find_win_with_ll(qi);
5934 if (llwin != NULL)
5935 wp = llwin;
5936 }
5937
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005938 qf_free_all(wp);
5939 if (wp == NULL)
5940 {
5941 /* quickfix list */
5942 qi->qf_curlist = 0;
5943 qi->qf_listcount = 0;
5944 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005945 else if (IS_LL_WINDOW(orig_wp))
5946 {
5947 /* If the location list window is open, then create a new empty
5948 * location list */
5949 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005950
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005951 /* first free the list reference in the location list window */
5952 ll_free_all(&orig_wp->w_llist_ref);
5953
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005954 orig_wp->w_llist_ref = new_ll;
5955 if (llwin != NULL)
5956 {
5957 llwin->w_llist = new_ll;
5958 new_ll->qf_refcount++;
5959 }
5960 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005961}
5962
Bram Moolenaard823fa92016-08-12 16:29:27 +02005963/*
5964 * Populate the quickfix list with the items supplied in the list
5965 * of dictionaries. "title" will be copied to w:quickfix_title.
5966 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5967 */
5968 int
5969set_errorlist(
5970 win_T *wp,
5971 list_T *list,
5972 int action,
5973 char_u *title,
5974 dict_T *what)
5975{
5976 qf_info_T *qi = &ql_info;
5977 int retval = OK;
5978
5979 if (wp != NULL)
5980 {
5981 qi = ll_get_or_alloc_list(wp);
5982 if (qi == NULL)
5983 return FAIL;
5984 }
5985
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005986 if (action == 'f')
5987 {
5988 /* Free the entire quickfix or location list stack */
5989 qf_free_stack(wp, qi);
5990 }
5991 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005992 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005993 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01005994 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005995 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005996 if (retval == OK)
5997 qf_list_changed(qi, qi->qf_curlist);
5998 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005999
6000 return retval;
6001}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006002
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006003/*
6004 * Mark the context as in use for all the lists in a quickfix stack.
6005 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006006 static int
6007mark_quickfix_ctx(qf_info_T *qi, int copyID)
6008{
6009 int i;
6010 int abort = FALSE;
6011 typval_T *ctx;
6012
6013 for (i = 0; i < LISTCOUNT && !abort; ++i)
6014 {
6015 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006016 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6017 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006018 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6019 }
6020
6021 return abort;
6022}
6023
6024/*
6025 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006026 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006027 */
6028 int
6029set_ref_in_quickfix(int copyID)
6030{
6031 int abort = FALSE;
6032 tabpage_T *tp;
6033 win_T *win;
6034
6035 abort = mark_quickfix_ctx(&ql_info, copyID);
6036 if (abort)
6037 return abort;
6038
6039 FOR_ALL_TAB_WINDOWS(tp, win)
6040 {
6041 if (win->w_llist != NULL)
6042 {
6043 abort = mark_quickfix_ctx(win->w_llist, copyID);
6044 if (abort)
6045 return abort;
6046 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006047 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6048 {
6049 /* In a location list window and none of the other windows is
6050 * referring to this location list. Mark the location list
6051 * context as still in use.
6052 */
6053 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6054 if (abort)
6055 return abort;
6056 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006057 }
6058
6059 return abort;
6060}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006061#endif
6062
Bram Moolenaar81695252004-12-29 20:58:21 +00006063/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006064 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006065 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006066 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006067 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006068 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006069 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006070 */
6071 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006072ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006073{
6074 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006075 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006076 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006077 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006078
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006079 switch (eap->cmdidx)
6080 {
6081 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6082 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6083 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6084 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6085 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6086 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6087 default: break;
6088 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006089 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6090 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006091 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006092#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006093 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006094 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006095#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006096 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006097
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006098 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01006099 if (eap->cmdidx == CMD_lbuffer
6100 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006101 || eap->cmdidx == CMD_laddbuffer)
6102 {
6103 qi = ll_get_or_alloc_list(curwin);
6104 if (qi == NULL)
6105 return;
6106 }
6107
Bram Moolenaar86b68352004-12-27 21:59:20 +00006108 if (*eap->arg == NUL)
6109 buf = curbuf;
6110 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6111 buf = buflist_findnr(atoi((char *)eap->arg));
6112 if (buf == NULL)
6113 EMSG(_(e_invarg));
6114 else if (buf->b_ml.ml_mfp == NULL)
6115 EMSG(_("E681: Buffer is not loaded"));
6116 else
6117 {
6118 if (eap->addr_count == 0)
6119 {
6120 eap->line1 = 1;
6121 eap->line2 = buf->b_ml.ml_line_count;
6122 }
6123 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6124 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6125 EMSG(_(e_invrange));
6126 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006127 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006128 char_u *qf_title = *eap->cmdlinep;
6129
6130 if (buf->b_sfname)
6131 {
6132 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6133 (char *)qf_title, (char *)buf->b_sfname);
6134 qf_title = IObuff;
6135 }
6136
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006137 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006138 (eap->cmdidx != CMD_caddbuffer
6139 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006140 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006141 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006142 if (res >= 0)
6143 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006144 if (au_name != NULL)
6145 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6146 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006147 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
6148 eap->cmdidx == CMD_lbuffer))
6149 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006150 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006151 }
6152}
6153
Bram Moolenaar1e015462005-09-25 22:16:38 +00006154#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006155/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006156 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6157 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006158 */
6159 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006160ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006161{
6162 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006163 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006164 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006165 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006166
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006167 switch (eap->cmdidx)
6168 {
6169 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6170 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6171 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6172 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6173 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6174 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6175 default: break;
6176 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006177 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6178 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006179 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006180#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006181 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006182 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006183#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006184 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006185
Bram Moolenaar3c097222017-12-21 20:54:49 +01006186 if (eap->cmdidx == CMD_lexpr
6187 || eap->cmdidx == CMD_lgetexpr
6188 || eap->cmdidx == CMD_laddexpr)
6189 {
6190 qi = ll_get_or_alloc_list(curwin);
6191 if (qi == NULL)
6192 return;
6193 }
6194
Bram Moolenaar4770d092006-01-12 23:22:24 +00006195 /* Evaluate the expression. When the result is a string or a list we can
6196 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006197 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006198 if (tv != NULL)
6199 {
6200 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6201 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6202 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006203 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006204 (eap->cmdidx != CMD_caddexpr
6205 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006206 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006207 NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006208 if (res >= 0)
6209 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006210 if (au_name != NULL)
6211 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6212 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006213 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
6214 eap->cmdidx == CMD_lexpr))
6215 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006216 }
6217 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006218 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006219 free_tv(tv);
6220 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006221}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006222#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006223
6224/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006225 * Get the location list for ":lhelpgrep"
6226 */
6227 static qf_info_T *
6228hgr_get_ll(int *new_ll)
6229{
6230 win_T *wp;
6231 qf_info_T *qi;
6232
6233 /* If the current window is a help window, then use it */
6234 if (bt_help(curwin->w_buffer))
6235 wp = curwin;
6236 else
6237 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006238 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006239
6240 if (wp == NULL) /* Help window not found */
6241 qi = NULL;
6242 else
6243 qi = wp->w_llist;
6244
6245 if (qi == NULL)
6246 {
6247 /* Allocate a new location list for help text matches */
6248 if ((qi = ll_new_list()) == NULL)
6249 return NULL;
6250 *new_ll = TRUE;
6251 }
6252
6253 return qi;
6254}
6255
6256/*
6257 * Search for a pattern in a help file.
6258 */
6259 static void
6260hgr_search_file(
6261 qf_info_T *qi,
6262 char_u *fname,
6263#ifdef FEAT_MBYTE
6264 vimconv_T *p_vc,
6265#endif
6266 regmatch_T *p_regmatch)
6267{
6268 FILE *fd;
6269 long lnum;
6270
6271 fd = mch_fopen((char *)fname, "r");
6272 if (fd == NULL)
6273 return;
6274
6275 lnum = 1;
6276 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6277 {
6278 char_u *line = IObuff;
6279#ifdef FEAT_MBYTE
6280 /* Convert a line if 'encoding' is not utf-8 and
6281 * the line contains a non-ASCII character. */
6282 if (p_vc->vc_type != CONV_NONE
6283 && has_non_ascii(IObuff))
6284 {
6285 line = string_convert(p_vc, IObuff, NULL);
6286 if (line == NULL)
6287 line = IObuff;
6288 }
6289#endif
6290
6291 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6292 {
6293 int l = (int)STRLEN(line);
6294
6295 /* remove trailing CR, LF, spaces, etc. */
6296 while (l > 0 && line[l - 1] <= ' ')
6297 line[--l] = NUL;
6298
6299 if (qf_add_entry(qi,
6300 qi->qf_curlist,
6301 NULL, /* dir */
6302 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006303 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006304 0,
6305 line,
6306 lnum,
6307 (int)(p_regmatch->startp[0] - line)
6308 + 1, /* col */
6309 FALSE, /* vis_col */
6310 NULL, /* search pattern */
6311 0, /* nr */
6312 1, /* type */
6313 TRUE /* valid */
6314 ) == FAIL)
6315 {
6316 got_int = TRUE;
6317#ifdef FEAT_MBYTE
6318 if (line != IObuff)
6319 vim_free(line);
6320#endif
6321 break;
6322 }
6323 }
6324#ifdef FEAT_MBYTE
6325 if (line != IObuff)
6326 vim_free(line);
6327#endif
6328 ++lnum;
6329 line_breakcheck();
6330 }
6331 fclose(fd);
6332}
6333
6334/*
6335 * Search for a pattern in all the help files in the doc directory under
6336 * the given directory.
6337 */
6338 static void
6339hgr_search_files_in_dir(
6340 qf_info_T *qi,
6341 char_u *dirname,
6342 regmatch_T *p_regmatch
6343#ifdef FEAT_MBYTE
6344 , vimconv_T *p_vc
6345#endif
6346#ifdef FEAT_MULTI_LANG
6347 , char_u *lang
6348#endif
6349 )
6350{
6351 int fcount;
6352 char_u **fnames;
6353 int fi;
6354
6355 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6356 add_pathsep(dirname);
6357 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6358 if (gen_expand_wildcards(1, &dirname, &fcount,
6359 &fnames, EW_FILE|EW_SILENT) == OK
6360 && fcount > 0)
6361 {
6362 for (fi = 0; fi < fcount && !got_int; ++fi)
6363 {
6364#ifdef FEAT_MULTI_LANG
6365 /* Skip files for a different language. */
6366 if (lang != NULL
6367 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006368 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006369 && !(STRNICMP(lang, "en", 2) == 0
6370 && STRNICMP("txt", fnames[fi]
6371 + STRLEN(fnames[fi]) - 3, 3) == 0))
6372 continue;
6373#endif
6374
6375 hgr_search_file(qi, fnames[fi],
6376#ifdef FEAT_MBYTE
6377 p_vc,
6378#endif
6379 p_regmatch);
6380 }
6381 FreeWild(fcount, fnames);
6382 }
6383}
6384
6385/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006386 * Search for a pattern in all the help files in the 'runtimepath'
6387 * and add the matches to a quickfix list.
6388 * 'arg' is the language specifier. If supplied, then only matches in the
6389 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006390 */
6391 static void
6392hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6393{
6394 char_u *p;
6395#ifdef FEAT_MULTI_LANG
6396 char_u *lang;
6397#endif
6398
6399#ifdef FEAT_MBYTE
6400 vimconv_T vc;
6401
6402 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6403 * differs. */
6404 vc.vc_type = CONV_NONE;
6405 if (!enc_utf8)
6406 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6407#endif
6408
6409#ifdef FEAT_MULTI_LANG
6410 /* Check for a specified language */
6411 lang = check_help_lang(arg);
6412#endif
6413
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006414 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006415 p = p_rtp;
6416 while (*p != NUL && !got_int)
6417 {
6418 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6419
6420 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6421#ifdef FEAT_MBYTE
6422 , &vc
6423#endif
6424#ifdef FEAT_MULTI_LANG
6425 , lang
6426#endif
6427 );
6428 }
6429
6430#ifdef FEAT_MBYTE
6431 if (vc.vc_type != CONV_NONE)
6432 convert_setup(&vc, NULL, NULL);
6433#endif
6434}
6435
6436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 * ":helpgrep {pattern}"
6438 */
6439 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006440ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
6442 regmatch_T regmatch;
6443 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006444 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006445 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006446 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447
Bram Moolenaar73633f82012-01-20 13:39:07 +01006448 switch (eap->cmdidx)
6449 {
6450 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6451 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6452 default: break;
6453 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006454 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6455 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006456 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006457#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006458 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006459 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006460#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006461 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006462
6463 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6464 save_cpo = p_cpo;
6465 p_cpo = empty_option;
6466
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006467 if (eap->cmdidx == CMD_lhelpgrep)
6468 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006469 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006470 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006471 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006472 }
6473
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6475 regmatch.rm_ic = FALSE;
6476 if (regmatch.regprog != NULL)
6477 {
6478 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006479 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006481 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006482
Bram Moolenaar473de612013-06-08 18:19:48 +02006483 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006485 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6486 qi->qf_lists[qi->qf_curlist].qf_ptr =
6487 qi->qf_lists[qi->qf_curlist].qf_start;
6488 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 }
6490
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006491 if (p_cpo == empty_option)
6492 p_cpo = save_cpo;
6493 else
6494 /* Darn, some plugin changed the value. */
6495 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
Bram Moolenaarb254af32017-12-18 19:48:58 +01006497 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006498 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
Bram Moolenaar73633f82012-01-20 13:39:07 +01006500 if (au_name != NULL)
6501 {
6502 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6503 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006504 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006505 /* autocommands made "qi" invalid */
6506 return;
6507 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006508
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006510 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006511 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006512 else
6513 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006514
6515 if (eap->cmdidx == CMD_lhelpgrep)
6516 {
6517 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006518 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006519 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006520 {
6521 if (new_qi)
6522 ll_free_all(&qi);
6523 }
6524 else if (curwin->w_llist == NULL)
6525 curwin->w_llist = qi;
6526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527}
6528
6529#endif /* FEAT_QUICKFIX */