blob: b791d6042405105b5a14064b0f6906a93791931a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
24static struct dir_stack_T *dir_stack = NULL;
25
26/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000029typedef struct qfline_S qfline_T;
30struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000031{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000032 qfline_T *qf_next; /* pointer to next error in the list */
33 qfline_T *qf_prev; /* pointer to previous error in the list */
34 linenr_T qf_lnum; /* line number where the error occurred */
35 int qf_fnum; /* file number for the line */
36 int qf_col; /* column where the error occurred */
37 int qf_nr; /* error number */
38 char_u *qf_pattern; /* search pattern for the error */
39 char_u *qf_text; /* description of the error */
40 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
41 char_u qf_cleared; /* set to TRUE if line has been deleted */
42 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000044 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
47/*
48 * There is a stack of error lists.
49 */
50#define LISTCOUNT 10
51
Bram Moolenaard12f5c12006-01-25 22:10:52 +000052typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020055 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000056 qfline_T *qf_ptr; /* pointer to the current error */
57 int qf_count; /* number of errors (0 means no error list) */
58 int qf_index; /* current index in the error list */
59 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020060 char_u *qf_title; /* title derived from the command that created
61 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000062} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
Bram Moolenaard12f5c12006-01-25 22:10:52 +000064struct qf_info_S
65{
66 /*
67 * Count of references to this list. Used only for location lists.
68 * When a location list window reference this list, qf_refcount
69 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
70 * reaches 0, the list is freed.
71 */
72 int qf_refcount;
73 int qf_listcount; /* current number of lists */
74 int qf_curlist; /* current error list */
75 qf_list_T qf_lists[LISTCOUNT];
76};
77
78static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaar68b76a62005-03-25 21:53:48 +000080#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
82/*
83 * Structure used to hold the info of one part of 'errorformat'
84 */
Bram Moolenaar01265852006-03-20 21:50:15 +000085typedef struct efm_S efm_T;
86struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000087{
88 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000089 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
91 char_u prefix; /* prefix of this format line: */
92 /* 'D' enter directory */
93 /* 'X' leave directory */
94 /* 'A' start of multi-line message */
95 /* 'E' error message */
96 /* 'W' warning message */
97 /* 'I' informational message */
98 /* 'C' continuation line */
99 /* 'Z' end of multi-line message */
100 /* 'G' general, unspecific message */
101 /* 'P' push file (partial) message */
102 /* 'Q' pop/quit file (partial) message */
103 /* 'O' overread (partial) message */
104 char_u flags; /* additional flags given in prefix */
105 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000106 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000107 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108};
109
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100110static 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);
111static void qf_store_title(qf_info_T *qi, char_u *title);
112static void qf_new_list(qf_info_T *qi, char_u *qf_title);
113static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200114static 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 +0100115static qf_info_T *ll_new_list(void);
116static void qf_msg(qf_info_T *qi);
117static void qf_free(qf_info_T *qi, int idx);
118static char_u *qf_types(int, int);
119static int qf_get_fnum(char_u *, char_u *);
120static char_u *qf_push_dir(char_u *, struct dir_stack_T **);
121static char_u *qf_pop_dir(struct dir_stack_T **);
122static char_u *qf_guess_filepath(char_u *);
123static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
124static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
127static int is_qf_win(win_T *win, qf_info_T *qi);
128static win_T *qf_find_win(qf_info_T *qi);
129static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200130static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200132static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static char_u *get_mef_name(void);
135static void restore_start_dir(char_u *dirname_start);
136static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
137static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
138static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
139static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000141/* Quickfix window check helper macro */
142#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
143/* Location list window check helper macro */
144#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
145/*
146 * Return location list for window 'wp'
147 * For location list window, return the referenced location list
148 */
149#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000152 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200153 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 * Return -1 for error, number of errors for success.
155 */
156 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100157qf_init(
158 win_T *wp,
159 char_u *efile,
160 char_u *errorformat,
161 int newlist, /* TRUE: start a new error list */
162 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164 qf_info_T *qi = &ql_info;
165
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000166 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000167 {
168 qi = ll_get_or_alloc_list(wp);
169 if (qi == NULL)
170 return FAIL;
171 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172
173 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200174 (linenr_T)0, (linenr_T)0,
175 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000176}
177
178/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200179 * Maximum number of bytes allowed per line while reading a errorfile.
180 */
181#define LINE_MAXLEN 4096
182
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200183 static char_u *
184qf_grow_linebuf(char_u **growbuf, int *growbufsiz, int newsz, int *allocsz)
185{
186 /*
187 * If the line exceeds LINE_MAXLEN exclude the last
188 * byte since it's not a NL character.
189 */
190 *allocsz = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
191 if (*growbuf == NULL)
192 {
193 *growbuf = alloc(*allocsz + 1);
194 if (*growbuf == NULL)
195 return NULL;
196 *growbufsiz = *allocsz;
197 }
198 else if (*allocsz > *growbufsiz)
199 {
200 *growbuf = vim_realloc(*growbuf, *allocsz + 1);
201 if (*growbuf == NULL)
202 return NULL;
203 *growbufsiz = *allocsz;
204 }
205 return *growbuf;
206}
207
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200208static struct fmtpattern
209{
210 char_u convchar;
211 char *pattern;
212} fmt_pat[FMT_PATTERNS] =
213 {
214 {'f', ".\\+"}, /* only used when at end */
215 {'n', "\\d\\+"},
216 {'l', "\\d\\+"},
217 {'c', "\\d\\+"},
218 {'t', "."},
219 {'m', ".\\+"},
220 {'r', ".*"},
221 {'p', "[- .]*"},
222 {'v', "\\d\\+"},
223 {'s', ".\\+"}
224 };
225
226/*
227 * Converts a 'errorformat' string to regular expression pattern
228 */
229 static int
230efm_to_regpat(
231 char_u *efm,
232 int len,
233 efm_T *fmt_ptr,
234 char_u *regpat,
235 char_u *errmsg)
236{
237 char_u *ptr;
238 char_u *efmp;
239 char_u *srcptr;
240 int round;
241 int idx = 0;
242
243 /*
244 * Build regexp pattern from current 'errorformat' option
245 */
246 ptr = regpat;
247 *ptr++ = '^';
248 round = 0;
249 for (efmp = efm; efmp < efm + len; ++efmp)
250 {
251 if (*efmp == '%')
252 {
253 ++efmp;
254 for (idx = 0; idx < FMT_PATTERNS; ++idx)
255 if (fmt_pat[idx].convchar == *efmp)
256 break;
257 if (idx < FMT_PATTERNS)
258 {
259 if (fmt_ptr->addr[idx])
260 {
261 sprintf((char *)errmsg,
262 _("E372: Too many %%%c in format string"), *efmp);
263 EMSG(errmsg);
264 return -1;
265 }
266 if ((idx
267 && idx < 6
268 && vim_strchr((char_u *)"DXOPQ",
269 fmt_ptr->prefix) != NULL)
270 || (idx == 6
271 && vim_strchr((char_u *)"OPQ",
272 fmt_ptr->prefix) == NULL))
273 {
274 sprintf((char *)errmsg,
275 _("E373: Unexpected %%%c in format string"), *efmp);
276 EMSG(errmsg);
277 return -1;
278 }
279 fmt_ptr->addr[idx] = (char_u)++round;
280 *ptr++ = '\\';
281 *ptr++ = '(';
282#ifdef BACKSLASH_IN_FILENAME
283 if (*efmp == 'f')
284 {
285 /* Also match "c:" in the file name, even when
286 * checking for a colon next: "%f:".
287 * "\%(\a:\)\=" */
288 STRCPY(ptr, "\\%(\\a:\\)\\=");
289 ptr += 10;
290 }
291#endif
292 if (*efmp == 'f' && efmp[1] != NUL)
293 {
294 if (efmp[1] != '\\' && efmp[1] != '%')
295 {
296 /* A file name may contain spaces, but this isn't
297 * in "\f". For "%f:%l:%m" there may be a ":" in
298 * the file name. Use ".\{-1,}x" instead (x is
299 * the next character), the requirement that :999:
300 * follows should work. */
301 STRCPY(ptr, ".\\{-1,}");
302 ptr += 7;
303 }
304 else
305 {
306 /* File name followed by '\\' or '%': include as
307 * many file name chars as possible. */
308 STRCPY(ptr, "\\f\\+");
309 ptr += 4;
310 }
311 }
312 else
313 {
314 srcptr = (char_u *)fmt_pat[idx].pattern;
315 while ((*ptr = *srcptr++) != NUL)
316 ++ptr;
317 }
318 *ptr++ = '\\';
319 *ptr++ = ')';
320 }
321 else if (*efmp == '*')
322 {
323 if (*++efmp == '[' || *efmp == '\\')
324 {
325 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
326 {
327 if (efmp[1] == '^')
328 *ptr++ = *++efmp;
329 if (efmp < efm + len)
330 {
331 *ptr++ = *++efmp; /* could be ']' */
332 while (efmp < efm + len
333 && (*ptr++ = *++efmp) != ']')
334 /* skip */;
335 if (efmp == efm + len)
336 {
337 EMSG(_("E374: Missing ] in format string"));
338 return -1;
339 }
340 }
341 }
342 else if (efmp < efm + len) /* %*\D, %*\s etc. */
343 *ptr++ = *++efmp;
344 *ptr++ = '\\';
345 *ptr++ = '+';
346 }
347 else
348 {
349 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
350 sprintf((char *)errmsg,
351 _("E375: Unsupported %%%c in format string"), *efmp);
352 EMSG(errmsg);
353 return -1;
354 }
355 }
356 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
357 *ptr++ = *efmp; /* regexp magic characters */
358 else if (*efmp == '#')
359 *ptr++ = '*';
360 else if (*efmp == '>')
361 fmt_ptr->conthere = TRUE;
362 else if (efmp == efm + 1) /* analyse prefix */
363 {
364 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
365 fmt_ptr->flags = *efmp++;
366 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
367 fmt_ptr->prefix = *efmp;
368 else
369 {
370 sprintf((char *)errmsg,
371 _("E376: Invalid %%%c in format string prefix"), *efmp);
372 EMSG(errmsg);
373 return -1;
374 }
375 }
376 else
377 {
378 sprintf((char *)errmsg,
379 _("E377: Invalid %%%c in format string"), *efmp);
380 EMSG(errmsg);
381 return -1;
382 }
383 }
384 else /* copy normal character */
385 {
386 if (*efmp == '\\' && efmp + 1 < efm + len)
387 ++efmp;
388 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
389 *ptr++ = '\\'; /* escape regexp atoms */
390 if (*efmp)
391 *ptr++ = *efmp;
392 }
393 }
394 *ptr++ = '$';
395 *ptr = NUL;
396
397 return 0;
398}
399
400 static void
401free_efm_list(efm_T **efm_first)
402{
403 efm_T *efm_ptr;
404
405 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
406 {
407 *efm_first = efm_ptr->next;
408 vim_regfree(efm_ptr->prog);
409 vim_free(efm_ptr);
410 }
411}
412
413/* Parse 'errorformat' option */
414 static efm_T *
415parse_efm_option(char_u *efm)
416{
417 char_u *errmsg = NULL;
418 int errmsglen;
419 efm_T *fmt_ptr = NULL;
420 efm_T *fmt_first = NULL;
421 efm_T *fmt_last = NULL;
422 char_u *fmtstr = NULL;
423 int len;
424 int i;
425 int round;
426
427 errmsglen = CMDBUFFSIZE + 1;
428 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
429 if (errmsg == NULL)
430 goto parse_efm_end;
431
432 /*
433 * Get some space to modify the format string into.
434 */
435 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
436 for (round = FMT_PATTERNS; round > 0; )
437 i += (int)STRLEN(fmt_pat[--round].pattern);
438#ifdef COLON_IN_FILENAME
439 i += 12; /* "%f" can become twelve chars longer */
440#else
441 i += 2; /* "%f" can become two chars longer */
442#endif
443 if ((fmtstr = alloc(i)) == NULL)
444 goto parse_efm_error;
445
446 while (efm[0] != NUL)
447 {
448 /*
449 * Allocate a new eformat structure and put it at the end of the list
450 */
451 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
452 if (fmt_ptr == NULL)
453 goto parse_efm_error;
454 if (fmt_first == NULL) /* first one */
455 fmt_first = fmt_ptr;
456 else
457 fmt_last->next = fmt_ptr;
458 fmt_last = fmt_ptr;
459
460 /*
461 * Isolate one part in the 'errorformat' option
462 */
463 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
464 if (efm[len] == '\\' && efm[len + 1] != NUL)
465 ++len;
466
467 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
468 goto parse_efm_error;
469 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
470 goto parse_efm_error;
471 /*
472 * Advance to next part
473 */
474 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
475 }
476
477 if (fmt_first == NULL) /* nothing found */
478 EMSG(_("E378: 'errorformat' contains no pattern"));
479
480 goto parse_efm_end;
481
482parse_efm_error:
483 free_efm_list(&fmt_first);
484
485parse_efm_end:
486 vim_free(fmtstr);
487 vim_free(errmsg);
488
489 return fmt_first;
490}
491
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200492/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000493 * Read the errorfile "efile" into memory, line by line, building the error
494 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +0200495 * Alternative: when "efile" is NULL read errors from buffer "buf".
496 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000497 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200498 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
499 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000500 * Return -1 for error, number of errors for success.
501 */
502 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100503qf_init_ext(
504 qf_info_T *qi,
505 char_u *efile,
506 buf_T *buf,
507 typval_T *tv,
508 char_u *errorformat,
509 int newlist, /* TRUE: start a new error list */
510 linenr_T lnumfirst, /* first line number to use */
511 linenr_T lnumlast, /* last line number to use */
512 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000513{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 char_u *namebuf;
515 char_u *errmsg;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200516 int errmsglen;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000517 char_u *pattern;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200518 char_u *growbuf = NULL;
519 int growbuflen;
Bram Moolenaar9a3b3312016-05-01 20:20:49 +0200520 int growbufsiz = 0;
521 char_u *linebuf = NULL;
522 int linelen = 0;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200523 int discard;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000525 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 int type = 0;
527 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000528 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 long lnum = 0L;
530 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000531 FILE *fd = NULL;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200532#ifdef FEAT_WINDOWS
533 qfline_T *old_last = NULL;
534#endif
Bram Moolenaar01265852006-03-20 21:50:15 +0000535 efm_T *fmt_first = NULL;
Bram Moolenaar01265852006-03-20 21:50:15 +0000536 efm_T *fmt_ptr;
537 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 char_u *efm;
539 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 int len;
541 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 int idx = 0;
543 int multiline = FALSE;
544 int multiignore = FALSE;
545 int multiscan = FALSE;
546 int retval = -1; /* default: return error flag */
547 char_u *directory = NULL;
548 char_u *currfile = NULL;
549 char_u *tail = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200550 char_u *p_buf = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000551 char_u *p_str = NULL;
552 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 struct dir_stack_T *file_stack = NULL;
554 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100556 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200557 errmsglen = CMDBUFFSIZE + 1;
558 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100559 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000560 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 goto qf_init_end;
562
Bram Moolenaar86b68352004-12-27 21:59:20 +0000563 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {
565 EMSG2(_(e_openerrf), efile);
566 goto qf_init_end;
567 }
568
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000569 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100571 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200572#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000573 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +0200574 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200575 /* Adding to existing list, use last entry. */
576 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200577 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579
580/*
581 * Each part of the format string is copied and modified from errorformat to
582 * regex prog. Only a few % characters are allowed.
583 */
584 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000585 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000586 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 else
588 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200590 fmt_first = parse_efm_option(efm);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594 /*
595 * got_int is reset here, because it was probably set when killing the
596 * ":make" command, but we still want to read the errorfile then.
597 */
598 got_int = FALSE;
599
600 /* Always ignore case when looking for a matching error. */
601 regmatch.rm_ic = TRUE;
602
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000603 if (tv != NULL)
604 {
605 if (tv->v_type == VAR_STRING)
606 p_str = tv->vval.v_string;
607 else if (tv->v_type == VAR_LIST)
608 p_li = tv->vval.v_list->lv_first;
609 }
610
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 /*
612 * Read the lines in the error file one by one.
613 * Try to recognize one of the error formats in each line.
614 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000615 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000617 /* Get the next line. */
618 if (fd == NULL)
619 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620 if (tv != NULL)
621 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000622 if (tv->v_type == VAR_STRING)
623 {
624 /* Get the next line from the supplied string */
625 char_u *p;
626
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200627 if (*p_str == NUL) /* Reached the end of the string */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000628 break;
629
630 p = vim_strchr(p_str, '\n');
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200631 if (p != NULL)
632 len = (int)(p - p_str) + 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000633 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000634 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000635
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200636 if (len > IOSIZE - 2)
637 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200638 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
639 &linelen);
640 if (linebuf == NULL)
641 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200642 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000643 else
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200644 {
645 linebuf = IObuff;
646 linelen = len;
647 }
648 vim_strncpy(linebuf, p_str, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000649
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200650 /*
651 * Increment using len in order to discard the rest of the
652 * line if it exceeds LINE_MAXLEN.
653 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000654 p_str += len;
655 }
656 else if (tv->v_type == VAR_LIST)
657 {
658 /* Get the next line from the supplied list */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200659 while (p_li != NULL
660 && (p_li->li_tv.v_type != VAR_STRING
661 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000662 p_li = p_li->li_next; /* Skip non-string items */
663
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200664 if (p_li == NULL) /* End of the list */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000665 break;
666
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000667 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200668 if (len > IOSIZE - 2)
669 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200670 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
671 &linelen);
672 if (linebuf == NULL)
673 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200674 }
675 else
676 {
677 linebuf = IObuff;
678 linelen = len;
679 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000680
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200681 vim_strncpy(linebuf, p_li->li_tv.vval.v_string, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000682
683 p_li = p_li->li_next; /* next item */
684 }
685 }
686 else
687 {
688 /* Get the next line from the supplied buffer */
689 if (buflnum > lnumlast)
690 break;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200691 p_buf = ml_get_buf(buf, buflnum++, FALSE);
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200692 len = (int)STRLEN(p_buf);
693 if (len > IOSIZE - 2)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200694 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200695 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
696 &linelen);
697 if (linebuf == NULL)
698 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200699 }
700 else
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200701 {
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200702 linebuf = IObuff;
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200703 linelen = len;
704 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200705 vim_strncpy(linebuf, p_buf, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000706 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000707 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200708 else
709 {
710 if (fgets((char *)IObuff, IOSIZE, fd) == NULL)
711 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000712
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200713 discard = FALSE;
714 linelen = (int)STRLEN(IObuff);
Bram Moolenaarb37662a2016-06-02 22:18:47 +0200715 if (linelen == IOSIZE - 1 && !(IObuff[linelen - 1] == '\n'
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200716#ifdef USE_CRNL
Bram Moolenaarb37662a2016-06-02 22:18:47 +0200717 || IObuff[linelen - 1] == '\r'
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200718#endif
719 ))
720 {
721 /*
722 * The current line exceeds IObuff, continue reading using
723 * growbuf until EOL or LINE_MAXLEN bytes is read.
724 */
725 if (growbuf == NULL)
726 {
727 growbufsiz = 2 * (IOSIZE - 1);
728 growbuf = alloc(growbufsiz);
729 if (growbuf == NULL)
730 goto qf_init_end;
731 }
732
733 /* Copy the read part of the line, excluding null-terminator */
734 memcpy(growbuf, IObuff, IOSIZE - 1);
735 growbuflen = linelen;
736
737 for (;;)
738 {
739 if (fgets((char *)growbuf + growbuflen,
740 growbufsiz - growbuflen, fd) == NULL)
741 break;
Bram Moolenaard9db8b42016-05-08 12:52:05 +0200742 linelen = (int)STRLEN(growbuf + growbuflen);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200743 growbuflen += linelen;
744 if (growbuf[growbuflen - 1] == '\n'
745#ifdef USE_CRNL
746 || growbuf[growbuflen - 1] == '\r'
747#endif
748 )
749 break;
750 if (growbufsiz == LINE_MAXLEN)
751 {
752 discard = TRUE;
753 break;
754 }
755
756 growbufsiz = 2 * growbufsiz < LINE_MAXLEN
757 ? 2 * growbufsiz : LINE_MAXLEN;
758 growbuf = vim_realloc(growbuf, 2 * growbufsiz);
759 if (growbuf == NULL)
760 goto qf_init_end;
761 }
762
763 while (discard)
764 {
765 /*
766 * The current line is longer than LINE_MAXLEN, continue
767 * reading but discard everything until EOL or EOF is
768 * reached.
769 */
770 if (fgets((char *)IObuff, IOSIZE, fd) == NULL
771 || (int)STRLEN(IObuff) < IOSIZE - 1
772 || IObuff[IOSIZE - 1] == '\n'
773#ifdef USE_CRNL
774 || IObuff[IOSIZE - 1] == '\r'
775#endif
776 )
777 break;
778 }
779
780 linebuf = growbuf;
781 linelen = growbuflen;
782 }
783 else
784 linebuf = IObuff;
785 }
786
787 if (linelen > 0 && linebuf[linelen - 1] == '\n')
788 linebuf[linelen - 1] = NUL;
789#ifdef USE_CRNL
790 if (linelen > 0 && linebuf[linelen - 1] == '\r')
791 linebuf[linelen - 1] = NUL;
Bram Moolenaar836082d2011-08-10 13:21:46 +0200792#endif
793
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200794#ifdef FEAT_MBYTE
795 remove_bom(linebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796#endif
797
Bram Moolenaar01265852006-03-20 21:50:15 +0000798 /* If there was no %> item start at the first pattern */
799 if (fmt_start == NULL)
800 fmt_ptr = fmt_first;
801 else
802 {
803 fmt_ptr = fmt_start;
804 fmt_start = NULL;
805 }
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 /*
808 * Try to match each part of 'errorformat' until we find a complete
809 * match or no match.
810 */
811 valid = TRUE;
812restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000813 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100815 int r;
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 idx = fmt_ptr->prefix;
818 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
819 continue;
820 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000821 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (!multiscan)
823 errmsg[0] = NUL;
824 lnum = 0;
825 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 enr = -1;
828 type = 0;
829 tail = NULL;
830
831 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200832 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100833 fmt_ptr->prog = regmatch.regprog;
834 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
836 if ((idx == 'C' || idx == 'Z') && !multiline)
837 continue;
838 if (vim_strchr((char_u *)"EWI", idx) != NULL)
839 type = idx;
840 else
841 type = 0;
842 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000843 * Extract error message data from matched line.
844 * We check for an actual submatch, because "\[" and "\]" in
845 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 */
847 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
848 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000849 int c;
850
851 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
852 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000853
854 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000855 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000856 *regmatch.endp[i] = NUL;
857 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
858 *regmatch.endp[i] = c;
859
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000861 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 continue;
863 }
864 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000865 {
866 if (regmatch.startp[i] == NULL)
867 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000871 {
872 if (regmatch.startp[i] == NULL)
873 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000877 {
878 if (regmatch.startp[i] == NULL)
879 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000883 {
884 if (regmatch.startp[i] == NULL)
885 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000887 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000888 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200889 {
890 if (linelen > errmsglen) {
891 /* linelen + null terminator */
892 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
893 goto qf_init_end;
894 errmsglen = linelen + 1;
895 }
896 vim_strncpy(errmsg, linebuf, linelen);
897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
899 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000900 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
901 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200903 if (len > errmsglen) {
904 /* len + null terminator */
905 if ((errmsg = vim_realloc(errmsg, len + 1))
906 == NULL)
907 goto qf_init_end;
908 errmsglen = len + 1;
909 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000910 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000913 {
914 if (regmatch.startp[i] == NULL)
915 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
919 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200920 char_u *match_ptr;
921
Bram Moolenaar4169da72006-06-20 18:49:32 +0000922 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
923 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200924 col = 0;
925 for (match_ptr = regmatch.startp[i];
926 match_ptr != regmatch.endp[i]; ++match_ptr)
927 {
928 ++col;
929 if (*match_ptr == TAB)
930 {
931 col += 7;
932 col -= col % 8;
933 }
934 }
935 ++col;
936 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 }
938 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
939 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000940 if (regmatch.startp[i] == NULL)
941 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000945 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
946 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000947 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
948 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000949 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
950 if (len > CMDBUFFSIZE - 5)
951 len = CMDBUFFSIZE - 5;
952 STRCPY(pattern, "^\\V");
953 STRNCAT(pattern, regmatch.startp[i], len);
954 pattern[len + 3] = '\\';
955 pattern[len + 4] = '$';
956 pattern[len + 5] = NUL;
957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 break;
959 }
960 }
961 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000962
Bram Moolenaar4770d092006-01-12 23:22:24 +0000963 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000965 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
967 if (idx == 'D') /* enter directory */
968 {
969 if (*namebuf == NUL)
970 {
971 EMSG(_("E379: Missing or empty directory name"));
972 goto error2;
973 }
974 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
975 goto error2;
976 }
977 else if (idx == 'X') /* leave directory */
978 directory = qf_pop_dir(&dir_stack);
979 }
980 namebuf[0] = NUL; /* no match found, remove file name */
981 lnum = 0; /* don't jump to this line */
982 valid = FALSE;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200983 if (linelen > errmsglen) {
984 /* linelen + null terminator */
985 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
986 goto qf_init_end;
987 errmsglen = linelen + 1;
988 }
989 /* copy whole line to error message */
990 vim_strncpy(errmsg, linebuf, linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000991 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 multiline = multiignore = FALSE;
993 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000994 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000996 /* honor %> item */
997 if (fmt_ptr->conthere)
998 fmt_start = fmt_ptr;
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +01001001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +01001003 multiignore = FALSE; /* reset continuation */
1004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1006 { /* continuation of multi-line msg */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001007 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
1008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (qfprev == NULL)
1010 goto error2;
1011 if (*errmsg && !multiignore)
1012 {
1013 len = (int)STRLEN(qfprev->qf_text);
1014 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
1015 == NULL)
1016 goto error2;
1017 STRCPY(ptr, qfprev->qf_text);
1018 vim_free(qfprev->qf_text);
1019 qfprev->qf_text = ptr;
1020 *(ptr += len) = '\n';
1021 STRCPY(++ptr, errmsg);
1022 }
1023 if (qfprev->qf_nr == -1)
1024 qfprev->qf_nr = enr;
1025 if (vim_isprintc(type) && !qfprev->qf_type)
1026 qfprev->qf_type = type; /* only printable chars allowed */
1027 if (!qfprev->qf_lnum)
1028 qfprev->qf_lnum = lnum;
1029 if (!qfprev->qf_col)
1030 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001031 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (!qfprev->qf_fnum)
1033 qfprev->qf_fnum = qf_get_fnum(directory,
1034 *namebuf || directory ? namebuf
1035 : currfile && valid ? currfile : 0);
1036 if (idx == 'Z')
1037 multiline = multiignore = FALSE;
1038 line_breakcheck();
1039 continue;
1040 }
1041 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1042 {
1043 /* global file names */
1044 valid = FALSE;
1045 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
1046 {
1047 if (*namebuf && idx == 'P')
1048 currfile = qf_push_dir(namebuf, &file_stack);
1049 else if (idx == 'Q')
1050 currfile = qf_pop_dir(&file_stack);
1051 *namebuf = NUL;
1052 if (tail && *tail)
1053 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00001054 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 multiscan = TRUE;
1056 goto restofline;
1057 }
1058 }
1059 }
1060 if (fmt_ptr->flags == '-') /* generally exclude this line */
1061 {
1062 if (multiline)
1063 multiignore = TRUE; /* also exclude continuation lines */
1064 continue;
1065 }
1066 }
1067
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001068 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001070 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001072 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001073 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 errmsg,
1075 lnum,
1076 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001077 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001078 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 enr,
1080 type,
1081 valid) == FAIL)
1082 goto error2;
1083 line_breakcheck();
1084 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00001085 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001087 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001089 /* no valid entry found */
1090 qi->qf_lists[qi->qf_curlist].qf_ptr =
1091 qi->qf_lists[qi->qf_curlist].qf_start;
1092 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1093 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 }
1095 else
1096 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001097 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1098 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1099 qi->qf_lists[qi->qf_curlist].qf_ptr =
1100 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001102 /* return number of matches */
1103 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 goto qf_init_ok;
1105 }
1106 EMSG(_(e_readerrf));
1107error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001108 qf_free(qi, qi->qf_curlist);
1109 qi->qf_listcount--;
1110 if (qi->qf_curlist > 0)
1111 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +00001113 if (fd != NULL)
1114 fclose(fd);
Bram Moolenaar688e3d12016-06-26 22:05:54 +02001115 free_efm_list(&fmt_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 qf_clean_dir_stack(&dir_stack);
1117 qf_clean_dir_stack(&file_stack);
1118qf_init_end:
1119 vim_free(namebuf);
1120 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001121 vim_free(pattern);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001122 vim_free(growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
1124#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001125 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126#endif
1127
1128 return retval;
1129}
1130
Bram Moolenaarfb604092014-07-23 15:55:00 +02001131 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001132qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001133{
1134 if (title != NULL)
1135 {
1136 char_u *p = alloc((int)STRLEN(title) + 2);
1137
1138 qi->qf_lists[qi->qf_curlist].qf_title = p;
1139 if (p != NULL)
1140 sprintf((char *)p, ":%s", (char *)title);
1141 }
1142}
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144/*
1145 * Prepare for adding a new quickfix list.
1146 */
1147 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001148qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149{
1150 int i;
1151
1152 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001153 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 * the current entry. This makes it possible to browse in a tree-like
1155 * way with ":grep'.
1156 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001157 while (qi->qf_listcount > qi->qf_curlist + 1)
1158 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
1160 /*
1161 * When the stack is full, remove to oldest entry
1162 * Otherwise, add a new entry.
1163 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001164 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001166 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001168 qi->qf_lists[i - 1] = qi->qf_lists[i];
1169 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001172 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001173 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001174 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175}
1176
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001177/*
1178 * Free a location list
1179 */
1180 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001181ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001182{
1183 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001184 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001185
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001186 qi = *pqi;
1187 if (qi == NULL)
1188 return;
1189 *pqi = NULL; /* Remove reference to this list */
1190
1191 qi->qf_refcount--;
1192 if (qi->qf_refcount < 1)
1193 {
1194 /* No references to this location list */
1195 for (i = 0; i < qi->qf_listcount; ++i)
1196 qf_free(qi, i);
1197 vim_free(qi);
1198 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001199}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001200
1201 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001202qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001203{
1204 int i;
1205 qf_info_T *qi = &ql_info;
1206
1207 if (wp != NULL)
1208 {
1209 /* location list */
1210 ll_free_all(&wp->w_llist);
1211 ll_free_all(&wp->w_llist_ref);
1212 }
1213 else
1214 /* quickfix list */
1215 for (i = 0; i < qi->qf_listcount; ++i)
1216 qf_free(qi, i);
1217}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219/*
1220 * Add an entry to the end of the list of errors.
1221 * Returns OK or FAIL.
1222 */
1223 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001224qf_add_entry(
1225 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001226 char_u *dir, /* optional directory name */
1227 char_u *fname, /* file name or NULL */
1228 int bufnum, /* buffer number or zero */
1229 char_u *mesg, /* message */
1230 long lnum, /* line number */
1231 int col, /* column */
1232 int vis_col, /* using visual column */
1233 char_u *pattern, /* search pattern */
1234 int nr, /* error number */
1235 int type, /* type character */
1236 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001238 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001239 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001241 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001243 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001244 {
1245 buf_T *buf = buflist_findnr(bufnum);
1246
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001247 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001248 if (buf != NULL)
1249 buf->b_has_qf_entry = TRUE;
1250 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001251 else
1252 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1254 {
1255 vim_free(qfp);
1256 return FAIL;
1257 }
1258 qfp->qf_lnum = lnum;
1259 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001260 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001261 if (pattern == NULL || *pattern == NUL)
1262 qfp->qf_pattern = NULL;
1263 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1264 {
1265 vim_free(qfp->qf_text);
1266 vim_free(qfp);
1267 return FAIL;
1268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 qfp->qf_nr = nr;
1270 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1271 type = 0;
1272 qfp->qf_type = type;
1273 qfp->qf_valid = valid;
1274
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001275 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001276 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1277 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001279 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001280 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1281 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001282 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 else
1285 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001286 qfp->qf_prev = *lastp;
1287 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001289 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001291 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001292 ++qi->qf_lists[qi->qf_curlist].qf_count;
1293 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1294 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001296 qi->qf_lists[qi->qf_curlist].qf_index =
1297 qi->qf_lists[qi->qf_curlist].qf_count;
1298 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
1300
1301 return OK;
1302}
1303
1304/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001305 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001306 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001307 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001308ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001309{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001310 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001311
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001312 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1313 if (qi != NULL)
1314 {
1315 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1316 qi->qf_refcount++;
1317 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001318
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001319 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001320}
1321
1322/*
1323 * Return the location list for window 'wp'.
1324 * If not present, allocate a location list
1325 */
1326 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001327ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001328{
1329 if (IS_LL_WINDOW(wp))
1330 /* For a location list window, use the referenced location list */
1331 return wp->w_llist_ref;
1332
1333 /*
1334 * For a non-location list window, w_llist_ref should not point to a
1335 * location list.
1336 */
1337 ll_free_all(&wp->w_llist_ref);
1338
1339 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001340 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001341 return wp->w_llist;
1342}
1343
1344/*
1345 * Copy the location list from window "from" to window "to".
1346 */
1347 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001348copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001349{
1350 qf_info_T *qi;
1351 int idx;
1352 int i;
1353
1354 /*
1355 * When copying from a location list window, copy the referenced
1356 * location list. For other windows, copy the location list for
1357 * that window.
1358 */
1359 if (IS_LL_WINDOW(from))
1360 qi = from->w_llist_ref;
1361 else
1362 qi = from->w_llist;
1363
1364 if (qi == NULL) /* no location list to copy */
1365 return;
1366
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001367 /* allocate a new location list */
1368 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001369 return;
1370
1371 to->w_llist->qf_listcount = qi->qf_listcount;
1372
1373 /* Copy the location lists one at a time */
1374 for (idx = 0; idx < qi->qf_listcount; idx++)
1375 {
1376 qf_list_T *from_qfl;
1377 qf_list_T *to_qfl;
1378
1379 to->w_llist->qf_curlist = idx;
1380
1381 from_qfl = &qi->qf_lists[idx];
1382 to_qfl = &to->w_llist->qf_lists[idx];
1383
1384 /* Some of the fields are populated by qf_add_entry() */
1385 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1386 to_qfl->qf_count = 0;
1387 to_qfl->qf_index = 0;
1388 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001389 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001390 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001391 if (from_qfl->qf_title != NULL)
1392 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1393 else
1394 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001395
1396 if (from_qfl->qf_count)
1397 {
1398 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001399 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001400
1401 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001402 for (i = 0, from_qfp = from_qfl->qf_start;
1403 i < from_qfl->qf_count && from_qfp != NULL;
1404 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001405 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001406 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001407 NULL,
1408 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001409 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001410 from_qfp->qf_text,
1411 from_qfp->qf_lnum,
1412 from_qfp->qf_col,
1413 from_qfp->qf_viscol,
1414 from_qfp->qf_pattern,
1415 from_qfp->qf_nr,
1416 0,
1417 from_qfp->qf_valid) == FAIL)
1418 {
1419 qf_free_all(to);
1420 return;
1421 }
1422 /*
1423 * qf_add_entry() will not set the qf_num field, as the
1424 * directory and file names are not supplied. So the qf_fnum
1425 * field is copied here.
1426 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001427 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001428 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1429 prevp->qf_type = from_qfp->qf_type; /* error type */
1430 if (from_qfl->qf_ptr == from_qfp)
1431 to_qfl->qf_ptr = prevp; /* current location */
1432 }
1433 }
1434
1435 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1436
1437 /* When no valid entries are present in the list, qf_ptr points to
1438 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001439 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001440 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001441 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001442 to_qfl->qf_index = 1;
1443 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001444 }
1445
1446 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1447}
1448
1449/*
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001450 * Get buffer number for file "dir.name".
1451 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 */
1453 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001454qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001456 char_u *ptr;
1457 buf_T *buf;
1458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 if (fname == NULL || *fname == NUL) /* no file name */
1460 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
Bram Moolenaare60acc12011-05-10 16:41:25 +02001462#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001463 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001464#endif
1465#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001466 if (directory != NULL)
1467 slash_adjust(directory);
1468 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001469#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001470 if (directory != NULL && !vim_isAbsName(fname)
1471 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1472 {
1473 /*
1474 * Here we check if the file really exists.
1475 * This should normally be true, but if make works without
1476 * "leaving directory"-messages we might have missed a
1477 * directory change.
1478 */
1479 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 vim_free(ptr);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001482 directory = qf_guess_filepath(fname);
1483 if (directory)
1484 ptr = concat_fnames(directory, fname, TRUE);
1485 else
1486 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001488 /* Use concatenated directory name and file name */
1489 buf = buflist_new(ptr, NULL, (linenr_T)0, 0);
1490 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001492 else
1493 buf = buflist_new(fname, NULL, (linenr_T)0, 0);
1494 if (buf == NULL)
1495 return 0;
1496 buf->b_has_qf_entry = TRUE;
1497 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498}
1499
1500/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001501 * Push dirbuf onto the directory stack and return pointer to actual dir or
1502 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 */
1504 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001505qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
1507 struct dir_stack_T *ds_new;
1508 struct dir_stack_T *ds_ptr;
1509
1510 /* allocate new stack element and hook it in */
1511 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1512 if (ds_new == NULL)
1513 return NULL;
1514
1515 ds_new->next = *stackptr;
1516 *stackptr = ds_new;
1517
1518 /* store directory on the stack */
1519 if (vim_isAbsName(dirbuf)
1520 || (*stackptr)->next == NULL
1521 || (*stackptr && dir_stack != *stackptr))
1522 (*stackptr)->dirname = vim_strsave(dirbuf);
1523 else
1524 {
1525 /* Okay we don't have an absolute path.
1526 * dirbuf must be a subdir of one of the directories on the stack.
1527 * Let's search...
1528 */
1529 ds_new = (*stackptr)->next;
1530 (*stackptr)->dirname = NULL;
1531 while (ds_new)
1532 {
1533 vim_free((*stackptr)->dirname);
1534 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1535 TRUE);
1536 if (mch_isdir((*stackptr)->dirname) == TRUE)
1537 break;
1538
1539 ds_new = ds_new->next;
1540 }
1541
1542 /* clean up all dirs we already left */
1543 while ((*stackptr)->next != ds_new)
1544 {
1545 ds_ptr = (*stackptr)->next;
1546 (*stackptr)->next = (*stackptr)->next->next;
1547 vim_free(ds_ptr->dirname);
1548 vim_free(ds_ptr);
1549 }
1550
1551 /* Nothing found -> it must be on top level */
1552 if (ds_new == NULL)
1553 {
1554 vim_free((*stackptr)->dirname);
1555 (*stackptr)->dirname = vim_strsave(dirbuf);
1556 }
1557 }
1558
1559 if ((*stackptr)->dirname != NULL)
1560 return (*stackptr)->dirname;
1561 else
1562 {
1563 ds_ptr = *stackptr;
1564 *stackptr = (*stackptr)->next;
1565 vim_free(ds_ptr);
1566 return NULL;
1567 }
1568}
1569
1570
1571/*
1572 * pop dirbuf from the directory stack and return previous directory or NULL if
1573 * stack is empty
1574 */
1575 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001576qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
1578 struct dir_stack_T *ds_ptr;
1579
1580 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1581 * What to do if it isn't? */
1582
1583 /* pop top element and free it */
1584 if (*stackptr != NULL)
1585 {
1586 ds_ptr = *stackptr;
1587 *stackptr = (*stackptr)->next;
1588 vim_free(ds_ptr->dirname);
1589 vim_free(ds_ptr);
1590 }
1591
1592 /* return NEW top element as current dir or NULL if stack is empty*/
1593 return *stackptr ? (*stackptr)->dirname : NULL;
1594}
1595
1596/*
1597 * clean up directory stack
1598 */
1599 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001600qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
1602 struct dir_stack_T *ds_ptr;
1603
1604 while ((ds_ptr = *stackptr) != NULL)
1605 {
1606 *stackptr = (*stackptr)->next;
1607 vim_free(ds_ptr->dirname);
1608 vim_free(ds_ptr);
1609 }
1610}
1611
1612/*
1613 * Check in which directory of the directory stack the given file can be
1614 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001615 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 * Cleans up intermediate directory entries.
1617 *
1618 * TODO: How to solve the following problem?
1619 * If we have the this directory tree:
1620 * ./
1621 * ./aa
1622 * ./aa/bb
1623 * ./bb
1624 * ./bb/x.c
1625 * and make says:
1626 * making all in aa
1627 * making all in bb
1628 * x.c:9: Error
1629 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1630 * qf_guess_filepath will return NULL.
1631 */
1632 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001633qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
1635 struct dir_stack_T *ds_ptr;
1636 struct dir_stack_T *ds_tmp;
1637 char_u *fullname;
1638
1639 /* no dirs on the stack - there's nothing we can do */
1640 if (dir_stack == NULL)
1641 return NULL;
1642
1643 ds_ptr = dir_stack->next;
1644 fullname = NULL;
1645 while (ds_ptr)
1646 {
1647 vim_free(fullname);
1648 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1649
1650 /* If concat_fnames failed, just go on. The worst thing that can happen
1651 * is that we delete the entire stack.
1652 */
1653 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1654 break;
1655
1656 ds_ptr = ds_ptr->next;
1657 }
1658
1659 vim_free(fullname);
1660
1661 /* clean up all dirs we already left */
1662 while (dir_stack->next != ds_ptr)
1663 {
1664 ds_tmp = dir_stack->next;
1665 dir_stack->next = dir_stack->next->next;
1666 vim_free(ds_tmp->dirname);
1667 vim_free(ds_tmp);
1668 }
1669
1670 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1671
1672}
1673
1674/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001675 * When loading a file from the quickfix, the auto commands may modify it.
1676 * This may invalidate the current quickfix entry. This function checks
1677 * whether a entry is still present in the quickfix.
1678 * Similar to location list.
1679 */
1680 static int
1681is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1682{
1683 qf_list_T *qfl;
1684 qfline_T *qfp;
1685 int i;
1686
1687 qfl = &qi->qf_lists[qi->qf_curlist];
1688
1689 /* Search for the entry in the current list */
1690 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1691 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001692 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001693 break;
1694
1695 if (i == qfl->qf_count) /* Entry is not found */
1696 return FALSE;
1697
1698 return TRUE;
1699}
1700
1701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 * jump to a quickfix line
1703 * if dir == FORWARD go "errornr" valid entries forward
1704 * if dir == BACKWARD go "errornr" valid entries backward
1705 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1706 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1707 * else if "errornr" is zero, redisplay the same line
1708 * else go to entry "errornr"
1709 */
1710 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001711qf_jump(
1712 qf_info_T *qi,
1713 int dir,
1714 int errornr,
1715 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001717 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001718 qfline_T *qf_ptr;
1719 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 int qf_index;
1721 int old_qf_fnum;
1722 int old_qf_index;
1723 int prev_index;
1724 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1725 char_u *err = e_no_more_items;
1726 linenr_T i;
1727 buf_T *old_curbuf;
1728 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 colnr_T screen_col;
1730 colnr_T char_col;
1731 char_u *line;
1732#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001733 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001734 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 int opened_window = FALSE;
1736 win_T *win;
1737 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001738 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001740 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 int print_message = TRUE;
1742 int len;
1743#ifdef FEAT_FOLDING
1744 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1745#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001746 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001747 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001749 if (qi == NULL)
1750 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001751
1752 if (qi->qf_curlist >= qi->qf_listcount
1753 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {
1755 EMSG(_(e_quickfix));
1756 return;
1757 }
1758
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001759 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001761 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 old_qf_index = qf_index;
1763 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1764 {
1765 while (errornr--)
1766 {
1767 old_qf_ptr = qf_ptr;
1768 prev_index = qf_index;
1769 old_qf_fnum = qf_ptr->qf_fnum;
1770 do
1771 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001772 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 || qf_ptr->qf_next == NULL)
1774 {
1775 qf_ptr = old_qf_ptr;
1776 qf_index = prev_index;
1777 if (err != NULL)
1778 {
1779 EMSG(_(err));
1780 goto theend;
1781 }
1782 errornr = 0;
1783 break;
1784 }
1785 ++qf_index;
1786 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001787 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1788 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1790 err = NULL;
1791 }
1792 }
1793 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1794 {
1795 while (errornr--)
1796 {
1797 old_qf_ptr = qf_ptr;
1798 prev_index = qf_index;
1799 old_qf_fnum = qf_ptr->qf_fnum;
1800 do
1801 {
1802 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1803 {
1804 qf_ptr = old_qf_ptr;
1805 qf_index = prev_index;
1806 if (err != NULL)
1807 {
1808 EMSG(_(err));
1809 goto theend;
1810 }
1811 errornr = 0;
1812 break;
1813 }
1814 --qf_index;
1815 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001816 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1817 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1819 err = NULL;
1820 }
1821 }
1822 else if (errornr != 0) /* go to specified number */
1823 {
1824 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1825 {
1826 --qf_index;
1827 qf_ptr = qf_ptr->qf_prev;
1828 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001829 while (errornr > qf_index && qf_index <
1830 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 && qf_ptr->qf_next != NULL)
1832 {
1833 ++qf_index;
1834 qf_ptr = qf_ptr->qf_next;
1835 }
1836 }
1837
1838#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001839 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1840 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 /* No need to print the error message if it's visible in the error
1842 * window */
1843 print_message = FALSE;
1844
1845 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001846 * For ":helpgrep" find a help window or open one.
1847 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001848 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001849 {
1850 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001851
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001852 if (cmdmod.tab != 0)
1853 wp = NULL;
1854 else
1855 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1856 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1857 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001858 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1859 win_enter(wp, TRUE);
1860 else
1861 {
1862 /*
1863 * Split off help window; put it at far top if no position
1864 * specified, the current window is vertically split and narrow.
1865 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001866 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001867 if (cmdmod.split == 0 && curwin->w_width != Columns
1868 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001869 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001870 if (qi != &ql_info)
1871 flags |= WSP_NEWLOC; /* don't copy the location list */
1872
1873 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001874 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001875 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001876
1877 if (curwin->w_height < p_hh)
1878 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001879
1880 if (qi != &ql_info) /* not a quickfix list */
1881 {
1882 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001883 curwin->w_llist = qi;
1884 qi->qf_refcount++;
1885 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001886 }
1887
1888 if (!p_im)
1889 restart_edit = 0; /* don't want insert mode in help file */
1890 }
1891
1892 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 * If currently in the quickfix window, find another window to show the
1894 * file in.
1895 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001896 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001898 win_T *usable_win_ptr = NULL;
1899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 /*
1901 * If there is no file specified, we don't know where to go.
1902 * But do advance, otherwise ":cn" gets stuck.
1903 */
1904 if (qf_ptr->qf_fnum == 0)
1905 goto theend;
1906
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001907 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001908
1909 ll_ref = curwin->w_llist_ref;
1910 if (ll_ref != NULL)
1911 {
1912 /* Find a window using the same location list that is not a
1913 * quickfix window. */
1914 FOR_ALL_WINDOWS(usable_win_ptr)
1915 if (usable_win_ptr->w_llist == ll_ref
1916 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001917 {
1918 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001919 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001920 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001921 }
1922
1923 if (!usable_win)
1924 {
1925 /* Locate a window showing a normal buffer */
1926 FOR_ALL_WINDOWS(win)
1927 if (win->w_buffer->b_p_bt[0] == NUL)
1928 {
1929 usable_win = 1;
1930 break;
1931 }
1932 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001935 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001936 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001938 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001939 {
1940 tabpage_T *tp;
1941 win_T *wp;
1942
1943 FOR_ALL_TAB_WINDOWS(tp, wp)
1944 {
1945 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1946 {
1947 goto_tabpage_win(tp, wp);
1948 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001949 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001950 }
1951 }
1952 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001953win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001954
1955 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001956 * If there is only one window and it is the quickfix window, create a
1957 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001958 */
1959 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001961 flags = WSP_ABOVE;
1962 if (ll_ref != NULL)
1963 flags |= WSP_NEWLOC;
1964 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 goto failed; /* not enough room for window */
1966 opened_window = TRUE; /* close it when fail */
1967 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001968 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001969 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001970 if (ll_ref != NULL)
1971 {
1972 /* The new window should use the location list from the
1973 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001974 curwin->w_llist = ll_ref;
1975 ll_ref->qf_refcount++;
1976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 }
1978 else
1979 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 if (curwin->w_llist_ref != NULL)
1981 {
1982 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001983 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001984 if (win == NULL)
1985 {
1986 /* Find the window showing the selected file */
1987 FOR_ALL_WINDOWS(win)
1988 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1989 break;
1990 if (win == NULL)
1991 {
1992 /* Find a previous usable window */
1993 win = curwin;
1994 do
1995 {
1996 if (win->w_buffer->b_p_bt[0] == NUL)
1997 break;
1998 if (win->w_prev == NULL)
1999 win = lastwin; /* wrap around the top */
2000 else
2001 win = win->w_prev; /* go to previous window */
2002 } while (win != curwin);
2003 }
2004 }
2005 win_goto(win);
2006
2007 /* If the location list for the window is not set, then set it
2008 * to the location list from the location window */
2009 if (win->w_llist == NULL)
2010 {
2011 win->w_llist = ll_ref;
2012 ll_ref->qf_refcount++;
2013 }
2014 }
2015 else
2016 {
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 /*
2019 * Try to find a window that shows the right buffer.
2020 * Default to the window just above the quickfix buffer.
2021 */
2022 win = curwin;
2023 altwin = NULL;
2024 for (;;)
2025 {
2026 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2027 break;
2028 if (win->w_prev == NULL)
2029 win = lastwin; /* wrap around the top */
2030 else
2031 win = win->w_prev; /* go to previous window */
2032
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002033 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
2035 /* Didn't find it, go to the window before the quickfix
2036 * window. */
2037 if (altwin != NULL)
2038 win = altwin;
2039 else if (curwin->w_prev != NULL)
2040 win = curwin->w_prev;
2041 else
2042 win = curwin->w_next;
2043 break;
2044 }
2045
2046 /* Remember a usable window. */
2047 if (altwin == NULL && !win->w_p_pvw
2048 && win->w_buffer->b_p_bt[0] == NUL)
2049 altwin = win;
2050 }
2051
2052 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 }
2056#endif
2057
2058 /*
2059 * If there is a file name,
2060 * read the wanted file if needed, and check autowrite etc.
2061 */
2062 old_curbuf = curbuf;
2063 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002064
2065 if (qf_ptr->qf_fnum != 0)
2066 {
2067 if (qf_ptr->qf_type == 1)
2068 {
2069 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2070 * set b_p_ro flag). */
2071 if (!can_abandon(curbuf, forceit))
2072 {
2073 EMSG(_(e_nowrtmsg));
2074 ok = FALSE;
2075 }
2076 else
2077 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002078 ECMD_HIDE + ECMD_SET_HELP,
2079 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002080 }
2081 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002082 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002083 int old_qf_curlist = qi->qf_curlist;
2084 int is_abort = FALSE;
2085
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002086 ok = buflist_getfile(qf_ptr->qf_fnum,
2087 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002088 if (qi != &ql_info && !win_valid(oldwin))
2089 {
2090 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002091 is_abort = TRUE;
2092 opened_window = FALSE;
2093 }
2094 else if (old_qf_curlist != qi->qf_curlist
2095 || !is_qf_entry_present(qi, qf_ptr))
2096 {
2097 if (qi == &ql_info)
2098 EMSG(_("E925: Current quickfix was changed"));
2099 else
2100 EMSG(_("E926: Current location list was changed"));
2101 is_abort = TRUE;
2102 }
2103
2104 if (is_abort)
2105 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002106 ok = FALSE;
2107 qi = NULL;
2108 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002109 }
2110 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002111 }
2112
2113 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
2115 /* When not switched to another buffer, still need to set pc mark */
2116 if (curbuf == old_curbuf)
2117 setpcmark();
2118
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002119 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002121 /*
2122 * Go to line with error, unless qf_lnum is 0.
2123 */
2124 i = qf_ptr->qf_lnum;
2125 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002127 if (i > curbuf->b_ml.ml_line_count)
2128 i = curbuf->b_ml.ml_line_count;
2129 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002131 if (qf_ptr->qf_col > 0)
2132 {
2133 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002134#ifdef FEAT_VIRTUALEDIT
2135 curwin->w_cursor.coladd = 0;
2136#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002137 if (qf_ptr->qf_viscol == TRUE)
2138 {
2139 /*
2140 * Check each character from the beginning of the error
2141 * line up to the error column. For each tab character
2142 * found, reduce the error column value by the length of
2143 * a tab character.
2144 */
2145 line = ml_get_curline();
2146 screen_col = 0;
2147 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2148 {
2149 if (*line == NUL)
2150 break;
2151 if (*line++ == '\t')
2152 {
2153 curwin->w_cursor.col -= 7 - (screen_col % 8);
2154 screen_col += 8 - (screen_col % 8);
2155 }
2156 else
2157 ++screen_col;
2158 }
2159 }
2160 check_cursor();
2161 }
2162 else
2163 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
2165 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002166 {
2167 pos_T save_cursor;
2168
2169 /* Move the cursor to the first line in the buffer */
2170 save_cursor = curwin->w_cursor;
2171 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002172 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2173 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002174 curwin->w_cursor = save_cursor;
2175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177#ifdef FEAT_FOLDING
2178 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2179 foldOpenCursor();
2180#endif
2181 if (print_message)
2182 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002183 /* Update the screen before showing the message, unless the screen
2184 * scrolled up. */
2185 if (!msg_scrolled)
2186 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002188 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2190 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2191 /* Add the message, skipping leading whitespace and newlines. */
2192 len = (int)STRLEN(IObuff);
2193 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2194
2195 /* Output the message. Overwrite to avoid scrolling when the 'O'
2196 * flag is present in 'shortmess'; But when not jumping, print the
2197 * whole message. */
2198 i = msg_scroll;
2199 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2200 msg_scroll = TRUE;
2201 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2202 msg_scroll = FALSE;
2203 msg_attr_keep(IObuff, 0, TRUE);
2204 msg_scroll = i;
2205 }
2206 }
2207 else
2208 {
2209#ifdef FEAT_WINDOWS
2210 if (opened_window)
2211 win_close(curwin, TRUE); /* Close opened window */
2212#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002213 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
2215 /*
2216 * Couldn't open file, so put index back where it was. This could
2217 * happen if the file was readonly and we changed something.
2218 */
2219#ifdef FEAT_WINDOWS
2220failed:
2221#endif
2222 qf_ptr = old_qf_ptr;
2223 qf_index = old_qf_index;
2224 }
2225 }
2226theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002227 if (qi != NULL)
2228 {
2229 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2230 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#ifdef FEAT_WINDOWS
2233 if (p_swb != old_swb && opened_window)
2234 {
2235 /* Restore old 'switchbuf' value, but not when an autocommand or
2236 * modeline has changed the value. */
2237 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002240 swb_flags = old_swb_flags;
2241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 else
2243 free_string_option(old_swb);
2244 }
2245#endif
2246}
2247
2248/*
2249 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002250 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 */
2252 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002253qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002255 buf_T *buf;
2256 char_u *fname;
2257 qfline_T *qfp;
2258 int i;
2259 int idx1 = 1;
2260 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002261 char_u *arg = eap->arg;
2262 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002264 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002266 if (eap->cmdidx == CMD_llist)
2267 {
2268 qi = GET_LOC_LIST(curwin);
2269 if (qi == NULL)
2270 {
2271 EMSG(_(e_loclist));
2272 return;
2273 }
2274 }
2275
2276 if (qi->qf_curlist >= qi->qf_listcount
2277 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
2279 EMSG(_(e_quickfix));
2280 return;
2281 }
2282 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2283 {
2284 EMSG(_(e_trailing));
2285 return;
2286 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002287 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 if (idx1 < 0)
2289 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2290 if (idx2 < 0)
2291 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2292
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002293 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002295 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2296 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2299 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002300 msg_putchar('\n');
2301 if (got_int)
2302 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002303
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002304 fname = NULL;
2305 if (qfp->qf_fnum != 0
2306 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2307 {
2308 fname = buf->b_fname;
2309 if (qfp->qf_type == 1) /* :helpgrep */
2310 fname = gettail(fname);
2311 }
2312 if (fname == NULL)
2313 sprintf((char *)IObuff, "%2d", i);
2314 else
2315 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2316 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002317 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002318 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2319 if (qfp->qf_lnum == 0)
2320 IObuff[0] = NUL;
2321 else if (qfp->qf_col == 0)
2322 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2323 else
2324 sprintf((char *)IObuff, ":%ld col %d",
2325 qfp->qf_lnum, qfp->qf_col);
2326 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2327 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2328 msg_puts_attr(IObuff, hl_attr(HLF_N));
2329 if (qfp->qf_pattern != NULL)
2330 {
2331 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2332 STRCAT(IObuff, ":");
2333 msg_puts(IObuff);
2334 }
2335 msg_puts((char_u *)" ");
2336
2337 /* Remove newlines and leading whitespace from the text. For an
2338 * unrecognized line keep the indent, the compiler may mark a word
2339 * with ^^^^. */
2340 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2342 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002343 msg_prt_line(IObuff, FALSE);
2344 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002346
2347 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002348 if (qfp == NULL)
2349 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002350 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 ui_breakcheck();
2352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353}
2354
2355/*
2356 * Remove newlines and leading whitespace from an error message.
2357 * Put the result in "buf[bufsize]".
2358 */
2359 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002360qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
2362 int i;
2363 char_u *p = text;
2364
2365 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2366 {
2367 if (*p == '\n')
2368 {
2369 buf[i] = ' ';
2370 while (*++p != NUL)
2371 if (!vim_iswhite(*p) && *p != '\n')
2372 break;
2373 }
2374 else
2375 buf[i] = *p++;
2376 }
2377 buf[i] = NUL;
2378}
2379
2380/*
2381 * ":colder [count]": Up in the quickfix stack.
2382 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002383 * ":lolder [count]": Up in the location list stack.
2384 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 */
2386 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002387qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002389 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 int count;
2391
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002392 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2393 {
2394 qi = GET_LOC_LIST(curwin);
2395 if (qi == NULL)
2396 {
2397 EMSG(_(e_loclist));
2398 return;
2399 }
2400 }
2401
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (eap->addr_count != 0)
2403 count = eap->line2;
2404 else
2405 count = 1;
2406 while (count--)
2407 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002408 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002410 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {
2412 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002413 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002415 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417 else
2418 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002419 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
2421 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002422 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002424 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002427 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428}
2429
2430 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002431qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002434 qi->qf_curlist + 1, qi->qf_listcount,
2435 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002437 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438#endif
2439}
2440
2441/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002442 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 */
2444 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002445qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002447 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002448 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002449 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002451 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002453 qfp = qi->qf_lists[idx].qf_start;
2454 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002455 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002456 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002457 vim_free(qfp->qf_text);
2458 stop = (qfp == qfpnext);
2459 vim_free(qfp->qf_pattern);
2460 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002461 if (stop)
2462 /* Somehow qf_count may have an incorrect value, set it to 1
2463 * to avoid crashing when it's wrong.
2464 * TODO: Avoid qf_count being incorrect. */
2465 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002466 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002467 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002468 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002470 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002471 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002472 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473}
2474
2475/*
2476 * qf_mark_adjust: adjust marks
2477 */
2478 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002479qf_mark_adjust(
2480 win_T *wp,
2481 linenr_T line1,
2482 linenr_T line2,
2483 long amount,
2484 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002486 int i;
2487 qfline_T *qfp;
2488 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002489 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002490 int found_one = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002492 if (!curbuf->b_has_qf_entry)
2493 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002494 if (wp != NULL)
2495 {
2496 if (wp->w_llist == NULL)
2497 return;
2498 qi = wp->w_llist;
2499 }
2500
2501 for (idx = 0; idx < qi->qf_listcount; ++idx)
2502 if (qi->qf_lists[idx].qf_count)
2503 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002504 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2505 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (qfp->qf_fnum == curbuf->b_fnum)
2507 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002508 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2510 {
2511 if (amount == MAXLNUM)
2512 qfp->qf_cleared = TRUE;
2513 else
2514 qfp->qf_lnum += amount;
2515 }
2516 else if (amount_after && qfp->qf_lnum > line2)
2517 qfp->qf_lnum += amount_after;
2518 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002519
2520 if (!found_one)
2521 curbuf->b_has_qf_entry = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522}
2523
2524/*
2525 * Make a nice message out of the error character and the error number:
2526 * char number message
2527 * e or E 0 " error"
2528 * w or W 0 " warning"
2529 * i or I 0 " info"
2530 * 0 0 ""
2531 * other 0 " c"
2532 * e or E n " error n"
2533 * w or W n " warning n"
2534 * i or I n " info n"
2535 * 0 n " error n"
2536 * other n " c n"
2537 * 1 x "" :helpgrep
2538 */
2539 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002540qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542 static char_u buf[20];
2543 static char_u cc[3];
2544 char_u *p;
2545
2546 if (c == 'W' || c == 'w')
2547 p = (char_u *)" warning";
2548 else if (c == 'I' || c == 'i')
2549 p = (char_u *)" info";
2550 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2551 p = (char_u *)" error";
2552 else if (c == 0 || c == 1)
2553 p = (char_u *)"";
2554 else
2555 {
2556 cc[0] = ' ';
2557 cc[1] = c;
2558 cc[2] = NUL;
2559 p = cc;
2560 }
2561
2562 if (nr <= 0)
2563 return p;
2564
2565 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2566 return buf;
2567}
2568
2569#if defined(FEAT_WINDOWS) || defined(PROTO)
2570/*
2571 * ":cwindow": open the quickfix window if we have errors to display,
2572 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002573 * ":lwindow": open the location list window if we have locations to display,
2574 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 */
2576 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002577ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002579 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 win_T *win;
2581
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002582 if (eap->cmdidx == CMD_lwindow)
2583 {
2584 qi = GET_LOC_LIST(curwin);
2585 if (qi == NULL)
2586 return;
2587 }
2588
2589 /* Look for an existing quickfix window. */
2590 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592 /*
2593 * If a quickfix window is open but we have no errors to display,
2594 * close the window. If a quickfix window is not open, then open
2595 * it if we have errors; otherwise, leave it closed.
2596 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002597 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002598 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002599 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
2601 if (win != NULL)
2602 ex_cclose(eap);
2603 }
2604 else if (win == NULL)
2605 ex_copen(eap);
2606}
2607
2608/*
2609 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002610 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002613ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002615 win_T *win = NULL;
2616 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002618 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2619 {
2620 qi = GET_LOC_LIST(curwin);
2621 if (qi == NULL)
2622 return;
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002625 /* Find existing quickfix window and close it. */
2626 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 if (win != NULL)
2628 win_close(win, FALSE);
2629}
2630
2631/*
2632 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002633 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 */
2635 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002636ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002638 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002641 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002642 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002643 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002645 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2646 {
2647 qi = GET_LOC_LIST(curwin);
2648 if (qi == NULL)
2649 {
2650 EMSG(_(e_loclist));
2651 return;
2652 }
2653 }
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 if (eap->addr_count != 0)
2656 height = eap->line2;
2657 else
2658 height = QF_WINHEIGHT;
2659
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#ifdef FEAT_GUI
2662 need_mouse_correct = TRUE;
2663#endif
2664
2665 /*
2666 * Find existing quickfix window, or open a new one.
2667 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002668 win = qf_find_win(qi);
2669
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002670 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002673 if (eap->addr_count != 0)
2674 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002675 if (cmdmod.split & WSP_VERT)
2676 {
2677 if (height != W_WIDTH(win))
2678 win_setwidth(height);
2679 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002680 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002681 win_setheight(height);
2682 }
2683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 else
2685 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002686 qf_buf = qf_find_buf(qi);
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 /* The current window becomes the previous window afterwards. */
2689 win = curwin;
2690
Bram Moolenaar77642c02012-11-20 17:55:10 +01002691 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2692 && cmdmod.split == 0)
2693 /* Create the new window at the very bottom, except when
2694 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002695 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002696 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002698 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002700 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002702 /*
2703 * For the location list window, create a reference to the
2704 * location list from the window 'win'.
2705 */
2706 curwin->w_llist_ref = win->w_llist;
2707 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002709
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002710 if (oldwin != curwin)
2711 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002712 if (qf_buf != NULL)
2713 /* Use the existing quickfix buffer */
2714 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002715 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002716 else
2717 {
2718 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002719 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002720 /* switch off 'swapfile' */
2721 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2722 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002723 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002724 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002725 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002726#ifdef FEAT_DIFF
2727 curwin->w_p_diff = FALSE;
2728#endif
2729#ifdef FEAT_FOLDING
2730 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2731 OPT_LOCAL);
2732#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002735 /* Only set the height when still in the same tab page and there is no
2736 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002737 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 win_setheight(height);
2739 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2740 if (win_valid(win))
2741 prevwin = win;
2742 }
2743
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002744 qf_set_title_var(qi);
2745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 /*
2747 * Fill the buffer with the quickfix list.
2748 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002749 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002751 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 curwin->w_cursor.col = 0;
2753 check_cursor();
2754 update_topline(); /* scroll to show the line */
2755}
2756
2757/*
2758 * Return the number of the current entry (line number in the quickfix
2759 * window).
2760 */
2761 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002762qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002764 qf_info_T *qi = &ql_info;
2765
2766 if (IS_LL_WINDOW(wp))
2767 /* In the location list window, use the referenced location list */
2768 qi = wp->w_llist_ref;
2769
2770 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771}
2772
2773/*
2774 * Update the cursor position in the quickfix window to the current error.
2775 * Return TRUE if there is a quickfix window.
2776 */
2777 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002778qf_win_pos_update(
2779 qf_info_T *qi,
2780 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 /*
2786 * Put the cursor on the current error in the quickfix window, so that
2787 * it's viewable.
2788 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002789 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if (win != NULL
2791 && qf_index <= win->w_buffer->b_ml.ml_line_count
2792 && old_qf_index != qf_index)
2793 {
2794 win_T *old_curwin = curwin;
2795
2796 curwin = win;
2797 curbuf = win->w_buffer;
2798 if (qf_index > old_qf_index)
2799 {
2800 curwin->w_redraw_top = old_qf_index;
2801 curwin->w_redraw_bot = qf_index;
2802 }
2803 else
2804 {
2805 curwin->w_redraw_top = qf_index;
2806 curwin->w_redraw_bot = old_qf_index;
2807 }
2808 curwin->w_cursor.lnum = qf_index;
2809 curwin->w_cursor.col = 0;
2810 update_topline(); /* scroll to show the line */
2811 redraw_later(VALID);
2812 curwin->w_redr_status = TRUE; /* update ruler */
2813 curwin = old_curwin;
2814 curbuf = curwin->w_buffer;
2815 }
2816 return win != NULL;
2817}
2818
2819/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002820 * Check whether the given window is displaying the specified quickfix/location
2821 * list buffer
2822 */
2823 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002824is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002825{
2826 /*
2827 * A window displaying the quickfix buffer will have the w_llist_ref field
2828 * set to NULL.
2829 * A window displaying a location list buffer will have the w_llist_ref
2830 * pointing to the location list.
2831 */
2832 if (bt_quickfix(win->w_buffer))
2833 if ((qi == &ql_info && win->w_llist_ref == NULL)
2834 || (qi != &ql_info && win->w_llist_ref == qi))
2835 return TRUE;
2836
2837 return FALSE;
2838}
2839
2840/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002841 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002842 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002843 */
2844 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002845qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002846{
2847 win_T *win;
2848
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002849 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002850 if (is_qf_win(win, qi))
2851 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002852
2853 return win;
2854}
2855
2856/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002857 * Find a quickfix buffer.
2858 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 */
2860 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002861qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002863 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002864 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
Bram Moolenaar9c102382006-05-03 21:26:49 +00002866 FOR_ALL_TAB_WINDOWS(tp, win)
2867 if (is_qf_win(win, qi))
2868 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002869
Bram Moolenaar9c102382006-05-03 21:26:49 +00002870 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871}
2872
2873/*
2874 * Find the quickfix buffer. If it exists, update the contents.
2875 */
2876 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002877qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
2879 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002880 win_T *win;
2881 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
2884 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002885 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 if (buf != NULL)
2887 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02002888 linenr_T old_line_count = buf->b_ml.ml_line_count;
2889
2890 if (old_last == NULL)
2891 /* set curwin/curbuf to buf and save a few things */
2892 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002894 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002895 {
2896 curwin_save = curwin;
2897 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002898 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002899 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002900 }
2901
Bram Moolenaar864293a2016-06-02 13:40:04 +02002902 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01002903
Bram Moolenaar864293a2016-06-02 13:40:04 +02002904 if (old_last == NULL)
2905 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002906 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002907
2908 /* restore curwin/curbuf and a few other things */
2909 aucmd_restbuf(&aco);
2910 }
2911
2912 /* Only redraw when added lines are visible. This avoids flickering
2913 * when the added lines are not visible. */
2914 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
2915 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917}
2918
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002919/*
2920 * Set "w:quickfix_title" if "qi" has a title.
2921 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002922 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002923qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002924{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002925 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2926 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002927 qi->qf_lists[qi->qf_curlist].qf_title);
2928}
2929
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930/*
2931 * Fill current buffer with quickfix errors, replacing any previous contents.
2932 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02002933 * If "old_last" is not NULL append the items after this one.
2934 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
2935 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 */
2937 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002938qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002940 linenr_T lnum;
2941 qfline_T *qfp;
2942 buf_T *errbuf;
2943 int len;
2944 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
Bram Moolenaar864293a2016-06-02 13:40:04 +02002946 if (old_last == NULL)
2947 {
2948 if (buf != curbuf)
2949 {
2950 EMSG2(_(e_intern2), "qf_fill_buffer()");
2951 return;
2952 }
2953
2954 /* delete all existing lines */
2955 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2956 (void)ml_delete((linenr_T)1, FALSE);
2957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002960 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
2962 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002963 if (old_last == NULL)
2964 {
2965 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2966 lnum = 0;
2967 }
2968 else
2969 {
2970 qfp = old_last->qf_next;
2971 lnum = buf->b_ml.ml_line_count;
2972 }
2973 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
2975 if (qfp->qf_fnum != 0
2976 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2977 && errbuf->b_fname != NULL)
2978 {
2979 if (qfp->qf_type == 1) /* :helpgrep */
2980 STRCPY(IObuff, gettail(errbuf->b_fname));
2981 else
2982 STRCPY(IObuff, errbuf->b_fname);
2983 len = (int)STRLEN(IObuff);
2984 }
2985 else
2986 len = 0;
2987 IObuff[len++] = '|';
2988
2989 if (qfp->qf_lnum > 0)
2990 {
2991 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2992 len += (int)STRLEN(IObuff + len);
2993
2994 if (qfp->qf_col > 0)
2995 {
2996 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2997 len += (int)STRLEN(IObuff + len);
2998 }
2999
3000 sprintf((char *)IObuff + len, "%s",
3001 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3002 len += (int)STRLEN(IObuff + len);
3003 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003004 else if (qfp->qf_pattern != NULL)
3005 {
3006 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3007 len += (int)STRLEN(IObuff + len);
3008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 IObuff[len++] = '|';
3010 IObuff[len++] = ' ';
3011
3012 /* Remove newlines and leading whitespace from the text.
3013 * For an unrecognized line keep the indent, the compiler may
3014 * mark a word with ^^^^. */
3015 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3016 IObuff + len, IOSIZE - len);
3017
Bram Moolenaar864293a2016-06-02 13:40:04 +02003018 if (ml_append_buf(buf, lnum, IObuff,
3019 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003021 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003023 if (qfp == NULL)
3024 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003026
3027 if (old_last == NULL)
3028 /* Delete the empty line which is now at the end */
3029 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 }
3031
3032 /* correct cursor position */
3033 check_lnums(TRUE);
3034
Bram Moolenaar864293a2016-06-02 13:40:04 +02003035 if (old_last == NULL)
3036 {
3037 /* Set the 'filetype' to "qf" each time after filling the buffer.
3038 * This resembles reading a file into a buffer, it's more logical when
3039 * using autocommands. */
3040 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3041 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003044 keep_filetype = TRUE; /* don't detect 'filetype' */
3045 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003047 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003049 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003051 /* make sure it will be redrawn */
3052 redraw_curbuf_later(NOT_VALID);
3053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 /* Restore KeyTyped, setting 'filetype' may reset it. */
3056 KeyTyped = old_KeyTyped;
3057}
3058
3059#endif /* FEAT_WINDOWS */
3060
3061/*
3062 * Return TRUE if "buf" is the quickfix buffer.
3063 */
3064 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003065bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003067 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068}
3069
3070/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003071 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3072 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 */
3074 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003075bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003077 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3078 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079}
3080
3081/*
3082 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3083 */
3084 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003085bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003087 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088}
3089
3090 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003091bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 if (bt_dontwrite(buf))
3094 {
3095 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3096 return TRUE;
3097 }
3098 return FALSE;
3099}
3100
3101/*
3102 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3103 * and 'bufhidden'.
3104 */
3105 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003106buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3109 switch (buf->b_p_bh[0])
3110 {
3111 case 'u': /* "unload" */
3112 case 'w': /* "wipe" */
3113 case 'd': return FALSE; /* "delete" */
3114 case 'h': return TRUE; /* "hide" */
3115 }
3116 return (p_hid || cmdmod.hide);
3117}
3118
3119/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003120 * Return TRUE when using ":vimgrep" for ":grep".
3121 */
3122 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003123grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003124{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003125 return ((cmdidx == CMD_grep
3126 || cmdidx == CMD_lgrep
3127 || cmdidx == CMD_grepadd
3128 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003129 && STRCMP("internal",
3130 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3131}
3132
3133/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003134 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 */
3136 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003137ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003139 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 char_u *cmd;
3141 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003142 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003143 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003144 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003145#ifdef FEAT_AUTOCMD
3146 char_u *au_name = NULL;
3147
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003148 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3149 if (grep_internal(eap->cmdidx))
3150 {
3151 ex_vimgrep(eap);
3152 return;
3153 }
3154
Bram Moolenaar7c626922005-02-07 22:01:03 +00003155 switch (eap->cmdidx)
3156 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003157 case CMD_make: au_name = (char_u *)"make"; break;
3158 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3159 case CMD_grep: au_name = (char_u *)"grep"; break;
3160 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3161 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3162 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003163 default: break;
3164 }
3165 if (au_name != NULL)
3166 {
3167 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3168 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003169# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003170 if (did_throw || force_abort)
3171 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003172# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003173 }
3174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
Bram Moolenaara6557602006-02-04 22:43:20 +00003176 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3177 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003178 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003179
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003181 fname = get_mef_name();
3182 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003184 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186 /*
3187 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3188 */
3189 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3190 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003191 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 cmd = alloc(len);
3193 if (cmd == NULL)
3194 return;
3195 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3196 (char *)p_shq);
3197 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003198 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 /*
3200 * Output a newline if there's something else than the :make command that
3201 * was typed (in which case the cursor is in column 0).
3202 */
3203 if (msg_col == 0)
3204 msg_didout = FALSE;
3205 msg_start();
3206 MSG_PUTS(":!");
3207 msg_outtrans(cmd); /* show what we are doing */
3208
3209 /* let the shell know if we are redirecting output or not */
3210 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3211
3212#ifdef AMIGA
3213 out_flush();
3214 /* read window status report and redraw before message */
3215 (void)char_avail();
3216#endif
3217
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003218 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003219 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3220 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003221 && eap->cmdidx != CMD_lgrepadd),
3222 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003223 if (wp != NULL)
3224 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003225#ifdef FEAT_AUTOCMD
3226 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003227 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003228 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3229 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003230 if (qi->qf_curlist < qi->qf_listcount)
3231 res = qi->qf_lists[qi->qf_curlist].qf_count;
3232 else
3233 res = 0;
3234 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003235#endif
3236 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003237 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
Bram Moolenaar7c626922005-02-07 22:01:03 +00003239 mch_remove(fname);
3240 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 vim_free(cmd);
3242}
3243
3244/*
3245 * Return the name for the errorfile, in allocated memory.
3246 * Find a new unique name when 'makeef' contains "##".
3247 * Returns NULL for error.
3248 */
3249 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003250get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
3252 char_u *p;
3253 char_u *name;
3254 static int start = -1;
3255 static int off = 0;
3256#ifdef HAVE_LSTAT
3257 struct stat sb;
3258#endif
3259
3260 if (*p_mef == NUL)
3261 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003262 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (name == NULL)
3264 EMSG(_(e_notmp));
3265 return name;
3266 }
3267
3268 for (p = p_mef; *p; ++p)
3269 if (p[0] == '#' && p[1] == '#')
3270 break;
3271
3272 if (*p == NUL)
3273 return vim_strsave(p_mef);
3274
3275 /* Keep trying until the name doesn't exist yet. */
3276 for (;;)
3277 {
3278 if (start == -1)
3279 start = mch_get_pid();
3280 else
3281 off += 19;
3282
3283 name = alloc((unsigned)STRLEN(p_mef) + 30);
3284 if (name == NULL)
3285 break;
3286 STRCPY(name, p_mef);
3287 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3288 STRCAT(name, p + 2);
3289 if (mch_getperm(name) < 0
3290#ifdef HAVE_LSTAT
3291 /* Don't accept a symbolic link, its a security risk. */
3292 && mch_lstat((char *)name, &sb) < 0
3293#endif
3294 )
3295 break;
3296 vim_free(name);
3297 }
3298 return name;
3299}
3300
3301/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003302 * Returns the number of valid entries in the current quickfix/location list.
3303 */
3304 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003305qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003306{
3307 qf_info_T *qi = &ql_info;
3308 qfline_T *qfp;
3309 int i, sz = 0;
3310 int prev_fnum = 0;
3311
3312 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3313 {
3314 /* Location list */
3315 qi = GET_LOC_LIST(curwin);
3316 if (qi == NULL)
3317 return 0;
3318 }
3319
3320 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003321 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003322 ++i, qfp = qfp->qf_next)
3323 {
3324 if (qfp->qf_valid)
3325 {
3326 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3327 sz++; /* Count all valid entries */
3328 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3329 {
3330 /* Count the number of files */
3331 sz++;
3332 prev_fnum = qfp->qf_fnum;
3333 }
3334 }
3335 }
3336
3337 return sz;
3338}
3339
3340/*
3341 * Returns the current index of the quickfix/location list.
3342 * Returns 0 if there is an error.
3343 */
3344 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003345qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003346{
3347 qf_info_T *qi = &ql_info;
3348
3349 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3350 {
3351 /* Location list */
3352 qi = GET_LOC_LIST(curwin);
3353 if (qi == NULL)
3354 return 0;
3355 }
3356
3357 return qi->qf_lists[qi->qf_curlist].qf_index;
3358}
3359
3360/*
3361 * Returns the current index in the quickfix/location list (counting only valid
3362 * entries). If no valid entries are in the list, then returns 1.
3363 */
3364 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003365qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003366{
3367 qf_info_T *qi = &ql_info;
3368 qf_list_T *qfl;
3369 qfline_T *qfp;
3370 int i, eidx = 0;
3371 int prev_fnum = 0;
3372
3373 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3374 {
3375 /* Location list */
3376 qi = GET_LOC_LIST(curwin);
3377 if (qi == NULL)
3378 return 1;
3379 }
3380
3381 qfl = &qi->qf_lists[qi->qf_curlist];
3382 qfp = qfl->qf_start;
3383
3384 /* check if the list has valid errors */
3385 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3386 return 1;
3387
3388 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3389 {
3390 if (qfp->qf_valid)
3391 {
3392 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3393 {
3394 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3395 {
3396 /* Count the number of files */
3397 eidx++;
3398 prev_fnum = qfp->qf_fnum;
3399 }
3400 }
3401 else
3402 eidx++;
3403 }
3404 }
3405
3406 return eidx ? eidx : 1;
3407}
3408
3409/*
3410 * Get the 'n'th valid error entry in the quickfix or location list.
3411 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3412 * For :cdo and :ldo returns the 'n'th valid error entry.
3413 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3414 */
3415 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003416qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003417{
3418 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3419 qfline_T *qfp = qfl->qf_start;
3420 int i, eidx;
3421 int prev_fnum = 0;
3422
3423 /* check if the list has valid errors */
3424 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3425 return 1;
3426
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003427 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003428 i++, qfp = qfp->qf_next)
3429 {
3430 if (qfp->qf_valid)
3431 {
3432 if (fdo)
3433 {
3434 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3435 {
3436 /* Count the number of files */
3437 eidx++;
3438 prev_fnum = qfp->qf_fnum;
3439 }
3440 }
3441 else
3442 eidx++;
3443 }
3444
3445 if (eidx == n)
3446 break;
3447 }
3448
3449 if (i <= qfl->qf_count)
3450 return i;
3451 else
3452 return 1;
3453}
3454
3455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003457 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003458 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 */
3460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003461ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003463 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003464 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003465
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003466 if (eap->cmdidx == CMD_ll
3467 || eap->cmdidx == CMD_lrewind
3468 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003469 || eap->cmdidx == CMD_llast
3470 || eap->cmdidx == CMD_ldo
3471 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003472 {
3473 qi = GET_LOC_LIST(curwin);
3474 if (qi == NULL)
3475 {
3476 EMSG(_(e_loclist));
3477 return;
3478 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479 }
3480
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003481 if (eap->addr_count > 0)
3482 errornr = (int)eap->line2;
3483 else
3484 {
3485 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3486 errornr = 0;
3487 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3488 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3489 errornr = 1;
3490 else
3491 errornr = 32767;
3492 }
3493
3494 /* For cdo and ldo commands, jump to the nth valid error.
3495 * For cfdo and lfdo commands, jump to the nth valid file entry.
3496 */
3497 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3498 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3499 errornr = qf_get_nth_valid_entry(qi,
3500 eap->addr_count > 0 ? (int)eap->line1 : 1,
3501 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3502
3503 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504}
3505
3506/*
3507 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003508 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003509 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 */
3511 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003512ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003514 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003515 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003516
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003517 if (eap->cmdidx == CMD_lnext
3518 || eap->cmdidx == CMD_lNext
3519 || eap->cmdidx == CMD_lprevious
3520 || eap->cmdidx == CMD_lnfile
3521 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003522 || eap->cmdidx == CMD_lpfile
3523 || eap->cmdidx == CMD_ldo
3524 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003525 {
3526 qi = GET_LOC_LIST(curwin);
3527 if (qi == NULL)
3528 {
3529 EMSG(_(e_loclist));
3530 return;
3531 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003532 }
3533
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003534 if (eap->addr_count > 0 &&
3535 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3536 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3537 errornr = (int)eap->line2;
3538 else
3539 errornr = 1;
3540
3541 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3542 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003544 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3545 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003547 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3548 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 ? BACKWARD_FILE
3550 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003551 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552}
3553
3554/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003555 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003556 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 */
3558 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003559ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003561 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003562 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003563#ifdef FEAT_AUTOCMD
3564 char_u *au_name = NULL;
3565#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003566
3567 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003568 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003569 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003570
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003571#ifdef FEAT_AUTOCMD
3572 switch (eap->cmdidx)
3573 {
3574 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3575 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3576 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3577 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3578 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3579 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3580 default: break;
3581 }
3582 if (au_name != NULL)
3583 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3584#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003585#ifdef FEAT_BROWSE
3586 if (cmdmod.browse)
3587 {
3588 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3589 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3590 if (browse_file == NULL)
3591 return;
3592 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3593 vim_free(browse_file);
3594 }
3595 else
3596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003598 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003599
3600 /*
3601 * This function is used by the :cfile, :cgetfile and :caddfile
3602 * commands.
3603 * :cfile always creates a new quickfix list and jumps to the
3604 * first error.
3605 * :cgetfile creates a new quickfix list but doesn't jump to the
3606 * first error.
3607 * :caddfile adds to an existing quickfix list. If there is no
3608 * quickfix list then a new list is created.
3609 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003610 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003611 && eap->cmdidx != CMD_laddfile),
3612 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003613 && (eap->cmdidx == CMD_cfile
3614 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003615 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003616#ifdef FEAT_AUTOCMD
3617 if (au_name != NULL)
3618 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3619#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003620 if (wp != NULL)
3621 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003622 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003623 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003624
3625 else
3626 {
3627#ifdef FEAT_AUTOCMD
3628 if (au_name != NULL)
3629 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3630#endif
3631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632}
3633
3634/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003635 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003636 * ":vimgrepadd {pattern} file(s)"
3637 * ":lvimgrep {pattern} file(s)"
3638 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003639 */
3640 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003641ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003642{
Bram Moolenaar81695252004-12-29 20:58:21 +00003643 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003644 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003645 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003646 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003647 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003648 char_u *s;
3649 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003650 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003651 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003652#ifdef FEAT_AUTOCMD
3653 qfline_T *cur_qf_start;
3654#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003655 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003656 buf_T *buf;
3657 int duplicate_name = FALSE;
3658 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003659 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003660 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003661 buf_T *first_match_buf = NULL;
3662 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003663 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003664#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3665 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003666#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003667 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003668 int flags = 0;
3669 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003670 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003671 char_u *dirname_start = NULL;
3672 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003673 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003674#ifdef FEAT_AUTOCMD
3675 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003676
3677 switch (eap->cmdidx)
3678 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003679 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3680 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3681 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003682 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003683 case CMD_grep: au_name = (char_u *)"grep"; break;
3684 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3685 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3686 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003687 default: break;
3688 }
3689 if (au_name != NULL)
3690 {
3691 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3692 curbuf->b_fname, TRUE, curbuf);
3693 if (did_throw || force_abort)
3694 return;
3695 }
3696#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003697
Bram Moolenaar754b5602006-02-09 23:53:20 +00003698 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003699 || eap->cmdidx == CMD_lvimgrep
3700 || eap->cmdidx == CMD_lgrepadd
3701 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003702 {
3703 qi = ll_get_or_alloc_list(curwin);
3704 if (qi == NULL)
3705 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003706 }
3707
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003708 if (eap->addr_count > 0)
3709 tomatch = eap->line2;
3710 else
3711 tomatch = MAXLNUM;
3712
Bram Moolenaar81695252004-12-29 20:58:21 +00003713 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003714 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003715 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003716 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003717 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003718 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003719 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003720 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003721 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003722
3723 if (s != NULL && *s == NUL)
3724 {
3725 /* Pattern is empty, use last search pattern. */
3726 if (last_search_pat() == NULL)
3727 {
3728 EMSG(_(e_noprevre));
3729 goto theend;
3730 }
3731 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3732 }
3733 else
3734 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3735
Bram Moolenaar86b68352004-12-27 21:59:20 +00003736 if (regmatch.regprog == NULL)
3737 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003738 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003739 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003740
3741 p = skipwhite(p);
3742 if (*p == NUL)
3743 {
3744 EMSG(_("E683: File name missing or invalid pattern"));
3745 goto theend;
3746 }
3747
Bram Moolenaar754b5602006-02-09 23:53:20 +00003748 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003749 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003750 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003751 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003752 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003753
3754 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003755 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003756 goto theend;
3757 if (fcount == 0)
3758 {
3759 EMSG(_(e_nomatch));
3760 goto theend;
3761 }
3762
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003763 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3764 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003765 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003766 {
3767 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003768 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003769 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003770
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003771 /* Remember the current directory, because a BufRead autocommand that does
3772 * ":lcd %:p:h" changes the meaning of short path names. */
3773 mch_dirname(dirname_start, MAXPATHL);
3774
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003775#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003776 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003777 * changing the current quickfix list. */
3778 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3779#endif
3780
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003781 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003782 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003783 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003784 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003785 if (time(NULL) > seconds)
3786 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003787 /* Display the file name every second or so, show the user we are
3788 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003789 seconds = time(NULL);
3790 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003791 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003792 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003793 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003794 else
3795 {
3796 msg_outtrans(p);
3797 vim_free(p);
3798 }
3799 msg_clr_eos();
3800 msg_didout = FALSE; /* overwrite this message */
3801 msg_nowait = TRUE; /* don't wait for this message */
3802 msg_col = 0;
3803 out_flush();
3804 }
3805
Bram Moolenaar81695252004-12-29 20:58:21 +00003806 buf = buflist_findname_exp(fnames[fi]);
3807 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3808 {
3809 /* Remember that a buffer with this name already exists. */
3810 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003811 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003812 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003813
3814#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3815 /* Don't do Filetype autocommands to avoid loading syntax and
3816 * indent scripts, a great speed improvement. */
3817 save_ei = au_event_disable(",Filetype");
3818#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003819 /* Don't use modelines here, it's useless. */
3820 save_mls = p_mls;
3821 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003822
3823 /* Load file into a buffer, so that 'fileencoding' is detected,
3824 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003825 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003826
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003827 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003828#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3829 au_event_restore(save_ei);
3830#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003831 }
3832 else
3833 /* Use existing, loaded buffer. */
3834 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003835
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003836#ifdef FEAT_AUTOCMD
3837 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3838 {
3839 int idx;
3840
3841 /* Autocommands changed the quickfix list. Find the one we were
3842 * using and restore it. */
3843 for (idx = 0; idx < LISTCOUNT; ++idx)
3844 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3845 {
3846 qi->qf_curlist = idx;
3847 break;
3848 }
3849 if (idx == LISTCOUNT)
3850 {
3851 /* List cannot be found, create a new one. */
3852 qf_new_list(qi, *eap->cmdlinep);
3853 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3854 }
3855 }
3856#endif
3857
Bram Moolenaar81695252004-12-29 20:58:21 +00003858 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003859 {
3860 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003861 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003862 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003863 else
3864 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003865 /* Try for a match in all lines of the buffer.
3866 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003867 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003868 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3869 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003870 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003871 col = 0;
3872 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003873 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003874 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003875 ;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003876 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003877 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003878 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003879 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003880 ml_get_buf(buf,
3881 regmatch.startpos[0].lnum + lnum, FALSE),
3882 regmatch.startpos[0].lnum + lnum,
3883 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003884 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003885 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003886 0, /* nr */
3887 0, /* type */
3888 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003889 ) == FAIL)
3890 {
3891 got_int = TRUE;
3892 break;
3893 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003894 found_match = TRUE;
3895 if (--tomatch == 0)
3896 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003897 if ((flags & VGR_GLOBAL) == 0
3898 || regmatch.endpos[0].lnum > 0)
3899 break;
3900 col = regmatch.endpos[0].col
3901 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003902 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003903 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003904 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003905 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003906 if (got_int)
3907 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003908 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003909#ifdef FEAT_AUTOCMD
3910 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3911#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003912
3913 if (using_dummy)
3914 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003915 if (found_match && first_match_buf == NULL)
3916 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003917 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003918 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003919 /* Never keep a dummy buffer if there is another buffer
3920 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003921 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003922 buf = NULL;
3923 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003924 else if (!cmdmod.hide
3925 || buf->b_p_bh[0] == 'u' /* "unload" */
3926 || buf->b_p_bh[0] == 'w' /* "wipe" */
3927 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003928 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003929 /* When no match was found we don't need to remember the
3930 * buffer, wipe it out. If there was a match and it
3931 * wasn't the first one or we won't jump there: only
3932 * unload the buffer.
3933 * Ignore 'hidden' here, because it may lead to having too
3934 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003935 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003936 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003937 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003938 buf = NULL;
3939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003940 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003941 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003942 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003943 buf = NULL;
3944 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003945 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003946
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003947 if (buf != NULL)
3948 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003949 /* If the buffer is still loaded we need to use the
3950 * directory we jumped to below. */
3951 if (buf == first_match_buf
3952 && target_dir == NULL
3953 && STRCMP(dirname_start, dirname_now) != 0)
3954 target_dir = vim_strsave(dirname_now);
3955
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003956 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003957 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003958 * need to be done (again). But not the window-local
3959 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003960 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003961#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003962 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3963 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003964#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003965 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003966 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003967 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003968 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003969 }
3970 }
3971
3972 FreeWild(fcount, fnames);
3973
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003974 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3975 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3976 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003977
3978#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02003979 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003980#endif
3981
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003982#ifdef FEAT_AUTOCMD
3983 if (au_name != NULL)
3984 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3985 curbuf->b_fname, TRUE, curbuf);
3986#endif
3987
Bram Moolenaar86b68352004-12-27 21:59:20 +00003988 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003989 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003990 {
3991 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003992 {
3993 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003994 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003995 if (buf != curbuf)
3996 /* If we jumped to another buffer redrawing will already be
3997 * taken care of. */
3998 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003999
4000 /* Jump to the directory used after loading the buffer. */
4001 if (curbuf == first_match_buf && target_dir != NULL)
4002 {
4003 exarg_T ea;
4004
4005 ea.arg = target_dir;
4006 ea.cmdidx = CMD_lcd;
4007 ex_cd(&ea);
4008 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004009 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004010 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004011 else
4012 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004013
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004014 /* If we loaded a dummy buffer into the current window, the autocommands
4015 * may have messed up things, need to redraw and recompute folds. */
4016 if (redraw_for_dummy)
4017 {
4018#ifdef FEAT_FOLDING
4019 foldUpdateAll(curwin);
4020#else
4021 redraw_later(NOT_VALID);
4022#endif
4023 }
4024
Bram Moolenaar86b68352004-12-27 21:59:20 +00004025theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004026 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004027 vim_free(dirname_now);
4028 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004029 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004030 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004031}
4032
4033/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004034 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00004035 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004036 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
4037 * If "s" is not NULL terminate the pattern with a NUL.
4038 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00004039 */
4040 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004041skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004042{
4043 int c;
4044
4045 if (vim_isIDc(*p))
4046 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004047 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004048 if (s != NULL)
4049 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004050 p = skiptowhite(p);
4051 if (s != NULL && *p != NUL)
4052 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004053 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004054 else
4055 {
4056 /* ":vimgrep /pattern/[g][j] fname" */
4057 if (s != NULL)
4058 *s = p + 1;
4059 c = *p;
4060 p = skip_regexp(p + 1, c, TRUE, NULL);
4061 if (*p != c)
4062 return NULL;
4063
4064 /* Truncate the pattern. */
4065 if (s != NULL)
4066 *p = NUL;
4067 ++p;
4068
4069 /* Find the flags */
4070 while (*p == 'g' || *p == 'j')
4071 {
4072 if (flags != NULL)
4073 {
4074 if (*p == 'g')
4075 *flags |= VGR_GLOBAL;
4076 else
4077 *flags |= VGR_NOJUMP;
4078 }
4079 ++p;
4080 }
4081 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004082 return p;
4083}
4084
4085/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004086 * Restore current working directory to "dirname_start" if they differ, taking
4087 * into account whether it is set locally or globally.
4088 */
4089 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004090restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004091{
4092 char_u *dirname_now = alloc(MAXPATHL);
4093
4094 if (NULL != dirname_now)
4095 {
4096 mch_dirname(dirname_now, MAXPATHL);
4097 if (STRCMP(dirname_start, dirname_now) != 0)
4098 {
4099 /* If the directory has changed, change it back by building up an
4100 * appropriate ex command and executing it. */
4101 exarg_T ea;
4102
4103 ea.arg = dirname_start;
4104 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4105 ex_cd(&ea);
4106 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004107 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004108 }
4109}
4110
4111/*
4112 * Load file "fname" into a dummy buffer and return the buffer pointer,
4113 * placing the directory resulting from the buffer load into the
4114 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4115 * prior to calling this function. Restores directory to "dirname_start" prior
4116 * to returning, if autocmds or the 'autochdir' option have changed it.
4117 *
4118 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4119 * or wipe_dummy_buffer() later!
4120 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004121 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004122 */
4123 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004124load_dummy_buffer(
4125 char_u *fname,
4126 char_u *dirname_start, /* in: old directory */
4127 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004128{
4129 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004130 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004131 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004132 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004133
4134 /* Allocate a buffer without putting it in the buffer list. */
4135 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4136 if (newbuf == NULL)
4137 return NULL;
4138
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004139 /* Init the options. */
4140 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4141
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004142 /* need to open the memfile before putting the buffer in a window */
4143 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004144 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004145 /* set curwin/curbuf to buf and save a few things */
4146 aucmd_prepbuf(&aco, newbuf);
4147
4148 /* Need to set the filename for autocommands. */
4149 (void)setfname(curbuf, fname, NULL, FALSE);
4150
Bram Moolenaar81695252004-12-29 20:58:21 +00004151 /* Create swap file now to avoid the ATTENTION message. */
4152 check_need_swap(TRUE);
4153
4154 /* Remove the "dummy" flag, otherwise autocommands may not
4155 * work. */
4156 curbuf->b_flags &= ~BF_DUMMY;
4157
4158 if (readfile(fname, NULL,
4159 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4160 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004161 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004162 && !(curbuf->b_flags & BF_NEW))
4163 {
4164 failed = FALSE;
4165 if (curbuf != newbuf)
4166 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004167 /* Bloody autocommands changed the buffer! Can happen when
4168 * using netrw and editing a remote file. Use the current
4169 * buffer instead, delete the dummy one after restoring the
4170 * window stuff. */
4171 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004172 newbuf = curbuf;
4173 }
4174 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004175
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004176 /* restore curwin/curbuf and a few other things */
4177 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004178 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
4179 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004180 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004181
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004182 /*
4183 * When autocommands/'autochdir' option changed directory: go back.
4184 * Let the caller know what the resulting dir was first, in case it is
4185 * important.
4186 */
4187 mch_dirname(resulting_dir, MAXPATHL);
4188 restore_start_dir(dirname_start);
4189
Bram Moolenaar81695252004-12-29 20:58:21 +00004190 if (!buf_valid(newbuf))
4191 return NULL;
4192 if (failed)
4193 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004194 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004195 return NULL;
4196 }
4197 return newbuf;
4198}
4199
4200/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004201 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4202 * directory to "dirname_start" prior to returning, if autocmds or the
4203 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004204 */
4205 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004206wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004207{
4208 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004209 {
4210#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4211 cleanup_T cs;
4212
4213 /* Reset the error/interrupt/exception state here so that aborting()
4214 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4215 * work when got_int is set. */
4216 enter_cleanup(&cs);
4217#endif
4218
Bram Moolenaar81695252004-12-29 20:58:21 +00004219 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004220
4221#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4222 /* Restore the error/interrupt/exception state if not discarded by a
4223 * new aborting error, interrupt, or uncaught exception. */
4224 leave_cleanup(&cs);
4225#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004226 /* When autocommands/'autochdir' option changed directory: go back. */
4227 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004228 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004229}
4230
4231/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004232 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4233 * directory to "dirname_start" prior to returning, if autocmds or the
4234 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004235 */
4236 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004237unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004238{
4239 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004240 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004241 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004242
4243 /* When autocommands/'autochdir' option changed directory: go back. */
4244 restore_start_dir(dirname_start);
4245 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004246}
4247
Bram Moolenaar05159a02005-02-26 23:04:13 +00004248#if defined(FEAT_EVAL) || defined(PROTO)
4249/*
4250 * Add each quickfix error to list "list" as a dictionary.
4251 */
4252 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004253get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004254{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004255 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004256 dict_T *dict;
4257 char_u buf[2];
4258 qfline_T *qfp;
4259 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004260 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004261
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004262 if (wp != NULL)
4263 {
4264 qi = GET_LOC_LIST(wp);
4265 if (qi == NULL)
4266 return FAIL;
4267 }
4268
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004269 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004270 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004271 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004272
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004273 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4274 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004275 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004276 /* Handle entries with a non-existing buffer number. */
4277 bufnum = qfp->qf_fnum;
4278 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4279 bufnum = 0;
4280
Bram Moolenaar05159a02005-02-26 23:04:13 +00004281 if ((dict = dict_alloc()) == NULL)
4282 return FAIL;
4283 if (list_append_dict(list, dict) == FAIL)
4284 return FAIL;
4285
4286 buf[0] = qfp->qf_type;
4287 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004288 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004289 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4290 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4291 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4292 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004293 || dict_add_nr_str(dict, "pattern", 0L,
4294 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4295 || dict_add_nr_str(dict, "text", 0L,
4296 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004297 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4298 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4299 return FAIL;
4300
4301 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004302 if (qfp == NULL)
4303 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004304 }
4305 return OK;
4306}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004307
4308/*
4309 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004310 * of dictionaries. "title" will be copied to w:quickfix_title.
4311 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004312 */
4313 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004314set_errorlist(
4315 win_T *wp,
4316 list_T *list,
4317 int action,
4318 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004319{
4320 listitem_T *li;
4321 dict_T *d;
4322 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004323 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004324 long lnum;
4325 int col, nr;
4326 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004327#ifdef FEAT_WINDOWS
4328 qfline_T *old_last = NULL;
4329#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004330 int valid, status;
4331 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004332 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004333 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004334
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004335 if (wp != NULL)
4336 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004337 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004338 if (qi == NULL)
4339 return FAIL;
4340 }
4341
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004342 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004343 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004344 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004345#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004346 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4347 /* Adding to existing list, use last entry. */
4348 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004349#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004350 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004351 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004352 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004353 qf_store_title(qi, title);
4354 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004355
4356 for (li = list->lv_first; li != NULL; li = li->li_next)
4357 {
4358 if (li->li_tv.v_type != VAR_DICT)
4359 continue; /* Skip non-dict items */
4360
4361 d = li->li_tv.vval.v_dict;
4362 if (d == NULL)
4363 continue;
4364
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004365 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004366 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004367 lnum = get_dict_number(d, (char_u *)"lnum");
4368 col = get_dict_number(d, (char_u *)"col");
4369 vcol = get_dict_number(d, (char_u *)"vcol");
4370 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004371 type = get_dict_string(d, (char_u *)"type", TRUE);
4372 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4373 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004374 if (text == NULL)
4375 text = vim_strsave((char_u *)"");
4376
4377 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004378 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004379 valid = FALSE;
4380
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004381 /* Mark entries with non-existing buffer number as not valid. Give the
4382 * error message only once. */
4383 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4384 {
4385 if (!did_bufnr_emsg)
4386 {
4387 did_bufnr_emsg = TRUE;
4388 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4389 }
4390 valid = FALSE;
4391 bufnum = 0;
4392 }
4393
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004394 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004395 NULL, /* dir */
4396 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004397 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004398 text,
4399 lnum,
4400 col,
4401 vcol, /* vis_col */
4402 pattern, /* search pattern */
4403 nr,
4404 type == NULL ? NUL : *type,
4405 valid);
4406
4407 vim_free(filename);
4408 vim_free(pattern);
4409 vim_free(text);
4410 vim_free(type);
4411
4412 if (status == FAIL)
4413 {
4414 retval = FAIL;
4415 break;
4416 }
4417 }
4418
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004419 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004420 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004421 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4422 else
4423 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004424 if (action != 'a') {
4425 qi->qf_lists[qi->qf_curlist].qf_ptr =
4426 qi->qf_lists[qi->qf_curlist].qf_start;
4427 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4428 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4429 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004430
4431#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004432 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004433 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004434#endif
4435
4436 return retval;
4437}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004438#endif
4439
Bram Moolenaar81695252004-12-29 20:58:21 +00004440/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004441 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004442 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004443 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004444 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004445 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004446 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004447 */
4448 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004449ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004450{
4451 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004452 qf_info_T *qi = &ql_info;
4453
Bram Moolenaardb552d602006-03-23 22:59:57 +00004454 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4455 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004456 {
4457 qi = ll_get_or_alloc_list(curwin);
4458 if (qi == NULL)
4459 return;
4460 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004461
4462 if (*eap->arg == NUL)
4463 buf = curbuf;
4464 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4465 buf = buflist_findnr(atoi((char *)eap->arg));
4466 if (buf == NULL)
4467 EMSG(_(e_invarg));
4468 else if (buf->b_ml.ml_mfp == NULL)
4469 EMSG(_("E681: Buffer is not loaded"));
4470 else
4471 {
4472 if (eap->addr_count == 0)
4473 {
4474 eap->line1 = 1;
4475 eap->line2 = buf->b_ml.ml_line_count;
4476 }
4477 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4478 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4479 EMSG(_(e_invrange));
4480 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004481 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004482 char_u *qf_title = *eap->cmdlinep;
4483
4484 if (buf->b_sfname)
4485 {
4486 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4487 (char *)qf_title, (char *)buf->b_sfname);
4488 qf_title = IObuff;
4489 }
4490
Bram Moolenaardb552d602006-03-23 22:59:57 +00004491 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4492 (eap->cmdidx != CMD_caddbuffer
4493 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004494 eap->line1, eap->line2,
4495 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004496 && (eap->cmdidx == CMD_cbuffer
4497 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004498 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4499 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004500 }
4501}
4502
Bram Moolenaar1e015462005-09-25 22:16:38 +00004503#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004504/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004505 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4506 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004507 */
4508 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004509ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004510{
4511 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004512 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004513
Bram Moolenaardb552d602006-03-23 22:59:57 +00004514 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4515 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004516 {
4517 qi = ll_get_or_alloc_list(curwin);
4518 if (qi == NULL)
4519 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004520 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004521
Bram Moolenaar4770d092006-01-12 23:22:24 +00004522 /* Evaluate the expression. When the result is a string or a list we can
4523 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004524 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004525 if (tv != NULL)
4526 {
4527 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4528 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4529 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004530 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004531 (eap->cmdidx != CMD_caddexpr
4532 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004533 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004534 && (eap->cmdidx == CMD_cexpr
4535 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004536 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004537 }
4538 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004539 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004540 free_tv(tv);
4541 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004542}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004543#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004544
4545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 * ":helpgrep {pattern}"
4547 */
4548 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004549ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550{
4551 regmatch_T regmatch;
4552 char_u *save_cpo;
4553 char_u *p;
4554 int fcount;
4555 char_u **fnames;
4556 FILE *fd;
4557 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004559#ifdef FEAT_MULTI_LANG
4560 char_u *lang;
4561#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004562 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004563 int new_qi = FALSE;
4564 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004565#ifdef FEAT_AUTOCMD
4566 char_u *au_name = NULL;
4567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004569#ifdef FEAT_MULTI_LANG
4570 /* Check for a specified language */
4571 lang = check_help_lang(eap->arg);
4572#endif
4573
Bram Moolenaar73633f82012-01-20 13:39:07 +01004574#ifdef FEAT_AUTOCMD
4575 switch (eap->cmdidx)
4576 {
4577 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4578 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4579 default: break;
4580 }
4581 if (au_name != NULL)
4582 {
4583 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4584 curbuf->b_fname, TRUE, curbuf);
4585 if (did_throw || force_abort)
4586 return;
4587 }
4588#endif
4589
4590 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4591 save_cpo = p_cpo;
4592 p_cpo = empty_option;
4593
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004594 if (eap->cmdidx == CMD_lhelpgrep)
4595 {
4596 /* Find an existing help window */
4597 FOR_ALL_WINDOWS(wp)
4598 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4599 break;
4600
4601 if (wp == NULL) /* Help window not found */
4602 qi = NULL;
4603 else
4604 qi = wp->w_llist;
4605
4606 if (qi == NULL)
4607 {
4608 /* Allocate a new location list for help text matches */
4609 if ((qi = ll_new_list()) == NULL)
4610 return;
4611 new_qi = TRUE;
4612 }
4613 }
4614
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4616 regmatch.rm_ic = FALSE;
4617 if (regmatch.regprog != NULL)
4618 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004619#ifdef FEAT_MBYTE
4620 vimconv_T vc;
4621
4622 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4623 * differs. */
4624 vc.vc_type = CONV_NONE;
4625 if (!enc_utf8)
4626 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4627#endif
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004630 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
4632 /* Go through all directories in 'runtimepath' */
4633 p = p_rtp;
4634 while (*p != NUL && !got_int)
4635 {
4636 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4637
4638 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4639 add_pathsep(NameBuff);
4640 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4641 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4642 &fnames, EW_FILE|EW_SILENT) == OK
4643 && fcount > 0)
4644 {
4645 for (fi = 0; fi < fcount && !got_int; ++fi)
4646 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004647#ifdef FEAT_MULTI_LANG
4648 /* Skip files for a different language. */
4649 if (lang != NULL
4650 && STRNICMP(lang, fnames[fi]
4651 + STRLEN(fnames[fi]) - 3, 2) != 0
4652 && !(STRNICMP(lang, "en", 2) == 0
4653 && STRNICMP("txt", fnames[fi]
4654 + STRLEN(fnames[fi]) - 3, 3) == 0))
4655 continue;
4656#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004657 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (fd != NULL)
4659 {
4660 lnum = 1;
4661 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4662 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004663 char_u *line = IObuff;
4664#ifdef FEAT_MBYTE
4665 /* Convert a line if 'encoding' is not utf-8 and
4666 * the line contains a non-ASCII character. */
4667 if (vc.vc_type != CONV_NONE
4668 && has_non_ascii(IObuff)) {
4669 line = string_convert(&vc, IObuff, NULL);
4670 if (line == NULL)
4671 line = IObuff;
4672 }
4673#endif
4674
4675 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004677 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
4679 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004680 while (l > 0 && line[l - 1] <= ' ')
4681 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004683 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 NULL, /* dir */
4685 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004686 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004687 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004689 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004690 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004691 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004692 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 0, /* nr */
4694 1, /* type */
4695 TRUE /* valid */
4696 ) == FAIL)
4697 {
4698 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004699#ifdef FEAT_MBYTE
4700 if (line != IObuff)
4701 vim_free(line);
4702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 break;
4704 }
4705 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004706#ifdef FEAT_MBYTE
4707 if (line != IObuff)
4708 vim_free(line);
4709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 ++lnum;
4711 line_breakcheck();
4712 }
4713 fclose(fd);
4714 }
4715 }
4716 FreeWild(fcount, fnames);
4717 }
4718 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004719
Bram Moolenaar473de612013-06-08 18:19:48 +02004720 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004721#ifdef FEAT_MBYTE
4722 if (vc.vc_type != CONV_NONE)
4723 convert_setup(&vc, NULL, NULL);
4724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004726 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4727 qi->qf_lists[qi->qf_curlist].qf_ptr =
4728 qi->qf_lists[qi->qf_curlist].qf_start;
4729 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004732 if (p_cpo == empty_option)
4733 p_cpo = save_cpo;
4734 else
4735 /* Darn, some plugin changed the value. */
4736 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004739 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740#endif
4741
Bram Moolenaar73633f82012-01-20 13:39:07 +01004742#ifdef FEAT_AUTOCMD
4743 if (au_name != NULL)
4744 {
4745 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4746 curbuf->b_fname, TRUE, curbuf);
4747 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4748 /* autocommands made "qi" invalid */
4749 return;
4750 }
4751#endif
4752
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004754 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004755 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004756 else
4757 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004758
4759 if (eap->cmdidx == CMD_lhelpgrep)
4760 {
4761 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004762 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004763 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4764 {
4765 if (new_qi)
4766 ll_free_all(&qi);
4767 }
4768 else if (curwin->w_llist == NULL)
4769 curwin->w_llist = qi;
4770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771}
4772
4773#endif /* FEAT_QUICKFIX */