blob: 631aaeaaf1302f0e785da3f6876c9506222a4671 [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 Moolenaard12f5c12006-01-25 22:10:52 +000050typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000051{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000052 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020053 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_ptr; /* pointer to the current error */
55 int qf_count; /* number of errors (0 means no error list) */
56 int qf_index; /* current index in the error list */
57 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020058 char_u *qf_title; /* title derived from the command that created
59 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000060} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaard12f5c12006-01-25 22:10:52 +000062struct qf_info_S
63{
64 /*
65 * Count of references to this list. Used only for location lists.
66 * When a location list window reference this list, qf_refcount
67 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
68 * reaches 0, the list is freed.
69 */
70 int qf_refcount;
71 int qf_listcount; /* current number of lists */
72 int qf_curlist; /* current error list */
73 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar361c8f02016-07-02 15:41:47 +020074
75 int qf_dir_curlist; /* error list for qf_dir_stack */
76 struct dir_stack_T *qf_dir_stack;
77 char_u *qf_directory;
78 struct dir_stack_T *qf_file_stack;
79 char_u *qf_currfile;
80 int qf_multiline;
81 int qf_multiignore;
82 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000083};
84
85static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaar68b76a62005-03-25 21:53:48 +000087#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89/*
90 * Structure used to hold the info of one part of 'errorformat'
91 */
Bram Moolenaar01265852006-03-20 21:50:15 +000092typedef struct efm_S efm_T;
93struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000094{
95 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000096 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
98 char_u prefix; /* prefix of this format line: */
99 /* 'D' enter directory */
100 /* 'X' leave directory */
101 /* 'A' start of multi-line message */
102 /* 'E' error message */
103 /* 'W' warning message */
104 /* 'I' informational message */
105 /* 'C' continuation line */
106 /* 'Z' end of multi-line message */
107 /* 'G' general, unspecific message */
108 /* 'P' push file (partial) message */
109 /* 'Q' pop/quit file (partial) message */
110 /* 'O' overread (partial) message */
111 char_u flags; /* additional flags given in prefix */
112 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000113 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115};
116
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100117static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
118
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100119static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title);
120static void qf_store_title(qf_info_T *qi, char_u *title);
121static void qf_new_list(qf_info_T *qi, char_u *qf_title);
122static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200123static int qf_add_entry(qf_info_T *qi, 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 +0100124static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100125static void qf_free(qf_info_T *qi, int idx);
126static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200127static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
128static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100129static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200130static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
132static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
135static int is_qf_win(win_T *win, qf_info_T *qi);
136static win_T *qf_find_win(qf_info_T *qi);
137static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200138static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200140static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static char_u *get_mef_name(void);
143static void restore_start_dir(char_u *dirname_start);
144static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
145static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
146static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
147static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000149/* Quickfix window check helper macro */
150#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
151/* Location list window check helper macro */
152#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
153/*
154 * Return location list for window 'wp'
155 * For location list window, return the referenced location list
156 */
157#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000160 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200161 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 * Return -1 for error, number of errors for success.
163 */
164 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100165qf_init(
166 win_T *wp,
167 char_u *efile,
168 char_u *errorformat,
169 int newlist, /* TRUE: start a new error list */
170 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172 qf_info_T *qi = &ql_info;
173
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000174 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000175 {
176 qi = ll_get_or_alloc_list(wp);
177 if (qi == NULL)
178 return FAIL;
179 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000180
181 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200182 (linenr_T)0, (linenr_T)0,
183 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000184}
185
186/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200187 * Maximum number of bytes allowed per line while reading a errorfile.
188 */
189#define LINE_MAXLEN 4096
190
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200191static struct fmtpattern
192{
193 char_u convchar;
194 char *pattern;
195} fmt_pat[FMT_PATTERNS] =
196 {
197 {'f', ".\\+"}, /* only used when at end */
198 {'n', "\\d\\+"},
199 {'l', "\\d\\+"},
200 {'c', "\\d\\+"},
201 {'t', "."},
202 {'m', ".\\+"},
203 {'r', ".*"},
204 {'p', "[- .]*"},
205 {'v', "\\d\\+"},
206 {'s', ".\\+"}
207 };
208
209/*
210 * Converts a 'errorformat' string to regular expression pattern
211 */
212 static int
213efm_to_regpat(
214 char_u *efm,
215 int len,
216 efm_T *fmt_ptr,
217 char_u *regpat,
218 char_u *errmsg)
219{
220 char_u *ptr;
221 char_u *efmp;
222 char_u *srcptr;
223 int round;
224 int idx = 0;
225
226 /*
227 * Build regexp pattern from current 'errorformat' option
228 */
229 ptr = regpat;
230 *ptr++ = '^';
231 round = 0;
232 for (efmp = efm; efmp < efm + len; ++efmp)
233 {
234 if (*efmp == '%')
235 {
236 ++efmp;
237 for (idx = 0; idx < FMT_PATTERNS; ++idx)
238 if (fmt_pat[idx].convchar == *efmp)
239 break;
240 if (idx < FMT_PATTERNS)
241 {
242 if (fmt_ptr->addr[idx])
243 {
244 sprintf((char *)errmsg,
245 _("E372: Too many %%%c in format string"), *efmp);
246 EMSG(errmsg);
247 return -1;
248 }
249 if ((idx
250 && idx < 6
251 && vim_strchr((char_u *)"DXOPQ",
252 fmt_ptr->prefix) != NULL)
253 || (idx == 6
254 && vim_strchr((char_u *)"OPQ",
255 fmt_ptr->prefix) == NULL))
256 {
257 sprintf((char *)errmsg,
258 _("E373: Unexpected %%%c in format string"), *efmp);
259 EMSG(errmsg);
260 return -1;
261 }
262 fmt_ptr->addr[idx] = (char_u)++round;
263 *ptr++ = '\\';
264 *ptr++ = '(';
265#ifdef BACKSLASH_IN_FILENAME
266 if (*efmp == 'f')
267 {
268 /* Also match "c:" in the file name, even when
269 * checking for a colon next: "%f:".
270 * "\%(\a:\)\=" */
271 STRCPY(ptr, "\\%(\\a:\\)\\=");
272 ptr += 10;
273 }
274#endif
275 if (*efmp == 'f' && efmp[1] != NUL)
276 {
277 if (efmp[1] != '\\' && efmp[1] != '%')
278 {
279 /* A file name may contain spaces, but this isn't
280 * in "\f". For "%f:%l:%m" there may be a ":" in
281 * the file name. Use ".\{-1,}x" instead (x is
282 * the next character), the requirement that :999:
283 * follows should work. */
284 STRCPY(ptr, ".\\{-1,}");
285 ptr += 7;
286 }
287 else
288 {
289 /* File name followed by '\\' or '%': include as
290 * many file name chars as possible. */
291 STRCPY(ptr, "\\f\\+");
292 ptr += 4;
293 }
294 }
295 else
296 {
297 srcptr = (char_u *)fmt_pat[idx].pattern;
298 while ((*ptr = *srcptr++) != NUL)
299 ++ptr;
300 }
301 *ptr++ = '\\';
302 *ptr++ = ')';
303 }
304 else if (*efmp == '*')
305 {
306 if (*++efmp == '[' || *efmp == '\\')
307 {
308 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
309 {
310 if (efmp[1] == '^')
311 *ptr++ = *++efmp;
312 if (efmp < efm + len)
313 {
314 *ptr++ = *++efmp; /* could be ']' */
315 while (efmp < efm + len
316 && (*ptr++ = *++efmp) != ']')
317 /* skip */;
318 if (efmp == efm + len)
319 {
320 EMSG(_("E374: Missing ] in format string"));
321 return -1;
322 }
323 }
324 }
325 else if (efmp < efm + len) /* %*\D, %*\s etc. */
326 *ptr++ = *++efmp;
327 *ptr++ = '\\';
328 *ptr++ = '+';
329 }
330 else
331 {
332 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
333 sprintf((char *)errmsg,
334 _("E375: Unsupported %%%c in format string"), *efmp);
335 EMSG(errmsg);
336 return -1;
337 }
338 }
339 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
340 *ptr++ = *efmp; /* regexp magic characters */
341 else if (*efmp == '#')
342 *ptr++ = '*';
343 else if (*efmp == '>')
344 fmt_ptr->conthere = TRUE;
345 else if (efmp == efm + 1) /* analyse prefix */
346 {
347 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
348 fmt_ptr->flags = *efmp++;
349 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
350 fmt_ptr->prefix = *efmp;
351 else
352 {
353 sprintf((char *)errmsg,
354 _("E376: Invalid %%%c in format string prefix"), *efmp);
355 EMSG(errmsg);
356 return -1;
357 }
358 }
359 else
360 {
361 sprintf((char *)errmsg,
362 _("E377: Invalid %%%c in format string"), *efmp);
363 EMSG(errmsg);
364 return -1;
365 }
366 }
367 else /* copy normal character */
368 {
369 if (*efmp == '\\' && efmp + 1 < efm + len)
370 ++efmp;
371 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
372 *ptr++ = '\\'; /* escape regexp atoms */
373 if (*efmp)
374 *ptr++ = *efmp;
375 }
376 }
377 *ptr++ = '$';
378 *ptr = NUL;
379
380 return 0;
381}
382
383 static void
384free_efm_list(efm_T **efm_first)
385{
386 efm_T *efm_ptr;
387
388 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
389 {
390 *efm_first = efm_ptr->next;
391 vim_regfree(efm_ptr->prog);
392 vim_free(efm_ptr);
393 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100394 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200395}
396
397/* Parse 'errorformat' option */
398 static efm_T *
399parse_efm_option(char_u *efm)
400{
401 char_u *errmsg = NULL;
402 int errmsglen;
403 efm_T *fmt_ptr = NULL;
404 efm_T *fmt_first = NULL;
405 efm_T *fmt_last = NULL;
406 char_u *fmtstr = NULL;
407 int len;
408 int i;
409 int round;
410
411 errmsglen = CMDBUFFSIZE + 1;
412 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
413 if (errmsg == NULL)
414 goto parse_efm_end;
415
416 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200417 * Each part of the format string is copied and modified from errorformat
418 * to regex prog. Only a few % characters are allowed.
419 */
420
421 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200422 * Get some space to modify the format string into.
423 */
424 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
425 for (round = FMT_PATTERNS; round > 0; )
426 i += (int)STRLEN(fmt_pat[--round].pattern);
427#ifdef COLON_IN_FILENAME
428 i += 12; /* "%f" can become twelve chars longer */
429#else
430 i += 2; /* "%f" can become two chars longer */
431#endif
432 if ((fmtstr = alloc(i)) == NULL)
433 goto parse_efm_error;
434
435 while (efm[0] != NUL)
436 {
437 /*
438 * Allocate a new eformat structure and put it at the end of the list
439 */
440 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
441 if (fmt_ptr == NULL)
442 goto parse_efm_error;
443 if (fmt_first == NULL) /* first one */
444 fmt_first = fmt_ptr;
445 else
446 fmt_last->next = fmt_ptr;
447 fmt_last = fmt_ptr;
448
449 /*
450 * Isolate one part in the 'errorformat' option
451 */
452 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
453 if (efm[len] == '\\' && efm[len + 1] != NUL)
454 ++len;
455
456 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
457 goto parse_efm_error;
458 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
459 goto parse_efm_error;
460 /*
461 * Advance to next part
462 */
463 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
464 }
465
466 if (fmt_first == NULL) /* nothing found */
467 EMSG(_("E378: 'errorformat' contains no pattern"));
468
469 goto parse_efm_end;
470
471parse_efm_error:
472 free_efm_list(&fmt_first);
473
474parse_efm_end:
475 vim_free(fmtstr);
476 vim_free(errmsg);
477
478 return fmt_first;
479}
480
Bram Moolenaare0d37972016-07-15 22:36:01 +0200481enum {
482 QF_FAIL = 0,
483 QF_OK = 1,
484 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200485 QF_NOMEM = 3,
486 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200487};
488
489typedef struct {
490 char_u *linebuf;
491 int linelen;
492 char_u *growbuf;
493 int growbufsiz;
494 FILE *fd;
495 typval_T *tv;
496 char_u *p_str;
497 listitem_T *p_li;
498 buf_T *buf;
499 linenr_T buflnum;
500 linenr_T lnumlast;
501} qfstate_T;
502
503 static char_u *
504qf_grow_linebuf(qfstate_T *state, int newsz)
505{
506 /*
507 * If the line exceeds LINE_MAXLEN exclude the last
508 * byte since it's not a NL character.
509 */
510 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
511 if (state->growbuf == NULL)
512 {
513 state->growbuf = alloc(state->linelen + 1);
514 if (state->growbuf == NULL)
515 return NULL;
516 state->growbufsiz = state->linelen;
517 }
518 else if (state->linelen > state->growbufsiz)
519 {
520 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
521 if (state->growbuf == NULL)
522 return NULL;
523 state->growbufsiz = state->linelen;
524 }
525 return state->growbuf;
526}
527
528/*
529 * Get the next string (separated by newline) from state->p_str.
530 */
531 static int
532qf_get_next_str_line(qfstate_T *state)
533{
534 /* Get the next line from the supplied string */
535 char_u *p_str = state->p_str;
536 char_u *p;
537 int len;
538
539 if (*p_str == NUL) /* Reached the end of the string */
540 return QF_END_OF_INPUT;
541
542 p = vim_strchr(p_str, '\n');
543 if (p != NULL)
544 len = (int)(p - p_str) + 1;
545 else
546 len = (int)STRLEN(p_str);
547
548 if (len > IOSIZE - 2)
549 {
550 state->linebuf = qf_grow_linebuf(state, len);
551 if (state->linebuf == NULL)
552 return QF_NOMEM;
553 }
554 else
555 {
556 state->linebuf = IObuff;
557 state->linelen = len;
558 }
559 vim_strncpy(state->linebuf, p_str, state->linelen);
560
561 /*
562 * Increment using len in order to discard the rest of the
563 * line if it exceeds LINE_MAXLEN.
564 */
565 p_str += len;
566 state->p_str = p_str;
567
568 return QF_OK;
569}
570
571/*
572 * Get the next string from state->p_Li.
573 */
574 static int
575qf_get_next_list_line(qfstate_T *state)
576{
577 listitem_T *p_li = state->p_li;
578 int len;
579
580 while (p_li != NULL
581 && (p_li->li_tv.v_type != VAR_STRING
582 || p_li->li_tv.vval.v_string == NULL))
583 p_li = p_li->li_next; /* Skip non-string items */
584
585 if (p_li == NULL) /* End of the list */
586 {
587 state->p_li = NULL;
588 return QF_END_OF_INPUT;
589 }
590
591 len = (int)STRLEN(p_li->li_tv.vval.v_string);
592 if (len > IOSIZE - 2)
593 {
594 state->linebuf = qf_grow_linebuf(state, len);
595 if (state->linebuf == NULL)
596 return QF_NOMEM;
597 }
598 else
599 {
600 state->linebuf = IObuff;
601 state->linelen = len;
602 }
603
604 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
605
606 state->p_li = p_li->li_next; /* next item */
607 return QF_OK;
608}
609
610/*
611 * Get the next string from state->buf.
612 */
613 static int
614qf_get_next_buf_line(qfstate_T *state)
615{
616 char_u *p_buf = NULL;
617 int len;
618
619 /* Get the next line from the supplied buffer */
620 if (state->buflnum > state->lnumlast)
621 return QF_END_OF_INPUT;
622
623 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
624 state->buflnum += 1;
625
626 len = (int)STRLEN(p_buf);
627 if (len > IOSIZE - 2)
628 {
629 state->linebuf = qf_grow_linebuf(state, len);
630 if (state->linebuf == NULL)
631 return QF_NOMEM;
632 }
633 else
634 {
635 state->linebuf = IObuff;
636 state->linelen = len;
637 }
638 vim_strncpy(state->linebuf, p_buf, state->linelen);
639
640 return QF_OK;
641}
642
643/*
644 * Get the next string from file state->fd.
645 */
646 static int
647qf_get_next_file_line(qfstate_T *state)
648{
649 int discard;
650 int growbuflen;
651
652 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
653 return QF_END_OF_INPUT;
654
655 discard = FALSE;
656 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200657 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200658 {
659 /*
660 * The current line exceeds IObuff, continue reading using
661 * growbuf until EOL or LINE_MAXLEN bytes is read.
662 */
663 if (state->growbuf == NULL)
664 {
665 state->growbufsiz = 2 * (IOSIZE - 1);
666 state->growbuf = alloc(state->growbufsiz);
667 if (state->growbuf == NULL)
668 return QF_NOMEM;
669 }
670
671 /* Copy the read part of the line, excluding null-terminator */
672 memcpy(state->growbuf, IObuff, IOSIZE - 1);
673 growbuflen = state->linelen;
674
675 for (;;)
676 {
677 if (fgets((char *)state->growbuf + growbuflen,
678 state->growbufsiz - growbuflen, state->fd) == NULL)
679 break;
680 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
681 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200682 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200683 break;
684 if (state->growbufsiz == LINE_MAXLEN)
685 {
686 discard = TRUE;
687 break;
688 }
689
690 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
691 ? 2 * state->growbufsiz : LINE_MAXLEN;
692 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
693 if (state->growbuf == NULL)
694 return QF_NOMEM;
695 }
696
697 while (discard)
698 {
699 /*
700 * The current line is longer than LINE_MAXLEN, continue
701 * reading but discard everything until EOL or EOF is
702 * reached.
703 */
704 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
705 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200706 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200707 break;
708 }
709
710 state->linebuf = state->growbuf;
711 state->linelen = growbuflen;
712 }
713 else
714 state->linebuf = IObuff;
715
716 return QF_OK;
717}
718
719/*
720 * Get the next string from a file/buffer/list/string.
721 */
722 static int
723qf_get_nextline(qfstate_T *state)
724{
725 int status = QF_FAIL;
726
727 if (state->fd == NULL)
728 {
729 if (state->tv != NULL)
730 {
731 if (state->tv->v_type == VAR_STRING)
732 /* Get the next line from the supplied string */
733 status = qf_get_next_str_line(state);
734 else if (state->tv->v_type == VAR_LIST)
735 /* Get the next line from the supplied list */
736 status = qf_get_next_list_line(state);
737 }
738 else
739 /* Get the next line from the supplied buffer */
740 status = qf_get_next_buf_line(state);
741 }
742 else
743 /* Get the next line from the supplied file */
744 status = qf_get_next_file_line(state);
745
746 if (status != QF_OK)
747 return status;
748
749 /* remove newline/CR from the line */
750 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200751 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200752 state->linebuf[state->linelen - 1] = NUL;
753#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200754 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
755 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200756#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200757 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200758
759#ifdef FEAT_MBYTE
760 remove_bom(state->linebuf);
761#endif
762
763 return QF_OK;
764}
765
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200766typedef struct {
767 char_u *namebuf;
768 char_u *errmsg;
769 int errmsglen;
770 long lnum;
771 int col;
772 char_u use_viscol;
773 char_u *pattern;
774 int enr;
775 int type;
776 int valid;
777} qffields_T;
778
779/*
780 * Parse a line and get the quickfix fields.
781 * Return the QF_ status.
782 */
783 static int
784qf_parse_line(
785 qf_info_T *qi,
786 char_u *linebuf,
787 int linelen,
788 efm_T *fmt_first,
789 qffields_T *fields)
790{
791 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200792 char_u *ptr;
793 int len;
794 int i;
795 int idx = 0;
796 char_u *tail = NULL;
797 regmatch_T regmatch;
798
799 /* Always ignore case when looking for a matching error. */
800 regmatch.rm_ic = TRUE;
801
802 /* If there was no %> item start at the first pattern */
803 if (fmt_start == NULL)
804 fmt_ptr = fmt_first;
805 else
806 {
807 fmt_ptr = fmt_start;
808 fmt_start = NULL;
809 }
810
811 /*
812 * Try to match each part of 'errorformat' until we find a complete
813 * match or no match.
814 */
815 fields->valid = TRUE;
816restofline:
817 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
818 {
819 int r;
820
821 idx = fmt_ptr->prefix;
822 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
823 continue;
824 fields->namebuf[0] = NUL;
825 fields->pattern[0] = NUL;
826 if (!qi->qf_multiscan)
827 fields->errmsg[0] = NUL;
828 fields->lnum = 0;
829 fields->col = 0;
830 fields->use_viscol = FALSE;
831 fields->enr = -1;
832 fields->type = 0;
833 tail = NULL;
834
835 regmatch.regprog = fmt_ptr->prog;
836 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
837 fmt_ptr->prog = regmatch.regprog;
838 if (r)
839 {
840 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
841 continue;
842 if (vim_strchr((char_u *)"EWI", idx) != NULL)
843 fields->type = idx;
844 else
845 fields->type = 0;
846 /*
847 * Extract error message data from matched line.
848 * We check for an actual submatch, because "\[" and "\]" in
849 * the 'errorformat' may cause the wrong submatch to be used.
850 */
851 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
852 {
853 int c;
854
855 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
856 continue;
857
858 /* Expand ~/file and $HOME/file to full path. */
859 c = *regmatch.endp[i];
860 *regmatch.endp[i] = NUL;
861 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
862 *regmatch.endp[i] = c;
863
864 if (vim_strchr((char_u *)"OPQ", idx) != NULL
865 && mch_getperm(fields->namebuf) == -1)
866 continue;
867 }
868 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
869 {
870 if (regmatch.startp[i] == NULL)
871 continue;
872 fields->enr = (int)atol((char *)regmatch.startp[i]);
873 }
874 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
875 {
876 if (regmatch.startp[i] == NULL)
877 continue;
878 fields->lnum = atol((char *)regmatch.startp[i]);
879 }
880 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
881 {
882 if (regmatch.startp[i] == NULL)
883 continue;
884 fields->col = (int)atol((char *)regmatch.startp[i]);
885 }
886 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
887 {
888 if (regmatch.startp[i] == NULL)
889 continue;
890 fields->type = *regmatch.startp[i];
891 }
892 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
893 {
894 if (linelen > fields->errmsglen) {
895 /* linelen + null terminator */
896 if ((fields->errmsg = vim_realloc(fields->errmsg,
897 linelen + 1)) == NULL)
898 return QF_NOMEM;
899 fields->errmsglen = linelen + 1;
900 }
901 vim_strncpy(fields->errmsg, linebuf, linelen);
902 }
903 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
904 {
905 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
906 continue;
907 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
908 if (len > fields->errmsglen) {
909 /* len + null terminator */
910 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
911 == NULL)
912 return QF_NOMEM;
913 fields->errmsglen = len + 1;
914 }
915 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
916 }
917 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
918 {
919 if (regmatch.startp[i] == NULL)
920 continue;
921 tail = regmatch.startp[i];
922 }
923 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
924 {
925 char_u *match_ptr;
926
927 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
928 continue;
929 fields->col = 0;
930 for (match_ptr = regmatch.startp[i];
931 match_ptr != regmatch.endp[i]; ++match_ptr)
932 {
933 ++fields->col;
934 if (*match_ptr == TAB)
935 {
936 fields->col += 7;
937 fields->col -= fields->col % 8;
938 }
939 }
940 ++fields->col;
941 fields->use_viscol = TRUE;
942 }
943 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
944 {
945 if (regmatch.startp[i] == NULL)
946 continue;
947 fields->col = (int)atol((char *)regmatch.startp[i]);
948 fields->use_viscol = TRUE;
949 }
950 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
951 {
952 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
953 continue;
954 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
955 if (len > CMDBUFFSIZE - 5)
956 len = CMDBUFFSIZE - 5;
957 STRCPY(fields->pattern, "^\\V");
958 STRNCAT(fields->pattern, regmatch.startp[i], len);
959 fields->pattern[len + 3] = '\\';
960 fields->pattern[len + 4] = '$';
961 fields->pattern[len + 5] = NUL;
962 }
963 break;
964 }
965 }
966 qi->qf_multiscan = FALSE;
967
968 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
969 {
970 if (fmt_ptr != NULL)
971 {
972 if (idx == 'D') /* enter directory */
973 {
974 if (*fields->namebuf == NUL)
975 {
976 EMSG(_("E379: Missing or empty directory name"));
977 return QF_FAIL;
978 }
979 qi->qf_directory =
980 qf_push_dir(fields->namebuf, &qi->qf_dir_stack, FALSE);
981 if (qi->qf_directory == NULL)
982 return QF_FAIL;
983 }
984 else if (idx == 'X') /* leave directory */
985 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
986 }
987 fields->namebuf[0] = NUL; /* no match found, remove file name */
988 fields->lnum = 0; /* don't jump to this line */
989 fields->valid = FALSE;
990 if (linelen > fields->errmsglen) {
991 /* linelen + null terminator */
992 if ((fields->errmsg = vim_realloc(fields->errmsg,
993 linelen + 1)) == NULL)
994 return QF_NOMEM;
995 fields->errmsglen = linelen + 1;
996 }
997 /* copy whole line to error message */
998 vim_strncpy(fields->errmsg, linebuf, linelen);
999 if (fmt_ptr == NULL)
1000 qi->qf_multiline = qi->qf_multiignore = FALSE;
1001 }
1002 else if (fmt_ptr != NULL)
1003 {
1004 /* honor %> item */
1005 if (fmt_ptr->conthere)
1006 fmt_start = fmt_ptr;
1007
1008 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1009 {
1010 qi->qf_multiline = TRUE; /* start of a multi-line message */
1011 qi->qf_multiignore = FALSE; /* reset continuation */
1012 }
1013 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1014 { /* continuation of multi-line msg */
Bram Moolenaar9b457942016-10-09 16:10:05 +02001015 if (!qi->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001016 {
Bram Moolenaar9b457942016-10-09 16:10:05 +02001017 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001018
Bram Moolenaar9b457942016-10-09 16:10:05 +02001019 if (qfprev == NULL)
1020 return QF_FAIL;
1021 if (*fields->errmsg && !qi->qf_multiignore)
1022 {
1023 len = (int)STRLEN(qfprev->qf_text);
1024 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1025 == NULL)
1026 return QF_FAIL;
1027 STRCPY(ptr, qfprev->qf_text);
1028 vim_free(qfprev->qf_text);
1029 qfprev->qf_text = ptr;
1030 *(ptr += len) = '\n';
1031 STRCPY(++ptr, fields->errmsg);
1032 }
1033 if (qfprev->qf_nr == -1)
1034 qfprev->qf_nr = fields->enr;
1035 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1036 /* only printable chars allowed */
1037 qfprev->qf_type = fields->type;
1038
1039 if (!qfprev->qf_lnum)
1040 qfprev->qf_lnum = fields->lnum;
1041 if (!qfprev->qf_col)
1042 qfprev->qf_col = fields->col;
1043 qfprev->qf_viscol = fields->use_viscol;
1044 if (!qfprev->qf_fnum)
1045 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1046 *fields->namebuf || qi->qf_directory != NULL
1047 ? fields->namebuf
1048 : qi->qf_currfile != NULL && fields->valid
1049 ? qi->qf_currfile : 0);
1050 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001051 if (idx == 'Z')
1052 qi->qf_multiline = qi->qf_multiignore = FALSE;
1053 line_breakcheck();
1054 return QF_IGNORE_LINE;
1055 }
1056 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1057 {
1058 /* global file names */
1059 fields->valid = FALSE;
1060 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1061 {
1062 if (*fields->namebuf && idx == 'P')
1063 qi->qf_currfile =
1064 qf_push_dir(fields->namebuf, &qi->qf_file_stack, TRUE);
1065 else if (idx == 'Q')
1066 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
1067 *fields->namebuf = NUL;
1068 if (tail && *tail)
1069 {
1070 STRMOVE(IObuff, skipwhite(tail));
1071 qi->qf_multiscan = TRUE;
1072 goto restofline;
1073 }
1074 }
1075 }
1076 if (fmt_ptr->flags == '-') /* generally exclude this line */
1077 {
1078 if (qi->qf_multiline)
1079 /* also exclude continuation lines */
1080 qi->qf_multiignore = TRUE;
1081 return QF_IGNORE_LINE;
1082 }
1083 }
1084
1085 return QF_OK;
1086}
1087
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001088/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001089 * Read the errorfile "efile" into memory, line by line, building the error
1090 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001091 * Alternative: when "efile" is NULL read errors from buffer "buf".
1092 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001093 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001094 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1095 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001096 * Return -1 for error, number of errors for success.
1097 */
1098 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001099qf_init_ext(
1100 qf_info_T *qi,
1101 char_u *efile,
1102 buf_T *buf,
1103 typval_T *tv,
1104 char_u *errorformat,
1105 int newlist, /* TRUE: start a new error list */
1106 linenr_T lnumfirst, /* first line number to use */
1107 linenr_T lnumlast, /* last line number to use */
1108 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001109{
Bram Moolenaare0d37972016-07-15 22:36:01 +02001110 qfstate_T state = {NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001111 NULL, 0, 0};
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001112 qffields_T fields = {NULL, NULL, 0, 0L, 0, FALSE, NULL, 0, 0, 0};
Bram Moolenaar864293a2016-06-02 13:40:04 +02001113#ifdef FEAT_WINDOWS
1114 qfline_T *old_last = NULL;
1115#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001116 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001118 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001120 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001122 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1123 fields.errmsglen = CMDBUFFSIZE + 1;
1124 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1125 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1126 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1127 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 goto qf_init_end;
1129
Bram Moolenaare0d37972016-07-15 22:36:01 +02001130 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
1132 EMSG2(_(e_openerrf), efile);
1133 goto qf_init_end;
1134 }
1135
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001136 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001138 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001139#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001140 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02001141 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001142 /* Adding to existing list, use last entry. */
1143 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001144 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001148 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001149 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 else
1151 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001153 /*
1154 * If we are not adding or adding to another list: clear the state.
1155 */
1156 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
1157 {
1158 qi->qf_dir_curlist = qi->qf_curlist;
1159 qf_clean_dir_stack(&qi->qf_dir_stack);
1160 qi->qf_directory = NULL;
1161 qf_clean_dir_stack(&qi->qf_file_stack);
1162 qi->qf_currfile = NULL;
1163 qi->qf_multiline = FALSE;
1164 qi->qf_multiignore = FALSE;
1165 qi->qf_multiscan = FALSE;
1166 }
1167
1168 /*
1169 * If the errorformat didn't change between calls, then reuse the
1170 * previously parsed values.
1171 */
1172 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1173 {
1174 /* free the previously parsed data */
1175 vim_free(last_efm);
1176 last_efm = NULL;
1177 free_efm_list(&fmt_first);
1178
1179 /* parse the current 'efm' */
1180 fmt_first = parse_efm_option(efm);
1181 if (fmt_first != NULL)
1182 last_efm = vim_strsave(efm);
1183 }
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
1188 /*
1189 * got_int is reset here, because it was probably set when killing the
1190 * ":make" command, but we still want to read the errorfile then.
1191 */
1192 got_int = FALSE;
1193
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001194 if (tv != NULL)
1195 {
1196 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001197 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001198 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001199 state.p_li = tv->vval.v_list->lv_first;
1200 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001201 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001202 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001203 state.buflnum = lnumfirst;
1204 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 /*
1207 * Read the lines in the error file one by one.
1208 * Try to recognize one of the error formats in each line.
1209 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001210 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001212 /* Get the next line from a file/buffer/list/string */
1213 status = qf_get_nextline(&state);
1214 if (status == QF_NOMEM) /* memory alloc failure */
1215 goto qf_init_end;
1216 if (status == QF_END_OF_INPUT) /* end of input */
1217 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001219 status = qf_parse_line(qi, state.linebuf, state.linelen, fmt_first,
1220 &fields);
1221 if (status == QF_FAIL)
1222 goto error2;
1223 if (status == QF_NOMEM)
1224 goto qf_init_end;
1225 if (status == QF_IGNORE_LINE)
1226 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001228 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001229 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001230 (*fields.namebuf || qi->qf_directory != NULL)
1231 ? fields.namebuf
1232 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001233 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001234 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001235 fields.errmsg,
1236 fields.lnum,
1237 fields.col,
1238 fields.use_viscol,
1239 fields.pattern,
1240 fields.enr,
1241 fields.type,
1242 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 goto error2;
1244 line_breakcheck();
1245 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001246 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001248 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001250 /* no valid entry found */
1251 qi->qf_lists[qi->qf_curlist].qf_ptr =
1252 qi->qf_lists[qi->qf_curlist].qf_start;
1253 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1254 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256 else
1257 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001258 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1259 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1260 qi->qf_lists[qi->qf_curlist].qf_ptr =
1261 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001263 /* return number of matches */
1264 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001265 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267 EMSG(_(e_readerrf));
1268error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001269 qf_free(qi, qi->qf_curlist);
1270 qi->qf_listcount--;
1271 if (qi->qf_curlist > 0)
1272 --qi->qf_curlist;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001273qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001274 if (state.fd != NULL)
1275 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001276 vim_free(fields.namebuf);
1277 vim_free(fields.errmsg);
1278 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001279 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001282 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283#endif
1284
1285 return retval;
1286}
1287
Bram Moolenaarfb604092014-07-23 15:55:00 +02001288 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001289qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001290{
1291 if (title != NULL)
1292 {
1293 char_u *p = alloc((int)STRLEN(title) + 2);
1294
1295 qi->qf_lists[qi->qf_curlist].qf_title = p;
1296 if (p != NULL)
1297 sprintf((char *)p, ":%s", (char *)title);
1298 }
1299}
1300
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301/*
1302 * Prepare for adding a new quickfix list.
1303 */
1304 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001305qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 int i;
1308
1309 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001310 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 * the current entry. This makes it possible to browse in a tree-like
1312 * way with ":grep'.
1313 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001314 while (qi->qf_listcount > qi->qf_curlist + 1)
1315 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
1317 /*
1318 * When the stack is full, remove to oldest entry
1319 * Otherwise, add a new entry.
1320 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001321 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001323 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001325 qi->qf_lists[i - 1] = qi->qf_lists[i];
1326 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001329 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001330 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001331 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332}
1333
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001334/*
1335 * Free a location list
1336 */
1337 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001338ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001339{
1340 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001341 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001342
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001343 qi = *pqi;
1344 if (qi == NULL)
1345 return;
1346 *pqi = NULL; /* Remove reference to this list */
1347
1348 qi->qf_refcount--;
1349 if (qi->qf_refcount < 1)
1350 {
1351 /* No references to this location list */
1352 for (i = 0; i < qi->qf_listcount; ++i)
1353 qf_free(qi, i);
1354 vim_free(qi);
1355 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001356}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001357
1358 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001359qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001360{
1361 int i;
1362 qf_info_T *qi = &ql_info;
1363
1364 if (wp != NULL)
1365 {
1366 /* location list */
1367 ll_free_all(&wp->w_llist);
1368 ll_free_all(&wp->w_llist_ref);
1369 }
1370 else
1371 /* quickfix list */
1372 for (i = 0; i < qi->qf_listcount; ++i)
1373 qf_free(qi, i);
1374}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376/*
1377 * Add an entry to the end of the list of errors.
1378 * Returns OK or FAIL.
1379 */
1380 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001381qf_add_entry(
1382 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001383 char_u *dir, /* optional directory name */
1384 char_u *fname, /* file name or NULL */
1385 int bufnum, /* buffer number or zero */
1386 char_u *mesg, /* message */
1387 long lnum, /* line number */
1388 int col, /* column */
1389 int vis_col, /* using visual column */
1390 char_u *pattern, /* search pattern */
1391 int nr, /* error number */
1392 int type, /* type character */
1393 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001395 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001396 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001398 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001400 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001401 {
1402 buf_T *buf = buflist_findnr(bufnum);
1403
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001404 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001405 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001406 buf->b_has_qf_entry |=
1407 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001408 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001409 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001410 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1412 {
1413 vim_free(qfp);
1414 return FAIL;
1415 }
1416 qfp->qf_lnum = lnum;
1417 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001418 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001419 if (pattern == NULL || *pattern == NUL)
1420 qfp->qf_pattern = NULL;
1421 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1422 {
1423 vim_free(qfp->qf_text);
1424 vim_free(qfp);
1425 return FAIL;
1426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 qfp->qf_nr = nr;
1428 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1429 type = 0;
1430 qfp->qf_type = type;
1431 qfp->qf_valid = valid;
1432
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001433 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001434 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1435 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001437 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001438 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1439 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001440 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442 else
1443 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001444 qfp->qf_prev = *lastp;
1445 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001447 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001449 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001450 ++qi->qf_lists[qi->qf_curlist].qf_count;
1451 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1452 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001454 qi->qf_lists[qi->qf_curlist].qf_index =
1455 qi->qf_lists[qi->qf_curlist].qf_count;
1456 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 }
1458
1459 return OK;
1460}
1461
1462/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001463 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001464 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001465 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001466ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001467{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001468 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001469
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001470 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1471 if (qi != NULL)
1472 {
1473 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1474 qi->qf_refcount++;
1475 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001476
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001477 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001478}
1479
1480/*
1481 * Return the location list for window 'wp'.
1482 * If not present, allocate a location list
1483 */
1484 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001485ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001486{
1487 if (IS_LL_WINDOW(wp))
1488 /* For a location list window, use the referenced location list */
1489 return wp->w_llist_ref;
1490
1491 /*
1492 * For a non-location list window, w_llist_ref should not point to a
1493 * location list.
1494 */
1495 ll_free_all(&wp->w_llist_ref);
1496
1497 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001498 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001499 return wp->w_llist;
1500}
1501
1502/*
1503 * Copy the location list from window "from" to window "to".
1504 */
1505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001506copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001507{
1508 qf_info_T *qi;
1509 int idx;
1510 int i;
1511
1512 /*
1513 * When copying from a location list window, copy the referenced
1514 * location list. For other windows, copy the location list for
1515 * that window.
1516 */
1517 if (IS_LL_WINDOW(from))
1518 qi = from->w_llist_ref;
1519 else
1520 qi = from->w_llist;
1521
1522 if (qi == NULL) /* no location list to copy */
1523 return;
1524
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001525 /* allocate a new location list */
1526 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001527 return;
1528
1529 to->w_llist->qf_listcount = qi->qf_listcount;
1530
1531 /* Copy the location lists one at a time */
1532 for (idx = 0; idx < qi->qf_listcount; idx++)
1533 {
1534 qf_list_T *from_qfl;
1535 qf_list_T *to_qfl;
1536
1537 to->w_llist->qf_curlist = idx;
1538
1539 from_qfl = &qi->qf_lists[idx];
1540 to_qfl = &to->w_llist->qf_lists[idx];
1541
1542 /* Some of the fields are populated by qf_add_entry() */
1543 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1544 to_qfl->qf_count = 0;
1545 to_qfl->qf_index = 0;
1546 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001547 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001549 if (from_qfl->qf_title != NULL)
1550 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1551 else
1552 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001553
1554 if (from_qfl->qf_count)
1555 {
1556 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001557 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001558
1559 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001560 for (i = 0, from_qfp = from_qfl->qf_start;
1561 i < from_qfl->qf_count && from_qfp != NULL;
1562 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001563 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001564 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001565 NULL,
1566 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001567 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001568 from_qfp->qf_text,
1569 from_qfp->qf_lnum,
1570 from_qfp->qf_col,
1571 from_qfp->qf_viscol,
1572 from_qfp->qf_pattern,
1573 from_qfp->qf_nr,
1574 0,
1575 from_qfp->qf_valid) == FAIL)
1576 {
1577 qf_free_all(to);
1578 return;
1579 }
1580 /*
1581 * qf_add_entry() will not set the qf_num field, as the
1582 * directory and file names are not supplied. So the qf_fnum
1583 * field is copied here.
1584 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001585 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001586 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1587 prevp->qf_type = from_qfp->qf_type; /* error type */
1588 if (from_qfl->qf_ptr == from_qfp)
1589 to_qfl->qf_ptr = prevp; /* current location */
1590 }
1591 }
1592
1593 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1594
1595 /* When no valid entries are present in the list, qf_ptr points to
1596 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001597 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001598 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001599 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001600 to_qfl->qf_index = 1;
1601 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001602 }
1603
1604 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1605}
1606
1607/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001608 * Looking up a buffer can be slow if there are many. Remember the last one
1609 * to make this a lot faster if there are multiple matches in the same file.
1610 */
1611static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001612static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001613
1614/*
Bram Moolenaar015102e2016-07-16 18:24:56 +02001615 * Get buffer number for file "directory.fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001616 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 */
1618 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001619qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
Bram Moolenaar82404332016-07-10 17:00:38 +02001621 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001622 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001623 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (fname == NULL || *fname == NUL) /* no file name */
1626 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaare60acc12011-05-10 16:41:25 +02001628#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001629 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001630#endif
1631#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001632 if (directory != NULL)
1633 slash_adjust(directory);
1634 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001635#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001636 if (directory != NULL && !vim_isAbsName(fname)
1637 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1638 {
1639 /*
1640 * Here we check if the file really exists.
1641 * This should normally be true, but if make works without
1642 * "leaving directory"-messages we might have missed a
1643 * directory change.
1644 */
1645 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001648 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001649 if (directory)
1650 ptr = concat_fnames(directory, fname, TRUE);
1651 else
1652 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001654 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001655 bufname = ptr;
1656 }
1657 else
1658 bufname = fname;
1659
1660 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001661 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001662 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001663 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001664 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001666 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001667 {
1668 vim_free(qf_last_bufname);
1669 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1670 if (bufname == ptr)
1671 qf_last_bufname = bufname;
1672 else
1673 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001674 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001675 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001676 if (buf == NULL)
1677 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001678
Bram Moolenaarc1542742016-07-20 21:44:37 +02001679 buf->b_has_qf_entry =
1680 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001681 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682}
1683
1684/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001685 * Push dirbuf onto the directory stack and return pointer to actual dir or
1686 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
1688 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001689qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 struct dir_stack_T *ds_new;
1692 struct dir_stack_T *ds_ptr;
1693
1694 /* allocate new stack element and hook it in */
1695 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1696 if (ds_new == NULL)
1697 return NULL;
1698
1699 ds_new->next = *stackptr;
1700 *stackptr = ds_new;
1701
1702 /* store directory on the stack */
1703 if (vim_isAbsName(dirbuf)
1704 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001705 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 (*stackptr)->dirname = vim_strsave(dirbuf);
1707 else
1708 {
1709 /* Okay we don't have an absolute path.
1710 * dirbuf must be a subdir of one of the directories on the stack.
1711 * Let's search...
1712 */
1713 ds_new = (*stackptr)->next;
1714 (*stackptr)->dirname = NULL;
1715 while (ds_new)
1716 {
1717 vim_free((*stackptr)->dirname);
1718 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1719 TRUE);
1720 if (mch_isdir((*stackptr)->dirname) == TRUE)
1721 break;
1722
1723 ds_new = ds_new->next;
1724 }
1725
1726 /* clean up all dirs we already left */
1727 while ((*stackptr)->next != ds_new)
1728 {
1729 ds_ptr = (*stackptr)->next;
1730 (*stackptr)->next = (*stackptr)->next->next;
1731 vim_free(ds_ptr->dirname);
1732 vim_free(ds_ptr);
1733 }
1734
1735 /* Nothing found -> it must be on top level */
1736 if (ds_new == NULL)
1737 {
1738 vim_free((*stackptr)->dirname);
1739 (*stackptr)->dirname = vim_strsave(dirbuf);
1740 }
1741 }
1742
1743 if ((*stackptr)->dirname != NULL)
1744 return (*stackptr)->dirname;
1745 else
1746 {
1747 ds_ptr = *stackptr;
1748 *stackptr = (*stackptr)->next;
1749 vim_free(ds_ptr);
1750 return NULL;
1751 }
1752}
1753
1754
1755/*
1756 * pop dirbuf from the directory stack and return previous directory or NULL if
1757 * stack is empty
1758 */
1759 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001760qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 struct dir_stack_T *ds_ptr;
1763
1764 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1765 * What to do if it isn't? */
1766
1767 /* pop top element and free it */
1768 if (*stackptr != NULL)
1769 {
1770 ds_ptr = *stackptr;
1771 *stackptr = (*stackptr)->next;
1772 vim_free(ds_ptr->dirname);
1773 vim_free(ds_ptr);
1774 }
1775
1776 /* return NEW top element as current dir or NULL if stack is empty*/
1777 return *stackptr ? (*stackptr)->dirname : NULL;
1778}
1779
1780/*
1781 * clean up directory stack
1782 */
1783 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001784qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 struct dir_stack_T *ds_ptr;
1787
1788 while ((ds_ptr = *stackptr) != NULL)
1789 {
1790 *stackptr = (*stackptr)->next;
1791 vim_free(ds_ptr->dirname);
1792 vim_free(ds_ptr);
1793 }
1794}
1795
1796/*
1797 * Check in which directory of the directory stack the given file can be
1798 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001799 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 * Cleans up intermediate directory entries.
1801 *
1802 * TODO: How to solve the following problem?
1803 * If we have the this directory tree:
1804 * ./
1805 * ./aa
1806 * ./aa/bb
1807 * ./bb
1808 * ./bb/x.c
1809 * and make says:
1810 * making all in aa
1811 * making all in bb
1812 * x.c:9: Error
1813 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1814 * qf_guess_filepath will return NULL.
1815 */
1816 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001817qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 struct dir_stack_T *ds_ptr;
1820 struct dir_stack_T *ds_tmp;
1821 char_u *fullname;
1822
1823 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001824 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 return NULL;
1826
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001827 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 fullname = NULL;
1829 while (ds_ptr)
1830 {
1831 vim_free(fullname);
1832 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1833
1834 /* If concat_fnames failed, just go on. The worst thing that can happen
1835 * is that we delete the entire stack.
1836 */
1837 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1838 break;
1839
1840 ds_ptr = ds_ptr->next;
1841 }
1842
1843 vim_free(fullname);
1844
1845 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001846 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001848 ds_tmp = qi->qf_dir_stack->next;
1849 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 vim_free(ds_tmp->dirname);
1851 vim_free(ds_tmp);
1852 }
1853
1854 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855}
1856
1857/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001858 * When loading a file from the quickfix, the auto commands may modify it.
1859 * This may invalidate the current quickfix entry. This function checks
1860 * whether a entry is still present in the quickfix.
1861 * Similar to location list.
1862 */
1863 static int
1864is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1865{
1866 qf_list_T *qfl;
1867 qfline_T *qfp;
1868 int i;
1869
1870 qfl = &qi->qf_lists[qi->qf_curlist];
1871
1872 /* Search for the entry in the current list */
1873 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1874 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001875 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001876 break;
1877
1878 if (i == qfl->qf_count) /* Entry is not found */
1879 return FALSE;
1880
1881 return TRUE;
1882}
1883
1884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 * jump to a quickfix line
1886 * if dir == FORWARD go "errornr" valid entries forward
1887 * if dir == BACKWARD go "errornr" valid entries backward
1888 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1889 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1890 * else if "errornr" is zero, redisplay the same line
1891 * else go to entry "errornr"
1892 */
1893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001894qf_jump(
1895 qf_info_T *qi,
1896 int dir,
1897 int errornr,
1898 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001900 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001901 qfline_T *qf_ptr;
1902 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 int qf_index;
1904 int old_qf_fnum;
1905 int old_qf_index;
1906 int prev_index;
1907 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1908 char_u *err = e_no_more_items;
1909 linenr_T i;
1910 buf_T *old_curbuf;
1911 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 colnr_T screen_col;
1913 colnr_T char_col;
1914 char_u *line;
1915#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001916 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001917 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 int opened_window = FALSE;
1919 win_T *win;
1920 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001921 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001923 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 int print_message = TRUE;
1925 int len;
1926#ifdef FEAT_FOLDING
1927 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1928#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001929 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001930 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001932 if (qi == NULL)
1933 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001934
1935 if (qi->qf_curlist >= qi->qf_listcount
1936 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 {
1938 EMSG(_(e_quickfix));
1939 return;
1940 }
1941
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001944 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 old_qf_index = qf_index;
1946 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1947 {
1948 while (errornr--)
1949 {
1950 old_qf_ptr = qf_ptr;
1951 prev_index = qf_index;
1952 old_qf_fnum = qf_ptr->qf_fnum;
1953 do
1954 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001955 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 || qf_ptr->qf_next == NULL)
1957 {
1958 qf_ptr = old_qf_ptr;
1959 qf_index = prev_index;
1960 if (err != NULL)
1961 {
1962 EMSG(_(err));
1963 goto theend;
1964 }
1965 errornr = 0;
1966 break;
1967 }
1968 ++qf_index;
1969 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001970 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1971 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1973 err = NULL;
1974 }
1975 }
1976 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1977 {
1978 while (errornr--)
1979 {
1980 old_qf_ptr = qf_ptr;
1981 prev_index = qf_index;
1982 old_qf_fnum = qf_ptr->qf_fnum;
1983 do
1984 {
1985 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1986 {
1987 qf_ptr = old_qf_ptr;
1988 qf_index = prev_index;
1989 if (err != NULL)
1990 {
1991 EMSG(_(err));
1992 goto theend;
1993 }
1994 errornr = 0;
1995 break;
1996 }
1997 --qf_index;
1998 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001999 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2000 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2002 err = NULL;
2003 }
2004 }
2005 else if (errornr != 0) /* go to specified number */
2006 {
2007 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2008 {
2009 --qf_index;
2010 qf_ptr = qf_ptr->qf_prev;
2011 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002012 while (errornr > qf_index && qf_index <
2013 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 && qf_ptr->qf_next != NULL)
2015 {
2016 ++qf_index;
2017 qf_ptr = qf_ptr->qf_next;
2018 }
2019 }
2020
2021#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002022 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2023 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 /* No need to print the error message if it's visible in the error
2025 * window */
2026 print_message = FALSE;
2027
2028 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002029 * For ":helpgrep" find a help window or open one.
2030 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002031 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002032 {
2033 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002034
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002035 if (cmdmod.tab != 0)
2036 wp = NULL;
2037 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002038 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002039 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2040 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002041 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2042 win_enter(wp, TRUE);
2043 else
2044 {
2045 /*
2046 * Split off help window; put it at far top if no position
2047 * specified, the current window is vertically split and narrow.
2048 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002049 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002050 if (cmdmod.split == 0 && curwin->w_width != Columns
2051 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002052 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002053 if (qi != &ql_info)
2054 flags |= WSP_NEWLOC; /* don't copy the location list */
2055
2056 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002057 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002058 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002059
2060 if (curwin->w_height < p_hh)
2061 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002062
2063 if (qi != &ql_info) /* not a quickfix list */
2064 {
2065 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002066 curwin->w_llist = qi;
2067 qi->qf_refcount++;
2068 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002069 }
2070
2071 if (!p_im)
2072 restart_edit = 0; /* don't want insert mode in help file */
2073 }
2074
2075 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 * If currently in the quickfix window, find another window to show the
2077 * file in.
2078 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002079 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002081 win_T *usable_win_ptr = NULL;
2082
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 /*
2084 * If there is no file specified, we don't know where to go.
2085 * But do advance, otherwise ":cn" gets stuck.
2086 */
2087 if (qf_ptr->qf_fnum == 0)
2088 goto theend;
2089
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002091
2092 ll_ref = curwin->w_llist_ref;
2093 if (ll_ref != NULL)
2094 {
2095 /* Find a window using the same location list that is not a
2096 * quickfix window. */
2097 FOR_ALL_WINDOWS(usable_win_ptr)
2098 if (usable_win_ptr->w_llist == ll_ref
2099 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002100 {
2101 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002102 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002103 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002104 }
2105
2106 if (!usable_win)
2107 {
2108 /* Locate a window showing a normal buffer */
2109 FOR_ALL_WINDOWS(win)
2110 if (win->w_buffer->b_p_bt[0] == NUL)
2111 {
2112 usable_win = 1;
2113 break;
2114 }
2115 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002118 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002119 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002121 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002122 {
2123 tabpage_T *tp;
2124 win_T *wp;
2125
2126 FOR_ALL_TAB_WINDOWS(tp, wp)
2127 {
2128 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2129 {
2130 goto_tabpage_win(tp, wp);
2131 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002132 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002133 }
2134 }
2135 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002136win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002137
2138 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002139 * If there is only one window and it is the quickfix window, create a
2140 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002141 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002142 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002144 flags = WSP_ABOVE;
2145 if (ll_ref != NULL)
2146 flags |= WSP_NEWLOC;
2147 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 goto failed; /* not enough room for window */
2149 opened_window = TRUE; /* close it when fail */
2150 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002151 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002152 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153 if (ll_ref != NULL)
2154 {
2155 /* The new window should use the location list from the
2156 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157 curwin->w_llist = ll_ref;
2158 ll_ref->qf_refcount++;
2159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
2161 else
2162 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002163 if (curwin->w_llist_ref != NULL)
2164 {
2165 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002166 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002167 if (win == NULL)
2168 {
2169 /* Find the window showing the selected file */
2170 FOR_ALL_WINDOWS(win)
2171 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2172 break;
2173 if (win == NULL)
2174 {
2175 /* Find a previous usable window */
2176 win = curwin;
2177 do
2178 {
2179 if (win->w_buffer->b_p_bt[0] == NUL)
2180 break;
2181 if (win->w_prev == NULL)
2182 win = lastwin; /* wrap around the top */
2183 else
2184 win = win->w_prev; /* go to previous window */
2185 } while (win != curwin);
2186 }
2187 }
2188 win_goto(win);
2189
2190 /* If the location list for the window is not set, then set it
2191 * to the location list from the location window */
2192 if (win->w_llist == NULL)
2193 {
2194 win->w_llist = ll_ref;
2195 ll_ref->qf_refcount++;
2196 }
2197 }
2198 else
2199 {
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 /*
2202 * Try to find a window that shows the right buffer.
2203 * Default to the window just above the quickfix buffer.
2204 */
2205 win = curwin;
2206 altwin = NULL;
2207 for (;;)
2208 {
2209 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2210 break;
2211 if (win->w_prev == NULL)
2212 win = lastwin; /* wrap around the top */
2213 else
2214 win = win->w_prev; /* go to previous window */
2215
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002216 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 /* Didn't find it, go to the window before the quickfix
2219 * window. */
2220 if (altwin != NULL)
2221 win = altwin;
2222 else if (curwin->w_prev != NULL)
2223 win = curwin->w_prev;
2224 else
2225 win = curwin->w_next;
2226 break;
2227 }
2228
2229 /* Remember a usable window. */
2230 if (altwin == NULL && !win->w_p_pvw
2231 && win->w_buffer->b_p_bt[0] == NUL)
2232 altwin = win;
2233 }
2234
2235 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
2238 }
2239#endif
2240
2241 /*
2242 * If there is a file name,
2243 * read the wanted file if needed, and check autowrite etc.
2244 */
2245 old_curbuf = curbuf;
2246 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002247
2248 if (qf_ptr->qf_fnum != 0)
2249 {
2250 if (qf_ptr->qf_type == 1)
2251 {
2252 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2253 * set b_p_ro flag). */
2254 if (!can_abandon(curbuf, forceit))
2255 {
2256 EMSG(_(e_nowrtmsg));
2257 ok = FALSE;
2258 }
2259 else
2260 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002261 ECMD_HIDE + ECMD_SET_HELP,
2262 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002263 }
2264 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002265 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002266 int old_qf_curlist = qi->qf_curlist;
2267 int is_abort = FALSE;
2268
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002269 ok = buflist_getfile(qf_ptr->qf_fnum,
2270 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002271 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002272 {
2273 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002274 is_abort = TRUE;
2275 opened_window = FALSE;
2276 }
2277 else if (old_qf_curlist != qi->qf_curlist
2278 || !is_qf_entry_present(qi, qf_ptr))
2279 {
2280 if (qi == &ql_info)
2281 EMSG(_("E925: Current quickfix was changed"));
2282 else
2283 EMSG(_("E926: Current location list was changed"));
2284 is_abort = TRUE;
2285 }
2286
2287 if (is_abort)
2288 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002289 ok = FALSE;
2290 qi = NULL;
2291 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002292 }
2293 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002294 }
2295
2296 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 /* When not switched to another buffer, still need to set pc mark */
2299 if (curbuf == old_curbuf)
2300 setpcmark();
2301
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002302 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002304 /*
2305 * Go to line with error, unless qf_lnum is 0.
2306 */
2307 i = qf_ptr->qf_lnum;
2308 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002310 if (i > curbuf->b_ml.ml_line_count)
2311 i = curbuf->b_ml.ml_line_count;
2312 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002314 if (qf_ptr->qf_col > 0)
2315 {
2316 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002317#ifdef FEAT_VIRTUALEDIT
2318 curwin->w_cursor.coladd = 0;
2319#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002320 if (qf_ptr->qf_viscol == TRUE)
2321 {
2322 /*
2323 * Check each character from the beginning of the error
2324 * line up to the error column. For each tab character
2325 * found, reduce the error column value by the length of
2326 * a tab character.
2327 */
2328 line = ml_get_curline();
2329 screen_col = 0;
2330 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2331 {
2332 if (*line == NUL)
2333 break;
2334 if (*line++ == '\t')
2335 {
2336 curwin->w_cursor.col -= 7 - (screen_col % 8);
2337 screen_col += 8 - (screen_col % 8);
2338 }
2339 else
2340 ++screen_col;
2341 }
2342 }
2343 check_cursor();
2344 }
2345 else
2346 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
2348 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002349 {
2350 pos_T save_cursor;
2351
2352 /* Move the cursor to the first line in the buffer */
2353 save_cursor = curwin->w_cursor;
2354 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002355 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2356 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002357 curwin->w_cursor = save_cursor;
2358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360#ifdef FEAT_FOLDING
2361 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2362 foldOpenCursor();
2363#endif
2364 if (print_message)
2365 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002366 /* Update the screen before showing the message, unless the screen
2367 * scrolled up. */
2368 if (!msg_scrolled)
2369 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002371 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2373 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2374 /* Add the message, skipping leading whitespace and newlines. */
2375 len = (int)STRLEN(IObuff);
2376 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2377
2378 /* Output the message. Overwrite to avoid scrolling when the 'O'
2379 * flag is present in 'shortmess'; But when not jumping, print the
2380 * whole message. */
2381 i = msg_scroll;
2382 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2383 msg_scroll = TRUE;
2384 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2385 msg_scroll = FALSE;
2386 msg_attr_keep(IObuff, 0, TRUE);
2387 msg_scroll = i;
2388 }
2389 }
2390 else
2391 {
2392#ifdef FEAT_WINDOWS
2393 if (opened_window)
2394 win_close(curwin, TRUE); /* Close opened window */
2395#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002396 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
2398 /*
2399 * Couldn't open file, so put index back where it was. This could
2400 * happen if the file was readonly and we changed something.
2401 */
2402#ifdef FEAT_WINDOWS
2403failed:
2404#endif
2405 qf_ptr = old_qf_ptr;
2406 qf_index = old_qf_index;
2407 }
2408 }
2409theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002410 if (qi != NULL)
2411 {
2412 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2413 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415#ifdef FEAT_WINDOWS
2416 if (p_swb != old_swb && opened_window)
2417 {
2418 /* Restore old 'switchbuf' value, but not when an autocommand or
2419 * modeline has changed the value. */
2420 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002423 swb_flags = old_swb_flags;
2424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 else
2426 free_string_option(old_swb);
2427 }
2428#endif
2429}
2430
2431/*
2432 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 */
2435 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002436qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002438 buf_T *buf;
2439 char_u *fname;
2440 qfline_T *qfp;
2441 int i;
2442 int idx1 = 1;
2443 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002444 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002445 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002446 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002448 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002450 if (eap->cmdidx == CMD_llist)
2451 {
2452 qi = GET_LOC_LIST(curwin);
2453 if (qi == NULL)
2454 {
2455 EMSG(_(e_loclist));
2456 return;
2457 }
2458 }
2459
2460 if (qi->qf_curlist >= qi->qf_listcount
2461 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
2463 EMSG(_(e_quickfix));
2464 return;
2465 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002466 if (*arg == '+')
2467 {
2468 ++arg;
2469 plus = TRUE;
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2472 {
2473 EMSG(_(e_trailing));
2474 return;
2475 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002476 if (plus)
2477 {
2478 i = qi->qf_lists[qi->qf_curlist].qf_index;
2479 idx2 = i + idx1;
2480 idx1 = i;
2481 }
2482 else
2483 {
2484 i = qi->qf_lists[qi->qf_curlist].qf_count;
2485 if (idx1 < 0)
2486 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2487 if (idx2 < 0)
2488 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002491 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002493 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2494 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2497 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002498 msg_putchar('\n');
2499 if (got_int)
2500 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002501
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002502 fname = NULL;
2503 if (qfp->qf_fnum != 0
2504 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2505 {
2506 fname = buf->b_fname;
2507 if (qfp->qf_type == 1) /* :helpgrep */
2508 fname = gettail(fname);
2509 }
2510 if (fname == NULL)
2511 sprintf((char *)IObuff, "%2d", i);
2512 else
2513 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2514 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002515 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002516 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2517 if (qfp->qf_lnum == 0)
2518 IObuff[0] = NUL;
2519 else if (qfp->qf_col == 0)
2520 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2521 else
2522 sprintf((char *)IObuff, ":%ld col %d",
2523 qfp->qf_lnum, qfp->qf_col);
2524 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2525 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2526 msg_puts_attr(IObuff, hl_attr(HLF_N));
2527 if (qfp->qf_pattern != NULL)
2528 {
2529 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2530 STRCAT(IObuff, ":");
2531 msg_puts(IObuff);
2532 }
2533 msg_puts((char_u *)" ");
2534
2535 /* Remove newlines and leading whitespace from the text. For an
2536 * unrecognized line keep the indent, the compiler may mark a word
2537 * with ^^^^. */
2538 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2540 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002541 msg_prt_line(IObuff, FALSE);
2542 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002544
2545 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002546 if (qfp == NULL)
2547 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002548 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 ui_breakcheck();
2550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551}
2552
2553/*
2554 * Remove newlines and leading whitespace from an error message.
2555 * Put the result in "buf[bufsize]".
2556 */
2557 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002558qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560 int i;
2561 char_u *p = text;
2562
2563 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2564 {
2565 if (*p == '\n')
2566 {
2567 buf[i] = ' ';
2568 while (*++p != NUL)
2569 if (!vim_iswhite(*p) && *p != '\n')
2570 break;
2571 }
2572 else
2573 buf[i] = *p++;
2574 }
2575 buf[i] = NUL;
2576}
2577
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002578 static void
2579qf_msg(qf_info_T *qi, int which, char *lead)
2580{
2581 char *title = (char *)qi->qf_lists[which].qf_title;
2582 int count = qi->qf_lists[which].qf_count;
2583 char_u buf[IOSIZE];
2584
2585 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2586 lead,
2587 which + 1,
2588 qi->qf_listcount,
2589 count);
2590
2591 if (title != NULL)
2592 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002593 size_t len = STRLEN(buf);
2594
2595 if (len < 34)
2596 {
2597 vim_memset(buf + len, ' ', 34 - len);
2598 buf[34] = NUL;
2599 }
2600 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002601 }
2602 trunc_string(buf, buf, Columns - 1, IOSIZE);
2603 msg(buf);
2604}
2605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606/*
2607 * ":colder [count]": Up in the quickfix stack.
2608 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002609 * ":lolder [count]": Up in the location list stack.
2610 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 */
2612 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002613qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002615 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 int count;
2617
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002618 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2619 {
2620 qi = GET_LOC_LIST(curwin);
2621 if (qi == NULL)
2622 {
2623 EMSG(_(e_loclist));
2624 return;
2625 }
2626 }
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (eap->addr_count != 0)
2629 count = eap->line2;
2630 else
2631 count = 1;
2632 while (count--)
2633 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002634 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002636 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
2638 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002639 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002641 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643 else
2644 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002645 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
2647 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002648 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002650 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 }
2652 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002653 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002655 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656#endif
2657}
2658
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002659 void
2660qf_history(exarg_T *eap)
2661{
2662 qf_info_T *qi = &ql_info;
2663 int i;
2664
2665 if (eap->cmdidx == CMD_lhistory)
2666 qi = GET_LOC_LIST(curwin);
2667 if (qi == NULL || (qi->qf_listcount == 0
2668 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2669 MSG(_("No entries"));
2670 else
2671 for (i = 0; i < qi->qf_listcount; ++i)
2672 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2673}
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002676 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 */
2678 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002679qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002681 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002682 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002683 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002685 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002687 qfp = qi->qf_lists[idx].qf_start;
2688 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002689 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002690 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002691 vim_free(qfp->qf_text);
2692 stop = (qfp == qfpnext);
2693 vim_free(qfp->qf_pattern);
2694 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002695 if (stop)
2696 /* Somehow qf_count may have an incorrect value, set it to 1
2697 * to avoid crashing when it's wrong.
2698 * TODO: Avoid qf_count being incorrect. */
2699 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002700 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002701 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002702 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002704 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002705 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002706 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002707
2708 qf_clean_dir_stack(&qi->qf_dir_stack);
2709 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710}
2711
2712/*
2713 * qf_mark_adjust: adjust marks
2714 */
2715 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002716qf_mark_adjust(
2717 win_T *wp,
2718 linenr_T line1,
2719 linenr_T line2,
2720 long amount,
2721 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002723 int i;
2724 qfline_T *qfp;
2725 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002726 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002727 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002728 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
Bram Moolenaarc1542742016-07-20 21:44:37 +02002730 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002731 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002732 if (wp != NULL)
2733 {
2734 if (wp->w_llist == NULL)
2735 return;
2736 qi = wp->w_llist;
2737 }
2738
2739 for (idx = 0; idx < qi->qf_listcount; ++idx)
2740 if (qi->qf_lists[idx].qf_count)
2741 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002742 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2743 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 if (qfp->qf_fnum == curbuf->b_fnum)
2745 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002746 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2748 {
2749 if (amount == MAXLNUM)
2750 qfp->qf_cleared = TRUE;
2751 else
2752 qfp->qf_lnum += amount;
2753 }
2754 else if (amount_after && qfp->qf_lnum > line2)
2755 qfp->qf_lnum += amount_after;
2756 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002757
2758 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002759 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760}
2761
2762/*
2763 * Make a nice message out of the error character and the error number:
2764 * char number message
2765 * e or E 0 " error"
2766 * w or W 0 " warning"
2767 * i or I 0 " info"
2768 * 0 0 ""
2769 * other 0 " c"
2770 * e or E n " error n"
2771 * w or W n " warning n"
2772 * i or I n " info n"
2773 * 0 n " error n"
2774 * other n " c n"
2775 * 1 x "" :helpgrep
2776 */
2777 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002778qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
2780 static char_u buf[20];
2781 static char_u cc[3];
2782 char_u *p;
2783
2784 if (c == 'W' || c == 'w')
2785 p = (char_u *)" warning";
2786 else if (c == 'I' || c == 'i')
2787 p = (char_u *)" info";
2788 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2789 p = (char_u *)" error";
2790 else if (c == 0 || c == 1)
2791 p = (char_u *)"";
2792 else
2793 {
2794 cc[0] = ' ';
2795 cc[1] = c;
2796 cc[2] = NUL;
2797 p = cc;
2798 }
2799
2800 if (nr <= 0)
2801 return p;
2802
2803 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2804 return buf;
2805}
2806
2807#if defined(FEAT_WINDOWS) || defined(PROTO)
2808/*
2809 * ":cwindow": open the quickfix window if we have errors to display,
2810 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002811 * ":lwindow": open the location list window if we have locations to display,
2812 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 */
2814 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002815ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002817 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 win_T *win;
2819
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002820 if (eap->cmdidx == CMD_lwindow)
2821 {
2822 qi = GET_LOC_LIST(curwin);
2823 if (qi == NULL)
2824 return;
2825 }
2826
2827 /* Look for an existing quickfix window. */
2828 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
2830 /*
2831 * If a quickfix window is open but we have no errors to display,
2832 * close the window. If a quickfix window is not open, then open
2833 * it if we have errors; otherwise, leave it closed.
2834 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002835 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002836 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002837 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
2839 if (win != NULL)
2840 ex_cclose(eap);
2841 }
2842 else if (win == NULL)
2843 ex_copen(eap);
2844}
2845
2846/*
2847 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002848 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002851ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002853 win_T *win = NULL;
2854 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002856 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2857 {
2858 qi = GET_LOC_LIST(curwin);
2859 if (qi == NULL)
2860 return;
2861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002863 /* Find existing quickfix window and close it. */
2864 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 if (win != NULL)
2866 win_close(win, FALSE);
2867}
2868
2869/*
2870 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002871 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 */
2873 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002874ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002876 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002879 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002880 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002881 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002883 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2884 {
2885 qi = GET_LOC_LIST(curwin);
2886 if (qi == NULL)
2887 {
2888 EMSG(_(e_loclist));
2889 return;
2890 }
2891 }
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 if (eap->addr_count != 0)
2894 height = eap->line2;
2895 else
2896 height = QF_WINHEIGHT;
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899#ifdef FEAT_GUI
2900 need_mouse_correct = TRUE;
2901#endif
2902
2903 /*
2904 * Find existing quickfix window, or open a new one.
2905 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002906 win = qf_find_win(qi);
2907
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002908 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002911 if (eap->addr_count != 0)
2912 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002913 if (cmdmod.split & WSP_VERT)
2914 {
2915 if (height != W_WIDTH(win))
2916 win_setwidth(height);
2917 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002918 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002919 win_setheight(height);
2920 }
2921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 else
2923 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002924 qf_buf = qf_find_buf(qi);
2925
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 /* The current window becomes the previous window afterwards. */
2927 win = curwin;
2928
Bram Moolenaar77642c02012-11-20 17:55:10 +01002929 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2930 && cmdmod.split == 0)
2931 /* Create the new window at the very bottom, except when
2932 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002933 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002934 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002936 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002938 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002940 /*
2941 * For the location list window, create a reference to the
2942 * location list from the window 'win'.
2943 */
2944 curwin->w_llist_ref = win->w_llist;
2945 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002947
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002948 if (oldwin != curwin)
2949 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002950 if (qf_buf != NULL)
2951 /* Use the existing quickfix buffer */
2952 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002953 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002954 else
2955 {
2956 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002957 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002958 /* switch off 'swapfile' */
2959 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2960 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002961 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002962 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002963 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002964#ifdef FEAT_DIFF
2965 curwin->w_p_diff = FALSE;
2966#endif
2967#ifdef FEAT_FOLDING
2968 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2969 OPT_LOCAL);
2970#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002973 /* Only set the height when still in the same tab page and there is no
2974 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002975 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 win_setheight(height);
2977 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2978 if (win_valid(win))
2979 prevwin = win;
2980 }
2981
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002982 qf_set_title_var(qi);
2983
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 /*
2985 * Fill the buffer with the quickfix list.
2986 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002987 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002989 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 curwin->w_cursor.col = 0;
2991 check_cursor();
2992 update_topline(); /* scroll to show the line */
2993}
2994
2995/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02002996 * Move the cursor in the quickfix window to "lnum".
2997 */
2998 static void
2999qf_win_goto(win_T *win, linenr_T lnum)
3000{
3001 win_T *old_curwin = curwin;
3002
3003 curwin = win;
3004 curbuf = win->w_buffer;
3005 curwin->w_cursor.lnum = lnum;
3006 curwin->w_cursor.col = 0;
3007#ifdef FEAT_VIRTUALEDIT
3008 curwin->w_cursor.coladd = 0;
3009#endif
3010 curwin->w_curswant = 0;
3011 update_topline(); /* scroll to show the line */
3012 redraw_later(VALID);
3013 curwin->w_redr_status = TRUE; /* update ruler */
3014 curwin = old_curwin;
3015 curbuf = curwin->w_buffer;
3016}
3017
3018/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003019 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003020 */
3021 void
3022ex_cbottom(exarg_T *eap UNUSED)
3023{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003024 qf_info_T *qi = &ql_info;
3025 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003026
Bram Moolenaar537ef082016-07-09 17:56:19 +02003027 if (eap->cmdidx == CMD_lbottom)
3028 {
3029 qi = GET_LOC_LIST(curwin);
3030 if (qi == NULL)
3031 {
3032 EMSG(_(e_loclist));
3033 return;
3034 }
3035 }
3036
3037 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003038 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3039 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3040}
3041
3042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 * Return the number of the current entry (line number in the quickfix
3044 * window).
3045 */
3046 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003047qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003049 qf_info_T *qi = &ql_info;
3050
3051 if (IS_LL_WINDOW(wp))
3052 /* In the location list window, use the referenced location list */
3053 qi = wp->w_llist_ref;
3054
3055 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056}
3057
3058/*
3059 * Update the cursor position in the quickfix window to the current error.
3060 * Return TRUE if there is a quickfix window.
3061 */
3062 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003063qf_win_pos_update(
3064 qf_info_T *qi,
3065 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003068 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069
3070 /*
3071 * Put the cursor on the current error in the quickfix window, so that
3072 * it's viewable.
3073 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003074 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 if (win != NULL
3076 && qf_index <= win->w_buffer->b_ml.ml_line_count
3077 && old_qf_index != qf_index)
3078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (qf_index > old_qf_index)
3080 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003081 win->w_redraw_top = old_qf_index;
3082 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
3084 else
3085 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003086 win->w_redraw_top = qf_index;
3087 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003089 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
3091 return win != NULL;
3092}
3093
3094/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003095 * Check whether the given window is displaying the specified quickfix/location
3096 * list buffer
3097 */
3098 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003099is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003100{
3101 /*
3102 * A window displaying the quickfix buffer will have the w_llist_ref field
3103 * set to NULL.
3104 * A window displaying a location list buffer will have the w_llist_ref
3105 * pointing to the location list.
3106 */
3107 if (bt_quickfix(win->w_buffer))
3108 if ((qi == &ql_info && win->w_llist_ref == NULL)
3109 || (qi != &ql_info && win->w_llist_ref == qi))
3110 return TRUE;
3111
3112 return FALSE;
3113}
3114
3115/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003116 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003117 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003118 */
3119 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003120qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003121{
3122 win_T *win;
3123
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003124 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003125 if (is_qf_win(win, qi))
3126 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003127
3128 return win;
3129}
3130
3131/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003132 * Find a quickfix buffer.
3133 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 */
3135 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003136qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003138 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003139 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
Bram Moolenaar9c102382006-05-03 21:26:49 +00003141 FOR_ALL_TAB_WINDOWS(tp, win)
3142 if (is_qf_win(win, qi))
3143 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003144
Bram Moolenaar9c102382006-05-03 21:26:49 +00003145 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146}
3147
3148/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003149 * Update the w:quickfix_title variable in the quickfix/location list window
3150 */
3151 static void
3152qf_update_win_titlevar(qf_info_T *qi)
3153{
3154 win_T *win;
3155 win_T *curwin_save;
3156
3157 if ((win = qf_find_win(qi)) != NULL)
3158 {
3159 curwin_save = curwin;
3160 curwin = win;
3161 qf_set_title_var(qi);
3162 curwin = curwin_save;
3163 }
3164}
3165
3166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 * Find the quickfix buffer. If it exists, update the contents.
3168 */
3169 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003170qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003173 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003177 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 if (buf != NULL)
3179 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003180 linenr_T old_line_count = buf->b_ml.ml_line_count;
3181
3182 if (old_last == NULL)
3183 /* set curwin/curbuf to buf and save a few things */
3184 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
Bram Moolenaard823fa92016-08-12 16:29:27 +02003186 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003187
Bram Moolenaar864293a2016-06-02 13:40:04 +02003188 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003189
Bram Moolenaar864293a2016-06-02 13:40:04 +02003190 if (old_last == NULL)
3191 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003192 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003193
3194 /* restore curwin/curbuf and a few other things */
3195 aucmd_restbuf(&aco);
3196 }
3197
3198 /* Only redraw when added lines are visible. This avoids flickering
3199 * when the added lines are not visible. */
3200 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3201 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
3203}
3204
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003205/*
3206 * Set "w:quickfix_title" if "qi" has a title.
3207 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003208 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003209qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003210{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003211 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3212 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003213 qi->qf_lists[qi->qf_curlist].qf_title);
3214}
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216/*
3217 * Fill current buffer with quickfix errors, replacing any previous contents.
3218 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003219 * If "old_last" is not NULL append the items after this one.
3220 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3221 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 */
3223 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003224qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003226 linenr_T lnum;
3227 qfline_T *qfp;
3228 buf_T *errbuf;
3229 int len;
3230 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
Bram Moolenaar864293a2016-06-02 13:40:04 +02003232 if (old_last == NULL)
3233 {
3234 if (buf != curbuf)
3235 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003236 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003237 return;
3238 }
3239
3240 /* delete all existing lines */
3241 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3242 (void)ml_delete((linenr_T)1, FALSE);
3243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003246 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
3248 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003249 if (old_last == NULL)
3250 {
3251 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3252 lnum = 0;
3253 }
3254 else
3255 {
3256 qfp = old_last->qf_next;
3257 lnum = buf->b_ml.ml_line_count;
3258 }
3259 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 {
3261 if (qfp->qf_fnum != 0
3262 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3263 && errbuf->b_fname != NULL)
3264 {
3265 if (qfp->qf_type == 1) /* :helpgrep */
3266 STRCPY(IObuff, gettail(errbuf->b_fname));
3267 else
3268 STRCPY(IObuff, errbuf->b_fname);
3269 len = (int)STRLEN(IObuff);
3270 }
3271 else
3272 len = 0;
3273 IObuff[len++] = '|';
3274
3275 if (qfp->qf_lnum > 0)
3276 {
3277 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3278 len += (int)STRLEN(IObuff + len);
3279
3280 if (qfp->qf_col > 0)
3281 {
3282 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3283 len += (int)STRLEN(IObuff + len);
3284 }
3285
3286 sprintf((char *)IObuff + len, "%s",
3287 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3288 len += (int)STRLEN(IObuff + len);
3289 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003290 else if (qfp->qf_pattern != NULL)
3291 {
3292 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3293 len += (int)STRLEN(IObuff + len);
3294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 IObuff[len++] = '|';
3296 IObuff[len++] = ' ';
3297
3298 /* Remove newlines and leading whitespace from the text.
3299 * For an unrecognized line keep the indent, the compiler may
3300 * mark a word with ^^^^. */
3301 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3302 IObuff + len, IOSIZE - len);
3303
Bram Moolenaar864293a2016-06-02 13:40:04 +02003304 if (ml_append_buf(buf, lnum, IObuff,
3305 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003307 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003309 if (qfp == NULL)
3310 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003312
3313 if (old_last == NULL)
3314 /* Delete the empty line which is now at the end */
3315 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317
3318 /* correct cursor position */
3319 check_lnums(TRUE);
3320
Bram Moolenaar864293a2016-06-02 13:40:04 +02003321 if (old_last == NULL)
3322 {
3323 /* Set the 'filetype' to "qf" each time after filling the buffer.
3324 * This resembles reading a file into a buffer, it's more logical when
3325 * using autocommands. */
3326 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3327 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003330 keep_filetype = TRUE; /* don't detect 'filetype' */
3331 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003333 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003335 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003337 /* make sure it will be redrawn */
3338 redraw_curbuf_later(NOT_VALID);
3339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
3341 /* Restore KeyTyped, setting 'filetype' may reset it. */
3342 KeyTyped = old_KeyTyped;
3343}
3344
3345#endif /* FEAT_WINDOWS */
3346
3347/*
3348 * Return TRUE if "buf" is the quickfix buffer.
3349 */
3350 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003351bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003353 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354}
3355
3356/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003357 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3358 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 */
3360 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003361bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003363 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3364 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365}
3366
3367/*
3368 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3369 */
3370 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003371bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003373 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374}
3375
3376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003377bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 if (bt_dontwrite(buf))
3380 {
3381 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3382 return TRUE;
3383 }
3384 return FALSE;
3385}
3386
3387/*
3388 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3389 * and 'bufhidden'.
3390 */
3391 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003392buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3395 switch (buf->b_p_bh[0])
3396 {
3397 case 'u': /* "unload" */
3398 case 'w': /* "wipe" */
3399 case 'd': return FALSE; /* "delete" */
3400 case 'h': return TRUE; /* "hide" */
3401 }
3402 return (p_hid || cmdmod.hide);
3403}
3404
3405/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003406 * Return TRUE when using ":vimgrep" for ":grep".
3407 */
3408 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003409grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003410{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003411 return ((cmdidx == CMD_grep
3412 || cmdidx == CMD_lgrep
3413 || cmdidx == CMD_grepadd
3414 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003415 && STRCMP("internal",
3416 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3417}
3418
3419/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003420 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 */
3422 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003423ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003425 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 char_u *cmd;
3427 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003428 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003429 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003430 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003431#ifdef FEAT_AUTOCMD
3432 char_u *au_name = NULL;
3433
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003434 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3435 if (grep_internal(eap->cmdidx))
3436 {
3437 ex_vimgrep(eap);
3438 return;
3439 }
3440
Bram Moolenaar7c626922005-02-07 22:01:03 +00003441 switch (eap->cmdidx)
3442 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003443 case CMD_make: au_name = (char_u *)"make"; break;
3444 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3445 case CMD_grep: au_name = (char_u *)"grep"; break;
3446 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3447 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3448 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003449 default: break;
3450 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003451 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3452 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003453 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003454# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003455 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003456 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003457# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003458 }
3459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaara6557602006-02-04 22:43:20 +00003461 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3462 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003463 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003466 fname = get_mef_name();
3467 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003469 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471 /*
3472 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3473 */
3474 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3475 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003476 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 cmd = alloc(len);
3478 if (cmd == NULL)
3479 return;
3480 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3481 (char *)p_shq);
3482 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003483 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 /*
3485 * Output a newline if there's something else than the :make command that
3486 * was typed (in which case the cursor is in column 0).
3487 */
3488 if (msg_col == 0)
3489 msg_didout = FALSE;
3490 msg_start();
3491 MSG_PUTS(":!");
3492 msg_outtrans(cmd); /* show what we are doing */
3493
3494 /* let the shell know if we are redirecting output or not */
3495 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3496
3497#ifdef AMIGA
3498 out_flush();
3499 /* read window status report and redraw before message */
3500 (void)char_avail();
3501#endif
3502
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003503 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003504 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3505 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003506 && eap->cmdidx != CMD_lgrepadd),
3507 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003508 if (wp != NULL)
3509 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003510#ifdef FEAT_AUTOCMD
3511 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003512 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003513 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3514 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003515 if (qi->qf_curlist < qi->qf_listcount)
3516 res = qi->qf_lists[qi->qf_curlist].qf_count;
3517 else
3518 res = 0;
3519 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003520#endif
3521 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003522 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
Bram Moolenaar7c626922005-02-07 22:01:03 +00003524 mch_remove(fname);
3525 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 vim_free(cmd);
3527}
3528
3529/*
3530 * Return the name for the errorfile, in allocated memory.
3531 * Find a new unique name when 'makeef' contains "##".
3532 * Returns NULL for error.
3533 */
3534 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003535get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536{
3537 char_u *p;
3538 char_u *name;
3539 static int start = -1;
3540 static int off = 0;
3541#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003542 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543#endif
3544
3545 if (*p_mef == NUL)
3546 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003547 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (name == NULL)
3549 EMSG(_(e_notmp));
3550 return name;
3551 }
3552
3553 for (p = p_mef; *p; ++p)
3554 if (p[0] == '#' && p[1] == '#')
3555 break;
3556
3557 if (*p == NUL)
3558 return vim_strsave(p_mef);
3559
3560 /* Keep trying until the name doesn't exist yet. */
3561 for (;;)
3562 {
3563 if (start == -1)
3564 start = mch_get_pid();
3565 else
3566 off += 19;
3567
3568 name = alloc((unsigned)STRLEN(p_mef) + 30);
3569 if (name == NULL)
3570 break;
3571 STRCPY(name, p_mef);
3572 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3573 STRCAT(name, p + 2);
3574 if (mch_getperm(name) < 0
3575#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003576 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 && mch_lstat((char *)name, &sb) < 0
3578#endif
3579 )
3580 break;
3581 vim_free(name);
3582 }
3583 return name;
3584}
3585
3586/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003587 * Returns the number of valid entries in the current quickfix/location list.
3588 */
3589 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003590qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003591{
3592 qf_info_T *qi = &ql_info;
3593 qfline_T *qfp;
3594 int i, sz = 0;
3595 int prev_fnum = 0;
3596
3597 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3598 {
3599 /* Location list */
3600 qi = GET_LOC_LIST(curwin);
3601 if (qi == NULL)
3602 return 0;
3603 }
3604
3605 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003606 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003607 ++i, qfp = qfp->qf_next)
3608 {
3609 if (qfp->qf_valid)
3610 {
3611 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3612 sz++; /* Count all valid entries */
3613 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3614 {
3615 /* Count the number of files */
3616 sz++;
3617 prev_fnum = qfp->qf_fnum;
3618 }
3619 }
3620 }
3621
3622 return sz;
3623}
3624
3625/*
3626 * Returns the current index of the quickfix/location list.
3627 * Returns 0 if there is an error.
3628 */
3629 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003630qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003631{
3632 qf_info_T *qi = &ql_info;
3633
3634 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3635 {
3636 /* Location list */
3637 qi = GET_LOC_LIST(curwin);
3638 if (qi == NULL)
3639 return 0;
3640 }
3641
3642 return qi->qf_lists[qi->qf_curlist].qf_index;
3643}
3644
3645/*
3646 * Returns the current index in the quickfix/location list (counting only valid
3647 * entries). If no valid entries are in the list, then returns 1.
3648 */
3649 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003650qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003651{
3652 qf_info_T *qi = &ql_info;
3653 qf_list_T *qfl;
3654 qfline_T *qfp;
3655 int i, eidx = 0;
3656 int prev_fnum = 0;
3657
3658 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3659 {
3660 /* Location list */
3661 qi = GET_LOC_LIST(curwin);
3662 if (qi == NULL)
3663 return 1;
3664 }
3665
3666 qfl = &qi->qf_lists[qi->qf_curlist];
3667 qfp = qfl->qf_start;
3668
3669 /* check if the list has valid errors */
3670 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3671 return 1;
3672
3673 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3674 {
3675 if (qfp->qf_valid)
3676 {
3677 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3678 {
3679 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3680 {
3681 /* Count the number of files */
3682 eidx++;
3683 prev_fnum = qfp->qf_fnum;
3684 }
3685 }
3686 else
3687 eidx++;
3688 }
3689 }
3690
3691 return eidx ? eidx : 1;
3692}
3693
3694/*
3695 * Get the 'n'th valid error entry in the quickfix or location list.
3696 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3697 * For :cdo and :ldo returns the 'n'th valid error entry.
3698 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3699 */
3700 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003701qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003702{
3703 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3704 qfline_T *qfp = qfl->qf_start;
3705 int i, eidx;
3706 int prev_fnum = 0;
3707
3708 /* check if the list has valid errors */
3709 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3710 return 1;
3711
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003712 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003713 i++, qfp = qfp->qf_next)
3714 {
3715 if (qfp->qf_valid)
3716 {
3717 if (fdo)
3718 {
3719 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3720 {
3721 /* Count the number of files */
3722 eidx++;
3723 prev_fnum = qfp->qf_fnum;
3724 }
3725 }
3726 else
3727 eidx++;
3728 }
3729
3730 if (eidx == n)
3731 break;
3732 }
3733
3734 if (i <= qfl->qf_count)
3735 return i;
3736 else
3737 return 1;
3738}
3739
3740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003742 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003743 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 */
3745 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003746ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003748 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003749 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003750
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003751 if (eap->cmdidx == CMD_ll
3752 || eap->cmdidx == CMD_lrewind
3753 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003754 || eap->cmdidx == CMD_llast
3755 || eap->cmdidx == CMD_ldo
3756 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003757 {
3758 qi = GET_LOC_LIST(curwin);
3759 if (qi == NULL)
3760 {
3761 EMSG(_(e_loclist));
3762 return;
3763 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003764 }
3765
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003766 if (eap->addr_count > 0)
3767 errornr = (int)eap->line2;
3768 else
3769 {
3770 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3771 errornr = 0;
3772 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3773 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3774 errornr = 1;
3775 else
3776 errornr = 32767;
3777 }
3778
3779 /* For cdo and ldo commands, jump to the nth valid error.
3780 * For cfdo and lfdo commands, jump to the nth valid file entry.
3781 */
3782 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3783 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3784 errornr = qf_get_nth_valid_entry(qi,
3785 eap->addr_count > 0 ? (int)eap->line1 : 1,
3786 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3787
3788 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789}
3790
3791/*
3792 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003793 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003794 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 */
3796 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003797ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003799 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003800 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003801
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003802 if (eap->cmdidx == CMD_lnext
3803 || eap->cmdidx == CMD_lNext
3804 || eap->cmdidx == CMD_lprevious
3805 || eap->cmdidx == CMD_lnfile
3806 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003807 || eap->cmdidx == CMD_lpfile
3808 || eap->cmdidx == CMD_ldo
3809 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003810 {
3811 qi = GET_LOC_LIST(curwin);
3812 if (qi == NULL)
3813 {
3814 EMSG(_(e_loclist));
3815 return;
3816 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003817 }
3818
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003819 if (eap->addr_count > 0 &&
3820 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3821 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3822 errornr = (int)eap->line2;
3823 else
3824 errornr = 1;
3825
3826 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3827 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003829 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3830 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003832 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3833 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 ? BACKWARD_FILE
3835 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003836 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837}
3838
3839/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003840 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003841 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 */
3843 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003844ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003846 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003847 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003848#ifdef FEAT_AUTOCMD
3849 char_u *au_name = NULL;
3850#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003851
3852 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003853 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003854 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003855
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003856#ifdef FEAT_AUTOCMD
3857 switch (eap->cmdidx)
3858 {
3859 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3860 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3861 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3862 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3863 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3864 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3865 default: break;
3866 }
3867 if (au_name != NULL)
3868 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3869#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003870#ifdef FEAT_BROWSE
3871 if (cmdmod.browse)
3872 {
3873 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3874 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3875 if (browse_file == NULL)
3876 return;
3877 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3878 vim_free(browse_file);
3879 }
3880 else
3881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003883 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003884
3885 /*
3886 * This function is used by the :cfile, :cgetfile and :caddfile
3887 * commands.
3888 * :cfile always creates a new quickfix list and jumps to the
3889 * first error.
3890 * :cgetfile creates a new quickfix list but doesn't jump to the
3891 * first error.
3892 * :caddfile adds to an existing quickfix list. If there is no
3893 * quickfix list then a new list is created.
3894 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003895 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003896 && eap->cmdidx != CMD_laddfile),
3897 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003898 && (eap->cmdidx == CMD_cfile
3899 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003900 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003901#ifdef FEAT_AUTOCMD
3902 if (au_name != NULL)
3903 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3904#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003905 if (wp != NULL)
3906 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003907 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003908 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003909
3910 else
3911 {
3912#ifdef FEAT_AUTOCMD
3913 if (au_name != NULL)
3914 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3915#endif
3916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917}
3918
3919/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003920 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003921 * ":vimgrepadd {pattern} file(s)"
3922 * ":lvimgrep {pattern} file(s)"
3923 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003924 */
3925 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003926ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003927{
Bram Moolenaar81695252004-12-29 20:58:21 +00003928 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003929 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003930 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003931 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003932 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003933 char_u *s;
3934 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003935 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003936 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003937#ifdef FEAT_AUTOCMD
3938 qfline_T *cur_qf_start;
3939#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003940 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003941 buf_T *buf;
3942 int duplicate_name = FALSE;
3943 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003944 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003945 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003946 buf_T *first_match_buf = NULL;
3947 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003948 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003949#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3950 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003951#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003952 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003953 int flags = 0;
3954 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003955 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003956 char_u *dirname_start = NULL;
3957 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003958 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003959#ifdef FEAT_AUTOCMD
3960 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003961
3962 switch (eap->cmdidx)
3963 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003964 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3965 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3966 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003967 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003968 case CMD_grep: au_name = (char_u *)"grep"; break;
3969 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3970 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3971 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003972 default: break;
3973 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003974 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3975 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003976 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01003977# ifdef FEAT_EVAL
3978 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003979 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01003980# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003981 }
3982#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003983
Bram Moolenaar754b5602006-02-09 23:53:20 +00003984 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003985 || eap->cmdidx == CMD_lvimgrep
3986 || eap->cmdidx == CMD_lgrepadd
3987 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003988 {
3989 qi = ll_get_or_alloc_list(curwin);
3990 if (qi == NULL)
3991 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003992 }
3993
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003994 if (eap->addr_count > 0)
3995 tomatch = eap->line2;
3996 else
3997 tomatch = MAXLNUM;
3998
Bram Moolenaar81695252004-12-29 20:58:21 +00003999 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004000 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004001 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004002 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004003 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004004 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004005 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004006 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004007 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004008
4009 if (s != NULL && *s == NUL)
4010 {
4011 /* Pattern is empty, use last search pattern. */
4012 if (last_search_pat() == NULL)
4013 {
4014 EMSG(_(e_noprevre));
4015 goto theend;
4016 }
4017 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4018 }
4019 else
4020 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4021
Bram Moolenaar86b68352004-12-27 21:59:20 +00004022 if (regmatch.regprog == NULL)
4023 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004024 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004025 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004026
4027 p = skipwhite(p);
4028 if (*p == NUL)
4029 {
4030 EMSG(_("E683: File name missing or invalid pattern"));
4031 goto theend;
4032 }
4033
Bram Moolenaar754b5602006-02-09 23:53:20 +00004034 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004035 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004036 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004037 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004038 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004039
4040 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004041 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004042 goto theend;
4043 if (fcount == 0)
4044 {
4045 EMSG(_(e_nomatch));
4046 goto theend;
4047 }
4048
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004049 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4050 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004051 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004052 {
4053 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004054 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004055 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004056
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004057 /* Remember the current directory, because a BufRead autocommand that does
4058 * ":lcd %:p:h" changes the meaning of short path names. */
4059 mch_dirname(dirname_start, MAXPATHL);
4060
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004061#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004062 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004063 * changing the current quickfix list. */
4064 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4065#endif
4066
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004067 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004068 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004069 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004070 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004071 if (time(NULL) > seconds)
4072 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004073 /* Display the file name every second or so, show the user we are
4074 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004075 seconds = time(NULL);
4076 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004077 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004078 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004079 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004080 else
4081 {
4082 msg_outtrans(p);
4083 vim_free(p);
4084 }
4085 msg_clr_eos();
4086 msg_didout = FALSE; /* overwrite this message */
4087 msg_nowait = TRUE; /* don't wait for this message */
4088 msg_col = 0;
4089 out_flush();
4090 }
4091
Bram Moolenaar81695252004-12-29 20:58:21 +00004092 buf = buflist_findname_exp(fnames[fi]);
4093 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4094 {
4095 /* Remember that a buffer with this name already exists. */
4096 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004097 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004098 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004099
4100#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4101 /* Don't do Filetype autocommands to avoid loading syntax and
4102 * indent scripts, a great speed improvement. */
4103 save_ei = au_event_disable(",Filetype");
4104#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004105 /* Don't use modelines here, it's useless. */
4106 save_mls = p_mls;
4107 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004108
4109 /* Load file into a buffer, so that 'fileencoding' is detected,
4110 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004111 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004112
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004113 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004114#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4115 au_event_restore(save_ei);
4116#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004117 }
4118 else
4119 /* Use existing, loaded buffer. */
4120 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004121
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004122#ifdef FEAT_AUTOCMD
4123 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4124 {
4125 int idx;
4126
4127 /* Autocommands changed the quickfix list. Find the one we were
4128 * using and restore it. */
4129 for (idx = 0; idx < LISTCOUNT; ++idx)
4130 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4131 {
4132 qi->qf_curlist = idx;
4133 break;
4134 }
4135 if (idx == LISTCOUNT)
4136 {
4137 /* List cannot be found, create a new one. */
4138 qf_new_list(qi, *eap->cmdlinep);
4139 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4140 }
4141 }
4142#endif
4143
Bram Moolenaar81695252004-12-29 20:58:21 +00004144 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004145 {
4146 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004147 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004148 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004149 else
4150 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004151 /* Try for a match in all lines of the buffer.
4152 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004153 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004154 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4155 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004156 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004157 col = 0;
4158 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004159 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004160 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004161 /* Pass the buffer number so that it gets used even for a
4162 * dummy buffer, unless duplicate_name is set, then the
4163 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004164 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004165 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004166 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004167 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004168 ml_get_buf(buf,
4169 regmatch.startpos[0].lnum + lnum, FALSE),
4170 regmatch.startpos[0].lnum + lnum,
4171 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004172 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004173 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004174 0, /* nr */
4175 0, /* type */
4176 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004177 ) == FAIL)
4178 {
4179 got_int = TRUE;
4180 break;
4181 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004182 found_match = TRUE;
4183 if (--tomatch == 0)
4184 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004185 if ((flags & VGR_GLOBAL) == 0
4186 || regmatch.endpos[0].lnum > 0)
4187 break;
4188 col = regmatch.endpos[0].col
4189 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004190 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004191 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004192 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004193 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004194 if (got_int)
4195 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004196 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004197#ifdef FEAT_AUTOCMD
4198 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4199#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004200
4201 if (using_dummy)
4202 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004203 if (found_match && first_match_buf == NULL)
4204 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004205 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004206 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004207 /* Never keep a dummy buffer if there is another buffer
4208 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004209 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004210 buf = NULL;
4211 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004212 else if (!cmdmod.hide
4213 || buf->b_p_bh[0] == 'u' /* "unload" */
4214 || buf->b_p_bh[0] == 'w' /* "wipe" */
4215 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004216 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004217 /* When no match was found we don't need to remember the
4218 * buffer, wipe it out. If there was a match and it
4219 * wasn't the first one or we won't jump there: only
4220 * unload the buffer.
4221 * Ignore 'hidden' here, because it may lead to having too
4222 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004223 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004224 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004225 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004226 buf = NULL;
4227 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004228 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004229 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004230 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004231 /* Keeping the buffer, remove the dummy flag. */
4232 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004233 buf = NULL;
4234 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004235 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004236
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004237 if (buf != NULL)
4238 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004239 /* Keeping the buffer, remove the dummy flag. */
4240 buf->b_flags &= ~BF_DUMMY;
4241
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004242 /* If the buffer is still loaded we need to use the
4243 * directory we jumped to below. */
4244 if (buf == first_match_buf
4245 && target_dir == NULL
4246 && STRCMP(dirname_start, dirname_now) != 0)
4247 target_dir = vim_strsave(dirname_now);
4248
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004249 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004250 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004251 * need to be done (again). But not the window-local
4252 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004253 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004254#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004255 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4256 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004257#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004258 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004259 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004260 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004261 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004262 }
4263 }
4264
4265 FreeWild(fcount, fnames);
4266
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004267 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4268 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4269 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004270
4271#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004272 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004273#endif
4274
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004275#ifdef FEAT_AUTOCMD
4276 if (au_name != NULL)
4277 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4278 curbuf->b_fname, TRUE, curbuf);
4279#endif
4280
Bram Moolenaar86b68352004-12-27 21:59:20 +00004281 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004282 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004283 {
4284 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004285 {
4286 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004287 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004288 if (buf != curbuf)
4289 /* If we jumped to another buffer redrawing will already be
4290 * taken care of. */
4291 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004292
4293 /* Jump to the directory used after loading the buffer. */
4294 if (curbuf == first_match_buf && target_dir != NULL)
4295 {
4296 exarg_T ea;
4297
4298 ea.arg = target_dir;
4299 ea.cmdidx = CMD_lcd;
4300 ex_cd(&ea);
4301 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004302 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004303 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004304 else
4305 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004306
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004307 /* If we loaded a dummy buffer into the current window, the autocommands
4308 * may have messed up things, need to redraw and recompute folds. */
4309 if (redraw_for_dummy)
4310 {
4311#ifdef FEAT_FOLDING
4312 foldUpdateAll(curwin);
4313#else
4314 redraw_later(NOT_VALID);
4315#endif
4316 }
4317
Bram Moolenaar86b68352004-12-27 21:59:20 +00004318theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004319 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004320 vim_free(dirname_now);
4321 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004322 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004323 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004324}
4325
4326/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004327 * Restore current working directory to "dirname_start" if they differ, taking
4328 * into account whether it is set locally or globally.
4329 */
4330 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004331restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004332{
4333 char_u *dirname_now = alloc(MAXPATHL);
4334
4335 if (NULL != dirname_now)
4336 {
4337 mch_dirname(dirname_now, MAXPATHL);
4338 if (STRCMP(dirname_start, dirname_now) != 0)
4339 {
4340 /* If the directory has changed, change it back by building up an
4341 * appropriate ex command and executing it. */
4342 exarg_T ea;
4343
4344 ea.arg = dirname_start;
4345 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4346 ex_cd(&ea);
4347 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004348 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004349 }
4350}
4351
4352/*
4353 * Load file "fname" into a dummy buffer and return the buffer pointer,
4354 * placing the directory resulting from the buffer load into the
4355 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4356 * prior to calling this function. Restores directory to "dirname_start" prior
4357 * to returning, if autocmds or the 'autochdir' option have changed it.
4358 *
4359 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4360 * or wipe_dummy_buffer() later!
4361 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004362 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004363 */
4364 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004365load_dummy_buffer(
4366 char_u *fname,
4367 char_u *dirname_start, /* in: old directory */
4368 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004369{
4370 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004371 bufref_T newbufref;
4372 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004373 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004374 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004375
4376 /* Allocate a buffer without putting it in the buffer list. */
4377 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4378 if (newbuf == NULL)
4379 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004380 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004381
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004382 /* Init the options. */
4383 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4384
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004385 /* need to open the memfile before putting the buffer in a window */
4386 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004387 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004388 /* set curwin/curbuf to buf and save a few things */
4389 aucmd_prepbuf(&aco, newbuf);
4390
4391 /* Need to set the filename for autocommands. */
4392 (void)setfname(curbuf, fname, NULL, FALSE);
4393
Bram Moolenaar81695252004-12-29 20:58:21 +00004394 /* Create swap file now to avoid the ATTENTION message. */
4395 check_need_swap(TRUE);
4396
4397 /* Remove the "dummy" flag, otherwise autocommands may not
4398 * work. */
4399 curbuf->b_flags &= ~BF_DUMMY;
4400
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004401 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004402 if (readfile(fname, NULL,
4403 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4404 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004405 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004406 && !(curbuf->b_flags & BF_NEW))
4407 {
4408 failed = FALSE;
4409 if (curbuf != newbuf)
4410 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004411 /* Bloody autocommands changed the buffer! Can happen when
4412 * using netrw and editing a remote file. Use the current
4413 * buffer instead, delete the dummy one after restoring the
4414 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004415 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004416 newbuf = curbuf;
4417 }
4418 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004419
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004420 /* restore curwin/curbuf and a few other things */
4421 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004422 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4423 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004424
4425 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4426 * skip it. */
4427 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004428 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004429
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004430 /*
4431 * When autocommands/'autochdir' option changed directory: go back.
4432 * Let the caller know what the resulting dir was first, in case it is
4433 * important.
4434 */
4435 mch_dirname(resulting_dir, MAXPATHL);
4436 restore_start_dir(dirname_start);
4437
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004438 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004439 return NULL;
4440 if (failed)
4441 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004442 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004443 return NULL;
4444 }
4445 return newbuf;
4446}
4447
4448/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004449 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4450 * directory to "dirname_start" prior to returning, if autocmds or the
4451 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004452 */
4453 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004454wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004455{
4456 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004457 {
4458#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4459 cleanup_T cs;
4460
4461 /* Reset the error/interrupt/exception state here so that aborting()
4462 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4463 * work when got_int is set. */
4464 enter_cleanup(&cs);
4465#endif
4466
Bram Moolenaar81695252004-12-29 20:58:21 +00004467 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004468
4469#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4470 /* Restore the error/interrupt/exception state if not discarded by a
4471 * new aborting error, interrupt, or uncaught exception. */
4472 leave_cleanup(&cs);
4473#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004474 /* When autocommands/'autochdir' option changed directory: go back. */
4475 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004476 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004477}
4478
4479/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004480 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4481 * directory to "dirname_start" prior to returning, if autocmds or the
4482 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004483 */
4484 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004485unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004486{
4487 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004488 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004489 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004490
4491 /* When autocommands/'autochdir' option changed directory: go back. */
4492 restore_start_dir(dirname_start);
4493 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004494}
4495
Bram Moolenaar05159a02005-02-26 23:04:13 +00004496#if defined(FEAT_EVAL) || defined(PROTO)
4497/*
4498 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004499 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004500 */
4501 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004502get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004503{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004504 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004505 dict_T *dict;
4506 char_u buf[2];
4507 qfline_T *qfp;
4508 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004509 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004510
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004511 if (wp != NULL)
4512 {
4513 qi = GET_LOC_LIST(wp);
4514 if (qi == NULL)
4515 return FAIL;
4516 }
4517
Bram Moolenaard823fa92016-08-12 16:29:27 +02004518 if (qf_idx == -1)
4519 qf_idx = qi->qf_curlist;
4520
4521 if (qf_idx >= qi->qf_listcount
4522 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004523 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004524
Bram Moolenaard823fa92016-08-12 16:29:27 +02004525 qfp = qi->qf_lists[qf_idx].qf_start;
4526 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004527 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004528 /* Handle entries with a non-existing buffer number. */
4529 bufnum = qfp->qf_fnum;
4530 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4531 bufnum = 0;
4532
Bram Moolenaar05159a02005-02-26 23:04:13 +00004533 if ((dict = dict_alloc()) == NULL)
4534 return FAIL;
4535 if (list_append_dict(list, dict) == FAIL)
4536 return FAIL;
4537
4538 buf[0] = qfp->qf_type;
4539 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004540 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004541 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4542 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4543 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4544 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004545 || dict_add_nr_str(dict, "pattern", 0L,
4546 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4547 || dict_add_nr_str(dict, "text", 0L,
4548 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004549 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4550 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4551 return FAIL;
4552
4553 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004554 if (qfp == NULL)
4555 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004556 }
4557 return OK;
4558}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004559
4560/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004561 * Flags used by getqflist()/getloclist() to determine which fields to return.
4562 */
4563enum {
4564 QF_GETLIST_NONE = 0x0,
4565 QF_GETLIST_TITLE = 0x1,
4566 QF_GETLIST_ITEMS = 0x2,
4567 QF_GETLIST_NR = 0x4,
4568 QF_GETLIST_WINID = 0x8,
4569 QF_GETLIST_ALL = 0xFF
4570};
4571
4572/*
4573 * Return quickfix/location list details (title) as a
4574 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4575 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004576 */
4577 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004578get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4579{
4580 qf_info_T *qi = &ql_info;
4581 int status = OK;
4582 int qf_idx;
4583 dictitem_T *di;
4584 int flags = QF_GETLIST_NONE;
4585
4586 if (wp != NULL)
4587 {
4588 qi = GET_LOC_LIST(wp);
4589 if (qi == NULL)
4590 return FAIL;
4591 }
4592
4593 qf_idx = qi->qf_curlist; /* default is the current list */
4594 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4595 {
4596 /* Use the specified quickfix/location list */
4597 if (di->di_tv.v_type == VAR_NUMBER)
4598 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004599 /* for zero use the current list */
4600 if (di->di_tv.vval.v_number != 0)
4601 {
4602 qf_idx = di->di_tv.vval.v_number - 1;
4603 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4604 return FAIL;
4605 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004606 flags |= QF_GETLIST_NR;
4607 }
4608 else
4609 return FAIL;
4610 }
4611
4612 if (dict_find(what, (char_u *)"all", -1) != NULL)
4613 flags |= QF_GETLIST_ALL;
4614
4615 if (dict_find(what, (char_u *)"title", -1) != NULL)
4616 flags |= QF_GETLIST_TITLE;
4617
4618 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4619 flags |= QF_GETLIST_WINID;
4620
4621 if (flags & QF_GETLIST_TITLE)
4622 {
4623 char_u *t;
4624 t = qi->qf_lists[qf_idx].qf_title;
4625 if (t == NULL)
4626 t = (char_u *)"";
4627 status = dict_add_nr_str(retdict, "title", 0L, t);
4628 }
4629 if ((status == OK) && (flags & QF_GETLIST_NR))
4630 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4631 if ((status == OK) && (flags & QF_GETLIST_WINID))
4632 {
4633 win_T *win;
4634 win = qf_find_win(qi);
4635 if (win != NULL)
4636 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4637 }
4638
4639 return status;
4640}
4641
4642/*
4643 * Add list of entries to quickfix/location list. Each list entry is
4644 * a dictionary with item information.
4645 */
4646 static int
4647qf_add_entries(
4648 qf_info_T *qi,
4649 list_T *list,
4650 char_u *title,
4651 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004652{
4653 listitem_T *li;
4654 dict_T *d;
4655 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004656 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004657 long lnum;
4658 int col, nr;
4659 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004660#ifdef FEAT_WINDOWS
4661 qfline_T *old_last = NULL;
4662#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004663 int valid, status;
4664 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004665 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004666
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004667 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004668 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004669 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004670#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004671 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4672 /* Adding to existing list, use last entry. */
4673 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004674#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004675 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004676 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004677 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004678 qf_store_title(qi, title);
4679 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004680
4681 for (li = list->lv_first; li != NULL; li = li->li_next)
4682 {
4683 if (li->li_tv.v_type != VAR_DICT)
4684 continue; /* Skip non-dict items */
4685
4686 d = li->li_tv.vval.v_dict;
4687 if (d == NULL)
4688 continue;
4689
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004690 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004691 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4692 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4693 col = (int)get_dict_number(d, (char_u *)"col");
4694 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4695 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004696 type = get_dict_string(d, (char_u *)"type", TRUE);
4697 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4698 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004699 if (text == NULL)
4700 text = vim_strsave((char_u *)"");
4701
4702 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004703 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004704 valid = FALSE;
4705
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004706 /* Mark entries with non-existing buffer number as not valid. Give the
4707 * error message only once. */
4708 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4709 {
4710 if (!did_bufnr_emsg)
4711 {
4712 did_bufnr_emsg = TRUE;
4713 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4714 }
4715 valid = FALSE;
4716 bufnum = 0;
4717 }
4718
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004719 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004720 NULL, /* dir */
4721 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004722 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004723 text,
4724 lnum,
4725 col,
4726 vcol, /* vis_col */
4727 pattern, /* search pattern */
4728 nr,
4729 type == NULL ? NUL : *type,
4730 valid);
4731
4732 vim_free(filename);
4733 vim_free(pattern);
4734 vim_free(text);
4735 vim_free(type);
4736
4737 if (status == FAIL)
4738 {
4739 retval = FAIL;
4740 break;
4741 }
4742 }
4743
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004744 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004745 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004746 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4747 else
4748 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004749 if (action != 'a') {
4750 qi->qf_lists[qi->qf_curlist].qf_ptr =
4751 qi->qf_lists[qi->qf_curlist].qf_start;
4752 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4753 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4754 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004755
4756#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004757 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004758 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004759#endif
4760
4761 return retval;
4762}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004763
4764 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004765qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004766{
4767 dictitem_T *di;
4768 int retval = FAIL;
4769 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004770 int newlist = FALSE;
4771
4772 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4773 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004774
4775 qf_idx = qi->qf_curlist; /* default is the current list */
4776 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4777 {
4778 /* Use the specified quickfix/location list */
4779 if (di->di_tv.v_type == VAR_NUMBER)
4780 {
4781 qf_idx = di->di_tv.vval.v_number - 1;
4782 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4783 return FAIL;
4784 }
4785 else
4786 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004787 newlist = FALSE; /* use the specified list */
4788 }
4789
4790 if (newlist)
4791 {
4792 qf_new_list(qi, NULL);
4793 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004794 }
4795
4796 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4797 {
4798 if (di->di_tv.v_type == VAR_STRING)
4799 {
4800 vim_free(qi->qf_lists[qf_idx].qf_title);
4801 qi->qf_lists[qf_idx].qf_title =
4802 get_dict_string(what, (char_u *)"title", TRUE);
4803 if (qf_idx == qi->qf_curlist)
4804 qf_update_win_titlevar(qi);
4805 retval = OK;
4806 }
4807 }
4808
4809 return retval;
4810}
4811
4812/*
4813 * Populate the quickfix list with the items supplied in the list
4814 * of dictionaries. "title" will be copied to w:quickfix_title.
4815 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
4816 */
4817 int
4818set_errorlist(
4819 win_T *wp,
4820 list_T *list,
4821 int action,
4822 char_u *title,
4823 dict_T *what)
4824{
4825 qf_info_T *qi = &ql_info;
4826 int retval = OK;
4827
4828 if (wp != NULL)
4829 {
4830 qi = ll_get_or_alloc_list(wp);
4831 if (qi == NULL)
4832 return FAIL;
4833 }
4834
4835 if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004836 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004837 else
4838 retval = qf_add_entries(qi, list, title, action);
4839
4840 return retval;
4841}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004842#endif
4843
Bram Moolenaar81695252004-12-29 20:58:21 +00004844/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004845 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004846 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004847 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004848 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004849 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004850 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004851 */
4852 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004853ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004854{
4855 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004856 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004857#ifdef FEAT_AUTOCMD
4858 char_u *au_name = NULL;
4859#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004860
Bram Moolenaardb552d602006-03-23 22:59:57 +00004861 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4862 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004863 {
4864 qi = ll_get_or_alloc_list(curwin);
4865 if (qi == NULL)
4866 return;
4867 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004868
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004869#ifdef FEAT_AUTOCMD
4870 switch (eap->cmdidx)
4871 {
4872 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
4873 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
4874 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
4875 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
4876 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
4877 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
4878 default: break;
4879 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004880 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4881 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004882 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004883# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004884 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004885 return;
4886# endif
4887 }
4888#endif
4889
Bram Moolenaar86b68352004-12-27 21:59:20 +00004890 if (*eap->arg == NUL)
4891 buf = curbuf;
4892 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4893 buf = buflist_findnr(atoi((char *)eap->arg));
4894 if (buf == NULL)
4895 EMSG(_(e_invarg));
4896 else if (buf->b_ml.ml_mfp == NULL)
4897 EMSG(_("E681: Buffer is not loaded"));
4898 else
4899 {
4900 if (eap->addr_count == 0)
4901 {
4902 eap->line1 = 1;
4903 eap->line2 = buf->b_ml.ml_line_count;
4904 }
4905 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4906 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4907 EMSG(_(e_invrange));
4908 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004909 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004910 char_u *qf_title = *eap->cmdlinep;
4911
4912 if (buf->b_sfname)
4913 {
4914 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4915 (char *)qf_title, (char *)buf->b_sfname);
4916 qf_title = IObuff;
4917 }
4918
Bram Moolenaardb552d602006-03-23 22:59:57 +00004919 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4920 (eap->cmdidx != CMD_caddbuffer
4921 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004922 eap->line1, eap->line2,
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004923 qf_title) > 0)
4924 {
4925#ifdef FEAT_AUTOCMD
4926 if (au_name != NULL)
4927 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4928 curbuf->b_fname, TRUE, curbuf);
4929#endif
4930 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
4931 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4932 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00004933 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004934 }
4935}
4936
Bram Moolenaar1e015462005-09-25 22:16:38 +00004937#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004938/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004939 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4940 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004941 */
4942 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004943ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004944{
4945 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004946 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004947#ifdef FEAT_AUTOCMD
4948 char_u *au_name = NULL;
4949#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004950
Bram Moolenaardb552d602006-03-23 22:59:57 +00004951 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4952 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004953 {
4954 qi = ll_get_or_alloc_list(curwin);
4955 if (qi == NULL)
4956 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004957 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004958
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004959#ifdef FEAT_AUTOCMD
4960 switch (eap->cmdidx)
4961 {
4962 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
4963 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
4964 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
4965 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
4966 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
4967 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
4968 default: break;
4969 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004970 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4971 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004972 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004973# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004974 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004975 return;
4976# endif
4977 }
4978#endif
4979
Bram Moolenaar4770d092006-01-12 23:22:24 +00004980 /* Evaluate the expression. When the result is a string or a list we can
4981 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004982 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004983 if (tv != NULL)
4984 {
4985 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4986 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4987 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004988 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004989 (eap->cmdidx != CMD_caddexpr
4990 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004991 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0)
4992 {
4993#ifdef FEAT_AUTOCMD
4994 if (au_name != NULL)
4995 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4996 curbuf->b_fname, TRUE, curbuf);
4997#endif
4998 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
4999 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5000 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005001 }
5002 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005003 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005004 free_tv(tv);
5005 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005006}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005007#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005008
5009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 * ":helpgrep {pattern}"
5011 */
5012 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005013ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
5015 regmatch_T regmatch;
5016 char_u *save_cpo;
5017 char_u *p;
5018 int fcount;
5019 char_u **fnames;
5020 FILE *fd;
5021 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005023#ifdef FEAT_MULTI_LANG
5024 char_u *lang;
5025#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005026 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005027 int new_qi = FALSE;
5028 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005029#ifdef FEAT_AUTOCMD
5030 char_u *au_name = NULL;
5031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005033#ifdef FEAT_MULTI_LANG
5034 /* Check for a specified language */
5035 lang = check_help_lang(eap->arg);
5036#endif
5037
Bram Moolenaar73633f82012-01-20 13:39:07 +01005038#ifdef FEAT_AUTOCMD
5039 switch (eap->cmdidx)
5040 {
5041 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5042 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5043 default: break;
5044 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005045 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5046 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005047 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005048# ifdef FEAT_EVAL
5049 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005050 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005051# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005052 }
5053#endif
5054
5055 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5056 save_cpo = p_cpo;
5057 p_cpo = empty_option;
5058
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005059 if (eap->cmdidx == CMD_lhelpgrep)
5060 {
5061 /* Find an existing help window */
5062 FOR_ALL_WINDOWS(wp)
5063 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5064 break;
5065
5066 if (wp == NULL) /* Help window not found */
5067 qi = NULL;
5068 else
5069 qi = wp->w_llist;
5070
5071 if (qi == NULL)
5072 {
5073 /* Allocate a new location list for help text matches */
5074 if ((qi = ll_new_list()) == NULL)
5075 return;
5076 new_qi = TRUE;
5077 }
5078 }
5079
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5081 regmatch.rm_ic = FALSE;
5082 if (regmatch.regprog != NULL)
5083 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005084#ifdef FEAT_MBYTE
5085 vimconv_T vc;
5086
5087 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5088 * differs. */
5089 vc.vc_type = CONV_NONE;
5090 if (!enc_utf8)
5091 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5092#endif
5093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005095 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096
5097 /* Go through all directories in 'runtimepath' */
5098 p = p_rtp;
5099 while (*p != NUL && !got_int)
5100 {
5101 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5102
5103 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5104 add_pathsep(NameBuff);
5105 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5106 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5107 &fnames, EW_FILE|EW_SILENT) == OK
5108 && fcount > 0)
5109 {
5110 for (fi = 0; fi < fcount && !got_int; ++fi)
5111 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005112#ifdef FEAT_MULTI_LANG
5113 /* Skip files for a different language. */
5114 if (lang != NULL
5115 && STRNICMP(lang, fnames[fi]
5116 + STRLEN(fnames[fi]) - 3, 2) != 0
5117 && !(STRNICMP(lang, "en", 2) == 0
5118 && STRNICMP("txt", fnames[fi]
5119 + STRLEN(fnames[fi]) - 3, 3) == 0))
5120 continue;
5121#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005122 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (fd != NULL)
5124 {
5125 lnum = 1;
5126 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5127 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005128 char_u *line = IObuff;
5129#ifdef FEAT_MBYTE
5130 /* Convert a line if 'encoding' is not utf-8 and
5131 * the line contains a non-ASCII character. */
5132 if (vc.vc_type != CONV_NONE
5133 && has_non_ascii(IObuff)) {
5134 line = string_convert(&vc, IObuff, NULL);
5135 if (line == NULL)
5136 line = IObuff;
5137 }
5138#endif
5139
5140 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005142 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
5144 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005145 while (l > 0 && line[l - 1] <= ' ')
5146 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005148 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 NULL, /* dir */
5150 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005151 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005152 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005154 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005155 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005156 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005157 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 0, /* nr */
5159 1, /* type */
5160 TRUE /* valid */
5161 ) == FAIL)
5162 {
5163 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005164#ifdef FEAT_MBYTE
5165 if (line != IObuff)
5166 vim_free(line);
5167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169 }
5170 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005171#ifdef FEAT_MBYTE
5172 if (line != IObuff)
5173 vim_free(line);
5174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 ++lnum;
5176 line_breakcheck();
5177 }
5178 fclose(fd);
5179 }
5180 }
5181 FreeWild(fcount, fnames);
5182 }
5183 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005184
Bram Moolenaar473de612013-06-08 18:19:48 +02005185 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005186#ifdef FEAT_MBYTE
5187 if (vc.vc_type != CONV_NONE)
5188 convert_setup(&vc, NULL, NULL);
5189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005191 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5192 qi->qf_lists[qi->qf_curlist].qf_ptr =
5193 qi->qf_lists[qi->qf_curlist].qf_start;
5194 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005197 if (p_cpo == empty_option)
5198 p_cpo = save_cpo;
5199 else
5200 /* Darn, some plugin changed the value. */
5201 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005204 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205#endif
5206
Bram Moolenaar73633f82012-01-20 13:39:07 +01005207#ifdef FEAT_AUTOCMD
5208 if (au_name != NULL)
5209 {
5210 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5211 curbuf->b_fname, TRUE, curbuf);
5212 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
5213 /* autocommands made "qi" invalid */
5214 return;
5215 }
5216#endif
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005219 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005220 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005221 else
5222 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005223
5224 if (eap->cmdidx == CMD_lhelpgrep)
5225 {
5226 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005227 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005228 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5229 {
5230 if (new_qi)
5231 ll_free_all(&qi);
5232 }
5233 else if (curwin->w_llist == NULL)
5234 curwin->w_llist = qi;
5235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236}
5237
5238#endif /* FEAT_QUICKFIX */