blob: 5ff4091d599b21defc842769830e556fc7e5c63e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020050/*
51 * Quickfix/Location list definition
52 * Contains a list of entries (qfline_T). qf_start points to the first entry
53 * and qf_last points to the last entry. qf_count contains the list size.
54 *
55 * Usually the list contains one or more entries. But an empty list can be
56 * created using setqflist()/setloclist() with a title and/or user context
57 * information and entries can be added later using setqflist()/setloclist().
58 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000059typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000060{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020061 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000062 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020063 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020065 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 int qf_index; /* current index in the error list */
67 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020068 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020069 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020070 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020071
72 struct dir_stack_T *qf_dir_stack;
73 char_u *qf_directory;
74 struct dir_stack_T *qf_file_stack;
75 char_u *qf_currfile;
76 int qf_multiline;
77 int qf_multiignore;
78 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000079} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020081/*
82 * Quickfix/Location list stack definition
83 * Contains a list of quickfix/location lists (qf_list_T)
84 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000085struct qf_info_S
86{
87 /*
88 * Count of references to this list. Used only for location lists.
89 * When a location list window reference this list, qf_refcount
90 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
91 * reaches 0, the list is freed.
92 */
93 int qf_refcount;
94 int qf_listcount; /* current number of lists */
95 int qf_curlist; /* current error list */
96 qf_list_T qf_lists[LISTCOUNT];
97};
98
99static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200100static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000102#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
104/*
105 * Structure used to hold the info of one part of 'errorformat'
106 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000107typedef struct efm_S efm_T;
108struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109{
110 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000111 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
113 char_u prefix; /* prefix of this format line: */
114 /* 'D' enter directory */
115 /* 'X' leave directory */
116 /* 'A' start of multi-line message */
117 /* 'E' error message */
118 /* 'W' warning message */
119 /* 'I' informational message */
120 /* 'C' continuation line */
121 /* 'Z' end of multi-line message */
122 /* 'G' general, unspecific message */
123 /* 'P' push file (partial) message */
124 /* 'Q' pop/quit file (partial) message */
125 /* 'O' overread (partial) message */
126 char_u flags; /* additional flags given in prefix */
127 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000128 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000129 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130};
131
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100132static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
133
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200134static int qf_init_ext(qf_info_T *qi, int qf_idx, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title, char_u *enc);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200135static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void qf_new_list(qf_info_T *qi, char_u *qf_title);
137static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200138static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void qf_free(qf_info_T *qi, int idx);
141static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200142static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200143static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200145static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
147static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
150static int is_qf_win(win_T *win, qf_info_T *qi);
151static win_T *qf_find_win(qf_info_T *qi);
152static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200153static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200155static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static char_u *get_mef_name(void);
158static void restore_start_dir(char_u *dirname_start);
159static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
160static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
161static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
162static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164/* Quickfix window check helper macro */
165#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
166/* Location list window check helper macro */
167#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
168/*
169 * Return location list for window 'wp'
170 * For location list window, return the referenced location list
171 */
172#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
173
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200175 * Looking up a buffer can be slow if there are many. Remember the last one
176 * to make this a lot faster if there are multiple matches in the same file.
177 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200178static char_u *qf_last_bufname = NULL;
179static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200180
181/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200183 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 * Return -1 for error, number of errors for success.
185 */
186 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200187qf_init(win_T *wp,
188 char_u *efile,
189 char_u *errorformat,
190 int newlist, /* TRUE: start a new error list */
191 char_u *qf_title,
192 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000194 qf_info_T *qi = &ql_info;
195
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000196 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000197 {
198 qi = ll_get_or_alloc_list(wp);
199 if (qi == NULL)
200 return FAIL;
201 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000202
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200203 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
204 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000205}
206
207/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200208 * Maximum number of bytes allowed per line while reading a errorfile.
209 */
210#define LINE_MAXLEN 4096
211
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200212static struct fmtpattern
213{
214 char_u convchar;
215 char *pattern;
216} fmt_pat[FMT_PATTERNS] =
217 {
218 {'f', ".\\+"}, /* only used when at end */
219 {'n', "\\d\\+"},
220 {'l', "\\d\\+"},
221 {'c', "\\d\\+"},
222 {'t', "."},
223 {'m', ".\\+"},
224 {'r', ".*"},
225 {'p', "[- .]*"},
226 {'v', "\\d\\+"},
227 {'s', ".\\+"}
228 };
229
230/*
231 * Converts a 'errorformat' string to regular expression pattern
232 */
233 static int
234efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200235 char_u *efm,
236 int len,
237 efm_T *fmt_ptr,
238 char_u *regpat,
239 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200240{
241 char_u *ptr;
242 char_u *efmp;
243 char_u *srcptr;
244 int round;
245 int idx = 0;
246
247 /*
248 * Build regexp pattern from current 'errorformat' option
249 */
250 ptr = regpat;
251 *ptr++ = '^';
252 round = 0;
253 for (efmp = efm; efmp < efm + len; ++efmp)
254 {
255 if (*efmp == '%')
256 {
257 ++efmp;
258 for (idx = 0; idx < FMT_PATTERNS; ++idx)
259 if (fmt_pat[idx].convchar == *efmp)
260 break;
261 if (idx < FMT_PATTERNS)
262 {
263 if (fmt_ptr->addr[idx])
264 {
265 sprintf((char *)errmsg,
266 _("E372: Too many %%%c in format string"), *efmp);
267 EMSG(errmsg);
268 return -1;
269 }
270 if ((idx
271 && idx < 6
272 && vim_strchr((char_u *)"DXOPQ",
273 fmt_ptr->prefix) != NULL)
274 || (idx == 6
275 && vim_strchr((char_u *)"OPQ",
276 fmt_ptr->prefix) == NULL))
277 {
278 sprintf((char *)errmsg,
279 _("E373: Unexpected %%%c in format string"), *efmp);
280 EMSG(errmsg);
281 return -1;
282 }
283 fmt_ptr->addr[idx] = (char_u)++round;
284 *ptr++ = '\\';
285 *ptr++ = '(';
286#ifdef BACKSLASH_IN_FILENAME
287 if (*efmp == 'f')
288 {
289 /* Also match "c:" in the file name, even when
290 * checking for a colon next: "%f:".
291 * "\%(\a:\)\=" */
292 STRCPY(ptr, "\\%(\\a:\\)\\=");
293 ptr += 10;
294 }
295#endif
296 if (*efmp == 'f' && efmp[1] != NUL)
297 {
298 if (efmp[1] != '\\' && efmp[1] != '%')
299 {
300 /* A file name may contain spaces, but this isn't
301 * in "\f". For "%f:%l:%m" there may be a ":" in
302 * the file name. Use ".\{-1,}x" instead (x is
303 * the next character), the requirement that :999:
304 * follows should work. */
305 STRCPY(ptr, ".\\{-1,}");
306 ptr += 7;
307 }
308 else
309 {
310 /* File name followed by '\\' or '%': include as
311 * many file name chars as possible. */
312 STRCPY(ptr, "\\f\\+");
313 ptr += 4;
314 }
315 }
316 else
317 {
318 srcptr = (char_u *)fmt_pat[idx].pattern;
319 while ((*ptr = *srcptr++) != NUL)
320 ++ptr;
321 }
322 *ptr++ = '\\';
323 *ptr++ = ')';
324 }
325 else if (*efmp == '*')
326 {
327 if (*++efmp == '[' || *efmp == '\\')
328 {
329 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
330 {
331 if (efmp[1] == '^')
332 *ptr++ = *++efmp;
333 if (efmp < efm + len)
334 {
335 *ptr++ = *++efmp; /* could be ']' */
336 while (efmp < efm + len
337 && (*ptr++ = *++efmp) != ']')
338 /* skip */;
339 if (efmp == efm + len)
340 {
341 EMSG(_("E374: Missing ] in format string"));
342 return -1;
343 }
344 }
345 }
346 else if (efmp < efm + len) /* %*\D, %*\s etc. */
347 *ptr++ = *++efmp;
348 *ptr++ = '\\';
349 *ptr++ = '+';
350 }
351 else
352 {
353 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
354 sprintf((char *)errmsg,
355 _("E375: Unsupported %%%c in format string"), *efmp);
356 EMSG(errmsg);
357 return -1;
358 }
359 }
360 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
361 *ptr++ = *efmp; /* regexp magic characters */
362 else if (*efmp == '#')
363 *ptr++ = '*';
364 else if (*efmp == '>')
365 fmt_ptr->conthere = TRUE;
366 else if (efmp == efm + 1) /* analyse prefix */
367 {
368 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
369 fmt_ptr->flags = *efmp++;
370 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
371 fmt_ptr->prefix = *efmp;
372 else
373 {
374 sprintf((char *)errmsg,
375 _("E376: Invalid %%%c in format string prefix"), *efmp);
376 EMSG(errmsg);
377 return -1;
378 }
379 }
380 else
381 {
382 sprintf((char *)errmsg,
383 _("E377: Invalid %%%c in format string"), *efmp);
384 EMSG(errmsg);
385 return -1;
386 }
387 }
388 else /* copy normal character */
389 {
390 if (*efmp == '\\' && efmp + 1 < efm + len)
391 ++efmp;
392 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
393 *ptr++ = '\\'; /* escape regexp atoms */
394 if (*efmp)
395 *ptr++ = *efmp;
396 }
397 }
398 *ptr++ = '$';
399 *ptr = NUL;
400
401 return 0;
402}
403
404 static void
405free_efm_list(efm_T **efm_first)
406{
407 efm_T *efm_ptr;
408
409 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
410 {
411 *efm_first = efm_ptr->next;
412 vim_regfree(efm_ptr->prog);
413 vim_free(efm_ptr);
414 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100415 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200416}
417
418/* Parse 'errorformat' option */
419 static efm_T *
420parse_efm_option(char_u *efm)
421{
422 char_u *errmsg = NULL;
423 int errmsglen;
424 efm_T *fmt_ptr = NULL;
425 efm_T *fmt_first = NULL;
426 efm_T *fmt_last = NULL;
427 char_u *fmtstr = NULL;
428 int len;
429 int i;
430 int round;
431
432 errmsglen = CMDBUFFSIZE + 1;
433 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
434 if (errmsg == NULL)
435 goto parse_efm_end;
436
437 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200438 * Each part of the format string is copied and modified from errorformat
439 * to regex prog. Only a few % characters are allowed.
440 */
441
442 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 * Get some space to modify the format string into.
444 */
445 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
446 for (round = FMT_PATTERNS; round > 0; )
447 i += (int)STRLEN(fmt_pat[--round].pattern);
448#ifdef COLON_IN_FILENAME
449 i += 12; /* "%f" can become twelve chars longer */
450#else
451 i += 2; /* "%f" can become two chars longer */
452#endif
453 if ((fmtstr = alloc(i)) == NULL)
454 goto parse_efm_error;
455
456 while (efm[0] != NUL)
457 {
458 /*
459 * Allocate a new eformat structure and put it at the end of the list
460 */
461 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
462 if (fmt_ptr == NULL)
463 goto parse_efm_error;
464 if (fmt_first == NULL) /* first one */
465 fmt_first = fmt_ptr;
466 else
467 fmt_last->next = fmt_ptr;
468 fmt_last = fmt_ptr;
469
470 /*
471 * Isolate one part in the 'errorformat' option
472 */
473 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
474 if (efm[len] == '\\' && efm[len + 1] != NUL)
475 ++len;
476
477 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
478 goto parse_efm_error;
479 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
480 goto parse_efm_error;
481 /*
482 * Advance to next part
483 */
484 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
485 }
486
487 if (fmt_first == NULL) /* nothing found */
488 EMSG(_("E378: 'errorformat' contains no pattern"));
489
490 goto parse_efm_end;
491
492parse_efm_error:
493 free_efm_list(&fmt_first);
494
495parse_efm_end:
496 vim_free(fmtstr);
497 vim_free(errmsg);
498
499 return fmt_first;
500}
501
Bram Moolenaare0d37972016-07-15 22:36:01 +0200502enum {
503 QF_FAIL = 0,
504 QF_OK = 1,
505 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200506 QF_NOMEM = 3,
507 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200508};
509
510typedef struct {
511 char_u *linebuf;
512 int linelen;
513 char_u *growbuf;
514 int growbufsiz;
515 FILE *fd;
516 typval_T *tv;
517 char_u *p_str;
518 listitem_T *p_li;
519 buf_T *buf;
520 linenr_T buflnum;
521 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100522 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200523} qfstate_T;
524
525 static char_u *
526qf_grow_linebuf(qfstate_T *state, int newsz)
527{
528 /*
529 * If the line exceeds LINE_MAXLEN exclude the last
530 * byte since it's not a NL character.
531 */
532 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
533 if (state->growbuf == NULL)
534 {
535 state->growbuf = alloc(state->linelen + 1);
536 if (state->growbuf == NULL)
537 return NULL;
538 state->growbufsiz = state->linelen;
539 }
540 else if (state->linelen > state->growbufsiz)
541 {
542 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
543 if (state->growbuf == NULL)
544 return NULL;
545 state->growbufsiz = state->linelen;
546 }
547 return state->growbuf;
548}
549
550/*
551 * Get the next string (separated by newline) from state->p_str.
552 */
553 static int
554qf_get_next_str_line(qfstate_T *state)
555{
556 /* Get the next line from the supplied string */
557 char_u *p_str = state->p_str;
558 char_u *p;
559 int len;
560
561 if (*p_str == NUL) /* Reached the end of the string */
562 return QF_END_OF_INPUT;
563
564 p = vim_strchr(p_str, '\n');
565 if (p != NULL)
566 len = (int)(p - p_str) + 1;
567 else
568 len = (int)STRLEN(p_str);
569
570 if (len > IOSIZE - 2)
571 {
572 state->linebuf = qf_grow_linebuf(state, len);
573 if (state->linebuf == NULL)
574 return QF_NOMEM;
575 }
576 else
577 {
578 state->linebuf = IObuff;
579 state->linelen = len;
580 }
581 vim_strncpy(state->linebuf, p_str, state->linelen);
582
583 /*
584 * Increment using len in order to discard the rest of the
585 * line if it exceeds LINE_MAXLEN.
586 */
587 p_str += len;
588 state->p_str = p_str;
589
590 return QF_OK;
591}
592
593/*
594 * Get the next string from state->p_Li.
595 */
596 static int
597qf_get_next_list_line(qfstate_T *state)
598{
599 listitem_T *p_li = state->p_li;
600 int len;
601
602 while (p_li != NULL
603 && (p_li->li_tv.v_type != VAR_STRING
604 || p_li->li_tv.vval.v_string == NULL))
605 p_li = p_li->li_next; /* Skip non-string items */
606
607 if (p_li == NULL) /* End of the list */
608 {
609 state->p_li = NULL;
610 return QF_END_OF_INPUT;
611 }
612
613 len = (int)STRLEN(p_li->li_tv.vval.v_string);
614 if (len > IOSIZE - 2)
615 {
616 state->linebuf = qf_grow_linebuf(state, len);
617 if (state->linebuf == NULL)
618 return QF_NOMEM;
619 }
620 else
621 {
622 state->linebuf = IObuff;
623 state->linelen = len;
624 }
625
626 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
627
628 state->p_li = p_li->li_next; /* next item */
629 return QF_OK;
630}
631
632/*
633 * Get the next string from state->buf.
634 */
635 static int
636qf_get_next_buf_line(qfstate_T *state)
637{
638 char_u *p_buf = NULL;
639 int len;
640
641 /* Get the next line from the supplied buffer */
642 if (state->buflnum > state->lnumlast)
643 return QF_END_OF_INPUT;
644
645 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
646 state->buflnum += 1;
647
648 len = (int)STRLEN(p_buf);
649 if (len > IOSIZE - 2)
650 {
651 state->linebuf = qf_grow_linebuf(state, len);
652 if (state->linebuf == NULL)
653 return QF_NOMEM;
654 }
655 else
656 {
657 state->linebuf = IObuff;
658 state->linelen = len;
659 }
660 vim_strncpy(state->linebuf, p_buf, state->linelen);
661
662 return QF_OK;
663}
664
665/*
666 * Get the next string from file state->fd.
667 */
668 static int
669qf_get_next_file_line(qfstate_T *state)
670{
671 int discard;
672 int growbuflen;
673
674 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
675 return QF_END_OF_INPUT;
676
677 discard = FALSE;
678 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200679 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200680 {
681 /*
682 * The current line exceeds IObuff, continue reading using
683 * growbuf until EOL or LINE_MAXLEN bytes is read.
684 */
685 if (state->growbuf == NULL)
686 {
687 state->growbufsiz = 2 * (IOSIZE - 1);
688 state->growbuf = alloc(state->growbufsiz);
689 if (state->growbuf == NULL)
690 return QF_NOMEM;
691 }
692
693 /* Copy the read part of the line, excluding null-terminator */
694 memcpy(state->growbuf, IObuff, IOSIZE - 1);
695 growbuflen = state->linelen;
696
697 for (;;)
698 {
699 if (fgets((char *)state->growbuf + growbuflen,
700 state->growbufsiz - growbuflen, state->fd) == NULL)
701 break;
702 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
703 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200704 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200705 break;
706 if (state->growbufsiz == LINE_MAXLEN)
707 {
708 discard = TRUE;
709 break;
710 }
711
712 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
713 ? 2 * state->growbufsiz : LINE_MAXLEN;
714 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
715 if (state->growbuf == NULL)
716 return QF_NOMEM;
717 }
718
719 while (discard)
720 {
721 /*
722 * The current line is longer than LINE_MAXLEN, continue
723 * reading but discard everything until EOL or EOF is
724 * reached.
725 */
726 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
727 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200728 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200729 break;
730 }
731
732 state->linebuf = state->growbuf;
733 state->linelen = growbuflen;
734 }
735 else
736 state->linebuf = IObuff;
737
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100738#ifdef FEAT_MBYTE
739 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200740 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
741 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100742 char_u *line;
743
744 line = string_convert(&state->vc, state->linebuf, &state->linelen);
745 if (line != NULL)
746 {
747 if (state->linelen < IOSIZE)
748 {
749 STRCPY(state->linebuf, line);
750 vim_free(line);
751 }
752 else
753 {
754 vim_free(state->growbuf);
755 state->linebuf = state->growbuf = line;
756 state->growbufsiz = state->linelen < LINE_MAXLEN
757 ? state->linelen : LINE_MAXLEN;
758 }
759 }
760 }
761#endif
762
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 return QF_OK;
764}
765
766/*
767 * Get the next string from a file/buffer/list/string.
768 */
769 static int
770qf_get_nextline(qfstate_T *state)
771{
772 int status = QF_FAIL;
773
774 if (state->fd == NULL)
775 {
776 if (state->tv != NULL)
777 {
778 if (state->tv->v_type == VAR_STRING)
779 /* Get the next line from the supplied string */
780 status = qf_get_next_str_line(state);
781 else if (state->tv->v_type == VAR_LIST)
782 /* Get the next line from the supplied list */
783 status = qf_get_next_list_line(state);
784 }
785 else
786 /* Get the next line from the supplied buffer */
787 status = qf_get_next_buf_line(state);
788 }
789 else
790 /* Get the next line from the supplied file */
791 status = qf_get_next_file_line(state);
792
793 if (status != QF_OK)
794 return status;
795
796 /* remove newline/CR from the line */
797 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200798 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799 state->linebuf[state->linelen - 1] = NUL;
800#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200801 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
802 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200803#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200804 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200805
806#ifdef FEAT_MBYTE
807 remove_bom(state->linebuf);
808#endif
809
810 return QF_OK;
811}
812
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200813typedef struct {
814 char_u *namebuf;
815 char_u *errmsg;
816 int errmsglen;
817 long lnum;
818 int col;
819 char_u use_viscol;
820 char_u *pattern;
821 int enr;
822 int type;
823 int valid;
824} qffields_T;
825
826/*
827 * Parse a line and get the quickfix fields.
828 * Return the QF_ status.
829 */
830 static int
831qf_parse_line(
832 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200833 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200834 char_u *linebuf,
835 int linelen,
836 efm_T *fmt_first,
837 qffields_T *fields)
838{
839 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200840 char_u *ptr;
841 int len;
842 int i;
843 int idx = 0;
844 char_u *tail = NULL;
845 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200846 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200847
848 /* Always ignore case when looking for a matching error. */
849 regmatch.rm_ic = TRUE;
850
851 /* If there was no %> item start at the first pattern */
852 if (fmt_start == NULL)
853 fmt_ptr = fmt_first;
854 else
855 {
856 fmt_ptr = fmt_start;
857 fmt_start = NULL;
858 }
859
860 /*
861 * Try to match each part of 'errorformat' until we find a complete
862 * match or no match.
863 */
864 fields->valid = TRUE;
865restofline:
866 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
867 {
868 int r;
869
870 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200871 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200872 continue;
873 fields->namebuf[0] = NUL;
874 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200875 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200876 fields->errmsg[0] = NUL;
877 fields->lnum = 0;
878 fields->col = 0;
879 fields->use_viscol = FALSE;
880 fields->enr = -1;
881 fields->type = 0;
882 tail = NULL;
883
884 regmatch.regprog = fmt_ptr->prog;
885 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
886 fmt_ptr->prog = regmatch.regprog;
887 if (r)
888 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200889 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200890 continue;
891 if (vim_strchr((char_u *)"EWI", idx) != NULL)
892 fields->type = idx;
893 else
894 fields->type = 0;
895 /*
896 * Extract error message data from matched line.
897 * We check for an actual submatch, because "\[" and "\]" in
898 * the 'errorformat' may cause the wrong submatch to be used.
899 */
900 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
901 {
902 int c;
903
904 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
905 continue;
906
907 /* Expand ~/file and $HOME/file to full path. */
908 c = *regmatch.endp[i];
909 *regmatch.endp[i] = NUL;
910 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
911 *regmatch.endp[i] = c;
912
913 if (vim_strchr((char_u *)"OPQ", idx) != NULL
914 && mch_getperm(fields->namebuf) == -1)
915 continue;
916 }
917 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
918 {
919 if (regmatch.startp[i] == NULL)
920 continue;
921 fields->enr = (int)atol((char *)regmatch.startp[i]);
922 }
923 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
924 {
925 if (regmatch.startp[i] == NULL)
926 continue;
927 fields->lnum = atol((char *)regmatch.startp[i]);
928 }
929 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
930 {
931 if (regmatch.startp[i] == NULL)
932 continue;
933 fields->col = (int)atol((char *)regmatch.startp[i]);
934 }
935 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
936 {
937 if (regmatch.startp[i] == NULL)
938 continue;
939 fields->type = *regmatch.startp[i];
940 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200941 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200942 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200943 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200944 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200945 /* linelen + null terminator */
946 if ((fields->errmsg = vim_realloc(fields->errmsg,
947 linelen + 1)) == NULL)
948 return QF_NOMEM;
949 fields->errmsglen = linelen + 1;
950 }
951 vim_strncpy(fields->errmsg, linebuf, linelen);
952 }
953 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
954 {
955 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
956 continue;
957 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200958 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200959 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200960 /* len + null terminator */
961 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
962 == NULL)
963 return QF_NOMEM;
964 fields->errmsglen = len + 1;
965 }
966 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
967 }
968 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
969 {
970 if (regmatch.startp[i] == NULL)
971 continue;
972 tail = regmatch.startp[i];
973 }
974 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
975 {
976 char_u *match_ptr;
977
978 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
979 continue;
980 fields->col = 0;
981 for (match_ptr = regmatch.startp[i];
982 match_ptr != regmatch.endp[i]; ++match_ptr)
983 {
984 ++fields->col;
985 if (*match_ptr == TAB)
986 {
987 fields->col += 7;
988 fields->col -= fields->col % 8;
989 }
990 }
991 ++fields->col;
992 fields->use_viscol = TRUE;
993 }
994 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
995 {
996 if (regmatch.startp[i] == NULL)
997 continue;
998 fields->col = (int)atol((char *)regmatch.startp[i]);
999 fields->use_viscol = TRUE;
1000 }
1001 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1002 {
1003 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1004 continue;
1005 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1006 if (len > CMDBUFFSIZE - 5)
1007 len = CMDBUFFSIZE - 5;
1008 STRCPY(fields->pattern, "^\\V");
1009 STRNCAT(fields->pattern, regmatch.startp[i], len);
1010 fields->pattern[len + 3] = '\\';
1011 fields->pattern[len + 4] = '$';
1012 fields->pattern[len + 5] = NUL;
1013 }
1014 break;
1015 }
1016 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001017 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001018
1019 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1020 {
1021 if (fmt_ptr != NULL)
1022 {
1023 if (idx == 'D') /* enter directory */
1024 {
1025 if (*fields->namebuf == NUL)
1026 {
1027 EMSG(_("E379: Missing or empty directory name"));
1028 return QF_FAIL;
1029 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001030 qfl->qf_directory =
1031 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1032 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001033 return QF_FAIL;
1034 }
1035 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001036 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001037 }
1038 fields->namebuf[0] = NUL; /* no match found, remove file name */
1039 fields->lnum = 0; /* don't jump to this line */
1040 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001041 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001042 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001043 /* linelen + null terminator */
1044 if ((fields->errmsg = vim_realloc(fields->errmsg,
1045 linelen + 1)) == NULL)
1046 return QF_NOMEM;
1047 fields->errmsglen = linelen + 1;
1048 }
1049 /* copy whole line to error message */
1050 vim_strncpy(fields->errmsg, linebuf, linelen);
1051 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001052 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001053 }
1054 else if (fmt_ptr != NULL)
1055 {
1056 /* honor %> item */
1057 if (fmt_ptr->conthere)
1058 fmt_start = fmt_ptr;
1059
1060 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1061 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001062 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1063 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001064 }
1065 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1066 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001067 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001068 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001069 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001070
Bram Moolenaar9b457942016-10-09 16:10:05 +02001071 if (qfprev == NULL)
1072 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001073 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001074 {
1075 len = (int)STRLEN(qfprev->qf_text);
1076 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1077 == NULL)
1078 return QF_FAIL;
1079 STRCPY(ptr, qfprev->qf_text);
1080 vim_free(qfprev->qf_text);
1081 qfprev->qf_text = ptr;
1082 *(ptr += len) = '\n';
1083 STRCPY(++ptr, fields->errmsg);
1084 }
1085 if (qfprev->qf_nr == -1)
1086 qfprev->qf_nr = fields->enr;
1087 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1088 /* only printable chars allowed */
1089 qfprev->qf_type = fields->type;
1090
1091 if (!qfprev->qf_lnum)
1092 qfprev->qf_lnum = fields->lnum;
1093 if (!qfprev->qf_col)
1094 qfprev->qf_col = fields->col;
1095 qfprev->qf_viscol = fields->use_viscol;
1096 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001097 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1098 qfl->qf_directory,
1099 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001100 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001101 : qfl->qf_currfile != NULL && fields->valid
1102 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001103 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001104 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001105 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001106 line_breakcheck();
1107 return QF_IGNORE_LINE;
1108 }
1109 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1110 {
1111 /* global file names */
1112 fields->valid = FALSE;
1113 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1114 {
1115 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001116 qfl->qf_currfile =
1117 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001118 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001119 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001120 *fields->namebuf = NUL;
1121 if (tail && *tail)
1122 {
1123 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001124 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001125 goto restofline;
1126 }
1127 }
1128 }
1129 if (fmt_ptr->flags == '-') /* generally exclude this line */
1130 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001131 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001132 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001133 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001134 return QF_IGNORE_LINE;
1135 }
1136 }
1137
1138 return QF_OK;
1139}
1140
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001141/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001142 * Read the errorfile "efile" into memory, line by line, building the error
1143 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001144 * Alternative: when "efile" is NULL read errors from buffer "buf".
1145 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001146 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001147 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1148 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001149 * Return -1 for error, number of errors for success.
1150 */
1151 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001152qf_init_ext(
1153 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001154 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001155 char_u *efile,
1156 buf_T *buf,
1157 typval_T *tv,
1158 char_u *errorformat,
1159 int newlist, /* TRUE: start a new error list */
1160 linenr_T lnumfirst, /* first line number to use */
1161 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001162 char_u *qf_title,
1163 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001164{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001165 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001166 qfstate_T state;
1167 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001168#ifdef FEAT_WINDOWS
1169 qfline_T *old_last = NULL;
1170#endif
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001171 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001172 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001174 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001176 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001178 /* Do not used the cached buffer, it may have been wiped out. */
1179 vim_free(qf_last_bufname);
1180 qf_last_bufname = NULL;
1181
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001182 vim_memset(&state, 0, sizeof(state));
1183 vim_memset(&fields, 0, sizeof(fields));
1184#ifdef FEAT_MBYTE
1185 state.vc.vc_type = CONV_NONE;
1186 if (enc != NULL && *enc != NUL)
1187 convert_setup(&state.vc, enc, p_enc);
1188#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001189 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1190 fields.errmsglen = CMDBUFFSIZE + 1;
1191 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1192 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001193 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 goto qf_init_end;
1195
Bram Moolenaare0d37972016-07-15 22:36:01 +02001196 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
1198 EMSG2(_(e_openerrf), efile);
1199 goto qf_init_end;
1200 }
1201
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001202 if (newlist || qf_idx == qi->qf_listcount)
1203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001205 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001206 qf_idx = qi->qf_curlist;
1207 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001208 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001209 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001210 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001211 adding = TRUE;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001212#ifdef FEAT_WINDOWS
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001213 if (qi->qf_lists[qf_idx].qf_count > 0)
1214 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001215#endif
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001218 qfl = &qi->qf_lists[qf_idx];
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001221 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001222 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 else
1224 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001226 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001227 * If the errorformat didn't change between calls, then reuse the
1228 * previously parsed values.
1229 */
1230 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1231 {
1232 /* free the previously parsed data */
1233 vim_free(last_efm);
1234 last_efm = NULL;
1235 free_efm_list(&fmt_first);
1236
1237 /* parse the current 'efm' */
1238 fmt_first = parse_efm_option(efm);
1239 if (fmt_first != NULL)
1240 last_efm = vim_strsave(efm);
1241 }
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
1246 /*
1247 * got_int is reset here, because it was probably set when killing the
1248 * ":make" command, but we still want to read the errorfile then.
1249 */
1250 got_int = FALSE;
1251
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001252 if (tv != NULL)
1253 {
1254 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001255 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001256 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001257 state.p_li = tv->vval.v_list->lv_first;
1258 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001259 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001260 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001261 state.buflnum = lnumfirst;
1262 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /*
1265 * Read the lines in the error file one by one.
1266 * Try to recognize one of the error formats in each line.
1267 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001268 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001270 /* Get the next line from a file/buffer/list/string */
1271 status = qf_get_nextline(&state);
1272 if (status == QF_NOMEM) /* memory alloc failure */
1273 goto qf_init_end;
1274 if (status == QF_END_OF_INPUT) /* end of input */
1275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001277 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1278 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001279 if (status == QF_FAIL)
1280 goto error2;
1281 if (status == QF_NOMEM)
1282 goto qf_init_end;
1283 if (status == QF_IGNORE_LINE)
1284 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001286 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001287 qf_idx,
1288 qfl->qf_directory,
1289 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001290 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001291 : ((qfl->qf_currfile != NULL && fields.valid)
1292 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001293 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001294 fields.errmsg,
1295 fields.lnum,
1296 fields.col,
1297 fields.use_viscol,
1298 fields.pattern,
1299 fields.enr,
1300 fields.type,
1301 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 goto error2;
1303 line_breakcheck();
1304 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001305 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001307 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001309 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001310 qfl->qf_ptr = qfl->qf_start;
1311 qfl->qf_index = 1;
1312 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 else
1315 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001316 qfl->qf_nonevalid = FALSE;
1317 if (qfl->qf_ptr == NULL)
1318 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001320 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001321 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001322 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324 EMSG(_(e_readerrf));
1325error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001326 if (!adding)
1327 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001328 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001329 qf_free(qi, qi->qf_curlist);
1330 qi->qf_listcount--;
1331 if (qi->qf_curlist > 0)
1332 --qi->qf_curlist;
1333 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001334qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001335 if (state.fd != NULL)
1336 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001337 vim_free(fields.namebuf);
1338 vim_free(fields.errmsg);
1339 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001340 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342#ifdef FEAT_WINDOWS
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001343 if (qf_idx == qi->qf_curlist)
1344 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001346#ifdef FEAT_MBYTE
1347 if (state.vc.vc_type != CONV_NONE)
1348 convert_setup(&state.vc, NULL, NULL);
1349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
1351 return retval;
1352}
1353
Bram Moolenaarfb604092014-07-23 15:55:00 +02001354 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001355qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001356{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001357 vim_free(qi->qf_lists[qf_idx].qf_title);
1358 qi->qf_lists[qf_idx].qf_title = NULL;
1359
Bram Moolenaarfb604092014-07-23 15:55:00 +02001360 if (title != NULL)
1361 {
1362 char_u *p = alloc((int)STRLEN(title) + 2);
1363
Bram Moolenaara3921f42017-06-04 15:30:34 +02001364 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001365 if (p != NULL)
1366 sprintf((char *)p, ":%s", (char *)title);
1367 }
1368}
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001371 * Prepare for adding a new quickfix list. If the current list is in the
1372 * middle of the stack, then all the following lists are freed and then
1373 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 */
1375 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001376qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 int i;
1379
1380 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001381 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 * the current entry. This makes it possible to browse in a tree-like
1383 * way with ":grep'.
1384 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001385 while (qi->qf_listcount > qi->qf_curlist + 1)
1386 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
1388 /*
1389 * When the stack is full, remove to oldest entry
1390 * Otherwise, add a new entry.
1391 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001392 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001394 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001396 qi->qf_lists[i - 1] = qi->qf_lists[i];
1397 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001400 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001401 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001402 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001403 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404}
1405
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001406/*
1407 * Free a location list
1408 */
1409 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001410ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001411{
1412 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001413 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001414
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001415 qi = *pqi;
1416 if (qi == NULL)
1417 return;
1418 *pqi = NULL; /* Remove reference to this list */
1419
1420 qi->qf_refcount--;
1421 if (qi->qf_refcount < 1)
1422 {
1423 /* No references to this location list */
1424 for (i = 0; i < qi->qf_listcount; ++i)
1425 qf_free(qi, i);
1426 vim_free(qi);
1427 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001428}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001429
1430 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001431qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001432{
1433 int i;
1434 qf_info_T *qi = &ql_info;
1435
1436 if (wp != NULL)
1437 {
1438 /* location list */
1439 ll_free_all(&wp->w_llist);
1440 ll_free_all(&wp->w_llist_ref);
1441 }
1442 else
1443 /* quickfix list */
1444 for (i = 0; i < qi->qf_listcount; ++i)
1445 qf_free(qi, i);
1446}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001447
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448/*
1449 * Add an entry to the end of the list of errors.
1450 * Returns OK or FAIL.
1451 */
1452 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001453qf_add_entry(
1454 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001455 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001456 char_u *dir, /* optional directory name */
1457 char_u *fname, /* file name or NULL */
1458 int bufnum, /* buffer number or zero */
1459 char_u *mesg, /* message */
1460 long lnum, /* line number */
1461 int col, /* column */
1462 int vis_col, /* using visual column */
1463 char_u *pattern, /* search pattern */
1464 int nr, /* error number */
1465 int type, /* type character */
1466 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001468 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001469 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001471 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001473 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001474 {
1475 buf_T *buf = buflist_findnr(bufnum);
1476
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001477 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001478 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001479 buf->b_has_qf_entry |=
1480 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001481 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001482 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001483 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1485 {
1486 vim_free(qfp);
1487 return FAIL;
1488 }
1489 qfp->qf_lnum = lnum;
1490 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001491 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001492 if (pattern == NULL || *pattern == NUL)
1493 qfp->qf_pattern = NULL;
1494 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1495 {
1496 vim_free(qfp->qf_text);
1497 vim_free(qfp);
1498 return FAIL;
1499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 qfp->qf_nr = nr;
1501 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1502 type = 0;
1503 qfp->qf_type = type;
1504 qfp->qf_valid = valid;
1505
Bram Moolenaara3921f42017-06-04 15:30:34 +02001506 lastp = &qi->qf_lists[qf_idx].qf_last;
1507 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001508 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001510 qi->qf_lists[qf_idx].qf_start = qfp;
1511 qi->qf_lists[qf_idx].qf_ptr = qfp;
1512 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001513 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 }
1515 else
1516 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001517 qfp->qf_prev = *lastp;
1518 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001520 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001522 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001523 ++qi->qf_lists[qf_idx].qf_count;
1524 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001525 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001527 qi->qf_lists[qf_idx].qf_index =
1528 qi->qf_lists[qf_idx].qf_count;
1529 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 }
1531
1532 return OK;
1533}
1534
1535/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001536 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001537 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001538 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001539ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001540{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001541 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001542
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001543 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1544 if (qi != NULL)
1545 {
1546 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1547 qi->qf_refcount++;
1548 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001549
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001550 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001551}
1552
1553/*
1554 * Return the location list for window 'wp'.
1555 * If not present, allocate a location list
1556 */
1557 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001558ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001559{
1560 if (IS_LL_WINDOW(wp))
1561 /* For a location list window, use the referenced location list */
1562 return wp->w_llist_ref;
1563
1564 /*
1565 * For a non-location list window, w_llist_ref should not point to a
1566 * location list.
1567 */
1568 ll_free_all(&wp->w_llist_ref);
1569
1570 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001571 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001572 return wp->w_llist;
1573}
1574
1575/*
1576 * Copy the location list from window "from" to window "to".
1577 */
1578 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001579copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001580{
1581 qf_info_T *qi;
1582 int idx;
1583 int i;
1584
1585 /*
1586 * When copying from a location list window, copy the referenced
1587 * location list. For other windows, copy the location list for
1588 * that window.
1589 */
1590 if (IS_LL_WINDOW(from))
1591 qi = from->w_llist_ref;
1592 else
1593 qi = from->w_llist;
1594
1595 if (qi == NULL) /* no location list to copy */
1596 return;
1597
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001598 /* allocate a new location list */
1599 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001600 return;
1601
1602 to->w_llist->qf_listcount = qi->qf_listcount;
1603
1604 /* Copy the location lists one at a time */
1605 for (idx = 0; idx < qi->qf_listcount; idx++)
1606 {
1607 qf_list_T *from_qfl;
1608 qf_list_T *to_qfl;
1609
1610 to->w_llist->qf_curlist = idx;
1611
1612 from_qfl = &qi->qf_lists[idx];
1613 to_qfl = &to->w_llist->qf_lists[idx];
1614
1615 /* Some of the fields are populated by qf_add_entry() */
1616 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1617 to_qfl->qf_count = 0;
1618 to_qfl->qf_index = 0;
1619 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001620 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001621 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001622 if (from_qfl->qf_title != NULL)
1623 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1624 else
1625 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001626 if (from_qfl->qf_ctx != NULL)
1627 {
1628 to_qfl->qf_ctx = alloc_tv();
1629 if (to_qfl->qf_ctx != NULL)
1630 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1631 }
1632 else
1633 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001634
1635 if (from_qfl->qf_count)
1636 {
1637 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001638 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001639
1640 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001641 for (i = 0, from_qfp = from_qfl->qf_start;
1642 i < from_qfl->qf_count && from_qfp != NULL;
1643 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001645 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001646 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001647 NULL,
1648 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001649 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001650 from_qfp->qf_text,
1651 from_qfp->qf_lnum,
1652 from_qfp->qf_col,
1653 from_qfp->qf_viscol,
1654 from_qfp->qf_pattern,
1655 from_qfp->qf_nr,
1656 0,
1657 from_qfp->qf_valid) == FAIL)
1658 {
1659 qf_free_all(to);
1660 return;
1661 }
1662 /*
1663 * qf_add_entry() will not set the qf_num field, as the
1664 * directory and file names are not supplied. So the qf_fnum
1665 * field is copied here.
1666 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001667 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001668 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1669 prevp->qf_type = from_qfp->qf_type; /* error type */
1670 if (from_qfl->qf_ptr == from_qfp)
1671 to_qfl->qf_ptr = prevp; /* current location */
1672 }
1673 }
1674
1675 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1676
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001677 /* Assign a new ID for the location list */
1678 to_qfl->qf_id = ++last_qf_id;
1679
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001680 /* When no valid entries are present in the list, qf_ptr points to
1681 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001682 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001683 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001684 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001685 to_qfl->qf_index = 1;
1686 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001687 }
1688
1689 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1690}
1691
1692/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001693 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001694 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 */
1696 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001697qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
Bram Moolenaar82404332016-07-10 17:00:38 +02001699 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001700 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001701 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (fname == NULL || *fname == NUL) /* no file name */
1704 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaare60acc12011-05-10 16:41:25 +02001706#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001707 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001708#endif
1709#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001710 if (directory != NULL)
1711 slash_adjust(directory);
1712 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001713#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001714 if (directory != NULL && !vim_isAbsName(fname)
1715 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1716 {
1717 /*
1718 * Here we check if the file really exists.
1719 * This should normally be true, but if make works without
1720 * "leaving directory"-messages we might have missed a
1721 * directory change.
1722 */
1723 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001726 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001727 if (directory)
1728 ptr = concat_fnames(directory, fname, TRUE);
1729 else
1730 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001732 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001733 bufname = ptr;
1734 }
1735 else
1736 bufname = fname;
1737
1738 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001739 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001740 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001741 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001742 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001744 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001745 {
1746 vim_free(qf_last_bufname);
1747 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1748 if (bufname == ptr)
1749 qf_last_bufname = bufname;
1750 else
1751 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001752 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001753 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001754 if (buf == NULL)
1755 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001756
Bram Moolenaarc1542742016-07-20 21:44:37 +02001757 buf->b_has_qf_entry =
1758 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001759 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760}
1761
1762/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001763 * Push dirbuf onto the directory stack and return pointer to actual dir or
1764 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 */
1766 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001767qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 struct dir_stack_T *ds_new;
1770 struct dir_stack_T *ds_ptr;
1771
1772 /* allocate new stack element and hook it in */
1773 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1774 if (ds_new == NULL)
1775 return NULL;
1776
1777 ds_new->next = *stackptr;
1778 *stackptr = ds_new;
1779
1780 /* store directory on the stack */
1781 if (vim_isAbsName(dirbuf)
1782 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001783 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 (*stackptr)->dirname = vim_strsave(dirbuf);
1785 else
1786 {
1787 /* Okay we don't have an absolute path.
1788 * dirbuf must be a subdir of one of the directories on the stack.
1789 * Let's search...
1790 */
1791 ds_new = (*stackptr)->next;
1792 (*stackptr)->dirname = NULL;
1793 while (ds_new)
1794 {
1795 vim_free((*stackptr)->dirname);
1796 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1797 TRUE);
1798 if (mch_isdir((*stackptr)->dirname) == TRUE)
1799 break;
1800
1801 ds_new = ds_new->next;
1802 }
1803
1804 /* clean up all dirs we already left */
1805 while ((*stackptr)->next != ds_new)
1806 {
1807 ds_ptr = (*stackptr)->next;
1808 (*stackptr)->next = (*stackptr)->next->next;
1809 vim_free(ds_ptr->dirname);
1810 vim_free(ds_ptr);
1811 }
1812
1813 /* Nothing found -> it must be on top level */
1814 if (ds_new == NULL)
1815 {
1816 vim_free((*stackptr)->dirname);
1817 (*stackptr)->dirname = vim_strsave(dirbuf);
1818 }
1819 }
1820
1821 if ((*stackptr)->dirname != NULL)
1822 return (*stackptr)->dirname;
1823 else
1824 {
1825 ds_ptr = *stackptr;
1826 *stackptr = (*stackptr)->next;
1827 vim_free(ds_ptr);
1828 return NULL;
1829 }
1830}
1831
1832
1833/*
1834 * pop dirbuf from the directory stack and return previous directory or NULL if
1835 * stack is empty
1836 */
1837 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001838qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 struct dir_stack_T *ds_ptr;
1841
1842 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1843 * What to do if it isn't? */
1844
1845 /* pop top element and free it */
1846 if (*stackptr != NULL)
1847 {
1848 ds_ptr = *stackptr;
1849 *stackptr = (*stackptr)->next;
1850 vim_free(ds_ptr->dirname);
1851 vim_free(ds_ptr);
1852 }
1853
1854 /* return NEW top element as current dir or NULL if stack is empty*/
1855 return *stackptr ? (*stackptr)->dirname : NULL;
1856}
1857
1858/*
1859 * clean up directory stack
1860 */
1861 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001862qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
1864 struct dir_stack_T *ds_ptr;
1865
1866 while ((ds_ptr = *stackptr) != NULL)
1867 {
1868 *stackptr = (*stackptr)->next;
1869 vim_free(ds_ptr->dirname);
1870 vim_free(ds_ptr);
1871 }
1872}
1873
1874/*
1875 * Check in which directory of the directory stack the given file can be
1876 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001877 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 * Cleans up intermediate directory entries.
1879 *
1880 * TODO: How to solve the following problem?
1881 * If we have the this directory tree:
1882 * ./
1883 * ./aa
1884 * ./aa/bb
1885 * ./bb
1886 * ./bb/x.c
1887 * and make says:
1888 * making all in aa
1889 * making all in bb
1890 * x.c:9: Error
1891 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1892 * qf_guess_filepath will return NULL.
1893 */
1894 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001895qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 struct dir_stack_T *ds_ptr;
1898 struct dir_stack_T *ds_tmp;
1899 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001900 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001903 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 return NULL;
1905
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001906 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 fullname = NULL;
1908 while (ds_ptr)
1909 {
1910 vim_free(fullname);
1911 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1912
1913 /* If concat_fnames failed, just go on. The worst thing that can happen
1914 * is that we delete the entire stack.
1915 */
1916 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1917 break;
1918
1919 ds_ptr = ds_ptr->next;
1920 }
1921
1922 vim_free(fullname);
1923
1924 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001925 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001927 ds_tmp = qfl->qf_dir_stack->next;
1928 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 vim_free(ds_tmp->dirname);
1930 vim_free(ds_tmp);
1931 }
1932
1933 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934}
1935
1936/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001937 * When loading a file from the quickfix, the auto commands may modify it.
1938 * This may invalidate the current quickfix entry. This function checks
1939 * whether a entry is still present in the quickfix.
1940 * Similar to location list.
1941 */
1942 static int
1943is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1944{
1945 qf_list_T *qfl;
1946 qfline_T *qfp;
1947 int i;
1948
1949 qfl = &qi->qf_lists[qi->qf_curlist];
1950
1951 /* Search for the entry in the current list */
1952 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1953 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001954 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001955 break;
1956
1957 if (i == qfl->qf_count) /* Entry is not found */
1958 return FALSE;
1959
1960 return TRUE;
1961}
1962
1963/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001964 * Get the next valid entry in the current quickfix/location list. The search
1965 * starts from the current entry. If next_file is TRUE, then return the next
1966 * valid entry in the next file in the list. Returns NULL on failure.
1967 */
1968 static qfline_T *
1969get_next_valid_entry(
1970 qf_info_T *qi,
1971 qfline_T *qf_ptr,
1972 int *qf_index,
1973 int dir)
1974{
1975 int idx;
1976 int old_qf_fnum;
1977
1978 idx = *qf_index;
1979 old_qf_fnum = qf_ptr->qf_fnum;
1980
1981 do
1982 {
1983 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
1984 || qf_ptr->qf_next == NULL)
1985 return NULL;
1986 ++idx;
1987 qf_ptr = qf_ptr->qf_next;
1988 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1989 && !qf_ptr->qf_valid)
1990 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1991
1992 *qf_index = idx;
1993 return qf_ptr;
1994}
1995
1996/*
1997 * Get the previous valid entry in the current quickfix/location list. The
1998 * search starts from the current entry. If prev_file is TRUE, then return the
1999 * previous valid entry in the previous file in the list. Returns NULL on
2000 * failure.
2001 */
2002 static qfline_T *
2003get_prev_valid_entry(
2004 qf_info_T *qi,
2005 qfline_T *qf_ptr,
2006 int *qf_index,
2007 int dir)
2008{
2009 int idx;
2010 int old_qf_fnum;
2011
2012 idx = *qf_index;
2013 old_qf_fnum = qf_ptr->qf_fnum;
2014
2015 do
2016 {
2017 if (idx == 1 || qf_ptr->qf_prev == NULL)
2018 return NULL;
2019 --idx;
2020 qf_ptr = qf_ptr->qf_prev;
2021 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2022 && !qf_ptr->qf_valid)
2023 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2024
2025 *qf_index = idx;
2026 return qf_ptr;
2027}
2028
2029/*
2030 * Get the n'th (errornr) previous/next valid entry from the current entry in
2031 * the quickfix list.
2032 * dir == FORWARD or FORWARD_FILE: next valid entry
2033 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2034 */
2035 static qfline_T *
2036get_nth_valid_entry(
2037 qf_info_T *qi,
2038 int errornr,
2039 qfline_T *qf_ptr,
2040 int *qf_index,
2041 int dir)
2042{
2043 qfline_T *prev_qf_ptr;
2044 int prev_index;
2045 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2046 char_u *err = e_no_more_items;
2047
2048 while (errornr--)
2049 {
2050 prev_qf_ptr = qf_ptr;
2051 prev_index = *qf_index;
2052
2053 if (dir == FORWARD || dir == FORWARD_FILE)
2054 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2055 else
2056 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2057 if (qf_ptr == NULL)
2058 {
2059 qf_ptr = prev_qf_ptr;
2060 *qf_index = prev_index;
2061 if (err != NULL)
2062 {
2063 EMSG(_(err));
2064 return NULL;
2065 }
2066 break;
2067 }
2068
2069 err = NULL;
2070 }
2071
2072 return qf_ptr;
2073}
2074
2075/*
2076 * Get n'th quickfix entry
2077 */
2078 static qfline_T *
2079get_nth_entry(
2080 qf_info_T *qi,
2081 int errornr,
2082 qfline_T *qf_ptr,
2083 int *qf_index)
2084{
2085 int qf_idx = *qf_index;
2086
2087 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2088 {
2089 --qf_idx;
2090 qf_ptr = qf_ptr->qf_prev;
2091 }
2092 while (errornr > qf_idx &&
2093 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2094 qf_ptr->qf_next != NULL)
2095 {
2096 ++qf_idx;
2097 qf_ptr = qf_ptr->qf_next;
2098 }
2099
2100 *qf_index = qf_idx;
2101 return qf_ptr;
2102}
2103
2104#ifdef FEAT_WINDOWS
2105/*
2106 * Find a help window or open one.
2107 */
2108 static int
2109jump_to_help_window(qf_info_T *qi, int *opened_window)
2110{
2111 win_T *wp;
2112 int flags;
2113
2114 if (cmdmod.tab != 0)
2115 wp = NULL;
2116 else
2117 FOR_ALL_WINDOWS(wp)
2118 if (bt_help(wp->w_buffer))
2119 break;
2120 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2121 win_enter(wp, TRUE);
2122 else
2123 {
2124 /*
2125 * Split off help window; put it at far top if no position
2126 * specified, the current window is vertically split and narrow.
2127 */
2128 flags = WSP_HELP;
2129 if (cmdmod.split == 0 && curwin->w_width != Columns
2130 && curwin->w_width < 80)
2131 flags |= WSP_TOP;
2132 if (qi != &ql_info)
2133 flags |= WSP_NEWLOC; /* don't copy the location list */
2134
2135 if (win_split(0, flags) == FAIL)
2136 return FAIL;
2137
2138 *opened_window = TRUE;
2139
2140 if (curwin->w_height < p_hh)
2141 win_setheight((int)p_hh);
2142
2143 if (qi != &ql_info) /* not a quickfix list */
2144 {
2145 /* The new window should use the supplied location list */
2146 curwin->w_llist = qi;
2147 qi->qf_refcount++;
2148 }
2149 }
2150
2151 if (!p_im)
2152 restart_edit = 0; /* don't want insert mode in help file */
2153
2154 return OK;
2155}
2156#endif
2157
2158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 * jump to a quickfix line
2160 * if dir == FORWARD go "errornr" valid entries forward
2161 * if dir == BACKWARD go "errornr" valid entries backward
2162 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2163 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2164 * else if "errornr" is zero, redisplay the same line
2165 * else go to entry "errornr"
2166 */
2167 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002168qf_jump(qf_info_T *qi,
2169 int dir,
2170 int errornr,
2171 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002173 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002174 qfline_T *qf_ptr;
2175 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 linenr_T i;
2179 buf_T *old_curbuf;
2180 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 colnr_T screen_col;
2182 colnr_T char_col;
2183 char_u *line;
2184#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002185 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002186 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 int opened_window = FALSE;
2188 win_T *win;
2189 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002190 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002192 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 int print_message = TRUE;
2194 int len;
2195#ifdef FEAT_FOLDING
2196 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2197#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002198 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002199 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002201 if (qi == NULL)
2202 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002203
2204 if (qi->qf_curlist >= qi->qf_listcount
2205 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
2207 EMSG(_(e_quickfix));
2208 return;
2209 }
2210
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002211 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002213 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 old_qf_index = qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002215 if (dir == FORWARD || dir == FORWARD_FILE ||
2216 dir == BACKWARD || dir == BACKWARD_FILE) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002218 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2219 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002221 qf_ptr = old_qf_ptr;
2222 qf_index = old_qf_index;
2223 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225 }
2226 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002227 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002230 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2231 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 /* No need to print the error message if it's visible in the error
2233 * window */
2234 print_message = FALSE;
2235
2236 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002237 * For ":helpgrep" find a help window or open one.
2238 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002239 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002240 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002241 if (jump_to_help_window(qi, &opened_window) == FAIL)
2242 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002243 }
2244
2245 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 * If currently in the quickfix window, find another window to show the
2247 * file in.
2248 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002249 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002251 win_T *usable_win_ptr = NULL;
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 /*
2254 * If there is no file specified, we don't know where to go.
2255 * But do advance, otherwise ":cn" gets stuck.
2256 */
2257 if (qf_ptr->qf_fnum == 0)
2258 goto theend;
2259
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002260 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002261
2262 ll_ref = curwin->w_llist_ref;
2263 if (ll_ref != NULL)
2264 {
2265 /* Find a window using the same location list that is not a
2266 * quickfix window. */
2267 FOR_ALL_WINDOWS(usable_win_ptr)
2268 if (usable_win_ptr->w_llist == ll_ref
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02002269 && !bt_quickfix(usable_win_ptr->w_buffer))
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002270 {
2271 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002272 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002273 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002274 }
2275
2276 if (!usable_win)
2277 {
2278 /* Locate a window showing a normal buffer */
2279 FOR_ALL_WINDOWS(win)
2280 if (win->w_buffer->b_p_bt[0] == NUL)
2281 {
2282 usable_win = 1;
2283 break;
2284 }
2285 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002286
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002288 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002289 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002291 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002292 {
2293 tabpage_T *tp;
2294 win_T *wp;
2295
2296 FOR_ALL_TAB_WINDOWS(tp, wp)
2297 {
2298 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2299 {
2300 goto_tabpage_win(tp, wp);
2301 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002302 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002303 }
2304 }
2305 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002306win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002307
2308 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002309 * If there is only one window and it is the quickfix window, create a
2310 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002311 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002312 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002314 flags = WSP_ABOVE;
2315 if (ll_ref != NULL)
2316 flags |= WSP_NEWLOC;
2317 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 goto failed; /* not enough room for window */
2319 opened_window = TRUE; /* close it when fail */
2320 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002321 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002322 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002323 if (ll_ref != NULL)
2324 {
2325 /* The new window should use the location list from the
2326 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002327 curwin->w_llist = ll_ref;
2328 ll_ref->qf_refcount++;
2329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331 else
2332 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002333 if (curwin->w_llist_ref != NULL)
2334 {
2335 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002336 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002337 if (win == NULL)
2338 {
2339 /* Find the window showing the selected file */
2340 FOR_ALL_WINDOWS(win)
2341 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2342 break;
2343 if (win == NULL)
2344 {
2345 /* Find a previous usable window */
2346 win = curwin;
2347 do
2348 {
2349 if (win->w_buffer->b_p_bt[0] == NUL)
2350 break;
2351 if (win->w_prev == NULL)
2352 win = lastwin; /* wrap around the top */
2353 else
2354 win = win->w_prev; /* go to previous window */
2355 } while (win != curwin);
2356 }
2357 }
2358 win_goto(win);
2359
2360 /* If the location list for the window is not set, then set it
2361 * to the location list from the location window */
2362 if (win->w_llist == NULL)
2363 {
2364 win->w_llist = ll_ref;
2365 ll_ref->qf_refcount++;
2366 }
2367 }
2368 else
2369 {
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 /*
2372 * Try to find a window that shows the right buffer.
2373 * Default to the window just above the quickfix buffer.
2374 */
2375 win = curwin;
2376 altwin = NULL;
2377 for (;;)
2378 {
2379 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2380 break;
2381 if (win->w_prev == NULL)
2382 win = lastwin; /* wrap around the top */
2383 else
2384 win = win->w_prev; /* go to previous window */
2385
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002386 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
2388 /* Didn't find it, go to the window before the quickfix
2389 * window. */
2390 if (altwin != NULL)
2391 win = altwin;
2392 else if (curwin->w_prev != NULL)
2393 win = curwin->w_prev;
2394 else
2395 win = curwin->w_next;
2396 break;
2397 }
2398
2399 /* Remember a usable window. */
2400 if (altwin == NULL && !win->w_p_pvw
2401 && win->w_buffer->b_p_bt[0] == NUL)
2402 altwin = win;
2403 }
2404
2405 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
2408 }
2409#endif
2410
2411 /*
2412 * If there is a file name,
2413 * read the wanted file if needed, and check autowrite etc.
2414 */
2415 old_curbuf = curbuf;
2416 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002417
2418 if (qf_ptr->qf_fnum != 0)
2419 {
2420 if (qf_ptr->qf_type == 1)
2421 {
2422 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2423 * set b_p_ro flag). */
2424 if (!can_abandon(curbuf, forceit))
2425 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002426 no_write_message();
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002427 ok = FALSE;
2428 }
2429 else
2430 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002431 ECMD_HIDE + ECMD_SET_HELP,
2432 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002433 }
2434 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002435 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002436 int old_qf_curlist = qi->qf_curlist;
2437 int is_abort = FALSE;
2438
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002439 ok = buflist_getfile(qf_ptr->qf_fnum,
2440 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002441 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002442 {
2443 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002444 is_abort = TRUE;
2445 opened_window = FALSE;
2446 }
2447 else if (old_qf_curlist != qi->qf_curlist
2448 || !is_qf_entry_present(qi, qf_ptr))
2449 {
2450 if (qi == &ql_info)
2451 EMSG(_("E925: Current quickfix was changed"));
2452 else
2453 EMSG(_("E926: Current location list was changed"));
2454 is_abort = TRUE;
2455 }
2456
2457 if (is_abort)
2458 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002459 ok = FALSE;
2460 qi = NULL;
2461 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002462 }
2463 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002464 }
2465
2466 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {
2468 /* When not switched to another buffer, still need to set pc mark */
2469 if (curbuf == old_curbuf)
2470 setpcmark();
2471
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002472 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002474 /*
2475 * Go to line with error, unless qf_lnum is 0.
2476 */
2477 i = qf_ptr->qf_lnum;
2478 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002480 if (i > curbuf->b_ml.ml_line_count)
2481 i = curbuf->b_ml.ml_line_count;
2482 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002484 if (qf_ptr->qf_col > 0)
2485 {
2486 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002487#ifdef FEAT_VIRTUALEDIT
2488 curwin->w_cursor.coladd = 0;
2489#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002490 if (qf_ptr->qf_viscol == TRUE)
2491 {
2492 /*
2493 * Check each character from the beginning of the error
2494 * line up to the error column. For each tab character
2495 * found, reduce the error column value by the length of
2496 * a tab character.
2497 */
2498 line = ml_get_curline();
2499 screen_col = 0;
2500 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2501 {
2502 if (*line == NUL)
2503 break;
2504 if (*line++ == '\t')
2505 {
2506 curwin->w_cursor.col -= 7 - (screen_col % 8);
2507 screen_col += 8 - (screen_col % 8);
2508 }
2509 else
2510 ++screen_col;
2511 }
2512 }
2513 check_cursor();
2514 }
2515 else
2516 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 }
2518 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002519 {
2520 pos_T save_cursor;
2521
2522 /* Move the cursor to the first line in the buffer */
2523 save_cursor = curwin->w_cursor;
2524 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002525 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002526 SEARCH_KEEP, NULL, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002527 curwin->w_cursor = save_cursor;
2528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530#ifdef FEAT_FOLDING
2531 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2532 foldOpenCursor();
2533#endif
2534 if (print_message)
2535 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002536 /* Update the screen before showing the message, unless the screen
2537 * scrolled up. */
2538 if (!msg_scrolled)
2539 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002541 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2543 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2544 /* Add the message, skipping leading whitespace and newlines. */
2545 len = (int)STRLEN(IObuff);
2546 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2547
2548 /* Output the message. Overwrite to avoid scrolling when the 'O'
2549 * flag is present in 'shortmess'; But when not jumping, print the
2550 * whole message. */
2551 i = msg_scroll;
2552 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2553 msg_scroll = TRUE;
2554 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2555 msg_scroll = FALSE;
2556 msg_attr_keep(IObuff, 0, TRUE);
2557 msg_scroll = i;
2558 }
2559 }
2560 else
2561 {
2562#ifdef FEAT_WINDOWS
2563 if (opened_window)
2564 win_close(curwin, TRUE); /* Close opened window */
2565#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002566 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
2568 /*
2569 * Couldn't open file, so put index back where it was. This could
2570 * happen if the file was readonly and we changed something.
2571 */
2572#ifdef FEAT_WINDOWS
2573failed:
2574#endif
2575 qf_ptr = old_qf_ptr;
2576 qf_index = old_qf_index;
2577 }
2578 }
2579theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002580 if (qi != NULL)
2581 {
2582 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2583 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585#ifdef FEAT_WINDOWS
2586 if (p_swb != old_swb && opened_window)
2587 {
2588 /* Restore old 'switchbuf' value, but not when an autocommand or
2589 * modeline has changed the value. */
2590 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002593 swb_flags = old_swb_flags;
2594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 else
2596 free_string_option(old_swb);
2597 }
2598#endif
2599}
2600
2601/*
2602 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002603 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 */
2605 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002606qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002608 buf_T *buf;
2609 char_u *fname;
2610 qfline_T *qfp;
2611 int i;
2612 int idx1 = 1;
2613 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002614 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002615 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002616 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002618 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002620 if (eap->cmdidx == CMD_llist)
2621 {
2622 qi = GET_LOC_LIST(curwin);
2623 if (qi == NULL)
2624 {
2625 EMSG(_(e_loclist));
2626 return;
2627 }
2628 }
2629
2630 if (qi->qf_curlist >= qi->qf_listcount
2631 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
2633 EMSG(_(e_quickfix));
2634 return;
2635 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002636 if (*arg == '+')
2637 {
2638 ++arg;
2639 plus = TRUE;
2640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2642 {
2643 EMSG(_(e_trailing));
2644 return;
2645 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002646 if (plus)
2647 {
2648 i = qi->qf_lists[qi->qf_curlist].qf_index;
2649 idx2 = i + idx1;
2650 idx1 = i;
2651 }
2652 else
2653 {
2654 i = qi->qf_lists[qi->qf_curlist].qf_count;
2655 if (idx1 < 0)
2656 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2657 if (idx2 < 0)
2658 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002661 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002663 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2664 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 {
2666 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2667 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002668 msg_putchar('\n');
2669 if (got_int)
2670 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002671
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002672 fname = NULL;
2673 if (qfp->qf_fnum != 0
2674 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2675 {
2676 fname = buf->b_fname;
2677 if (qfp->qf_type == 1) /* :helpgrep */
2678 fname = gettail(fname);
2679 }
2680 if (fname == NULL)
2681 sprintf((char *)IObuff, "%2d", i);
2682 else
2683 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2684 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002685 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar21020352017-06-13 17:21:04 +02002686 ? HL_ATTR(HLF_QFL) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002687 if (qfp->qf_lnum == 0)
2688 IObuff[0] = NUL;
2689 else if (qfp->qf_col == 0)
2690 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2691 else
2692 sprintf((char *)IObuff, ":%ld col %d",
2693 qfp->qf_lnum, qfp->qf_col);
2694 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2695 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002696 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002697 if (qfp->qf_pattern != NULL)
2698 {
2699 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2700 STRCAT(IObuff, ":");
2701 msg_puts(IObuff);
2702 }
2703 msg_puts((char_u *)" ");
2704
2705 /* Remove newlines and leading whitespace from the text. For an
2706 * unrecognized line keep the indent, the compiler may mark a word
2707 * with ^^^^. */
2708 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2710 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002711 msg_prt_line(IObuff, FALSE);
2712 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002714
2715 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002716 if (qfp == NULL)
2717 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002718 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 ui_breakcheck();
2720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721}
2722
2723/*
2724 * Remove newlines and leading whitespace from an error message.
2725 * Put the result in "buf[bufsize]".
2726 */
2727 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002728qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729{
2730 int i;
2731 char_u *p = text;
2732
2733 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2734 {
2735 if (*p == '\n')
2736 {
2737 buf[i] = ' ';
2738 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002739 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 break;
2741 }
2742 else
2743 buf[i] = *p++;
2744 }
2745 buf[i] = NUL;
2746}
2747
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002748 static void
2749qf_msg(qf_info_T *qi, int which, char *lead)
2750{
2751 char *title = (char *)qi->qf_lists[which].qf_title;
2752 int count = qi->qf_lists[which].qf_count;
2753 char_u buf[IOSIZE];
2754
2755 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2756 lead,
2757 which + 1,
2758 qi->qf_listcount,
2759 count);
2760
2761 if (title != NULL)
2762 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002763 size_t len = STRLEN(buf);
2764
2765 if (len < 34)
2766 {
2767 vim_memset(buf + len, ' ', 34 - len);
2768 buf[34] = NUL;
2769 }
2770 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002771 }
2772 trunc_string(buf, buf, Columns - 1, IOSIZE);
2773 msg(buf);
2774}
2775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776/*
2777 * ":colder [count]": Up in the quickfix stack.
2778 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002779 * ":lolder [count]": Up in the location list stack.
2780 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 */
2782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002783qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002785 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 int count;
2787
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002788 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2789 {
2790 qi = GET_LOC_LIST(curwin);
2791 if (qi == NULL)
2792 {
2793 EMSG(_(e_loclist));
2794 return;
2795 }
2796 }
2797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (eap->addr_count != 0)
2799 count = eap->line2;
2800 else
2801 count = 1;
2802 while (count--)
2803 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002804 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002806 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
2808 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002809 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002811 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813 else
2814 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002815 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 {
2817 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002820 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002823 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002825 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826#endif
2827}
2828
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002829 void
2830qf_history(exarg_T *eap)
2831{
2832 qf_info_T *qi = &ql_info;
2833 int i;
2834
2835 if (eap->cmdidx == CMD_lhistory)
2836 qi = GET_LOC_LIST(curwin);
2837 if (qi == NULL || (qi->qf_listcount == 0
2838 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2839 MSG(_("No entries"));
2840 else
2841 for (i = 0; i < qi->qf_listcount; ++i)
2842 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2843}
2844
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002846 * Free all the entries in the error list "idx". Note that other information
2847 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 */
2849 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002850qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002852 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002853 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002854 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002855 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002857 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002859 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002860 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002861 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002862 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002863 vim_free(qfp->qf_text);
2864 stop = (qfp == qfpnext);
2865 vim_free(qfp->qf_pattern);
2866 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002867 if (stop)
2868 /* Somehow qf_count may have an incorrect value, set it to 1
2869 * to avoid crashing when it's wrong.
2870 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002871 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002872 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002873 qfl->qf_start = qfpnext;
2874 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002876
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002877 qfl->qf_index = 0;
2878 qfl->qf_start = NULL;
2879 qfl->qf_last = NULL;
2880 qfl->qf_ptr = NULL;
2881 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002882
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002883 qf_clean_dir_stack(&qfl->qf_dir_stack);
2884 qfl->qf_directory = NULL;
2885 qf_clean_dir_stack(&qfl->qf_file_stack);
2886 qfl->qf_currfile = NULL;
2887 qfl->qf_multiline = FALSE;
2888 qfl->qf_multiignore = FALSE;
2889 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890}
2891
2892/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002893 * Free error list "idx". Frees all the entries in the quickfix list,
2894 * associated context information and the title.
2895 */
2896 static void
2897qf_free(qf_info_T *qi, int idx)
2898{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002899 qf_list_T *qfl = &qi->qf_lists[idx];
2900
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002901 qf_free_items(qi, idx);
2902
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002903 vim_free(qfl->qf_title);
2904 qfl->qf_title = NULL;
2905 free_tv(qfl->qf_ctx);
2906 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002907 qfl->qf_id = 0;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002908}
2909
2910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 * qf_mark_adjust: adjust marks
2912 */
2913 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002914qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002915 win_T *wp,
2916 linenr_T line1,
2917 linenr_T line2,
2918 long amount,
2919 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002921 int i;
2922 qfline_T *qfp;
2923 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002924 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002925 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002926 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
Bram Moolenaarc1542742016-07-20 21:44:37 +02002928 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002929 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002930 if (wp != NULL)
2931 {
2932 if (wp->w_llist == NULL)
2933 return;
2934 qi = wp->w_llist;
2935 }
2936
2937 for (idx = 0; idx < qi->qf_listcount; ++idx)
2938 if (qi->qf_lists[idx].qf_count)
2939 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002940 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2941 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 if (qfp->qf_fnum == curbuf->b_fnum)
2943 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002944 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2946 {
2947 if (amount == MAXLNUM)
2948 qfp->qf_cleared = TRUE;
2949 else
2950 qfp->qf_lnum += amount;
2951 }
2952 else if (amount_after && qfp->qf_lnum > line2)
2953 qfp->qf_lnum += amount_after;
2954 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002955
2956 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002957 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958}
2959
2960/*
2961 * Make a nice message out of the error character and the error number:
2962 * char number message
2963 * e or E 0 " error"
2964 * w or W 0 " warning"
2965 * i or I 0 " info"
2966 * 0 0 ""
2967 * other 0 " c"
2968 * e or E n " error n"
2969 * w or W n " warning n"
2970 * i or I n " info n"
2971 * 0 n " error n"
2972 * other n " c n"
2973 * 1 x "" :helpgrep
2974 */
2975 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002976qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977{
2978 static char_u buf[20];
2979 static char_u cc[3];
2980 char_u *p;
2981
2982 if (c == 'W' || c == 'w')
2983 p = (char_u *)" warning";
2984 else if (c == 'I' || c == 'i')
2985 p = (char_u *)" info";
2986 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2987 p = (char_u *)" error";
2988 else if (c == 0 || c == 1)
2989 p = (char_u *)"";
2990 else
2991 {
2992 cc[0] = ' ';
2993 cc[1] = c;
2994 cc[2] = NUL;
2995 p = cc;
2996 }
2997
2998 if (nr <= 0)
2999 return p;
3000
3001 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3002 return buf;
3003}
3004
3005#if defined(FEAT_WINDOWS) || defined(PROTO)
3006/*
3007 * ":cwindow": open the quickfix window if we have errors to display,
3008 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003009 * ":lwindow": open the location list window if we have locations to display,
3010 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 */
3012 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003013ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003015 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 win_T *win;
3017
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003018 if (eap->cmdidx == CMD_lwindow)
3019 {
3020 qi = GET_LOC_LIST(curwin);
3021 if (qi == NULL)
3022 return;
3023 }
3024
3025 /* Look for an existing quickfix window. */
3026 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
3028 /*
3029 * If a quickfix window is open but we have no errors to display,
3030 * close the window. If a quickfix window is not open, then open
3031 * it if we have errors; otherwise, leave it closed.
3032 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003033 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003034 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003035 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 {
3037 if (win != NULL)
3038 ex_cclose(eap);
3039 }
3040 else if (win == NULL)
3041 ex_copen(eap);
3042}
3043
3044/*
3045 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003046 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003049ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003051 win_T *win = NULL;
3052 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003054 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3055 {
3056 qi = GET_LOC_LIST(curwin);
3057 if (qi == NULL)
3058 return;
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003061 /* Find existing quickfix window and close it. */
3062 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 if (win != NULL)
3064 win_close(win, FALSE);
3065}
3066
3067/*
3068 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003069 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 */
3071 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003072ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003074 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003077 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003078 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003079 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003081 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3082 {
3083 qi = GET_LOC_LIST(curwin);
3084 if (qi == NULL)
3085 {
3086 EMSG(_(e_loclist));
3087 return;
3088 }
3089 }
3090
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (eap->addr_count != 0)
3092 height = eap->line2;
3093 else
3094 height = QF_WINHEIGHT;
3095
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097#ifdef FEAT_GUI
3098 need_mouse_correct = TRUE;
3099#endif
3100
3101 /*
3102 * Find existing quickfix window, or open a new one.
3103 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003104 win = qf_find_win(qi);
3105
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003106 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003109 if (eap->addr_count != 0)
3110 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003111 if (cmdmod.split & WSP_VERT)
3112 {
3113 if (height != W_WIDTH(win))
3114 win_setwidth(height);
3115 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003116 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003117 win_setheight(height);
3118 }
3119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 else
3121 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00003122 qf_buf = qf_find_buf(qi);
3123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 /* The current window becomes the previous window afterwards. */
3125 win = curwin;
3126
Bram Moolenaar77642c02012-11-20 17:55:10 +01003127 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3128 && cmdmod.split == 0)
3129 /* Create the new window at the very bottom, except when
3130 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003131 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003132 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003134 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003136 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003138 /*
3139 * For the location list window, create a reference to the
3140 * location list from the window 'win'.
3141 */
3142 curwin->w_llist_ref = win->w_llist;
3143 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003145
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003146 if (oldwin != curwin)
3147 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003148 if (qf_buf != NULL)
3149 /* Use the existing quickfix buffer */
3150 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003151 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003152 else
3153 {
3154 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003155 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003156 /* switch off 'swapfile' */
3157 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3158 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003159 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003160 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003161 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003162#ifdef FEAT_DIFF
3163 curwin->w_p_diff = FALSE;
3164#endif
3165#ifdef FEAT_FOLDING
3166 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3167 OPT_LOCAL);
3168#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003171 /* Only set the height when still in the same tab page and there is no
3172 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003173 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 win_setheight(height);
3175 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3176 if (win_valid(win))
3177 prevwin = win;
3178 }
3179
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003180 qf_set_title_var(qi);
3181
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 /*
3183 * Fill the buffer with the quickfix list.
3184 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003185 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003187 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 curwin->w_cursor.col = 0;
3189 check_cursor();
3190 update_topline(); /* scroll to show the line */
3191}
3192
3193/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003194 * Move the cursor in the quickfix window to "lnum".
3195 */
3196 static void
3197qf_win_goto(win_T *win, linenr_T lnum)
3198{
3199 win_T *old_curwin = curwin;
3200
3201 curwin = win;
3202 curbuf = win->w_buffer;
3203 curwin->w_cursor.lnum = lnum;
3204 curwin->w_cursor.col = 0;
3205#ifdef FEAT_VIRTUALEDIT
3206 curwin->w_cursor.coladd = 0;
3207#endif
3208 curwin->w_curswant = 0;
3209 update_topline(); /* scroll to show the line */
3210 redraw_later(VALID);
3211 curwin->w_redr_status = TRUE; /* update ruler */
3212 curwin = old_curwin;
3213 curbuf = curwin->w_buffer;
3214}
3215
3216/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003217 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003218 */
3219 void
3220ex_cbottom(exarg_T *eap UNUSED)
3221{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003222 qf_info_T *qi = &ql_info;
3223 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003224
Bram Moolenaar537ef082016-07-09 17:56:19 +02003225 if (eap->cmdidx == CMD_lbottom)
3226 {
3227 qi = GET_LOC_LIST(curwin);
3228 if (qi == NULL)
3229 {
3230 EMSG(_(e_loclist));
3231 return;
3232 }
3233 }
3234
3235 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003236 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3237 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3238}
3239
3240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 * Return the number of the current entry (line number in the quickfix
3242 * window).
3243 */
3244 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003245qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003247 qf_info_T *qi = &ql_info;
3248
3249 if (IS_LL_WINDOW(wp))
3250 /* In the location list window, use the referenced location list */
3251 qi = wp->w_llist_ref;
3252
3253 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254}
3255
3256/*
3257 * Update the cursor position in the quickfix window to the current error.
3258 * Return TRUE if there is a quickfix window.
3259 */
3260 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003261qf_win_pos_update(
3262 qf_info_T *qi,
3263 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003266 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
3268 /*
3269 * Put the cursor on the current error in the quickfix window, so that
3270 * it's viewable.
3271 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003272 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 if (win != NULL
3274 && qf_index <= win->w_buffer->b_ml.ml_line_count
3275 && old_qf_index != qf_index)
3276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (qf_index > old_qf_index)
3278 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003279 win->w_redraw_top = old_qf_index;
3280 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282 else
3283 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003284 win->w_redraw_top = qf_index;
3285 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003287 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 }
3289 return win != NULL;
3290}
3291
3292/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003293 * Check whether the given window is displaying the specified quickfix/location
3294 * list buffer
3295 */
3296 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003297is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003298{
3299 /*
3300 * A window displaying the quickfix buffer will have the w_llist_ref field
3301 * set to NULL.
3302 * A window displaying a location list buffer will have the w_llist_ref
3303 * pointing to the location list.
3304 */
3305 if (bt_quickfix(win->w_buffer))
3306 if ((qi == &ql_info && win->w_llist_ref == NULL)
3307 || (qi != &ql_info && win->w_llist_ref == qi))
3308 return TRUE;
3309
3310 return FALSE;
3311}
3312
3313/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003314 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003315 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003316 */
3317 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003318qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003319{
3320 win_T *win;
3321
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003322 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003323 if (is_qf_win(win, qi))
3324 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003325
3326 return win;
3327}
3328
3329/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003330 * Find a quickfix buffer.
3331 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 */
3333 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003334qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003336 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003337 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar9c102382006-05-03 21:26:49 +00003339 FOR_ALL_TAB_WINDOWS(tp, win)
3340 if (is_qf_win(win, qi))
3341 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003342
Bram Moolenaar9c102382006-05-03 21:26:49 +00003343 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344}
3345
3346/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003347 * Update the w:quickfix_title variable in the quickfix/location list window
3348 */
3349 static void
3350qf_update_win_titlevar(qf_info_T *qi)
3351{
3352 win_T *win;
3353 win_T *curwin_save;
3354
3355 if ((win = qf_find_win(qi)) != NULL)
3356 {
3357 curwin_save = curwin;
3358 curwin = win;
3359 qf_set_title_var(qi);
3360 curwin = curwin_save;
3361 }
3362}
3363
3364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 * Find the quickfix buffer. If it exists, update the contents.
3366 */
3367 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003368qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003371 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003375 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 if (buf != NULL)
3377 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003378 linenr_T old_line_count = buf->b_ml.ml_line_count;
3379
3380 if (old_last == NULL)
3381 /* set curwin/curbuf to buf and save a few things */
3382 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaard823fa92016-08-12 16:29:27 +02003384 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003385
Bram Moolenaar864293a2016-06-02 13:40:04 +02003386 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003387 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003388
Bram Moolenaar864293a2016-06-02 13:40:04 +02003389 if (old_last == NULL)
3390 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003391 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003392
3393 /* restore curwin/curbuf and a few other things */
3394 aucmd_restbuf(&aco);
3395 }
3396
3397 /* Only redraw when added lines are visible. This avoids flickering
3398 * when the added lines are not visible. */
3399 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3400 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402}
3403
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003404/*
3405 * Set "w:quickfix_title" if "qi" has a title.
3406 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003407 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003408qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003409{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003410 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3411 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003412 qi->qf_lists[qi->qf_curlist].qf_title);
3413}
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415/*
3416 * Fill current buffer with quickfix errors, replacing any previous contents.
3417 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003418 * If "old_last" is not NULL append the items after this one.
3419 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3420 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 */
3422 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003423qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003425 linenr_T lnum;
3426 qfline_T *qfp;
3427 buf_T *errbuf;
3428 int len;
3429 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaar864293a2016-06-02 13:40:04 +02003431 if (old_last == NULL)
3432 {
3433 if (buf != curbuf)
3434 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003435 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003436 return;
3437 }
3438
3439 /* delete all existing lines */
3440 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3441 (void)ml_delete((linenr_T)1, FALSE);
3442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
3444 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003445 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
3447 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003448 if (old_last == NULL)
3449 {
3450 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3451 lnum = 0;
3452 }
3453 else
3454 {
3455 qfp = old_last->qf_next;
3456 lnum = buf->b_ml.ml_line_count;
3457 }
3458 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
3460 if (qfp->qf_fnum != 0
3461 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3462 && errbuf->b_fname != NULL)
3463 {
3464 if (qfp->qf_type == 1) /* :helpgrep */
3465 STRCPY(IObuff, gettail(errbuf->b_fname));
3466 else
3467 STRCPY(IObuff, errbuf->b_fname);
3468 len = (int)STRLEN(IObuff);
3469 }
3470 else
3471 len = 0;
3472 IObuff[len++] = '|';
3473
3474 if (qfp->qf_lnum > 0)
3475 {
3476 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3477 len += (int)STRLEN(IObuff + len);
3478
3479 if (qfp->qf_col > 0)
3480 {
3481 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3482 len += (int)STRLEN(IObuff + len);
3483 }
3484
3485 sprintf((char *)IObuff + len, "%s",
3486 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3487 len += (int)STRLEN(IObuff + len);
3488 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003489 else if (qfp->qf_pattern != NULL)
3490 {
3491 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3492 len += (int)STRLEN(IObuff + len);
3493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 IObuff[len++] = '|';
3495 IObuff[len++] = ' ';
3496
3497 /* Remove newlines and leading whitespace from the text.
3498 * For an unrecognized line keep the indent, the compiler may
3499 * mark a word with ^^^^. */
3500 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3501 IObuff + len, IOSIZE - len);
3502
Bram Moolenaar864293a2016-06-02 13:40:04 +02003503 if (ml_append_buf(buf, lnum, IObuff,
3504 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003506 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003508 if (qfp == NULL)
3509 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003511
3512 if (old_last == NULL)
3513 /* Delete the empty line which is now at the end */
3514 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 }
3516
3517 /* correct cursor position */
3518 check_lnums(TRUE);
3519
Bram Moolenaar864293a2016-06-02 13:40:04 +02003520 if (old_last == NULL)
3521 {
3522 /* Set the 'filetype' to "qf" each time after filling the buffer.
3523 * This resembles reading a file into a buffer, it's more logical when
3524 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003525#ifdef FEAT_AUTOCMD
3526 ++curbuf_lock;
3527#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003528 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3529 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003532 keep_filetype = TRUE; /* don't detect 'filetype' */
3533 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003535 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003537 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003538 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003540 /* make sure it will be redrawn */
3541 redraw_curbuf_later(NOT_VALID);
3542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
3544 /* Restore KeyTyped, setting 'filetype' may reset it. */
3545 KeyTyped = old_KeyTyped;
3546}
3547
3548#endif /* FEAT_WINDOWS */
3549
3550/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003551 * Return TRUE when using ":vimgrep" for ":grep".
3552 */
3553 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003554grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003555{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003556 return ((cmdidx == CMD_grep
3557 || cmdidx == CMD_lgrep
3558 || cmdidx == CMD_grepadd
3559 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003560 && STRCMP("internal",
3561 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3562}
3563
3564/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003565 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 */
3567 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003568ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003570 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003572 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003574 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003575 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003576 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003577#ifdef FEAT_AUTOCMD
3578 char_u *au_name = NULL;
3579
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003580 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3581 if (grep_internal(eap->cmdidx))
3582 {
3583 ex_vimgrep(eap);
3584 return;
3585 }
3586
Bram Moolenaar7c626922005-02-07 22:01:03 +00003587 switch (eap->cmdidx)
3588 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003589 case CMD_make: au_name = (char_u *)"make"; break;
3590 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3591 case CMD_grep: au_name = (char_u *)"grep"; break;
3592 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3593 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3594 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003595 default: break;
3596 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003597 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3598 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003599 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003600# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003601 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003602 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003603# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003604 }
3605#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003606#ifdef FEAT_MBYTE
3607 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
Bram Moolenaara6557602006-02-04 22:43:20 +00003610 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3611 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003612 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003615 fname = get_mef_name();
3616 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003618 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
3620 /*
3621 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3622 */
3623 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3624 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003625 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 cmd = alloc(len);
3627 if (cmd == NULL)
3628 return;
3629 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3630 (char *)p_shq);
3631 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003632 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 /*
3634 * Output a newline if there's something else than the :make command that
3635 * was typed (in which case the cursor is in column 0).
3636 */
3637 if (msg_col == 0)
3638 msg_didout = FALSE;
3639 msg_start();
3640 MSG_PUTS(":!");
3641 msg_outtrans(cmd); /* show what we are doing */
3642
3643 /* let the shell know if we are redirecting output or not */
3644 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3645
3646#ifdef AMIGA
3647 out_flush();
3648 /* read window status report and redraw before message */
3649 (void)char_avail();
3650#endif
3651
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003652 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003653 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3654 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003655 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003656 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003657 if (wp != NULL)
3658 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003659#ifdef FEAT_AUTOCMD
3660 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003661 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003662 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3663 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003664 if (qi->qf_curlist < qi->qf_listcount)
3665 res = qi->qf_lists[qi->qf_curlist].qf_count;
3666 else
3667 res = 0;
3668 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003669#endif
3670 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003671 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
Bram Moolenaar7c626922005-02-07 22:01:03 +00003673 mch_remove(fname);
3674 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 vim_free(cmd);
3676}
3677
3678/*
3679 * Return the name for the errorfile, in allocated memory.
3680 * Find a new unique name when 'makeef' contains "##".
3681 * Returns NULL for error.
3682 */
3683 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003684get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685{
3686 char_u *p;
3687 char_u *name;
3688 static int start = -1;
3689 static int off = 0;
3690#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003691 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692#endif
3693
3694 if (*p_mef == NUL)
3695 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003696 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (name == NULL)
3698 EMSG(_(e_notmp));
3699 return name;
3700 }
3701
3702 for (p = p_mef; *p; ++p)
3703 if (p[0] == '#' && p[1] == '#')
3704 break;
3705
3706 if (*p == NUL)
3707 return vim_strsave(p_mef);
3708
3709 /* Keep trying until the name doesn't exist yet. */
3710 for (;;)
3711 {
3712 if (start == -1)
3713 start = mch_get_pid();
3714 else
3715 off += 19;
3716
3717 name = alloc((unsigned)STRLEN(p_mef) + 30);
3718 if (name == NULL)
3719 break;
3720 STRCPY(name, p_mef);
3721 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3722 STRCAT(name, p + 2);
3723 if (mch_getperm(name) < 0
3724#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003725 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 && mch_lstat((char *)name, &sb) < 0
3727#endif
3728 )
3729 break;
3730 vim_free(name);
3731 }
3732 return name;
3733}
3734
3735/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003736 * Returns the number of valid entries in the current quickfix/location list.
3737 */
3738 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003739qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003740{
3741 qf_info_T *qi = &ql_info;
3742 qfline_T *qfp;
3743 int i, sz = 0;
3744 int prev_fnum = 0;
3745
3746 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3747 {
3748 /* Location list */
3749 qi = GET_LOC_LIST(curwin);
3750 if (qi == NULL)
3751 return 0;
3752 }
3753
3754 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003755 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003756 ++i, qfp = qfp->qf_next)
3757 {
3758 if (qfp->qf_valid)
3759 {
3760 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3761 sz++; /* Count all valid entries */
3762 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3763 {
3764 /* Count the number of files */
3765 sz++;
3766 prev_fnum = qfp->qf_fnum;
3767 }
3768 }
3769 }
3770
3771 return sz;
3772}
3773
3774/*
3775 * Returns the current index of the quickfix/location list.
3776 * Returns 0 if there is an error.
3777 */
3778 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003779qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003780{
3781 qf_info_T *qi = &ql_info;
3782
3783 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3784 {
3785 /* Location list */
3786 qi = GET_LOC_LIST(curwin);
3787 if (qi == NULL)
3788 return 0;
3789 }
3790
3791 return qi->qf_lists[qi->qf_curlist].qf_index;
3792}
3793
3794/*
3795 * Returns the current index in the quickfix/location list (counting only valid
3796 * entries). If no valid entries are in the list, then returns 1.
3797 */
3798 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003799qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003800{
3801 qf_info_T *qi = &ql_info;
3802 qf_list_T *qfl;
3803 qfline_T *qfp;
3804 int i, eidx = 0;
3805 int prev_fnum = 0;
3806
3807 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3808 {
3809 /* Location list */
3810 qi = GET_LOC_LIST(curwin);
3811 if (qi == NULL)
3812 return 1;
3813 }
3814
3815 qfl = &qi->qf_lists[qi->qf_curlist];
3816 qfp = qfl->qf_start;
3817
3818 /* check if the list has valid errors */
3819 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3820 return 1;
3821
3822 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3823 {
3824 if (qfp->qf_valid)
3825 {
3826 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3827 {
3828 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3829 {
3830 /* Count the number of files */
3831 eidx++;
3832 prev_fnum = qfp->qf_fnum;
3833 }
3834 }
3835 else
3836 eidx++;
3837 }
3838 }
3839
3840 return eidx ? eidx : 1;
3841}
3842
3843/*
3844 * Get the 'n'th valid error entry in the quickfix or location list.
3845 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3846 * For :cdo and :ldo returns the 'n'th valid error entry.
3847 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3848 */
3849 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003850qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003851{
3852 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3853 qfline_T *qfp = qfl->qf_start;
3854 int i, eidx;
3855 int prev_fnum = 0;
3856
3857 /* check if the list has valid errors */
3858 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3859 return 1;
3860
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003861 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003862 i++, qfp = qfp->qf_next)
3863 {
3864 if (qfp->qf_valid)
3865 {
3866 if (fdo)
3867 {
3868 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3869 {
3870 /* Count the number of files */
3871 eidx++;
3872 prev_fnum = qfp->qf_fnum;
3873 }
3874 }
3875 else
3876 eidx++;
3877 }
3878
3879 if (eidx == n)
3880 break;
3881 }
3882
3883 if (i <= qfl->qf_count)
3884 return i;
3885 else
3886 return 1;
3887}
3888
3889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003891 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003892 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 */
3894 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003895ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003897 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003898 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003899
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003900 if (eap->cmdidx == CMD_ll
3901 || eap->cmdidx == CMD_lrewind
3902 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003903 || eap->cmdidx == CMD_llast
3904 || eap->cmdidx == CMD_ldo
3905 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003906 {
3907 qi = GET_LOC_LIST(curwin);
3908 if (qi == NULL)
3909 {
3910 EMSG(_(e_loclist));
3911 return;
3912 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003913 }
3914
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003915 if (eap->addr_count > 0)
3916 errornr = (int)eap->line2;
3917 else
3918 {
3919 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3920 errornr = 0;
3921 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3922 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3923 errornr = 1;
3924 else
3925 errornr = 32767;
3926 }
3927
3928 /* For cdo and ldo commands, jump to the nth valid error.
3929 * For cfdo and lfdo commands, jump to the nth valid file entry.
3930 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02003931 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
3932 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003933 errornr = qf_get_nth_valid_entry(qi,
3934 eap->addr_count > 0 ? (int)eap->line1 : 1,
3935 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3936
3937 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938}
3939
3940/*
3941 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003942 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003943 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 */
3945 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003946ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003948 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003949 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003950
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003951 if (eap->cmdidx == CMD_lnext
3952 || eap->cmdidx == CMD_lNext
3953 || eap->cmdidx == CMD_lprevious
3954 || eap->cmdidx == CMD_lnfile
3955 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003956 || eap->cmdidx == CMD_lpfile
3957 || eap->cmdidx == CMD_ldo
3958 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003959 {
3960 qi = GET_LOC_LIST(curwin);
3961 if (qi == NULL)
3962 {
3963 EMSG(_(e_loclist));
3964 return;
3965 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003966 }
3967
Bram Moolenaar55b69262017-08-13 13:42:01 +02003968 if (eap->addr_count > 0
3969 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
3970 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003971 errornr = (int)eap->line2;
3972 else
3973 errornr = 1;
3974
3975 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3976 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003978 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3979 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003981 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3982 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 ? BACKWARD_FILE
3984 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003985 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986}
3987
3988/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003989 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003990 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 */
3992 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003993ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003995 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003996 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003997 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003998#ifdef FEAT_AUTOCMD
3999 char_u *au_name = NULL;
4000#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004001
4002 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004003 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004004 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004005
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004006#ifdef FEAT_AUTOCMD
4007 switch (eap->cmdidx)
4008 {
4009 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4010 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4011 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4012 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4013 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4014 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4015 default: break;
4016 }
4017 if (au_name != NULL)
4018 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
4019#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004020#ifdef FEAT_MBYTE
4021 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4022#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004023#ifdef FEAT_BROWSE
4024 if (cmdmod.browse)
4025 {
4026 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4027 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4028 if (browse_file == NULL)
4029 return;
4030 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4031 vim_free(browse_file);
4032 }
4033 else
4034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004036 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004037
4038 /*
4039 * This function is used by the :cfile, :cgetfile and :caddfile
4040 * commands.
4041 * :cfile always creates a new quickfix list and jumps to the
4042 * first error.
4043 * :cgetfile creates a new quickfix list but doesn't jump to the
4044 * first error.
4045 * :caddfile adds to an existing quickfix list. If there is no
4046 * quickfix list then a new list is created.
4047 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004048 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004049 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004050 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004051 && (eap->cmdidx == CMD_cfile
4052 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004053 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004054#ifdef FEAT_AUTOCMD
4055 if (au_name != NULL)
4056 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4057#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004058 if (wp != NULL)
4059 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004060 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004061 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004062
4063 else
4064 {
4065#ifdef FEAT_AUTOCMD
4066 if (au_name != NULL)
4067 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
4068#endif
4069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070}
4071
4072/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004073 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004074 * ":vimgrepadd {pattern} file(s)"
4075 * ":lvimgrep {pattern} file(s)"
4076 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004077 */
4078 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004079ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004080{
Bram Moolenaar81695252004-12-29 20:58:21 +00004081 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004082 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004083 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004084 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004085 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004086 char_u *s;
4087 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004088 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004089 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004090#ifdef FEAT_AUTOCMD
4091 qfline_T *cur_qf_start;
4092#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004093 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004094 buf_T *buf;
4095 int duplicate_name = FALSE;
4096 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004097 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004098 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004099 buf_T *first_match_buf = NULL;
4100 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004101 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004102#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4103 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004104#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004105 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004106 int flags = 0;
4107 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004108 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004109 char_u *dirname_start = NULL;
4110 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004111 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004112#ifdef FEAT_AUTOCMD
4113 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004114
4115 switch (eap->cmdidx)
4116 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004117 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4118 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4119 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004120 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004121 case CMD_grep: au_name = (char_u *)"grep"; break;
4122 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4123 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4124 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004125 default: break;
4126 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004127 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4128 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004129 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004130# ifdef FEAT_EVAL
4131 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004132 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004133# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004134 }
4135#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004136
Bram Moolenaar754b5602006-02-09 23:53:20 +00004137 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004138 || eap->cmdidx == CMD_lvimgrep
4139 || eap->cmdidx == CMD_lgrepadd
4140 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004141 {
4142 qi = ll_get_or_alloc_list(curwin);
4143 if (qi == NULL)
4144 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004145 }
4146
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004147 if (eap->addr_count > 0)
4148 tomatch = eap->line2;
4149 else
4150 tomatch = MAXLNUM;
4151
Bram Moolenaar81695252004-12-29 20:58:21 +00004152 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004153 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004154 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004155 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004156 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004157 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004158 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004159 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004160 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004161
4162 if (s != NULL && *s == NUL)
4163 {
4164 /* Pattern is empty, use last search pattern. */
4165 if (last_search_pat() == NULL)
4166 {
4167 EMSG(_(e_noprevre));
4168 goto theend;
4169 }
4170 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4171 }
4172 else
4173 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4174
Bram Moolenaar86b68352004-12-27 21:59:20 +00004175 if (regmatch.regprog == NULL)
4176 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004177 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004178 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004179
4180 p = skipwhite(p);
4181 if (*p == NUL)
4182 {
4183 EMSG(_("E683: File name missing or invalid pattern"));
4184 goto theend;
4185 }
4186
Bram Moolenaar55b69262017-08-13 13:42:01 +02004187 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4188 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004189 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004190 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004191 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004192
4193 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004194 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004195 goto theend;
4196 if (fcount == 0)
4197 {
4198 EMSG(_(e_nomatch));
4199 goto theend;
4200 }
4201
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004202 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4203 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004204 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004205 {
4206 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004207 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004208 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004209
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004210 /* Remember the current directory, because a BufRead autocommand that does
4211 * ":lcd %:p:h" changes the meaning of short path names. */
4212 mch_dirname(dirname_start, MAXPATHL);
4213
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004214#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004215 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004216 * changing the current quickfix list. */
4217 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4218#endif
4219
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004220 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004221 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004222 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004223 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004224 if (time(NULL) > seconds)
4225 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004226 /* Display the file name every second or so, show the user we are
4227 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004228 seconds = time(NULL);
4229 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004230 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004231 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004232 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004233 else
4234 {
4235 msg_outtrans(p);
4236 vim_free(p);
4237 }
4238 msg_clr_eos();
4239 msg_didout = FALSE; /* overwrite this message */
4240 msg_nowait = TRUE; /* don't wait for this message */
4241 msg_col = 0;
4242 out_flush();
4243 }
4244
Bram Moolenaar81695252004-12-29 20:58:21 +00004245 buf = buflist_findname_exp(fnames[fi]);
4246 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4247 {
4248 /* Remember that a buffer with this name already exists. */
4249 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004250 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004251 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004252
4253#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4254 /* Don't do Filetype autocommands to avoid loading syntax and
4255 * indent scripts, a great speed improvement. */
4256 save_ei = au_event_disable(",Filetype");
4257#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004258 /* Don't use modelines here, it's useless. */
4259 save_mls = p_mls;
4260 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004261
4262 /* Load file into a buffer, so that 'fileencoding' is detected,
4263 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004264 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004265
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004266 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004267#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4268 au_event_restore(save_ei);
4269#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004270 }
4271 else
4272 /* Use existing, loaded buffer. */
4273 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004274
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004275#ifdef FEAT_AUTOCMD
4276 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4277 {
4278 int idx;
4279
4280 /* Autocommands changed the quickfix list. Find the one we were
4281 * using and restore it. */
4282 for (idx = 0; idx < LISTCOUNT; ++idx)
4283 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4284 {
4285 qi->qf_curlist = idx;
4286 break;
4287 }
4288 if (idx == LISTCOUNT)
4289 {
4290 /* List cannot be found, create a new one. */
4291 qf_new_list(qi, *eap->cmdlinep);
4292 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4293 }
4294 }
4295#endif
4296
Bram Moolenaar81695252004-12-29 20:58:21 +00004297 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004298 {
4299 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004300 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004301 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004302 else
4303 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004304 /* Try for a match in all lines of the buffer.
4305 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004306 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004307 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4308 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004309 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004310 col = 0;
4311 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004312 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004313 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004314 /* Pass the buffer number so that it gets used even for a
4315 * dummy buffer, unless duplicate_name is set, then the
4316 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004317 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004318 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004319 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004320 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004321 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004322 ml_get_buf(buf,
4323 regmatch.startpos[0].lnum + lnum, FALSE),
4324 regmatch.startpos[0].lnum + lnum,
4325 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004326 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004327 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004328 0, /* nr */
4329 0, /* type */
4330 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004331 ) == FAIL)
4332 {
4333 got_int = TRUE;
4334 break;
4335 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004336 found_match = TRUE;
4337 if (--tomatch == 0)
4338 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004339 if ((flags & VGR_GLOBAL) == 0
4340 || regmatch.endpos[0].lnum > 0)
4341 break;
4342 col = regmatch.endpos[0].col
4343 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004344 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004345 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004346 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004347 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004348 if (got_int)
4349 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004350 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004351#ifdef FEAT_AUTOCMD
4352 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4353#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004354
4355 if (using_dummy)
4356 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004357 if (found_match && first_match_buf == NULL)
4358 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004359 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004360 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004361 /* Never keep a dummy buffer if there is another buffer
4362 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004363 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004364 buf = NULL;
4365 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004366 else if (!cmdmod.hide
4367 || buf->b_p_bh[0] == 'u' /* "unload" */
4368 || buf->b_p_bh[0] == 'w' /* "wipe" */
4369 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004370 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004371 /* When no match was found we don't need to remember the
4372 * buffer, wipe it out. If there was a match and it
4373 * wasn't the first one or we won't jump there: only
4374 * unload the buffer.
4375 * Ignore 'hidden' here, because it may lead to having too
4376 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004377 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004378 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004379 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004380 buf = NULL;
4381 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004382 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004383 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004384 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004385 /* Keeping the buffer, remove the dummy flag. */
4386 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004387 buf = NULL;
4388 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004389 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004390
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004391 if (buf != NULL)
4392 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004393 /* Keeping the buffer, remove the dummy flag. */
4394 buf->b_flags &= ~BF_DUMMY;
4395
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004396 /* If the buffer is still loaded we need to use the
4397 * directory we jumped to below. */
4398 if (buf == first_match_buf
4399 && target_dir == NULL
4400 && STRCMP(dirname_start, dirname_now) != 0)
4401 target_dir = vim_strsave(dirname_now);
4402
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004403 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004404 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004405 * need to be done (again). But not the window-local
4406 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004407 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004408#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004409 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4410 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004411#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004412 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004413 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004414 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004415 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004416 }
4417 }
4418
4419 FreeWild(fcount, fnames);
4420
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004421 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4422 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4423 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004424
4425#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004426 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004427#endif
4428
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004429#ifdef FEAT_AUTOCMD
4430 if (au_name != NULL)
4431 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4432 curbuf->b_fname, TRUE, curbuf);
4433#endif
4434
Bram Moolenaar86b68352004-12-27 21:59:20 +00004435 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004436 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004437 {
4438 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004439 {
4440 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004441 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004442 if (buf != curbuf)
4443 /* If we jumped to another buffer redrawing will already be
4444 * taken care of. */
4445 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004446
4447 /* Jump to the directory used after loading the buffer. */
4448 if (curbuf == first_match_buf && target_dir != NULL)
4449 {
4450 exarg_T ea;
4451
4452 ea.arg = target_dir;
4453 ea.cmdidx = CMD_lcd;
4454 ex_cd(&ea);
4455 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004456 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004457 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004458 else
4459 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004460
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004461 /* If we loaded a dummy buffer into the current window, the autocommands
4462 * may have messed up things, need to redraw and recompute folds. */
4463 if (redraw_for_dummy)
4464 {
4465#ifdef FEAT_FOLDING
4466 foldUpdateAll(curwin);
4467#else
4468 redraw_later(NOT_VALID);
4469#endif
4470 }
4471
Bram Moolenaar86b68352004-12-27 21:59:20 +00004472theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004473 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004474 vim_free(dirname_now);
4475 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004476 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004477 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004478}
4479
4480/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004481 * Restore current working directory to "dirname_start" if they differ, taking
4482 * into account whether it is set locally or globally.
4483 */
4484 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004485restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004486{
4487 char_u *dirname_now = alloc(MAXPATHL);
4488
4489 if (NULL != dirname_now)
4490 {
4491 mch_dirname(dirname_now, MAXPATHL);
4492 if (STRCMP(dirname_start, dirname_now) != 0)
4493 {
4494 /* If the directory has changed, change it back by building up an
4495 * appropriate ex command and executing it. */
4496 exarg_T ea;
4497
4498 ea.arg = dirname_start;
4499 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4500 ex_cd(&ea);
4501 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004502 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004503 }
4504}
4505
4506/*
4507 * Load file "fname" into a dummy buffer and return the buffer pointer,
4508 * placing the directory resulting from the buffer load into the
4509 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4510 * prior to calling this function. Restores directory to "dirname_start" prior
4511 * to returning, if autocmds or the 'autochdir' option have changed it.
4512 *
4513 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4514 * or wipe_dummy_buffer() later!
4515 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004516 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004517 */
4518 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004519load_dummy_buffer(
4520 char_u *fname,
4521 char_u *dirname_start, /* in: old directory */
4522 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004523{
4524 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004525 bufref_T newbufref;
4526 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004527 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004528 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004529
4530 /* Allocate a buffer without putting it in the buffer list. */
4531 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4532 if (newbuf == NULL)
4533 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004534 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004535
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004536 /* Init the options. */
4537 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4538
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004539 /* need to open the memfile before putting the buffer in a window */
4540 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004541 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004542 /* set curwin/curbuf to buf and save a few things */
4543 aucmd_prepbuf(&aco, newbuf);
4544
4545 /* Need to set the filename for autocommands. */
4546 (void)setfname(curbuf, fname, NULL, FALSE);
4547
Bram Moolenaar81695252004-12-29 20:58:21 +00004548 /* Create swap file now to avoid the ATTENTION message. */
4549 check_need_swap(TRUE);
4550
4551 /* Remove the "dummy" flag, otherwise autocommands may not
4552 * work. */
4553 curbuf->b_flags &= ~BF_DUMMY;
4554
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004555 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004556 if (readfile(fname, NULL,
4557 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4558 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004559 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004560 && !(curbuf->b_flags & BF_NEW))
4561 {
4562 failed = FALSE;
4563 if (curbuf != newbuf)
4564 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004565 /* Bloody autocommands changed the buffer! Can happen when
4566 * using netrw and editing a remote file. Use the current
4567 * buffer instead, delete the dummy one after restoring the
4568 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004569 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004570 newbuf = curbuf;
4571 }
4572 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004573
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004574 /* restore curwin/curbuf and a few other things */
4575 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004576 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4577 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004578
4579 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4580 * skip it. */
4581 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004582 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004583
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004584 /*
4585 * When autocommands/'autochdir' option changed directory: go back.
4586 * Let the caller know what the resulting dir was first, in case it is
4587 * important.
4588 */
4589 mch_dirname(resulting_dir, MAXPATHL);
4590 restore_start_dir(dirname_start);
4591
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004592 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004593 return NULL;
4594 if (failed)
4595 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004596 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004597 return NULL;
4598 }
4599 return newbuf;
4600}
4601
4602/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004603 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4604 * directory to "dirname_start" prior to returning, if autocmds or the
4605 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004606 */
4607 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004608wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004609{
4610 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004611 {
4612#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4613 cleanup_T cs;
4614
4615 /* Reset the error/interrupt/exception state here so that aborting()
4616 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4617 * work when got_int is set. */
4618 enter_cleanup(&cs);
4619#endif
4620
Bram Moolenaar81695252004-12-29 20:58:21 +00004621 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004622
4623#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4624 /* Restore the error/interrupt/exception state if not discarded by a
4625 * new aborting error, interrupt, or uncaught exception. */
4626 leave_cleanup(&cs);
4627#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004628 /* When autocommands/'autochdir' option changed directory: go back. */
4629 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004630 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004631}
4632
4633/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004634 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4635 * directory to "dirname_start" prior to returning, if autocmds or the
4636 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004637 */
4638 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004639unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004640{
4641 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004642 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004643 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004644
4645 /* When autocommands/'autochdir' option changed directory: go back. */
4646 restore_start_dir(dirname_start);
4647 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004648}
4649
Bram Moolenaar05159a02005-02-26 23:04:13 +00004650#if defined(FEAT_EVAL) || defined(PROTO)
4651/*
4652 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004653 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004654 */
4655 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004656get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004657{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004658 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004659 dict_T *dict;
4660 char_u buf[2];
4661 qfline_T *qfp;
4662 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004663 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004664
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004665 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004666 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004667 qi = &ql_info;
4668 if (wp != NULL)
4669 {
4670 qi = GET_LOC_LIST(wp);
4671 if (qi == NULL)
4672 return FAIL;
4673 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004674 }
4675
Bram Moolenaard823fa92016-08-12 16:29:27 +02004676 if (qf_idx == -1)
4677 qf_idx = qi->qf_curlist;
4678
4679 if (qf_idx >= qi->qf_listcount
4680 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004681 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004682
Bram Moolenaard823fa92016-08-12 16:29:27 +02004683 qfp = qi->qf_lists[qf_idx].qf_start;
4684 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004685 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004686 /* Handle entries with a non-existing buffer number. */
4687 bufnum = qfp->qf_fnum;
4688 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4689 bufnum = 0;
4690
Bram Moolenaar05159a02005-02-26 23:04:13 +00004691 if ((dict = dict_alloc()) == NULL)
4692 return FAIL;
4693 if (list_append_dict(list, dict) == FAIL)
4694 return FAIL;
4695
4696 buf[0] = qfp->qf_type;
4697 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004698 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004699 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4700 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4701 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4702 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004703 || dict_add_nr_str(dict, "pattern", 0L,
4704 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4705 || dict_add_nr_str(dict, "text", 0L,
4706 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004707 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4708 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4709 return FAIL;
4710
4711 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004712 if (qfp == NULL)
4713 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004714 }
4715 return OK;
4716}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004717
4718/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004719 * Flags used by getqflist()/getloclist() to determine which fields to return.
4720 */
4721enum {
4722 QF_GETLIST_NONE = 0x0,
4723 QF_GETLIST_TITLE = 0x1,
4724 QF_GETLIST_ITEMS = 0x2,
4725 QF_GETLIST_NR = 0x4,
4726 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004727 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004728 QF_GETLIST_ID = 0x20,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004729 QF_GETLIST_ALL = 0xFF
4730};
4731
4732/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004733 * Parse text from 'di' and return the quickfix list items
4734 */
4735 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004736qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004737{
4738 int status = FAIL;
4739 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004740 char_u *errorformat = p_efm;
4741 dictitem_T *efm_di;
4742 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004743
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004744 /* Only a List value is supported */
4745 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004746 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004747 /* If errorformat is supplied then use it, otherwise use the 'efm'
4748 * option setting
4749 */
4750 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4751 {
4752 if (efm_di->di_tv.v_type != VAR_STRING ||
4753 efm_di->di_tv.vval.v_string == NULL)
4754 return FAIL;
4755 errorformat = efm_di->di_tv.vval.v_string;
4756 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004757
Bram Moolenaar36538222017-09-02 19:51:44 +02004758 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004759 if (l == NULL)
4760 return FAIL;
4761
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004762 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4763 if (qi != NULL)
4764 {
4765 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4766 qi->qf_refcount++;
4767
Bram Moolenaar36538222017-09-02 19:51:44 +02004768 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004769 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4770 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004771 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004772 qf_free(qi, 0);
4773 }
4774 free(qi);
4775 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004776 dict_add_list(retdict, "items", l);
4777 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004778 }
4779
4780 return status;
4781}
4782
4783/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004784 * Return the quickfix/location list number with the given identifier.
4785 * Returns -1 if list is not found.
4786 */
4787 static int
4788qf_id2nr(qf_info_T *qi, int_u qfid)
4789{
4790 int qf_idx;
4791
4792 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4793 if (qi->qf_lists[qf_idx].qf_id == qfid)
4794 return qf_idx;
4795 return -1;
4796}
4797
4798/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004799 * Return quickfix/location list details (title) as a
4800 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4801 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004802 */
4803 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004804qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004805{
4806 qf_info_T *qi = &ql_info;
4807 int status = OK;
4808 int qf_idx;
4809 dictitem_T *di;
4810 int flags = QF_GETLIST_NONE;
4811
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004812 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004813 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004814
Bram Moolenaard823fa92016-08-12 16:29:27 +02004815 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004816 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004817
4818 /* List is not present or is empty */
4819 if (qi == NULL || qi->qf_listcount == 0)
4820 {
4821 /* If querying for the size of the list, return 0 */
4822 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4823 && (di->di_tv.v_type == VAR_STRING)
4824 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4825 return dict_add_nr_str(retdict, "nr", 0, NULL);
4826 return FAIL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004827 }
4828
4829 qf_idx = qi->qf_curlist; /* default is the current list */
4830 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4831 {
4832 /* Use the specified quickfix/location list */
4833 if (di->di_tv.v_type == VAR_NUMBER)
4834 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004835 /* for zero use the current list */
4836 if (di->di_tv.vval.v_number != 0)
4837 {
4838 qf_idx = di->di_tv.vval.v_number - 1;
4839 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4840 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02004841 }
Bram Moolenaar55b69262017-08-13 13:42:01 +02004842 }
4843 else if ((di->di_tv.v_type == VAR_STRING)
4844 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004845 /* Get the last quickfix list number */
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004846 qf_idx = qi->qf_listcount - 1;
4847 else
4848 return FAIL;
4849 flags |= QF_GETLIST_NR;
4850 }
4851
4852 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
4853 {
4854 /* Look for a list with the specified id */
4855 if (di->di_tv.v_type == VAR_NUMBER)
4856 {
4857 /* For zero, use the current list or the list specifed by 'nr' */
4858 if (di->di_tv.vval.v_number != 0)
4859 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004860 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
4861 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004862 return FAIL; /* List not found */
4863 }
4864 flags |= QF_GETLIST_ID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004865 }
4866 else
4867 return FAIL;
4868 }
4869
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004870 if (dict_find(what, (char_u *)"all", -1) != NULL)
4871 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004872
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004873 if (dict_find(what, (char_u *)"title", -1) != NULL)
4874 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004875
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004876 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4877 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004878
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004879 if (dict_find(what, (char_u *)"context", -1) != NULL)
4880 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004881
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004882 if (dict_find(what, (char_u *)"items", -1) != NULL)
4883 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004884
Bram Moolenaard823fa92016-08-12 16:29:27 +02004885 if (flags & QF_GETLIST_TITLE)
4886 {
4887 char_u *t;
4888 t = qi->qf_lists[qf_idx].qf_title;
4889 if (t == NULL)
4890 t = (char_u *)"";
4891 status = dict_add_nr_str(retdict, "title", 0L, t);
4892 }
4893 if ((status == OK) && (flags & QF_GETLIST_NR))
4894 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4895 if ((status == OK) && (flags & QF_GETLIST_WINID))
4896 {
4897 win_T *win;
4898 win = qf_find_win(qi);
4899 if (win != NULL)
4900 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4901 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004902 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4903 {
4904 list_T *l = list_alloc();
4905 if (l != NULL)
4906 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004907 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004908 dict_add_list(retdict, "items", l);
4909 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004910 else
4911 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004912 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004913
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004914 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4915 {
4916 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4917 {
4918 di = dictitem_alloc((char_u *)"context");
4919 if (di != NULL)
4920 {
4921 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004922 status = dict_add(retdict, di);
4923 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004924 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004925 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004926 else
4927 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004928 }
4929 else
4930 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4931 }
4932
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004933 if ((status == OK) && (flags & QF_GETLIST_ID))
4934 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
4935 NULL);
4936
Bram Moolenaard823fa92016-08-12 16:29:27 +02004937 return status;
4938}
4939
4940/*
4941 * Add list of entries to quickfix/location list. Each list entry is
4942 * a dictionary with item information.
4943 */
4944 static int
4945qf_add_entries(
4946 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004947 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004948 list_T *list,
4949 char_u *title,
4950 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004951{
4952 listitem_T *li;
4953 dict_T *d;
4954 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004955 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004956 long lnum;
4957 int col, nr;
4958 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004959#ifdef FEAT_WINDOWS
4960 qfline_T *old_last = NULL;
4961#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004962 int valid, status;
4963 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004964 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004965
Bram Moolenaara3921f42017-06-04 15:30:34 +02004966 if (action == ' ' || qf_idx == qi->qf_listcount)
4967 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004968 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004969 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004970 qf_idx = qi->qf_curlist;
4971 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004972#ifdef FEAT_WINDOWS
Bram Moolenaara3921f42017-06-04 15:30:34 +02004973 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004974 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004975 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004976#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004977 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004978 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004979 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004980 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004981 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004982
4983 for (li = list->lv_first; li != NULL; li = li->li_next)
4984 {
4985 if (li->li_tv.v_type != VAR_DICT)
4986 continue; /* Skip non-dict items */
4987
4988 d = li->li_tv.vval.v_dict;
4989 if (d == NULL)
4990 continue;
4991
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004992 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004993 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4994 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4995 col = (int)get_dict_number(d, (char_u *)"col");
4996 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4997 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004998 type = get_dict_string(d, (char_u *)"type", TRUE);
4999 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5000 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005001 if (text == NULL)
5002 text = vim_strsave((char_u *)"");
5003
5004 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005005 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005006 valid = FALSE;
5007
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005008 /* Mark entries with non-existing buffer number as not valid. Give the
5009 * error message only once. */
5010 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5011 {
5012 if (!did_bufnr_emsg)
5013 {
5014 did_bufnr_emsg = TRUE;
5015 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5016 }
5017 valid = FALSE;
5018 bufnum = 0;
5019 }
5020
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005021 /* If the 'valid' field is present it overrules the detected value. */
5022 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5023 valid = (int)get_dict_number(d, (char_u *)"valid");
5024
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005025 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005026 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005027 NULL, /* dir */
5028 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005029 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005030 text,
5031 lnum,
5032 col,
5033 vcol, /* vis_col */
5034 pattern, /* search pattern */
5035 nr,
5036 type == NULL ? NUL : *type,
5037 valid);
5038
5039 vim_free(filename);
5040 vim_free(pattern);
5041 vim_free(text);
5042 vim_free(type);
5043
5044 if (status == FAIL)
5045 {
5046 retval = FAIL;
5047 break;
5048 }
5049 }
5050
Bram Moolenaara3921f42017-06-04 15:30:34 +02005051 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005052 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005053 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005054 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005055 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005056 if (action != 'a')
5057 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005058 qi->qf_lists[qf_idx].qf_ptr =
5059 qi->qf_lists[qf_idx].qf_start;
5060 if (qi->qf_lists[qf_idx].qf_count > 0)
5061 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005062 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005063
5064#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005065 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005066 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005067#endif
5068
5069 return retval;
5070}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005071
5072 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005073qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005074{
5075 dictitem_T *di;
5076 int retval = FAIL;
5077 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005078 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005079 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005080
5081 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5082 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005083
5084 qf_idx = qi->qf_curlist; /* default is the current list */
5085 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5086 {
5087 /* Use the specified quickfix/location list */
5088 if (di->di_tv.v_type == VAR_NUMBER)
5089 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005090 /* for zero use the current list */
5091 if (di->di_tv.vval.v_number != 0)
5092 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005093
Bram Moolenaar55b69262017-08-13 13:42:01 +02005094 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5095 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005096 /*
5097 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005098 * non-available list and add the new list at the end of the
5099 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005100 */
5101 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005102 qf_idx = qi->qf_listcount - 1;
5103 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005104 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005105 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005106 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005107 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005108 }
5109 else if (di->di_tv.v_type == VAR_STRING
5110 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005111 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005112 if (qi->qf_listcount > 0)
5113 qf_idx = qi->qf_listcount - 1;
5114 else if (newlist)
5115 qf_idx = 0;
5116 else
5117 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005118 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005119 else
5120 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005121 }
5122
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005123 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5124 {
5125 /* Use the quickfix/location list with the specified id */
5126 if (di->di_tv.v_type == VAR_NUMBER)
5127 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005128 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5129 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005130 return FAIL; /* List not found */
5131 }
5132 else
5133 return FAIL;
5134 }
5135
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005136 if (newlist)
5137 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005138 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005139 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005140 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005141 }
5142
5143 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5144 {
5145 if (di->di_tv.v_type == VAR_STRING)
5146 {
5147 vim_free(qi->qf_lists[qf_idx].qf_title);
5148 qi->qf_lists[qf_idx].qf_title =
5149 get_dict_string(what, (char_u *)"title", TRUE);
5150 if (qf_idx == qi->qf_curlist)
5151 qf_update_win_titlevar(qi);
5152 retval = OK;
5153 }
5154 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005155
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005156 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5157 {
5158 if (di->di_tv.v_type == VAR_LIST)
5159 {
5160 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5161
5162 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5163 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005164 if (action == 'r')
5165 {
5166 /*
5167 * When replacing the quickfix list entries using
5168 * qf_add_entries(), the title is set with a ':' prefix.
5169 * Restore the title with the saved title.
5170 */
5171 vim_free(qi->qf_lists[qf_idx].qf_title);
5172 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5173 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005174 vim_free(title_save);
5175 }
5176 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005177
Bram Moolenaar36538222017-09-02 19:51:44 +02005178 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5179 {
5180 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5181 return FAIL;
5182 errorformat = di->di_tv.vval.v_string;
5183 }
5184
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005185 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005186 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005187 /* Only a List value is supported */
5188 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005189 {
5190 if (action == 'r')
5191 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005192 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005193 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5194 retval = OK;
5195 }
5196 else
5197 return FAIL;
5198 }
5199
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005200 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5201 {
5202 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005203
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005204 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005205 ctx = alloc_tv();
5206 if (ctx != NULL)
5207 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005208 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005209 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005210 }
5211
Bram Moolenaard823fa92016-08-12 16:29:27 +02005212 return retval;
5213}
5214
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005215/*
5216 * Find the non-location list window with the specified location list.
5217 */
5218 static win_T *
5219find_win_with_ll(qf_info_T *qi)
5220{
5221 win_T *wp = NULL;
5222
5223 FOR_ALL_WINDOWS(wp)
5224 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5225 return wp;
5226
5227 return NULL;
5228}
5229
5230/*
5231 * Free the entire quickfix/location list stack.
5232 * If the quickfix/location list window is open, then clear it.
5233 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005234 static void
5235qf_free_stack(win_T *wp, qf_info_T *qi)
5236{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005237 win_T *qfwin = qf_find_win(qi);
5238 win_T *llwin = NULL;
5239 win_T *orig_wp = wp;
5240
5241 if (qfwin != NULL)
5242 {
5243 /* If the quickfix/location list window is open, then clear it */
5244 if (qi->qf_curlist < qi->qf_listcount)
5245 qf_free(qi, qi->qf_curlist);
5246 qf_update_buffer(qi, NULL);
5247 }
5248
5249 if (wp != NULL && IS_LL_WINDOW(wp))
5250 {
5251 /* If in the location list window, then use the non-location list
5252 * window with this location list (if present)
5253 */
5254 llwin = find_win_with_ll(qi);
5255 if (llwin != NULL)
5256 wp = llwin;
5257 }
5258
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005259 qf_free_all(wp);
5260 if (wp == NULL)
5261 {
5262 /* quickfix list */
5263 qi->qf_curlist = 0;
5264 qi->qf_listcount = 0;
5265 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005266 else if (IS_LL_WINDOW(orig_wp))
5267 {
5268 /* If the location list window is open, then create a new empty
5269 * location list */
5270 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005271
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005272 /* first free the list reference in the location list window */
5273 ll_free_all(&orig_wp->w_llist_ref);
5274
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005275 orig_wp->w_llist_ref = new_ll;
5276 if (llwin != NULL)
5277 {
5278 llwin->w_llist = new_ll;
5279 new_ll->qf_refcount++;
5280 }
5281 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005282}
5283
Bram Moolenaard823fa92016-08-12 16:29:27 +02005284/*
5285 * Populate the quickfix list with the items supplied in the list
5286 * of dictionaries. "title" will be copied to w:quickfix_title.
5287 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5288 */
5289 int
5290set_errorlist(
5291 win_T *wp,
5292 list_T *list,
5293 int action,
5294 char_u *title,
5295 dict_T *what)
5296{
5297 qf_info_T *qi = &ql_info;
5298 int retval = OK;
5299
5300 if (wp != NULL)
5301 {
5302 qi = ll_get_or_alloc_list(wp);
5303 if (qi == NULL)
5304 return FAIL;
5305 }
5306
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005307 if (action == 'f')
5308 {
5309 /* Free the entire quickfix or location list stack */
5310 qf_free_stack(wp, qi);
5311 }
5312 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005313 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005314 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005315 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005316
5317 return retval;
5318}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005319
5320 static int
5321mark_quickfix_ctx(qf_info_T *qi, int copyID)
5322{
5323 int i;
5324 int abort = FALSE;
5325 typval_T *ctx;
5326
5327 for (i = 0; i < LISTCOUNT && !abort; ++i)
5328 {
5329 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005330 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5331 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005332 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5333 }
5334
5335 return abort;
5336}
5337
5338/*
5339 * Mark the context of the quickfix list and the location lists (if present) as
5340 * "in use". So that garabage collection doesn't free the context.
5341 */
5342 int
5343set_ref_in_quickfix(int copyID)
5344{
5345 int abort = FALSE;
5346 tabpage_T *tp;
5347 win_T *win;
5348
5349 abort = mark_quickfix_ctx(&ql_info, copyID);
5350 if (abort)
5351 return abort;
5352
5353 FOR_ALL_TAB_WINDOWS(tp, win)
5354 {
5355 if (win->w_llist != NULL)
5356 {
5357 abort = mark_quickfix_ctx(win->w_llist, copyID);
5358 if (abort)
5359 return abort;
5360 }
5361 }
5362
5363 return abort;
5364}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005365#endif
5366
Bram Moolenaar81695252004-12-29 20:58:21 +00005367/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005368 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005369 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005370 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005371 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005372 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005373 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005374 */
5375 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005376ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005377{
5378 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005379 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005380#ifdef FEAT_AUTOCMD
5381 char_u *au_name = NULL;
5382#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005383
Bram Moolenaardb552d602006-03-23 22:59:57 +00005384 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5385 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005386 {
5387 qi = ll_get_or_alloc_list(curwin);
5388 if (qi == NULL)
5389 return;
5390 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005391
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005392#ifdef FEAT_AUTOCMD
5393 switch (eap->cmdidx)
5394 {
5395 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5396 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5397 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5398 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5399 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5400 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5401 default: break;
5402 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005403 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5404 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005405 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005406# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005407 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005408 return;
5409# endif
5410 }
5411#endif
5412
Bram Moolenaar86b68352004-12-27 21:59:20 +00005413 if (*eap->arg == NUL)
5414 buf = curbuf;
5415 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5416 buf = buflist_findnr(atoi((char *)eap->arg));
5417 if (buf == NULL)
5418 EMSG(_(e_invarg));
5419 else if (buf->b_ml.ml_mfp == NULL)
5420 EMSG(_("E681: Buffer is not loaded"));
5421 else
5422 {
5423 if (eap->addr_count == 0)
5424 {
5425 eap->line1 = 1;
5426 eap->line2 = buf->b_ml.ml_line_count;
5427 }
5428 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5429 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5430 EMSG(_(e_invrange));
5431 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005432 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005433 char_u *qf_title = *eap->cmdlinep;
5434
5435 if (buf->b_sfname)
5436 {
5437 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5438 (char *)qf_title, (char *)buf->b_sfname);
5439 qf_title = IObuff;
5440 }
5441
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005442 if (qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005443 (eap->cmdidx != CMD_caddbuffer
5444 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005445 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005446 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005447 {
5448#ifdef FEAT_AUTOCMD
5449 if (au_name != NULL)
5450 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5451 curbuf->b_fname, TRUE, curbuf);
5452#endif
5453 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5454 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5455 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005456 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005457 }
5458}
5459
Bram Moolenaar1e015462005-09-25 22:16:38 +00005460#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005461/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005462 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5463 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005464 */
5465 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005466ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005467{
5468 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005469 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005470#ifdef FEAT_AUTOCMD
5471 char_u *au_name = NULL;
5472#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005473
Bram Moolenaardb552d602006-03-23 22:59:57 +00005474 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5475 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005476 {
5477 qi = ll_get_or_alloc_list(curwin);
5478 if (qi == NULL)
5479 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005480 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005481
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005482#ifdef FEAT_AUTOCMD
5483 switch (eap->cmdidx)
5484 {
5485 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5486 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5487 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5488 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5489 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5490 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5491 default: break;
5492 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005493 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5494 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005495 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005496# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005497 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005498 return;
5499# endif
5500 }
5501#endif
5502
Bram Moolenaar4770d092006-01-12 23:22:24 +00005503 /* Evaluate the expression. When the result is a string or a list we can
5504 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005505 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005506 if (tv != NULL)
5507 {
5508 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5509 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5510 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02005511 if (qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005512 (eap->cmdidx != CMD_caddexpr
5513 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005514 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5515 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005516 {
5517#ifdef FEAT_AUTOCMD
5518 if (au_name != NULL)
5519 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5520 curbuf->b_fname, TRUE, curbuf);
5521#endif
5522 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5523 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5524 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005525 }
5526 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005527 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005528 free_tv(tv);
5529 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005530}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005531#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005532
5533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 * ":helpgrep {pattern}"
5535 */
5536 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005537ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 regmatch_T regmatch;
5540 char_u *save_cpo;
5541 char_u *p;
5542 int fcount;
5543 char_u **fnames;
5544 FILE *fd;
5545 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005547#ifdef FEAT_MULTI_LANG
5548 char_u *lang;
5549#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005550 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005551 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005552 int new_qi = FALSE;
5553 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005554#ifdef FEAT_AUTOCMD
5555 char_u *au_name = NULL;
5556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005558#ifdef FEAT_MULTI_LANG
5559 /* Check for a specified language */
5560 lang = check_help_lang(eap->arg);
5561#endif
5562
Bram Moolenaar73633f82012-01-20 13:39:07 +01005563#ifdef FEAT_AUTOCMD
5564 switch (eap->cmdidx)
5565 {
5566 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5567 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5568 default: break;
5569 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005570 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5571 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005572 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005573# ifdef FEAT_EVAL
5574 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005575 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005576# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005577 }
5578#endif
5579
5580 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5581 save_cpo = p_cpo;
5582 p_cpo = empty_option;
5583
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005584 if (eap->cmdidx == CMD_lhelpgrep)
5585 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005586 /* If the current window is a help window, then use it */
5587 if (bt_help(curwin->w_buffer))
5588 wp = curwin;
5589 else
5590 /* Find an existing help window */
5591 FOR_ALL_WINDOWS(wp)
5592 if (bt_help(wp->w_buffer))
5593 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005594
5595 if (wp == NULL) /* Help window not found */
5596 qi = NULL;
5597 else
5598 qi = wp->w_llist;
5599
5600 if (qi == NULL)
5601 {
5602 /* Allocate a new location list for help text matches */
5603 if ((qi = ll_new_list()) == NULL)
5604 return;
5605 new_qi = TRUE;
5606 }
5607 }
5608
Bram Moolenaaree85df32017-03-19 14:19:50 +01005609 /* Autocommands may change the list. Save it for later comparison */
5610 save_qi = qi;
5611
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5613 regmatch.rm_ic = FALSE;
5614 if (regmatch.regprog != NULL)
5615 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005616#ifdef FEAT_MBYTE
5617 vimconv_T vc;
5618
5619 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5620 * differs. */
5621 vc.vc_type = CONV_NONE;
5622 if (!enc_utf8)
5623 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5624#endif
5625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005627 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
5629 /* Go through all directories in 'runtimepath' */
5630 p = p_rtp;
5631 while (*p != NUL && !got_int)
5632 {
5633 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5634
5635 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5636 add_pathsep(NameBuff);
5637 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5638 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5639 &fnames, EW_FILE|EW_SILENT) == OK
5640 && fcount > 0)
5641 {
5642 for (fi = 0; fi < fcount && !got_int; ++fi)
5643 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005644#ifdef FEAT_MULTI_LANG
5645 /* Skip files for a different language. */
5646 if (lang != NULL
5647 && STRNICMP(lang, fnames[fi]
5648 + STRLEN(fnames[fi]) - 3, 2) != 0
5649 && !(STRNICMP(lang, "en", 2) == 0
5650 && STRNICMP("txt", fnames[fi]
5651 + STRLEN(fnames[fi]) - 3, 3) == 0))
5652 continue;
5653#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005654 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 if (fd != NULL)
5656 {
5657 lnum = 1;
5658 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5659 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005660 char_u *line = IObuff;
5661#ifdef FEAT_MBYTE
5662 /* Convert a line if 'encoding' is not utf-8 and
5663 * the line contains a non-ASCII character. */
5664 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005665 && has_non_ascii(IObuff))
5666 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005667 line = string_convert(&vc, IObuff, NULL);
5668 if (line == NULL)
5669 line = IObuff;
5670 }
5671#endif
5672
5673 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005675 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676
5677 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005678 while (l > 0 && line[l - 1] <= ' ')
5679 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005681 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005682 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 NULL, /* dir */
5684 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005685 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005686 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005688 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005689 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005690 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005691 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 0, /* nr */
5693 1, /* type */
5694 TRUE /* valid */
5695 ) == FAIL)
5696 {
5697 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005698#ifdef FEAT_MBYTE
5699 if (line != IObuff)
5700 vim_free(line);
5701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 break;
5703 }
5704 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005705#ifdef FEAT_MBYTE
5706 if (line != IObuff)
5707 vim_free(line);
5708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 ++lnum;
5710 line_breakcheck();
5711 }
5712 fclose(fd);
5713 }
5714 }
5715 FreeWild(fcount, fnames);
5716 }
5717 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005718
Bram Moolenaar473de612013-06-08 18:19:48 +02005719 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005720#ifdef FEAT_MBYTE
5721 if (vc.vc_type != CONV_NONE)
5722 convert_setup(&vc, NULL, NULL);
5723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005725 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5726 qi->qf_lists[qi->qf_curlist].qf_ptr =
5727 qi->qf_lists[qi->qf_curlist].qf_start;
5728 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005731 if (p_cpo == empty_option)
5732 p_cpo = save_cpo;
5733 else
5734 /* Darn, some plugin changed the value. */
5735 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005738 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739#endif
5740
Bram Moolenaar73633f82012-01-20 13:39:07 +01005741#ifdef FEAT_AUTOCMD
5742 if (au_name != NULL)
5743 {
5744 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5745 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005746 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005747 /* autocommands made "qi" invalid */
5748 return;
5749 }
5750#endif
5751
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005753 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005754 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005755 else
5756 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005757
5758 if (eap->cmdidx == CMD_lhelpgrep)
5759 {
5760 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005761 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005762 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005763 {
5764 if (new_qi)
5765 ll_free_all(&qi);
5766 }
5767 else if (curwin->w_llist == NULL)
5768 curwin->w_llist = qi;
5769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770}
5771
5772#endif /* FEAT_QUICKFIX */