blob: 58c4227b6ae5aaff7fc01ba5672cb884cd4d5b1a [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;
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001115 int adding = FALSE;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001116#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001117 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001119 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001121 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001123 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1124 fields.errmsglen = CMDBUFFSIZE + 1;
1125 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1126 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1127 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1128 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 goto qf_init_end;
1130
Bram Moolenaare0d37972016-07-15 22:36:01 +02001131 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 {
1133 EMSG2(_(e_openerrf), efile);
1134 goto qf_init_end;
1135 }
1136
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001137 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001139 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001140#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001141 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02001142 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001143 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001144 adding = TRUE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001145 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001146 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001150 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001151 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 else
1153 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001155 /*
1156 * If we are not adding or adding to another list: clear the state.
1157 */
1158 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
1159 {
1160 qi->qf_dir_curlist = qi->qf_curlist;
1161 qf_clean_dir_stack(&qi->qf_dir_stack);
1162 qi->qf_directory = NULL;
1163 qf_clean_dir_stack(&qi->qf_file_stack);
1164 qi->qf_currfile = NULL;
1165 qi->qf_multiline = FALSE;
1166 qi->qf_multiignore = FALSE;
1167 qi->qf_multiscan = FALSE;
1168 }
1169
1170 /*
1171 * If the errorformat didn't change between calls, then reuse the
1172 * previously parsed values.
1173 */
1174 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1175 {
1176 /* free the previously parsed data */
1177 vim_free(last_efm);
1178 last_efm = NULL;
1179 free_efm_list(&fmt_first);
1180
1181 /* parse the current 'efm' */
1182 fmt_first = parse_efm_option(efm);
1183 if (fmt_first != NULL)
1184 last_efm = vim_strsave(efm);
1185 }
1186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
1190 /*
1191 * got_int is reset here, because it was probably set when killing the
1192 * ":make" command, but we still want to read the errorfile then.
1193 */
1194 got_int = FALSE;
1195
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001196 if (tv != NULL)
1197 {
1198 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001199 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001200 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001201 state.p_li = tv->vval.v_list->lv_first;
1202 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001203 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001204 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001205 state.buflnum = lnumfirst;
1206 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001207
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 /*
1209 * Read the lines in the error file one by one.
1210 * Try to recognize one of the error formats in each line.
1211 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001212 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001214 /* Get the next line from a file/buffer/list/string */
1215 status = qf_get_nextline(&state);
1216 if (status == QF_NOMEM) /* memory alloc failure */
1217 goto qf_init_end;
1218 if (status == QF_END_OF_INPUT) /* end of input */
1219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001221 status = qf_parse_line(qi, state.linebuf, state.linelen, fmt_first,
1222 &fields);
1223 if (status == QF_FAIL)
1224 goto error2;
1225 if (status == QF_NOMEM)
1226 goto qf_init_end;
1227 if (status == QF_IGNORE_LINE)
1228 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001230 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001231 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001232 (*fields.namebuf || qi->qf_directory != NULL)
1233 ? fields.namebuf
1234 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001235 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001236 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001237 fields.errmsg,
1238 fields.lnum,
1239 fields.col,
1240 fields.use_viscol,
1241 fields.pattern,
1242 fields.enr,
1243 fields.type,
1244 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 goto error2;
1246 line_breakcheck();
1247 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001248 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001250 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001252 /* no valid entry found */
1253 qi->qf_lists[qi->qf_curlist].qf_ptr =
1254 qi->qf_lists[qi->qf_curlist].qf_start;
1255 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1256 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258 else
1259 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001260 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1261 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1262 qi->qf_lists[qi->qf_curlist].qf_ptr =
1263 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001265 /* return number of matches */
1266 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001267 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269 EMSG(_(e_readerrf));
1270error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001271 if (!adding)
1272 {
1273 qf_free(qi, qi->qf_curlist);
1274 qi->qf_listcount--;
1275 if (qi->qf_curlist > 0)
1276 --qi->qf_curlist;
1277 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001278qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001279 if (state.fd != NULL)
1280 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001281 vim_free(fields.namebuf);
1282 vim_free(fields.errmsg);
1283 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001284 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
1286#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001287 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
1289
1290 return retval;
1291}
1292
Bram Moolenaarfb604092014-07-23 15:55:00 +02001293 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001294qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001295{
1296 if (title != NULL)
1297 {
1298 char_u *p = alloc((int)STRLEN(title) + 2);
1299
1300 qi->qf_lists[qi->qf_curlist].qf_title = p;
1301 if (p != NULL)
1302 sprintf((char *)p, ":%s", (char *)title);
1303 }
1304}
1305
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306/*
1307 * Prepare for adding a new quickfix list.
1308 */
1309 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001310qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 int i;
1313
1314 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001315 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 * the current entry. This makes it possible to browse in a tree-like
1317 * way with ":grep'.
1318 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001319 while (qi->qf_listcount > qi->qf_curlist + 1)
1320 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 /*
1323 * When the stack is full, remove to oldest entry
1324 * Otherwise, add a new entry.
1325 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001326 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001328 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001330 qi->qf_lists[i - 1] = qi->qf_lists[i];
1331 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001334 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001335 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001336 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337}
1338
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001339/*
1340 * Free a location list
1341 */
1342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001343ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001344{
1345 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001346 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001347
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001348 qi = *pqi;
1349 if (qi == NULL)
1350 return;
1351 *pqi = NULL; /* Remove reference to this list */
1352
1353 qi->qf_refcount--;
1354 if (qi->qf_refcount < 1)
1355 {
1356 /* No references to this location list */
1357 for (i = 0; i < qi->qf_listcount; ++i)
1358 qf_free(qi, i);
1359 vim_free(qi);
1360 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001361}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001362
1363 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001364qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001365{
1366 int i;
1367 qf_info_T *qi = &ql_info;
1368
1369 if (wp != NULL)
1370 {
1371 /* location list */
1372 ll_free_all(&wp->w_llist);
1373 ll_free_all(&wp->w_llist_ref);
1374 }
1375 else
1376 /* quickfix list */
1377 for (i = 0; i < qi->qf_listcount; ++i)
1378 qf_free(qi, i);
1379}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381/*
1382 * Add an entry to the end of the list of errors.
1383 * Returns OK or FAIL.
1384 */
1385 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001386qf_add_entry(
1387 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001388 char_u *dir, /* optional directory name */
1389 char_u *fname, /* file name or NULL */
1390 int bufnum, /* buffer number or zero */
1391 char_u *mesg, /* message */
1392 long lnum, /* line number */
1393 int col, /* column */
1394 int vis_col, /* using visual column */
1395 char_u *pattern, /* search pattern */
1396 int nr, /* error number */
1397 int type, /* type character */
1398 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001400 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001401 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001403 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001405 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001406 {
1407 buf_T *buf = buflist_findnr(bufnum);
1408
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001409 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001410 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001411 buf->b_has_qf_entry |=
1412 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001413 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001414 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001415 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1417 {
1418 vim_free(qfp);
1419 return FAIL;
1420 }
1421 qfp->qf_lnum = lnum;
1422 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001423 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001424 if (pattern == NULL || *pattern == NUL)
1425 qfp->qf_pattern = NULL;
1426 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1427 {
1428 vim_free(qfp->qf_text);
1429 vim_free(qfp);
1430 return FAIL;
1431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 qfp->qf_nr = nr;
1433 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1434 type = 0;
1435 qfp->qf_type = type;
1436 qfp->qf_valid = valid;
1437
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001438 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001439 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1440 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001442 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001443 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1444 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001445 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447 else
1448 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001449 qfp->qf_prev = *lastp;
1450 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001452 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001454 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001455 ++qi->qf_lists[qi->qf_curlist].qf_count;
1456 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1457 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001459 qi->qf_lists[qi->qf_curlist].qf_index =
1460 qi->qf_lists[qi->qf_curlist].qf_count;
1461 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463
1464 return OK;
1465}
1466
1467/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001468 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001469 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001470 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001471ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001472{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001473 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001474
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001475 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1476 if (qi != NULL)
1477 {
1478 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1479 qi->qf_refcount++;
1480 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001481
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001482 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001483}
1484
1485/*
1486 * Return the location list for window 'wp'.
1487 * If not present, allocate a location list
1488 */
1489 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001490ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001491{
1492 if (IS_LL_WINDOW(wp))
1493 /* For a location list window, use the referenced location list */
1494 return wp->w_llist_ref;
1495
1496 /*
1497 * For a non-location list window, w_llist_ref should not point to a
1498 * location list.
1499 */
1500 ll_free_all(&wp->w_llist_ref);
1501
1502 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001503 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001504 return wp->w_llist;
1505}
1506
1507/*
1508 * Copy the location list from window "from" to window "to".
1509 */
1510 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001511copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001512{
1513 qf_info_T *qi;
1514 int idx;
1515 int i;
1516
1517 /*
1518 * When copying from a location list window, copy the referenced
1519 * location list. For other windows, copy the location list for
1520 * that window.
1521 */
1522 if (IS_LL_WINDOW(from))
1523 qi = from->w_llist_ref;
1524 else
1525 qi = from->w_llist;
1526
1527 if (qi == NULL) /* no location list to copy */
1528 return;
1529
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001530 /* allocate a new location list */
1531 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001532 return;
1533
1534 to->w_llist->qf_listcount = qi->qf_listcount;
1535
1536 /* Copy the location lists one at a time */
1537 for (idx = 0; idx < qi->qf_listcount; idx++)
1538 {
1539 qf_list_T *from_qfl;
1540 qf_list_T *to_qfl;
1541
1542 to->w_llist->qf_curlist = idx;
1543
1544 from_qfl = &qi->qf_lists[idx];
1545 to_qfl = &to->w_llist->qf_lists[idx];
1546
1547 /* Some of the fields are populated by qf_add_entry() */
1548 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1549 to_qfl->qf_count = 0;
1550 to_qfl->qf_index = 0;
1551 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001552 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001553 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001554 if (from_qfl->qf_title != NULL)
1555 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1556 else
1557 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001558
1559 if (from_qfl->qf_count)
1560 {
1561 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001562 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001563
1564 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001565 for (i = 0, from_qfp = from_qfl->qf_start;
1566 i < from_qfl->qf_count && from_qfp != NULL;
1567 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001568 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001569 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001570 NULL,
1571 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001572 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001573 from_qfp->qf_text,
1574 from_qfp->qf_lnum,
1575 from_qfp->qf_col,
1576 from_qfp->qf_viscol,
1577 from_qfp->qf_pattern,
1578 from_qfp->qf_nr,
1579 0,
1580 from_qfp->qf_valid) == FAIL)
1581 {
1582 qf_free_all(to);
1583 return;
1584 }
1585 /*
1586 * qf_add_entry() will not set the qf_num field, as the
1587 * directory and file names are not supplied. So the qf_fnum
1588 * field is copied here.
1589 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001590 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001591 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1592 prevp->qf_type = from_qfp->qf_type; /* error type */
1593 if (from_qfl->qf_ptr == from_qfp)
1594 to_qfl->qf_ptr = prevp; /* current location */
1595 }
1596 }
1597
1598 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1599
1600 /* When no valid entries are present in the list, qf_ptr points to
1601 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001602 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001603 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001604 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001605 to_qfl->qf_index = 1;
1606 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001607 }
1608
1609 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1610}
1611
1612/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001613 * Looking up a buffer can be slow if there are many. Remember the last one
1614 * to make this a lot faster if there are multiple matches in the same file.
1615 */
1616static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001617static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001618
1619/*
Bram Moolenaar015102e2016-07-16 18:24:56 +02001620 * Get buffer number for file "directory.fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001621 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 */
1623 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001624qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
Bram Moolenaar82404332016-07-10 17:00:38 +02001626 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001627 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001628 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (fname == NULL || *fname == NUL) /* no file name */
1631 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaare60acc12011-05-10 16:41:25 +02001633#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001634 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001635#endif
1636#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001637 if (directory != NULL)
1638 slash_adjust(directory);
1639 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001640#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001641 if (directory != NULL && !vim_isAbsName(fname)
1642 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1643 {
1644 /*
1645 * Here we check if the file really exists.
1646 * This should normally be true, but if make works without
1647 * "leaving directory"-messages we might have missed a
1648 * directory change.
1649 */
1650 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001653 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001654 if (directory)
1655 ptr = concat_fnames(directory, fname, TRUE);
1656 else
1657 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001659 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001660 bufname = ptr;
1661 }
1662 else
1663 bufname = fname;
1664
1665 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001666 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001667 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001668 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001669 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001671 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001672 {
1673 vim_free(qf_last_bufname);
1674 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1675 if (bufname == ptr)
1676 qf_last_bufname = bufname;
1677 else
1678 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001679 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001680 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001681 if (buf == NULL)
1682 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001683
Bram Moolenaarc1542742016-07-20 21:44:37 +02001684 buf->b_has_qf_entry =
1685 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001686 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687}
1688
1689/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001690 * Push dirbuf onto the directory stack and return pointer to actual dir or
1691 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 */
1693 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001694qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 struct dir_stack_T *ds_new;
1697 struct dir_stack_T *ds_ptr;
1698
1699 /* allocate new stack element and hook it in */
1700 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1701 if (ds_new == NULL)
1702 return NULL;
1703
1704 ds_new->next = *stackptr;
1705 *stackptr = ds_new;
1706
1707 /* store directory on the stack */
1708 if (vim_isAbsName(dirbuf)
1709 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001710 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 (*stackptr)->dirname = vim_strsave(dirbuf);
1712 else
1713 {
1714 /* Okay we don't have an absolute path.
1715 * dirbuf must be a subdir of one of the directories on the stack.
1716 * Let's search...
1717 */
1718 ds_new = (*stackptr)->next;
1719 (*stackptr)->dirname = NULL;
1720 while (ds_new)
1721 {
1722 vim_free((*stackptr)->dirname);
1723 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1724 TRUE);
1725 if (mch_isdir((*stackptr)->dirname) == TRUE)
1726 break;
1727
1728 ds_new = ds_new->next;
1729 }
1730
1731 /* clean up all dirs we already left */
1732 while ((*stackptr)->next != ds_new)
1733 {
1734 ds_ptr = (*stackptr)->next;
1735 (*stackptr)->next = (*stackptr)->next->next;
1736 vim_free(ds_ptr->dirname);
1737 vim_free(ds_ptr);
1738 }
1739
1740 /* Nothing found -> it must be on top level */
1741 if (ds_new == NULL)
1742 {
1743 vim_free((*stackptr)->dirname);
1744 (*stackptr)->dirname = vim_strsave(dirbuf);
1745 }
1746 }
1747
1748 if ((*stackptr)->dirname != NULL)
1749 return (*stackptr)->dirname;
1750 else
1751 {
1752 ds_ptr = *stackptr;
1753 *stackptr = (*stackptr)->next;
1754 vim_free(ds_ptr);
1755 return NULL;
1756 }
1757}
1758
1759
1760/*
1761 * pop dirbuf from the directory stack and return previous directory or NULL if
1762 * stack is empty
1763 */
1764 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001765qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 struct dir_stack_T *ds_ptr;
1768
1769 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1770 * What to do if it isn't? */
1771
1772 /* pop top element and free it */
1773 if (*stackptr != NULL)
1774 {
1775 ds_ptr = *stackptr;
1776 *stackptr = (*stackptr)->next;
1777 vim_free(ds_ptr->dirname);
1778 vim_free(ds_ptr);
1779 }
1780
1781 /* return NEW top element as current dir or NULL if stack is empty*/
1782 return *stackptr ? (*stackptr)->dirname : NULL;
1783}
1784
1785/*
1786 * clean up directory stack
1787 */
1788 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001789qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790{
1791 struct dir_stack_T *ds_ptr;
1792
1793 while ((ds_ptr = *stackptr) != NULL)
1794 {
1795 *stackptr = (*stackptr)->next;
1796 vim_free(ds_ptr->dirname);
1797 vim_free(ds_ptr);
1798 }
1799}
1800
1801/*
1802 * Check in which directory of the directory stack the given file can be
1803 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001804 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 * Cleans up intermediate directory entries.
1806 *
1807 * TODO: How to solve the following problem?
1808 * If we have the this directory tree:
1809 * ./
1810 * ./aa
1811 * ./aa/bb
1812 * ./bb
1813 * ./bb/x.c
1814 * and make says:
1815 * making all in aa
1816 * making all in bb
1817 * x.c:9: Error
1818 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1819 * qf_guess_filepath will return NULL.
1820 */
1821 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001822qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 struct dir_stack_T *ds_ptr;
1825 struct dir_stack_T *ds_tmp;
1826 char_u *fullname;
1827
1828 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001829 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 return NULL;
1831
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001832 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 fullname = NULL;
1834 while (ds_ptr)
1835 {
1836 vim_free(fullname);
1837 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1838
1839 /* If concat_fnames failed, just go on. The worst thing that can happen
1840 * is that we delete the entire stack.
1841 */
1842 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1843 break;
1844
1845 ds_ptr = ds_ptr->next;
1846 }
1847
1848 vim_free(fullname);
1849
1850 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001851 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001853 ds_tmp = qi->qf_dir_stack->next;
1854 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 vim_free(ds_tmp->dirname);
1856 vim_free(ds_tmp);
1857 }
1858
1859 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860}
1861
1862/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001863 * When loading a file from the quickfix, the auto commands may modify it.
1864 * This may invalidate the current quickfix entry. This function checks
1865 * whether a entry is still present in the quickfix.
1866 * Similar to location list.
1867 */
1868 static int
1869is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1870{
1871 qf_list_T *qfl;
1872 qfline_T *qfp;
1873 int i;
1874
1875 qfl = &qi->qf_lists[qi->qf_curlist];
1876
1877 /* Search for the entry in the current list */
1878 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1879 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001880 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001881 break;
1882
1883 if (i == qfl->qf_count) /* Entry is not found */
1884 return FALSE;
1885
1886 return TRUE;
1887}
1888
1889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 * jump to a quickfix line
1891 * if dir == FORWARD go "errornr" valid entries forward
1892 * if dir == BACKWARD go "errornr" valid entries backward
1893 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1894 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1895 * else if "errornr" is zero, redisplay the same line
1896 * else go to entry "errornr"
1897 */
1898 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001899qf_jump(
1900 qf_info_T *qi,
1901 int dir,
1902 int errornr,
1903 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001905 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001906 qfline_T *qf_ptr;
1907 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 int qf_index;
1909 int old_qf_fnum;
1910 int old_qf_index;
1911 int prev_index;
1912 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1913 char_u *err = e_no_more_items;
1914 linenr_T i;
1915 buf_T *old_curbuf;
1916 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 colnr_T screen_col;
1918 colnr_T char_col;
1919 char_u *line;
1920#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001921 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001922 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 int opened_window = FALSE;
1924 win_T *win;
1925 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001926 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001928 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 int print_message = TRUE;
1930 int len;
1931#ifdef FEAT_FOLDING
1932 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1933#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001934 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001935 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001937 if (qi == NULL)
1938 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001939
1940 if (qi->qf_curlist >= qi->qf_listcount
1941 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
1943 EMSG(_(e_quickfix));
1944 return;
1945 }
1946
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001947 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001949 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 old_qf_index = qf_index;
1951 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1952 {
1953 while (errornr--)
1954 {
1955 old_qf_ptr = qf_ptr;
1956 prev_index = qf_index;
1957 old_qf_fnum = qf_ptr->qf_fnum;
1958 do
1959 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001960 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 || qf_ptr->qf_next == NULL)
1962 {
1963 qf_ptr = old_qf_ptr;
1964 qf_index = prev_index;
1965 if (err != NULL)
1966 {
1967 EMSG(_(err));
1968 goto theend;
1969 }
1970 errornr = 0;
1971 break;
1972 }
1973 ++qf_index;
1974 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001975 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1976 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1978 err = NULL;
1979 }
1980 }
1981 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1982 {
1983 while (errornr--)
1984 {
1985 old_qf_ptr = qf_ptr;
1986 prev_index = qf_index;
1987 old_qf_fnum = qf_ptr->qf_fnum;
1988 do
1989 {
1990 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1991 {
1992 qf_ptr = old_qf_ptr;
1993 qf_index = prev_index;
1994 if (err != NULL)
1995 {
1996 EMSG(_(err));
1997 goto theend;
1998 }
1999 errornr = 0;
2000 break;
2001 }
2002 --qf_index;
2003 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002004 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2005 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2007 err = NULL;
2008 }
2009 }
2010 else if (errornr != 0) /* go to specified number */
2011 {
2012 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2013 {
2014 --qf_index;
2015 qf_ptr = qf_ptr->qf_prev;
2016 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017 while (errornr > qf_index && qf_index <
2018 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 && qf_ptr->qf_next != NULL)
2020 {
2021 ++qf_index;
2022 qf_ptr = qf_ptr->qf_next;
2023 }
2024 }
2025
2026#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002027 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2028 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 /* No need to print the error message if it's visible in the error
2030 * window */
2031 print_message = FALSE;
2032
2033 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002034 * For ":helpgrep" find a help window or open one.
2035 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002036 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002037 {
2038 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002039
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002040 if (cmdmod.tab != 0)
2041 wp = NULL;
2042 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002043 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002044 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2045 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002046 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2047 win_enter(wp, TRUE);
2048 else
2049 {
2050 /*
2051 * Split off help window; put it at far top if no position
2052 * specified, the current window is vertically split and narrow.
2053 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002054 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002055 if (cmdmod.split == 0 && curwin->w_width != Columns
2056 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002057 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002058 if (qi != &ql_info)
2059 flags |= WSP_NEWLOC; /* don't copy the location list */
2060
2061 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002062 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002063 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002064
2065 if (curwin->w_height < p_hh)
2066 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002067
2068 if (qi != &ql_info) /* not a quickfix list */
2069 {
2070 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002071 curwin->w_llist = qi;
2072 qi->qf_refcount++;
2073 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002074 }
2075
2076 if (!p_im)
2077 restart_edit = 0; /* don't want insert mode in help file */
2078 }
2079
2080 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 * If currently in the quickfix window, find another window to show the
2082 * file in.
2083 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002084 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002086 win_T *usable_win_ptr = NULL;
2087
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 /*
2089 * If there is no file specified, we don't know where to go.
2090 * But do advance, otherwise ":cn" gets stuck.
2091 */
2092 if (qf_ptr->qf_fnum == 0)
2093 goto theend;
2094
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002095 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002096
2097 ll_ref = curwin->w_llist_ref;
2098 if (ll_ref != NULL)
2099 {
2100 /* Find a window using the same location list that is not a
2101 * quickfix window. */
2102 FOR_ALL_WINDOWS(usable_win_ptr)
2103 if (usable_win_ptr->w_llist == ll_ref
2104 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002105 {
2106 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002107 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002108 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002109 }
2110
2111 if (!usable_win)
2112 {
2113 /* Locate a window showing a normal buffer */
2114 FOR_ALL_WINDOWS(win)
2115 if (win->w_buffer->b_p_bt[0] == NUL)
2116 {
2117 usable_win = 1;
2118 break;
2119 }
2120 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002123 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002124 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002126 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002127 {
2128 tabpage_T *tp;
2129 win_T *wp;
2130
2131 FOR_ALL_TAB_WINDOWS(tp, wp)
2132 {
2133 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2134 {
2135 goto_tabpage_win(tp, wp);
2136 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002137 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002138 }
2139 }
2140 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002141win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002142
2143 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002144 * If there is only one window and it is the quickfix window, create a
2145 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002146 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002147 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002149 flags = WSP_ABOVE;
2150 if (ll_ref != NULL)
2151 flags |= WSP_NEWLOC;
2152 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 goto failed; /* not enough room for window */
2154 opened_window = TRUE; /* close it when fail */
2155 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002156 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002157 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 if (ll_ref != NULL)
2159 {
2160 /* The new window should use the location list from the
2161 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002162 curwin->w_llist = ll_ref;
2163 ll_ref->qf_refcount++;
2164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 else
2167 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002168 if (curwin->w_llist_ref != NULL)
2169 {
2170 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002171 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002172 if (win == NULL)
2173 {
2174 /* Find the window showing the selected file */
2175 FOR_ALL_WINDOWS(win)
2176 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2177 break;
2178 if (win == NULL)
2179 {
2180 /* Find a previous usable window */
2181 win = curwin;
2182 do
2183 {
2184 if (win->w_buffer->b_p_bt[0] == NUL)
2185 break;
2186 if (win->w_prev == NULL)
2187 win = lastwin; /* wrap around the top */
2188 else
2189 win = win->w_prev; /* go to previous window */
2190 } while (win != curwin);
2191 }
2192 }
2193 win_goto(win);
2194
2195 /* If the location list for the window is not set, then set it
2196 * to the location list from the location window */
2197 if (win->w_llist == NULL)
2198 {
2199 win->w_llist = ll_ref;
2200 ll_ref->qf_refcount++;
2201 }
2202 }
2203 else
2204 {
2205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 /*
2207 * Try to find a window that shows the right buffer.
2208 * Default to the window just above the quickfix buffer.
2209 */
2210 win = curwin;
2211 altwin = NULL;
2212 for (;;)
2213 {
2214 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2215 break;
2216 if (win->w_prev == NULL)
2217 win = lastwin; /* wrap around the top */
2218 else
2219 win = win->w_prev; /* go to previous window */
2220
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002221 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 /* Didn't find it, go to the window before the quickfix
2224 * window. */
2225 if (altwin != NULL)
2226 win = altwin;
2227 else if (curwin->w_prev != NULL)
2228 win = curwin->w_prev;
2229 else
2230 win = curwin->w_next;
2231 break;
2232 }
2233
2234 /* Remember a usable window. */
2235 if (altwin == NULL && !win->w_p_pvw
2236 && win->w_buffer->b_p_bt[0] == NUL)
2237 altwin = win;
2238 }
2239
2240 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243 }
2244#endif
2245
2246 /*
2247 * If there is a file name,
2248 * read the wanted file if needed, and check autowrite etc.
2249 */
2250 old_curbuf = curbuf;
2251 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002252
2253 if (qf_ptr->qf_fnum != 0)
2254 {
2255 if (qf_ptr->qf_type == 1)
2256 {
2257 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2258 * set b_p_ro flag). */
2259 if (!can_abandon(curbuf, forceit))
2260 {
2261 EMSG(_(e_nowrtmsg));
2262 ok = FALSE;
2263 }
2264 else
2265 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002266 ECMD_HIDE + ECMD_SET_HELP,
2267 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002268 }
2269 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002270 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002271 int old_qf_curlist = qi->qf_curlist;
2272 int is_abort = FALSE;
2273
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002274 ok = buflist_getfile(qf_ptr->qf_fnum,
2275 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002276 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002277 {
2278 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002279 is_abort = TRUE;
2280 opened_window = FALSE;
2281 }
2282 else if (old_qf_curlist != qi->qf_curlist
2283 || !is_qf_entry_present(qi, qf_ptr))
2284 {
2285 if (qi == &ql_info)
2286 EMSG(_("E925: Current quickfix was changed"));
2287 else
2288 EMSG(_("E926: Current location list was changed"));
2289 is_abort = TRUE;
2290 }
2291
2292 if (is_abort)
2293 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002294 ok = FALSE;
2295 qi = NULL;
2296 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002297 }
2298 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002299 }
2300
2301 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 /* When not switched to another buffer, still need to set pc mark */
2304 if (curbuf == old_curbuf)
2305 setpcmark();
2306
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002307 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002309 /*
2310 * Go to line with error, unless qf_lnum is 0.
2311 */
2312 i = qf_ptr->qf_lnum;
2313 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002315 if (i > curbuf->b_ml.ml_line_count)
2316 i = curbuf->b_ml.ml_line_count;
2317 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002319 if (qf_ptr->qf_col > 0)
2320 {
2321 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002322#ifdef FEAT_VIRTUALEDIT
2323 curwin->w_cursor.coladd = 0;
2324#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002325 if (qf_ptr->qf_viscol == TRUE)
2326 {
2327 /*
2328 * Check each character from the beginning of the error
2329 * line up to the error column. For each tab character
2330 * found, reduce the error column value by the length of
2331 * a tab character.
2332 */
2333 line = ml_get_curline();
2334 screen_col = 0;
2335 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2336 {
2337 if (*line == NUL)
2338 break;
2339 if (*line++ == '\t')
2340 {
2341 curwin->w_cursor.col -= 7 - (screen_col % 8);
2342 screen_col += 8 - (screen_col % 8);
2343 }
2344 else
2345 ++screen_col;
2346 }
2347 }
2348 check_cursor();
2349 }
2350 else
2351 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002354 {
2355 pos_T save_cursor;
2356
2357 /* Move the cursor to the first line in the buffer */
2358 save_cursor = curwin->w_cursor;
2359 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002360 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2361 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002362 curwin->w_cursor = save_cursor;
2363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
2365#ifdef FEAT_FOLDING
2366 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2367 foldOpenCursor();
2368#endif
2369 if (print_message)
2370 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002371 /* Update the screen before showing the message, unless the screen
2372 * scrolled up. */
2373 if (!msg_scrolled)
2374 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002376 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2378 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2379 /* Add the message, skipping leading whitespace and newlines. */
2380 len = (int)STRLEN(IObuff);
2381 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2382
2383 /* Output the message. Overwrite to avoid scrolling when the 'O'
2384 * flag is present in 'shortmess'; But when not jumping, print the
2385 * whole message. */
2386 i = msg_scroll;
2387 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2388 msg_scroll = TRUE;
2389 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2390 msg_scroll = FALSE;
2391 msg_attr_keep(IObuff, 0, TRUE);
2392 msg_scroll = i;
2393 }
2394 }
2395 else
2396 {
2397#ifdef FEAT_WINDOWS
2398 if (opened_window)
2399 win_close(curwin, TRUE); /* Close opened window */
2400#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002401 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
2403 /*
2404 * Couldn't open file, so put index back where it was. This could
2405 * happen if the file was readonly and we changed something.
2406 */
2407#ifdef FEAT_WINDOWS
2408failed:
2409#endif
2410 qf_ptr = old_qf_ptr;
2411 qf_index = old_qf_index;
2412 }
2413 }
2414theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002415 if (qi != NULL)
2416 {
2417 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2418 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420#ifdef FEAT_WINDOWS
2421 if (p_swb != old_swb && opened_window)
2422 {
2423 /* Restore old 'switchbuf' value, but not when an autocommand or
2424 * modeline has changed the value. */
2425 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002426 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002428 swb_flags = old_swb_flags;
2429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 else
2431 free_string_option(old_swb);
2432 }
2433#endif
2434}
2435
2436/*
2437 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002438 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 */
2440 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002441qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002443 buf_T *buf;
2444 char_u *fname;
2445 qfline_T *qfp;
2446 int i;
2447 int idx1 = 1;
2448 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002449 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002450 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002451 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002453 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002455 if (eap->cmdidx == CMD_llist)
2456 {
2457 qi = GET_LOC_LIST(curwin);
2458 if (qi == NULL)
2459 {
2460 EMSG(_(e_loclist));
2461 return;
2462 }
2463 }
2464
2465 if (qi->qf_curlist >= qi->qf_listcount
2466 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {
2468 EMSG(_(e_quickfix));
2469 return;
2470 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002471 if (*arg == '+')
2472 {
2473 ++arg;
2474 plus = TRUE;
2475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2477 {
2478 EMSG(_(e_trailing));
2479 return;
2480 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002481 if (plus)
2482 {
2483 i = qi->qf_lists[qi->qf_curlist].qf_index;
2484 idx2 = i + idx1;
2485 idx1 = i;
2486 }
2487 else
2488 {
2489 i = qi->qf_lists[qi->qf_curlist].qf_count;
2490 if (idx1 < 0)
2491 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2492 if (idx2 < 0)
2493 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002496 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002498 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2499 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
2501 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2502 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002503 msg_putchar('\n');
2504 if (got_int)
2505 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002506
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002507 fname = NULL;
2508 if (qfp->qf_fnum != 0
2509 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2510 {
2511 fname = buf->b_fname;
2512 if (qfp->qf_type == 1) /* :helpgrep */
2513 fname = gettail(fname);
2514 }
2515 if (fname == NULL)
2516 sprintf((char *)IObuff, "%2d", i);
2517 else
2518 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2519 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002520 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002521 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2522 if (qfp->qf_lnum == 0)
2523 IObuff[0] = NUL;
2524 else if (qfp->qf_col == 0)
2525 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2526 else
2527 sprintf((char *)IObuff, ":%ld col %d",
2528 qfp->qf_lnum, qfp->qf_col);
2529 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2530 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2531 msg_puts_attr(IObuff, hl_attr(HLF_N));
2532 if (qfp->qf_pattern != NULL)
2533 {
2534 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2535 STRCAT(IObuff, ":");
2536 msg_puts(IObuff);
2537 }
2538 msg_puts((char_u *)" ");
2539
2540 /* Remove newlines and leading whitespace from the text. For an
2541 * unrecognized line keep the indent, the compiler may mark a word
2542 * with ^^^^. */
2543 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2545 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002546 msg_prt_line(IObuff, FALSE);
2547 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002549
2550 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002551 if (qfp == NULL)
2552 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002553 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 ui_breakcheck();
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556}
2557
2558/*
2559 * Remove newlines and leading whitespace from an error message.
2560 * Put the result in "buf[bufsize]".
2561 */
2562 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002563qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 int i;
2566 char_u *p = text;
2567
2568 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2569 {
2570 if (*p == '\n')
2571 {
2572 buf[i] = ' ';
2573 while (*++p != NUL)
2574 if (!vim_iswhite(*p) && *p != '\n')
2575 break;
2576 }
2577 else
2578 buf[i] = *p++;
2579 }
2580 buf[i] = NUL;
2581}
2582
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002583 static void
2584qf_msg(qf_info_T *qi, int which, char *lead)
2585{
2586 char *title = (char *)qi->qf_lists[which].qf_title;
2587 int count = qi->qf_lists[which].qf_count;
2588 char_u buf[IOSIZE];
2589
2590 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2591 lead,
2592 which + 1,
2593 qi->qf_listcount,
2594 count);
2595
2596 if (title != NULL)
2597 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002598 size_t len = STRLEN(buf);
2599
2600 if (len < 34)
2601 {
2602 vim_memset(buf + len, ' ', 34 - len);
2603 buf[34] = NUL;
2604 }
2605 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002606 }
2607 trunc_string(buf, buf, Columns - 1, IOSIZE);
2608 msg(buf);
2609}
2610
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611/*
2612 * ":colder [count]": Up in the quickfix stack.
2613 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002614 * ":lolder [count]": Up in the location list stack.
2615 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 */
2617 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002618qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002620 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 int count;
2622
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002623 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2624 {
2625 qi = GET_LOC_LIST(curwin);
2626 if (qi == NULL)
2627 {
2628 EMSG(_(e_loclist));
2629 return;
2630 }
2631 }
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (eap->addr_count != 0)
2634 count = eap->line2;
2635 else
2636 count = 1;
2637 while (count--)
2638 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002639 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002641 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
2643 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002644 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002646 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648 else
2649 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002650 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
2652 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002653 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002655 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
2657 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002658 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002660 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#endif
2662}
2663
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002664 void
2665qf_history(exarg_T *eap)
2666{
2667 qf_info_T *qi = &ql_info;
2668 int i;
2669
2670 if (eap->cmdidx == CMD_lhistory)
2671 qi = GET_LOC_LIST(curwin);
2672 if (qi == NULL || (qi->qf_listcount == 0
2673 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2674 MSG(_("No entries"));
2675 else
2676 for (i = 0; i < qi->qf_listcount; ++i)
2677 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2678}
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002681 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 */
2683 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002684qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002686 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002687 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002688 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002690 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002692 qfp = qi->qf_lists[idx].qf_start;
2693 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002694 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002695 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002696 vim_free(qfp->qf_text);
2697 stop = (qfp == qfpnext);
2698 vim_free(qfp->qf_pattern);
2699 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002700 if (stop)
2701 /* Somehow qf_count may have an incorrect value, set it to 1
2702 * to avoid crashing when it's wrong.
2703 * TODO: Avoid qf_count being incorrect. */
2704 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002705 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002706 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002707 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002709 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002710 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002711 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002712
2713 qf_clean_dir_stack(&qi->qf_dir_stack);
2714 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715}
2716
2717/*
2718 * qf_mark_adjust: adjust marks
2719 */
2720 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002721qf_mark_adjust(
2722 win_T *wp,
2723 linenr_T line1,
2724 linenr_T line2,
2725 long amount,
2726 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002728 int i;
2729 qfline_T *qfp;
2730 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002731 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002732 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002733 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
Bram Moolenaarc1542742016-07-20 21:44:37 +02002735 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002736 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002737 if (wp != NULL)
2738 {
2739 if (wp->w_llist == NULL)
2740 return;
2741 qi = wp->w_llist;
2742 }
2743
2744 for (idx = 0; idx < qi->qf_listcount; ++idx)
2745 if (qi->qf_lists[idx].qf_count)
2746 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002747 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2748 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (qfp->qf_fnum == curbuf->b_fnum)
2750 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002751 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2753 {
2754 if (amount == MAXLNUM)
2755 qfp->qf_cleared = TRUE;
2756 else
2757 qfp->qf_lnum += amount;
2758 }
2759 else if (amount_after && qfp->qf_lnum > line2)
2760 qfp->qf_lnum += amount_after;
2761 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002762
2763 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002764 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765}
2766
2767/*
2768 * Make a nice message out of the error character and the error number:
2769 * char number message
2770 * e or E 0 " error"
2771 * w or W 0 " warning"
2772 * i or I 0 " info"
2773 * 0 0 ""
2774 * other 0 " c"
2775 * e or E n " error n"
2776 * w or W n " warning n"
2777 * i or I n " info n"
2778 * 0 n " error n"
2779 * other n " c n"
2780 * 1 x "" :helpgrep
2781 */
2782 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002783qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
2785 static char_u buf[20];
2786 static char_u cc[3];
2787 char_u *p;
2788
2789 if (c == 'W' || c == 'w')
2790 p = (char_u *)" warning";
2791 else if (c == 'I' || c == 'i')
2792 p = (char_u *)" info";
2793 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2794 p = (char_u *)" error";
2795 else if (c == 0 || c == 1)
2796 p = (char_u *)"";
2797 else
2798 {
2799 cc[0] = ' ';
2800 cc[1] = c;
2801 cc[2] = NUL;
2802 p = cc;
2803 }
2804
2805 if (nr <= 0)
2806 return p;
2807
2808 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2809 return buf;
2810}
2811
2812#if defined(FEAT_WINDOWS) || defined(PROTO)
2813/*
2814 * ":cwindow": open the quickfix window if we have errors to display,
2815 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002816 * ":lwindow": open the location list window if we have locations to display,
2817 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 */
2819 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002820ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002822 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 win_T *win;
2824
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002825 if (eap->cmdidx == CMD_lwindow)
2826 {
2827 qi = GET_LOC_LIST(curwin);
2828 if (qi == NULL)
2829 return;
2830 }
2831
2832 /* Look for an existing quickfix window. */
2833 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 /*
2836 * If a quickfix window is open but we have no errors to display,
2837 * close the window. If a quickfix window is not open, then open
2838 * it if we have errors; otherwise, leave it closed.
2839 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002840 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002841 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002842 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
2844 if (win != NULL)
2845 ex_cclose(eap);
2846 }
2847 else if (win == NULL)
2848 ex_copen(eap);
2849}
2850
2851/*
2852 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002853 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002856ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002858 win_T *win = NULL;
2859 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002861 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2862 {
2863 qi = GET_LOC_LIST(curwin);
2864 if (qi == NULL)
2865 return;
2866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002868 /* Find existing quickfix window and close it. */
2869 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 if (win != NULL)
2871 win_close(win, FALSE);
2872}
2873
2874/*
2875 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002876 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 */
2878 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002879ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002881 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002884 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002885 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002886 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002888 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2889 {
2890 qi = GET_LOC_LIST(curwin);
2891 if (qi == NULL)
2892 {
2893 EMSG(_(e_loclist));
2894 return;
2895 }
2896 }
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 if (eap->addr_count != 0)
2899 height = eap->line2;
2900 else
2901 height = QF_WINHEIGHT;
2902
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904#ifdef FEAT_GUI
2905 need_mouse_correct = TRUE;
2906#endif
2907
2908 /*
2909 * Find existing quickfix window, or open a new one.
2910 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 win = qf_find_win(qi);
2912
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002913 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002916 if (eap->addr_count != 0)
2917 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002918 if (cmdmod.split & WSP_VERT)
2919 {
2920 if (height != W_WIDTH(win))
2921 win_setwidth(height);
2922 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002923 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002924 win_setheight(height);
2925 }
2926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 else
2928 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002929 qf_buf = qf_find_buf(qi);
2930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 /* The current window becomes the previous window afterwards. */
2932 win = curwin;
2933
Bram Moolenaar77642c02012-11-20 17:55:10 +01002934 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2935 && cmdmod.split == 0)
2936 /* Create the new window at the very bottom, except when
2937 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002938 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002939 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002941 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002943 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002945 /*
2946 * For the location list window, create a reference to the
2947 * location list from the window 'win'.
2948 */
2949 curwin->w_llist_ref = win->w_llist;
2950 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002952
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002953 if (oldwin != curwin)
2954 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002955 if (qf_buf != NULL)
2956 /* Use the existing quickfix buffer */
2957 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002958 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002959 else
2960 {
2961 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002962 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002963 /* switch off 'swapfile' */
2964 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2965 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002966 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002967 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002968 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002969#ifdef FEAT_DIFF
2970 curwin->w_p_diff = FALSE;
2971#endif
2972#ifdef FEAT_FOLDING
2973 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2974 OPT_LOCAL);
2975#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002978 /* Only set the height when still in the same tab page and there is no
2979 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002980 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 win_setheight(height);
2982 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2983 if (win_valid(win))
2984 prevwin = win;
2985 }
2986
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002987 qf_set_title_var(qi);
2988
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 /*
2990 * Fill the buffer with the quickfix list.
2991 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002992 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002994 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 curwin->w_cursor.col = 0;
2996 check_cursor();
2997 update_topline(); /* scroll to show the line */
2998}
2999
3000/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003001 * Move the cursor in the quickfix window to "lnum".
3002 */
3003 static void
3004qf_win_goto(win_T *win, linenr_T lnum)
3005{
3006 win_T *old_curwin = curwin;
3007
3008 curwin = win;
3009 curbuf = win->w_buffer;
3010 curwin->w_cursor.lnum = lnum;
3011 curwin->w_cursor.col = 0;
3012#ifdef FEAT_VIRTUALEDIT
3013 curwin->w_cursor.coladd = 0;
3014#endif
3015 curwin->w_curswant = 0;
3016 update_topline(); /* scroll to show the line */
3017 redraw_later(VALID);
3018 curwin->w_redr_status = TRUE; /* update ruler */
3019 curwin = old_curwin;
3020 curbuf = curwin->w_buffer;
3021}
3022
3023/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003024 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003025 */
3026 void
3027ex_cbottom(exarg_T *eap UNUSED)
3028{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003029 qf_info_T *qi = &ql_info;
3030 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003031
Bram Moolenaar537ef082016-07-09 17:56:19 +02003032 if (eap->cmdidx == CMD_lbottom)
3033 {
3034 qi = GET_LOC_LIST(curwin);
3035 if (qi == NULL)
3036 {
3037 EMSG(_(e_loclist));
3038 return;
3039 }
3040 }
3041
3042 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003043 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3044 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3045}
3046
3047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 * Return the number of the current entry (line number in the quickfix
3049 * window).
3050 */
3051 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003052qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003054 qf_info_T *qi = &ql_info;
3055
3056 if (IS_LL_WINDOW(wp))
3057 /* In the location list window, use the referenced location list */
3058 qi = wp->w_llist_ref;
3059
3060 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061}
3062
3063/*
3064 * Update the cursor position in the quickfix window to the current error.
3065 * Return TRUE if there is a quickfix window.
3066 */
3067 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003068qf_win_pos_update(
3069 qf_info_T *qi,
3070 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003073 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
3075 /*
3076 * Put the cursor on the current error in the quickfix window, so that
3077 * it's viewable.
3078 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003079 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 if (win != NULL
3081 && qf_index <= win->w_buffer->b_ml.ml_line_count
3082 && old_qf_index != qf_index)
3083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 if (qf_index > old_qf_index)
3085 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003086 win->w_redraw_top = old_qf_index;
3087 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 }
3089 else
3090 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003091 win->w_redraw_top = qf_index;
3092 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003094 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 }
3096 return win != NULL;
3097}
3098
3099/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003100 * Check whether the given window is displaying the specified quickfix/location
3101 * list buffer
3102 */
3103 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003104is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003105{
3106 /*
3107 * A window displaying the quickfix buffer will have the w_llist_ref field
3108 * set to NULL.
3109 * A window displaying a location list buffer will have the w_llist_ref
3110 * pointing to the location list.
3111 */
3112 if (bt_quickfix(win->w_buffer))
3113 if ((qi == &ql_info && win->w_llist_ref == NULL)
3114 || (qi != &ql_info && win->w_llist_ref == qi))
3115 return TRUE;
3116
3117 return FALSE;
3118}
3119
3120/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003121 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003122 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003123 */
3124 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003125qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003126{
3127 win_T *win;
3128
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003129 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003130 if (is_qf_win(win, qi))
3131 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003132
3133 return win;
3134}
3135
3136/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003137 * Find a quickfix buffer.
3138 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 */
3140 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003141qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003143 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003144 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
Bram Moolenaar9c102382006-05-03 21:26:49 +00003146 FOR_ALL_TAB_WINDOWS(tp, win)
3147 if (is_qf_win(win, qi))
3148 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003149
Bram Moolenaar9c102382006-05-03 21:26:49 +00003150 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151}
3152
3153/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003154 * Update the w:quickfix_title variable in the quickfix/location list window
3155 */
3156 static void
3157qf_update_win_titlevar(qf_info_T *qi)
3158{
3159 win_T *win;
3160 win_T *curwin_save;
3161
3162 if ((win = qf_find_win(qi)) != NULL)
3163 {
3164 curwin_save = curwin;
3165 curwin = win;
3166 qf_set_title_var(qi);
3167 curwin = curwin_save;
3168 }
3169}
3170
3171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 * Find the quickfix buffer. If it exists, update the contents.
3173 */
3174 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003175qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
3177 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003178 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003182 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 if (buf != NULL)
3184 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003185 linenr_T old_line_count = buf->b_ml.ml_line_count;
3186
3187 if (old_last == NULL)
3188 /* set curwin/curbuf to buf and save a few things */
3189 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
Bram Moolenaard823fa92016-08-12 16:29:27 +02003191 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003192
Bram Moolenaar864293a2016-06-02 13:40:04 +02003193 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003194
Bram Moolenaar864293a2016-06-02 13:40:04 +02003195 if (old_last == NULL)
3196 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003197 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003198
3199 /* restore curwin/curbuf and a few other things */
3200 aucmd_restbuf(&aco);
3201 }
3202
3203 /* Only redraw when added lines are visible. This avoids flickering
3204 * when the added lines are not visible. */
3205 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3206 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208}
3209
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003210/*
3211 * Set "w:quickfix_title" if "qi" has a title.
3212 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003213 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003214qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003215{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003216 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3217 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003218 qi->qf_lists[qi->qf_curlist].qf_title);
3219}
3220
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221/*
3222 * Fill current buffer with quickfix errors, replacing any previous contents.
3223 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003224 * If "old_last" is not NULL append the items after this one.
3225 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3226 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 */
3228 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003229qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003231 linenr_T lnum;
3232 qfline_T *qfp;
3233 buf_T *errbuf;
3234 int len;
3235 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
Bram Moolenaar864293a2016-06-02 13:40:04 +02003237 if (old_last == NULL)
3238 {
3239 if (buf != curbuf)
3240 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003241 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003242 return;
3243 }
3244
3245 /* delete all existing lines */
3246 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3247 (void)ml_delete((linenr_T)1, FALSE);
3248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003251 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 {
3253 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003254 if (old_last == NULL)
3255 {
3256 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3257 lnum = 0;
3258 }
3259 else
3260 {
3261 qfp = old_last->qf_next;
3262 lnum = buf->b_ml.ml_line_count;
3263 }
3264 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
3266 if (qfp->qf_fnum != 0
3267 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3268 && errbuf->b_fname != NULL)
3269 {
3270 if (qfp->qf_type == 1) /* :helpgrep */
3271 STRCPY(IObuff, gettail(errbuf->b_fname));
3272 else
3273 STRCPY(IObuff, errbuf->b_fname);
3274 len = (int)STRLEN(IObuff);
3275 }
3276 else
3277 len = 0;
3278 IObuff[len++] = '|';
3279
3280 if (qfp->qf_lnum > 0)
3281 {
3282 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3283 len += (int)STRLEN(IObuff + len);
3284
3285 if (qfp->qf_col > 0)
3286 {
3287 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3288 len += (int)STRLEN(IObuff + len);
3289 }
3290
3291 sprintf((char *)IObuff + len, "%s",
3292 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3293 len += (int)STRLEN(IObuff + len);
3294 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003295 else if (qfp->qf_pattern != NULL)
3296 {
3297 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3298 len += (int)STRLEN(IObuff + len);
3299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 IObuff[len++] = '|';
3301 IObuff[len++] = ' ';
3302
3303 /* Remove newlines and leading whitespace from the text.
3304 * For an unrecognized line keep the indent, the compiler may
3305 * mark a word with ^^^^. */
3306 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3307 IObuff + len, IOSIZE - len);
3308
Bram Moolenaar864293a2016-06-02 13:40:04 +02003309 if (ml_append_buf(buf, lnum, IObuff,
3310 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003312 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003314 if (qfp == NULL)
3315 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003317
3318 if (old_last == NULL)
3319 /* Delete the empty line which is now at the end */
3320 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
3322
3323 /* correct cursor position */
3324 check_lnums(TRUE);
3325
Bram Moolenaar864293a2016-06-02 13:40:04 +02003326 if (old_last == NULL)
3327 {
3328 /* Set the 'filetype' to "qf" each time after filling the buffer.
3329 * This resembles reading a file into a buffer, it's more logical when
3330 * using autocommands. */
3331 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3332 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
3334#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003335 keep_filetype = TRUE; /* don't detect 'filetype' */
3336 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003338 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003340 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003342 /* make sure it will be redrawn */
3343 redraw_curbuf_later(NOT_VALID);
3344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
3346 /* Restore KeyTyped, setting 'filetype' may reset it. */
3347 KeyTyped = old_KeyTyped;
3348}
3349
3350#endif /* FEAT_WINDOWS */
3351
3352/*
3353 * Return TRUE if "buf" is the quickfix buffer.
3354 */
3355 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003356bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003358 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359}
3360
3361/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003362 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3363 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 */
3365 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003366bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003368 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3369 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370}
3371
3372/*
3373 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3374 */
3375 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003376bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003378 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379}
3380
3381 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003382bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
3384 if (bt_dontwrite(buf))
3385 {
3386 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3387 return TRUE;
3388 }
3389 return FALSE;
3390}
3391
3392/*
3393 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3394 * and 'bufhidden'.
3395 */
3396 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003397buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3400 switch (buf->b_p_bh[0])
3401 {
3402 case 'u': /* "unload" */
3403 case 'w': /* "wipe" */
3404 case 'd': return FALSE; /* "delete" */
3405 case 'h': return TRUE; /* "hide" */
3406 }
3407 return (p_hid || cmdmod.hide);
3408}
3409
3410/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003411 * Return TRUE when using ":vimgrep" for ":grep".
3412 */
3413 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003414grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003415{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003416 return ((cmdidx == CMD_grep
3417 || cmdidx == CMD_lgrep
3418 || cmdidx == CMD_grepadd
3419 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003420 && STRCMP("internal",
3421 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3422}
3423
3424/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003425 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 */
3427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003428ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003430 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 char_u *cmd;
3432 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003433 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003434 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003435 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003436#ifdef FEAT_AUTOCMD
3437 char_u *au_name = NULL;
3438
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003439 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3440 if (grep_internal(eap->cmdidx))
3441 {
3442 ex_vimgrep(eap);
3443 return;
3444 }
3445
Bram Moolenaar7c626922005-02-07 22:01:03 +00003446 switch (eap->cmdidx)
3447 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003448 case CMD_make: au_name = (char_u *)"make"; break;
3449 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3450 case CMD_grep: au_name = (char_u *)"grep"; break;
3451 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3452 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3453 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003454 default: break;
3455 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003456 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3457 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003458 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003459# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003460 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003461 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003462# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003463 }
3464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
Bram Moolenaara6557602006-02-04 22:43:20 +00003466 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3467 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003468 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003469
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003471 fname = get_mef_name();
3472 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003474 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476 /*
3477 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3478 */
3479 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3480 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003481 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 cmd = alloc(len);
3483 if (cmd == NULL)
3484 return;
3485 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3486 (char *)p_shq);
3487 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003488 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 /*
3490 * Output a newline if there's something else than the :make command that
3491 * was typed (in which case the cursor is in column 0).
3492 */
3493 if (msg_col == 0)
3494 msg_didout = FALSE;
3495 msg_start();
3496 MSG_PUTS(":!");
3497 msg_outtrans(cmd); /* show what we are doing */
3498
3499 /* let the shell know if we are redirecting output or not */
3500 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3501
3502#ifdef AMIGA
3503 out_flush();
3504 /* read window status report and redraw before message */
3505 (void)char_avail();
3506#endif
3507
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003508 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003509 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3510 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003511 && eap->cmdidx != CMD_lgrepadd),
3512 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003513 if (wp != NULL)
3514 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003515#ifdef FEAT_AUTOCMD
3516 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003517 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003518 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3519 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003520 if (qi->qf_curlist < qi->qf_listcount)
3521 res = qi->qf_lists[qi->qf_curlist].qf_count;
3522 else
3523 res = 0;
3524 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003525#endif
3526 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003527 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
Bram Moolenaar7c626922005-02-07 22:01:03 +00003529 mch_remove(fname);
3530 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 vim_free(cmd);
3532}
3533
3534/*
3535 * Return the name for the errorfile, in allocated memory.
3536 * Find a new unique name when 'makeef' contains "##".
3537 * Returns NULL for error.
3538 */
3539 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003540get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
3542 char_u *p;
3543 char_u *name;
3544 static int start = -1;
3545 static int off = 0;
3546#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003547 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548#endif
3549
3550 if (*p_mef == NUL)
3551 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003552 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (name == NULL)
3554 EMSG(_(e_notmp));
3555 return name;
3556 }
3557
3558 for (p = p_mef; *p; ++p)
3559 if (p[0] == '#' && p[1] == '#')
3560 break;
3561
3562 if (*p == NUL)
3563 return vim_strsave(p_mef);
3564
3565 /* Keep trying until the name doesn't exist yet. */
3566 for (;;)
3567 {
3568 if (start == -1)
3569 start = mch_get_pid();
3570 else
3571 off += 19;
3572
3573 name = alloc((unsigned)STRLEN(p_mef) + 30);
3574 if (name == NULL)
3575 break;
3576 STRCPY(name, p_mef);
3577 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3578 STRCAT(name, p + 2);
3579 if (mch_getperm(name) < 0
3580#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003581 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 && mch_lstat((char *)name, &sb) < 0
3583#endif
3584 )
3585 break;
3586 vim_free(name);
3587 }
3588 return name;
3589}
3590
3591/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003592 * Returns the number of valid entries in the current quickfix/location list.
3593 */
3594 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003595qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003596{
3597 qf_info_T *qi = &ql_info;
3598 qfline_T *qfp;
3599 int i, sz = 0;
3600 int prev_fnum = 0;
3601
3602 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3603 {
3604 /* Location list */
3605 qi = GET_LOC_LIST(curwin);
3606 if (qi == NULL)
3607 return 0;
3608 }
3609
3610 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003611 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003612 ++i, qfp = qfp->qf_next)
3613 {
3614 if (qfp->qf_valid)
3615 {
3616 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3617 sz++; /* Count all valid entries */
3618 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3619 {
3620 /* Count the number of files */
3621 sz++;
3622 prev_fnum = qfp->qf_fnum;
3623 }
3624 }
3625 }
3626
3627 return sz;
3628}
3629
3630/*
3631 * Returns the current index of the quickfix/location list.
3632 * Returns 0 if there is an error.
3633 */
3634 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003635qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003636{
3637 qf_info_T *qi = &ql_info;
3638
3639 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3640 {
3641 /* Location list */
3642 qi = GET_LOC_LIST(curwin);
3643 if (qi == NULL)
3644 return 0;
3645 }
3646
3647 return qi->qf_lists[qi->qf_curlist].qf_index;
3648}
3649
3650/*
3651 * Returns the current index in the quickfix/location list (counting only valid
3652 * entries). If no valid entries are in the list, then returns 1.
3653 */
3654 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003655qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003656{
3657 qf_info_T *qi = &ql_info;
3658 qf_list_T *qfl;
3659 qfline_T *qfp;
3660 int i, eidx = 0;
3661 int prev_fnum = 0;
3662
3663 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3664 {
3665 /* Location list */
3666 qi = GET_LOC_LIST(curwin);
3667 if (qi == NULL)
3668 return 1;
3669 }
3670
3671 qfl = &qi->qf_lists[qi->qf_curlist];
3672 qfp = qfl->qf_start;
3673
3674 /* check if the list has valid errors */
3675 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3676 return 1;
3677
3678 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3679 {
3680 if (qfp->qf_valid)
3681 {
3682 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3683 {
3684 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3685 {
3686 /* Count the number of files */
3687 eidx++;
3688 prev_fnum = qfp->qf_fnum;
3689 }
3690 }
3691 else
3692 eidx++;
3693 }
3694 }
3695
3696 return eidx ? eidx : 1;
3697}
3698
3699/*
3700 * Get the 'n'th valid error entry in the quickfix or location list.
3701 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3702 * For :cdo and :ldo returns the 'n'th valid error entry.
3703 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3704 */
3705 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003706qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003707{
3708 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3709 qfline_T *qfp = qfl->qf_start;
3710 int i, eidx;
3711 int prev_fnum = 0;
3712
3713 /* check if the list has valid errors */
3714 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3715 return 1;
3716
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003717 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003718 i++, qfp = qfp->qf_next)
3719 {
3720 if (qfp->qf_valid)
3721 {
3722 if (fdo)
3723 {
3724 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3725 {
3726 /* Count the number of files */
3727 eidx++;
3728 prev_fnum = qfp->qf_fnum;
3729 }
3730 }
3731 else
3732 eidx++;
3733 }
3734
3735 if (eidx == n)
3736 break;
3737 }
3738
3739 if (i <= qfl->qf_count)
3740 return i;
3741 else
3742 return 1;
3743}
3744
3745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003747 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003748 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 */
3750 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003751ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003753 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003754 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003755
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003756 if (eap->cmdidx == CMD_ll
3757 || eap->cmdidx == CMD_lrewind
3758 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003759 || eap->cmdidx == CMD_llast
3760 || eap->cmdidx == CMD_ldo
3761 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003762 {
3763 qi = GET_LOC_LIST(curwin);
3764 if (qi == NULL)
3765 {
3766 EMSG(_(e_loclist));
3767 return;
3768 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003769 }
3770
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003771 if (eap->addr_count > 0)
3772 errornr = (int)eap->line2;
3773 else
3774 {
3775 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3776 errornr = 0;
3777 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3778 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3779 errornr = 1;
3780 else
3781 errornr = 32767;
3782 }
3783
3784 /* For cdo and ldo commands, jump to the nth valid error.
3785 * For cfdo and lfdo commands, jump to the nth valid file entry.
3786 */
3787 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3788 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3789 errornr = qf_get_nth_valid_entry(qi,
3790 eap->addr_count > 0 ? (int)eap->line1 : 1,
3791 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3792
3793 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794}
3795
3796/*
3797 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003798 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003799 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 */
3801 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003802ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003804 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003805 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003806
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003807 if (eap->cmdidx == CMD_lnext
3808 || eap->cmdidx == CMD_lNext
3809 || eap->cmdidx == CMD_lprevious
3810 || eap->cmdidx == CMD_lnfile
3811 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003812 || eap->cmdidx == CMD_lpfile
3813 || eap->cmdidx == CMD_ldo
3814 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003815 {
3816 qi = GET_LOC_LIST(curwin);
3817 if (qi == NULL)
3818 {
3819 EMSG(_(e_loclist));
3820 return;
3821 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003822 }
3823
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003824 if (eap->addr_count > 0 &&
3825 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3826 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3827 errornr = (int)eap->line2;
3828 else
3829 errornr = 1;
3830
3831 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3832 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003834 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3835 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003837 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3838 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 ? BACKWARD_FILE
3840 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003841 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842}
3843
3844/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003845 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003846 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 */
3848 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003849ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003851 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003852 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003853#ifdef FEAT_AUTOCMD
3854 char_u *au_name = NULL;
3855#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003856
3857 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003858 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003859 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003860
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003861#ifdef FEAT_AUTOCMD
3862 switch (eap->cmdidx)
3863 {
3864 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3865 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3866 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3867 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3868 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3869 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3870 default: break;
3871 }
3872 if (au_name != NULL)
3873 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3874#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003875#ifdef FEAT_BROWSE
3876 if (cmdmod.browse)
3877 {
3878 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3879 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3880 if (browse_file == NULL)
3881 return;
3882 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3883 vim_free(browse_file);
3884 }
3885 else
3886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003888 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003889
3890 /*
3891 * This function is used by the :cfile, :cgetfile and :caddfile
3892 * commands.
3893 * :cfile always creates a new quickfix list and jumps to the
3894 * first error.
3895 * :cgetfile creates a new quickfix list but doesn't jump to the
3896 * first error.
3897 * :caddfile adds to an existing quickfix list. If there is no
3898 * quickfix list then a new list is created.
3899 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003900 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003901 && eap->cmdidx != CMD_laddfile),
3902 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003903 && (eap->cmdidx == CMD_cfile
3904 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003905 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003906#ifdef FEAT_AUTOCMD
3907 if (au_name != NULL)
3908 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3909#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003910 if (wp != NULL)
3911 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003912 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003913 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003914
3915 else
3916 {
3917#ifdef FEAT_AUTOCMD
3918 if (au_name != NULL)
3919 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3920#endif
3921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922}
3923
3924/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003925 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003926 * ":vimgrepadd {pattern} file(s)"
3927 * ":lvimgrep {pattern} file(s)"
3928 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003929 */
3930 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003931ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003932{
Bram Moolenaar81695252004-12-29 20:58:21 +00003933 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003934 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003935 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003936 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003937 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003938 char_u *s;
3939 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003940 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003941 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003942#ifdef FEAT_AUTOCMD
3943 qfline_T *cur_qf_start;
3944#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003945 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003946 buf_T *buf;
3947 int duplicate_name = FALSE;
3948 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003949 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003950 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003951 buf_T *first_match_buf = NULL;
3952 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003953 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003954#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3955 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003956#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003957 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003958 int flags = 0;
3959 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003960 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003961 char_u *dirname_start = NULL;
3962 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003963 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003964#ifdef FEAT_AUTOCMD
3965 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003966
3967 switch (eap->cmdidx)
3968 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003969 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3970 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3971 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003972 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003973 case CMD_grep: au_name = (char_u *)"grep"; break;
3974 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3975 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3976 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003977 default: break;
3978 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003979 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3980 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003981 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01003982# ifdef FEAT_EVAL
3983 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003984 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01003985# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003986 }
3987#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003988
Bram Moolenaar754b5602006-02-09 23:53:20 +00003989 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003990 || eap->cmdidx == CMD_lvimgrep
3991 || eap->cmdidx == CMD_lgrepadd
3992 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003993 {
3994 qi = ll_get_or_alloc_list(curwin);
3995 if (qi == NULL)
3996 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003997 }
3998
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003999 if (eap->addr_count > 0)
4000 tomatch = eap->line2;
4001 else
4002 tomatch = MAXLNUM;
4003
Bram Moolenaar81695252004-12-29 20:58:21 +00004004 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004005 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004006 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004007 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004008 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004009 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004010 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004011 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004012 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004013
4014 if (s != NULL && *s == NUL)
4015 {
4016 /* Pattern is empty, use last search pattern. */
4017 if (last_search_pat() == NULL)
4018 {
4019 EMSG(_(e_noprevre));
4020 goto theend;
4021 }
4022 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4023 }
4024 else
4025 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4026
Bram Moolenaar86b68352004-12-27 21:59:20 +00004027 if (regmatch.regprog == NULL)
4028 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004029 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004030 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004031
4032 p = skipwhite(p);
4033 if (*p == NUL)
4034 {
4035 EMSG(_("E683: File name missing or invalid pattern"));
4036 goto theend;
4037 }
4038
Bram Moolenaar754b5602006-02-09 23:53:20 +00004039 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004040 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004041 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004042 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004043 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004044
4045 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004046 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004047 goto theend;
4048 if (fcount == 0)
4049 {
4050 EMSG(_(e_nomatch));
4051 goto theend;
4052 }
4053
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004054 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4055 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004056 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004057 {
4058 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004059 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004060 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004061
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004062 /* Remember the current directory, because a BufRead autocommand that does
4063 * ":lcd %:p:h" changes the meaning of short path names. */
4064 mch_dirname(dirname_start, MAXPATHL);
4065
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004066#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004067 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004068 * changing the current quickfix list. */
4069 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4070#endif
4071
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004072 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004073 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004074 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004075 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004076 if (time(NULL) > seconds)
4077 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004078 /* Display the file name every second or so, show the user we are
4079 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004080 seconds = time(NULL);
4081 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004082 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004083 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004084 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004085 else
4086 {
4087 msg_outtrans(p);
4088 vim_free(p);
4089 }
4090 msg_clr_eos();
4091 msg_didout = FALSE; /* overwrite this message */
4092 msg_nowait = TRUE; /* don't wait for this message */
4093 msg_col = 0;
4094 out_flush();
4095 }
4096
Bram Moolenaar81695252004-12-29 20:58:21 +00004097 buf = buflist_findname_exp(fnames[fi]);
4098 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4099 {
4100 /* Remember that a buffer with this name already exists. */
4101 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004102 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004103 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004104
4105#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4106 /* Don't do Filetype autocommands to avoid loading syntax and
4107 * indent scripts, a great speed improvement. */
4108 save_ei = au_event_disable(",Filetype");
4109#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004110 /* Don't use modelines here, it's useless. */
4111 save_mls = p_mls;
4112 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004113
4114 /* Load file into a buffer, so that 'fileencoding' is detected,
4115 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004116 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004117
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004118 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004119#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4120 au_event_restore(save_ei);
4121#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004122 }
4123 else
4124 /* Use existing, loaded buffer. */
4125 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004126
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004127#ifdef FEAT_AUTOCMD
4128 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4129 {
4130 int idx;
4131
4132 /* Autocommands changed the quickfix list. Find the one we were
4133 * using and restore it. */
4134 for (idx = 0; idx < LISTCOUNT; ++idx)
4135 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4136 {
4137 qi->qf_curlist = idx;
4138 break;
4139 }
4140 if (idx == LISTCOUNT)
4141 {
4142 /* List cannot be found, create a new one. */
4143 qf_new_list(qi, *eap->cmdlinep);
4144 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4145 }
4146 }
4147#endif
4148
Bram Moolenaar81695252004-12-29 20:58:21 +00004149 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004150 {
4151 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004152 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004153 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004154 else
4155 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004156 /* Try for a match in all lines of the buffer.
4157 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004158 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004159 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4160 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004161 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004162 col = 0;
4163 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004164 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004165 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004166 /* Pass the buffer number so that it gets used even for a
4167 * dummy buffer, unless duplicate_name is set, then the
4168 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004169 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004170 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004171 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004172 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004173 ml_get_buf(buf,
4174 regmatch.startpos[0].lnum + lnum, FALSE),
4175 regmatch.startpos[0].lnum + lnum,
4176 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004177 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004178 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004179 0, /* nr */
4180 0, /* type */
4181 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004182 ) == FAIL)
4183 {
4184 got_int = TRUE;
4185 break;
4186 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004187 found_match = TRUE;
4188 if (--tomatch == 0)
4189 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004190 if ((flags & VGR_GLOBAL) == 0
4191 || regmatch.endpos[0].lnum > 0)
4192 break;
4193 col = regmatch.endpos[0].col
4194 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004195 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004196 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004197 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004198 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004199 if (got_int)
4200 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004201 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004202#ifdef FEAT_AUTOCMD
4203 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4204#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004205
4206 if (using_dummy)
4207 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004208 if (found_match && first_match_buf == NULL)
4209 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004210 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004211 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004212 /* Never keep a dummy buffer if there is another buffer
4213 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004214 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004215 buf = NULL;
4216 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004217 else if (!cmdmod.hide
4218 || buf->b_p_bh[0] == 'u' /* "unload" */
4219 || buf->b_p_bh[0] == 'w' /* "wipe" */
4220 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004221 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004222 /* When no match was found we don't need to remember the
4223 * buffer, wipe it out. If there was a match and it
4224 * wasn't the first one or we won't jump there: only
4225 * unload the buffer.
4226 * Ignore 'hidden' here, because it may lead to having too
4227 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004228 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004229 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004230 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004231 buf = NULL;
4232 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004233 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004234 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004235 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004236 /* Keeping the buffer, remove the dummy flag. */
4237 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004238 buf = NULL;
4239 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004240 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004241
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004242 if (buf != NULL)
4243 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004244 /* Keeping the buffer, remove the dummy flag. */
4245 buf->b_flags &= ~BF_DUMMY;
4246
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004247 /* If the buffer is still loaded we need to use the
4248 * directory we jumped to below. */
4249 if (buf == first_match_buf
4250 && target_dir == NULL
4251 && STRCMP(dirname_start, dirname_now) != 0)
4252 target_dir = vim_strsave(dirname_now);
4253
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004254 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004255 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004256 * need to be done (again). But not the window-local
4257 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004258 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004259#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004260 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4261 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004262#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004263 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004264 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004265 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004266 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004267 }
4268 }
4269
4270 FreeWild(fcount, fnames);
4271
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004272 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4273 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4274 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004275
4276#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004277 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004278#endif
4279
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004280#ifdef FEAT_AUTOCMD
4281 if (au_name != NULL)
4282 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4283 curbuf->b_fname, TRUE, curbuf);
4284#endif
4285
Bram Moolenaar86b68352004-12-27 21:59:20 +00004286 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004287 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004288 {
4289 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004290 {
4291 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004292 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004293 if (buf != curbuf)
4294 /* If we jumped to another buffer redrawing will already be
4295 * taken care of. */
4296 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004297
4298 /* Jump to the directory used after loading the buffer. */
4299 if (curbuf == first_match_buf && target_dir != NULL)
4300 {
4301 exarg_T ea;
4302
4303 ea.arg = target_dir;
4304 ea.cmdidx = CMD_lcd;
4305 ex_cd(&ea);
4306 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004307 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004308 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004309 else
4310 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004311
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004312 /* If we loaded a dummy buffer into the current window, the autocommands
4313 * may have messed up things, need to redraw and recompute folds. */
4314 if (redraw_for_dummy)
4315 {
4316#ifdef FEAT_FOLDING
4317 foldUpdateAll(curwin);
4318#else
4319 redraw_later(NOT_VALID);
4320#endif
4321 }
4322
Bram Moolenaar86b68352004-12-27 21:59:20 +00004323theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004324 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004325 vim_free(dirname_now);
4326 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004327 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004328 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004329}
4330
4331/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004332 * Restore current working directory to "dirname_start" if they differ, taking
4333 * into account whether it is set locally or globally.
4334 */
4335 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004336restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004337{
4338 char_u *dirname_now = alloc(MAXPATHL);
4339
4340 if (NULL != dirname_now)
4341 {
4342 mch_dirname(dirname_now, MAXPATHL);
4343 if (STRCMP(dirname_start, dirname_now) != 0)
4344 {
4345 /* If the directory has changed, change it back by building up an
4346 * appropriate ex command and executing it. */
4347 exarg_T ea;
4348
4349 ea.arg = dirname_start;
4350 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4351 ex_cd(&ea);
4352 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004353 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004354 }
4355}
4356
4357/*
4358 * Load file "fname" into a dummy buffer and return the buffer pointer,
4359 * placing the directory resulting from the buffer load into the
4360 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4361 * prior to calling this function. Restores directory to "dirname_start" prior
4362 * to returning, if autocmds or the 'autochdir' option have changed it.
4363 *
4364 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4365 * or wipe_dummy_buffer() later!
4366 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004367 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004368 */
4369 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004370load_dummy_buffer(
4371 char_u *fname,
4372 char_u *dirname_start, /* in: old directory */
4373 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004374{
4375 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004376 bufref_T newbufref;
4377 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004378 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004379 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004380
4381 /* Allocate a buffer without putting it in the buffer list. */
4382 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4383 if (newbuf == NULL)
4384 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004385 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004386
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004387 /* Init the options. */
4388 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4389
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004390 /* need to open the memfile before putting the buffer in a window */
4391 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004392 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004393 /* set curwin/curbuf to buf and save a few things */
4394 aucmd_prepbuf(&aco, newbuf);
4395
4396 /* Need to set the filename for autocommands. */
4397 (void)setfname(curbuf, fname, NULL, FALSE);
4398
Bram Moolenaar81695252004-12-29 20:58:21 +00004399 /* Create swap file now to avoid the ATTENTION message. */
4400 check_need_swap(TRUE);
4401
4402 /* Remove the "dummy" flag, otherwise autocommands may not
4403 * work. */
4404 curbuf->b_flags &= ~BF_DUMMY;
4405
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004406 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004407 if (readfile(fname, NULL,
4408 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4409 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004410 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004411 && !(curbuf->b_flags & BF_NEW))
4412 {
4413 failed = FALSE;
4414 if (curbuf != newbuf)
4415 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004416 /* Bloody autocommands changed the buffer! Can happen when
4417 * using netrw and editing a remote file. Use the current
4418 * buffer instead, delete the dummy one after restoring the
4419 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004420 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004421 newbuf = curbuf;
4422 }
4423 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004424
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004425 /* restore curwin/curbuf and a few other things */
4426 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004427 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4428 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004429
4430 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4431 * skip it. */
4432 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004433 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004434
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004435 /*
4436 * When autocommands/'autochdir' option changed directory: go back.
4437 * Let the caller know what the resulting dir was first, in case it is
4438 * important.
4439 */
4440 mch_dirname(resulting_dir, MAXPATHL);
4441 restore_start_dir(dirname_start);
4442
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004443 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004444 return NULL;
4445 if (failed)
4446 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004447 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004448 return NULL;
4449 }
4450 return newbuf;
4451}
4452
4453/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004454 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4455 * directory to "dirname_start" prior to returning, if autocmds or the
4456 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004457 */
4458 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004459wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004460{
4461 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004462 {
4463#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4464 cleanup_T cs;
4465
4466 /* Reset the error/interrupt/exception state here so that aborting()
4467 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4468 * work when got_int is set. */
4469 enter_cleanup(&cs);
4470#endif
4471
Bram Moolenaar81695252004-12-29 20:58:21 +00004472 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004473
4474#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4475 /* Restore the error/interrupt/exception state if not discarded by a
4476 * new aborting error, interrupt, or uncaught exception. */
4477 leave_cleanup(&cs);
4478#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004479 /* When autocommands/'autochdir' option changed directory: go back. */
4480 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004481 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004482}
4483
4484/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004485 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4486 * directory to "dirname_start" prior to returning, if autocmds or the
4487 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004488 */
4489 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004490unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004491{
4492 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004493 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004494 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004495
4496 /* When autocommands/'autochdir' option changed directory: go back. */
4497 restore_start_dir(dirname_start);
4498 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004499}
4500
Bram Moolenaar05159a02005-02-26 23:04:13 +00004501#if defined(FEAT_EVAL) || defined(PROTO)
4502/*
4503 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004504 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004505 */
4506 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004507get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004508{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004509 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004510 dict_T *dict;
4511 char_u buf[2];
4512 qfline_T *qfp;
4513 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004514 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004515
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004516 if (wp != NULL)
4517 {
4518 qi = GET_LOC_LIST(wp);
4519 if (qi == NULL)
4520 return FAIL;
4521 }
4522
Bram Moolenaard823fa92016-08-12 16:29:27 +02004523 if (qf_idx == -1)
4524 qf_idx = qi->qf_curlist;
4525
4526 if (qf_idx >= qi->qf_listcount
4527 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004528 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004529
Bram Moolenaard823fa92016-08-12 16:29:27 +02004530 qfp = qi->qf_lists[qf_idx].qf_start;
4531 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004532 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004533 /* Handle entries with a non-existing buffer number. */
4534 bufnum = qfp->qf_fnum;
4535 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4536 bufnum = 0;
4537
Bram Moolenaar05159a02005-02-26 23:04:13 +00004538 if ((dict = dict_alloc()) == NULL)
4539 return FAIL;
4540 if (list_append_dict(list, dict) == FAIL)
4541 return FAIL;
4542
4543 buf[0] = qfp->qf_type;
4544 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004545 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004546 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4547 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4548 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4549 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004550 || dict_add_nr_str(dict, "pattern", 0L,
4551 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4552 || dict_add_nr_str(dict, "text", 0L,
4553 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004554 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4555 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4556 return FAIL;
4557
4558 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004559 if (qfp == NULL)
4560 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004561 }
4562 return OK;
4563}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004564
4565/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004566 * Flags used by getqflist()/getloclist() to determine which fields to return.
4567 */
4568enum {
4569 QF_GETLIST_NONE = 0x0,
4570 QF_GETLIST_TITLE = 0x1,
4571 QF_GETLIST_ITEMS = 0x2,
4572 QF_GETLIST_NR = 0x4,
4573 QF_GETLIST_WINID = 0x8,
4574 QF_GETLIST_ALL = 0xFF
4575};
4576
4577/*
4578 * Return quickfix/location list details (title) as a
4579 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4580 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004581 */
4582 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004583get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4584{
4585 qf_info_T *qi = &ql_info;
4586 int status = OK;
4587 int qf_idx;
4588 dictitem_T *di;
4589 int flags = QF_GETLIST_NONE;
4590
4591 if (wp != NULL)
4592 {
4593 qi = GET_LOC_LIST(wp);
4594 if (qi == NULL)
4595 return FAIL;
4596 }
4597
4598 qf_idx = qi->qf_curlist; /* default is the current list */
4599 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4600 {
4601 /* Use the specified quickfix/location list */
4602 if (di->di_tv.v_type == VAR_NUMBER)
4603 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004604 /* for zero use the current list */
4605 if (di->di_tv.vval.v_number != 0)
4606 {
4607 qf_idx = di->di_tv.vval.v_number - 1;
4608 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4609 return FAIL;
4610 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004611 flags |= QF_GETLIST_NR;
4612 }
4613 else
4614 return FAIL;
4615 }
4616
4617 if (dict_find(what, (char_u *)"all", -1) != NULL)
4618 flags |= QF_GETLIST_ALL;
4619
4620 if (dict_find(what, (char_u *)"title", -1) != NULL)
4621 flags |= QF_GETLIST_TITLE;
4622
4623 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4624 flags |= QF_GETLIST_WINID;
4625
4626 if (flags & QF_GETLIST_TITLE)
4627 {
4628 char_u *t;
4629 t = qi->qf_lists[qf_idx].qf_title;
4630 if (t == NULL)
4631 t = (char_u *)"";
4632 status = dict_add_nr_str(retdict, "title", 0L, t);
4633 }
4634 if ((status == OK) && (flags & QF_GETLIST_NR))
4635 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4636 if ((status == OK) && (flags & QF_GETLIST_WINID))
4637 {
4638 win_T *win;
4639 win = qf_find_win(qi);
4640 if (win != NULL)
4641 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4642 }
4643
4644 return status;
4645}
4646
4647/*
4648 * Add list of entries to quickfix/location list. Each list entry is
4649 * a dictionary with item information.
4650 */
4651 static int
4652qf_add_entries(
4653 qf_info_T *qi,
4654 list_T *list,
4655 char_u *title,
4656 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004657{
4658 listitem_T *li;
4659 dict_T *d;
4660 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004661 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004662 long lnum;
4663 int col, nr;
4664 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004665#ifdef FEAT_WINDOWS
4666 qfline_T *old_last = NULL;
4667#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004668 int valid, status;
4669 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004670 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004671
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004672 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004673 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004674 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004675#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004676 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4677 /* Adding to existing list, use last entry. */
4678 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004679#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004680 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004681 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004682 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004683 qf_store_title(qi, title);
4684 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004685
4686 for (li = list->lv_first; li != NULL; li = li->li_next)
4687 {
4688 if (li->li_tv.v_type != VAR_DICT)
4689 continue; /* Skip non-dict items */
4690
4691 d = li->li_tv.vval.v_dict;
4692 if (d == NULL)
4693 continue;
4694
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004695 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004696 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4697 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4698 col = (int)get_dict_number(d, (char_u *)"col");
4699 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4700 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004701 type = get_dict_string(d, (char_u *)"type", TRUE);
4702 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4703 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004704 if (text == NULL)
4705 text = vim_strsave((char_u *)"");
4706
4707 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004708 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004709 valid = FALSE;
4710
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004711 /* Mark entries with non-existing buffer number as not valid. Give the
4712 * error message only once. */
4713 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4714 {
4715 if (!did_bufnr_emsg)
4716 {
4717 did_bufnr_emsg = TRUE;
4718 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4719 }
4720 valid = FALSE;
4721 bufnum = 0;
4722 }
4723
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004724 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004725 NULL, /* dir */
4726 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004727 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004728 text,
4729 lnum,
4730 col,
4731 vcol, /* vis_col */
4732 pattern, /* search pattern */
4733 nr,
4734 type == NULL ? NUL : *type,
4735 valid);
4736
4737 vim_free(filename);
4738 vim_free(pattern);
4739 vim_free(text);
4740 vim_free(type);
4741
4742 if (status == FAIL)
4743 {
4744 retval = FAIL;
4745 break;
4746 }
4747 }
4748
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004749 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004750 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004751 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4752 else
4753 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004754 if (action != 'a') {
4755 qi->qf_lists[qi->qf_curlist].qf_ptr =
4756 qi->qf_lists[qi->qf_curlist].qf_start;
4757 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4758 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4759 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004760
4761#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004762 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004763 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004764#endif
4765
4766 return retval;
4767}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004768
4769 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004770qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004771{
4772 dictitem_T *di;
4773 int retval = FAIL;
4774 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004775 int newlist = FALSE;
4776
4777 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4778 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004779
4780 qf_idx = qi->qf_curlist; /* default is the current list */
4781 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4782 {
4783 /* Use the specified quickfix/location list */
4784 if (di->di_tv.v_type == VAR_NUMBER)
4785 {
4786 qf_idx = di->di_tv.vval.v_number - 1;
4787 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4788 return FAIL;
4789 }
4790 else
4791 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004792 newlist = FALSE; /* use the specified list */
4793 }
4794
4795 if (newlist)
4796 {
4797 qf_new_list(qi, NULL);
4798 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004799 }
4800
4801 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4802 {
4803 if (di->di_tv.v_type == VAR_STRING)
4804 {
4805 vim_free(qi->qf_lists[qf_idx].qf_title);
4806 qi->qf_lists[qf_idx].qf_title =
4807 get_dict_string(what, (char_u *)"title", TRUE);
4808 if (qf_idx == qi->qf_curlist)
4809 qf_update_win_titlevar(qi);
4810 retval = OK;
4811 }
4812 }
4813
4814 return retval;
4815}
4816
4817/*
4818 * Populate the quickfix list with the items supplied in the list
4819 * of dictionaries. "title" will be copied to w:quickfix_title.
4820 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
4821 */
4822 int
4823set_errorlist(
4824 win_T *wp,
4825 list_T *list,
4826 int action,
4827 char_u *title,
4828 dict_T *what)
4829{
4830 qf_info_T *qi = &ql_info;
4831 int retval = OK;
4832
4833 if (wp != NULL)
4834 {
4835 qi = ll_get_or_alloc_list(wp);
4836 if (qi == NULL)
4837 return FAIL;
4838 }
4839
4840 if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004841 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004842 else
4843 retval = qf_add_entries(qi, list, title, action);
4844
4845 return retval;
4846}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004847#endif
4848
Bram Moolenaar81695252004-12-29 20:58:21 +00004849/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004850 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004851 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004852 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004853 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004854 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004855 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004856 */
4857 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004858ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004859{
4860 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004861 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004862#ifdef FEAT_AUTOCMD
4863 char_u *au_name = NULL;
4864#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004865
Bram Moolenaardb552d602006-03-23 22:59:57 +00004866 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4867 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004868 {
4869 qi = ll_get_or_alloc_list(curwin);
4870 if (qi == NULL)
4871 return;
4872 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004873
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004874#ifdef FEAT_AUTOCMD
4875 switch (eap->cmdidx)
4876 {
4877 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
4878 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
4879 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
4880 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
4881 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
4882 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
4883 default: break;
4884 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004885 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4886 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004887 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004888# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004889 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004890 return;
4891# endif
4892 }
4893#endif
4894
Bram Moolenaar86b68352004-12-27 21:59:20 +00004895 if (*eap->arg == NUL)
4896 buf = curbuf;
4897 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4898 buf = buflist_findnr(atoi((char *)eap->arg));
4899 if (buf == NULL)
4900 EMSG(_(e_invarg));
4901 else if (buf->b_ml.ml_mfp == NULL)
4902 EMSG(_("E681: Buffer is not loaded"));
4903 else
4904 {
4905 if (eap->addr_count == 0)
4906 {
4907 eap->line1 = 1;
4908 eap->line2 = buf->b_ml.ml_line_count;
4909 }
4910 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4911 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4912 EMSG(_(e_invrange));
4913 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004914 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004915 char_u *qf_title = *eap->cmdlinep;
4916
4917 if (buf->b_sfname)
4918 {
4919 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4920 (char *)qf_title, (char *)buf->b_sfname);
4921 qf_title = IObuff;
4922 }
4923
Bram Moolenaardb552d602006-03-23 22:59:57 +00004924 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4925 (eap->cmdidx != CMD_caddbuffer
4926 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004927 eap->line1, eap->line2,
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004928 qf_title) > 0)
4929 {
4930#ifdef FEAT_AUTOCMD
4931 if (au_name != NULL)
4932 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4933 curbuf->b_fname, TRUE, curbuf);
4934#endif
4935 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
4936 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4937 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00004938 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004939 }
4940}
4941
Bram Moolenaar1e015462005-09-25 22:16:38 +00004942#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004943/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004944 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4945 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004946 */
4947 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004948ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004949{
4950 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004951 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004952#ifdef FEAT_AUTOCMD
4953 char_u *au_name = NULL;
4954#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004955
Bram Moolenaardb552d602006-03-23 22:59:57 +00004956 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4957 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004958 {
4959 qi = ll_get_or_alloc_list(curwin);
4960 if (qi == NULL)
4961 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004962 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004963
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004964#ifdef FEAT_AUTOCMD
4965 switch (eap->cmdidx)
4966 {
4967 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
4968 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
4969 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
4970 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
4971 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
4972 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
4973 default: break;
4974 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004975 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4976 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004977 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004978# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004979 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004980 return;
4981# endif
4982 }
4983#endif
4984
Bram Moolenaar4770d092006-01-12 23:22:24 +00004985 /* Evaluate the expression. When the result is a string or a list we can
4986 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004987 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004988 if (tv != NULL)
4989 {
4990 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4991 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4992 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004993 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004994 (eap->cmdidx != CMD_caddexpr
4995 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004996 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0)
4997 {
4998#ifdef FEAT_AUTOCMD
4999 if (au_name != NULL)
5000 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5001 curbuf->b_fname, TRUE, curbuf);
5002#endif
5003 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5004 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5005 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005006 }
5007 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005008 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005009 free_tv(tv);
5010 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005011}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005012#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005013
5014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 * ":helpgrep {pattern}"
5016 */
5017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005018ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
5020 regmatch_T regmatch;
5021 char_u *save_cpo;
5022 char_u *p;
5023 int fcount;
5024 char_u **fnames;
5025 FILE *fd;
5026 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005028#ifdef FEAT_MULTI_LANG
5029 char_u *lang;
5030#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005031 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005032 int new_qi = FALSE;
5033 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005034#ifdef FEAT_AUTOCMD
5035 char_u *au_name = NULL;
5036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005038#ifdef FEAT_MULTI_LANG
5039 /* Check for a specified language */
5040 lang = check_help_lang(eap->arg);
5041#endif
5042
Bram Moolenaar73633f82012-01-20 13:39:07 +01005043#ifdef FEAT_AUTOCMD
5044 switch (eap->cmdidx)
5045 {
5046 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5047 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5048 default: break;
5049 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005050 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5051 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005052 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005053# ifdef FEAT_EVAL
5054 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005055 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005056# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005057 }
5058#endif
5059
5060 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5061 save_cpo = p_cpo;
5062 p_cpo = empty_option;
5063
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005064 if (eap->cmdidx == CMD_lhelpgrep)
5065 {
5066 /* Find an existing help window */
5067 FOR_ALL_WINDOWS(wp)
5068 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5069 break;
5070
5071 if (wp == NULL) /* Help window not found */
5072 qi = NULL;
5073 else
5074 qi = wp->w_llist;
5075
5076 if (qi == NULL)
5077 {
5078 /* Allocate a new location list for help text matches */
5079 if ((qi = ll_new_list()) == NULL)
5080 return;
5081 new_qi = TRUE;
5082 }
5083 }
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5086 regmatch.rm_ic = FALSE;
5087 if (regmatch.regprog != NULL)
5088 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005089#ifdef FEAT_MBYTE
5090 vimconv_T vc;
5091
5092 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5093 * differs. */
5094 vc.vc_type = CONV_NONE;
5095 if (!enc_utf8)
5096 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5097#endif
5098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005100 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
5102 /* Go through all directories in 'runtimepath' */
5103 p = p_rtp;
5104 while (*p != NUL && !got_int)
5105 {
5106 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5107
5108 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5109 add_pathsep(NameBuff);
5110 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5111 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5112 &fnames, EW_FILE|EW_SILENT) == OK
5113 && fcount > 0)
5114 {
5115 for (fi = 0; fi < fcount && !got_int; ++fi)
5116 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005117#ifdef FEAT_MULTI_LANG
5118 /* Skip files for a different language. */
5119 if (lang != NULL
5120 && STRNICMP(lang, fnames[fi]
5121 + STRLEN(fnames[fi]) - 3, 2) != 0
5122 && !(STRNICMP(lang, "en", 2) == 0
5123 && STRNICMP("txt", fnames[fi]
5124 + STRLEN(fnames[fi]) - 3, 3) == 0))
5125 continue;
5126#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005127 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (fd != NULL)
5129 {
5130 lnum = 1;
5131 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5132 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005133 char_u *line = IObuff;
5134#ifdef FEAT_MBYTE
5135 /* Convert a line if 'encoding' is not utf-8 and
5136 * the line contains a non-ASCII character. */
5137 if (vc.vc_type != CONV_NONE
5138 && has_non_ascii(IObuff)) {
5139 line = string_convert(&vc, IObuff, NULL);
5140 if (line == NULL)
5141 line = IObuff;
5142 }
5143#endif
5144
5145 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005147 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005150 while (l > 0 && line[l - 1] <= ' ')
5151 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005153 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 NULL, /* dir */
5155 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005156 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005157 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005159 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005160 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005161 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005162 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 0, /* nr */
5164 1, /* type */
5165 TRUE /* valid */
5166 ) == FAIL)
5167 {
5168 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005169#ifdef FEAT_MBYTE
5170 if (line != IObuff)
5171 vim_free(line);
5172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174 }
5175 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005176#ifdef FEAT_MBYTE
5177 if (line != IObuff)
5178 vim_free(line);
5179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 ++lnum;
5181 line_breakcheck();
5182 }
5183 fclose(fd);
5184 }
5185 }
5186 FreeWild(fcount, fnames);
5187 }
5188 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005189
Bram Moolenaar473de612013-06-08 18:19:48 +02005190 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005191#ifdef FEAT_MBYTE
5192 if (vc.vc_type != CONV_NONE)
5193 convert_setup(&vc, NULL, NULL);
5194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005196 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5197 qi->qf_lists[qi->qf_curlist].qf_ptr =
5198 qi->qf_lists[qi->qf_curlist].qf_start;
5199 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005202 if (p_cpo == empty_option)
5203 p_cpo = save_cpo;
5204 else
5205 /* Darn, some plugin changed the value. */
5206 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
5208#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005209 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#endif
5211
Bram Moolenaar73633f82012-01-20 13:39:07 +01005212#ifdef FEAT_AUTOCMD
5213 if (au_name != NULL)
5214 {
5215 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5216 curbuf->b_fname, TRUE, curbuf);
5217 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
5218 /* autocommands made "qi" invalid */
5219 return;
5220 }
5221#endif
5222
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005224 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005225 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005226 else
5227 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005228
5229 if (eap->cmdidx == CMD_lhelpgrep)
5230 {
5231 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005232 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005233 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5234 {
5235 if (new_qi)
5236 ll_free_all(&qi);
5237 }
5238 else if (curwin->w_llist == NULL)
5239 curwin->w_llist = qi;
5240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241}
5242
5243#endif /* FEAT_QUICKFIX */