blob: 366e4bd9223bf01ebcab4a36bdecc856f2d95352 [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 Moolenaarbcf77722016-06-28 21:11:32 +02001104 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 }
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 Moolenaarbcf77722016-06-28 21:11:32 +02001112qf_init_end:
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);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 vim_free(namebuf);
1119 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001120 vim_free(pattern);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001121 vim_free(growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122
1123#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001124 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#endif
1126
1127 return retval;
1128}
1129
Bram Moolenaarfb604092014-07-23 15:55:00 +02001130 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001131qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001132{
1133 if (title != NULL)
1134 {
1135 char_u *p = alloc((int)STRLEN(title) + 2);
1136
1137 qi->qf_lists[qi->qf_curlist].qf_title = p;
1138 if (p != NULL)
1139 sprintf((char *)p, ":%s", (char *)title);
1140 }
1141}
1142
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143/*
1144 * Prepare for adding a new quickfix list.
1145 */
1146 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001147qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148{
1149 int i;
1150
1151 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001152 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * the current entry. This makes it possible to browse in a tree-like
1154 * way with ":grep'.
1155 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001156 while (qi->qf_listcount > qi->qf_curlist + 1)
1157 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
1159 /*
1160 * When the stack is full, remove to oldest entry
1161 * Otherwise, add a new entry.
1162 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001163 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001165 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001167 qi->qf_lists[i - 1] = qi->qf_lists[i];
1168 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 }
1170 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001171 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001172 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001173 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174}
1175
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001176/*
1177 * Free a location list
1178 */
1179 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001180ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001181{
1182 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001183 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001184
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001185 qi = *pqi;
1186 if (qi == NULL)
1187 return;
1188 *pqi = NULL; /* Remove reference to this list */
1189
1190 qi->qf_refcount--;
1191 if (qi->qf_refcount < 1)
1192 {
1193 /* No references to this location list */
1194 for (i = 0; i < qi->qf_listcount; ++i)
1195 qf_free(qi, i);
1196 vim_free(qi);
1197 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001198}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001199
1200 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001201qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001202{
1203 int i;
1204 qf_info_T *qi = &ql_info;
1205
1206 if (wp != NULL)
1207 {
1208 /* location list */
1209 ll_free_all(&wp->w_llist);
1210 ll_free_all(&wp->w_llist_ref);
1211 }
1212 else
1213 /* quickfix list */
1214 for (i = 0; i < qi->qf_listcount; ++i)
1215 qf_free(qi, i);
1216}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218/*
1219 * Add an entry to the end of the list of errors.
1220 * Returns OK or FAIL.
1221 */
1222 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001223qf_add_entry(
1224 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001225 char_u *dir, /* optional directory name */
1226 char_u *fname, /* file name or NULL */
1227 int bufnum, /* buffer number or zero */
1228 char_u *mesg, /* message */
1229 long lnum, /* line number */
1230 int col, /* column */
1231 int vis_col, /* using visual column */
1232 char_u *pattern, /* search pattern */
1233 int nr, /* error number */
1234 int type, /* type character */
1235 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001237 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001238 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001240 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001242 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001243 {
1244 buf_T *buf = buflist_findnr(bufnum);
1245
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001246 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001247 if (buf != NULL)
1248 buf->b_has_qf_entry = TRUE;
1249 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001250 else
1251 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1253 {
1254 vim_free(qfp);
1255 return FAIL;
1256 }
1257 qfp->qf_lnum = lnum;
1258 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001259 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001260 if (pattern == NULL || *pattern == NUL)
1261 qfp->qf_pattern = NULL;
1262 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1263 {
1264 vim_free(qfp->qf_text);
1265 vim_free(qfp);
1266 return FAIL;
1267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 qfp->qf_nr = nr;
1269 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1270 type = 0;
1271 qfp->qf_type = type;
1272 qfp->qf_valid = valid;
1273
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001274 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001275 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1276 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001278 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001279 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1280 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001281 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 else
1284 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001285 qfp->qf_prev = *lastp;
1286 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001288 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001290 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001291 ++qi->qf_lists[qi->qf_curlist].qf_count;
1292 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1293 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001295 qi->qf_lists[qi->qf_curlist].qf_index =
1296 qi->qf_lists[qi->qf_curlist].qf_count;
1297 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299
1300 return OK;
1301}
1302
1303/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001304 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001305 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001306 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001307ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001308{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001309 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001310
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001311 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1312 if (qi != NULL)
1313 {
1314 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1315 qi->qf_refcount++;
1316 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001317
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001318 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001319}
1320
1321/*
1322 * Return the location list for window 'wp'.
1323 * If not present, allocate a location list
1324 */
1325 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001326ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001327{
1328 if (IS_LL_WINDOW(wp))
1329 /* For a location list window, use the referenced location list */
1330 return wp->w_llist_ref;
1331
1332 /*
1333 * For a non-location list window, w_llist_ref should not point to a
1334 * location list.
1335 */
1336 ll_free_all(&wp->w_llist_ref);
1337
1338 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001339 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001340 return wp->w_llist;
1341}
1342
1343/*
1344 * Copy the location list from window "from" to window "to".
1345 */
1346 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001347copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001348{
1349 qf_info_T *qi;
1350 int idx;
1351 int i;
1352
1353 /*
1354 * When copying from a location list window, copy the referenced
1355 * location list. For other windows, copy the location list for
1356 * that window.
1357 */
1358 if (IS_LL_WINDOW(from))
1359 qi = from->w_llist_ref;
1360 else
1361 qi = from->w_llist;
1362
1363 if (qi == NULL) /* no location list to copy */
1364 return;
1365
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001366 /* allocate a new location list */
1367 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001368 return;
1369
1370 to->w_llist->qf_listcount = qi->qf_listcount;
1371
1372 /* Copy the location lists one at a time */
1373 for (idx = 0; idx < qi->qf_listcount; idx++)
1374 {
1375 qf_list_T *from_qfl;
1376 qf_list_T *to_qfl;
1377
1378 to->w_llist->qf_curlist = idx;
1379
1380 from_qfl = &qi->qf_lists[idx];
1381 to_qfl = &to->w_llist->qf_lists[idx];
1382
1383 /* Some of the fields are populated by qf_add_entry() */
1384 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1385 to_qfl->qf_count = 0;
1386 to_qfl->qf_index = 0;
1387 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001388 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001389 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001390 if (from_qfl->qf_title != NULL)
1391 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1392 else
1393 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001394
1395 if (from_qfl->qf_count)
1396 {
1397 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001398 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001399
1400 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001401 for (i = 0, from_qfp = from_qfl->qf_start;
1402 i < from_qfl->qf_count && from_qfp != NULL;
1403 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001404 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001405 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001406 NULL,
1407 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001408 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001409 from_qfp->qf_text,
1410 from_qfp->qf_lnum,
1411 from_qfp->qf_col,
1412 from_qfp->qf_viscol,
1413 from_qfp->qf_pattern,
1414 from_qfp->qf_nr,
1415 0,
1416 from_qfp->qf_valid) == FAIL)
1417 {
1418 qf_free_all(to);
1419 return;
1420 }
1421 /*
1422 * qf_add_entry() will not set the qf_num field, as the
1423 * directory and file names are not supplied. So the qf_fnum
1424 * field is copied here.
1425 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001426 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001427 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1428 prevp->qf_type = from_qfp->qf_type; /* error type */
1429 if (from_qfl->qf_ptr == from_qfp)
1430 to_qfl->qf_ptr = prevp; /* current location */
1431 }
1432 }
1433
1434 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1435
1436 /* When no valid entries are present in the list, qf_ptr points to
1437 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001438 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001439 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001440 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001441 to_qfl->qf_index = 1;
1442 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001443 }
1444
1445 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1446}
1447
1448/*
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001449 * Get buffer number for file "dir.name".
1450 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 */
1452 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001453qf_get_fnum(char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001455 char_u *ptr;
1456 buf_T *buf;
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if (fname == NULL || *fname == NUL) /* no file name */
1459 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
Bram Moolenaare60acc12011-05-10 16:41:25 +02001461#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001462 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001463#endif
1464#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001465 if (directory != NULL)
1466 slash_adjust(directory);
1467 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001468#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001469 if (directory != NULL && !vim_isAbsName(fname)
1470 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1471 {
1472 /*
1473 * Here we check if the file really exists.
1474 * This should normally be true, but if make works without
1475 * "leaving directory"-messages we might have missed a
1476 * directory change.
1477 */
1478 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 vim_free(ptr);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001481 directory = qf_guess_filepath(fname);
1482 if (directory)
1483 ptr = concat_fnames(directory, fname, TRUE);
1484 else
1485 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001487 /* Use concatenated directory name and file name */
1488 buf = buflist_new(ptr, NULL, (linenr_T)0, 0);
1489 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001491 else
1492 buf = buflist_new(fname, NULL, (linenr_T)0, 0);
1493 if (buf == NULL)
1494 return 0;
1495 buf->b_has_qf_entry = TRUE;
1496 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497}
1498
1499/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001500 * Push dirbuf onto the directory stack and return pointer to actual dir or
1501 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 */
1503 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001504qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
1506 struct dir_stack_T *ds_new;
1507 struct dir_stack_T *ds_ptr;
1508
1509 /* allocate new stack element and hook it in */
1510 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1511 if (ds_new == NULL)
1512 return NULL;
1513
1514 ds_new->next = *stackptr;
1515 *stackptr = ds_new;
1516
1517 /* store directory on the stack */
1518 if (vim_isAbsName(dirbuf)
1519 || (*stackptr)->next == NULL
1520 || (*stackptr && dir_stack != *stackptr))
1521 (*stackptr)->dirname = vim_strsave(dirbuf);
1522 else
1523 {
1524 /* Okay we don't have an absolute path.
1525 * dirbuf must be a subdir of one of the directories on the stack.
1526 * Let's search...
1527 */
1528 ds_new = (*stackptr)->next;
1529 (*stackptr)->dirname = NULL;
1530 while (ds_new)
1531 {
1532 vim_free((*stackptr)->dirname);
1533 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1534 TRUE);
1535 if (mch_isdir((*stackptr)->dirname) == TRUE)
1536 break;
1537
1538 ds_new = ds_new->next;
1539 }
1540
1541 /* clean up all dirs we already left */
1542 while ((*stackptr)->next != ds_new)
1543 {
1544 ds_ptr = (*stackptr)->next;
1545 (*stackptr)->next = (*stackptr)->next->next;
1546 vim_free(ds_ptr->dirname);
1547 vim_free(ds_ptr);
1548 }
1549
1550 /* Nothing found -> it must be on top level */
1551 if (ds_new == NULL)
1552 {
1553 vim_free((*stackptr)->dirname);
1554 (*stackptr)->dirname = vim_strsave(dirbuf);
1555 }
1556 }
1557
1558 if ((*stackptr)->dirname != NULL)
1559 return (*stackptr)->dirname;
1560 else
1561 {
1562 ds_ptr = *stackptr;
1563 *stackptr = (*stackptr)->next;
1564 vim_free(ds_ptr);
1565 return NULL;
1566 }
1567}
1568
1569
1570/*
1571 * pop dirbuf from the directory stack and return previous directory or NULL if
1572 * stack is empty
1573 */
1574 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001575qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
1577 struct dir_stack_T *ds_ptr;
1578
1579 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1580 * What to do if it isn't? */
1581
1582 /* pop top element and free it */
1583 if (*stackptr != NULL)
1584 {
1585 ds_ptr = *stackptr;
1586 *stackptr = (*stackptr)->next;
1587 vim_free(ds_ptr->dirname);
1588 vim_free(ds_ptr);
1589 }
1590
1591 /* return NEW top element as current dir or NULL if stack is empty*/
1592 return *stackptr ? (*stackptr)->dirname : NULL;
1593}
1594
1595/*
1596 * clean up directory stack
1597 */
1598 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001599qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
1601 struct dir_stack_T *ds_ptr;
1602
1603 while ((ds_ptr = *stackptr) != NULL)
1604 {
1605 *stackptr = (*stackptr)->next;
1606 vim_free(ds_ptr->dirname);
1607 vim_free(ds_ptr);
1608 }
1609}
1610
1611/*
1612 * Check in which directory of the directory stack the given file can be
1613 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001614 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 * Cleans up intermediate directory entries.
1616 *
1617 * TODO: How to solve the following problem?
1618 * If we have the this directory tree:
1619 * ./
1620 * ./aa
1621 * ./aa/bb
1622 * ./bb
1623 * ./bb/x.c
1624 * and make says:
1625 * making all in aa
1626 * making all in bb
1627 * x.c:9: Error
1628 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1629 * qf_guess_filepath will return NULL.
1630 */
1631 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001632qf_guess_filepath(char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 struct dir_stack_T *ds_ptr;
1635 struct dir_stack_T *ds_tmp;
1636 char_u *fullname;
1637
1638 /* no dirs on the stack - there's nothing we can do */
1639 if (dir_stack == NULL)
1640 return NULL;
1641
1642 ds_ptr = dir_stack->next;
1643 fullname = NULL;
1644 while (ds_ptr)
1645 {
1646 vim_free(fullname);
1647 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1648
1649 /* If concat_fnames failed, just go on. The worst thing that can happen
1650 * is that we delete the entire stack.
1651 */
1652 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1653 break;
1654
1655 ds_ptr = ds_ptr->next;
1656 }
1657
1658 vim_free(fullname);
1659
1660 /* clean up all dirs we already left */
1661 while (dir_stack->next != ds_ptr)
1662 {
1663 ds_tmp = dir_stack->next;
1664 dir_stack->next = dir_stack->next->next;
1665 vim_free(ds_tmp->dirname);
1666 vim_free(ds_tmp);
1667 }
1668
1669 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1670
1671}
1672
1673/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001674 * When loading a file from the quickfix, the auto commands may modify it.
1675 * This may invalidate the current quickfix entry. This function checks
1676 * whether a entry is still present in the quickfix.
1677 * Similar to location list.
1678 */
1679 static int
1680is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1681{
1682 qf_list_T *qfl;
1683 qfline_T *qfp;
1684 int i;
1685
1686 qfl = &qi->qf_lists[qi->qf_curlist];
1687
1688 /* Search for the entry in the current list */
1689 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1690 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001691 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001692 break;
1693
1694 if (i == qfl->qf_count) /* Entry is not found */
1695 return FALSE;
1696
1697 return TRUE;
1698}
1699
1700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 * jump to a quickfix line
1702 * if dir == FORWARD go "errornr" valid entries forward
1703 * if dir == BACKWARD go "errornr" valid entries backward
1704 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1705 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1706 * else if "errornr" is zero, redisplay the same line
1707 * else go to entry "errornr"
1708 */
1709 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001710qf_jump(
1711 qf_info_T *qi,
1712 int dir,
1713 int errornr,
1714 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001716 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001717 qfline_T *qf_ptr;
1718 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 int qf_index;
1720 int old_qf_fnum;
1721 int old_qf_index;
1722 int prev_index;
1723 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1724 char_u *err = e_no_more_items;
1725 linenr_T i;
1726 buf_T *old_curbuf;
1727 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 colnr_T screen_col;
1729 colnr_T char_col;
1730 char_u *line;
1731#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001732 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001733 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 int opened_window = FALSE;
1735 win_T *win;
1736 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001737 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001739 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 int print_message = TRUE;
1741 int len;
1742#ifdef FEAT_FOLDING
1743 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1744#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001745 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001746 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001748 if (qi == NULL)
1749 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001750
1751 if (qi->qf_curlist >= qi->qf_listcount
1752 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 {
1754 EMSG(_(e_quickfix));
1755 return;
1756 }
1757
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001758 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001760 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 old_qf_index = qf_index;
1762 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1763 {
1764 while (errornr--)
1765 {
1766 old_qf_ptr = qf_ptr;
1767 prev_index = qf_index;
1768 old_qf_fnum = qf_ptr->qf_fnum;
1769 do
1770 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001771 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 || qf_ptr->qf_next == NULL)
1773 {
1774 qf_ptr = old_qf_ptr;
1775 qf_index = prev_index;
1776 if (err != NULL)
1777 {
1778 EMSG(_(err));
1779 goto theend;
1780 }
1781 errornr = 0;
1782 break;
1783 }
1784 ++qf_index;
1785 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001786 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1787 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1789 err = NULL;
1790 }
1791 }
1792 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1793 {
1794 while (errornr--)
1795 {
1796 old_qf_ptr = qf_ptr;
1797 prev_index = qf_index;
1798 old_qf_fnum = qf_ptr->qf_fnum;
1799 do
1800 {
1801 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1802 {
1803 qf_ptr = old_qf_ptr;
1804 qf_index = prev_index;
1805 if (err != NULL)
1806 {
1807 EMSG(_(err));
1808 goto theend;
1809 }
1810 errornr = 0;
1811 break;
1812 }
1813 --qf_index;
1814 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001815 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1816 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1818 err = NULL;
1819 }
1820 }
1821 else if (errornr != 0) /* go to specified number */
1822 {
1823 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1824 {
1825 --qf_index;
1826 qf_ptr = qf_ptr->qf_prev;
1827 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001828 while (errornr > qf_index && qf_index <
1829 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 && qf_ptr->qf_next != NULL)
1831 {
1832 ++qf_index;
1833 qf_ptr = qf_ptr->qf_next;
1834 }
1835 }
1836
1837#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001838 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1839 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 /* No need to print the error message if it's visible in the error
1841 * window */
1842 print_message = FALSE;
1843
1844 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001845 * For ":helpgrep" find a help window or open one.
1846 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001847 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001848 {
1849 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001850
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001851 if (cmdmod.tab != 0)
1852 wp = NULL;
1853 else
1854 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1855 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1856 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001857 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1858 win_enter(wp, TRUE);
1859 else
1860 {
1861 /*
1862 * Split off help window; put it at far top if no position
1863 * specified, the current window is vertically split and narrow.
1864 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001865 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001866 if (cmdmod.split == 0 && curwin->w_width != Columns
1867 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001868 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001869 if (qi != &ql_info)
1870 flags |= WSP_NEWLOC; /* don't copy the location list */
1871
1872 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001873 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001874 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001875
1876 if (curwin->w_height < p_hh)
1877 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001878
1879 if (qi != &ql_info) /* not a quickfix list */
1880 {
1881 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001882 curwin->w_llist = qi;
1883 qi->qf_refcount++;
1884 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001885 }
1886
1887 if (!p_im)
1888 restart_edit = 0; /* don't want insert mode in help file */
1889 }
1890
1891 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 * If currently in the quickfix window, find another window to show the
1893 * file in.
1894 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001895 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001897 win_T *usable_win_ptr = NULL;
1898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 /*
1900 * If there is no file specified, we don't know where to go.
1901 * But do advance, otherwise ":cn" gets stuck.
1902 */
1903 if (qf_ptr->qf_fnum == 0)
1904 goto theend;
1905
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001906 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001907
1908 ll_ref = curwin->w_llist_ref;
1909 if (ll_ref != NULL)
1910 {
1911 /* Find a window using the same location list that is not a
1912 * quickfix window. */
1913 FOR_ALL_WINDOWS(usable_win_ptr)
1914 if (usable_win_ptr->w_llist == ll_ref
1915 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001916 {
1917 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001918 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001919 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001920 }
1921
1922 if (!usable_win)
1923 {
1924 /* Locate a window showing a normal buffer */
1925 FOR_ALL_WINDOWS(win)
1926 if (win->w_buffer->b_p_bt[0] == NUL)
1927 {
1928 usable_win = 1;
1929 break;
1930 }
1931 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001932
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001934 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001935 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001937 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001938 {
1939 tabpage_T *tp;
1940 win_T *wp;
1941
1942 FOR_ALL_TAB_WINDOWS(tp, wp)
1943 {
1944 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1945 {
1946 goto_tabpage_win(tp, wp);
1947 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001948 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001949 }
1950 }
1951 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001952win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001953
1954 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001955 * If there is only one window and it is the quickfix window, create a
1956 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001957 */
1958 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001960 flags = WSP_ABOVE;
1961 if (ll_ref != NULL)
1962 flags |= WSP_NEWLOC;
1963 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 goto failed; /* not enough room for window */
1965 opened_window = TRUE; /* close it when fail */
1966 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001967 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001968 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001969 if (ll_ref != NULL)
1970 {
1971 /* The new window should use the location list from the
1972 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973 curwin->w_llist = ll_ref;
1974 ll_ref->qf_refcount++;
1975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977 else
1978 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001979 if (curwin->w_llist_ref != NULL)
1980 {
1981 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001982 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001983 if (win == NULL)
1984 {
1985 /* Find the window showing the selected file */
1986 FOR_ALL_WINDOWS(win)
1987 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1988 break;
1989 if (win == NULL)
1990 {
1991 /* Find a previous usable window */
1992 win = curwin;
1993 do
1994 {
1995 if (win->w_buffer->b_p_bt[0] == NUL)
1996 break;
1997 if (win->w_prev == NULL)
1998 win = lastwin; /* wrap around the top */
1999 else
2000 win = win->w_prev; /* go to previous window */
2001 } while (win != curwin);
2002 }
2003 }
2004 win_goto(win);
2005
2006 /* If the location list for the window is not set, then set it
2007 * to the location list from the location window */
2008 if (win->w_llist == NULL)
2009 {
2010 win->w_llist = ll_ref;
2011 ll_ref->qf_refcount++;
2012 }
2013 }
2014 else
2015 {
2016
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 /*
2018 * Try to find a window that shows the right buffer.
2019 * Default to the window just above the quickfix buffer.
2020 */
2021 win = curwin;
2022 altwin = NULL;
2023 for (;;)
2024 {
2025 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2026 break;
2027 if (win->w_prev == NULL)
2028 win = lastwin; /* wrap around the top */
2029 else
2030 win = win->w_prev; /* go to previous window */
2031
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002032 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
2034 /* Didn't find it, go to the window before the quickfix
2035 * window. */
2036 if (altwin != NULL)
2037 win = altwin;
2038 else if (curwin->w_prev != NULL)
2039 win = curwin->w_prev;
2040 else
2041 win = curwin->w_next;
2042 break;
2043 }
2044
2045 /* Remember a usable window. */
2046 if (altwin == NULL && !win->w_p_pvw
2047 && win->w_buffer->b_p_bt[0] == NUL)
2048 altwin = win;
2049 }
2050
2051 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054 }
2055#endif
2056
2057 /*
2058 * If there is a file name,
2059 * read the wanted file if needed, and check autowrite etc.
2060 */
2061 old_curbuf = curbuf;
2062 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002063
2064 if (qf_ptr->qf_fnum != 0)
2065 {
2066 if (qf_ptr->qf_type == 1)
2067 {
2068 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2069 * set b_p_ro flag). */
2070 if (!can_abandon(curbuf, forceit))
2071 {
2072 EMSG(_(e_nowrtmsg));
2073 ok = FALSE;
2074 }
2075 else
2076 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002077 ECMD_HIDE + ECMD_SET_HELP,
2078 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002079 }
2080 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002081 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002082 int old_qf_curlist = qi->qf_curlist;
2083 int is_abort = FALSE;
2084
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002085 ok = buflist_getfile(qf_ptr->qf_fnum,
2086 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002087 if (qi != &ql_info && !win_valid(oldwin))
2088 {
2089 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002090 is_abort = TRUE;
2091 opened_window = FALSE;
2092 }
2093 else if (old_qf_curlist != qi->qf_curlist
2094 || !is_qf_entry_present(qi, qf_ptr))
2095 {
2096 if (qi == &ql_info)
2097 EMSG(_("E925: Current quickfix was changed"));
2098 else
2099 EMSG(_("E926: Current location list was changed"));
2100 is_abort = TRUE;
2101 }
2102
2103 if (is_abort)
2104 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002105 ok = FALSE;
2106 qi = NULL;
2107 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002108 }
2109 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002110 }
2111
2112 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
2114 /* When not switched to another buffer, still need to set pc mark */
2115 if (curbuf == old_curbuf)
2116 setpcmark();
2117
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002118 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002120 /*
2121 * Go to line with error, unless qf_lnum is 0.
2122 */
2123 i = qf_ptr->qf_lnum;
2124 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002126 if (i > curbuf->b_ml.ml_line_count)
2127 i = curbuf->b_ml.ml_line_count;
2128 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002130 if (qf_ptr->qf_col > 0)
2131 {
2132 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002133#ifdef FEAT_VIRTUALEDIT
2134 curwin->w_cursor.coladd = 0;
2135#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002136 if (qf_ptr->qf_viscol == TRUE)
2137 {
2138 /*
2139 * Check each character from the beginning of the error
2140 * line up to the error column. For each tab character
2141 * found, reduce the error column value by the length of
2142 * a tab character.
2143 */
2144 line = ml_get_curline();
2145 screen_col = 0;
2146 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2147 {
2148 if (*line == NUL)
2149 break;
2150 if (*line++ == '\t')
2151 {
2152 curwin->w_cursor.col -= 7 - (screen_col % 8);
2153 screen_col += 8 - (screen_col % 8);
2154 }
2155 else
2156 ++screen_col;
2157 }
2158 }
2159 check_cursor();
2160 }
2161 else
2162 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002165 {
2166 pos_T save_cursor;
2167
2168 /* Move the cursor to the first line in the buffer */
2169 save_cursor = curwin->w_cursor;
2170 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002171 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2172 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002173 curwin->w_cursor = save_cursor;
2174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
2176#ifdef FEAT_FOLDING
2177 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2178 foldOpenCursor();
2179#endif
2180 if (print_message)
2181 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002182 /* Update the screen before showing the message, unless the screen
2183 * scrolled up. */
2184 if (!msg_scrolled)
2185 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002187 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2189 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2190 /* Add the message, skipping leading whitespace and newlines. */
2191 len = (int)STRLEN(IObuff);
2192 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2193
2194 /* Output the message. Overwrite to avoid scrolling when the 'O'
2195 * flag is present in 'shortmess'; But when not jumping, print the
2196 * whole message. */
2197 i = msg_scroll;
2198 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2199 msg_scroll = TRUE;
2200 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2201 msg_scroll = FALSE;
2202 msg_attr_keep(IObuff, 0, TRUE);
2203 msg_scroll = i;
2204 }
2205 }
2206 else
2207 {
2208#ifdef FEAT_WINDOWS
2209 if (opened_window)
2210 win_close(curwin, TRUE); /* Close opened window */
2211#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002212 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
2214 /*
2215 * Couldn't open file, so put index back where it was. This could
2216 * happen if the file was readonly and we changed something.
2217 */
2218#ifdef FEAT_WINDOWS
2219failed:
2220#endif
2221 qf_ptr = old_qf_ptr;
2222 qf_index = old_qf_index;
2223 }
2224 }
2225theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002226 if (qi != NULL)
2227 {
2228 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2229 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231#ifdef FEAT_WINDOWS
2232 if (p_swb != old_swb && opened_window)
2233 {
2234 /* Restore old 'switchbuf' value, but not when an autocommand or
2235 * modeline has changed the value. */
2236 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002239 swb_flags = old_swb_flags;
2240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 else
2242 free_string_option(old_swb);
2243 }
2244#endif
2245}
2246
2247/*
2248 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002249 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 */
2251 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002252qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002254 buf_T *buf;
2255 char_u *fname;
2256 qfline_T *qfp;
2257 int i;
2258 int idx1 = 1;
2259 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002260 char_u *arg = eap->arg;
2261 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002263 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002265 if (eap->cmdidx == CMD_llist)
2266 {
2267 qi = GET_LOC_LIST(curwin);
2268 if (qi == NULL)
2269 {
2270 EMSG(_(e_loclist));
2271 return;
2272 }
2273 }
2274
2275 if (qi->qf_curlist >= qi->qf_listcount
2276 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278 EMSG(_(e_quickfix));
2279 return;
2280 }
2281 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2282 {
2283 EMSG(_(e_trailing));
2284 return;
2285 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002286 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 if (idx1 < 0)
2288 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2289 if (idx2 < 0)
2290 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2291
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002292 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002294 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2295 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2298 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002299 msg_putchar('\n');
2300 if (got_int)
2301 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002302
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002303 fname = NULL;
2304 if (qfp->qf_fnum != 0
2305 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2306 {
2307 fname = buf->b_fname;
2308 if (qfp->qf_type == 1) /* :helpgrep */
2309 fname = gettail(fname);
2310 }
2311 if (fname == NULL)
2312 sprintf((char *)IObuff, "%2d", i);
2313 else
2314 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2315 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002316 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002317 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2318 if (qfp->qf_lnum == 0)
2319 IObuff[0] = NUL;
2320 else if (qfp->qf_col == 0)
2321 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2322 else
2323 sprintf((char *)IObuff, ":%ld col %d",
2324 qfp->qf_lnum, qfp->qf_col);
2325 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2326 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2327 msg_puts_attr(IObuff, hl_attr(HLF_N));
2328 if (qfp->qf_pattern != NULL)
2329 {
2330 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2331 STRCAT(IObuff, ":");
2332 msg_puts(IObuff);
2333 }
2334 msg_puts((char_u *)" ");
2335
2336 /* Remove newlines and leading whitespace from the text. For an
2337 * unrecognized line keep the indent, the compiler may mark a word
2338 * with ^^^^. */
2339 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2341 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002342 msg_prt_line(IObuff, FALSE);
2343 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002345
2346 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002347 if (qfp == NULL)
2348 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002349 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 ui_breakcheck();
2351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352}
2353
2354/*
2355 * Remove newlines and leading whitespace from an error message.
2356 * Put the result in "buf[bufsize]".
2357 */
2358 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002359qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361 int i;
2362 char_u *p = text;
2363
2364 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2365 {
2366 if (*p == '\n')
2367 {
2368 buf[i] = ' ';
2369 while (*++p != NUL)
2370 if (!vim_iswhite(*p) && *p != '\n')
2371 break;
2372 }
2373 else
2374 buf[i] = *p++;
2375 }
2376 buf[i] = NUL;
2377}
2378
2379/*
2380 * ":colder [count]": Up in the quickfix stack.
2381 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002382 * ":lolder [count]": Up in the location list stack.
2383 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 */
2385 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002386qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002388 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 int count;
2390
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002391 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2392 {
2393 qi = GET_LOC_LIST(curwin);
2394 if (qi == NULL)
2395 {
2396 EMSG(_(e_loclist));
2397 return;
2398 }
2399 }
2400
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (eap->addr_count != 0)
2402 count = eap->line2;
2403 else
2404 count = 1;
2405 while (count--)
2406 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002407 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002409 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
2411 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002414 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 else
2417 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002418 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
2420 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002421 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002423 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002426 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427}
2428
2429 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002430qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 qi->qf_curlist + 1, qi->qf_listcount,
2434 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002436 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437#endif
2438}
2439
2440/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002441 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 */
2443 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002444qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002446 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002447 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002448 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002450 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002452 qfp = qi->qf_lists[idx].qf_start;
2453 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002454 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002455 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002456 vim_free(qfp->qf_text);
2457 stop = (qfp == qfpnext);
2458 vim_free(qfp->qf_pattern);
2459 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002460 if (stop)
2461 /* Somehow qf_count may have an incorrect value, set it to 1
2462 * to avoid crashing when it's wrong.
2463 * TODO: Avoid qf_count being incorrect. */
2464 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002465 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002466 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002467 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002469 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002470 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002471 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472}
2473
2474/*
2475 * qf_mark_adjust: adjust marks
2476 */
2477 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002478qf_mark_adjust(
2479 win_T *wp,
2480 linenr_T line1,
2481 linenr_T line2,
2482 long amount,
2483 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002485 int i;
2486 qfline_T *qfp;
2487 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002488 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002489 int found_one = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002491 if (!curbuf->b_has_qf_entry)
2492 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002493 if (wp != NULL)
2494 {
2495 if (wp->w_llist == NULL)
2496 return;
2497 qi = wp->w_llist;
2498 }
2499
2500 for (idx = 0; idx < qi->qf_listcount; ++idx)
2501 if (qi->qf_lists[idx].qf_count)
2502 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002503 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2504 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 if (qfp->qf_fnum == curbuf->b_fnum)
2506 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002507 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2509 {
2510 if (amount == MAXLNUM)
2511 qfp->qf_cleared = TRUE;
2512 else
2513 qfp->qf_lnum += amount;
2514 }
2515 else if (amount_after && qfp->qf_lnum > line2)
2516 qfp->qf_lnum += amount_after;
2517 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002518
2519 if (!found_one)
2520 curbuf->b_has_qf_entry = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521}
2522
2523/*
2524 * Make a nice message out of the error character and the error number:
2525 * char number message
2526 * e or E 0 " error"
2527 * w or W 0 " warning"
2528 * i or I 0 " info"
2529 * 0 0 ""
2530 * other 0 " c"
2531 * e or E n " error n"
2532 * w or W n " warning n"
2533 * i or I n " info n"
2534 * 0 n " error n"
2535 * other n " c n"
2536 * 1 x "" :helpgrep
2537 */
2538 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002539qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 static char_u buf[20];
2542 static char_u cc[3];
2543 char_u *p;
2544
2545 if (c == 'W' || c == 'w')
2546 p = (char_u *)" warning";
2547 else if (c == 'I' || c == 'i')
2548 p = (char_u *)" info";
2549 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2550 p = (char_u *)" error";
2551 else if (c == 0 || c == 1)
2552 p = (char_u *)"";
2553 else
2554 {
2555 cc[0] = ' ';
2556 cc[1] = c;
2557 cc[2] = NUL;
2558 p = cc;
2559 }
2560
2561 if (nr <= 0)
2562 return p;
2563
2564 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2565 return buf;
2566}
2567
2568#if defined(FEAT_WINDOWS) || defined(PROTO)
2569/*
2570 * ":cwindow": open the quickfix window if we have errors to display,
2571 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002572 * ":lwindow": open the location list window if we have locations to display,
2573 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 */
2575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002576ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002578 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 win_T *win;
2580
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002581 if (eap->cmdidx == CMD_lwindow)
2582 {
2583 qi = GET_LOC_LIST(curwin);
2584 if (qi == NULL)
2585 return;
2586 }
2587
2588 /* Look for an existing quickfix window. */
2589 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
2591 /*
2592 * If a quickfix window is open but we have no errors to display,
2593 * close the window. If a quickfix window is not open, then open
2594 * it if we have errors; otherwise, leave it closed.
2595 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002596 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002597 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002598 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
2600 if (win != NULL)
2601 ex_cclose(eap);
2602 }
2603 else if (win == NULL)
2604 ex_copen(eap);
2605}
2606
2607/*
2608 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002609 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002612ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002614 win_T *win = NULL;
2615 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002617 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2618 {
2619 qi = GET_LOC_LIST(curwin);
2620 if (qi == NULL)
2621 return;
2622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002624 /* Find existing quickfix window and close it. */
2625 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (win != NULL)
2627 win_close(win, FALSE);
2628}
2629
2630/*
2631 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002632 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 */
2634 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002635ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002637 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002640 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002641 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002642 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002644 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2645 {
2646 qi = GET_LOC_LIST(curwin);
2647 if (qi == NULL)
2648 {
2649 EMSG(_(e_loclist));
2650 return;
2651 }
2652 }
2653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (eap->addr_count != 0)
2655 height = eap->line2;
2656 else
2657 height = QF_WINHEIGHT;
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660#ifdef FEAT_GUI
2661 need_mouse_correct = TRUE;
2662#endif
2663
2664 /*
2665 * Find existing quickfix window, or open a new one.
2666 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002667 win = qf_find_win(qi);
2668
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002669 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002672 if (eap->addr_count != 0)
2673 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002674 if (cmdmod.split & WSP_VERT)
2675 {
2676 if (height != W_WIDTH(win))
2677 win_setwidth(height);
2678 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002679 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002680 win_setheight(height);
2681 }
2682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 else
2684 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002685 qf_buf = qf_find_buf(qi);
2686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 /* The current window becomes the previous window afterwards. */
2688 win = curwin;
2689
Bram Moolenaar77642c02012-11-20 17:55:10 +01002690 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2691 && cmdmod.split == 0)
2692 /* Create the new window at the very bottom, except when
2693 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002694 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002695 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002697 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002699 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002701 /*
2702 * For the location list window, create a reference to the
2703 * location list from the window 'win'.
2704 */
2705 curwin->w_llist_ref = win->w_llist;
2706 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002708
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002709 if (oldwin != curwin)
2710 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002711 if (qf_buf != NULL)
2712 /* Use the existing quickfix buffer */
2713 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002714 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002715 else
2716 {
2717 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002718 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002719 /* switch off 'swapfile' */
2720 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2721 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002722 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002723 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002724 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002725#ifdef FEAT_DIFF
2726 curwin->w_p_diff = FALSE;
2727#endif
2728#ifdef FEAT_FOLDING
2729 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2730 OPT_LOCAL);
2731#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002734 /* Only set the height when still in the same tab page and there is no
2735 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002736 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 win_setheight(height);
2738 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2739 if (win_valid(win))
2740 prevwin = win;
2741 }
2742
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002743 qf_set_title_var(qi);
2744
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 /*
2746 * Fill the buffer with the quickfix list.
2747 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002748 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002750 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 curwin->w_cursor.col = 0;
2752 check_cursor();
2753 update_topline(); /* scroll to show the line */
2754}
2755
2756/*
2757 * Return the number of the current entry (line number in the quickfix
2758 * window).
2759 */
2760 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002761qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002763 qf_info_T *qi = &ql_info;
2764
2765 if (IS_LL_WINDOW(wp))
2766 /* In the location list window, use the referenced location list */
2767 qi = wp->w_llist_ref;
2768
2769 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770}
2771
2772/*
2773 * Update the cursor position in the quickfix window to the current error.
2774 * Return TRUE if there is a quickfix window.
2775 */
2776 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002777qf_win_pos_update(
2778 qf_info_T *qi,
2779 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
2781 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002782 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
2784 /*
2785 * Put the cursor on the current error in the quickfix window, so that
2786 * it's viewable.
2787 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002788 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 if (win != NULL
2790 && qf_index <= win->w_buffer->b_ml.ml_line_count
2791 && old_qf_index != qf_index)
2792 {
2793 win_T *old_curwin = curwin;
2794
2795 curwin = win;
2796 curbuf = win->w_buffer;
2797 if (qf_index > old_qf_index)
2798 {
2799 curwin->w_redraw_top = old_qf_index;
2800 curwin->w_redraw_bot = qf_index;
2801 }
2802 else
2803 {
2804 curwin->w_redraw_top = qf_index;
2805 curwin->w_redraw_bot = old_qf_index;
2806 }
2807 curwin->w_cursor.lnum = qf_index;
2808 curwin->w_cursor.col = 0;
2809 update_topline(); /* scroll to show the line */
2810 redraw_later(VALID);
2811 curwin->w_redr_status = TRUE; /* update ruler */
2812 curwin = old_curwin;
2813 curbuf = curwin->w_buffer;
2814 }
2815 return win != NULL;
2816}
2817
2818/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002819 * Check whether the given window is displaying the specified quickfix/location
2820 * list buffer
2821 */
2822 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002823is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002824{
2825 /*
2826 * A window displaying the quickfix buffer will have the w_llist_ref field
2827 * set to NULL.
2828 * A window displaying a location list buffer will have the w_llist_ref
2829 * pointing to the location list.
2830 */
2831 if (bt_quickfix(win->w_buffer))
2832 if ((qi == &ql_info && win->w_llist_ref == NULL)
2833 || (qi != &ql_info && win->w_llist_ref == qi))
2834 return TRUE;
2835
2836 return FALSE;
2837}
2838
2839/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002840 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002841 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002842 */
2843 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002844qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002845{
2846 win_T *win;
2847
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002848 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002849 if (is_qf_win(win, qi))
2850 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002851
2852 return win;
2853}
2854
2855/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002856 * Find a quickfix buffer.
2857 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 */
2859 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002860qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002862 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002863 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
Bram Moolenaar9c102382006-05-03 21:26:49 +00002865 FOR_ALL_TAB_WINDOWS(tp, win)
2866 if (is_qf_win(win, qi))
2867 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002868
Bram Moolenaar9c102382006-05-03 21:26:49 +00002869 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870}
2871
2872/*
2873 * Find the quickfix buffer. If it exists, update the contents.
2874 */
2875 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002876qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877{
2878 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002879 win_T *win;
2880 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
2883 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002884 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 if (buf != NULL)
2886 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02002887 linenr_T old_line_count = buf->b_ml.ml_line_count;
2888
2889 if (old_last == NULL)
2890 /* set curwin/curbuf to buf and save a few things */
2891 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002893 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002894 {
2895 curwin_save = curwin;
2896 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002897 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002898 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002899 }
2900
Bram Moolenaar864293a2016-06-02 13:40:04 +02002901 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01002902
Bram Moolenaar864293a2016-06-02 13:40:04 +02002903 if (old_last == NULL)
2904 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002905 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002906
2907 /* restore curwin/curbuf and a few other things */
2908 aucmd_restbuf(&aco);
2909 }
2910
2911 /* Only redraw when added lines are visible. This avoids flickering
2912 * when the added lines are not visible. */
2913 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
2914 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
2916}
2917
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002918/*
2919 * Set "w:quickfix_title" if "qi" has a title.
2920 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002921 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002922qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002923{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002924 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2925 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002926 qi->qf_lists[qi->qf_curlist].qf_title);
2927}
2928
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929/*
2930 * Fill current buffer with quickfix errors, replacing any previous contents.
2931 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02002932 * If "old_last" is not NULL append the items after this one.
2933 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
2934 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 */
2936 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002937qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002939 linenr_T lnum;
2940 qfline_T *qfp;
2941 buf_T *errbuf;
2942 int len;
2943 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
Bram Moolenaar864293a2016-06-02 13:40:04 +02002945 if (old_last == NULL)
2946 {
2947 if (buf != curbuf)
2948 {
2949 EMSG2(_(e_intern2), "qf_fill_buffer()");
2950 return;
2951 }
2952
2953 /* delete all existing lines */
2954 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2955 (void)ml_delete((linenr_T)1, FALSE);
2956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002959 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
2961 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002962 if (old_last == NULL)
2963 {
2964 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2965 lnum = 0;
2966 }
2967 else
2968 {
2969 qfp = old_last->qf_next;
2970 lnum = buf->b_ml.ml_line_count;
2971 }
2972 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 {
2974 if (qfp->qf_fnum != 0
2975 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2976 && errbuf->b_fname != NULL)
2977 {
2978 if (qfp->qf_type == 1) /* :helpgrep */
2979 STRCPY(IObuff, gettail(errbuf->b_fname));
2980 else
2981 STRCPY(IObuff, errbuf->b_fname);
2982 len = (int)STRLEN(IObuff);
2983 }
2984 else
2985 len = 0;
2986 IObuff[len++] = '|';
2987
2988 if (qfp->qf_lnum > 0)
2989 {
2990 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2991 len += (int)STRLEN(IObuff + len);
2992
2993 if (qfp->qf_col > 0)
2994 {
2995 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2996 len += (int)STRLEN(IObuff + len);
2997 }
2998
2999 sprintf((char *)IObuff + len, "%s",
3000 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3001 len += (int)STRLEN(IObuff + len);
3002 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003003 else if (qfp->qf_pattern != NULL)
3004 {
3005 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3006 len += (int)STRLEN(IObuff + len);
3007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 IObuff[len++] = '|';
3009 IObuff[len++] = ' ';
3010
3011 /* Remove newlines and leading whitespace from the text.
3012 * For an unrecognized line keep the indent, the compiler may
3013 * mark a word with ^^^^. */
3014 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3015 IObuff + len, IOSIZE - len);
3016
Bram Moolenaar864293a2016-06-02 13:40:04 +02003017 if (ml_append_buf(buf, lnum, IObuff,
3018 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003020 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003022 if (qfp == NULL)
3023 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003025
3026 if (old_last == NULL)
3027 /* Delete the empty line which is now at the end */
3028 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 }
3030
3031 /* correct cursor position */
3032 check_lnums(TRUE);
3033
Bram Moolenaar864293a2016-06-02 13:40:04 +02003034 if (old_last == NULL)
3035 {
3036 /* Set the 'filetype' to "qf" each time after filling the buffer.
3037 * This resembles reading a file into a buffer, it's more logical when
3038 * using autocommands. */
3039 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3040 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
3042#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003043 keep_filetype = TRUE; /* don't detect 'filetype' */
3044 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003046 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003048 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003050 /* make sure it will be redrawn */
3051 redraw_curbuf_later(NOT_VALID);
3052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 /* Restore KeyTyped, setting 'filetype' may reset it. */
3055 KeyTyped = old_KeyTyped;
3056}
3057
3058#endif /* FEAT_WINDOWS */
3059
3060/*
3061 * Return TRUE if "buf" is the quickfix buffer.
3062 */
3063 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003064bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003066 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067}
3068
3069/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003070 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3071 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 */
3073 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003074bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003076 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3077 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078}
3079
3080/*
3081 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3082 */
3083 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003084bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003086 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087}
3088
3089 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003090bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 if (bt_dontwrite(buf))
3093 {
3094 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3095 return TRUE;
3096 }
3097 return FALSE;
3098}
3099
3100/*
3101 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3102 * and 'bufhidden'.
3103 */
3104 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003105buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3108 switch (buf->b_p_bh[0])
3109 {
3110 case 'u': /* "unload" */
3111 case 'w': /* "wipe" */
3112 case 'd': return FALSE; /* "delete" */
3113 case 'h': return TRUE; /* "hide" */
3114 }
3115 return (p_hid || cmdmod.hide);
3116}
3117
3118/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003119 * Return TRUE when using ":vimgrep" for ":grep".
3120 */
3121 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003122grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003123{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003124 return ((cmdidx == CMD_grep
3125 || cmdidx == CMD_lgrep
3126 || cmdidx == CMD_grepadd
3127 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003128 && STRCMP("internal",
3129 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3130}
3131
3132/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003133 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 */
3135 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003136ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003138 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 char_u *cmd;
3140 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003141 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003142 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003143 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003144#ifdef FEAT_AUTOCMD
3145 char_u *au_name = NULL;
3146
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003147 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3148 if (grep_internal(eap->cmdidx))
3149 {
3150 ex_vimgrep(eap);
3151 return;
3152 }
3153
Bram Moolenaar7c626922005-02-07 22:01:03 +00003154 switch (eap->cmdidx)
3155 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003156 case CMD_make: au_name = (char_u *)"make"; break;
3157 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3158 case CMD_grep: au_name = (char_u *)"grep"; break;
3159 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3160 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3161 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003162 default: break;
3163 }
3164 if (au_name != NULL)
3165 {
3166 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3167 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003168# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003169 if (did_throw || force_abort)
3170 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003171# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003172 }
3173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
Bram Moolenaara6557602006-02-04 22:43:20 +00003175 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3176 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003177 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003178
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003180 fname = get_mef_name();
3181 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003183 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185 /*
3186 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3187 */
3188 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3189 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003190 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 cmd = alloc(len);
3192 if (cmd == NULL)
3193 return;
3194 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3195 (char *)p_shq);
3196 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003197 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 /*
3199 * Output a newline if there's something else than the :make command that
3200 * was typed (in which case the cursor is in column 0).
3201 */
3202 if (msg_col == 0)
3203 msg_didout = FALSE;
3204 msg_start();
3205 MSG_PUTS(":!");
3206 msg_outtrans(cmd); /* show what we are doing */
3207
3208 /* let the shell know if we are redirecting output or not */
3209 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3210
3211#ifdef AMIGA
3212 out_flush();
3213 /* read window status report and redraw before message */
3214 (void)char_avail();
3215#endif
3216
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003217 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003218 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3219 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003220 && eap->cmdidx != CMD_lgrepadd),
3221 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003222 if (wp != NULL)
3223 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003224#ifdef FEAT_AUTOCMD
3225 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003226 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003227 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3228 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003229 if (qi->qf_curlist < qi->qf_listcount)
3230 res = qi->qf_lists[qi->qf_curlist].qf_count;
3231 else
3232 res = 0;
3233 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003234#endif
3235 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003236 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
Bram Moolenaar7c626922005-02-07 22:01:03 +00003238 mch_remove(fname);
3239 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 vim_free(cmd);
3241}
3242
3243/*
3244 * Return the name for the errorfile, in allocated memory.
3245 * Find a new unique name when 'makeef' contains "##".
3246 * Returns NULL for error.
3247 */
3248 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003249get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 char_u *p;
3252 char_u *name;
3253 static int start = -1;
3254 static int off = 0;
3255#ifdef HAVE_LSTAT
3256 struct stat sb;
3257#endif
3258
3259 if (*p_mef == NUL)
3260 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003261 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (name == NULL)
3263 EMSG(_(e_notmp));
3264 return name;
3265 }
3266
3267 for (p = p_mef; *p; ++p)
3268 if (p[0] == '#' && p[1] == '#')
3269 break;
3270
3271 if (*p == NUL)
3272 return vim_strsave(p_mef);
3273
3274 /* Keep trying until the name doesn't exist yet. */
3275 for (;;)
3276 {
3277 if (start == -1)
3278 start = mch_get_pid();
3279 else
3280 off += 19;
3281
3282 name = alloc((unsigned)STRLEN(p_mef) + 30);
3283 if (name == NULL)
3284 break;
3285 STRCPY(name, p_mef);
3286 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3287 STRCAT(name, p + 2);
3288 if (mch_getperm(name) < 0
3289#ifdef HAVE_LSTAT
3290 /* Don't accept a symbolic link, its a security risk. */
3291 && mch_lstat((char *)name, &sb) < 0
3292#endif
3293 )
3294 break;
3295 vim_free(name);
3296 }
3297 return name;
3298}
3299
3300/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003301 * Returns the number of valid entries in the current quickfix/location list.
3302 */
3303 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003304qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003305{
3306 qf_info_T *qi = &ql_info;
3307 qfline_T *qfp;
3308 int i, sz = 0;
3309 int prev_fnum = 0;
3310
3311 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3312 {
3313 /* Location list */
3314 qi = GET_LOC_LIST(curwin);
3315 if (qi == NULL)
3316 return 0;
3317 }
3318
3319 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003320 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003321 ++i, qfp = qfp->qf_next)
3322 {
3323 if (qfp->qf_valid)
3324 {
3325 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3326 sz++; /* Count all valid entries */
3327 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3328 {
3329 /* Count the number of files */
3330 sz++;
3331 prev_fnum = qfp->qf_fnum;
3332 }
3333 }
3334 }
3335
3336 return sz;
3337}
3338
3339/*
3340 * Returns the current index of the quickfix/location list.
3341 * Returns 0 if there is an error.
3342 */
3343 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003344qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003345{
3346 qf_info_T *qi = &ql_info;
3347
3348 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3349 {
3350 /* Location list */
3351 qi = GET_LOC_LIST(curwin);
3352 if (qi == NULL)
3353 return 0;
3354 }
3355
3356 return qi->qf_lists[qi->qf_curlist].qf_index;
3357}
3358
3359/*
3360 * Returns the current index in the quickfix/location list (counting only valid
3361 * entries). If no valid entries are in the list, then returns 1.
3362 */
3363 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003364qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003365{
3366 qf_info_T *qi = &ql_info;
3367 qf_list_T *qfl;
3368 qfline_T *qfp;
3369 int i, eidx = 0;
3370 int prev_fnum = 0;
3371
3372 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3373 {
3374 /* Location list */
3375 qi = GET_LOC_LIST(curwin);
3376 if (qi == NULL)
3377 return 1;
3378 }
3379
3380 qfl = &qi->qf_lists[qi->qf_curlist];
3381 qfp = qfl->qf_start;
3382
3383 /* check if the list has valid errors */
3384 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3385 return 1;
3386
3387 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3388 {
3389 if (qfp->qf_valid)
3390 {
3391 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3392 {
3393 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3394 {
3395 /* Count the number of files */
3396 eidx++;
3397 prev_fnum = qfp->qf_fnum;
3398 }
3399 }
3400 else
3401 eidx++;
3402 }
3403 }
3404
3405 return eidx ? eidx : 1;
3406}
3407
3408/*
3409 * Get the 'n'th valid error entry in the quickfix or location list.
3410 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3411 * For :cdo and :ldo returns the 'n'th valid error entry.
3412 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3413 */
3414 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003415qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003416{
3417 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3418 qfline_T *qfp = qfl->qf_start;
3419 int i, eidx;
3420 int prev_fnum = 0;
3421
3422 /* check if the list has valid errors */
3423 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3424 return 1;
3425
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003426 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003427 i++, qfp = qfp->qf_next)
3428 {
3429 if (qfp->qf_valid)
3430 {
3431 if (fdo)
3432 {
3433 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3434 {
3435 /* Count the number of files */
3436 eidx++;
3437 prev_fnum = qfp->qf_fnum;
3438 }
3439 }
3440 else
3441 eidx++;
3442 }
3443
3444 if (eidx == n)
3445 break;
3446 }
3447
3448 if (i <= qfl->qf_count)
3449 return i;
3450 else
3451 return 1;
3452}
3453
3454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003456 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003457 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 */
3459 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003460ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003462 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003463 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003464
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003465 if (eap->cmdidx == CMD_ll
3466 || eap->cmdidx == CMD_lrewind
3467 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003468 || eap->cmdidx == CMD_llast
3469 || eap->cmdidx == CMD_ldo
3470 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003471 {
3472 qi = GET_LOC_LIST(curwin);
3473 if (qi == NULL)
3474 {
3475 EMSG(_(e_loclist));
3476 return;
3477 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003478 }
3479
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003480 if (eap->addr_count > 0)
3481 errornr = (int)eap->line2;
3482 else
3483 {
3484 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3485 errornr = 0;
3486 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3487 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3488 errornr = 1;
3489 else
3490 errornr = 32767;
3491 }
3492
3493 /* For cdo and ldo commands, jump to the nth valid error.
3494 * For cfdo and lfdo commands, jump to the nth valid file entry.
3495 */
3496 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3497 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3498 errornr = qf_get_nth_valid_entry(qi,
3499 eap->addr_count > 0 ? (int)eap->line1 : 1,
3500 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3501
3502 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503}
3504
3505/*
3506 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003507 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003508 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 */
3510 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003511ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003513 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003514 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003515
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003516 if (eap->cmdidx == CMD_lnext
3517 || eap->cmdidx == CMD_lNext
3518 || eap->cmdidx == CMD_lprevious
3519 || eap->cmdidx == CMD_lnfile
3520 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003521 || eap->cmdidx == CMD_lpfile
3522 || eap->cmdidx == CMD_ldo
3523 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003524 {
3525 qi = GET_LOC_LIST(curwin);
3526 if (qi == NULL)
3527 {
3528 EMSG(_(e_loclist));
3529 return;
3530 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003531 }
3532
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003533 if (eap->addr_count > 0 &&
3534 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3535 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3536 errornr = (int)eap->line2;
3537 else
3538 errornr = 1;
3539
3540 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3541 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003543 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3544 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003546 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3547 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 ? BACKWARD_FILE
3549 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003550 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551}
3552
3553/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003554 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003555 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 */
3557 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003558ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003560 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003561 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003562#ifdef FEAT_AUTOCMD
3563 char_u *au_name = NULL;
3564#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003565
3566 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003567 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003568 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003569
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003570#ifdef FEAT_AUTOCMD
3571 switch (eap->cmdidx)
3572 {
3573 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3574 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3575 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3576 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3577 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3578 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3579 default: break;
3580 }
3581 if (au_name != NULL)
3582 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3583#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003584#ifdef FEAT_BROWSE
3585 if (cmdmod.browse)
3586 {
3587 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3588 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3589 if (browse_file == NULL)
3590 return;
3591 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3592 vim_free(browse_file);
3593 }
3594 else
3595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003597 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003598
3599 /*
3600 * This function is used by the :cfile, :cgetfile and :caddfile
3601 * commands.
3602 * :cfile always creates a new quickfix list and jumps to the
3603 * first error.
3604 * :cgetfile creates a new quickfix list but doesn't jump to the
3605 * first error.
3606 * :caddfile adds to an existing quickfix list. If there is no
3607 * quickfix list then a new list is created.
3608 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003609 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003610 && eap->cmdidx != CMD_laddfile),
3611 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003612 && (eap->cmdidx == CMD_cfile
3613 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003614 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003615#ifdef FEAT_AUTOCMD
3616 if (au_name != NULL)
3617 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3618#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003619 if (wp != NULL)
3620 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003621 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003622 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003623
3624 else
3625 {
3626#ifdef FEAT_AUTOCMD
3627 if (au_name != NULL)
3628 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3629#endif
3630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631}
3632
3633/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003634 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003635 * ":vimgrepadd {pattern} file(s)"
3636 * ":lvimgrep {pattern} file(s)"
3637 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003638 */
3639 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003640ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003641{
Bram Moolenaar81695252004-12-29 20:58:21 +00003642 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003643 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003644 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003645 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003646 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003647 char_u *s;
3648 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003649 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003650 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003651#ifdef FEAT_AUTOCMD
3652 qfline_T *cur_qf_start;
3653#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003654 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003655 buf_T *buf;
3656 int duplicate_name = FALSE;
3657 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003658 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003659 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003660 buf_T *first_match_buf = NULL;
3661 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003662 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003663#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3664 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003665#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003666 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003667 int flags = 0;
3668 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003669 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003670 char_u *dirname_start = NULL;
3671 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003672 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003673#ifdef FEAT_AUTOCMD
3674 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003675
3676 switch (eap->cmdidx)
3677 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003678 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3679 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3680 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003681 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003682 case CMD_grep: au_name = (char_u *)"grep"; break;
3683 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3684 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3685 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003686 default: break;
3687 }
3688 if (au_name != NULL)
3689 {
3690 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3691 curbuf->b_fname, TRUE, curbuf);
3692 if (did_throw || force_abort)
3693 return;
3694 }
3695#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003696
Bram Moolenaar754b5602006-02-09 23:53:20 +00003697 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003698 || eap->cmdidx == CMD_lvimgrep
3699 || eap->cmdidx == CMD_lgrepadd
3700 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003701 {
3702 qi = ll_get_or_alloc_list(curwin);
3703 if (qi == NULL)
3704 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003705 }
3706
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003707 if (eap->addr_count > 0)
3708 tomatch = eap->line2;
3709 else
3710 tomatch = MAXLNUM;
3711
Bram Moolenaar81695252004-12-29 20:58:21 +00003712 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003713 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003714 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003715 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003716 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003717 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003718 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003719 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003720 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003721
3722 if (s != NULL && *s == NUL)
3723 {
3724 /* Pattern is empty, use last search pattern. */
3725 if (last_search_pat() == NULL)
3726 {
3727 EMSG(_(e_noprevre));
3728 goto theend;
3729 }
3730 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3731 }
3732 else
3733 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3734
Bram Moolenaar86b68352004-12-27 21:59:20 +00003735 if (regmatch.regprog == NULL)
3736 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003737 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003738 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003739
3740 p = skipwhite(p);
3741 if (*p == NUL)
3742 {
3743 EMSG(_("E683: File name missing or invalid pattern"));
3744 goto theend;
3745 }
3746
Bram Moolenaar754b5602006-02-09 23:53:20 +00003747 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003748 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003749 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003750 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003751 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003752
3753 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003754 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003755 goto theend;
3756 if (fcount == 0)
3757 {
3758 EMSG(_(e_nomatch));
3759 goto theend;
3760 }
3761
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003762 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3763 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003764 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003765 {
3766 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003767 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003768 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003769
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003770 /* Remember the current directory, because a BufRead autocommand that does
3771 * ":lcd %:p:h" changes the meaning of short path names. */
3772 mch_dirname(dirname_start, MAXPATHL);
3773
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003774#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003775 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003776 * changing the current quickfix list. */
3777 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3778#endif
3779
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003780 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003781 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003782 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003783 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003784 if (time(NULL) > seconds)
3785 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003786 /* Display the file name every second or so, show the user we are
3787 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003788 seconds = time(NULL);
3789 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003790 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003791 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003792 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003793 else
3794 {
3795 msg_outtrans(p);
3796 vim_free(p);
3797 }
3798 msg_clr_eos();
3799 msg_didout = FALSE; /* overwrite this message */
3800 msg_nowait = TRUE; /* don't wait for this message */
3801 msg_col = 0;
3802 out_flush();
3803 }
3804
Bram Moolenaar81695252004-12-29 20:58:21 +00003805 buf = buflist_findname_exp(fnames[fi]);
3806 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3807 {
3808 /* Remember that a buffer with this name already exists. */
3809 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003810 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003811 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003812
3813#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3814 /* Don't do Filetype autocommands to avoid loading syntax and
3815 * indent scripts, a great speed improvement. */
3816 save_ei = au_event_disable(",Filetype");
3817#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003818 /* Don't use modelines here, it's useless. */
3819 save_mls = p_mls;
3820 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003821
3822 /* Load file into a buffer, so that 'fileencoding' is detected,
3823 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003824 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003825
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003826 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003827#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3828 au_event_restore(save_ei);
3829#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003830 }
3831 else
3832 /* Use existing, loaded buffer. */
3833 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003834
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003835#ifdef FEAT_AUTOCMD
3836 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3837 {
3838 int idx;
3839
3840 /* Autocommands changed the quickfix list. Find the one we were
3841 * using and restore it. */
3842 for (idx = 0; idx < LISTCOUNT; ++idx)
3843 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3844 {
3845 qi->qf_curlist = idx;
3846 break;
3847 }
3848 if (idx == LISTCOUNT)
3849 {
3850 /* List cannot be found, create a new one. */
3851 qf_new_list(qi, *eap->cmdlinep);
3852 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3853 }
3854 }
3855#endif
3856
Bram Moolenaar81695252004-12-29 20:58:21 +00003857 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003858 {
3859 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003860 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003861 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003862 else
3863 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003864 /* Try for a match in all lines of the buffer.
3865 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003866 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003867 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3868 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003869 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003870 col = 0;
3871 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003872 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003873 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003874 ;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003875 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003876 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003877 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003878 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003879 ml_get_buf(buf,
3880 regmatch.startpos[0].lnum + lnum, FALSE),
3881 regmatch.startpos[0].lnum + lnum,
3882 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003883 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003884 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003885 0, /* nr */
3886 0, /* type */
3887 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003888 ) == FAIL)
3889 {
3890 got_int = TRUE;
3891 break;
3892 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003893 found_match = TRUE;
3894 if (--tomatch == 0)
3895 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003896 if ((flags & VGR_GLOBAL) == 0
3897 || regmatch.endpos[0].lnum > 0)
3898 break;
3899 col = regmatch.endpos[0].col
3900 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003901 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003902 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003903 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003904 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003905 if (got_int)
3906 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003907 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003908#ifdef FEAT_AUTOCMD
3909 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3910#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003911
3912 if (using_dummy)
3913 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003914 if (found_match && first_match_buf == NULL)
3915 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003916 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003917 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003918 /* Never keep a dummy buffer if there is another buffer
3919 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003920 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003921 buf = NULL;
3922 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003923 else if (!cmdmod.hide
3924 || buf->b_p_bh[0] == 'u' /* "unload" */
3925 || buf->b_p_bh[0] == 'w' /* "wipe" */
3926 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003927 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003928 /* When no match was found we don't need to remember the
3929 * buffer, wipe it out. If there was a match and it
3930 * wasn't the first one or we won't jump there: only
3931 * unload the buffer.
3932 * Ignore 'hidden' here, because it may lead to having too
3933 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003934 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003935 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003936 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003937 buf = NULL;
3938 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003939 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003940 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003941 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003942 buf = NULL;
3943 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003944 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003945
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003946 if (buf != NULL)
3947 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003948 /* If the buffer is still loaded we need to use the
3949 * directory we jumped to below. */
3950 if (buf == first_match_buf
3951 && target_dir == NULL
3952 && STRCMP(dirname_start, dirname_now) != 0)
3953 target_dir = vim_strsave(dirname_now);
3954
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003955 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003956 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003957 * need to be done (again). But not the window-local
3958 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003959 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003960#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003961 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3962 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003963#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003964 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003965 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003966 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003967 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003968 }
3969 }
3970
3971 FreeWild(fcount, fnames);
3972
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003973 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3974 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3975 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003976
3977#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02003978 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003979#endif
3980
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003981#ifdef FEAT_AUTOCMD
3982 if (au_name != NULL)
3983 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3984 curbuf->b_fname, TRUE, curbuf);
3985#endif
3986
Bram Moolenaar86b68352004-12-27 21:59:20 +00003987 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003988 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003989 {
3990 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003991 {
3992 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003993 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003994 if (buf != curbuf)
3995 /* If we jumped to another buffer redrawing will already be
3996 * taken care of. */
3997 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003998
3999 /* Jump to the directory used after loading the buffer. */
4000 if (curbuf == first_match_buf && target_dir != NULL)
4001 {
4002 exarg_T ea;
4003
4004 ea.arg = target_dir;
4005 ea.cmdidx = CMD_lcd;
4006 ex_cd(&ea);
4007 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004008 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004009 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004010 else
4011 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004012
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004013 /* If we loaded a dummy buffer into the current window, the autocommands
4014 * may have messed up things, need to redraw and recompute folds. */
4015 if (redraw_for_dummy)
4016 {
4017#ifdef FEAT_FOLDING
4018 foldUpdateAll(curwin);
4019#else
4020 redraw_later(NOT_VALID);
4021#endif
4022 }
4023
Bram Moolenaar86b68352004-12-27 21:59:20 +00004024theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004025 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004026 vim_free(dirname_now);
4027 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004028 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004029 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004030}
4031
4032/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004033 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00004034 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004035 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
4036 * If "s" is not NULL terminate the pattern with a NUL.
4037 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00004038 */
4039 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004040skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004041{
4042 int c;
4043
4044 if (vim_isIDc(*p))
4045 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004046 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004047 if (s != NULL)
4048 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004049 p = skiptowhite(p);
4050 if (s != NULL && *p != NUL)
4051 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004052 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004053 else
4054 {
4055 /* ":vimgrep /pattern/[g][j] fname" */
4056 if (s != NULL)
4057 *s = p + 1;
4058 c = *p;
4059 p = skip_regexp(p + 1, c, TRUE, NULL);
4060 if (*p != c)
4061 return NULL;
4062
4063 /* Truncate the pattern. */
4064 if (s != NULL)
4065 *p = NUL;
4066 ++p;
4067
4068 /* Find the flags */
4069 while (*p == 'g' || *p == 'j')
4070 {
4071 if (flags != NULL)
4072 {
4073 if (*p == 'g')
4074 *flags |= VGR_GLOBAL;
4075 else
4076 *flags |= VGR_NOJUMP;
4077 }
4078 ++p;
4079 }
4080 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004081 return p;
4082}
4083
4084/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004085 * Restore current working directory to "dirname_start" if they differ, taking
4086 * into account whether it is set locally or globally.
4087 */
4088 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004089restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004090{
4091 char_u *dirname_now = alloc(MAXPATHL);
4092
4093 if (NULL != dirname_now)
4094 {
4095 mch_dirname(dirname_now, MAXPATHL);
4096 if (STRCMP(dirname_start, dirname_now) != 0)
4097 {
4098 /* If the directory has changed, change it back by building up an
4099 * appropriate ex command and executing it. */
4100 exarg_T ea;
4101
4102 ea.arg = dirname_start;
4103 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4104 ex_cd(&ea);
4105 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004106 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004107 }
4108}
4109
4110/*
4111 * Load file "fname" into a dummy buffer and return the buffer pointer,
4112 * placing the directory resulting from the buffer load into the
4113 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4114 * prior to calling this function. Restores directory to "dirname_start" prior
4115 * to returning, if autocmds or the 'autochdir' option have changed it.
4116 *
4117 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4118 * or wipe_dummy_buffer() later!
4119 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004120 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004121 */
4122 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004123load_dummy_buffer(
4124 char_u *fname,
4125 char_u *dirname_start, /* in: old directory */
4126 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004127{
4128 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004129 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004130 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004131 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004132
4133 /* Allocate a buffer without putting it in the buffer list. */
4134 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4135 if (newbuf == NULL)
4136 return NULL;
4137
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004138 /* Init the options. */
4139 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4140
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004141 /* need to open the memfile before putting the buffer in a window */
4142 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004143 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004144 /* set curwin/curbuf to buf and save a few things */
4145 aucmd_prepbuf(&aco, newbuf);
4146
4147 /* Need to set the filename for autocommands. */
4148 (void)setfname(curbuf, fname, NULL, FALSE);
4149
Bram Moolenaar81695252004-12-29 20:58:21 +00004150 /* Create swap file now to avoid the ATTENTION message. */
4151 check_need_swap(TRUE);
4152
4153 /* Remove the "dummy" flag, otherwise autocommands may not
4154 * work. */
4155 curbuf->b_flags &= ~BF_DUMMY;
4156
4157 if (readfile(fname, NULL,
4158 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4159 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004160 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004161 && !(curbuf->b_flags & BF_NEW))
4162 {
4163 failed = FALSE;
4164 if (curbuf != newbuf)
4165 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004166 /* Bloody autocommands changed the buffer! Can happen when
4167 * using netrw and editing a remote file. Use the current
4168 * buffer instead, delete the dummy one after restoring the
4169 * window stuff. */
4170 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004171 newbuf = curbuf;
4172 }
4173 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004174
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004175 /* restore curwin/curbuf and a few other things */
4176 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004177 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
4178 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004179 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004180
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004181 /*
4182 * When autocommands/'autochdir' option changed directory: go back.
4183 * Let the caller know what the resulting dir was first, in case it is
4184 * important.
4185 */
4186 mch_dirname(resulting_dir, MAXPATHL);
4187 restore_start_dir(dirname_start);
4188
Bram Moolenaar81695252004-12-29 20:58:21 +00004189 if (!buf_valid(newbuf))
4190 return NULL;
4191 if (failed)
4192 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004193 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004194 return NULL;
4195 }
4196 return newbuf;
4197}
4198
4199/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004200 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4201 * directory to "dirname_start" prior to returning, if autocmds or the
4202 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004203 */
4204 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004205wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004206{
4207 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004208 {
4209#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4210 cleanup_T cs;
4211
4212 /* Reset the error/interrupt/exception state here so that aborting()
4213 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4214 * work when got_int is set. */
4215 enter_cleanup(&cs);
4216#endif
4217
Bram Moolenaar81695252004-12-29 20:58:21 +00004218 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004219
4220#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4221 /* Restore the error/interrupt/exception state if not discarded by a
4222 * new aborting error, interrupt, or uncaught exception. */
4223 leave_cleanup(&cs);
4224#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004225 /* When autocommands/'autochdir' option changed directory: go back. */
4226 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004227 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004228}
4229
4230/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004231 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4232 * directory to "dirname_start" prior to returning, if autocmds or the
4233 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004234 */
4235 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004236unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004237{
4238 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004239 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004240 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004241
4242 /* When autocommands/'autochdir' option changed directory: go back. */
4243 restore_start_dir(dirname_start);
4244 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004245}
4246
Bram Moolenaar05159a02005-02-26 23:04:13 +00004247#if defined(FEAT_EVAL) || defined(PROTO)
4248/*
4249 * Add each quickfix error to list "list" as a dictionary.
4250 */
4251 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004252get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004253{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004254 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004255 dict_T *dict;
4256 char_u buf[2];
4257 qfline_T *qfp;
4258 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004259 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004260
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004261 if (wp != NULL)
4262 {
4263 qi = GET_LOC_LIST(wp);
4264 if (qi == NULL)
4265 return FAIL;
4266 }
4267
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004268 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004269 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004270 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004271
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004272 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4273 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004274 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004275 /* Handle entries with a non-existing buffer number. */
4276 bufnum = qfp->qf_fnum;
4277 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4278 bufnum = 0;
4279
Bram Moolenaar05159a02005-02-26 23:04:13 +00004280 if ((dict = dict_alloc()) == NULL)
4281 return FAIL;
4282 if (list_append_dict(list, dict) == FAIL)
4283 return FAIL;
4284
4285 buf[0] = qfp->qf_type;
4286 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004287 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004288 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4289 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4290 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4291 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004292 || dict_add_nr_str(dict, "pattern", 0L,
4293 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4294 || dict_add_nr_str(dict, "text", 0L,
4295 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004296 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4297 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4298 return FAIL;
4299
4300 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004301 if (qfp == NULL)
4302 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004303 }
4304 return OK;
4305}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004306
4307/*
4308 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004309 * of dictionaries. "title" will be copied to w:quickfix_title.
4310 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004311 */
4312 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004313set_errorlist(
4314 win_T *wp,
4315 list_T *list,
4316 int action,
4317 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004318{
4319 listitem_T *li;
4320 dict_T *d;
4321 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004322 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004323 long lnum;
4324 int col, nr;
4325 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004326#ifdef FEAT_WINDOWS
4327 qfline_T *old_last = NULL;
4328#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004329 int valid, status;
4330 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004331 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004332 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004333
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004334 if (wp != NULL)
4335 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004336 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004337 if (qi == NULL)
4338 return FAIL;
4339 }
4340
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004341 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004342 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004343 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004344#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004345 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4346 /* Adding to existing list, use last entry. */
4347 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004348#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004349 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004350 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004351 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004352 qf_store_title(qi, title);
4353 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004354
4355 for (li = list->lv_first; li != NULL; li = li->li_next)
4356 {
4357 if (li->li_tv.v_type != VAR_DICT)
4358 continue; /* Skip non-dict items */
4359
4360 d = li->li_tv.vval.v_dict;
4361 if (d == NULL)
4362 continue;
4363
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004364 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004365 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004366 lnum = get_dict_number(d, (char_u *)"lnum");
4367 col = get_dict_number(d, (char_u *)"col");
4368 vcol = get_dict_number(d, (char_u *)"vcol");
4369 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004370 type = get_dict_string(d, (char_u *)"type", TRUE);
4371 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4372 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004373 if (text == NULL)
4374 text = vim_strsave((char_u *)"");
4375
4376 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004377 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004378 valid = FALSE;
4379
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004380 /* Mark entries with non-existing buffer number as not valid. Give the
4381 * error message only once. */
4382 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4383 {
4384 if (!did_bufnr_emsg)
4385 {
4386 did_bufnr_emsg = TRUE;
4387 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4388 }
4389 valid = FALSE;
4390 bufnum = 0;
4391 }
4392
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004393 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004394 NULL, /* dir */
4395 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004396 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004397 text,
4398 lnum,
4399 col,
4400 vcol, /* vis_col */
4401 pattern, /* search pattern */
4402 nr,
4403 type == NULL ? NUL : *type,
4404 valid);
4405
4406 vim_free(filename);
4407 vim_free(pattern);
4408 vim_free(text);
4409 vim_free(type);
4410
4411 if (status == FAIL)
4412 {
4413 retval = FAIL;
4414 break;
4415 }
4416 }
4417
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004418 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004419 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004420 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4421 else
4422 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004423 if (action != 'a') {
4424 qi->qf_lists[qi->qf_curlist].qf_ptr =
4425 qi->qf_lists[qi->qf_curlist].qf_start;
4426 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4427 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4428 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004429
4430#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004431 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004432 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004433#endif
4434
4435 return retval;
4436}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004437#endif
4438
Bram Moolenaar81695252004-12-29 20:58:21 +00004439/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004440 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004441 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004442 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004443 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004444 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004445 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004446 */
4447 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004448ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004449{
4450 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004451 qf_info_T *qi = &ql_info;
4452
Bram Moolenaardb552d602006-03-23 22:59:57 +00004453 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4454 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004455 {
4456 qi = ll_get_or_alloc_list(curwin);
4457 if (qi == NULL)
4458 return;
4459 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004460
4461 if (*eap->arg == NUL)
4462 buf = curbuf;
4463 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4464 buf = buflist_findnr(atoi((char *)eap->arg));
4465 if (buf == NULL)
4466 EMSG(_(e_invarg));
4467 else if (buf->b_ml.ml_mfp == NULL)
4468 EMSG(_("E681: Buffer is not loaded"));
4469 else
4470 {
4471 if (eap->addr_count == 0)
4472 {
4473 eap->line1 = 1;
4474 eap->line2 = buf->b_ml.ml_line_count;
4475 }
4476 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4477 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4478 EMSG(_(e_invrange));
4479 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004480 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004481 char_u *qf_title = *eap->cmdlinep;
4482
4483 if (buf->b_sfname)
4484 {
4485 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4486 (char *)qf_title, (char *)buf->b_sfname);
4487 qf_title = IObuff;
4488 }
4489
Bram Moolenaardb552d602006-03-23 22:59:57 +00004490 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4491 (eap->cmdidx != CMD_caddbuffer
4492 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004493 eap->line1, eap->line2,
4494 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004495 && (eap->cmdidx == CMD_cbuffer
4496 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004497 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4498 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004499 }
4500}
4501
Bram Moolenaar1e015462005-09-25 22:16:38 +00004502#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004503/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004504 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4505 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004506 */
4507 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004508ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004509{
4510 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004511 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004512
Bram Moolenaardb552d602006-03-23 22:59:57 +00004513 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4514 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004515 {
4516 qi = ll_get_or_alloc_list(curwin);
4517 if (qi == NULL)
4518 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004519 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004520
Bram Moolenaar4770d092006-01-12 23:22:24 +00004521 /* Evaluate the expression. When the result is a string or a list we can
4522 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004523 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004524 if (tv != NULL)
4525 {
4526 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4527 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4528 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004529 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004530 (eap->cmdidx != CMD_caddexpr
4531 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004532 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004533 && (eap->cmdidx == CMD_cexpr
4534 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004535 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004536 }
4537 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004538 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004539 free_tv(tv);
4540 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004541}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004542#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004543
4544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 * ":helpgrep {pattern}"
4546 */
4547 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004548ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549{
4550 regmatch_T regmatch;
4551 char_u *save_cpo;
4552 char_u *p;
4553 int fcount;
4554 char_u **fnames;
4555 FILE *fd;
4556 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004558#ifdef FEAT_MULTI_LANG
4559 char_u *lang;
4560#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004561 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004562 int new_qi = FALSE;
4563 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004564#ifdef FEAT_AUTOCMD
4565 char_u *au_name = NULL;
4566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004568#ifdef FEAT_MULTI_LANG
4569 /* Check for a specified language */
4570 lang = check_help_lang(eap->arg);
4571#endif
4572
Bram Moolenaar73633f82012-01-20 13:39:07 +01004573#ifdef FEAT_AUTOCMD
4574 switch (eap->cmdidx)
4575 {
4576 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4577 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4578 default: break;
4579 }
4580 if (au_name != NULL)
4581 {
4582 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4583 curbuf->b_fname, TRUE, curbuf);
4584 if (did_throw || force_abort)
4585 return;
4586 }
4587#endif
4588
4589 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4590 save_cpo = p_cpo;
4591 p_cpo = empty_option;
4592
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004593 if (eap->cmdidx == CMD_lhelpgrep)
4594 {
4595 /* Find an existing help window */
4596 FOR_ALL_WINDOWS(wp)
4597 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4598 break;
4599
4600 if (wp == NULL) /* Help window not found */
4601 qi = NULL;
4602 else
4603 qi = wp->w_llist;
4604
4605 if (qi == NULL)
4606 {
4607 /* Allocate a new location list for help text matches */
4608 if ((qi = ll_new_list()) == NULL)
4609 return;
4610 new_qi = TRUE;
4611 }
4612 }
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4615 regmatch.rm_ic = FALSE;
4616 if (regmatch.regprog != NULL)
4617 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004618#ifdef FEAT_MBYTE
4619 vimconv_T vc;
4620
4621 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4622 * differs. */
4623 vc.vc_type = CONV_NONE;
4624 if (!enc_utf8)
4625 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4626#endif
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004629 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631 /* Go through all directories in 'runtimepath' */
4632 p = p_rtp;
4633 while (*p != NUL && !got_int)
4634 {
4635 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4636
4637 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4638 add_pathsep(NameBuff);
4639 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4640 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4641 &fnames, EW_FILE|EW_SILENT) == OK
4642 && fcount > 0)
4643 {
4644 for (fi = 0; fi < fcount && !got_int; ++fi)
4645 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004646#ifdef FEAT_MULTI_LANG
4647 /* Skip files for a different language. */
4648 if (lang != NULL
4649 && STRNICMP(lang, fnames[fi]
4650 + STRLEN(fnames[fi]) - 3, 2) != 0
4651 && !(STRNICMP(lang, "en", 2) == 0
4652 && STRNICMP("txt", fnames[fi]
4653 + STRLEN(fnames[fi]) - 3, 3) == 0))
4654 continue;
4655#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004656 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if (fd != NULL)
4658 {
4659 lnum = 1;
4660 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4661 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004662 char_u *line = IObuff;
4663#ifdef FEAT_MBYTE
4664 /* Convert a line if 'encoding' is not utf-8 and
4665 * the line contains a non-ASCII character. */
4666 if (vc.vc_type != CONV_NONE
4667 && has_non_ascii(IObuff)) {
4668 line = string_convert(&vc, IObuff, NULL);
4669 if (line == NULL)
4670 line = IObuff;
4671 }
4672#endif
4673
4674 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004676 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004679 while (l > 0 && line[l - 1] <= ' ')
4680 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004682 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 NULL, /* dir */
4684 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004685 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004686 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004688 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004689 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004690 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004691 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 0, /* nr */
4693 1, /* type */
4694 TRUE /* valid */
4695 ) == FAIL)
4696 {
4697 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004698#ifdef FEAT_MBYTE
4699 if (line != IObuff)
4700 vim_free(line);
4701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 break;
4703 }
4704 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004705#ifdef FEAT_MBYTE
4706 if (line != IObuff)
4707 vim_free(line);
4708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 ++lnum;
4710 line_breakcheck();
4711 }
4712 fclose(fd);
4713 }
4714 }
4715 FreeWild(fcount, fnames);
4716 }
4717 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004718
Bram Moolenaar473de612013-06-08 18:19:48 +02004719 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004720#ifdef FEAT_MBYTE
4721 if (vc.vc_type != CONV_NONE)
4722 convert_setup(&vc, NULL, NULL);
4723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004725 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4726 qi->qf_lists[qi->qf_curlist].qf_ptr =
4727 qi->qf_lists[qi->qf_curlist].qf_start;
4728 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 }
4730
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004731 if (p_cpo == empty_option)
4732 p_cpo = save_cpo;
4733 else
4734 /* Darn, some plugin changed the value. */
4735 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
4737#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004738 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739#endif
4740
Bram Moolenaar73633f82012-01-20 13:39:07 +01004741#ifdef FEAT_AUTOCMD
4742 if (au_name != NULL)
4743 {
4744 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4745 curbuf->b_fname, TRUE, curbuf);
4746 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4747 /* autocommands made "qi" invalid */
4748 return;
4749 }
4750#endif
4751
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004753 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004754 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004755 else
4756 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004757
4758 if (eap->cmdidx == CMD_lhelpgrep)
4759 {
4760 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004761 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004762 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4763 {
4764 if (new_qi)
4765 ll_free_all(&qi);
4766 }
4767 else if (curwin->w_llist == NULL)
4768 curwin->w_llist = qi;
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770}
4771
4772#endif /* FEAT_QUICKFIX */