blob: 466c5c2e81e43c45cddb4180cd87d22ba8f4714b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaard12f5c12006-01-25 22:10:52 +000050typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000051{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000052 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020053 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_ptr; /* pointer to the current error */
55 int qf_count; /* number of errors (0 means no error list) */
56 int qf_index; /* current index in the error list */
57 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020058 char_u *qf_title; /* title derived from the command that created
59 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000060} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaard12f5c12006-01-25 22:10:52 +000062struct qf_info_S
63{
64 /*
65 * Count of references to this list. Used only for location lists.
66 * When a location list window reference this list, qf_refcount
67 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
68 * reaches 0, the list is freed.
69 */
70 int qf_refcount;
71 int qf_listcount; /* current number of lists */
72 int qf_curlist; /* current error list */
73 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar361c8f02016-07-02 15:41:47 +020074
75 int qf_dir_curlist; /* error list for qf_dir_stack */
76 struct dir_stack_T *qf_dir_stack;
77 char_u *qf_directory;
78 struct dir_stack_T *qf_file_stack;
79 char_u *qf_currfile;
80 int qf_multiline;
81 int qf_multiignore;
82 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000083};
84
85static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaar68b76a62005-03-25 21:53:48 +000087#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89/*
90 * Structure used to hold the info of one part of 'errorformat'
91 */
Bram Moolenaar01265852006-03-20 21:50:15 +000092typedef struct efm_S efm_T;
93struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000094{
95 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000096 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
98 char_u prefix; /* prefix of this format line: */
99 /* 'D' enter directory */
100 /* 'X' leave directory */
101 /* 'A' start of multi-line message */
102 /* 'E' error message */
103 /* 'W' warning message */
104 /* 'I' informational message */
105 /* 'C' continuation line */
106 /* 'Z' end of multi-line message */
107 /* 'G' general, unspecific message */
108 /* 'P' push file (partial) message */
109 /* 'Q' pop/quit file (partial) message */
110 /* 'O' overread (partial) message */
111 char_u flags; /* additional flags given in prefix */
112 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000113 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115};
116
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100117static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
118
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100119static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title, char_u *enc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void qf_store_title(qf_info_T *qi, char_u *title);
121static void qf_new_list(qf_info_T *qi, char_u *qf_title);
122static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200123static int qf_add_entry(qf_info_T *qi, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100125static void qf_free(qf_info_T *qi, int idx);
126static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200127static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
128static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100129static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200130static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
132static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
135static int is_qf_win(win_T *win, qf_info_T *qi);
136static win_T *qf_find_win(qf_info_T *qi);
137static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200138static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200140static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static char_u *get_mef_name(void);
143static void restore_start_dir(char_u *dirname_start);
144static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
145static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
146static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
147static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000149/* Quickfix window check helper macro */
150#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
151/* Location list window check helper macro */
152#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
153/*
154 * Return location list for window 'wp'
155 * For location list window, return the referenced location list
156 */
157#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000160 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200161 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 * Return -1 for error, number of errors for success.
163 */
164 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100165qf_init(
166 win_T *wp,
167 char_u *efile,
168 char_u *errorformat,
169 int newlist, /* TRUE: start a new error list */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100170 char_u *qf_title,
171 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000173 qf_info_T *qi = &ql_info;
174
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000175 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000176 {
177 qi = ll_get_or_alloc_list(wp);
178 if (qi == NULL)
179 return FAIL;
180 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000181
182 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200183 (linenr_T)0, (linenr_T)0,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100184 qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000185}
186
187/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200188 * Maximum number of bytes allowed per line while reading a errorfile.
189 */
190#define LINE_MAXLEN 4096
191
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200192static struct fmtpattern
193{
194 char_u convchar;
195 char *pattern;
196} fmt_pat[FMT_PATTERNS] =
197 {
198 {'f', ".\\+"}, /* only used when at end */
199 {'n', "\\d\\+"},
200 {'l', "\\d\\+"},
201 {'c', "\\d\\+"},
202 {'t', "."},
203 {'m', ".\\+"},
204 {'r', ".*"},
205 {'p', "[- .]*"},
206 {'v', "\\d\\+"},
207 {'s', ".\\+"}
208 };
209
210/*
211 * Converts a 'errorformat' string to regular expression pattern
212 */
213 static int
214efm_to_regpat(
215 char_u *efm,
216 int len,
217 efm_T *fmt_ptr,
218 char_u *regpat,
219 char_u *errmsg)
220{
221 char_u *ptr;
222 char_u *efmp;
223 char_u *srcptr;
224 int round;
225 int idx = 0;
226
227 /*
228 * Build regexp pattern from current 'errorformat' option
229 */
230 ptr = regpat;
231 *ptr++ = '^';
232 round = 0;
233 for (efmp = efm; efmp < efm + len; ++efmp)
234 {
235 if (*efmp == '%')
236 {
237 ++efmp;
238 for (idx = 0; idx < FMT_PATTERNS; ++idx)
239 if (fmt_pat[idx].convchar == *efmp)
240 break;
241 if (idx < FMT_PATTERNS)
242 {
243 if (fmt_ptr->addr[idx])
244 {
245 sprintf((char *)errmsg,
246 _("E372: Too many %%%c in format string"), *efmp);
247 EMSG(errmsg);
248 return -1;
249 }
250 if ((idx
251 && idx < 6
252 && vim_strchr((char_u *)"DXOPQ",
253 fmt_ptr->prefix) != NULL)
254 || (idx == 6
255 && vim_strchr((char_u *)"OPQ",
256 fmt_ptr->prefix) == NULL))
257 {
258 sprintf((char *)errmsg,
259 _("E373: Unexpected %%%c in format string"), *efmp);
260 EMSG(errmsg);
261 return -1;
262 }
263 fmt_ptr->addr[idx] = (char_u)++round;
264 *ptr++ = '\\';
265 *ptr++ = '(';
266#ifdef BACKSLASH_IN_FILENAME
267 if (*efmp == 'f')
268 {
269 /* Also match "c:" in the file name, even when
270 * checking for a colon next: "%f:".
271 * "\%(\a:\)\=" */
272 STRCPY(ptr, "\\%(\\a:\\)\\=");
273 ptr += 10;
274 }
275#endif
276 if (*efmp == 'f' && efmp[1] != NUL)
277 {
278 if (efmp[1] != '\\' && efmp[1] != '%')
279 {
280 /* A file name may contain spaces, but this isn't
281 * in "\f". For "%f:%l:%m" there may be a ":" in
282 * the file name. Use ".\{-1,}x" instead (x is
283 * the next character), the requirement that :999:
284 * follows should work. */
285 STRCPY(ptr, ".\\{-1,}");
286 ptr += 7;
287 }
288 else
289 {
290 /* File name followed by '\\' or '%': include as
291 * many file name chars as possible. */
292 STRCPY(ptr, "\\f\\+");
293 ptr += 4;
294 }
295 }
296 else
297 {
298 srcptr = (char_u *)fmt_pat[idx].pattern;
299 while ((*ptr = *srcptr++) != NUL)
300 ++ptr;
301 }
302 *ptr++ = '\\';
303 *ptr++ = ')';
304 }
305 else if (*efmp == '*')
306 {
307 if (*++efmp == '[' || *efmp == '\\')
308 {
309 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
310 {
311 if (efmp[1] == '^')
312 *ptr++ = *++efmp;
313 if (efmp < efm + len)
314 {
315 *ptr++ = *++efmp; /* could be ']' */
316 while (efmp < efm + len
317 && (*ptr++ = *++efmp) != ']')
318 /* skip */;
319 if (efmp == efm + len)
320 {
321 EMSG(_("E374: Missing ] in format string"));
322 return -1;
323 }
324 }
325 }
326 else if (efmp < efm + len) /* %*\D, %*\s etc. */
327 *ptr++ = *++efmp;
328 *ptr++ = '\\';
329 *ptr++ = '+';
330 }
331 else
332 {
333 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
334 sprintf((char *)errmsg,
335 _("E375: Unsupported %%%c in format string"), *efmp);
336 EMSG(errmsg);
337 return -1;
338 }
339 }
340 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
341 *ptr++ = *efmp; /* regexp magic characters */
342 else if (*efmp == '#')
343 *ptr++ = '*';
344 else if (*efmp == '>')
345 fmt_ptr->conthere = TRUE;
346 else if (efmp == efm + 1) /* analyse prefix */
347 {
348 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
349 fmt_ptr->flags = *efmp++;
350 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
351 fmt_ptr->prefix = *efmp;
352 else
353 {
354 sprintf((char *)errmsg,
355 _("E376: Invalid %%%c in format string prefix"), *efmp);
356 EMSG(errmsg);
357 return -1;
358 }
359 }
360 else
361 {
362 sprintf((char *)errmsg,
363 _("E377: Invalid %%%c in format string"), *efmp);
364 EMSG(errmsg);
365 return -1;
366 }
367 }
368 else /* copy normal character */
369 {
370 if (*efmp == '\\' && efmp + 1 < efm + len)
371 ++efmp;
372 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
373 *ptr++ = '\\'; /* escape regexp atoms */
374 if (*efmp)
375 *ptr++ = *efmp;
376 }
377 }
378 *ptr++ = '$';
379 *ptr = NUL;
380
381 return 0;
382}
383
384 static void
385free_efm_list(efm_T **efm_first)
386{
387 efm_T *efm_ptr;
388
389 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
390 {
391 *efm_first = efm_ptr->next;
392 vim_regfree(efm_ptr->prog);
393 vim_free(efm_ptr);
394 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100395 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200396}
397
398/* Parse 'errorformat' option */
399 static efm_T *
400parse_efm_option(char_u *efm)
401{
402 char_u *errmsg = NULL;
403 int errmsglen;
404 efm_T *fmt_ptr = NULL;
405 efm_T *fmt_first = NULL;
406 efm_T *fmt_last = NULL;
407 char_u *fmtstr = NULL;
408 int len;
409 int i;
410 int round;
411
412 errmsglen = CMDBUFFSIZE + 1;
413 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
414 if (errmsg == NULL)
415 goto parse_efm_end;
416
417 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200418 * Each part of the format string is copied and modified from errorformat
419 * to regex prog. Only a few % characters are allowed.
420 */
421
422 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200423 * Get some space to modify the format string into.
424 */
425 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
426 for (round = FMT_PATTERNS; round > 0; )
427 i += (int)STRLEN(fmt_pat[--round].pattern);
428#ifdef COLON_IN_FILENAME
429 i += 12; /* "%f" can become twelve chars longer */
430#else
431 i += 2; /* "%f" can become two chars longer */
432#endif
433 if ((fmtstr = alloc(i)) == NULL)
434 goto parse_efm_error;
435
436 while (efm[0] != NUL)
437 {
438 /*
439 * Allocate a new eformat structure and put it at the end of the list
440 */
441 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
442 if (fmt_ptr == NULL)
443 goto parse_efm_error;
444 if (fmt_first == NULL) /* first one */
445 fmt_first = fmt_ptr;
446 else
447 fmt_last->next = fmt_ptr;
448 fmt_last = fmt_ptr;
449
450 /*
451 * Isolate one part in the 'errorformat' option
452 */
453 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
454 if (efm[len] == '\\' && efm[len + 1] != NUL)
455 ++len;
456
457 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
458 goto parse_efm_error;
459 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
460 goto parse_efm_error;
461 /*
462 * Advance to next part
463 */
464 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
465 }
466
467 if (fmt_first == NULL) /* nothing found */
468 EMSG(_("E378: 'errorformat' contains no pattern"));
469
470 goto parse_efm_end;
471
472parse_efm_error:
473 free_efm_list(&fmt_first);
474
475parse_efm_end:
476 vim_free(fmtstr);
477 vim_free(errmsg);
478
479 return fmt_first;
480}
481
Bram Moolenaare0d37972016-07-15 22:36:01 +0200482enum {
483 QF_FAIL = 0,
484 QF_OK = 1,
485 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200486 QF_NOMEM = 3,
487 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200488};
489
490typedef struct {
491 char_u *linebuf;
492 int linelen;
493 char_u *growbuf;
494 int growbufsiz;
495 FILE *fd;
496 typval_T *tv;
497 char_u *p_str;
498 listitem_T *p_li;
499 buf_T *buf;
500 linenr_T buflnum;
501 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100502 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200503} qfstate_T;
504
505 static char_u *
506qf_grow_linebuf(qfstate_T *state, int newsz)
507{
508 /*
509 * If the line exceeds LINE_MAXLEN exclude the last
510 * byte since it's not a NL character.
511 */
512 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
513 if (state->growbuf == NULL)
514 {
515 state->growbuf = alloc(state->linelen + 1);
516 if (state->growbuf == NULL)
517 return NULL;
518 state->growbufsiz = state->linelen;
519 }
520 else if (state->linelen > state->growbufsiz)
521 {
522 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
523 if (state->growbuf == NULL)
524 return NULL;
525 state->growbufsiz = state->linelen;
526 }
527 return state->growbuf;
528}
529
530/*
531 * Get the next string (separated by newline) from state->p_str.
532 */
533 static int
534qf_get_next_str_line(qfstate_T *state)
535{
536 /* Get the next line from the supplied string */
537 char_u *p_str = state->p_str;
538 char_u *p;
539 int len;
540
541 if (*p_str == NUL) /* Reached the end of the string */
542 return QF_END_OF_INPUT;
543
544 p = vim_strchr(p_str, '\n');
545 if (p != NULL)
546 len = (int)(p - p_str) + 1;
547 else
548 len = (int)STRLEN(p_str);
549
550 if (len > IOSIZE - 2)
551 {
552 state->linebuf = qf_grow_linebuf(state, len);
553 if (state->linebuf == NULL)
554 return QF_NOMEM;
555 }
556 else
557 {
558 state->linebuf = IObuff;
559 state->linelen = len;
560 }
561 vim_strncpy(state->linebuf, p_str, state->linelen);
562
563 /*
564 * Increment using len in order to discard the rest of the
565 * line if it exceeds LINE_MAXLEN.
566 */
567 p_str += len;
568 state->p_str = p_str;
569
570 return QF_OK;
571}
572
573/*
574 * Get the next string from state->p_Li.
575 */
576 static int
577qf_get_next_list_line(qfstate_T *state)
578{
579 listitem_T *p_li = state->p_li;
580 int len;
581
582 while (p_li != NULL
583 && (p_li->li_tv.v_type != VAR_STRING
584 || p_li->li_tv.vval.v_string == NULL))
585 p_li = p_li->li_next; /* Skip non-string items */
586
587 if (p_li == NULL) /* End of the list */
588 {
589 state->p_li = NULL;
590 return QF_END_OF_INPUT;
591 }
592
593 len = (int)STRLEN(p_li->li_tv.vval.v_string);
594 if (len > IOSIZE - 2)
595 {
596 state->linebuf = qf_grow_linebuf(state, len);
597 if (state->linebuf == NULL)
598 return QF_NOMEM;
599 }
600 else
601 {
602 state->linebuf = IObuff;
603 state->linelen = len;
604 }
605
606 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
607
608 state->p_li = p_li->li_next; /* next item */
609 return QF_OK;
610}
611
612/*
613 * Get the next string from state->buf.
614 */
615 static int
616qf_get_next_buf_line(qfstate_T *state)
617{
618 char_u *p_buf = NULL;
619 int len;
620
621 /* Get the next line from the supplied buffer */
622 if (state->buflnum > state->lnumlast)
623 return QF_END_OF_INPUT;
624
625 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
626 state->buflnum += 1;
627
628 len = (int)STRLEN(p_buf);
629 if (len > IOSIZE - 2)
630 {
631 state->linebuf = qf_grow_linebuf(state, len);
632 if (state->linebuf == NULL)
633 return QF_NOMEM;
634 }
635 else
636 {
637 state->linebuf = IObuff;
638 state->linelen = len;
639 }
640 vim_strncpy(state->linebuf, p_buf, state->linelen);
641
642 return QF_OK;
643}
644
645/*
646 * Get the next string from file state->fd.
647 */
648 static int
649qf_get_next_file_line(qfstate_T *state)
650{
651 int discard;
652 int growbuflen;
653
654 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
655 return QF_END_OF_INPUT;
656
657 discard = FALSE;
658 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200659 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200660 {
661 /*
662 * The current line exceeds IObuff, continue reading using
663 * growbuf until EOL or LINE_MAXLEN bytes is read.
664 */
665 if (state->growbuf == NULL)
666 {
667 state->growbufsiz = 2 * (IOSIZE - 1);
668 state->growbuf = alloc(state->growbufsiz);
669 if (state->growbuf == NULL)
670 return QF_NOMEM;
671 }
672
673 /* Copy the read part of the line, excluding null-terminator */
674 memcpy(state->growbuf, IObuff, IOSIZE - 1);
675 growbuflen = state->linelen;
676
677 for (;;)
678 {
679 if (fgets((char *)state->growbuf + growbuflen,
680 state->growbufsiz - growbuflen, state->fd) == NULL)
681 break;
682 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
683 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200684 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200685 break;
686 if (state->growbufsiz == LINE_MAXLEN)
687 {
688 discard = TRUE;
689 break;
690 }
691
692 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
693 ? 2 * state->growbufsiz : LINE_MAXLEN;
694 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
695 if (state->growbuf == NULL)
696 return QF_NOMEM;
697 }
698
699 while (discard)
700 {
701 /*
702 * The current line is longer than LINE_MAXLEN, continue
703 * reading but discard everything until EOL or EOF is
704 * reached.
705 */
706 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
707 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200708 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200709 break;
710 }
711
712 state->linebuf = state->growbuf;
713 state->linelen = growbuflen;
714 }
715 else
716 state->linebuf = IObuff;
717
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100718#ifdef FEAT_MBYTE
719 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200720 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
721 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100722 char_u *line;
723
724 line = string_convert(&state->vc, state->linebuf, &state->linelen);
725 if (line != NULL)
726 {
727 if (state->linelen < IOSIZE)
728 {
729 STRCPY(state->linebuf, line);
730 vim_free(line);
731 }
732 else
733 {
734 vim_free(state->growbuf);
735 state->linebuf = state->growbuf = line;
736 state->growbufsiz = state->linelen < LINE_MAXLEN
737 ? state->linelen : LINE_MAXLEN;
738 }
739 }
740 }
741#endif
742
Bram Moolenaare0d37972016-07-15 22:36:01 +0200743 return QF_OK;
744}
745
746/*
747 * Get the next string from a file/buffer/list/string.
748 */
749 static int
750qf_get_nextline(qfstate_T *state)
751{
752 int status = QF_FAIL;
753
754 if (state->fd == NULL)
755 {
756 if (state->tv != NULL)
757 {
758 if (state->tv->v_type == VAR_STRING)
759 /* Get the next line from the supplied string */
760 status = qf_get_next_str_line(state);
761 else if (state->tv->v_type == VAR_LIST)
762 /* Get the next line from the supplied list */
763 status = qf_get_next_list_line(state);
764 }
765 else
766 /* Get the next line from the supplied buffer */
767 status = qf_get_next_buf_line(state);
768 }
769 else
770 /* Get the next line from the supplied file */
771 status = qf_get_next_file_line(state);
772
773 if (status != QF_OK)
774 return status;
775
776 /* remove newline/CR from the line */
777 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200778 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200779 state->linebuf[state->linelen - 1] = NUL;
780#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200781 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
782 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200783#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200784 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200785
786#ifdef FEAT_MBYTE
787 remove_bom(state->linebuf);
788#endif
789
790 return QF_OK;
791}
792
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200793typedef struct {
794 char_u *namebuf;
795 char_u *errmsg;
796 int errmsglen;
797 long lnum;
798 int col;
799 char_u use_viscol;
800 char_u *pattern;
801 int enr;
802 int type;
803 int valid;
804} qffields_T;
805
806/*
807 * Parse a line and get the quickfix fields.
808 * Return the QF_ status.
809 */
810 static int
811qf_parse_line(
812 qf_info_T *qi,
813 char_u *linebuf,
814 int linelen,
815 efm_T *fmt_first,
816 qffields_T *fields)
817{
818 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200819 char_u *ptr;
820 int len;
821 int i;
822 int idx = 0;
823 char_u *tail = NULL;
824 regmatch_T regmatch;
825
826 /* Always ignore case when looking for a matching error. */
827 regmatch.rm_ic = TRUE;
828
829 /* If there was no %> item start at the first pattern */
830 if (fmt_start == NULL)
831 fmt_ptr = fmt_first;
832 else
833 {
834 fmt_ptr = fmt_start;
835 fmt_start = NULL;
836 }
837
838 /*
839 * Try to match each part of 'errorformat' until we find a complete
840 * match or no match.
841 */
842 fields->valid = TRUE;
843restofline:
844 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
845 {
846 int r;
847
848 idx = fmt_ptr->prefix;
849 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
850 continue;
851 fields->namebuf[0] = NUL;
852 fields->pattern[0] = NUL;
853 if (!qi->qf_multiscan)
854 fields->errmsg[0] = NUL;
855 fields->lnum = 0;
856 fields->col = 0;
857 fields->use_viscol = FALSE;
858 fields->enr = -1;
859 fields->type = 0;
860 tail = NULL;
861
862 regmatch.regprog = fmt_ptr->prog;
863 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
864 fmt_ptr->prog = regmatch.regprog;
865 if (r)
866 {
867 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
868 continue;
869 if (vim_strchr((char_u *)"EWI", idx) != NULL)
870 fields->type = idx;
871 else
872 fields->type = 0;
873 /*
874 * Extract error message data from matched line.
875 * We check for an actual submatch, because "\[" and "\]" in
876 * the 'errorformat' may cause the wrong submatch to be used.
877 */
878 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
879 {
880 int c;
881
882 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
883 continue;
884
885 /* Expand ~/file and $HOME/file to full path. */
886 c = *regmatch.endp[i];
887 *regmatch.endp[i] = NUL;
888 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
889 *regmatch.endp[i] = c;
890
891 if (vim_strchr((char_u *)"OPQ", idx) != NULL
892 && mch_getperm(fields->namebuf) == -1)
893 continue;
894 }
895 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
896 {
897 if (regmatch.startp[i] == NULL)
898 continue;
899 fields->enr = (int)atol((char *)regmatch.startp[i]);
900 }
901 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
902 {
903 if (regmatch.startp[i] == NULL)
904 continue;
905 fields->lnum = atol((char *)regmatch.startp[i]);
906 }
907 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
908 {
909 if (regmatch.startp[i] == NULL)
910 continue;
911 fields->col = (int)atol((char *)regmatch.startp[i]);
912 }
913 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
914 {
915 if (regmatch.startp[i] == NULL)
916 continue;
917 fields->type = *regmatch.startp[i];
918 }
919 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
920 {
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200921 if (linelen > fields->errmsglen)
922 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200923 /* linelen + null terminator */
924 if ((fields->errmsg = vim_realloc(fields->errmsg,
925 linelen + 1)) == NULL)
926 return QF_NOMEM;
927 fields->errmsglen = linelen + 1;
928 }
929 vim_strncpy(fields->errmsg, linebuf, linelen);
930 }
931 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
932 {
933 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
934 continue;
935 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200936 if (len > fields->errmsglen)
937 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200938 /* len + null terminator */
939 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
940 == NULL)
941 return QF_NOMEM;
942 fields->errmsglen = len + 1;
943 }
944 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
945 }
946 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
947 {
948 if (regmatch.startp[i] == NULL)
949 continue;
950 tail = regmatch.startp[i];
951 }
952 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
953 {
954 char_u *match_ptr;
955
956 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
957 continue;
958 fields->col = 0;
959 for (match_ptr = regmatch.startp[i];
960 match_ptr != regmatch.endp[i]; ++match_ptr)
961 {
962 ++fields->col;
963 if (*match_ptr == TAB)
964 {
965 fields->col += 7;
966 fields->col -= fields->col % 8;
967 }
968 }
969 ++fields->col;
970 fields->use_viscol = TRUE;
971 }
972 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
973 {
974 if (regmatch.startp[i] == NULL)
975 continue;
976 fields->col = (int)atol((char *)regmatch.startp[i]);
977 fields->use_viscol = TRUE;
978 }
979 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
980 {
981 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
982 continue;
983 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
984 if (len > CMDBUFFSIZE - 5)
985 len = CMDBUFFSIZE - 5;
986 STRCPY(fields->pattern, "^\\V");
987 STRNCAT(fields->pattern, regmatch.startp[i], len);
988 fields->pattern[len + 3] = '\\';
989 fields->pattern[len + 4] = '$';
990 fields->pattern[len + 5] = NUL;
991 }
992 break;
993 }
994 }
995 qi->qf_multiscan = FALSE;
996
997 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
998 {
999 if (fmt_ptr != NULL)
1000 {
1001 if (idx == 'D') /* enter directory */
1002 {
1003 if (*fields->namebuf == NUL)
1004 {
1005 EMSG(_("E379: Missing or empty directory name"));
1006 return QF_FAIL;
1007 }
1008 qi->qf_directory =
1009 qf_push_dir(fields->namebuf, &qi->qf_dir_stack, FALSE);
1010 if (qi->qf_directory == NULL)
1011 return QF_FAIL;
1012 }
1013 else if (idx == 'X') /* leave directory */
1014 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
1015 }
1016 fields->namebuf[0] = NUL; /* no match found, remove file name */
1017 fields->lnum = 0; /* don't jump to this line */
1018 fields->valid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001019 if (linelen > fields->errmsglen)
1020 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001021 /* linelen + null terminator */
1022 if ((fields->errmsg = vim_realloc(fields->errmsg,
1023 linelen + 1)) == NULL)
1024 return QF_NOMEM;
1025 fields->errmsglen = linelen + 1;
1026 }
1027 /* copy whole line to error message */
1028 vim_strncpy(fields->errmsg, linebuf, linelen);
1029 if (fmt_ptr == NULL)
1030 qi->qf_multiline = qi->qf_multiignore = FALSE;
1031 }
1032 else if (fmt_ptr != NULL)
1033 {
1034 /* honor %> item */
1035 if (fmt_ptr->conthere)
1036 fmt_start = fmt_ptr;
1037
1038 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1039 {
1040 qi->qf_multiline = TRUE; /* start of a multi-line message */
1041 qi->qf_multiignore = FALSE; /* reset continuation */
1042 }
1043 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1044 { /* continuation of multi-line msg */
Bram Moolenaar9b457942016-10-09 16:10:05 +02001045 if (!qi->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001046 {
Bram Moolenaar9b457942016-10-09 16:10:05 +02001047 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001048
Bram Moolenaar9b457942016-10-09 16:10:05 +02001049 if (qfprev == NULL)
1050 return QF_FAIL;
1051 if (*fields->errmsg && !qi->qf_multiignore)
1052 {
1053 len = (int)STRLEN(qfprev->qf_text);
1054 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1055 == NULL)
1056 return QF_FAIL;
1057 STRCPY(ptr, qfprev->qf_text);
1058 vim_free(qfprev->qf_text);
1059 qfprev->qf_text = ptr;
1060 *(ptr += len) = '\n';
1061 STRCPY(++ptr, fields->errmsg);
1062 }
1063 if (qfprev->qf_nr == -1)
1064 qfprev->qf_nr = fields->enr;
1065 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1066 /* only printable chars allowed */
1067 qfprev->qf_type = fields->type;
1068
1069 if (!qfprev->qf_lnum)
1070 qfprev->qf_lnum = fields->lnum;
1071 if (!qfprev->qf_col)
1072 qfprev->qf_col = fields->col;
1073 qfprev->qf_viscol = fields->use_viscol;
1074 if (!qfprev->qf_fnum)
1075 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1076 *fields->namebuf || qi->qf_directory != NULL
1077 ? fields->namebuf
1078 : qi->qf_currfile != NULL && fields->valid
1079 ? qi->qf_currfile : 0);
1080 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001081 if (idx == 'Z')
1082 qi->qf_multiline = qi->qf_multiignore = FALSE;
1083 line_breakcheck();
1084 return QF_IGNORE_LINE;
1085 }
1086 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1087 {
1088 /* global file names */
1089 fields->valid = FALSE;
1090 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1091 {
1092 if (*fields->namebuf && idx == 'P')
1093 qi->qf_currfile =
1094 qf_push_dir(fields->namebuf, &qi->qf_file_stack, TRUE);
1095 else if (idx == 'Q')
1096 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
1097 *fields->namebuf = NUL;
1098 if (tail && *tail)
1099 {
1100 STRMOVE(IObuff, skipwhite(tail));
1101 qi->qf_multiscan = TRUE;
1102 goto restofline;
1103 }
1104 }
1105 }
1106 if (fmt_ptr->flags == '-') /* generally exclude this line */
1107 {
1108 if (qi->qf_multiline)
1109 /* also exclude continuation lines */
1110 qi->qf_multiignore = TRUE;
1111 return QF_IGNORE_LINE;
1112 }
1113 }
1114
1115 return QF_OK;
1116}
1117
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001118/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001119 * Read the errorfile "efile" into memory, line by line, building the error
1120 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001121 * Alternative: when "efile" is NULL read errors from buffer "buf".
1122 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001123 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001124 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1125 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001126 * Return -1 for error, number of errors for success.
1127 */
1128 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001129qf_init_ext(
1130 qf_info_T *qi,
1131 char_u *efile,
1132 buf_T *buf,
1133 typval_T *tv,
1134 char_u *errorformat,
1135 int newlist, /* TRUE: start a new error list */
1136 linenr_T lnumfirst, /* first line number to use */
1137 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001138 char_u *qf_title,
1139 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001140{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001141 qfstate_T state;
1142 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001143#ifdef FEAT_WINDOWS
1144 qfline_T *old_last = NULL;
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001145 int adding = FALSE;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001146#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001147 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001149 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001151 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001153 vim_memset(&state, 0, sizeof(state));
1154 vim_memset(&fields, 0, sizeof(fields));
1155#ifdef FEAT_MBYTE
1156 state.vc.vc_type = CONV_NONE;
1157 if (enc != NULL && *enc != NUL)
1158 convert_setup(&state.vc, enc, p_enc);
1159#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001160 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1161 fields.errmsglen = CMDBUFFSIZE + 1;
1162 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1163 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1164 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1165 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 goto qf_init_end;
1167
Bram Moolenaare0d37972016-07-15 22:36:01 +02001168 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
1170 EMSG2(_(e_openerrf), efile);
1171 goto qf_init_end;
1172 }
1173
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001174 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001176 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001177#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001178 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02001179 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001180 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001181 adding = TRUE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001182 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001183 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001187 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001188 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 else
1190 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001192 /*
1193 * If we are not adding or adding to another list: clear the state.
1194 */
1195 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
1196 {
1197 qi->qf_dir_curlist = qi->qf_curlist;
1198 qf_clean_dir_stack(&qi->qf_dir_stack);
1199 qi->qf_directory = NULL;
1200 qf_clean_dir_stack(&qi->qf_file_stack);
1201 qi->qf_currfile = NULL;
1202 qi->qf_multiline = FALSE;
1203 qi->qf_multiignore = FALSE;
1204 qi->qf_multiscan = FALSE;
1205 }
1206
1207 /*
1208 * If the errorformat didn't change between calls, then reuse the
1209 * previously parsed values.
1210 */
1211 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1212 {
1213 /* free the previously parsed data */
1214 vim_free(last_efm);
1215 last_efm = NULL;
1216 free_efm_list(&fmt_first);
1217
1218 /* parse the current 'efm' */
1219 fmt_first = parse_efm_option(efm);
1220 if (fmt_first != NULL)
1221 last_efm = vim_strsave(efm);
1222 }
1223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
1227 /*
1228 * got_int is reset here, because it was probably set when killing the
1229 * ":make" command, but we still want to read the errorfile then.
1230 */
1231 got_int = FALSE;
1232
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001233 if (tv != NULL)
1234 {
1235 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001236 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001237 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001238 state.p_li = tv->vval.v_list->lv_first;
1239 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001240 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001241 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001242 state.buflnum = lnumfirst;
1243 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 /*
1246 * Read the lines in the error file one by one.
1247 * Try to recognize one of the error formats in each line.
1248 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001249 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001251 /* Get the next line from a file/buffer/list/string */
1252 status = qf_get_nextline(&state);
1253 if (status == QF_NOMEM) /* memory alloc failure */
1254 goto qf_init_end;
1255 if (status == QF_END_OF_INPUT) /* end of input */
1256 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001258 status = qf_parse_line(qi, state.linebuf, state.linelen, fmt_first,
1259 &fields);
1260 if (status == QF_FAIL)
1261 goto error2;
1262 if (status == QF_NOMEM)
1263 goto qf_init_end;
1264 if (status == QF_IGNORE_LINE)
1265 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001267 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001268 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001269 (*fields.namebuf || qi->qf_directory != NULL)
1270 ? fields.namebuf
1271 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001272 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001273 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001274 fields.errmsg,
1275 fields.lnum,
1276 fields.col,
1277 fields.use_viscol,
1278 fields.pattern,
1279 fields.enr,
1280 fields.type,
1281 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 goto error2;
1283 line_breakcheck();
1284 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001285 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001287 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001289 /* no valid entry found */
1290 qi->qf_lists[qi->qf_curlist].qf_ptr =
1291 qi->qf_lists[qi->qf_curlist].qf_start;
1292 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1293 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295 else
1296 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001297 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1298 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1299 qi->qf_lists[qi->qf_curlist].qf_ptr =
1300 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001302 /* return number of matches */
1303 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001304 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 EMSG(_(e_readerrf));
1307error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001308 if (!adding)
1309 {
1310 qf_free(qi, qi->qf_curlist);
1311 qi->qf_listcount--;
1312 if (qi->qf_curlist > 0)
1313 --qi->qf_curlist;
1314 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001315qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001316 if (state.fd != NULL)
1317 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001318 vim_free(fields.namebuf);
1319 vim_free(fields.errmsg);
1320 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001321 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
1323#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001324 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001326#ifdef FEAT_MBYTE
1327 if (state.vc.vc_type != CONV_NONE)
1328 convert_setup(&state.vc, NULL, NULL);
1329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
1331 return retval;
1332}
1333
Bram Moolenaarfb604092014-07-23 15:55:00 +02001334 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001335qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001336{
1337 if (title != NULL)
1338 {
1339 char_u *p = alloc((int)STRLEN(title) + 2);
1340
1341 qi->qf_lists[qi->qf_curlist].qf_title = p;
1342 if (p != NULL)
1343 sprintf((char *)p, ":%s", (char *)title);
1344 }
1345}
1346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347/*
1348 * Prepare for adding a new quickfix list.
1349 */
1350 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001351qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 int i;
1354
1355 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001356 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 * the current entry. This makes it possible to browse in a tree-like
1358 * way with ":grep'.
1359 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001360 while (qi->qf_listcount > qi->qf_curlist + 1)
1361 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
1363 /*
1364 * When the stack is full, remove to oldest entry
1365 * Otherwise, add a new entry.
1366 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001367 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001369 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001371 qi->qf_lists[i - 1] = qi->qf_lists[i];
1372 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001375 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001376 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001377 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378}
1379
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001380/*
1381 * Free a location list
1382 */
1383 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001384ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001385{
1386 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001387 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001388
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001389 qi = *pqi;
1390 if (qi == NULL)
1391 return;
1392 *pqi = NULL; /* Remove reference to this list */
1393
1394 qi->qf_refcount--;
1395 if (qi->qf_refcount < 1)
1396 {
1397 /* No references to this location list */
1398 for (i = 0; i < qi->qf_listcount; ++i)
1399 qf_free(qi, i);
1400 vim_free(qi);
1401 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001402}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001403
1404 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001405qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001406{
1407 int i;
1408 qf_info_T *qi = &ql_info;
1409
1410 if (wp != NULL)
1411 {
1412 /* location list */
1413 ll_free_all(&wp->w_llist);
1414 ll_free_all(&wp->w_llist_ref);
1415 }
1416 else
1417 /* quickfix list */
1418 for (i = 0; i < qi->qf_listcount; ++i)
1419 qf_free(qi, i);
1420}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422/*
1423 * Add an entry to the end of the list of errors.
1424 * Returns OK or FAIL.
1425 */
1426 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001427qf_add_entry(
1428 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001429 char_u *dir, /* optional directory name */
1430 char_u *fname, /* file name or NULL */
1431 int bufnum, /* buffer number or zero */
1432 char_u *mesg, /* message */
1433 long lnum, /* line number */
1434 int col, /* column */
1435 int vis_col, /* using visual column */
1436 char_u *pattern, /* search pattern */
1437 int nr, /* error number */
1438 int type, /* type character */
1439 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001441 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001442 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001444 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001446 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001447 {
1448 buf_T *buf = buflist_findnr(bufnum);
1449
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001450 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001451 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001452 buf->b_has_qf_entry |=
1453 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001454 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001455 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001456 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1458 {
1459 vim_free(qfp);
1460 return FAIL;
1461 }
1462 qfp->qf_lnum = lnum;
1463 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001464 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001465 if (pattern == NULL || *pattern == NUL)
1466 qfp->qf_pattern = NULL;
1467 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1468 {
1469 vim_free(qfp->qf_text);
1470 vim_free(qfp);
1471 return FAIL;
1472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 qfp->qf_nr = nr;
1474 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1475 type = 0;
1476 qfp->qf_type = type;
1477 qfp->qf_valid = valid;
1478
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001479 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001480 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1481 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001483 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001484 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1485 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001486 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
1488 else
1489 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001490 qfp->qf_prev = *lastp;
1491 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001493 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001495 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001496 ++qi->qf_lists[qi->qf_curlist].qf_count;
1497 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1498 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001500 qi->qf_lists[qi->qf_curlist].qf_index =
1501 qi->qf_lists[qi->qf_curlist].qf_count;
1502 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504
1505 return OK;
1506}
1507
1508/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001509 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001510 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001511 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001512ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001513{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001514 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001515
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001516 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1517 if (qi != NULL)
1518 {
1519 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1520 qi->qf_refcount++;
1521 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001523 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001524}
1525
1526/*
1527 * Return the location list for window 'wp'.
1528 * If not present, allocate a location list
1529 */
1530 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001531ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001532{
1533 if (IS_LL_WINDOW(wp))
1534 /* For a location list window, use the referenced location list */
1535 return wp->w_llist_ref;
1536
1537 /*
1538 * For a non-location list window, w_llist_ref should not point to a
1539 * location list.
1540 */
1541 ll_free_all(&wp->w_llist_ref);
1542
1543 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001544 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001545 return wp->w_llist;
1546}
1547
1548/*
1549 * Copy the location list from window "from" to window "to".
1550 */
1551 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001552copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001553{
1554 qf_info_T *qi;
1555 int idx;
1556 int i;
1557
1558 /*
1559 * When copying from a location list window, copy the referenced
1560 * location list. For other windows, copy the location list for
1561 * that window.
1562 */
1563 if (IS_LL_WINDOW(from))
1564 qi = from->w_llist_ref;
1565 else
1566 qi = from->w_llist;
1567
1568 if (qi == NULL) /* no location list to copy */
1569 return;
1570
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001571 /* allocate a new location list */
1572 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001573 return;
1574
1575 to->w_llist->qf_listcount = qi->qf_listcount;
1576
1577 /* Copy the location lists one at a time */
1578 for (idx = 0; idx < qi->qf_listcount; idx++)
1579 {
1580 qf_list_T *from_qfl;
1581 qf_list_T *to_qfl;
1582
1583 to->w_llist->qf_curlist = idx;
1584
1585 from_qfl = &qi->qf_lists[idx];
1586 to_qfl = &to->w_llist->qf_lists[idx];
1587
1588 /* Some of the fields are populated by qf_add_entry() */
1589 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1590 to_qfl->qf_count = 0;
1591 to_qfl->qf_index = 0;
1592 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001593 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001594 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001595 if (from_qfl->qf_title != NULL)
1596 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1597 else
1598 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001599
1600 if (from_qfl->qf_count)
1601 {
1602 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001603 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001604
1605 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001606 for (i = 0, from_qfp = from_qfl->qf_start;
1607 i < from_qfl->qf_count && from_qfp != NULL;
1608 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001609 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001610 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001611 NULL,
1612 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001613 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001614 from_qfp->qf_text,
1615 from_qfp->qf_lnum,
1616 from_qfp->qf_col,
1617 from_qfp->qf_viscol,
1618 from_qfp->qf_pattern,
1619 from_qfp->qf_nr,
1620 0,
1621 from_qfp->qf_valid) == FAIL)
1622 {
1623 qf_free_all(to);
1624 return;
1625 }
1626 /*
1627 * qf_add_entry() will not set the qf_num field, as the
1628 * directory and file names are not supplied. So the qf_fnum
1629 * field is copied here.
1630 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001631 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001632 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1633 prevp->qf_type = from_qfp->qf_type; /* error type */
1634 if (from_qfl->qf_ptr == from_qfp)
1635 to_qfl->qf_ptr = prevp; /* current location */
1636 }
1637 }
1638
1639 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1640
1641 /* When no valid entries are present in the list, qf_ptr points to
1642 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001643 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001644 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001645 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001646 to_qfl->qf_index = 1;
1647 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001648 }
1649
1650 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1651}
1652
1653/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001654 * Looking up a buffer can be slow if there are many. Remember the last one
1655 * to make this a lot faster if there are multiple matches in the same file.
1656 */
1657static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001658static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001659
1660/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001661 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001662 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 */
1664 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001665qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
Bram Moolenaar82404332016-07-10 17:00:38 +02001667 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001668 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001669 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001670
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (fname == NULL || *fname == NUL) /* no file name */
1672 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaare60acc12011-05-10 16:41:25 +02001674#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001675 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001676#endif
1677#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001678 if (directory != NULL)
1679 slash_adjust(directory);
1680 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001681#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001682 if (directory != NULL && !vim_isAbsName(fname)
1683 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1684 {
1685 /*
1686 * Here we check if the file really exists.
1687 * This should normally be true, but if make works without
1688 * "leaving directory"-messages we might have missed a
1689 * directory change.
1690 */
1691 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001694 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001695 if (directory)
1696 ptr = concat_fnames(directory, fname, TRUE);
1697 else
1698 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001700 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001701 bufname = ptr;
1702 }
1703 else
1704 bufname = fname;
1705
1706 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001707 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001708 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001709 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001710 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001712 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001713 {
1714 vim_free(qf_last_bufname);
1715 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1716 if (bufname == ptr)
1717 qf_last_bufname = bufname;
1718 else
1719 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001720 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001721 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001722 if (buf == NULL)
1723 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001724
Bram Moolenaarc1542742016-07-20 21:44:37 +02001725 buf->b_has_qf_entry =
1726 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001727 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728}
1729
1730/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001731 * Push dirbuf onto the directory stack and return pointer to actual dir or
1732 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 */
1734 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001735qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
1737 struct dir_stack_T *ds_new;
1738 struct dir_stack_T *ds_ptr;
1739
1740 /* allocate new stack element and hook it in */
1741 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1742 if (ds_new == NULL)
1743 return NULL;
1744
1745 ds_new->next = *stackptr;
1746 *stackptr = ds_new;
1747
1748 /* store directory on the stack */
1749 if (vim_isAbsName(dirbuf)
1750 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001751 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 (*stackptr)->dirname = vim_strsave(dirbuf);
1753 else
1754 {
1755 /* Okay we don't have an absolute path.
1756 * dirbuf must be a subdir of one of the directories on the stack.
1757 * Let's search...
1758 */
1759 ds_new = (*stackptr)->next;
1760 (*stackptr)->dirname = NULL;
1761 while (ds_new)
1762 {
1763 vim_free((*stackptr)->dirname);
1764 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1765 TRUE);
1766 if (mch_isdir((*stackptr)->dirname) == TRUE)
1767 break;
1768
1769 ds_new = ds_new->next;
1770 }
1771
1772 /* clean up all dirs we already left */
1773 while ((*stackptr)->next != ds_new)
1774 {
1775 ds_ptr = (*stackptr)->next;
1776 (*stackptr)->next = (*stackptr)->next->next;
1777 vim_free(ds_ptr->dirname);
1778 vim_free(ds_ptr);
1779 }
1780
1781 /* Nothing found -> it must be on top level */
1782 if (ds_new == NULL)
1783 {
1784 vim_free((*stackptr)->dirname);
1785 (*stackptr)->dirname = vim_strsave(dirbuf);
1786 }
1787 }
1788
1789 if ((*stackptr)->dirname != NULL)
1790 return (*stackptr)->dirname;
1791 else
1792 {
1793 ds_ptr = *stackptr;
1794 *stackptr = (*stackptr)->next;
1795 vim_free(ds_ptr);
1796 return NULL;
1797 }
1798}
1799
1800
1801/*
1802 * pop dirbuf from the directory stack and return previous directory or NULL if
1803 * stack is empty
1804 */
1805 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001806qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807{
1808 struct dir_stack_T *ds_ptr;
1809
1810 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1811 * What to do if it isn't? */
1812
1813 /* pop top element and free it */
1814 if (*stackptr != NULL)
1815 {
1816 ds_ptr = *stackptr;
1817 *stackptr = (*stackptr)->next;
1818 vim_free(ds_ptr->dirname);
1819 vim_free(ds_ptr);
1820 }
1821
1822 /* return NEW top element as current dir or NULL if stack is empty*/
1823 return *stackptr ? (*stackptr)->dirname : NULL;
1824}
1825
1826/*
1827 * clean up directory stack
1828 */
1829 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001830qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 struct dir_stack_T *ds_ptr;
1833
1834 while ((ds_ptr = *stackptr) != NULL)
1835 {
1836 *stackptr = (*stackptr)->next;
1837 vim_free(ds_ptr->dirname);
1838 vim_free(ds_ptr);
1839 }
1840}
1841
1842/*
1843 * Check in which directory of the directory stack the given file can be
1844 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001845 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 * Cleans up intermediate directory entries.
1847 *
1848 * TODO: How to solve the following problem?
1849 * If we have the this directory tree:
1850 * ./
1851 * ./aa
1852 * ./aa/bb
1853 * ./bb
1854 * ./bb/x.c
1855 * and make says:
1856 * making all in aa
1857 * making all in bb
1858 * x.c:9: Error
1859 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1860 * qf_guess_filepath will return NULL.
1861 */
1862 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001863qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
1865 struct dir_stack_T *ds_ptr;
1866 struct dir_stack_T *ds_tmp;
1867 char_u *fullname;
1868
1869 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001870 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 return NULL;
1872
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001873 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 fullname = NULL;
1875 while (ds_ptr)
1876 {
1877 vim_free(fullname);
1878 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1879
1880 /* If concat_fnames failed, just go on. The worst thing that can happen
1881 * is that we delete the entire stack.
1882 */
1883 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1884 break;
1885
1886 ds_ptr = ds_ptr->next;
1887 }
1888
1889 vim_free(fullname);
1890
1891 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001892 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001894 ds_tmp = qi->qf_dir_stack->next;
1895 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 vim_free(ds_tmp->dirname);
1897 vim_free(ds_tmp);
1898 }
1899
1900 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901}
1902
1903/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001904 * When loading a file from the quickfix, the auto commands may modify it.
1905 * This may invalidate the current quickfix entry. This function checks
1906 * whether a entry is still present in the quickfix.
1907 * Similar to location list.
1908 */
1909 static int
1910is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1911{
1912 qf_list_T *qfl;
1913 qfline_T *qfp;
1914 int i;
1915
1916 qfl = &qi->qf_lists[qi->qf_curlist];
1917
1918 /* Search for the entry in the current list */
1919 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1920 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001921 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001922 break;
1923
1924 if (i == qfl->qf_count) /* Entry is not found */
1925 return FALSE;
1926
1927 return TRUE;
1928}
1929
1930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 * jump to a quickfix line
1932 * if dir == FORWARD go "errornr" valid entries forward
1933 * if dir == BACKWARD go "errornr" valid entries backward
1934 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1935 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1936 * else if "errornr" is zero, redisplay the same line
1937 * else go to entry "errornr"
1938 */
1939 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001940qf_jump(
1941 qf_info_T *qi,
1942 int dir,
1943 int errornr,
1944 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001946 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001947 qfline_T *qf_ptr;
1948 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 int qf_index;
1950 int old_qf_fnum;
1951 int old_qf_index;
1952 int prev_index;
1953 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1954 char_u *err = e_no_more_items;
1955 linenr_T i;
1956 buf_T *old_curbuf;
1957 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 colnr_T screen_col;
1959 colnr_T char_col;
1960 char_u *line;
1961#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001962 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001963 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 int opened_window = FALSE;
1965 win_T *win;
1966 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001967 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001969 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 int print_message = TRUE;
1971 int len;
1972#ifdef FEAT_FOLDING
1973 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1974#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001975 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001976 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001978 if (qi == NULL)
1979 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980
1981 if (qi->qf_curlist >= qi->qf_listcount
1982 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 EMSG(_(e_quickfix));
1985 return;
1986 }
1987
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001988 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001990 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 old_qf_index = qf_index;
1992 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1993 {
1994 while (errornr--)
1995 {
1996 old_qf_ptr = qf_ptr;
1997 prev_index = qf_index;
1998 old_qf_fnum = qf_ptr->qf_fnum;
1999 do
2000 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002001 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 || qf_ptr->qf_next == NULL)
2003 {
2004 qf_ptr = old_qf_ptr;
2005 qf_index = prev_index;
2006 if (err != NULL)
2007 {
2008 EMSG(_(err));
2009 goto theend;
2010 }
2011 errornr = 0;
2012 break;
2013 }
2014 ++qf_index;
2015 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002016 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2017 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2019 err = NULL;
2020 }
2021 }
2022 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
2023 {
2024 while (errornr--)
2025 {
2026 old_qf_ptr = qf_ptr;
2027 prev_index = qf_index;
2028 old_qf_fnum = qf_ptr->qf_fnum;
2029 do
2030 {
2031 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
2032 {
2033 qf_ptr = old_qf_ptr;
2034 qf_index = prev_index;
2035 if (err != NULL)
2036 {
2037 EMSG(_(err));
2038 goto theend;
2039 }
2040 errornr = 0;
2041 break;
2042 }
2043 --qf_index;
2044 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002045 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2046 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2048 err = NULL;
2049 }
2050 }
2051 else if (errornr != 0) /* go to specified number */
2052 {
2053 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2054 {
2055 --qf_index;
2056 qf_ptr = qf_ptr->qf_prev;
2057 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002058 while (errornr > qf_index && qf_index <
2059 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 && qf_ptr->qf_next != NULL)
2061 {
2062 ++qf_index;
2063 qf_ptr = qf_ptr->qf_next;
2064 }
2065 }
2066
2067#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002068 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2069 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 /* No need to print the error message if it's visible in the error
2071 * window */
2072 print_message = FALSE;
2073
2074 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002075 * For ":helpgrep" find a help window or open one.
2076 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002077 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002078 {
2079 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002080
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002081 if (cmdmod.tab != 0)
2082 wp = NULL;
2083 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002084 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002085 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2086 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002087 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2088 win_enter(wp, TRUE);
2089 else
2090 {
2091 /*
2092 * Split off help window; put it at far top if no position
2093 * specified, the current window is vertically split and narrow.
2094 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002095 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002096 if (cmdmod.split == 0 && curwin->w_width != Columns
2097 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002098 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002099 if (qi != &ql_info)
2100 flags |= WSP_NEWLOC; /* don't copy the location list */
2101
2102 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002103 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002104 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002105
2106 if (curwin->w_height < p_hh)
2107 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002108
2109 if (qi != &ql_info) /* not a quickfix list */
2110 {
2111 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002112 curwin->w_llist = qi;
2113 qi->qf_refcount++;
2114 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002115 }
2116
2117 if (!p_im)
2118 restart_edit = 0; /* don't want insert mode in help file */
2119 }
2120
2121 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 * If currently in the quickfix window, find another window to show the
2123 * file in.
2124 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002125 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002127 win_T *usable_win_ptr = NULL;
2128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 /*
2130 * If there is no file specified, we don't know where to go.
2131 * But do advance, otherwise ":cn" gets stuck.
2132 */
2133 if (qf_ptr->qf_fnum == 0)
2134 goto theend;
2135
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002136 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002137
2138 ll_ref = curwin->w_llist_ref;
2139 if (ll_ref != NULL)
2140 {
2141 /* Find a window using the same location list that is not a
2142 * quickfix window. */
2143 FOR_ALL_WINDOWS(usable_win_ptr)
2144 if (usable_win_ptr->w_llist == ll_ref
2145 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002146 {
2147 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002148 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002149 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002150 }
2151
2152 if (!usable_win)
2153 {
2154 /* Locate a window showing a normal buffer */
2155 FOR_ALL_WINDOWS(win)
2156 if (win->w_buffer->b_p_bt[0] == NUL)
2157 {
2158 usable_win = 1;
2159 break;
2160 }
2161 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002164 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002165 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002167 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002168 {
2169 tabpage_T *tp;
2170 win_T *wp;
2171
2172 FOR_ALL_TAB_WINDOWS(tp, wp)
2173 {
2174 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2175 {
2176 goto_tabpage_win(tp, wp);
2177 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002178 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002179 }
2180 }
2181 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002182win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002183
2184 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002185 * If there is only one window and it is the quickfix window, create a
2186 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002187 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002188 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002190 flags = WSP_ABOVE;
2191 if (ll_ref != NULL)
2192 flags |= WSP_NEWLOC;
2193 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 goto failed; /* not enough room for window */
2195 opened_window = TRUE; /* close it when fail */
2196 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002197 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002198 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002199 if (ll_ref != NULL)
2200 {
2201 /* The new window should use the location list from the
2202 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002203 curwin->w_llist = ll_ref;
2204 ll_ref->qf_refcount++;
2205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207 else
2208 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002209 if (curwin->w_llist_ref != NULL)
2210 {
2211 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002212 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002213 if (win == NULL)
2214 {
2215 /* Find the window showing the selected file */
2216 FOR_ALL_WINDOWS(win)
2217 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2218 break;
2219 if (win == NULL)
2220 {
2221 /* Find a previous usable window */
2222 win = curwin;
2223 do
2224 {
2225 if (win->w_buffer->b_p_bt[0] == NUL)
2226 break;
2227 if (win->w_prev == NULL)
2228 win = lastwin; /* wrap around the top */
2229 else
2230 win = win->w_prev; /* go to previous window */
2231 } while (win != curwin);
2232 }
2233 }
2234 win_goto(win);
2235
2236 /* If the location list for the window is not set, then set it
2237 * to the location list from the location window */
2238 if (win->w_llist == NULL)
2239 {
2240 win->w_llist = ll_ref;
2241 ll_ref->qf_refcount++;
2242 }
2243 }
2244 else
2245 {
2246
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 /*
2248 * Try to find a window that shows the right buffer.
2249 * Default to the window just above the quickfix buffer.
2250 */
2251 win = curwin;
2252 altwin = NULL;
2253 for (;;)
2254 {
2255 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2256 break;
2257 if (win->w_prev == NULL)
2258 win = lastwin; /* wrap around the top */
2259 else
2260 win = win->w_prev; /* go to previous window */
2261
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002262 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
2264 /* Didn't find it, go to the window before the quickfix
2265 * window. */
2266 if (altwin != NULL)
2267 win = altwin;
2268 else if (curwin->w_prev != NULL)
2269 win = curwin->w_prev;
2270 else
2271 win = curwin->w_next;
2272 break;
2273 }
2274
2275 /* Remember a usable window. */
2276 if (altwin == NULL && !win->w_p_pvw
2277 && win->w_buffer->b_p_bt[0] == NUL)
2278 altwin = win;
2279 }
2280
2281 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
2284 }
2285#endif
2286
2287 /*
2288 * If there is a file name,
2289 * read the wanted file if needed, and check autowrite etc.
2290 */
2291 old_curbuf = curbuf;
2292 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002293
2294 if (qf_ptr->qf_fnum != 0)
2295 {
2296 if (qf_ptr->qf_type == 1)
2297 {
2298 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2299 * set b_p_ro flag). */
2300 if (!can_abandon(curbuf, forceit))
2301 {
2302 EMSG(_(e_nowrtmsg));
2303 ok = FALSE;
2304 }
2305 else
2306 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002307 ECMD_HIDE + ECMD_SET_HELP,
2308 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002309 }
2310 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002311 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002312 int old_qf_curlist = qi->qf_curlist;
2313 int is_abort = FALSE;
2314
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002315 ok = buflist_getfile(qf_ptr->qf_fnum,
2316 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002317 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002318 {
2319 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002320 is_abort = TRUE;
2321 opened_window = FALSE;
2322 }
2323 else if (old_qf_curlist != qi->qf_curlist
2324 || !is_qf_entry_present(qi, qf_ptr))
2325 {
2326 if (qi == &ql_info)
2327 EMSG(_("E925: Current quickfix was changed"));
2328 else
2329 EMSG(_("E926: Current location list was changed"));
2330 is_abort = TRUE;
2331 }
2332
2333 if (is_abort)
2334 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002335 ok = FALSE;
2336 qi = NULL;
2337 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002338 }
2339 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002340 }
2341
2342 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 /* When not switched to another buffer, still need to set pc mark */
2345 if (curbuf == old_curbuf)
2346 setpcmark();
2347
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002348 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002350 /*
2351 * Go to line with error, unless qf_lnum is 0.
2352 */
2353 i = qf_ptr->qf_lnum;
2354 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002356 if (i > curbuf->b_ml.ml_line_count)
2357 i = curbuf->b_ml.ml_line_count;
2358 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002360 if (qf_ptr->qf_col > 0)
2361 {
2362 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002363#ifdef FEAT_VIRTUALEDIT
2364 curwin->w_cursor.coladd = 0;
2365#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002366 if (qf_ptr->qf_viscol == TRUE)
2367 {
2368 /*
2369 * Check each character from the beginning of the error
2370 * line up to the error column. For each tab character
2371 * found, reduce the error column value by the length of
2372 * a tab character.
2373 */
2374 line = ml_get_curline();
2375 screen_col = 0;
2376 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2377 {
2378 if (*line == NUL)
2379 break;
2380 if (*line++ == '\t')
2381 {
2382 curwin->w_cursor.col -= 7 - (screen_col % 8);
2383 screen_col += 8 - (screen_col % 8);
2384 }
2385 else
2386 ++screen_col;
2387 }
2388 }
2389 check_cursor();
2390 }
2391 else
2392 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002395 {
2396 pos_T save_cursor;
2397
2398 /* Move the cursor to the first line in the buffer */
2399 save_cursor = curwin->w_cursor;
2400 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002401 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2402 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002403 curwin->w_cursor = save_cursor;
2404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406#ifdef FEAT_FOLDING
2407 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2408 foldOpenCursor();
2409#endif
2410 if (print_message)
2411 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002412 /* Update the screen before showing the message, unless the screen
2413 * scrolled up. */
2414 if (!msg_scrolled)
2415 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002417 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2419 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2420 /* Add the message, skipping leading whitespace and newlines. */
2421 len = (int)STRLEN(IObuff);
2422 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2423
2424 /* Output the message. Overwrite to avoid scrolling when the 'O'
2425 * flag is present in 'shortmess'; But when not jumping, print the
2426 * whole message. */
2427 i = msg_scroll;
2428 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2429 msg_scroll = TRUE;
2430 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2431 msg_scroll = FALSE;
2432 msg_attr_keep(IObuff, 0, TRUE);
2433 msg_scroll = i;
2434 }
2435 }
2436 else
2437 {
2438#ifdef FEAT_WINDOWS
2439 if (opened_window)
2440 win_close(curwin, TRUE); /* Close opened window */
2441#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002442 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 /*
2445 * Couldn't open file, so put index back where it was. This could
2446 * happen if the file was readonly and we changed something.
2447 */
2448#ifdef FEAT_WINDOWS
2449failed:
2450#endif
2451 qf_ptr = old_qf_ptr;
2452 qf_index = old_qf_index;
2453 }
2454 }
2455theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002456 if (qi != NULL)
2457 {
2458 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2459 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461#ifdef FEAT_WINDOWS
2462 if (p_swb != old_swb && opened_window)
2463 {
2464 /* Restore old 'switchbuf' value, but not when an autocommand or
2465 * modeline has changed the value. */
2466 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002469 swb_flags = old_swb_flags;
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 else
2472 free_string_option(old_swb);
2473 }
2474#endif
2475}
2476
2477/*
2478 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002479 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
2481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002482qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002484 buf_T *buf;
2485 char_u *fname;
2486 qfline_T *qfp;
2487 int i;
2488 int idx1 = 1;
2489 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002490 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002491 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002492 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002494 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002496 if (eap->cmdidx == CMD_llist)
2497 {
2498 qi = GET_LOC_LIST(curwin);
2499 if (qi == NULL)
2500 {
2501 EMSG(_(e_loclist));
2502 return;
2503 }
2504 }
2505
2506 if (qi->qf_curlist >= qi->qf_listcount
2507 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
2509 EMSG(_(e_quickfix));
2510 return;
2511 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002512 if (*arg == '+')
2513 {
2514 ++arg;
2515 plus = TRUE;
2516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2518 {
2519 EMSG(_(e_trailing));
2520 return;
2521 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002522 if (plus)
2523 {
2524 i = qi->qf_lists[qi->qf_curlist].qf_index;
2525 idx2 = i + idx1;
2526 idx1 = i;
2527 }
2528 else
2529 {
2530 i = qi->qf_lists[qi->qf_curlist].qf_count;
2531 if (idx1 < 0)
2532 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2533 if (idx2 < 0)
2534 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002537 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002539 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2540 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
2542 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2543 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002544 msg_putchar('\n');
2545 if (got_int)
2546 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002547
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002548 fname = NULL;
2549 if (qfp->qf_fnum != 0
2550 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2551 {
2552 fname = buf->b_fname;
2553 if (qfp->qf_type == 1) /* :helpgrep */
2554 fname = gettail(fname);
2555 }
2556 if (fname == NULL)
2557 sprintf((char *)IObuff, "%2d", i);
2558 else
2559 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2560 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002561 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar8820b482017-03-16 17:23:31 +01002562 ? HL_ATTR(HLF_L) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002563 if (qfp->qf_lnum == 0)
2564 IObuff[0] = NUL;
2565 else if (qfp->qf_col == 0)
2566 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2567 else
2568 sprintf((char *)IObuff, ":%ld col %d",
2569 qfp->qf_lnum, qfp->qf_col);
2570 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2571 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002572 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002573 if (qfp->qf_pattern != NULL)
2574 {
2575 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2576 STRCAT(IObuff, ":");
2577 msg_puts(IObuff);
2578 }
2579 msg_puts((char_u *)" ");
2580
2581 /* Remove newlines and leading whitespace from the text. For an
2582 * unrecognized line keep the indent, the compiler may mark a word
2583 * with ^^^^. */
2584 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2586 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002587 msg_prt_line(IObuff, FALSE);
2588 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002590
2591 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002592 if (qfp == NULL)
2593 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002594 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 ui_breakcheck();
2596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597}
2598
2599/*
2600 * Remove newlines and leading whitespace from an error message.
2601 * Put the result in "buf[bufsize]".
2602 */
2603 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002604qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
2606 int i;
2607 char_u *p = text;
2608
2609 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2610 {
2611 if (*p == '\n')
2612 {
2613 buf[i] = ' ';
2614 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002615 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 break;
2617 }
2618 else
2619 buf[i] = *p++;
2620 }
2621 buf[i] = NUL;
2622}
2623
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002624 static void
2625qf_msg(qf_info_T *qi, int which, char *lead)
2626{
2627 char *title = (char *)qi->qf_lists[which].qf_title;
2628 int count = qi->qf_lists[which].qf_count;
2629 char_u buf[IOSIZE];
2630
2631 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2632 lead,
2633 which + 1,
2634 qi->qf_listcount,
2635 count);
2636
2637 if (title != NULL)
2638 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002639 size_t len = STRLEN(buf);
2640
2641 if (len < 34)
2642 {
2643 vim_memset(buf + len, ' ', 34 - len);
2644 buf[34] = NUL;
2645 }
2646 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002647 }
2648 trunc_string(buf, buf, Columns - 1, IOSIZE);
2649 msg(buf);
2650}
2651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652/*
2653 * ":colder [count]": Up in the quickfix stack.
2654 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002655 * ":lolder [count]": Up in the location list stack.
2656 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 */
2658 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002659qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002661 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 int count;
2663
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002664 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2665 {
2666 qi = GET_LOC_LIST(curwin);
2667 if (qi == NULL)
2668 {
2669 EMSG(_(e_loclist));
2670 return;
2671 }
2672 }
2673
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (eap->addr_count != 0)
2675 count = eap->line2;
2676 else
2677 count = 1;
2678 while (count--)
2679 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002680 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002682 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {
2684 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002687 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689 else
2690 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002691 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
2693 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002694 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002696 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002699 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002701 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#endif
2703}
2704
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002705 void
2706qf_history(exarg_T *eap)
2707{
2708 qf_info_T *qi = &ql_info;
2709 int i;
2710
2711 if (eap->cmdidx == CMD_lhistory)
2712 qi = GET_LOC_LIST(curwin);
2713 if (qi == NULL || (qi->qf_listcount == 0
2714 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2715 MSG(_("No entries"));
2716 else
2717 for (i = 0; i < qi->qf_listcount; ++i)
2718 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2719}
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002722 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
2724 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002725qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002727 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002728 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002729 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002731 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002733 qfp = qi->qf_lists[idx].qf_start;
2734 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002735 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002736 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002737 vim_free(qfp->qf_text);
2738 stop = (qfp == qfpnext);
2739 vim_free(qfp->qf_pattern);
2740 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002741 if (stop)
2742 /* Somehow qf_count may have an incorrect value, set it to 1
2743 * to avoid crashing when it's wrong.
2744 * TODO: Avoid qf_count being incorrect. */
2745 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002746 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002747 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002748 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002750 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002751 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002752 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002753
2754 qf_clean_dir_stack(&qi->qf_dir_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002755 qi->qf_directory = NULL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002756 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002757 qi->qf_currfile = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758}
2759
2760/*
2761 * qf_mark_adjust: adjust marks
2762 */
2763 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002764qf_mark_adjust(
2765 win_T *wp,
2766 linenr_T line1,
2767 linenr_T line2,
2768 long amount,
2769 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002771 int i;
2772 qfline_T *qfp;
2773 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002774 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002775 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002776 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
Bram Moolenaarc1542742016-07-20 21:44:37 +02002778 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002779 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002780 if (wp != NULL)
2781 {
2782 if (wp->w_llist == NULL)
2783 return;
2784 qi = wp->w_llist;
2785 }
2786
2787 for (idx = 0; idx < qi->qf_listcount; ++idx)
2788 if (qi->qf_lists[idx].qf_count)
2789 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002790 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2791 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (qfp->qf_fnum == curbuf->b_fnum)
2793 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002794 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2796 {
2797 if (amount == MAXLNUM)
2798 qfp->qf_cleared = TRUE;
2799 else
2800 qfp->qf_lnum += amount;
2801 }
2802 else if (amount_after && qfp->qf_lnum > line2)
2803 qfp->qf_lnum += amount_after;
2804 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002805
2806 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002807 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808}
2809
2810/*
2811 * Make a nice message out of the error character and the error number:
2812 * char number message
2813 * e or E 0 " error"
2814 * w or W 0 " warning"
2815 * i or I 0 " info"
2816 * 0 0 ""
2817 * other 0 " c"
2818 * e or E n " error n"
2819 * w or W n " warning n"
2820 * i or I n " info n"
2821 * 0 n " error n"
2822 * other n " c n"
2823 * 1 x "" :helpgrep
2824 */
2825 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002826qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 static char_u buf[20];
2829 static char_u cc[3];
2830 char_u *p;
2831
2832 if (c == 'W' || c == 'w')
2833 p = (char_u *)" warning";
2834 else if (c == 'I' || c == 'i')
2835 p = (char_u *)" info";
2836 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2837 p = (char_u *)" error";
2838 else if (c == 0 || c == 1)
2839 p = (char_u *)"";
2840 else
2841 {
2842 cc[0] = ' ';
2843 cc[1] = c;
2844 cc[2] = NUL;
2845 p = cc;
2846 }
2847
2848 if (nr <= 0)
2849 return p;
2850
2851 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2852 return buf;
2853}
2854
2855#if defined(FEAT_WINDOWS) || defined(PROTO)
2856/*
2857 * ":cwindow": open the quickfix window if we have errors to display,
2858 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002859 * ":lwindow": open the location list window if we have locations to display,
2860 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 */
2862 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002863ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002865 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 win_T *win;
2867
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002868 if (eap->cmdidx == CMD_lwindow)
2869 {
2870 qi = GET_LOC_LIST(curwin);
2871 if (qi == NULL)
2872 return;
2873 }
2874
2875 /* Look for an existing quickfix window. */
2876 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
2878 /*
2879 * If a quickfix window is open but we have no errors to display,
2880 * close the window. If a quickfix window is not open, then open
2881 * it if we have errors; otherwise, leave it closed.
2882 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002883 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002884 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002885 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
2887 if (win != NULL)
2888 ex_cclose(eap);
2889 }
2890 else if (win == NULL)
2891 ex_copen(eap);
2892}
2893
2894/*
2895 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002896 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002899ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002901 win_T *win = NULL;
2902 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002904 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2905 {
2906 qi = GET_LOC_LIST(curwin);
2907 if (qi == NULL)
2908 return;
2909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 /* Find existing quickfix window and close it. */
2912 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if (win != NULL)
2914 win_close(win, FALSE);
2915}
2916
2917/*
2918 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 */
2921 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002922ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002924 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002927 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002928 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002929 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002931 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2932 {
2933 qi = GET_LOC_LIST(curwin);
2934 if (qi == NULL)
2935 {
2936 EMSG(_(e_loclist));
2937 return;
2938 }
2939 }
2940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 if (eap->addr_count != 0)
2942 height = eap->line2;
2943 else
2944 height = QF_WINHEIGHT;
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947#ifdef FEAT_GUI
2948 need_mouse_correct = TRUE;
2949#endif
2950
2951 /*
2952 * Find existing quickfix window, or open a new one.
2953 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002954 win = qf_find_win(qi);
2955
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002956 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002959 if (eap->addr_count != 0)
2960 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002961 if (cmdmod.split & WSP_VERT)
2962 {
2963 if (height != W_WIDTH(win))
2964 win_setwidth(height);
2965 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002966 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002967 win_setheight(height);
2968 }
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 else
2971 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002972 qf_buf = qf_find_buf(qi);
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 /* The current window becomes the previous window afterwards. */
2975 win = curwin;
2976
Bram Moolenaar77642c02012-11-20 17:55:10 +01002977 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2978 && cmdmod.split == 0)
2979 /* Create the new window at the very bottom, except when
2980 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002981 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002982 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002984 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002986 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002988 /*
2989 * For the location list window, create a reference to the
2990 * location list from the window 'win'.
2991 */
2992 curwin->w_llist_ref = win->w_llist;
2993 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002995
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002996 if (oldwin != curwin)
2997 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002998 if (qf_buf != NULL)
2999 /* Use the existing quickfix buffer */
3000 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003001 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003002 else
3003 {
3004 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003005 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003006 /* switch off 'swapfile' */
3007 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3008 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003009 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003010 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003011 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003012#ifdef FEAT_DIFF
3013 curwin->w_p_diff = FALSE;
3014#endif
3015#ifdef FEAT_FOLDING
3016 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3017 OPT_LOCAL);
3018#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003021 /* Only set the height when still in the same tab page and there is no
3022 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003023 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 win_setheight(height);
3025 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3026 if (win_valid(win))
3027 prevwin = win;
3028 }
3029
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003030 qf_set_title_var(qi);
3031
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 /*
3033 * Fill the buffer with the quickfix list.
3034 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003035 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003037 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 curwin->w_cursor.col = 0;
3039 check_cursor();
3040 update_topline(); /* scroll to show the line */
3041}
3042
3043/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003044 * Move the cursor in the quickfix window to "lnum".
3045 */
3046 static void
3047qf_win_goto(win_T *win, linenr_T lnum)
3048{
3049 win_T *old_curwin = curwin;
3050
3051 curwin = win;
3052 curbuf = win->w_buffer;
3053 curwin->w_cursor.lnum = lnum;
3054 curwin->w_cursor.col = 0;
3055#ifdef FEAT_VIRTUALEDIT
3056 curwin->w_cursor.coladd = 0;
3057#endif
3058 curwin->w_curswant = 0;
3059 update_topline(); /* scroll to show the line */
3060 redraw_later(VALID);
3061 curwin->w_redr_status = TRUE; /* update ruler */
3062 curwin = old_curwin;
3063 curbuf = curwin->w_buffer;
3064}
3065
3066/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003067 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003068 */
3069 void
3070ex_cbottom(exarg_T *eap UNUSED)
3071{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003072 qf_info_T *qi = &ql_info;
3073 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003074
Bram Moolenaar537ef082016-07-09 17:56:19 +02003075 if (eap->cmdidx == CMD_lbottom)
3076 {
3077 qi = GET_LOC_LIST(curwin);
3078 if (qi == NULL)
3079 {
3080 EMSG(_(e_loclist));
3081 return;
3082 }
3083 }
3084
3085 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003086 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3087 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3088}
3089
3090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 * Return the number of the current entry (line number in the quickfix
3092 * window).
3093 */
3094 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003095qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003097 qf_info_T *qi = &ql_info;
3098
3099 if (IS_LL_WINDOW(wp))
3100 /* In the location list window, use the referenced location list */
3101 qi = wp->w_llist_ref;
3102
3103 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104}
3105
3106/*
3107 * Update the cursor position in the quickfix window to the current error.
3108 * Return TRUE if there is a quickfix window.
3109 */
3110 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003111qf_win_pos_update(
3112 qf_info_T *qi,
3113 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003116 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
3118 /*
3119 * Put the cursor on the current error in the quickfix window, so that
3120 * it's viewable.
3121 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003122 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 if (win != NULL
3124 && qf_index <= win->w_buffer->b_ml.ml_line_count
3125 && old_qf_index != qf_index)
3126 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (qf_index > old_qf_index)
3128 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003129 win->w_redraw_top = old_qf_index;
3130 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
3132 else
3133 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003134 win->w_redraw_top = qf_index;
3135 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003137 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 }
3139 return win != NULL;
3140}
3141
3142/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003143 * Check whether the given window is displaying the specified quickfix/location
3144 * list buffer
3145 */
3146 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003147is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003148{
3149 /*
3150 * A window displaying the quickfix buffer will have the w_llist_ref field
3151 * set to NULL.
3152 * A window displaying a location list buffer will have the w_llist_ref
3153 * pointing to the location list.
3154 */
3155 if (bt_quickfix(win->w_buffer))
3156 if ((qi == &ql_info && win->w_llist_ref == NULL)
3157 || (qi != &ql_info && win->w_llist_ref == qi))
3158 return TRUE;
3159
3160 return FALSE;
3161}
3162
3163/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003164 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003165 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003166 */
3167 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003168qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003169{
3170 win_T *win;
3171
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003172 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003173 if (is_qf_win(win, qi))
3174 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003175
3176 return win;
3177}
3178
3179/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003180 * Find a quickfix buffer.
3181 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 */
3183 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003184qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003186 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003187 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
Bram Moolenaar9c102382006-05-03 21:26:49 +00003189 FOR_ALL_TAB_WINDOWS(tp, win)
3190 if (is_qf_win(win, qi))
3191 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003192
Bram Moolenaar9c102382006-05-03 21:26:49 +00003193 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194}
3195
3196/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003197 * Update the w:quickfix_title variable in the quickfix/location list window
3198 */
3199 static void
3200qf_update_win_titlevar(qf_info_T *qi)
3201{
3202 win_T *win;
3203 win_T *curwin_save;
3204
3205 if ((win = qf_find_win(qi)) != NULL)
3206 {
3207 curwin_save = curwin;
3208 curwin = win;
3209 qf_set_title_var(qi);
3210 curwin = curwin_save;
3211 }
3212}
3213
3214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 * Find the quickfix buffer. If it exists, update the contents.
3216 */
3217 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003218qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
3220 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003221 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
3224 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003225 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 if (buf != NULL)
3227 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003228 linenr_T old_line_count = buf->b_ml.ml_line_count;
3229
3230 if (old_last == NULL)
3231 /* set curwin/curbuf to buf and save a few things */
3232 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
Bram Moolenaard823fa92016-08-12 16:29:27 +02003234 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003235
Bram Moolenaar864293a2016-06-02 13:40:04 +02003236 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003237
Bram Moolenaar864293a2016-06-02 13:40:04 +02003238 if (old_last == NULL)
3239 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003240 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003241
3242 /* restore curwin/curbuf and a few other things */
3243 aucmd_restbuf(&aco);
3244 }
3245
3246 /* Only redraw when added lines are visible. This avoids flickering
3247 * when the added lines are not visible. */
3248 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3249 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251}
3252
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003253/*
3254 * Set "w:quickfix_title" if "qi" has a title.
3255 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003256 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003257qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003258{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003259 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3260 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003261 qi->qf_lists[qi->qf_curlist].qf_title);
3262}
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264/*
3265 * Fill current buffer with quickfix errors, replacing any previous contents.
3266 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003267 * If "old_last" is not NULL append the items after this one.
3268 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3269 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 */
3271 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003272qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003274 linenr_T lnum;
3275 qfline_T *qfp;
3276 buf_T *errbuf;
3277 int len;
3278 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaar864293a2016-06-02 13:40:04 +02003280 if (old_last == NULL)
3281 {
3282 if (buf != curbuf)
3283 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003284 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003285 return;
3286 }
3287
3288 /* delete all existing lines */
3289 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3290 (void)ml_delete((linenr_T)1, FALSE);
3291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
3293 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003294 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
3296 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003297 if (old_last == NULL)
3298 {
3299 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3300 lnum = 0;
3301 }
3302 else
3303 {
3304 qfp = old_last->qf_next;
3305 lnum = buf->b_ml.ml_line_count;
3306 }
3307 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 if (qfp->qf_fnum != 0
3310 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3311 && errbuf->b_fname != NULL)
3312 {
3313 if (qfp->qf_type == 1) /* :helpgrep */
3314 STRCPY(IObuff, gettail(errbuf->b_fname));
3315 else
3316 STRCPY(IObuff, errbuf->b_fname);
3317 len = (int)STRLEN(IObuff);
3318 }
3319 else
3320 len = 0;
3321 IObuff[len++] = '|';
3322
3323 if (qfp->qf_lnum > 0)
3324 {
3325 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3326 len += (int)STRLEN(IObuff + len);
3327
3328 if (qfp->qf_col > 0)
3329 {
3330 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3331 len += (int)STRLEN(IObuff + len);
3332 }
3333
3334 sprintf((char *)IObuff + len, "%s",
3335 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3336 len += (int)STRLEN(IObuff + len);
3337 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003338 else if (qfp->qf_pattern != NULL)
3339 {
3340 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3341 len += (int)STRLEN(IObuff + len);
3342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 IObuff[len++] = '|';
3344 IObuff[len++] = ' ';
3345
3346 /* Remove newlines and leading whitespace from the text.
3347 * For an unrecognized line keep the indent, the compiler may
3348 * mark a word with ^^^^. */
3349 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3350 IObuff + len, IOSIZE - len);
3351
Bram Moolenaar864293a2016-06-02 13:40:04 +02003352 if (ml_append_buf(buf, lnum, IObuff,
3353 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003355 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003357 if (qfp == NULL)
3358 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003360
3361 if (old_last == NULL)
3362 /* Delete the empty line which is now at the end */
3363 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
3365
3366 /* correct cursor position */
3367 check_lnums(TRUE);
3368
Bram Moolenaar864293a2016-06-02 13:40:04 +02003369 if (old_last == NULL)
3370 {
3371 /* Set the 'filetype' to "qf" each time after filling the buffer.
3372 * This resembles reading a file into a buffer, it's more logical when
3373 * using autocommands. */
3374 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3375 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
3377#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003378 keep_filetype = TRUE; /* don't detect 'filetype' */
3379 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003381 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003383 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003385 /* make sure it will be redrawn */
3386 redraw_curbuf_later(NOT_VALID);
3387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
3389 /* Restore KeyTyped, setting 'filetype' may reset it. */
3390 KeyTyped = old_KeyTyped;
3391}
3392
3393#endif /* FEAT_WINDOWS */
3394
3395/*
3396 * Return TRUE if "buf" is the quickfix buffer.
3397 */
3398 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003399bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003401 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402}
3403
3404/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003405 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3406 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 */
3408 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003409bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003411 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3412 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413}
3414
3415/*
3416 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3417 */
3418 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003419bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003421 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422}
3423
3424 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003425bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 if (bt_dontwrite(buf))
3428 {
3429 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3430 return TRUE;
3431 }
3432 return FALSE;
3433}
3434
3435/*
3436 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3437 * and 'bufhidden'.
3438 */
3439 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003440buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3443 switch (buf->b_p_bh[0])
3444 {
3445 case 'u': /* "unload" */
3446 case 'w': /* "wipe" */
3447 case 'd': return FALSE; /* "delete" */
3448 case 'h': return TRUE; /* "hide" */
3449 }
3450 return (p_hid || cmdmod.hide);
3451}
3452
3453/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003454 * Return TRUE when using ":vimgrep" for ":grep".
3455 */
3456 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003457grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003458{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003459 return ((cmdidx == CMD_grep
3460 || cmdidx == CMD_lgrep
3461 || cmdidx == CMD_grepadd
3462 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003463 && STRCMP("internal",
3464 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3465}
3466
3467/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003468 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 */
3470 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003471ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003473 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003475 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003477 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003478 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003479 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003480#ifdef FEAT_AUTOCMD
3481 char_u *au_name = NULL;
3482
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003483 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3484 if (grep_internal(eap->cmdidx))
3485 {
3486 ex_vimgrep(eap);
3487 return;
3488 }
3489
Bram Moolenaar7c626922005-02-07 22:01:03 +00003490 switch (eap->cmdidx)
3491 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003492 case CMD_make: au_name = (char_u *)"make"; break;
3493 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3494 case CMD_grep: au_name = (char_u *)"grep"; break;
3495 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3496 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3497 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003498 default: break;
3499 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003500 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3501 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003502 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003503# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003504 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003505 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003506# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003507 }
3508#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003509#ifdef FEAT_MBYTE
3510 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaara6557602006-02-04 22:43:20 +00003513 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3514 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003515 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003518 fname = get_mef_name();
3519 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003521 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523 /*
3524 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3525 */
3526 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3527 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003528 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 cmd = alloc(len);
3530 if (cmd == NULL)
3531 return;
3532 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3533 (char *)p_shq);
3534 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003535 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 /*
3537 * Output a newline if there's something else than the :make command that
3538 * was typed (in which case the cursor is in column 0).
3539 */
3540 if (msg_col == 0)
3541 msg_didout = FALSE;
3542 msg_start();
3543 MSG_PUTS(":!");
3544 msg_outtrans(cmd); /* show what we are doing */
3545
3546 /* let the shell know if we are redirecting output or not */
3547 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3548
3549#ifdef AMIGA
3550 out_flush();
3551 /* read window status report and redraw before message */
3552 (void)char_avail();
3553#endif
3554
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003555 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003556 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3557 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003558 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003559 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003560 if (wp != NULL)
3561 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003562#ifdef FEAT_AUTOCMD
3563 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003564 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003565 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3566 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003567 if (qi->qf_curlist < qi->qf_listcount)
3568 res = qi->qf_lists[qi->qf_curlist].qf_count;
3569 else
3570 res = 0;
3571 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003572#endif
3573 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003574 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar7c626922005-02-07 22:01:03 +00003576 mch_remove(fname);
3577 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 vim_free(cmd);
3579}
3580
3581/*
3582 * Return the name for the errorfile, in allocated memory.
3583 * Find a new unique name when 'makeef' contains "##".
3584 * Returns NULL for error.
3585 */
3586 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003587get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
3589 char_u *p;
3590 char_u *name;
3591 static int start = -1;
3592 static int off = 0;
3593#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003594 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595#endif
3596
3597 if (*p_mef == NUL)
3598 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003599 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (name == NULL)
3601 EMSG(_(e_notmp));
3602 return name;
3603 }
3604
3605 for (p = p_mef; *p; ++p)
3606 if (p[0] == '#' && p[1] == '#')
3607 break;
3608
3609 if (*p == NUL)
3610 return vim_strsave(p_mef);
3611
3612 /* Keep trying until the name doesn't exist yet. */
3613 for (;;)
3614 {
3615 if (start == -1)
3616 start = mch_get_pid();
3617 else
3618 off += 19;
3619
3620 name = alloc((unsigned)STRLEN(p_mef) + 30);
3621 if (name == NULL)
3622 break;
3623 STRCPY(name, p_mef);
3624 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3625 STRCAT(name, p + 2);
3626 if (mch_getperm(name) < 0
3627#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003628 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 && mch_lstat((char *)name, &sb) < 0
3630#endif
3631 )
3632 break;
3633 vim_free(name);
3634 }
3635 return name;
3636}
3637
3638/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003639 * Returns the number of valid entries in the current quickfix/location list.
3640 */
3641 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003642qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003643{
3644 qf_info_T *qi = &ql_info;
3645 qfline_T *qfp;
3646 int i, sz = 0;
3647 int prev_fnum = 0;
3648
3649 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3650 {
3651 /* Location list */
3652 qi = GET_LOC_LIST(curwin);
3653 if (qi == NULL)
3654 return 0;
3655 }
3656
3657 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003658 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003659 ++i, qfp = qfp->qf_next)
3660 {
3661 if (qfp->qf_valid)
3662 {
3663 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3664 sz++; /* Count all valid entries */
3665 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3666 {
3667 /* Count the number of files */
3668 sz++;
3669 prev_fnum = qfp->qf_fnum;
3670 }
3671 }
3672 }
3673
3674 return sz;
3675}
3676
3677/*
3678 * Returns the current index of the quickfix/location list.
3679 * Returns 0 if there is an error.
3680 */
3681 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003682qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003683{
3684 qf_info_T *qi = &ql_info;
3685
3686 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3687 {
3688 /* Location list */
3689 qi = GET_LOC_LIST(curwin);
3690 if (qi == NULL)
3691 return 0;
3692 }
3693
3694 return qi->qf_lists[qi->qf_curlist].qf_index;
3695}
3696
3697/*
3698 * Returns the current index in the quickfix/location list (counting only valid
3699 * entries). If no valid entries are in the list, then returns 1.
3700 */
3701 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003702qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003703{
3704 qf_info_T *qi = &ql_info;
3705 qf_list_T *qfl;
3706 qfline_T *qfp;
3707 int i, eidx = 0;
3708 int prev_fnum = 0;
3709
3710 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3711 {
3712 /* Location list */
3713 qi = GET_LOC_LIST(curwin);
3714 if (qi == NULL)
3715 return 1;
3716 }
3717
3718 qfl = &qi->qf_lists[qi->qf_curlist];
3719 qfp = qfl->qf_start;
3720
3721 /* check if the list has valid errors */
3722 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3723 return 1;
3724
3725 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3726 {
3727 if (qfp->qf_valid)
3728 {
3729 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3730 {
3731 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3732 {
3733 /* Count the number of files */
3734 eidx++;
3735 prev_fnum = qfp->qf_fnum;
3736 }
3737 }
3738 else
3739 eidx++;
3740 }
3741 }
3742
3743 return eidx ? eidx : 1;
3744}
3745
3746/*
3747 * Get the 'n'th valid error entry in the quickfix or location list.
3748 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3749 * For :cdo and :ldo returns the 'n'th valid error entry.
3750 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3751 */
3752 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003753qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003754{
3755 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3756 qfline_T *qfp = qfl->qf_start;
3757 int i, eidx;
3758 int prev_fnum = 0;
3759
3760 /* check if the list has valid errors */
3761 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3762 return 1;
3763
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003764 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003765 i++, qfp = qfp->qf_next)
3766 {
3767 if (qfp->qf_valid)
3768 {
3769 if (fdo)
3770 {
3771 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3772 {
3773 /* Count the number of files */
3774 eidx++;
3775 prev_fnum = qfp->qf_fnum;
3776 }
3777 }
3778 else
3779 eidx++;
3780 }
3781
3782 if (eidx == n)
3783 break;
3784 }
3785
3786 if (i <= qfl->qf_count)
3787 return i;
3788 else
3789 return 1;
3790}
3791
3792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003794 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003795 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 */
3797 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003798ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003800 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003801 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003802
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003803 if (eap->cmdidx == CMD_ll
3804 || eap->cmdidx == CMD_lrewind
3805 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003806 || eap->cmdidx == CMD_llast
3807 || eap->cmdidx == CMD_ldo
3808 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003809 {
3810 qi = GET_LOC_LIST(curwin);
3811 if (qi == NULL)
3812 {
3813 EMSG(_(e_loclist));
3814 return;
3815 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003816 }
3817
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003818 if (eap->addr_count > 0)
3819 errornr = (int)eap->line2;
3820 else
3821 {
3822 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3823 errornr = 0;
3824 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3825 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3826 errornr = 1;
3827 else
3828 errornr = 32767;
3829 }
3830
3831 /* For cdo and ldo commands, jump to the nth valid error.
3832 * For cfdo and lfdo commands, jump to the nth valid file entry.
3833 */
3834 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3835 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3836 errornr = qf_get_nth_valid_entry(qi,
3837 eap->addr_count > 0 ? (int)eap->line1 : 1,
3838 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3839
3840 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841}
3842
3843/*
3844 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003845 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003846 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 */
3848 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003849ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003851 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003852 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003853
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003854 if (eap->cmdidx == CMD_lnext
3855 || eap->cmdidx == CMD_lNext
3856 || eap->cmdidx == CMD_lprevious
3857 || eap->cmdidx == CMD_lnfile
3858 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003859 || eap->cmdidx == CMD_lpfile
3860 || eap->cmdidx == CMD_ldo
3861 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003862 {
3863 qi = GET_LOC_LIST(curwin);
3864 if (qi == NULL)
3865 {
3866 EMSG(_(e_loclist));
3867 return;
3868 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003869 }
3870
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003871 if (eap->addr_count > 0 &&
3872 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3873 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3874 errornr = (int)eap->line2;
3875 else
3876 errornr = 1;
3877
3878 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3879 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003881 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3882 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003884 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3885 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 ? BACKWARD_FILE
3887 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003888 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889}
3890
3891/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003892 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003893 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 */
3895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003896ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003898 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003899 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003900 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003901#ifdef FEAT_AUTOCMD
3902 char_u *au_name = NULL;
3903#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003904
3905 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003906 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003907 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003908
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003909#ifdef FEAT_AUTOCMD
3910 switch (eap->cmdidx)
3911 {
3912 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3913 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3914 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3915 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3916 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3917 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3918 default: break;
3919 }
3920 if (au_name != NULL)
3921 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3922#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003923#ifdef FEAT_MBYTE
3924 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3925#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003926#ifdef FEAT_BROWSE
3927 if (cmdmod.browse)
3928 {
3929 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3930 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3931 if (browse_file == NULL)
3932 return;
3933 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3934 vim_free(browse_file);
3935 }
3936 else
3937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003939 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003940
3941 /*
3942 * This function is used by the :cfile, :cgetfile and :caddfile
3943 * commands.
3944 * :cfile always creates a new quickfix list and jumps to the
3945 * first error.
3946 * :cgetfile creates a new quickfix list but doesn't jump to the
3947 * first error.
3948 * :caddfile adds to an existing quickfix list. If there is no
3949 * quickfix list then a new list is created.
3950 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003951 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003952 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003953 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003954 && (eap->cmdidx == CMD_cfile
3955 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003956 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003957#ifdef FEAT_AUTOCMD
3958 if (au_name != NULL)
3959 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3960#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003961 if (wp != NULL)
3962 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003963 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003964 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003965
3966 else
3967 {
3968#ifdef FEAT_AUTOCMD
3969 if (au_name != NULL)
3970 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3971#endif
3972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973}
3974
3975/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003976 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003977 * ":vimgrepadd {pattern} file(s)"
3978 * ":lvimgrep {pattern} file(s)"
3979 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003980 */
3981 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003982ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003983{
Bram Moolenaar81695252004-12-29 20:58:21 +00003984 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003985 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003986 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003987 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003988 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003989 char_u *s;
3990 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003991 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003992 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003993#ifdef FEAT_AUTOCMD
3994 qfline_T *cur_qf_start;
3995#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003996 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003997 buf_T *buf;
3998 int duplicate_name = FALSE;
3999 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004000 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004001 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004002 buf_T *first_match_buf = NULL;
4003 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004004 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004005#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4006 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004007#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004008 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004009 int flags = 0;
4010 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004011 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004012 char_u *dirname_start = NULL;
4013 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004014 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004015#ifdef FEAT_AUTOCMD
4016 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004017
4018 switch (eap->cmdidx)
4019 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004020 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4021 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4022 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004023 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004024 case CMD_grep: au_name = (char_u *)"grep"; break;
4025 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4026 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4027 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004028 default: break;
4029 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004030 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4031 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004032 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004033# ifdef FEAT_EVAL
4034 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004035 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004036# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004037 }
4038#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004039
Bram Moolenaar754b5602006-02-09 23:53:20 +00004040 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004041 || eap->cmdidx == CMD_lvimgrep
4042 || eap->cmdidx == CMD_lgrepadd
4043 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004044 {
4045 qi = ll_get_or_alloc_list(curwin);
4046 if (qi == NULL)
4047 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004048 }
4049
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004050 if (eap->addr_count > 0)
4051 tomatch = eap->line2;
4052 else
4053 tomatch = MAXLNUM;
4054
Bram Moolenaar81695252004-12-29 20:58:21 +00004055 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004056 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004057 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004058 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004059 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004060 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004061 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004062 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004063 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004064
4065 if (s != NULL && *s == NUL)
4066 {
4067 /* Pattern is empty, use last search pattern. */
4068 if (last_search_pat() == NULL)
4069 {
4070 EMSG(_(e_noprevre));
4071 goto theend;
4072 }
4073 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4074 }
4075 else
4076 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4077
Bram Moolenaar86b68352004-12-27 21:59:20 +00004078 if (regmatch.regprog == NULL)
4079 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004080 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004081 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004082
4083 p = skipwhite(p);
4084 if (*p == NUL)
4085 {
4086 EMSG(_("E683: File name missing or invalid pattern"));
4087 goto theend;
4088 }
4089
Bram Moolenaar754b5602006-02-09 23:53:20 +00004090 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004091 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004092 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004093 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004094 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004095
4096 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004097 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004098 goto theend;
4099 if (fcount == 0)
4100 {
4101 EMSG(_(e_nomatch));
4102 goto theend;
4103 }
4104
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004105 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4106 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004107 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004108 {
4109 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004110 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004111 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004112
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004113 /* Remember the current directory, because a BufRead autocommand that does
4114 * ":lcd %:p:h" changes the meaning of short path names. */
4115 mch_dirname(dirname_start, MAXPATHL);
4116
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004117#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004118 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004119 * changing the current quickfix list. */
4120 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4121#endif
4122
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004123 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004124 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004125 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004126 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004127 if (time(NULL) > seconds)
4128 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004129 /* Display the file name every second or so, show the user we are
4130 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004131 seconds = time(NULL);
4132 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004133 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004134 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004135 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004136 else
4137 {
4138 msg_outtrans(p);
4139 vim_free(p);
4140 }
4141 msg_clr_eos();
4142 msg_didout = FALSE; /* overwrite this message */
4143 msg_nowait = TRUE; /* don't wait for this message */
4144 msg_col = 0;
4145 out_flush();
4146 }
4147
Bram Moolenaar81695252004-12-29 20:58:21 +00004148 buf = buflist_findname_exp(fnames[fi]);
4149 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4150 {
4151 /* Remember that a buffer with this name already exists. */
4152 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004153 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004154 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004155
4156#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4157 /* Don't do Filetype autocommands to avoid loading syntax and
4158 * indent scripts, a great speed improvement. */
4159 save_ei = au_event_disable(",Filetype");
4160#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004161 /* Don't use modelines here, it's useless. */
4162 save_mls = p_mls;
4163 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004164
4165 /* Load file into a buffer, so that 'fileencoding' is detected,
4166 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004167 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004168
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004169 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004170#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4171 au_event_restore(save_ei);
4172#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004173 }
4174 else
4175 /* Use existing, loaded buffer. */
4176 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004177
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004178#ifdef FEAT_AUTOCMD
4179 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4180 {
4181 int idx;
4182
4183 /* Autocommands changed the quickfix list. Find the one we were
4184 * using and restore it. */
4185 for (idx = 0; idx < LISTCOUNT; ++idx)
4186 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4187 {
4188 qi->qf_curlist = idx;
4189 break;
4190 }
4191 if (idx == LISTCOUNT)
4192 {
4193 /* List cannot be found, create a new one. */
4194 qf_new_list(qi, *eap->cmdlinep);
4195 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4196 }
4197 }
4198#endif
4199
Bram Moolenaar81695252004-12-29 20:58:21 +00004200 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004201 {
4202 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004203 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004204 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004205 else
4206 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004207 /* Try for a match in all lines of the buffer.
4208 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004209 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004210 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4211 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004212 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004213 col = 0;
4214 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004215 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004216 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004217 /* Pass the buffer number so that it gets used even for a
4218 * dummy buffer, unless duplicate_name is set, then the
4219 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004220 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004221 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004222 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004223 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004224 ml_get_buf(buf,
4225 regmatch.startpos[0].lnum + lnum, FALSE),
4226 regmatch.startpos[0].lnum + lnum,
4227 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004228 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004229 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004230 0, /* nr */
4231 0, /* type */
4232 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004233 ) == FAIL)
4234 {
4235 got_int = TRUE;
4236 break;
4237 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004238 found_match = TRUE;
4239 if (--tomatch == 0)
4240 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004241 if ((flags & VGR_GLOBAL) == 0
4242 || regmatch.endpos[0].lnum > 0)
4243 break;
4244 col = regmatch.endpos[0].col
4245 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004246 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004247 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004248 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004249 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004250 if (got_int)
4251 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004252 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004253#ifdef FEAT_AUTOCMD
4254 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4255#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004256
4257 if (using_dummy)
4258 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004259 if (found_match && first_match_buf == NULL)
4260 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004261 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004262 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004263 /* Never keep a dummy buffer if there is another buffer
4264 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004265 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004266 buf = NULL;
4267 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004268 else if (!cmdmod.hide
4269 || buf->b_p_bh[0] == 'u' /* "unload" */
4270 || buf->b_p_bh[0] == 'w' /* "wipe" */
4271 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004272 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004273 /* When no match was found we don't need to remember the
4274 * buffer, wipe it out. If there was a match and it
4275 * wasn't the first one or we won't jump there: only
4276 * unload the buffer.
4277 * Ignore 'hidden' here, because it may lead to having too
4278 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004279 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004280 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004281 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004282 buf = NULL;
4283 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004284 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004285 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004286 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004287 /* Keeping the buffer, remove the dummy flag. */
4288 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004289 buf = NULL;
4290 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004291 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004292
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004293 if (buf != NULL)
4294 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004295 /* Keeping the buffer, remove the dummy flag. */
4296 buf->b_flags &= ~BF_DUMMY;
4297
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004298 /* If the buffer is still loaded we need to use the
4299 * directory we jumped to below. */
4300 if (buf == first_match_buf
4301 && target_dir == NULL
4302 && STRCMP(dirname_start, dirname_now) != 0)
4303 target_dir = vim_strsave(dirname_now);
4304
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004305 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004306 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004307 * need to be done (again). But not the window-local
4308 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004309 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004310#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004311 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4312 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004313#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004314 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004315 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004316 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004317 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004318 }
4319 }
4320
4321 FreeWild(fcount, fnames);
4322
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004323 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4324 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4325 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004326
4327#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004328 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004329#endif
4330
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004331#ifdef FEAT_AUTOCMD
4332 if (au_name != NULL)
4333 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4334 curbuf->b_fname, TRUE, curbuf);
4335#endif
4336
Bram Moolenaar86b68352004-12-27 21:59:20 +00004337 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004338 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004339 {
4340 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004341 {
4342 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004343 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004344 if (buf != curbuf)
4345 /* If we jumped to another buffer redrawing will already be
4346 * taken care of. */
4347 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004348
4349 /* Jump to the directory used after loading the buffer. */
4350 if (curbuf == first_match_buf && target_dir != NULL)
4351 {
4352 exarg_T ea;
4353
4354 ea.arg = target_dir;
4355 ea.cmdidx = CMD_lcd;
4356 ex_cd(&ea);
4357 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004358 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004359 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004360 else
4361 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004362
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004363 /* If we loaded a dummy buffer into the current window, the autocommands
4364 * may have messed up things, need to redraw and recompute folds. */
4365 if (redraw_for_dummy)
4366 {
4367#ifdef FEAT_FOLDING
4368 foldUpdateAll(curwin);
4369#else
4370 redraw_later(NOT_VALID);
4371#endif
4372 }
4373
Bram Moolenaar86b68352004-12-27 21:59:20 +00004374theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004375 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004376 vim_free(dirname_now);
4377 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004378 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004379 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004380}
4381
4382/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004383 * Restore current working directory to "dirname_start" if they differ, taking
4384 * into account whether it is set locally or globally.
4385 */
4386 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004387restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004388{
4389 char_u *dirname_now = alloc(MAXPATHL);
4390
4391 if (NULL != dirname_now)
4392 {
4393 mch_dirname(dirname_now, MAXPATHL);
4394 if (STRCMP(dirname_start, dirname_now) != 0)
4395 {
4396 /* If the directory has changed, change it back by building up an
4397 * appropriate ex command and executing it. */
4398 exarg_T ea;
4399
4400 ea.arg = dirname_start;
4401 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4402 ex_cd(&ea);
4403 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004404 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004405 }
4406}
4407
4408/*
4409 * Load file "fname" into a dummy buffer and return the buffer pointer,
4410 * placing the directory resulting from the buffer load into the
4411 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4412 * prior to calling this function. Restores directory to "dirname_start" prior
4413 * to returning, if autocmds or the 'autochdir' option have changed it.
4414 *
4415 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4416 * or wipe_dummy_buffer() later!
4417 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004418 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004419 */
4420 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004421load_dummy_buffer(
4422 char_u *fname,
4423 char_u *dirname_start, /* in: old directory */
4424 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004425{
4426 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004427 bufref_T newbufref;
4428 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004429 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004430 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004431
4432 /* Allocate a buffer without putting it in the buffer list. */
4433 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4434 if (newbuf == NULL)
4435 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004436 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004437
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004438 /* Init the options. */
4439 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4440
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004441 /* need to open the memfile before putting the buffer in a window */
4442 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004443 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004444 /* set curwin/curbuf to buf and save a few things */
4445 aucmd_prepbuf(&aco, newbuf);
4446
4447 /* Need to set the filename for autocommands. */
4448 (void)setfname(curbuf, fname, NULL, FALSE);
4449
Bram Moolenaar81695252004-12-29 20:58:21 +00004450 /* Create swap file now to avoid the ATTENTION message. */
4451 check_need_swap(TRUE);
4452
4453 /* Remove the "dummy" flag, otherwise autocommands may not
4454 * work. */
4455 curbuf->b_flags &= ~BF_DUMMY;
4456
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004457 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004458 if (readfile(fname, NULL,
4459 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4460 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004461 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004462 && !(curbuf->b_flags & BF_NEW))
4463 {
4464 failed = FALSE;
4465 if (curbuf != newbuf)
4466 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004467 /* Bloody autocommands changed the buffer! Can happen when
4468 * using netrw and editing a remote file. Use the current
4469 * buffer instead, delete the dummy one after restoring the
4470 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004471 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004472 newbuf = curbuf;
4473 }
4474 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004475
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004476 /* restore curwin/curbuf and a few other things */
4477 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004478 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4479 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004480
4481 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4482 * skip it. */
4483 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004484 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004485
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004486 /*
4487 * When autocommands/'autochdir' option changed directory: go back.
4488 * Let the caller know what the resulting dir was first, in case it is
4489 * important.
4490 */
4491 mch_dirname(resulting_dir, MAXPATHL);
4492 restore_start_dir(dirname_start);
4493
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004494 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004495 return NULL;
4496 if (failed)
4497 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004498 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004499 return NULL;
4500 }
4501 return newbuf;
4502}
4503
4504/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004505 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4506 * directory to "dirname_start" prior to returning, if autocmds or the
4507 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004508 */
4509 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004510wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004511{
4512 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004513 {
4514#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4515 cleanup_T cs;
4516
4517 /* Reset the error/interrupt/exception state here so that aborting()
4518 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4519 * work when got_int is set. */
4520 enter_cleanup(&cs);
4521#endif
4522
Bram Moolenaar81695252004-12-29 20:58:21 +00004523 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004524
4525#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4526 /* Restore the error/interrupt/exception state if not discarded by a
4527 * new aborting error, interrupt, or uncaught exception. */
4528 leave_cleanup(&cs);
4529#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004530 /* When autocommands/'autochdir' option changed directory: go back. */
4531 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004532 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004533}
4534
4535/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004536 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4537 * directory to "dirname_start" prior to returning, if autocmds or the
4538 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004539 */
4540 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004541unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004542{
4543 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004544 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004545 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004546
4547 /* When autocommands/'autochdir' option changed directory: go back. */
4548 restore_start_dir(dirname_start);
4549 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004550}
4551
Bram Moolenaar05159a02005-02-26 23:04:13 +00004552#if defined(FEAT_EVAL) || defined(PROTO)
4553/*
4554 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004555 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004556 */
4557 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004558get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004559{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004560 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004561 dict_T *dict;
4562 char_u buf[2];
4563 qfline_T *qfp;
4564 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004565 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004566
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004567 if (wp != NULL)
4568 {
4569 qi = GET_LOC_LIST(wp);
4570 if (qi == NULL)
4571 return FAIL;
4572 }
4573
Bram Moolenaard823fa92016-08-12 16:29:27 +02004574 if (qf_idx == -1)
4575 qf_idx = qi->qf_curlist;
4576
4577 if (qf_idx >= qi->qf_listcount
4578 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004579 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004580
Bram Moolenaard823fa92016-08-12 16:29:27 +02004581 qfp = qi->qf_lists[qf_idx].qf_start;
4582 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004583 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004584 /* Handle entries with a non-existing buffer number. */
4585 bufnum = qfp->qf_fnum;
4586 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4587 bufnum = 0;
4588
Bram Moolenaar05159a02005-02-26 23:04:13 +00004589 if ((dict = dict_alloc()) == NULL)
4590 return FAIL;
4591 if (list_append_dict(list, dict) == FAIL)
4592 return FAIL;
4593
4594 buf[0] = qfp->qf_type;
4595 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004596 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004597 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4598 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4599 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4600 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004601 || dict_add_nr_str(dict, "pattern", 0L,
4602 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4603 || dict_add_nr_str(dict, "text", 0L,
4604 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004605 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4606 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4607 return FAIL;
4608
4609 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004610 if (qfp == NULL)
4611 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004612 }
4613 return OK;
4614}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004615
4616/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004617 * Flags used by getqflist()/getloclist() to determine which fields to return.
4618 */
4619enum {
4620 QF_GETLIST_NONE = 0x0,
4621 QF_GETLIST_TITLE = 0x1,
4622 QF_GETLIST_ITEMS = 0x2,
4623 QF_GETLIST_NR = 0x4,
4624 QF_GETLIST_WINID = 0x8,
4625 QF_GETLIST_ALL = 0xFF
4626};
4627
4628/*
4629 * Return quickfix/location list details (title) as a
4630 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4631 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004632 */
4633 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004634get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4635{
4636 qf_info_T *qi = &ql_info;
4637 int status = OK;
4638 int qf_idx;
4639 dictitem_T *di;
4640 int flags = QF_GETLIST_NONE;
4641
4642 if (wp != NULL)
4643 {
4644 qi = GET_LOC_LIST(wp);
4645 if (qi == NULL)
4646 return FAIL;
4647 }
4648
4649 qf_idx = qi->qf_curlist; /* default is the current list */
4650 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4651 {
4652 /* Use the specified quickfix/location list */
4653 if (di->di_tv.v_type == VAR_NUMBER)
4654 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004655 /* for zero use the current list */
4656 if (di->di_tv.vval.v_number != 0)
4657 {
4658 qf_idx = di->di_tv.vval.v_number - 1;
4659 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4660 return FAIL;
4661 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004662 flags |= QF_GETLIST_NR;
4663 }
4664 else
4665 return FAIL;
4666 }
4667
4668 if (dict_find(what, (char_u *)"all", -1) != NULL)
4669 flags |= QF_GETLIST_ALL;
4670
4671 if (dict_find(what, (char_u *)"title", -1) != NULL)
4672 flags |= QF_GETLIST_TITLE;
4673
4674 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4675 flags |= QF_GETLIST_WINID;
4676
4677 if (flags & QF_GETLIST_TITLE)
4678 {
4679 char_u *t;
4680 t = qi->qf_lists[qf_idx].qf_title;
4681 if (t == NULL)
4682 t = (char_u *)"";
4683 status = dict_add_nr_str(retdict, "title", 0L, t);
4684 }
4685 if ((status == OK) && (flags & QF_GETLIST_NR))
4686 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4687 if ((status == OK) && (flags & QF_GETLIST_WINID))
4688 {
4689 win_T *win;
4690 win = qf_find_win(qi);
4691 if (win != NULL)
4692 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4693 }
4694
4695 return status;
4696}
4697
4698/*
4699 * Add list of entries to quickfix/location list. Each list entry is
4700 * a dictionary with item information.
4701 */
4702 static int
4703qf_add_entries(
4704 qf_info_T *qi,
4705 list_T *list,
4706 char_u *title,
4707 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004708{
4709 listitem_T *li;
4710 dict_T *d;
4711 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004712 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004713 long lnum;
4714 int col, nr;
4715 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004716#ifdef FEAT_WINDOWS
4717 qfline_T *old_last = NULL;
4718#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004719 int valid, status;
4720 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004721 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004722
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004723 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004724 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004725 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004726#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004727 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4728 /* Adding to existing list, use last entry. */
4729 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004730#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004731 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004732 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004733 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004734 qf_store_title(qi, title);
4735 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004736
4737 for (li = list->lv_first; li != NULL; li = li->li_next)
4738 {
4739 if (li->li_tv.v_type != VAR_DICT)
4740 continue; /* Skip non-dict items */
4741
4742 d = li->li_tv.vval.v_dict;
4743 if (d == NULL)
4744 continue;
4745
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004746 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004747 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4748 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4749 col = (int)get_dict_number(d, (char_u *)"col");
4750 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4751 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004752 type = get_dict_string(d, (char_u *)"type", TRUE);
4753 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4754 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004755 if (text == NULL)
4756 text = vim_strsave((char_u *)"");
4757
4758 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004759 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004760 valid = FALSE;
4761
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004762 /* Mark entries with non-existing buffer number as not valid. Give the
4763 * error message only once. */
4764 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4765 {
4766 if (!did_bufnr_emsg)
4767 {
4768 did_bufnr_emsg = TRUE;
4769 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4770 }
4771 valid = FALSE;
4772 bufnum = 0;
4773 }
4774
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004775 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004776 NULL, /* dir */
4777 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004778 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004779 text,
4780 lnum,
4781 col,
4782 vcol, /* vis_col */
4783 pattern, /* search pattern */
4784 nr,
4785 type == NULL ? NUL : *type,
4786 valid);
4787
4788 vim_free(filename);
4789 vim_free(pattern);
4790 vim_free(text);
4791 vim_free(type);
4792
4793 if (status == FAIL)
4794 {
4795 retval = FAIL;
4796 break;
4797 }
4798 }
4799
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004800 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004801 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004802 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4803 else
4804 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004805 if (action != 'a')
4806 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004807 qi->qf_lists[qi->qf_curlist].qf_ptr =
4808 qi->qf_lists[qi->qf_curlist].qf_start;
4809 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4810 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4811 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004812
4813#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004814 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004815 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004816#endif
4817
4818 return retval;
4819}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004820
4821 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004822qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004823{
4824 dictitem_T *di;
4825 int retval = FAIL;
4826 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004827 int newlist = FALSE;
4828
4829 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4830 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004831
4832 qf_idx = qi->qf_curlist; /* default is the current list */
4833 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4834 {
4835 /* Use the specified quickfix/location list */
4836 if (di->di_tv.v_type == VAR_NUMBER)
4837 {
4838 qf_idx = di->di_tv.vval.v_number - 1;
4839 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4840 return FAIL;
4841 }
4842 else
4843 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004844 newlist = FALSE; /* use the specified list */
4845 }
4846
4847 if (newlist)
4848 {
4849 qf_new_list(qi, NULL);
4850 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004851 }
4852
4853 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4854 {
4855 if (di->di_tv.v_type == VAR_STRING)
4856 {
4857 vim_free(qi->qf_lists[qf_idx].qf_title);
4858 qi->qf_lists[qf_idx].qf_title =
4859 get_dict_string(what, (char_u *)"title", TRUE);
4860 if (qf_idx == qi->qf_curlist)
4861 qf_update_win_titlevar(qi);
4862 retval = OK;
4863 }
4864 }
4865
4866 return retval;
4867}
4868
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004869 static void
4870qf_free_stack(win_T *wp, qf_info_T *qi)
4871{
4872 qf_free_all(wp);
4873 if (wp == NULL)
4874 {
4875 /* quickfix list */
4876 qi->qf_curlist = 0;
4877 qi->qf_listcount = 0;
4878 }
4879}
4880
Bram Moolenaard823fa92016-08-12 16:29:27 +02004881/*
4882 * Populate the quickfix list with the items supplied in the list
4883 * of dictionaries. "title" will be copied to w:quickfix_title.
4884 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
4885 */
4886 int
4887set_errorlist(
4888 win_T *wp,
4889 list_T *list,
4890 int action,
4891 char_u *title,
4892 dict_T *what)
4893{
4894 qf_info_T *qi = &ql_info;
4895 int retval = OK;
4896
4897 if (wp != NULL)
4898 {
4899 qi = ll_get_or_alloc_list(wp);
4900 if (qi == NULL)
4901 return FAIL;
4902 }
4903
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004904 if (action == 'f')
4905 {
4906 /* Free the entire quickfix or location list stack */
4907 qf_free_stack(wp, qi);
4908 }
4909 else if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004910 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004911 else
4912 retval = qf_add_entries(qi, list, title, action);
4913
4914 return retval;
4915}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004916#endif
4917
Bram Moolenaar81695252004-12-29 20:58:21 +00004918/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004919 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004920 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004921 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004922 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004923 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004924 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004925 */
4926 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004927ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004928{
4929 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004930 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004931#ifdef FEAT_AUTOCMD
4932 char_u *au_name = NULL;
4933#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004934
Bram Moolenaardb552d602006-03-23 22:59:57 +00004935 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4936 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004937 {
4938 qi = ll_get_or_alloc_list(curwin);
4939 if (qi == NULL)
4940 return;
4941 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004942
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004943#ifdef FEAT_AUTOCMD
4944 switch (eap->cmdidx)
4945 {
4946 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
4947 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
4948 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
4949 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
4950 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
4951 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
4952 default: break;
4953 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004954 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4955 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004956 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004957# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004958 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004959 return;
4960# endif
4961 }
4962#endif
4963
Bram Moolenaar86b68352004-12-27 21:59:20 +00004964 if (*eap->arg == NUL)
4965 buf = curbuf;
4966 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4967 buf = buflist_findnr(atoi((char *)eap->arg));
4968 if (buf == NULL)
4969 EMSG(_(e_invarg));
4970 else if (buf->b_ml.ml_mfp == NULL)
4971 EMSG(_("E681: Buffer is not loaded"));
4972 else
4973 {
4974 if (eap->addr_count == 0)
4975 {
4976 eap->line1 = 1;
4977 eap->line2 = buf->b_ml.ml_line_count;
4978 }
4979 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4980 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4981 EMSG(_(e_invrange));
4982 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004983 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004984 char_u *qf_title = *eap->cmdlinep;
4985
4986 if (buf->b_sfname)
4987 {
4988 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4989 (char *)qf_title, (char *)buf->b_sfname);
4990 qf_title = IObuff;
4991 }
4992
Bram Moolenaardb552d602006-03-23 22:59:57 +00004993 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4994 (eap->cmdidx != CMD_caddbuffer
4995 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004996 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004997 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004998 {
4999#ifdef FEAT_AUTOCMD
5000 if (au_name != NULL)
5001 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5002 curbuf->b_fname, TRUE, curbuf);
5003#endif
5004 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5005 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5006 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005007 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005008 }
5009}
5010
Bram Moolenaar1e015462005-09-25 22:16:38 +00005011#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005012/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005013 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5014 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005015 */
5016 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005017ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005018{
5019 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005020 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005021#ifdef FEAT_AUTOCMD
5022 char_u *au_name = NULL;
5023#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005024
Bram Moolenaardb552d602006-03-23 22:59:57 +00005025 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5026 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005027 {
5028 qi = ll_get_or_alloc_list(curwin);
5029 if (qi == NULL)
5030 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005031 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005032
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005033#ifdef FEAT_AUTOCMD
5034 switch (eap->cmdidx)
5035 {
5036 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5037 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5038 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5039 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5040 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5041 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5042 default: break;
5043 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005044 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5045 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005046 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005047# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005048 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005049 return;
5050# endif
5051 }
5052#endif
5053
Bram Moolenaar4770d092006-01-12 23:22:24 +00005054 /* Evaluate the expression. When the result is a string or a list we can
5055 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005056 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005057 if (tv != NULL)
5058 {
5059 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5060 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5061 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01005062 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005063 (eap->cmdidx != CMD_caddexpr
5064 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005065 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5066 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005067 {
5068#ifdef FEAT_AUTOCMD
5069 if (au_name != NULL)
5070 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5071 curbuf->b_fname, TRUE, curbuf);
5072#endif
5073 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5074 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5075 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005076 }
5077 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005078 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005079 free_tv(tv);
5080 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005081}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005082#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005083
5084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 * ":helpgrep {pattern}"
5086 */
5087 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005088ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089{
5090 regmatch_T regmatch;
5091 char_u *save_cpo;
5092 char_u *p;
5093 int fcount;
5094 char_u **fnames;
5095 FILE *fd;
5096 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005098#ifdef FEAT_MULTI_LANG
5099 char_u *lang;
5100#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005101 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005102 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005103 int new_qi = FALSE;
5104 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005105#ifdef FEAT_AUTOCMD
5106 char_u *au_name = NULL;
5107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005109#ifdef FEAT_MULTI_LANG
5110 /* Check for a specified language */
5111 lang = check_help_lang(eap->arg);
5112#endif
5113
Bram Moolenaar73633f82012-01-20 13:39:07 +01005114#ifdef FEAT_AUTOCMD
5115 switch (eap->cmdidx)
5116 {
5117 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5118 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5119 default: break;
5120 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005121 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5122 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005123 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005124# ifdef FEAT_EVAL
5125 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005126 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005127# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005128 }
5129#endif
5130
5131 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5132 save_cpo = p_cpo;
5133 p_cpo = empty_option;
5134
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005135 if (eap->cmdidx == CMD_lhelpgrep)
5136 {
5137 /* Find an existing help window */
5138 FOR_ALL_WINDOWS(wp)
5139 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5140 break;
5141
5142 if (wp == NULL) /* Help window not found */
5143 qi = NULL;
5144 else
5145 qi = wp->w_llist;
5146
5147 if (qi == NULL)
5148 {
5149 /* Allocate a new location list for help text matches */
5150 if ((qi = ll_new_list()) == NULL)
5151 return;
5152 new_qi = TRUE;
5153 }
5154 }
5155
Bram Moolenaaree85df32017-03-19 14:19:50 +01005156 /* Autocommands may change the list. Save it for later comparison */
5157 save_qi = qi;
5158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5160 regmatch.rm_ic = FALSE;
5161 if (regmatch.regprog != NULL)
5162 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005163#ifdef FEAT_MBYTE
5164 vimconv_T vc;
5165
5166 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5167 * differs. */
5168 vc.vc_type = CONV_NONE;
5169 if (!enc_utf8)
5170 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5171#endif
5172
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005174 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
5176 /* Go through all directories in 'runtimepath' */
5177 p = p_rtp;
5178 while (*p != NUL && !got_int)
5179 {
5180 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5181
5182 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5183 add_pathsep(NameBuff);
5184 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5185 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5186 &fnames, EW_FILE|EW_SILENT) == OK
5187 && fcount > 0)
5188 {
5189 for (fi = 0; fi < fcount && !got_int; ++fi)
5190 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005191#ifdef FEAT_MULTI_LANG
5192 /* Skip files for a different language. */
5193 if (lang != NULL
5194 && STRNICMP(lang, fnames[fi]
5195 + STRLEN(fnames[fi]) - 3, 2) != 0
5196 && !(STRNICMP(lang, "en", 2) == 0
5197 && STRNICMP("txt", fnames[fi]
5198 + STRLEN(fnames[fi]) - 3, 3) == 0))
5199 continue;
5200#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005201 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (fd != NULL)
5203 {
5204 lnum = 1;
5205 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5206 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005207 char_u *line = IObuff;
5208#ifdef FEAT_MBYTE
5209 /* Convert a line if 'encoding' is not utf-8 and
5210 * the line contains a non-ASCII character. */
5211 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005212 && has_non_ascii(IObuff))
5213 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005214 line = string_convert(&vc, IObuff, NULL);
5215 if (line == NULL)
5216 line = IObuff;
5217 }
5218#endif
5219
5220 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005222 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
5224 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005225 while (l > 0 && line[l - 1] <= ' ')
5226 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005228 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 NULL, /* dir */
5230 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005231 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005232 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005234 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005235 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005236 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005237 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 0, /* nr */
5239 1, /* type */
5240 TRUE /* valid */
5241 ) == FAIL)
5242 {
5243 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005244#ifdef FEAT_MBYTE
5245 if (line != IObuff)
5246 vim_free(line);
5247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 break;
5249 }
5250 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005251#ifdef FEAT_MBYTE
5252 if (line != IObuff)
5253 vim_free(line);
5254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 ++lnum;
5256 line_breakcheck();
5257 }
5258 fclose(fd);
5259 }
5260 }
5261 FreeWild(fcount, fnames);
5262 }
5263 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005264
Bram Moolenaar473de612013-06-08 18:19:48 +02005265 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005266#ifdef FEAT_MBYTE
5267 if (vc.vc_type != CONV_NONE)
5268 convert_setup(&vc, NULL, NULL);
5269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005271 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5272 qi->qf_lists[qi->qf_curlist].qf_ptr =
5273 qi->qf_lists[qi->qf_curlist].qf_start;
5274 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005277 if (p_cpo == empty_option)
5278 p_cpo = save_cpo;
5279 else
5280 /* Darn, some plugin changed the value. */
5281 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005284 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#endif
5286
Bram Moolenaar73633f82012-01-20 13:39:07 +01005287#ifdef FEAT_AUTOCMD
5288 if (au_name != NULL)
5289 {
5290 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5291 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005292 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005293 /* autocommands made "qi" invalid */
5294 return;
5295 }
5296#endif
5297
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005299 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005300 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005301 else
5302 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005303
5304 if (eap->cmdidx == CMD_lhelpgrep)
5305 {
5306 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005307 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005308 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5309 {
5310 if (new_qi)
5311 ll_free_all(&qi);
5312 }
5313 else if (curwin->w_llist == NULL)
5314 curwin->w_llist = qi;
5315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316}
5317
5318#endif /* FEAT_QUICKFIX */