blob: 19801e31ec9f1bb7a9396830bea8c99f733be5ea [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;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002261 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002262 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002264 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002266 if (eap->cmdidx == CMD_llist)
2267 {
2268 qi = GET_LOC_LIST(curwin);
2269 if (qi == NULL)
2270 {
2271 EMSG(_(e_loclist));
2272 return;
2273 }
2274 }
2275
2276 if (qi->qf_curlist >= qi->qf_listcount
2277 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
2279 EMSG(_(e_quickfix));
2280 return;
2281 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002282 if (*arg == '+')
2283 {
2284 ++arg;
2285 plus = TRUE;
2286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2288 {
2289 EMSG(_(e_trailing));
2290 return;
2291 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002292 if (plus)
2293 {
2294 i = qi->qf_lists[qi->qf_curlist].qf_index;
2295 idx2 = i + idx1;
2296 idx1 = i;
2297 }
2298 else
2299 {
2300 i = qi->qf_lists[qi->qf_curlist].qf_count;
2301 if (idx1 < 0)
2302 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2303 if (idx2 < 0)
2304 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002307 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002309 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2310 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
2312 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2313 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002314 msg_putchar('\n');
2315 if (got_int)
2316 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002317
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002318 fname = NULL;
2319 if (qfp->qf_fnum != 0
2320 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2321 {
2322 fname = buf->b_fname;
2323 if (qfp->qf_type == 1) /* :helpgrep */
2324 fname = gettail(fname);
2325 }
2326 if (fname == NULL)
2327 sprintf((char *)IObuff, "%2d", i);
2328 else
2329 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2330 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002331 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002332 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2333 if (qfp->qf_lnum == 0)
2334 IObuff[0] = NUL;
2335 else if (qfp->qf_col == 0)
2336 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2337 else
2338 sprintf((char *)IObuff, ":%ld col %d",
2339 qfp->qf_lnum, qfp->qf_col);
2340 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2341 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2342 msg_puts_attr(IObuff, hl_attr(HLF_N));
2343 if (qfp->qf_pattern != NULL)
2344 {
2345 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2346 STRCAT(IObuff, ":");
2347 msg_puts(IObuff);
2348 }
2349 msg_puts((char_u *)" ");
2350
2351 /* Remove newlines and leading whitespace from the text. For an
2352 * unrecognized line keep the indent, the compiler may mark a word
2353 * with ^^^^. */
2354 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2356 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002357 msg_prt_line(IObuff, FALSE);
2358 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002360
2361 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002362 if (qfp == NULL)
2363 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002364 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 ui_breakcheck();
2366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367}
2368
2369/*
2370 * Remove newlines and leading whitespace from an error message.
2371 * Put the result in "buf[bufsize]".
2372 */
2373 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002374qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
2376 int i;
2377 char_u *p = text;
2378
2379 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2380 {
2381 if (*p == '\n')
2382 {
2383 buf[i] = ' ';
2384 while (*++p != NUL)
2385 if (!vim_iswhite(*p) && *p != '\n')
2386 break;
2387 }
2388 else
2389 buf[i] = *p++;
2390 }
2391 buf[i] = NUL;
2392}
2393
2394/*
2395 * ":colder [count]": Up in the quickfix stack.
2396 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002397 * ":lolder [count]": Up in the location list stack.
2398 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
2400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002401qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002403 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 int count;
2405
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002406 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2407 {
2408 qi = GET_LOC_LIST(curwin);
2409 if (qi == NULL)
2410 {
2411 EMSG(_(e_loclist));
2412 return;
2413 }
2414 }
2415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (eap->addr_count != 0)
2417 count = eap->line2;
2418 else
2419 count = 1;
2420 while (count--)
2421 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002422 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002424 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
2426 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002429 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431 else
2432 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
2435 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002438 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002441 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442}
2443
2444 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002445qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002448 qi->qf_curlist + 1, qi->qf_listcount,
2449 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002451 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452#endif
2453}
2454
2455/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002456 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 */
2458 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002459qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002461 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002462 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002463 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002465 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002467 qfp = qi->qf_lists[idx].qf_start;
2468 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002469 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002470 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002471 vim_free(qfp->qf_text);
2472 stop = (qfp == qfpnext);
2473 vim_free(qfp->qf_pattern);
2474 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002475 if (stop)
2476 /* Somehow qf_count may have an incorrect value, set it to 1
2477 * to avoid crashing when it's wrong.
2478 * TODO: Avoid qf_count being incorrect. */
2479 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002480 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002481 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002482 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002484 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002485 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002486 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487}
2488
2489/*
2490 * qf_mark_adjust: adjust marks
2491 */
2492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002493qf_mark_adjust(
2494 win_T *wp,
2495 linenr_T line1,
2496 linenr_T line2,
2497 long amount,
2498 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002500 int i;
2501 qfline_T *qfp;
2502 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002503 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002504 int found_one = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002506 if (!curbuf->b_has_qf_entry)
2507 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002508 if (wp != NULL)
2509 {
2510 if (wp->w_llist == NULL)
2511 return;
2512 qi = wp->w_llist;
2513 }
2514
2515 for (idx = 0; idx < qi->qf_listcount; ++idx)
2516 if (qi->qf_lists[idx].qf_count)
2517 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002518 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2519 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (qfp->qf_fnum == curbuf->b_fnum)
2521 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002522 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2524 {
2525 if (amount == MAXLNUM)
2526 qfp->qf_cleared = TRUE;
2527 else
2528 qfp->qf_lnum += amount;
2529 }
2530 else if (amount_after && qfp->qf_lnum > line2)
2531 qfp->qf_lnum += amount_after;
2532 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002533
2534 if (!found_one)
2535 curbuf->b_has_qf_entry = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536}
2537
2538/*
2539 * Make a nice message out of the error character and the error number:
2540 * char number message
2541 * e or E 0 " error"
2542 * w or W 0 " warning"
2543 * i or I 0 " info"
2544 * 0 0 ""
2545 * other 0 " c"
2546 * e or E n " error n"
2547 * w or W n " warning n"
2548 * i or I n " info n"
2549 * 0 n " error n"
2550 * other n " c n"
2551 * 1 x "" :helpgrep
2552 */
2553 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002554qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556 static char_u buf[20];
2557 static char_u cc[3];
2558 char_u *p;
2559
2560 if (c == 'W' || c == 'w')
2561 p = (char_u *)" warning";
2562 else if (c == 'I' || c == 'i')
2563 p = (char_u *)" info";
2564 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2565 p = (char_u *)" error";
2566 else if (c == 0 || c == 1)
2567 p = (char_u *)"";
2568 else
2569 {
2570 cc[0] = ' ';
2571 cc[1] = c;
2572 cc[2] = NUL;
2573 p = cc;
2574 }
2575
2576 if (nr <= 0)
2577 return p;
2578
2579 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2580 return buf;
2581}
2582
2583#if defined(FEAT_WINDOWS) || defined(PROTO)
2584/*
2585 * ":cwindow": open the quickfix window if we have errors to display,
2586 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002587 * ":lwindow": open the location list window if we have locations to display,
2588 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
2590 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002591ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002593 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 win_T *win;
2595
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002596 if (eap->cmdidx == CMD_lwindow)
2597 {
2598 qi = GET_LOC_LIST(curwin);
2599 if (qi == NULL)
2600 return;
2601 }
2602
2603 /* Look for an existing quickfix window. */
2604 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605
2606 /*
2607 * If a quickfix window is open but we have no errors to display,
2608 * close the window. If a quickfix window is not open, then open
2609 * it if we have errors; otherwise, leave it closed.
2610 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002611 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002612 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002613 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 if (win != NULL)
2616 ex_cclose(eap);
2617 }
2618 else if (win == NULL)
2619 ex_copen(eap);
2620}
2621
2622/*
2623 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002624 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002627ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002629 win_T *win = NULL;
2630 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002632 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2633 {
2634 qi = GET_LOC_LIST(curwin);
2635 if (qi == NULL)
2636 return;
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002639 /* Find existing quickfix window and close it. */
2640 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (win != NULL)
2642 win_close(win, FALSE);
2643}
2644
2645/*
2646 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002647 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 */
2649 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002650ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002652 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002655 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002656 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002657 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002659 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2660 {
2661 qi = GET_LOC_LIST(curwin);
2662 if (qi == NULL)
2663 {
2664 EMSG(_(e_loclist));
2665 return;
2666 }
2667 }
2668
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (eap->addr_count != 0)
2670 height = eap->line2;
2671 else
2672 height = QF_WINHEIGHT;
2673
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675#ifdef FEAT_GUI
2676 need_mouse_correct = TRUE;
2677#endif
2678
2679 /*
2680 * Find existing quickfix window, or open a new one.
2681 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002682 win = qf_find_win(qi);
2683
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002684 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002685 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002687 if (eap->addr_count != 0)
2688 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002689 if (cmdmod.split & WSP_VERT)
2690 {
2691 if (height != W_WIDTH(win))
2692 win_setwidth(height);
2693 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002694 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002695 win_setheight(height);
2696 }
2697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 else
2699 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002700 qf_buf = qf_find_buf(qi);
2701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 /* The current window becomes the previous window afterwards. */
2703 win = curwin;
2704
Bram Moolenaar77642c02012-11-20 17:55:10 +01002705 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2706 && cmdmod.split == 0)
2707 /* Create the new window at the very bottom, except when
2708 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002709 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002710 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002712 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002714 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002716 /*
2717 * For the location list window, create a reference to the
2718 * location list from the window 'win'.
2719 */
2720 curwin->w_llist_ref = win->w_llist;
2721 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002723
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002724 if (oldwin != curwin)
2725 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002726 if (qf_buf != NULL)
2727 /* Use the existing quickfix buffer */
2728 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002729 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002730 else
2731 {
2732 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002733 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002734 /* switch off 'swapfile' */
2735 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2736 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002737 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002738 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002739 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002740#ifdef FEAT_DIFF
2741 curwin->w_p_diff = FALSE;
2742#endif
2743#ifdef FEAT_FOLDING
2744 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2745 OPT_LOCAL);
2746#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002749 /* Only set the height when still in the same tab page and there is no
2750 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002751 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 win_setheight(height);
2753 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2754 if (win_valid(win))
2755 prevwin = win;
2756 }
2757
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002758 qf_set_title_var(qi);
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 /*
2761 * Fill the buffer with the quickfix list.
2762 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002763 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002765 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 curwin->w_cursor.col = 0;
2767 check_cursor();
2768 update_topline(); /* scroll to show the line */
2769}
2770
2771/*
2772 * Return the number of the current entry (line number in the quickfix
2773 * window).
2774 */
2775 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002776qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002778 qf_info_T *qi = &ql_info;
2779
2780 if (IS_LL_WINDOW(wp))
2781 /* In the location list window, use the referenced location list */
2782 qi = wp->w_llist_ref;
2783
2784 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785}
2786
2787/*
2788 * Update the cursor position in the quickfix window to the current error.
2789 * Return TRUE if there is a quickfix window.
2790 */
2791 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002792qf_win_pos_update(
2793 qf_info_T *qi,
2794 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002797 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799 /*
2800 * Put the cursor on the current error in the quickfix window, so that
2801 * it's viewable.
2802 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002803 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (win != NULL
2805 && qf_index <= win->w_buffer->b_ml.ml_line_count
2806 && old_qf_index != qf_index)
2807 {
2808 win_T *old_curwin = curwin;
2809
2810 curwin = win;
2811 curbuf = win->w_buffer;
2812 if (qf_index > old_qf_index)
2813 {
2814 curwin->w_redraw_top = old_qf_index;
2815 curwin->w_redraw_bot = qf_index;
2816 }
2817 else
2818 {
2819 curwin->w_redraw_top = qf_index;
2820 curwin->w_redraw_bot = old_qf_index;
2821 }
2822 curwin->w_cursor.lnum = qf_index;
2823 curwin->w_cursor.col = 0;
2824 update_topline(); /* scroll to show the line */
2825 redraw_later(VALID);
2826 curwin->w_redr_status = TRUE; /* update ruler */
2827 curwin = old_curwin;
2828 curbuf = curwin->w_buffer;
2829 }
2830 return win != NULL;
2831}
2832
2833/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002834 * Check whether the given window is displaying the specified quickfix/location
2835 * list buffer
2836 */
2837 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002838is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002839{
2840 /*
2841 * A window displaying the quickfix buffer will have the w_llist_ref field
2842 * set to NULL.
2843 * A window displaying a location list buffer will have the w_llist_ref
2844 * pointing to the location list.
2845 */
2846 if (bt_quickfix(win->w_buffer))
2847 if ((qi == &ql_info && win->w_llist_ref == NULL)
2848 || (qi != &ql_info && win->w_llist_ref == qi))
2849 return TRUE;
2850
2851 return FALSE;
2852}
2853
2854/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002855 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002856 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002857 */
2858 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002859qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002860{
2861 win_T *win;
2862
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002863 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002864 if (is_qf_win(win, qi))
2865 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002866
2867 return win;
2868}
2869
2870/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002871 * Find a quickfix buffer.
2872 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 */
2874 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002875qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002877 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002878 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
Bram Moolenaar9c102382006-05-03 21:26:49 +00002880 FOR_ALL_TAB_WINDOWS(tp, win)
2881 if (is_qf_win(win, qi))
2882 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002883
Bram Moolenaar9c102382006-05-03 21:26:49 +00002884 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885}
2886
2887/*
2888 * Find the quickfix buffer. If it exists, update the contents.
2889 */
2890 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002891qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
2893 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002894 win_T *win;
2895 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002899 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 if (buf != NULL)
2901 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02002902 linenr_T old_line_count = buf->b_ml.ml_line_count;
2903
2904 if (old_last == NULL)
2905 /* set curwin/curbuf to buf and save a few things */
2906 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002908 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002909 {
2910 curwin_save = curwin;
2911 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002912 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002913 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002914 }
2915
Bram Moolenaar864293a2016-06-02 13:40:04 +02002916 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01002917
Bram Moolenaar864293a2016-06-02 13:40:04 +02002918 if (old_last == NULL)
2919 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002920 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002921
2922 /* restore curwin/curbuf and a few other things */
2923 aucmd_restbuf(&aco);
2924 }
2925
2926 /* Only redraw when added lines are visible. This avoids flickering
2927 * when the added lines are not visible. */
2928 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
2929 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
2931}
2932
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002933/*
2934 * Set "w:quickfix_title" if "qi" has a title.
2935 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002937qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002938{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002939 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2940 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002941 qi->qf_lists[qi->qf_curlist].qf_title);
2942}
2943
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944/*
2945 * Fill current buffer with quickfix errors, replacing any previous contents.
2946 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02002947 * If "old_last" is not NULL append the items after this one.
2948 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
2949 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 */
2951 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002952qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002954 linenr_T lnum;
2955 qfline_T *qfp;
2956 buf_T *errbuf;
2957 int len;
2958 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
Bram Moolenaar864293a2016-06-02 13:40:04 +02002960 if (old_last == NULL)
2961 {
2962 if (buf != curbuf)
2963 {
2964 EMSG2(_(e_intern2), "qf_fill_buffer()");
2965 return;
2966 }
2967
2968 /* delete all existing lines */
2969 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2970 (void)ml_delete((linenr_T)1, FALSE);
2971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
2973 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002974 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
2976 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002977 if (old_last == NULL)
2978 {
2979 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2980 lnum = 0;
2981 }
2982 else
2983 {
2984 qfp = old_last->qf_next;
2985 lnum = buf->b_ml.ml_line_count;
2986 }
2987 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
2989 if (qfp->qf_fnum != 0
2990 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2991 && errbuf->b_fname != NULL)
2992 {
2993 if (qfp->qf_type == 1) /* :helpgrep */
2994 STRCPY(IObuff, gettail(errbuf->b_fname));
2995 else
2996 STRCPY(IObuff, errbuf->b_fname);
2997 len = (int)STRLEN(IObuff);
2998 }
2999 else
3000 len = 0;
3001 IObuff[len++] = '|';
3002
3003 if (qfp->qf_lnum > 0)
3004 {
3005 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3006 len += (int)STRLEN(IObuff + len);
3007
3008 if (qfp->qf_col > 0)
3009 {
3010 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3011 len += (int)STRLEN(IObuff + len);
3012 }
3013
3014 sprintf((char *)IObuff + len, "%s",
3015 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3016 len += (int)STRLEN(IObuff + len);
3017 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003018 else if (qfp->qf_pattern != NULL)
3019 {
3020 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3021 len += (int)STRLEN(IObuff + len);
3022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 IObuff[len++] = '|';
3024 IObuff[len++] = ' ';
3025
3026 /* Remove newlines and leading whitespace from the text.
3027 * For an unrecognized line keep the indent, the compiler may
3028 * mark a word with ^^^^. */
3029 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3030 IObuff + len, IOSIZE - len);
3031
Bram Moolenaar864293a2016-06-02 13:40:04 +02003032 if (ml_append_buf(buf, lnum, IObuff,
3033 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003035 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003037 if (qfp == NULL)
3038 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003040
3041 if (old_last == NULL)
3042 /* Delete the empty line which is now at the end */
3043 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 }
3045
3046 /* correct cursor position */
3047 check_lnums(TRUE);
3048
Bram Moolenaar864293a2016-06-02 13:40:04 +02003049 if (old_last == NULL)
3050 {
3051 /* Set the 'filetype' to "qf" each time after filling the buffer.
3052 * This resembles reading a file into a buffer, it's more logical when
3053 * using autocommands. */
3054 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3055 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
3057#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003058 keep_filetype = TRUE; /* don't detect 'filetype' */
3059 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003061 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003063 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003065 /* make sure it will be redrawn */
3066 redraw_curbuf_later(NOT_VALID);
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 /* Restore KeyTyped, setting 'filetype' may reset it. */
3070 KeyTyped = old_KeyTyped;
3071}
3072
3073#endif /* FEAT_WINDOWS */
3074
3075/*
3076 * Return TRUE if "buf" is the quickfix buffer.
3077 */
3078 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003079bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003081 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082}
3083
3084/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003085 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3086 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 */
3088 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003089bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003091 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3092 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093}
3094
3095/*
3096 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3097 */
3098 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003099bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003101 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102}
3103
3104 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003105bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 if (bt_dontwrite(buf))
3108 {
3109 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3110 return TRUE;
3111 }
3112 return FALSE;
3113}
3114
3115/*
3116 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3117 * and 'bufhidden'.
3118 */
3119 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003120buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3123 switch (buf->b_p_bh[0])
3124 {
3125 case 'u': /* "unload" */
3126 case 'w': /* "wipe" */
3127 case 'd': return FALSE; /* "delete" */
3128 case 'h': return TRUE; /* "hide" */
3129 }
3130 return (p_hid || cmdmod.hide);
3131}
3132
3133/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003134 * Return TRUE when using ":vimgrep" for ":grep".
3135 */
3136 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003137grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003138{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003139 return ((cmdidx == CMD_grep
3140 || cmdidx == CMD_lgrep
3141 || cmdidx == CMD_grepadd
3142 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003143 && STRCMP("internal",
3144 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3145}
3146
3147/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003148 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 */
3150 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003151ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003153 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 char_u *cmd;
3155 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003156 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003157 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003158 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003159#ifdef FEAT_AUTOCMD
3160 char_u *au_name = NULL;
3161
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003162 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3163 if (grep_internal(eap->cmdidx))
3164 {
3165 ex_vimgrep(eap);
3166 return;
3167 }
3168
Bram Moolenaar7c626922005-02-07 22:01:03 +00003169 switch (eap->cmdidx)
3170 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003171 case CMD_make: au_name = (char_u *)"make"; break;
3172 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3173 case CMD_grep: au_name = (char_u *)"grep"; break;
3174 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3175 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3176 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003177 default: break;
3178 }
3179 if (au_name != NULL)
3180 {
3181 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3182 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003183# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003184 if (did_throw || force_abort)
3185 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003186# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003187 }
3188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
Bram Moolenaara6557602006-02-04 22:43:20 +00003190 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3191 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003192 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003195 fname = get_mef_name();
3196 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003198 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
3200 /*
3201 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3202 */
3203 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3204 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003205 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 cmd = alloc(len);
3207 if (cmd == NULL)
3208 return;
3209 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3210 (char *)p_shq);
3211 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003212 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 /*
3214 * Output a newline if there's something else than the :make command that
3215 * was typed (in which case the cursor is in column 0).
3216 */
3217 if (msg_col == 0)
3218 msg_didout = FALSE;
3219 msg_start();
3220 MSG_PUTS(":!");
3221 msg_outtrans(cmd); /* show what we are doing */
3222
3223 /* let the shell know if we are redirecting output or not */
3224 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3225
3226#ifdef AMIGA
3227 out_flush();
3228 /* read window status report and redraw before message */
3229 (void)char_avail();
3230#endif
3231
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003232 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003233 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3234 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003235 && eap->cmdidx != CMD_lgrepadd),
3236 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003237 if (wp != NULL)
3238 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003239#ifdef FEAT_AUTOCMD
3240 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003241 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003242 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3243 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003244 if (qi->qf_curlist < qi->qf_listcount)
3245 res = qi->qf_lists[qi->qf_curlist].qf_count;
3246 else
3247 res = 0;
3248 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003249#endif
3250 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003251 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Bram Moolenaar7c626922005-02-07 22:01:03 +00003253 mch_remove(fname);
3254 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 vim_free(cmd);
3256}
3257
3258/*
3259 * Return the name for the errorfile, in allocated memory.
3260 * Find a new unique name when 'makeef' contains "##".
3261 * Returns NULL for error.
3262 */
3263 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003264get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 char_u *p;
3267 char_u *name;
3268 static int start = -1;
3269 static int off = 0;
3270#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003271 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
3273
3274 if (*p_mef == NUL)
3275 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003276 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (name == NULL)
3278 EMSG(_(e_notmp));
3279 return name;
3280 }
3281
3282 for (p = p_mef; *p; ++p)
3283 if (p[0] == '#' && p[1] == '#')
3284 break;
3285
3286 if (*p == NUL)
3287 return vim_strsave(p_mef);
3288
3289 /* Keep trying until the name doesn't exist yet. */
3290 for (;;)
3291 {
3292 if (start == -1)
3293 start = mch_get_pid();
3294 else
3295 off += 19;
3296
3297 name = alloc((unsigned)STRLEN(p_mef) + 30);
3298 if (name == NULL)
3299 break;
3300 STRCPY(name, p_mef);
3301 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3302 STRCAT(name, p + 2);
3303 if (mch_getperm(name) < 0
3304#ifdef HAVE_LSTAT
3305 /* Don't accept a symbolic link, its a security risk. */
3306 && mch_lstat((char *)name, &sb) < 0
3307#endif
3308 )
3309 break;
3310 vim_free(name);
3311 }
3312 return name;
3313}
3314
3315/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003316 * Returns the number of valid entries in the current quickfix/location list.
3317 */
3318 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003319qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003320{
3321 qf_info_T *qi = &ql_info;
3322 qfline_T *qfp;
3323 int i, sz = 0;
3324 int prev_fnum = 0;
3325
3326 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3327 {
3328 /* Location list */
3329 qi = GET_LOC_LIST(curwin);
3330 if (qi == NULL)
3331 return 0;
3332 }
3333
3334 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003335 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003336 ++i, qfp = qfp->qf_next)
3337 {
3338 if (qfp->qf_valid)
3339 {
3340 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3341 sz++; /* Count all valid entries */
3342 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3343 {
3344 /* Count the number of files */
3345 sz++;
3346 prev_fnum = qfp->qf_fnum;
3347 }
3348 }
3349 }
3350
3351 return sz;
3352}
3353
3354/*
3355 * Returns the current index of the quickfix/location list.
3356 * Returns 0 if there is an error.
3357 */
3358 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003359qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003360{
3361 qf_info_T *qi = &ql_info;
3362
3363 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3364 {
3365 /* Location list */
3366 qi = GET_LOC_LIST(curwin);
3367 if (qi == NULL)
3368 return 0;
3369 }
3370
3371 return qi->qf_lists[qi->qf_curlist].qf_index;
3372}
3373
3374/*
3375 * Returns the current index in the quickfix/location list (counting only valid
3376 * entries). If no valid entries are in the list, then returns 1.
3377 */
3378 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003379qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003380{
3381 qf_info_T *qi = &ql_info;
3382 qf_list_T *qfl;
3383 qfline_T *qfp;
3384 int i, eidx = 0;
3385 int prev_fnum = 0;
3386
3387 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3388 {
3389 /* Location list */
3390 qi = GET_LOC_LIST(curwin);
3391 if (qi == NULL)
3392 return 1;
3393 }
3394
3395 qfl = &qi->qf_lists[qi->qf_curlist];
3396 qfp = qfl->qf_start;
3397
3398 /* check if the list has valid errors */
3399 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3400 return 1;
3401
3402 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3403 {
3404 if (qfp->qf_valid)
3405 {
3406 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3407 {
3408 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3409 {
3410 /* Count the number of files */
3411 eidx++;
3412 prev_fnum = qfp->qf_fnum;
3413 }
3414 }
3415 else
3416 eidx++;
3417 }
3418 }
3419
3420 return eidx ? eidx : 1;
3421}
3422
3423/*
3424 * Get the 'n'th valid error entry in the quickfix or location list.
3425 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3426 * For :cdo and :ldo returns the 'n'th valid error entry.
3427 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3428 */
3429 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003430qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003431{
3432 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3433 qfline_T *qfp = qfl->qf_start;
3434 int i, eidx;
3435 int prev_fnum = 0;
3436
3437 /* check if the list has valid errors */
3438 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3439 return 1;
3440
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003441 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003442 i++, qfp = qfp->qf_next)
3443 {
3444 if (qfp->qf_valid)
3445 {
3446 if (fdo)
3447 {
3448 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3449 {
3450 /* Count the number of files */
3451 eidx++;
3452 prev_fnum = qfp->qf_fnum;
3453 }
3454 }
3455 else
3456 eidx++;
3457 }
3458
3459 if (eidx == n)
3460 break;
3461 }
3462
3463 if (i <= qfl->qf_count)
3464 return i;
3465 else
3466 return 1;
3467}
3468
3469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003471 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003472 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 */
3474 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003475ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003477 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003478 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003480 if (eap->cmdidx == CMD_ll
3481 || eap->cmdidx == CMD_lrewind
3482 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003483 || eap->cmdidx == CMD_llast
3484 || eap->cmdidx == CMD_ldo
3485 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003486 {
3487 qi = GET_LOC_LIST(curwin);
3488 if (qi == NULL)
3489 {
3490 EMSG(_(e_loclist));
3491 return;
3492 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003493 }
3494
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003495 if (eap->addr_count > 0)
3496 errornr = (int)eap->line2;
3497 else
3498 {
3499 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3500 errornr = 0;
3501 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3502 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3503 errornr = 1;
3504 else
3505 errornr = 32767;
3506 }
3507
3508 /* For cdo and ldo commands, jump to the nth valid error.
3509 * For cfdo and lfdo commands, jump to the nth valid file entry.
3510 */
3511 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3512 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3513 errornr = qf_get_nth_valid_entry(qi,
3514 eap->addr_count > 0 ? (int)eap->line1 : 1,
3515 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3516
3517 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518}
3519
3520/*
3521 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003522 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003523 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 */
3525 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003526ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003528 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003529 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003530
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003531 if (eap->cmdidx == CMD_lnext
3532 || eap->cmdidx == CMD_lNext
3533 || eap->cmdidx == CMD_lprevious
3534 || eap->cmdidx == CMD_lnfile
3535 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003536 || eap->cmdidx == CMD_lpfile
3537 || eap->cmdidx == CMD_ldo
3538 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003539 {
3540 qi = GET_LOC_LIST(curwin);
3541 if (qi == NULL)
3542 {
3543 EMSG(_(e_loclist));
3544 return;
3545 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003546 }
3547
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003548 if (eap->addr_count > 0 &&
3549 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3550 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3551 errornr = (int)eap->line2;
3552 else
3553 errornr = 1;
3554
3555 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3556 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003558 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3559 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003561 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3562 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 ? BACKWARD_FILE
3564 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003565 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003569 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003570 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 */
3572 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003573ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003575 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003576 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003577#ifdef FEAT_AUTOCMD
3578 char_u *au_name = NULL;
3579#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003580
3581 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003582 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003583 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003584
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003585#ifdef FEAT_AUTOCMD
3586 switch (eap->cmdidx)
3587 {
3588 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3589 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3590 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3591 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3592 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3593 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3594 default: break;
3595 }
3596 if (au_name != NULL)
3597 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3598#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003599#ifdef FEAT_BROWSE
3600 if (cmdmod.browse)
3601 {
3602 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3603 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3604 if (browse_file == NULL)
3605 return;
3606 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3607 vim_free(browse_file);
3608 }
3609 else
3610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003612 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003613
3614 /*
3615 * This function is used by the :cfile, :cgetfile and :caddfile
3616 * commands.
3617 * :cfile always creates a new quickfix list and jumps to the
3618 * first error.
3619 * :cgetfile creates a new quickfix list but doesn't jump to the
3620 * first error.
3621 * :caddfile adds to an existing quickfix list. If there is no
3622 * quickfix list then a new list is created.
3623 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003624 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003625 && eap->cmdidx != CMD_laddfile),
3626 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003627 && (eap->cmdidx == CMD_cfile
3628 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003629 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003630#ifdef FEAT_AUTOCMD
3631 if (au_name != NULL)
3632 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3633#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003634 if (wp != NULL)
3635 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003636 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003637 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003638
3639 else
3640 {
3641#ifdef FEAT_AUTOCMD
3642 if (au_name != NULL)
3643 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3644#endif
3645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646}
3647
3648/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003649 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003650 * ":vimgrepadd {pattern} file(s)"
3651 * ":lvimgrep {pattern} file(s)"
3652 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003653 */
3654 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003655ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003656{
Bram Moolenaar81695252004-12-29 20:58:21 +00003657 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003658 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003659 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003660 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003661 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003662 char_u *s;
3663 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003664 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003665 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003666#ifdef FEAT_AUTOCMD
3667 qfline_T *cur_qf_start;
3668#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003669 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003670 buf_T *buf;
3671 int duplicate_name = FALSE;
3672 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003673 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003674 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003675 buf_T *first_match_buf = NULL;
3676 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003677 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003678#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3679 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003680#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003681 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003682 int flags = 0;
3683 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003684 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003685 char_u *dirname_start = NULL;
3686 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003687 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003688#ifdef FEAT_AUTOCMD
3689 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003690
3691 switch (eap->cmdidx)
3692 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003693 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3694 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3695 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003696 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003697 case CMD_grep: au_name = (char_u *)"grep"; break;
3698 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3699 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3700 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003701 default: break;
3702 }
3703 if (au_name != NULL)
3704 {
3705 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3706 curbuf->b_fname, TRUE, curbuf);
3707 if (did_throw || force_abort)
3708 return;
3709 }
3710#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003711
Bram Moolenaar754b5602006-02-09 23:53:20 +00003712 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003713 || eap->cmdidx == CMD_lvimgrep
3714 || eap->cmdidx == CMD_lgrepadd
3715 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003716 {
3717 qi = ll_get_or_alloc_list(curwin);
3718 if (qi == NULL)
3719 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003720 }
3721
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003722 if (eap->addr_count > 0)
3723 tomatch = eap->line2;
3724 else
3725 tomatch = MAXLNUM;
3726
Bram Moolenaar81695252004-12-29 20:58:21 +00003727 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003728 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003729 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003730 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003731 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003732 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003733 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003734 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003735 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003736
3737 if (s != NULL && *s == NUL)
3738 {
3739 /* Pattern is empty, use last search pattern. */
3740 if (last_search_pat() == NULL)
3741 {
3742 EMSG(_(e_noprevre));
3743 goto theend;
3744 }
3745 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3746 }
3747 else
3748 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3749
Bram Moolenaar86b68352004-12-27 21:59:20 +00003750 if (regmatch.regprog == NULL)
3751 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003752 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003753 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003754
3755 p = skipwhite(p);
3756 if (*p == NUL)
3757 {
3758 EMSG(_("E683: File name missing or invalid pattern"));
3759 goto theend;
3760 }
3761
Bram Moolenaar754b5602006-02-09 23:53:20 +00003762 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003763 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003764 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003765 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003766 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003767
3768 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003769 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003770 goto theend;
3771 if (fcount == 0)
3772 {
3773 EMSG(_(e_nomatch));
3774 goto theend;
3775 }
3776
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003777 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3778 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003779 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003780 {
3781 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003782 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003783 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003784
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003785 /* Remember the current directory, because a BufRead autocommand that does
3786 * ":lcd %:p:h" changes the meaning of short path names. */
3787 mch_dirname(dirname_start, MAXPATHL);
3788
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003789#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003790 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003791 * changing the current quickfix list. */
3792 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3793#endif
3794
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003795 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003796 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003797 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003798 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003799 if (time(NULL) > seconds)
3800 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003801 /* Display the file name every second or so, show the user we are
3802 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003803 seconds = time(NULL);
3804 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003805 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003806 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003807 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003808 else
3809 {
3810 msg_outtrans(p);
3811 vim_free(p);
3812 }
3813 msg_clr_eos();
3814 msg_didout = FALSE; /* overwrite this message */
3815 msg_nowait = TRUE; /* don't wait for this message */
3816 msg_col = 0;
3817 out_flush();
3818 }
3819
Bram Moolenaar81695252004-12-29 20:58:21 +00003820 buf = buflist_findname_exp(fnames[fi]);
3821 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3822 {
3823 /* Remember that a buffer with this name already exists. */
3824 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003825 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003826 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003827
3828#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3829 /* Don't do Filetype autocommands to avoid loading syntax and
3830 * indent scripts, a great speed improvement. */
3831 save_ei = au_event_disable(",Filetype");
3832#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003833 /* Don't use modelines here, it's useless. */
3834 save_mls = p_mls;
3835 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003836
3837 /* Load file into a buffer, so that 'fileencoding' is detected,
3838 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003839 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003840
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003841 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003842#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3843 au_event_restore(save_ei);
3844#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003845 }
3846 else
3847 /* Use existing, loaded buffer. */
3848 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003849
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003850#ifdef FEAT_AUTOCMD
3851 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3852 {
3853 int idx;
3854
3855 /* Autocommands changed the quickfix list. Find the one we were
3856 * using and restore it. */
3857 for (idx = 0; idx < LISTCOUNT; ++idx)
3858 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3859 {
3860 qi->qf_curlist = idx;
3861 break;
3862 }
3863 if (idx == LISTCOUNT)
3864 {
3865 /* List cannot be found, create a new one. */
3866 qf_new_list(qi, *eap->cmdlinep);
3867 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3868 }
3869 }
3870#endif
3871
Bram Moolenaar81695252004-12-29 20:58:21 +00003872 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003873 {
3874 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003875 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003876 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003877 else
3878 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003879 /* Try for a match in all lines of the buffer.
3880 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003881 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003882 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3883 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003884 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003885 col = 0;
3886 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003887 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003888 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003889 ;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003890 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003891 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003892 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003893 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003894 ml_get_buf(buf,
3895 regmatch.startpos[0].lnum + lnum, FALSE),
3896 regmatch.startpos[0].lnum + lnum,
3897 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003898 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003899 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003900 0, /* nr */
3901 0, /* type */
3902 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003903 ) == FAIL)
3904 {
3905 got_int = TRUE;
3906 break;
3907 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003908 found_match = TRUE;
3909 if (--tomatch == 0)
3910 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003911 if ((flags & VGR_GLOBAL) == 0
3912 || regmatch.endpos[0].lnum > 0)
3913 break;
3914 col = regmatch.endpos[0].col
3915 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003916 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003917 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003918 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003919 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003920 if (got_int)
3921 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003922 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003923#ifdef FEAT_AUTOCMD
3924 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3925#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003926
3927 if (using_dummy)
3928 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003929 if (found_match && first_match_buf == NULL)
3930 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003931 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003932 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003933 /* Never keep a dummy buffer if there is another buffer
3934 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003935 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003936 buf = NULL;
3937 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003938 else if (!cmdmod.hide
3939 || buf->b_p_bh[0] == 'u' /* "unload" */
3940 || buf->b_p_bh[0] == 'w' /* "wipe" */
3941 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003942 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003943 /* When no match was found we don't need to remember the
3944 * buffer, wipe it out. If there was a match and it
3945 * wasn't the first one or we won't jump there: only
3946 * unload the buffer.
3947 * Ignore 'hidden' here, because it may lead to having too
3948 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003949 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003950 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003951 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003952 buf = NULL;
3953 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003954 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003955 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003956 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003957 buf = NULL;
3958 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003959 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003960
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003961 if (buf != NULL)
3962 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003963 /* If the buffer is still loaded we need to use the
3964 * directory we jumped to below. */
3965 if (buf == first_match_buf
3966 && target_dir == NULL
3967 && STRCMP(dirname_start, dirname_now) != 0)
3968 target_dir = vim_strsave(dirname_now);
3969
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003970 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003971 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003972 * need to be done (again). But not the window-local
3973 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003974 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003975#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003976 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3977 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003978#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003979 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003980 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003981 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003982 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003983 }
3984 }
3985
3986 FreeWild(fcount, fnames);
3987
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003988 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3989 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3990 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003991
3992#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02003993 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003994#endif
3995
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003996#ifdef FEAT_AUTOCMD
3997 if (au_name != NULL)
3998 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3999 curbuf->b_fname, TRUE, curbuf);
4000#endif
4001
Bram Moolenaar86b68352004-12-27 21:59:20 +00004002 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004003 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004004 {
4005 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004006 {
4007 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004008 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004009 if (buf != curbuf)
4010 /* If we jumped to another buffer redrawing will already be
4011 * taken care of. */
4012 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004013
4014 /* Jump to the directory used after loading the buffer. */
4015 if (curbuf == first_match_buf && target_dir != NULL)
4016 {
4017 exarg_T ea;
4018
4019 ea.arg = target_dir;
4020 ea.cmdidx = CMD_lcd;
4021 ex_cd(&ea);
4022 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004023 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004024 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004025 else
4026 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004027
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004028 /* If we loaded a dummy buffer into the current window, the autocommands
4029 * may have messed up things, need to redraw and recompute folds. */
4030 if (redraw_for_dummy)
4031 {
4032#ifdef FEAT_FOLDING
4033 foldUpdateAll(curwin);
4034#else
4035 redraw_later(NOT_VALID);
4036#endif
4037 }
4038
Bram Moolenaar86b68352004-12-27 21:59:20 +00004039theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004040 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004041 vim_free(dirname_now);
4042 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004043 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004044 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004045}
4046
4047/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004048 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00004049 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004050 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
4051 * If "s" is not NULL terminate the pattern with a NUL.
4052 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00004053 */
4054 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004055skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004056{
4057 int c;
4058
4059 if (vim_isIDc(*p))
4060 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004061 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004062 if (s != NULL)
4063 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004064 p = skiptowhite(p);
4065 if (s != NULL && *p != NUL)
4066 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004067 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004068 else
4069 {
4070 /* ":vimgrep /pattern/[g][j] fname" */
4071 if (s != NULL)
4072 *s = p + 1;
4073 c = *p;
4074 p = skip_regexp(p + 1, c, TRUE, NULL);
4075 if (*p != c)
4076 return NULL;
4077
4078 /* Truncate the pattern. */
4079 if (s != NULL)
4080 *p = NUL;
4081 ++p;
4082
4083 /* Find the flags */
4084 while (*p == 'g' || *p == 'j')
4085 {
4086 if (flags != NULL)
4087 {
4088 if (*p == 'g')
4089 *flags |= VGR_GLOBAL;
4090 else
4091 *flags |= VGR_NOJUMP;
4092 }
4093 ++p;
4094 }
4095 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004096 return p;
4097}
4098
4099/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004100 * Restore current working directory to "dirname_start" if they differ, taking
4101 * into account whether it is set locally or globally.
4102 */
4103 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004104restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004105{
4106 char_u *dirname_now = alloc(MAXPATHL);
4107
4108 if (NULL != dirname_now)
4109 {
4110 mch_dirname(dirname_now, MAXPATHL);
4111 if (STRCMP(dirname_start, dirname_now) != 0)
4112 {
4113 /* If the directory has changed, change it back by building up an
4114 * appropriate ex command and executing it. */
4115 exarg_T ea;
4116
4117 ea.arg = dirname_start;
4118 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4119 ex_cd(&ea);
4120 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004121 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004122 }
4123}
4124
4125/*
4126 * Load file "fname" into a dummy buffer and return the buffer pointer,
4127 * placing the directory resulting from the buffer load into the
4128 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4129 * prior to calling this function. Restores directory to "dirname_start" prior
4130 * to returning, if autocmds or the 'autochdir' option have changed it.
4131 *
4132 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4133 * or wipe_dummy_buffer() later!
4134 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004135 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004136 */
4137 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004138load_dummy_buffer(
4139 char_u *fname,
4140 char_u *dirname_start, /* in: old directory */
4141 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004142{
4143 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004144 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004145 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004146 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004147
4148 /* Allocate a buffer without putting it in the buffer list. */
4149 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4150 if (newbuf == NULL)
4151 return NULL;
4152
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004153 /* Init the options. */
4154 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4155
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004156 /* need to open the memfile before putting the buffer in a window */
4157 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004158 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004159 /* set curwin/curbuf to buf and save a few things */
4160 aucmd_prepbuf(&aco, newbuf);
4161
4162 /* Need to set the filename for autocommands. */
4163 (void)setfname(curbuf, fname, NULL, FALSE);
4164
Bram Moolenaar81695252004-12-29 20:58:21 +00004165 /* Create swap file now to avoid the ATTENTION message. */
4166 check_need_swap(TRUE);
4167
4168 /* Remove the "dummy" flag, otherwise autocommands may not
4169 * work. */
4170 curbuf->b_flags &= ~BF_DUMMY;
4171
4172 if (readfile(fname, NULL,
4173 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4174 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004175 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004176 && !(curbuf->b_flags & BF_NEW))
4177 {
4178 failed = FALSE;
4179 if (curbuf != newbuf)
4180 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004181 /* Bloody autocommands changed the buffer! Can happen when
4182 * using netrw and editing a remote file. Use the current
4183 * buffer instead, delete the dummy one after restoring the
4184 * window stuff. */
4185 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004186 newbuf = curbuf;
4187 }
4188 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004189
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004190 /* restore curwin/curbuf and a few other things */
4191 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004192 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
4193 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004194 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004195
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004196 /*
4197 * When autocommands/'autochdir' option changed directory: go back.
4198 * Let the caller know what the resulting dir was first, in case it is
4199 * important.
4200 */
4201 mch_dirname(resulting_dir, MAXPATHL);
4202 restore_start_dir(dirname_start);
4203
Bram Moolenaar81695252004-12-29 20:58:21 +00004204 if (!buf_valid(newbuf))
4205 return NULL;
4206 if (failed)
4207 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004208 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004209 return NULL;
4210 }
4211 return newbuf;
4212}
4213
4214/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004215 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4216 * directory to "dirname_start" prior to returning, if autocmds or the
4217 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004218 */
4219 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004220wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004221{
4222 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004223 {
4224#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4225 cleanup_T cs;
4226
4227 /* Reset the error/interrupt/exception state here so that aborting()
4228 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4229 * work when got_int is set. */
4230 enter_cleanup(&cs);
4231#endif
4232
Bram Moolenaar81695252004-12-29 20:58:21 +00004233 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004234
4235#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4236 /* Restore the error/interrupt/exception state if not discarded by a
4237 * new aborting error, interrupt, or uncaught exception. */
4238 leave_cleanup(&cs);
4239#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004240 /* When autocommands/'autochdir' option changed directory: go back. */
4241 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004242 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004243}
4244
4245/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004246 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4247 * directory to "dirname_start" prior to returning, if autocmds or the
4248 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004249 */
4250 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004251unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004252{
4253 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004254 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004255 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004256
4257 /* When autocommands/'autochdir' option changed directory: go back. */
4258 restore_start_dir(dirname_start);
4259 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004260}
4261
Bram Moolenaar05159a02005-02-26 23:04:13 +00004262#if defined(FEAT_EVAL) || defined(PROTO)
4263/*
4264 * Add each quickfix error to list "list" as a dictionary.
4265 */
4266 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004267get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004268{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004269 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004270 dict_T *dict;
4271 char_u buf[2];
4272 qfline_T *qfp;
4273 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004274 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004275
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004276 if (wp != NULL)
4277 {
4278 qi = GET_LOC_LIST(wp);
4279 if (qi == NULL)
4280 return FAIL;
4281 }
4282
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004283 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004284 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004285 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004286
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004287 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4288 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004289 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004290 /* Handle entries with a non-existing buffer number. */
4291 bufnum = qfp->qf_fnum;
4292 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4293 bufnum = 0;
4294
Bram Moolenaar05159a02005-02-26 23:04:13 +00004295 if ((dict = dict_alloc()) == NULL)
4296 return FAIL;
4297 if (list_append_dict(list, dict) == FAIL)
4298 return FAIL;
4299
4300 buf[0] = qfp->qf_type;
4301 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004302 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004303 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4304 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4305 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4306 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004307 || dict_add_nr_str(dict, "pattern", 0L,
4308 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4309 || dict_add_nr_str(dict, "text", 0L,
4310 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004311 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4312 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4313 return FAIL;
4314
4315 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004316 if (qfp == NULL)
4317 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004318 }
4319 return OK;
4320}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004321
4322/*
4323 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004324 * of dictionaries. "title" will be copied to w:quickfix_title.
4325 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004326 */
4327 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004328set_errorlist(
4329 win_T *wp,
4330 list_T *list,
4331 int action,
4332 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004333{
4334 listitem_T *li;
4335 dict_T *d;
4336 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004337 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004338 long lnum;
4339 int col, nr;
4340 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004341#ifdef FEAT_WINDOWS
4342 qfline_T *old_last = NULL;
4343#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004344 int valid, status;
4345 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004346 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004347 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004348
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004349 if (wp != NULL)
4350 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004351 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004352 if (qi == NULL)
4353 return FAIL;
4354 }
4355
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004356 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004357 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004358 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004359#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004360 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4361 /* Adding to existing list, use last entry. */
4362 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004363#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004364 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004365 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004366 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004367 qf_store_title(qi, title);
4368 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004369
4370 for (li = list->lv_first; li != NULL; li = li->li_next)
4371 {
4372 if (li->li_tv.v_type != VAR_DICT)
4373 continue; /* Skip non-dict items */
4374
4375 d = li->li_tv.vval.v_dict;
4376 if (d == NULL)
4377 continue;
4378
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004379 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004380 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004381 lnum = get_dict_number(d, (char_u *)"lnum");
4382 col = get_dict_number(d, (char_u *)"col");
4383 vcol = get_dict_number(d, (char_u *)"vcol");
4384 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004385 type = get_dict_string(d, (char_u *)"type", TRUE);
4386 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4387 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004388 if (text == NULL)
4389 text = vim_strsave((char_u *)"");
4390
4391 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004392 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004393 valid = FALSE;
4394
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004395 /* Mark entries with non-existing buffer number as not valid. Give the
4396 * error message only once. */
4397 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4398 {
4399 if (!did_bufnr_emsg)
4400 {
4401 did_bufnr_emsg = TRUE;
4402 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4403 }
4404 valid = FALSE;
4405 bufnum = 0;
4406 }
4407
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004408 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004409 NULL, /* dir */
4410 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004411 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004412 text,
4413 lnum,
4414 col,
4415 vcol, /* vis_col */
4416 pattern, /* search pattern */
4417 nr,
4418 type == NULL ? NUL : *type,
4419 valid);
4420
4421 vim_free(filename);
4422 vim_free(pattern);
4423 vim_free(text);
4424 vim_free(type);
4425
4426 if (status == FAIL)
4427 {
4428 retval = FAIL;
4429 break;
4430 }
4431 }
4432
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004433 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004434 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004435 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4436 else
4437 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004438 if (action != 'a') {
4439 qi->qf_lists[qi->qf_curlist].qf_ptr =
4440 qi->qf_lists[qi->qf_curlist].qf_start;
4441 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4442 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4443 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004444
4445#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004446 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004447 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004448#endif
4449
4450 return retval;
4451}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004452#endif
4453
Bram Moolenaar81695252004-12-29 20:58:21 +00004454/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004455 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004456 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004457 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004458 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004459 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004460 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004461 */
4462 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004463ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004464{
4465 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004466 qf_info_T *qi = &ql_info;
4467
Bram Moolenaardb552d602006-03-23 22:59:57 +00004468 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4469 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004470 {
4471 qi = ll_get_or_alloc_list(curwin);
4472 if (qi == NULL)
4473 return;
4474 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004475
4476 if (*eap->arg == NUL)
4477 buf = curbuf;
4478 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4479 buf = buflist_findnr(atoi((char *)eap->arg));
4480 if (buf == NULL)
4481 EMSG(_(e_invarg));
4482 else if (buf->b_ml.ml_mfp == NULL)
4483 EMSG(_("E681: Buffer is not loaded"));
4484 else
4485 {
4486 if (eap->addr_count == 0)
4487 {
4488 eap->line1 = 1;
4489 eap->line2 = buf->b_ml.ml_line_count;
4490 }
4491 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4492 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4493 EMSG(_(e_invrange));
4494 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004495 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004496 char_u *qf_title = *eap->cmdlinep;
4497
4498 if (buf->b_sfname)
4499 {
4500 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4501 (char *)qf_title, (char *)buf->b_sfname);
4502 qf_title = IObuff;
4503 }
4504
Bram Moolenaardb552d602006-03-23 22:59:57 +00004505 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4506 (eap->cmdidx != CMD_caddbuffer
4507 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004508 eap->line1, eap->line2,
4509 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004510 && (eap->cmdidx == CMD_cbuffer
4511 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004512 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4513 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004514 }
4515}
4516
Bram Moolenaar1e015462005-09-25 22:16:38 +00004517#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004518/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004519 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4520 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004521 */
4522 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004523ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004524{
4525 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004526 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004527
Bram Moolenaardb552d602006-03-23 22:59:57 +00004528 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4529 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004530 {
4531 qi = ll_get_or_alloc_list(curwin);
4532 if (qi == NULL)
4533 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004534 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004535
Bram Moolenaar4770d092006-01-12 23:22:24 +00004536 /* Evaluate the expression. When the result is a string or a list we can
4537 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004538 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004539 if (tv != NULL)
4540 {
4541 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4542 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4543 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004544 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004545 (eap->cmdidx != CMD_caddexpr
4546 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004547 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004548 && (eap->cmdidx == CMD_cexpr
4549 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004550 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004551 }
4552 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004553 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004554 free_tv(tv);
4555 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004556}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004557#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004558
4559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 * ":helpgrep {pattern}"
4561 */
4562 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004563ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564{
4565 regmatch_T regmatch;
4566 char_u *save_cpo;
4567 char_u *p;
4568 int fcount;
4569 char_u **fnames;
4570 FILE *fd;
4571 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004573#ifdef FEAT_MULTI_LANG
4574 char_u *lang;
4575#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004576 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004577 int new_qi = FALSE;
4578 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004579#ifdef FEAT_AUTOCMD
4580 char_u *au_name = NULL;
4581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004583#ifdef FEAT_MULTI_LANG
4584 /* Check for a specified language */
4585 lang = check_help_lang(eap->arg);
4586#endif
4587
Bram Moolenaar73633f82012-01-20 13:39:07 +01004588#ifdef FEAT_AUTOCMD
4589 switch (eap->cmdidx)
4590 {
4591 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4592 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4593 default: break;
4594 }
4595 if (au_name != NULL)
4596 {
4597 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4598 curbuf->b_fname, TRUE, curbuf);
4599 if (did_throw || force_abort)
4600 return;
4601 }
4602#endif
4603
4604 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4605 save_cpo = p_cpo;
4606 p_cpo = empty_option;
4607
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004608 if (eap->cmdidx == CMD_lhelpgrep)
4609 {
4610 /* Find an existing help window */
4611 FOR_ALL_WINDOWS(wp)
4612 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4613 break;
4614
4615 if (wp == NULL) /* Help window not found */
4616 qi = NULL;
4617 else
4618 qi = wp->w_llist;
4619
4620 if (qi == NULL)
4621 {
4622 /* Allocate a new location list for help text matches */
4623 if ((qi = ll_new_list()) == NULL)
4624 return;
4625 new_qi = TRUE;
4626 }
4627 }
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4630 regmatch.rm_ic = FALSE;
4631 if (regmatch.regprog != NULL)
4632 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004633#ifdef FEAT_MBYTE
4634 vimconv_T vc;
4635
4636 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4637 * differs. */
4638 vc.vc_type = CONV_NONE;
4639 if (!enc_utf8)
4640 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4641#endif
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004644 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
4646 /* Go through all directories in 'runtimepath' */
4647 p = p_rtp;
4648 while (*p != NUL && !got_int)
4649 {
4650 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4651
4652 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4653 add_pathsep(NameBuff);
4654 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4655 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4656 &fnames, EW_FILE|EW_SILENT) == OK
4657 && fcount > 0)
4658 {
4659 for (fi = 0; fi < fcount && !got_int; ++fi)
4660 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004661#ifdef FEAT_MULTI_LANG
4662 /* Skip files for a different language. */
4663 if (lang != NULL
4664 && STRNICMP(lang, fnames[fi]
4665 + STRLEN(fnames[fi]) - 3, 2) != 0
4666 && !(STRNICMP(lang, "en", 2) == 0
4667 && STRNICMP("txt", fnames[fi]
4668 + STRLEN(fnames[fi]) - 3, 3) == 0))
4669 continue;
4670#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004671 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (fd != NULL)
4673 {
4674 lnum = 1;
4675 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4676 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004677 char_u *line = IObuff;
4678#ifdef FEAT_MBYTE
4679 /* Convert a line if 'encoding' is not utf-8 and
4680 * the line contains a non-ASCII character. */
4681 if (vc.vc_type != CONV_NONE
4682 && has_non_ascii(IObuff)) {
4683 line = string_convert(&vc, IObuff, NULL);
4684 if (line == NULL)
4685 line = IObuff;
4686 }
4687#endif
4688
4689 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004691 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692
4693 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004694 while (l > 0 && line[l - 1] <= ' ')
4695 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004697 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 NULL, /* dir */
4699 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004700 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004701 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004703 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004704 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004705 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004706 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 0, /* nr */
4708 1, /* type */
4709 TRUE /* valid */
4710 ) == FAIL)
4711 {
4712 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004713#ifdef FEAT_MBYTE
4714 if (line != IObuff)
4715 vim_free(line);
4716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 break;
4718 }
4719 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004720#ifdef FEAT_MBYTE
4721 if (line != IObuff)
4722 vim_free(line);
4723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 ++lnum;
4725 line_breakcheck();
4726 }
4727 fclose(fd);
4728 }
4729 }
4730 FreeWild(fcount, fnames);
4731 }
4732 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004733
Bram Moolenaar473de612013-06-08 18:19:48 +02004734 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004735#ifdef FEAT_MBYTE
4736 if (vc.vc_type != CONV_NONE)
4737 convert_setup(&vc, NULL, NULL);
4738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004740 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4741 qi->qf_lists[qi->qf_curlist].qf_ptr =
4742 qi->qf_lists[qi->qf_curlist].qf_start;
4743 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004746 if (p_cpo == empty_option)
4747 p_cpo = save_cpo;
4748 else
4749 /* Darn, some plugin changed the value. */
4750 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004753 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754#endif
4755
Bram Moolenaar73633f82012-01-20 13:39:07 +01004756#ifdef FEAT_AUTOCMD
4757 if (au_name != NULL)
4758 {
4759 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4760 curbuf->b_fname, TRUE, curbuf);
4761 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4762 /* autocommands made "qi" invalid */
4763 return;
4764 }
4765#endif
4766
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004768 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004769 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004770 else
4771 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004772
4773 if (eap->cmdidx == CMD_lhelpgrep)
4774 {
4775 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004776 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004777 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4778 {
4779 if (new_qi)
4780 ll_free_all(&qi);
4781 }
4782 else if (curwin->w_llist == NULL)
4783 curwin->w_llist = qi;
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785}
4786
4787#endif /* FEAT_QUICKFIX */