blob: d2921b21c6a8daa75a48e539ae630c31023be9ed [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 Moolenaar31bdd132017-04-15 15:22:52 +02002753 qi->qf_lists[idx].qf_last = NULL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002754
2755 qf_clean_dir_stack(&qi->qf_dir_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002756 qi->qf_directory = NULL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002757 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002758 qi->qf_currfile = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759}
2760
2761/*
2762 * qf_mark_adjust: adjust marks
2763 */
2764 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002765qf_mark_adjust(
2766 win_T *wp,
2767 linenr_T line1,
2768 linenr_T line2,
2769 long amount,
2770 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002772 int i;
2773 qfline_T *qfp;
2774 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002775 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002776 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002777 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
Bram Moolenaarc1542742016-07-20 21:44:37 +02002779 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002780 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002781 if (wp != NULL)
2782 {
2783 if (wp->w_llist == NULL)
2784 return;
2785 qi = wp->w_llist;
2786 }
2787
2788 for (idx = 0; idx < qi->qf_listcount; ++idx)
2789 if (qi->qf_lists[idx].qf_count)
2790 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002791 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2792 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (qfp->qf_fnum == curbuf->b_fnum)
2794 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002795 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2797 {
2798 if (amount == MAXLNUM)
2799 qfp->qf_cleared = TRUE;
2800 else
2801 qfp->qf_lnum += amount;
2802 }
2803 else if (amount_after && qfp->qf_lnum > line2)
2804 qfp->qf_lnum += amount_after;
2805 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002806
2807 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002808 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809}
2810
2811/*
2812 * Make a nice message out of the error character and the error number:
2813 * char number message
2814 * e or E 0 " error"
2815 * w or W 0 " warning"
2816 * i or I 0 " info"
2817 * 0 0 ""
2818 * other 0 " c"
2819 * e or E n " error n"
2820 * w or W n " warning n"
2821 * i or I n " info n"
2822 * 0 n " error n"
2823 * other n " c n"
2824 * 1 x "" :helpgrep
2825 */
2826 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002827qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
2829 static char_u buf[20];
2830 static char_u cc[3];
2831 char_u *p;
2832
2833 if (c == 'W' || c == 'w')
2834 p = (char_u *)" warning";
2835 else if (c == 'I' || c == 'i')
2836 p = (char_u *)" info";
2837 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2838 p = (char_u *)" error";
2839 else if (c == 0 || c == 1)
2840 p = (char_u *)"";
2841 else
2842 {
2843 cc[0] = ' ';
2844 cc[1] = c;
2845 cc[2] = NUL;
2846 p = cc;
2847 }
2848
2849 if (nr <= 0)
2850 return p;
2851
2852 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2853 return buf;
2854}
2855
2856#if defined(FEAT_WINDOWS) || defined(PROTO)
2857/*
2858 * ":cwindow": open the quickfix window if we have errors to display,
2859 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002860 * ":lwindow": open the location list window if we have locations to display,
2861 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 */
2863 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002864ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002866 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 win_T *win;
2868
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002869 if (eap->cmdidx == CMD_lwindow)
2870 {
2871 qi = GET_LOC_LIST(curwin);
2872 if (qi == NULL)
2873 return;
2874 }
2875
2876 /* Look for an existing quickfix window. */
2877 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
2879 /*
2880 * If a quickfix window is open but we have no errors to display,
2881 * close the window. If a quickfix window is not open, then open
2882 * it if we have errors; otherwise, leave it closed.
2883 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002884 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002885 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002886 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
2888 if (win != NULL)
2889 ex_cclose(eap);
2890 }
2891 else if (win == NULL)
2892 ex_copen(eap);
2893}
2894
2895/*
2896 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002897 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002900ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002902 win_T *win = NULL;
2903 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002905 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2906 {
2907 qi = GET_LOC_LIST(curwin);
2908 if (qi == NULL)
2909 return;
2910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002912 /* Find existing quickfix window and close it. */
2913 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 if (win != NULL)
2915 win_close(win, FALSE);
2916}
2917
2918/*
2919 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002920 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 */
2922 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002923ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002925 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002928 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002929 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002930 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002932 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2933 {
2934 qi = GET_LOC_LIST(curwin);
2935 if (qi == NULL)
2936 {
2937 EMSG(_(e_loclist));
2938 return;
2939 }
2940 }
2941
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 if (eap->addr_count != 0)
2943 height = eap->line2;
2944 else
2945 height = QF_WINHEIGHT;
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948#ifdef FEAT_GUI
2949 need_mouse_correct = TRUE;
2950#endif
2951
2952 /*
2953 * Find existing quickfix window, or open a new one.
2954 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002955 win = qf_find_win(qi);
2956
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002957 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002960 if (eap->addr_count != 0)
2961 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002962 if (cmdmod.split & WSP_VERT)
2963 {
2964 if (height != W_WIDTH(win))
2965 win_setwidth(height);
2966 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002967 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002968 win_setheight(height);
2969 }
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 else
2972 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002973 qf_buf = qf_find_buf(qi);
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 /* The current window becomes the previous window afterwards. */
2976 win = curwin;
2977
Bram Moolenaar77642c02012-11-20 17:55:10 +01002978 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2979 && cmdmod.split == 0)
2980 /* Create the new window at the very bottom, except when
2981 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002982 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002983 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002985 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002987 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002989 /*
2990 * For the location list window, create a reference to the
2991 * location list from the window 'win'.
2992 */
2993 curwin->w_llist_ref = win->w_llist;
2994 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002996
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002997 if (oldwin != curwin)
2998 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002999 if (qf_buf != NULL)
3000 /* Use the existing quickfix buffer */
3001 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003002 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003003 else
3004 {
3005 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003006 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003007 /* switch off 'swapfile' */
3008 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3009 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003010 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003011 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003012 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003013#ifdef FEAT_DIFF
3014 curwin->w_p_diff = FALSE;
3015#endif
3016#ifdef FEAT_FOLDING
3017 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3018 OPT_LOCAL);
3019#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003022 /* Only set the height when still in the same tab page and there is no
3023 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003024 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 win_setheight(height);
3026 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3027 if (win_valid(win))
3028 prevwin = win;
3029 }
3030
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003031 qf_set_title_var(qi);
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 /*
3034 * Fill the buffer with the quickfix list.
3035 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003036 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003038 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 curwin->w_cursor.col = 0;
3040 check_cursor();
3041 update_topline(); /* scroll to show the line */
3042}
3043
3044/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003045 * Move the cursor in the quickfix window to "lnum".
3046 */
3047 static void
3048qf_win_goto(win_T *win, linenr_T lnum)
3049{
3050 win_T *old_curwin = curwin;
3051
3052 curwin = win;
3053 curbuf = win->w_buffer;
3054 curwin->w_cursor.lnum = lnum;
3055 curwin->w_cursor.col = 0;
3056#ifdef FEAT_VIRTUALEDIT
3057 curwin->w_cursor.coladd = 0;
3058#endif
3059 curwin->w_curswant = 0;
3060 update_topline(); /* scroll to show the line */
3061 redraw_later(VALID);
3062 curwin->w_redr_status = TRUE; /* update ruler */
3063 curwin = old_curwin;
3064 curbuf = curwin->w_buffer;
3065}
3066
3067/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003068 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003069 */
3070 void
3071ex_cbottom(exarg_T *eap UNUSED)
3072{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003073 qf_info_T *qi = &ql_info;
3074 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003075
Bram Moolenaar537ef082016-07-09 17:56:19 +02003076 if (eap->cmdidx == CMD_lbottom)
3077 {
3078 qi = GET_LOC_LIST(curwin);
3079 if (qi == NULL)
3080 {
3081 EMSG(_(e_loclist));
3082 return;
3083 }
3084 }
3085
3086 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003087 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3088 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3089}
3090
3091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 * Return the number of the current entry (line number in the quickfix
3093 * window).
3094 */
3095 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003096qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003098 qf_info_T *qi = &ql_info;
3099
3100 if (IS_LL_WINDOW(wp))
3101 /* In the location list window, use the referenced location list */
3102 qi = wp->w_llist_ref;
3103
3104 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105}
3106
3107/*
3108 * Update the cursor position in the quickfix window to the current error.
3109 * Return TRUE if there is a quickfix window.
3110 */
3111 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003112qf_win_pos_update(
3113 qf_info_T *qi,
3114 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003117 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 /*
3120 * Put the cursor on the current error in the quickfix window, so that
3121 * it's viewable.
3122 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003123 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 if (win != NULL
3125 && qf_index <= win->w_buffer->b_ml.ml_line_count
3126 && old_qf_index != qf_index)
3127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (qf_index > old_qf_index)
3129 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003130 win->w_redraw_top = old_qf_index;
3131 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 }
3133 else
3134 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003135 win->w_redraw_top = qf_index;
3136 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003138 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 }
3140 return win != NULL;
3141}
3142
3143/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003144 * Check whether the given window is displaying the specified quickfix/location
3145 * list buffer
3146 */
3147 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003148is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003149{
3150 /*
3151 * A window displaying the quickfix buffer will have the w_llist_ref field
3152 * set to NULL.
3153 * A window displaying a location list buffer will have the w_llist_ref
3154 * pointing to the location list.
3155 */
3156 if (bt_quickfix(win->w_buffer))
3157 if ((qi == &ql_info && win->w_llist_ref == NULL)
3158 || (qi != &ql_info && win->w_llist_ref == qi))
3159 return TRUE;
3160
3161 return FALSE;
3162}
3163
3164/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003165 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003166 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003167 */
3168 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003169qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003170{
3171 win_T *win;
3172
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003173 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003174 if (is_qf_win(win, qi))
3175 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003176
3177 return win;
3178}
3179
3180/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003181 * Find a quickfix buffer.
3182 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 */
3184 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003185qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003187 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003188 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
Bram Moolenaar9c102382006-05-03 21:26:49 +00003190 FOR_ALL_TAB_WINDOWS(tp, win)
3191 if (is_qf_win(win, qi))
3192 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003193
Bram Moolenaar9c102382006-05-03 21:26:49 +00003194 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195}
3196
3197/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003198 * Update the w:quickfix_title variable in the quickfix/location list window
3199 */
3200 static void
3201qf_update_win_titlevar(qf_info_T *qi)
3202{
3203 win_T *win;
3204 win_T *curwin_save;
3205
3206 if ((win = qf_find_win(qi)) != NULL)
3207 {
3208 curwin_save = curwin;
3209 curwin = win;
3210 qf_set_title_var(qi);
3211 curwin = curwin_save;
3212 }
3213}
3214
3215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 * Find the quickfix buffer. If it exists, update the contents.
3217 */
3218 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003219qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
3221 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003222 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
3225 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003226 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 if (buf != NULL)
3228 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003229 linenr_T old_line_count = buf->b_ml.ml_line_count;
3230
3231 if (old_last == NULL)
3232 /* set curwin/curbuf to buf and save a few things */
3233 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234
Bram Moolenaard823fa92016-08-12 16:29:27 +02003235 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003236
Bram Moolenaar864293a2016-06-02 13:40:04 +02003237 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003238
Bram Moolenaar864293a2016-06-02 13:40:04 +02003239 if (old_last == NULL)
3240 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003241 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003242
3243 /* restore curwin/curbuf and a few other things */
3244 aucmd_restbuf(&aco);
3245 }
3246
3247 /* Only redraw when added lines are visible. This avoids flickering
3248 * when the added lines are not visible. */
3249 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3250 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 }
3252}
3253
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003254/*
3255 * Set "w:quickfix_title" if "qi" has a title.
3256 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003257 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003258qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003259{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003260 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3261 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003262 qi->qf_lists[qi->qf_curlist].qf_title);
3263}
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265/*
3266 * Fill current buffer with quickfix errors, replacing any previous contents.
3267 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003268 * If "old_last" is not NULL append the items after this one.
3269 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3270 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 */
3272 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003273qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003275 linenr_T lnum;
3276 qfline_T *qfp;
3277 buf_T *errbuf;
3278 int len;
3279 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar864293a2016-06-02 13:40:04 +02003281 if (old_last == NULL)
3282 {
3283 if (buf != curbuf)
3284 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003285 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003286 return;
3287 }
3288
3289 /* delete all existing lines */
3290 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3291 (void)ml_delete((linenr_T)1, FALSE);
3292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
3294 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003295 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003298 if (old_last == NULL)
3299 {
3300 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3301 lnum = 0;
3302 }
3303 else
3304 {
3305 qfp = old_last->qf_next;
3306 lnum = buf->b_ml.ml_line_count;
3307 }
3308 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 if (qfp->qf_fnum != 0
3311 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3312 && errbuf->b_fname != NULL)
3313 {
3314 if (qfp->qf_type == 1) /* :helpgrep */
3315 STRCPY(IObuff, gettail(errbuf->b_fname));
3316 else
3317 STRCPY(IObuff, errbuf->b_fname);
3318 len = (int)STRLEN(IObuff);
3319 }
3320 else
3321 len = 0;
3322 IObuff[len++] = '|';
3323
3324 if (qfp->qf_lnum > 0)
3325 {
3326 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3327 len += (int)STRLEN(IObuff + len);
3328
3329 if (qfp->qf_col > 0)
3330 {
3331 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3332 len += (int)STRLEN(IObuff + len);
3333 }
3334
3335 sprintf((char *)IObuff + len, "%s",
3336 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3337 len += (int)STRLEN(IObuff + len);
3338 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003339 else if (qfp->qf_pattern != NULL)
3340 {
3341 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3342 len += (int)STRLEN(IObuff + len);
3343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 IObuff[len++] = '|';
3345 IObuff[len++] = ' ';
3346
3347 /* Remove newlines and leading whitespace from the text.
3348 * For an unrecognized line keep the indent, the compiler may
3349 * mark a word with ^^^^. */
3350 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3351 IObuff + len, IOSIZE - len);
3352
Bram Moolenaar864293a2016-06-02 13:40:04 +02003353 if (ml_append_buf(buf, lnum, IObuff,
3354 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003356 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003358 if (qfp == NULL)
3359 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003361
3362 if (old_last == NULL)
3363 /* Delete the empty line which is now at the end */
3364 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366
3367 /* correct cursor position */
3368 check_lnums(TRUE);
3369
Bram Moolenaar864293a2016-06-02 13:40:04 +02003370 if (old_last == NULL)
3371 {
3372 /* Set the 'filetype' to "qf" each time after filling the buffer.
3373 * This resembles reading a file into a buffer, it's more logical when
3374 * using autocommands. */
3375 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3376 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
3378#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003379 keep_filetype = TRUE; /* don't detect 'filetype' */
3380 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003382 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003384 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003386 /* make sure it will be redrawn */
3387 redraw_curbuf_later(NOT_VALID);
3388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
3390 /* Restore KeyTyped, setting 'filetype' may reset it. */
3391 KeyTyped = old_KeyTyped;
3392}
3393
3394#endif /* FEAT_WINDOWS */
3395
3396/*
3397 * Return TRUE if "buf" is the quickfix buffer.
3398 */
3399 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003400bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003402 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403}
3404
3405/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003406 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3407 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 */
3409 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003410bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003412 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3413 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414}
3415
3416/*
3417 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3418 */
3419 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003420bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003422 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423}
3424
3425 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003426bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
3428 if (bt_dontwrite(buf))
3429 {
3430 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3431 return TRUE;
3432 }
3433 return FALSE;
3434}
3435
3436/*
3437 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3438 * and 'bufhidden'.
3439 */
3440 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003441buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3444 switch (buf->b_p_bh[0])
3445 {
3446 case 'u': /* "unload" */
3447 case 'w': /* "wipe" */
3448 case 'd': return FALSE; /* "delete" */
3449 case 'h': return TRUE; /* "hide" */
3450 }
3451 return (p_hid || cmdmod.hide);
3452}
3453
3454/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003455 * Return TRUE when using ":vimgrep" for ":grep".
3456 */
3457 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003458grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003459{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003460 return ((cmdidx == CMD_grep
3461 || cmdidx == CMD_lgrep
3462 || cmdidx == CMD_grepadd
3463 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003464 && STRCMP("internal",
3465 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3466}
3467
3468/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003469 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 */
3471 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003472ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003474 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003476 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003478 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003479 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003480 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003481#ifdef FEAT_AUTOCMD
3482 char_u *au_name = NULL;
3483
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003484 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3485 if (grep_internal(eap->cmdidx))
3486 {
3487 ex_vimgrep(eap);
3488 return;
3489 }
3490
Bram Moolenaar7c626922005-02-07 22:01:03 +00003491 switch (eap->cmdidx)
3492 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003493 case CMD_make: au_name = (char_u *)"make"; break;
3494 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3495 case CMD_grep: au_name = (char_u *)"grep"; break;
3496 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3497 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3498 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003499 default: break;
3500 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003501 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3502 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003503 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003504# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003505 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003506 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003507# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003508 }
3509#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003510#ifdef FEAT_MBYTE
3511 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
Bram Moolenaara6557602006-02-04 22:43:20 +00003514 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3515 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003516 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003519 fname = get_mef_name();
3520 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003522 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
3524 /*
3525 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3526 */
3527 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3528 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003529 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 cmd = alloc(len);
3531 if (cmd == NULL)
3532 return;
3533 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3534 (char *)p_shq);
3535 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003536 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 /*
3538 * Output a newline if there's something else than the :make command that
3539 * was typed (in which case the cursor is in column 0).
3540 */
3541 if (msg_col == 0)
3542 msg_didout = FALSE;
3543 msg_start();
3544 MSG_PUTS(":!");
3545 msg_outtrans(cmd); /* show what we are doing */
3546
3547 /* let the shell know if we are redirecting output or not */
3548 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3549
3550#ifdef AMIGA
3551 out_flush();
3552 /* read window status report and redraw before message */
3553 (void)char_avail();
3554#endif
3555
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003556 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003557 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3558 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003559 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003560 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003561 if (wp != NULL)
3562 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003563#ifdef FEAT_AUTOCMD
3564 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003565 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003566 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3567 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003568 if (qi->qf_curlist < qi->qf_listcount)
3569 res = qi->qf_lists[qi->qf_curlist].qf_count;
3570 else
3571 res = 0;
3572 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003573#endif
3574 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003575 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
Bram Moolenaar7c626922005-02-07 22:01:03 +00003577 mch_remove(fname);
3578 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 vim_free(cmd);
3580}
3581
3582/*
3583 * Return the name for the errorfile, in allocated memory.
3584 * Find a new unique name when 'makeef' contains "##".
3585 * Returns NULL for error.
3586 */
3587 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003588get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
3590 char_u *p;
3591 char_u *name;
3592 static int start = -1;
3593 static int off = 0;
3594#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003595 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596#endif
3597
3598 if (*p_mef == NUL)
3599 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003600 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (name == NULL)
3602 EMSG(_(e_notmp));
3603 return name;
3604 }
3605
3606 for (p = p_mef; *p; ++p)
3607 if (p[0] == '#' && p[1] == '#')
3608 break;
3609
3610 if (*p == NUL)
3611 return vim_strsave(p_mef);
3612
3613 /* Keep trying until the name doesn't exist yet. */
3614 for (;;)
3615 {
3616 if (start == -1)
3617 start = mch_get_pid();
3618 else
3619 off += 19;
3620
3621 name = alloc((unsigned)STRLEN(p_mef) + 30);
3622 if (name == NULL)
3623 break;
3624 STRCPY(name, p_mef);
3625 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3626 STRCAT(name, p + 2);
3627 if (mch_getperm(name) < 0
3628#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003629 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 && mch_lstat((char *)name, &sb) < 0
3631#endif
3632 )
3633 break;
3634 vim_free(name);
3635 }
3636 return name;
3637}
3638
3639/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003640 * Returns the number of valid entries in the current quickfix/location list.
3641 */
3642 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003643qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003644{
3645 qf_info_T *qi = &ql_info;
3646 qfline_T *qfp;
3647 int i, sz = 0;
3648 int prev_fnum = 0;
3649
3650 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3651 {
3652 /* Location list */
3653 qi = GET_LOC_LIST(curwin);
3654 if (qi == NULL)
3655 return 0;
3656 }
3657
3658 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003659 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003660 ++i, qfp = qfp->qf_next)
3661 {
3662 if (qfp->qf_valid)
3663 {
3664 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3665 sz++; /* Count all valid entries */
3666 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3667 {
3668 /* Count the number of files */
3669 sz++;
3670 prev_fnum = qfp->qf_fnum;
3671 }
3672 }
3673 }
3674
3675 return sz;
3676}
3677
3678/*
3679 * Returns the current index of the quickfix/location list.
3680 * Returns 0 if there is an error.
3681 */
3682 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003683qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003684{
3685 qf_info_T *qi = &ql_info;
3686
3687 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3688 {
3689 /* Location list */
3690 qi = GET_LOC_LIST(curwin);
3691 if (qi == NULL)
3692 return 0;
3693 }
3694
3695 return qi->qf_lists[qi->qf_curlist].qf_index;
3696}
3697
3698/*
3699 * Returns the current index in the quickfix/location list (counting only valid
3700 * entries). If no valid entries are in the list, then returns 1.
3701 */
3702 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003703qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003704{
3705 qf_info_T *qi = &ql_info;
3706 qf_list_T *qfl;
3707 qfline_T *qfp;
3708 int i, eidx = 0;
3709 int prev_fnum = 0;
3710
3711 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3712 {
3713 /* Location list */
3714 qi = GET_LOC_LIST(curwin);
3715 if (qi == NULL)
3716 return 1;
3717 }
3718
3719 qfl = &qi->qf_lists[qi->qf_curlist];
3720 qfp = qfl->qf_start;
3721
3722 /* check if the list has valid errors */
3723 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3724 return 1;
3725
3726 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3727 {
3728 if (qfp->qf_valid)
3729 {
3730 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3731 {
3732 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3733 {
3734 /* Count the number of files */
3735 eidx++;
3736 prev_fnum = qfp->qf_fnum;
3737 }
3738 }
3739 else
3740 eidx++;
3741 }
3742 }
3743
3744 return eidx ? eidx : 1;
3745}
3746
3747/*
3748 * Get the 'n'th valid error entry in the quickfix or location list.
3749 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3750 * For :cdo and :ldo returns the 'n'th valid error entry.
3751 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3752 */
3753 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003754qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003755{
3756 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3757 qfline_T *qfp = qfl->qf_start;
3758 int i, eidx;
3759 int prev_fnum = 0;
3760
3761 /* check if the list has valid errors */
3762 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3763 return 1;
3764
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003765 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003766 i++, qfp = qfp->qf_next)
3767 {
3768 if (qfp->qf_valid)
3769 {
3770 if (fdo)
3771 {
3772 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3773 {
3774 /* Count the number of files */
3775 eidx++;
3776 prev_fnum = qfp->qf_fnum;
3777 }
3778 }
3779 else
3780 eidx++;
3781 }
3782
3783 if (eidx == n)
3784 break;
3785 }
3786
3787 if (i <= qfl->qf_count)
3788 return i;
3789 else
3790 return 1;
3791}
3792
3793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003795 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003796 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 */
3798 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003799ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003801 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003802 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003803
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003804 if (eap->cmdidx == CMD_ll
3805 || eap->cmdidx == CMD_lrewind
3806 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003807 || eap->cmdidx == CMD_llast
3808 || eap->cmdidx == CMD_ldo
3809 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003810 {
3811 qi = GET_LOC_LIST(curwin);
3812 if (qi == NULL)
3813 {
3814 EMSG(_(e_loclist));
3815 return;
3816 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003817 }
3818
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003819 if (eap->addr_count > 0)
3820 errornr = (int)eap->line2;
3821 else
3822 {
3823 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3824 errornr = 0;
3825 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3826 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3827 errornr = 1;
3828 else
3829 errornr = 32767;
3830 }
3831
3832 /* For cdo and ldo commands, jump to the nth valid error.
3833 * For cfdo and lfdo commands, jump to the nth valid file entry.
3834 */
3835 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3836 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3837 errornr = qf_get_nth_valid_entry(qi,
3838 eap->addr_count > 0 ? (int)eap->line1 : 1,
3839 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3840
3841 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842}
3843
3844/*
3845 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003846 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003847 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 */
3849 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003850ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003852 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003853 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003854
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003855 if (eap->cmdidx == CMD_lnext
3856 || eap->cmdidx == CMD_lNext
3857 || eap->cmdidx == CMD_lprevious
3858 || eap->cmdidx == CMD_lnfile
3859 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003860 || eap->cmdidx == CMD_lpfile
3861 || eap->cmdidx == CMD_ldo
3862 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003863 {
3864 qi = GET_LOC_LIST(curwin);
3865 if (qi == NULL)
3866 {
3867 EMSG(_(e_loclist));
3868 return;
3869 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003870 }
3871
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003872 if (eap->addr_count > 0 &&
3873 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3874 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3875 errornr = (int)eap->line2;
3876 else
3877 errornr = 1;
3878
3879 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3880 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003882 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3883 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003885 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3886 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 ? BACKWARD_FILE
3888 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003889 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890}
3891
3892/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003893 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003894 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 */
3896 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003897ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003899 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003900 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003901 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003902#ifdef FEAT_AUTOCMD
3903 char_u *au_name = NULL;
3904#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003905
3906 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003907 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003908 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003909
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003910#ifdef FEAT_AUTOCMD
3911 switch (eap->cmdidx)
3912 {
3913 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3914 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3915 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3916 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3917 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3918 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3919 default: break;
3920 }
3921 if (au_name != NULL)
3922 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3923#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003924#ifdef FEAT_MBYTE
3925 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3926#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003927#ifdef FEAT_BROWSE
3928 if (cmdmod.browse)
3929 {
3930 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3931 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3932 if (browse_file == NULL)
3933 return;
3934 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3935 vim_free(browse_file);
3936 }
3937 else
3938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003940 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003941
3942 /*
3943 * This function is used by the :cfile, :cgetfile and :caddfile
3944 * commands.
3945 * :cfile always creates a new quickfix list and jumps to the
3946 * first error.
3947 * :cgetfile creates a new quickfix list but doesn't jump to the
3948 * first error.
3949 * :caddfile adds to an existing quickfix list. If there is no
3950 * quickfix list then a new list is created.
3951 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003952 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003953 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003954 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003955 && (eap->cmdidx == CMD_cfile
3956 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003957 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003958#ifdef FEAT_AUTOCMD
3959 if (au_name != NULL)
3960 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3961#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003962 if (wp != NULL)
3963 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003964 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003965 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003966
3967 else
3968 {
3969#ifdef FEAT_AUTOCMD
3970 if (au_name != NULL)
3971 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3972#endif
3973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974}
3975
3976/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003977 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003978 * ":vimgrepadd {pattern} file(s)"
3979 * ":lvimgrep {pattern} file(s)"
3980 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003981 */
3982 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003983ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003984{
Bram Moolenaar81695252004-12-29 20:58:21 +00003985 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003986 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003987 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003988 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003989 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003990 char_u *s;
3991 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003992 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003993 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003994#ifdef FEAT_AUTOCMD
3995 qfline_T *cur_qf_start;
3996#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003997 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003998 buf_T *buf;
3999 int duplicate_name = FALSE;
4000 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004001 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004002 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004003 buf_T *first_match_buf = NULL;
4004 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004005 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004006#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4007 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004008#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004009 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004010 int flags = 0;
4011 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004012 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004013 char_u *dirname_start = NULL;
4014 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004015 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004016#ifdef FEAT_AUTOCMD
4017 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004018
4019 switch (eap->cmdidx)
4020 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004021 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4022 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4023 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004024 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004025 case CMD_grep: au_name = (char_u *)"grep"; break;
4026 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4027 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4028 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004029 default: break;
4030 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004031 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4032 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004033 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004034# ifdef FEAT_EVAL
4035 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004036 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004037# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004038 }
4039#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004040
Bram Moolenaar754b5602006-02-09 23:53:20 +00004041 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004042 || eap->cmdidx == CMD_lvimgrep
4043 || eap->cmdidx == CMD_lgrepadd
4044 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004045 {
4046 qi = ll_get_or_alloc_list(curwin);
4047 if (qi == NULL)
4048 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004049 }
4050
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004051 if (eap->addr_count > 0)
4052 tomatch = eap->line2;
4053 else
4054 tomatch = MAXLNUM;
4055
Bram Moolenaar81695252004-12-29 20:58:21 +00004056 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004057 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004058 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004059 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004060 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004061 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004062 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004063 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004064 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004065
4066 if (s != NULL && *s == NUL)
4067 {
4068 /* Pattern is empty, use last search pattern. */
4069 if (last_search_pat() == NULL)
4070 {
4071 EMSG(_(e_noprevre));
4072 goto theend;
4073 }
4074 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4075 }
4076 else
4077 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4078
Bram Moolenaar86b68352004-12-27 21:59:20 +00004079 if (regmatch.regprog == NULL)
4080 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004081 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004082 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004083
4084 p = skipwhite(p);
4085 if (*p == NUL)
4086 {
4087 EMSG(_("E683: File name missing or invalid pattern"));
4088 goto theend;
4089 }
4090
Bram Moolenaar754b5602006-02-09 23:53:20 +00004091 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004092 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004093 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004094 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004095 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004096
4097 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004098 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004099 goto theend;
4100 if (fcount == 0)
4101 {
4102 EMSG(_(e_nomatch));
4103 goto theend;
4104 }
4105
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004106 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4107 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004108 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004109 {
4110 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004111 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004112 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004113
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004114 /* Remember the current directory, because a BufRead autocommand that does
4115 * ":lcd %:p:h" changes the meaning of short path names. */
4116 mch_dirname(dirname_start, MAXPATHL);
4117
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004118#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004119 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004120 * changing the current quickfix list. */
4121 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4122#endif
4123
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004124 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004125 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004126 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004127 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004128 if (time(NULL) > seconds)
4129 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004130 /* Display the file name every second or so, show the user we are
4131 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004132 seconds = time(NULL);
4133 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004134 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004135 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004136 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004137 else
4138 {
4139 msg_outtrans(p);
4140 vim_free(p);
4141 }
4142 msg_clr_eos();
4143 msg_didout = FALSE; /* overwrite this message */
4144 msg_nowait = TRUE; /* don't wait for this message */
4145 msg_col = 0;
4146 out_flush();
4147 }
4148
Bram Moolenaar81695252004-12-29 20:58:21 +00004149 buf = buflist_findname_exp(fnames[fi]);
4150 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4151 {
4152 /* Remember that a buffer with this name already exists. */
4153 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004154 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004155 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004156
4157#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4158 /* Don't do Filetype autocommands to avoid loading syntax and
4159 * indent scripts, a great speed improvement. */
4160 save_ei = au_event_disable(",Filetype");
4161#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004162 /* Don't use modelines here, it's useless. */
4163 save_mls = p_mls;
4164 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004165
4166 /* Load file into a buffer, so that 'fileencoding' is detected,
4167 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004168 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004169
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004170 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004171#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4172 au_event_restore(save_ei);
4173#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004174 }
4175 else
4176 /* Use existing, loaded buffer. */
4177 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004178
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004179#ifdef FEAT_AUTOCMD
4180 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4181 {
4182 int idx;
4183
4184 /* Autocommands changed the quickfix list. Find the one we were
4185 * using and restore it. */
4186 for (idx = 0; idx < LISTCOUNT; ++idx)
4187 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4188 {
4189 qi->qf_curlist = idx;
4190 break;
4191 }
4192 if (idx == LISTCOUNT)
4193 {
4194 /* List cannot be found, create a new one. */
4195 qf_new_list(qi, *eap->cmdlinep);
4196 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4197 }
4198 }
4199#endif
4200
Bram Moolenaar81695252004-12-29 20:58:21 +00004201 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004202 {
4203 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004204 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004205 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004206 else
4207 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004208 /* Try for a match in all lines of the buffer.
4209 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004210 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004211 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4212 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004213 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004214 col = 0;
4215 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004216 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004217 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004218 /* Pass the buffer number so that it gets used even for a
4219 * dummy buffer, unless duplicate_name is set, then the
4220 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004221 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004222 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004223 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004224 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004225 ml_get_buf(buf,
4226 regmatch.startpos[0].lnum + lnum, FALSE),
4227 regmatch.startpos[0].lnum + lnum,
4228 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004229 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004230 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004231 0, /* nr */
4232 0, /* type */
4233 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004234 ) == FAIL)
4235 {
4236 got_int = TRUE;
4237 break;
4238 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004239 found_match = TRUE;
4240 if (--tomatch == 0)
4241 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004242 if ((flags & VGR_GLOBAL) == 0
4243 || regmatch.endpos[0].lnum > 0)
4244 break;
4245 col = regmatch.endpos[0].col
4246 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004247 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004248 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004249 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004250 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004251 if (got_int)
4252 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004253 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004254#ifdef FEAT_AUTOCMD
4255 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4256#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004257
4258 if (using_dummy)
4259 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004260 if (found_match && first_match_buf == NULL)
4261 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004262 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004263 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004264 /* Never keep a dummy buffer if there is another buffer
4265 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004266 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004267 buf = NULL;
4268 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004269 else if (!cmdmod.hide
4270 || buf->b_p_bh[0] == 'u' /* "unload" */
4271 || buf->b_p_bh[0] == 'w' /* "wipe" */
4272 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004273 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004274 /* When no match was found we don't need to remember the
4275 * buffer, wipe it out. If there was a match and it
4276 * wasn't the first one or we won't jump there: only
4277 * unload the buffer.
4278 * Ignore 'hidden' here, because it may lead to having too
4279 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004280 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004281 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004282 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004283 buf = NULL;
4284 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004285 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004286 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004287 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004288 /* Keeping the buffer, remove the dummy flag. */
4289 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004290 buf = NULL;
4291 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004292 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004293
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004294 if (buf != NULL)
4295 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004296 /* Keeping the buffer, remove the dummy flag. */
4297 buf->b_flags &= ~BF_DUMMY;
4298
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004299 /* If the buffer is still loaded we need to use the
4300 * directory we jumped to below. */
4301 if (buf == first_match_buf
4302 && target_dir == NULL
4303 && STRCMP(dirname_start, dirname_now) != 0)
4304 target_dir = vim_strsave(dirname_now);
4305
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004306 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004307 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004308 * need to be done (again). But not the window-local
4309 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004310 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004311#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004312 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4313 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004314#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004315 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004316 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004317 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004318 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004319 }
4320 }
4321
4322 FreeWild(fcount, fnames);
4323
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004324 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4325 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4326 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004327
4328#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004329 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004330#endif
4331
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004332#ifdef FEAT_AUTOCMD
4333 if (au_name != NULL)
4334 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4335 curbuf->b_fname, TRUE, curbuf);
4336#endif
4337
Bram Moolenaar86b68352004-12-27 21:59:20 +00004338 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004339 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004340 {
4341 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004342 {
4343 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004344 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004345 if (buf != curbuf)
4346 /* If we jumped to another buffer redrawing will already be
4347 * taken care of. */
4348 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004349
4350 /* Jump to the directory used after loading the buffer. */
4351 if (curbuf == first_match_buf && target_dir != NULL)
4352 {
4353 exarg_T ea;
4354
4355 ea.arg = target_dir;
4356 ea.cmdidx = CMD_lcd;
4357 ex_cd(&ea);
4358 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004359 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004360 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004361 else
4362 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004363
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004364 /* If we loaded a dummy buffer into the current window, the autocommands
4365 * may have messed up things, need to redraw and recompute folds. */
4366 if (redraw_for_dummy)
4367 {
4368#ifdef FEAT_FOLDING
4369 foldUpdateAll(curwin);
4370#else
4371 redraw_later(NOT_VALID);
4372#endif
4373 }
4374
Bram Moolenaar86b68352004-12-27 21:59:20 +00004375theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004376 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004377 vim_free(dirname_now);
4378 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004379 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004380 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004381}
4382
4383/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004384 * Restore current working directory to "dirname_start" if they differ, taking
4385 * into account whether it is set locally or globally.
4386 */
4387 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004388restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004389{
4390 char_u *dirname_now = alloc(MAXPATHL);
4391
4392 if (NULL != dirname_now)
4393 {
4394 mch_dirname(dirname_now, MAXPATHL);
4395 if (STRCMP(dirname_start, dirname_now) != 0)
4396 {
4397 /* If the directory has changed, change it back by building up an
4398 * appropriate ex command and executing it. */
4399 exarg_T ea;
4400
4401 ea.arg = dirname_start;
4402 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4403 ex_cd(&ea);
4404 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004405 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004406 }
4407}
4408
4409/*
4410 * Load file "fname" into a dummy buffer and return the buffer pointer,
4411 * placing the directory resulting from the buffer load into the
4412 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4413 * prior to calling this function. Restores directory to "dirname_start" prior
4414 * to returning, if autocmds or the 'autochdir' option have changed it.
4415 *
4416 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4417 * or wipe_dummy_buffer() later!
4418 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004419 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004420 */
4421 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004422load_dummy_buffer(
4423 char_u *fname,
4424 char_u *dirname_start, /* in: old directory */
4425 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004426{
4427 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004428 bufref_T newbufref;
4429 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004430 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004431 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004432
4433 /* Allocate a buffer without putting it in the buffer list. */
4434 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4435 if (newbuf == NULL)
4436 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004437 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004438
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004439 /* Init the options. */
4440 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4441
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004442 /* need to open the memfile before putting the buffer in a window */
4443 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004444 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004445 /* set curwin/curbuf to buf and save a few things */
4446 aucmd_prepbuf(&aco, newbuf);
4447
4448 /* Need to set the filename for autocommands. */
4449 (void)setfname(curbuf, fname, NULL, FALSE);
4450
Bram Moolenaar81695252004-12-29 20:58:21 +00004451 /* Create swap file now to avoid the ATTENTION message. */
4452 check_need_swap(TRUE);
4453
4454 /* Remove the "dummy" flag, otherwise autocommands may not
4455 * work. */
4456 curbuf->b_flags &= ~BF_DUMMY;
4457
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004458 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004459 if (readfile(fname, NULL,
4460 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4461 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004462 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004463 && !(curbuf->b_flags & BF_NEW))
4464 {
4465 failed = FALSE;
4466 if (curbuf != newbuf)
4467 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004468 /* Bloody autocommands changed the buffer! Can happen when
4469 * using netrw and editing a remote file. Use the current
4470 * buffer instead, delete the dummy one after restoring the
4471 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004472 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004473 newbuf = curbuf;
4474 }
4475 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004476
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004477 /* restore curwin/curbuf and a few other things */
4478 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004479 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4480 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004481
4482 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4483 * skip it. */
4484 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004485 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004486
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004487 /*
4488 * When autocommands/'autochdir' option changed directory: go back.
4489 * Let the caller know what the resulting dir was first, in case it is
4490 * important.
4491 */
4492 mch_dirname(resulting_dir, MAXPATHL);
4493 restore_start_dir(dirname_start);
4494
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004495 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004496 return NULL;
4497 if (failed)
4498 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004499 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004500 return NULL;
4501 }
4502 return newbuf;
4503}
4504
4505/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004506 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4507 * directory to "dirname_start" prior to returning, if autocmds or the
4508 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004509 */
4510 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004511wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004512{
4513 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004514 {
4515#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4516 cleanup_T cs;
4517
4518 /* Reset the error/interrupt/exception state here so that aborting()
4519 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4520 * work when got_int is set. */
4521 enter_cleanup(&cs);
4522#endif
4523
Bram Moolenaar81695252004-12-29 20:58:21 +00004524 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004525
4526#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4527 /* Restore the error/interrupt/exception state if not discarded by a
4528 * new aborting error, interrupt, or uncaught exception. */
4529 leave_cleanup(&cs);
4530#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004531 /* When autocommands/'autochdir' option changed directory: go back. */
4532 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004533 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004534}
4535
4536/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004537 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4538 * directory to "dirname_start" prior to returning, if autocmds or the
4539 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004540 */
4541 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004542unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004543{
4544 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004545 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004546 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004547
4548 /* When autocommands/'autochdir' option changed directory: go back. */
4549 restore_start_dir(dirname_start);
4550 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004551}
4552
Bram Moolenaar05159a02005-02-26 23:04:13 +00004553#if defined(FEAT_EVAL) || defined(PROTO)
4554/*
4555 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004556 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004557 */
4558 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004559get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004560{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004561 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004562 dict_T *dict;
4563 char_u buf[2];
4564 qfline_T *qfp;
4565 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004566 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004567
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004568 if (wp != NULL)
4569 {
4570 qi = GET_LOC_LIST(wp);
4571 if (qi == NULL)
4572 return FAIL;
4573 }
4574
Bram Moolenaard823fa92016-08-12 16:29:27 +02004575 if (qf_idx == -1)
4576 qf_idx = qi->qf_curlist;
4577
4578 if (qf_idx >= qi->qf_listcount
4579 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004580 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004581
Bram Moolenaard823fa92016-08-12 16:29:27 +02004582 qfp = qi->qf_lists[qf_idx].qf_start;
4583 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004584 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004585 /* Handle entries with a non-existing buffer number. */
4586 bufnum = qfp->qf_fnum;
4587 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4588 bufnum = 0;
4589
Bram Moolenaar05159a02005-02-26 23:04:13 +00004590 if ((dict = dict_alloc()) == NULL)
4591 return FAIL;
4592 if (list_append_dict(list, dict) == FAIL)
4593 return FAIL;
4594
4595 buf[0] = qfp->qf_type;
4596 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004597 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004598 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4599 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4600 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4601 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004602 || dict_add_nr_str(dict, "pattern", 0L,
4603 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4604 || dict_add_nr_str(dict, "text", 0L,
4605 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004606 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4607 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4608 return FAIL;
4609
4610 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004611 if (qfp == NULL)
4612 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004613 }
4614 return OK;
4615}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004616
4617/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004618 * Flags used by getqflist()/getloclist() to determine which fields to return.
4619 */
4620enum {
4621 QF_GETLIST_NONE = 0x0,
4622 QF_GETLIST_TITLE = 0x1,
4623 QF_GETLIST_ITEMS = 0x2,
4624 QF_GETLIST_NR = 0x4,
4625 QF_GETLIST_WINID = 0x8,
4626 QF_GETLIST_ALL = 0xFF
4627};
4628
4629/*
4630 * Return quickfix/location list details (title) as a
4631 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4632 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004633 */
4634 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004635get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4636{
4637 qf_info_T *qi = &ql_info;
4638 int status = OK;
4639 int qf_idx;
4640 dictitem_T *di;
4641 int flags = QF_GETLIST_NONE;
4642
4643 if (wp != NULL)
4644 {
4645 qi = GET_LOC_LIST(wp);
4646 if (qi == NULL)
4647 return FAIL;
4648 }
4649
4650 qf_idx = qi->qf_curlist; /* default is the current list */
4651 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4652 {
4653 /* Use the specified quickfix/location list */
4654 if (di->di_tv.v_type == VAR_NUMBER)
4655 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004656 /* for zero use the current list */
4657 if (di->di_tv.vval.v_number != 0)
4658 {
4659 qf_idx = di->di_tv.vval.v_number - 1;
4660 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4661 return FAIL;
4662 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004663 flags |= QF_GETLIST_NR;
4664 }
4665 else
4666 return FAIL;
4667 }
4668
4669 if (dict_find(what, (char_u *)"all", -1) != NULL)
4670 flags |= QF_GETLIST_ALL;
4671
4672 if (dict_find(what, (char_u *)"title", -1) != NULL)
4673 flags |= QF_GETLIST_TITLE;
4674
4675 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4676 flags |= QF_GETLIST_WINID;
4677
4678 if (flags & QF_GETLIST_TITLE)
4679 {
4680 char_u *t;
4681 t = qi->qf_lists[qf_idx].qf_title;
4682 if (t == NULL)
4683 t = (char_u *)"";
4684 status = dict_add_nr_str(retdict, "title", 0L, t);
4685 }
4686 if ((status == OK) && (flags & QF_GETLIST_NR))
4687 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4688 if ((status == OK) && (flags & QF_GETLIST_WINID))
4689 {
4690 win_T *win;
4691 win = qf_find_win(qi);
4692 if (win != NULL)
4693 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4694 }
4695
4696 return status;
4697}
4698
4699/*
4700 * Add list of entries to quickfix/location list. Each list entry is
4701 * a dictionary with item information.
4702 */
4703 static int
4704qf_add_entries(
4705 qf_info_T *qi,
4706 list_T *list,
4707 char_u *title,
4708 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004709{
4710 listitem_T *li;
4711 dict_T *d;
4712 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004713 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004714 long lnum;
4715 int col, nr;
4716 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004717#ifdef FEAT_WINDOWS
4718 qfline_T *old_last = NULL;
4719#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004720 int valid, status;
4721 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004722 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004723
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004724 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004725 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004726 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004727#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004728 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4729 /* Adding to existing list, use last entry. */
4730 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004731#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004732 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004733 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004734 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004735 qf_store_title(qi, title);
4736 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004737
4738 for (li = list->lv_first; li != NULL; li = li->li_next)
4739 {
4740 if (li->li_tv.v_type != VAR_DICT)
4741 continue; /* Skip non-dict items */
4742
4743 d = li->li_tv.vval.v_dict;
4744 if (d == NULL)
4745 continue;
4746
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004747 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004748 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4749 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4750 col = (int)get_dict_number(d, (char_u *)"col");
4751 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4752 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004753 type = get_dict_string(d, (char_u *)"type", TRUE);
4754 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4755 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004756 if (text == NULL)
4757 text = vim_strsave((char_u *)"");
4758
4759 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004760 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004761 valid = FALSE;
4762
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004763 /* Mark entries with non-existing buffer number as not valid. Give the
4764 * error message only once. */
4765 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4766 {
4767 if (!did_bufnr_emsg)
4768 {
4769 did_bufnr_emsg = TRUE;
4770 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4771 }
4772 valid = FALSE;
4773 bufnum = 0;
4774 }
4775
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004776 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004777 NULL, /* dir */
4778 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004779 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004780 text,
4781 lnum,
4782 col,
4783 vcol, /* vis_col */
4784 pattern, /* search pattern */
4785 nr,
4786 type == NULL ? NUL : *type,
4787 valid);
4788
4789 vim_free(filename);
4790 vim_free(pattern);
4791 vim_free(text);
4792 vim_free(type);
4793
4794 if (status == FAIL)
4795 {
4796 retval = FAIL;
4797 break;
4798 }
4799 }
4800
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004801 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004802 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004803 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4804 else
4805 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004806 if (action != 'a')
4807 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004808 qi->qf_lists[qi->qf_curlist].qf_ptr =
4809 qi->qf_lists[qi->qf_curlist].qf_start;
4810 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4811 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4812 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004813
4814#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004815 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004816 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004817#endif
4818
4819 return retval;
4820}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004821
4822 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004823qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004824{
4825 dictitem_T *di;
4826 int retval = FAIL;
4827 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004828 int newlist = FALSE;
4829
4830 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4831 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004832
4833 qf_idx = qi->qf_curlist; /* default is the current list */
4834 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4835 {
4836 /* Use the specified quickfix/location list */
4837 if (di->di_tv.v_type == VAR_NUMBER)
4838 {
4839 qf_idx = di->di_tv.vval.v_number - 1;
4840 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4841 return FAIL;
4842 }
4843 else
4844 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004845 newlist = FALSE; /* use the specified list */
4846 }
4847
4848 if (newlist)
4849 {
4850 qf_new_list(qi, NULL);
4851 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004852 }
4853
4854 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4855 {
4856 if (di->di_tv.v_type == VAR_STRING)
4857 {
4858 vim_free(qi->qf_lists[qf_idx].qf_title);
4859 qi->qf_lists[qf_idx].qf_title =
4860 get_dict_string(what, (char_u *)"title", TRUE);
4861 if (qf_idx == qi->qf_curlist)
4862 qf_update_win_titlevar(qi);
4863 retval = OK;
4864 }
4865 }
4866
4867 return retval;
4868}
4869
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004870/*
4871 * Find the non-location list window with the specified location list.
4872 */
4873 static win_T *
4874find_win_with_ll(qf_info_T *qi)
4875{
4876 win_T *wp = NULL;
4877
4878 FOR_ALL_WINDOWS(wp)
4879 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
4880 return wp;
4881
4882 return NULL;
4883}
4884
4885/*
4886 * Free the entire quickfix/location list stack.
4887 * If the quickfix/location list window is open, then clear it.
4888 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004889 static void
4890qf_free_stack(win_T *wp, qf_info_T *qi)
4891{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004892 win_T *qfwin = qf_find_win(qi);
4893 win_T *llwin = NULL;
4894 win_T *orig_wp = wp;
4895
4896 if (qfwin != NULL)
4897 {
4898 /* If the quickfix/location list window is open, then clear it */
4899 if (qi->qf_curlist < qi->qf_listcount)
4900 qf_free(qi, qi->qf_curlist);
4901 qf_update_buffer(qi, NULL);
4902 }
4903
4904 if (wp != NULL && IS_LL_WINDOW(wp))
4905 {
4906 /* If in the location list window, then use the non-location list
4907 * window with this location list (if present)
4908 */
4909 llwin = find_win_with_ll(qi);
4910 if (llwin != NULL)
4911 wp = llwin;
4912 }
4913
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004914 qf_free_all(wp);
4915 if (wp == NULL)
4916 {
4917 /* quickfix list */
4918 qi->qf_curlist = 0;
4919 qi->qf_listcount = 0;
4920 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004921 else if (IS_LL_WINDOW(orig_wp))
4922 {
4923 /* If the location list window is open, then create a new empty
4924 * location list */
4925 qf_info_T *new_ll = ll_new_list();
4926 orig_wp->w_llist_ref = new_ll;
4927 if (llwin != NULL)
4928 {
4929 llwin->w_llist = new_ll;
4930 new_ll->qf_refcount++;
4931 }
4932 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004933}
4934
Bram Moolenaard823fa92016-08-12 16:29:27 +02004935/*
4936 * Populate the quickfix list with the items supplied in the list
4937 * of dictionaries. "title" will be copied to w:quickfix_title.
4938 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
4939 */
4940 int
4941set_errorlist(
4942 win_T *wp,
4943 list_T *list,
4944 int action,
4945 char_u *title,
4946 dict_T *what)
4947{
4948 qf_info_T *qi = &ql_info;
4949 int retval = OK;
4950
4951 if (wp != NULL)
4952 {
4953 qi = ll_get_or_alloc_list(wp);
4954 if (qi == NULL)
4955 return FAIL;
4956 }
4957
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004958 if (action == 'f')
4959 {
4960 /* Free the entire quickfix or location list stack */
4961 qf_free_stack(wp, qi);
4962 }
4963 else if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004964 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004965 else
4966 retval = qf_add_entries(qi, list, title, action);
4967
4968 return retval;
4969}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004970#endif
4971
Bram Moolenaar81695252004-12-29 20:58:21 +00004972/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004973 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004974 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004975 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004976 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004977 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004978 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004979 */
4980 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004981ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004982{
4983 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004984 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004985#ifdef FEAT_AUTOCMD
4986 char_u *au_name = NULL;
4987#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004988
Bram Moolenaardb552d602006-03-23 22:59:57 +00004989 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4990 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004991 {
4992 qi = ll_get_or_alloc_list(curwin);
4993 if (qi == NULL)
4994 return;
4995 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004996
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004997#ifdef FEAT_AUTOCMD
4998 switch (eap->cmdidx)
4999 {
5000 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5001 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5002 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5003 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5004 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5005 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5006 default: break;
5007 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005008 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5009 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005010 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005011# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005012 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005013 return;
5014# endif
5015 }
5016#endif
5017
Bram Moolenaar86b68352004-12-27 21:59:20 +00005018 if (*eap->arg == NUL)
5019 buf = curbuf;
5020 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5021 buf = buflist_findnr(atoi((char *)eap->arg));
5022 if (buf == NULL)
5023 EMSG(_(e_invarg));
5024 else if (buf->b_ml.ml_mfp == NULL)
5025 EMSG(_("E681: Buffer is not loaded"));
5026 else
5027 {
5028 if (eap->addr_count == 0)
5029 {
5030 eap->line1 = 1;
5031 eap->line2 = buf->b_ml.ml_line_count;
5032 }
5033 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5034 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5035 EMSG(_(e_invrange));
5036 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005037 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005038 char_u *qf_title = *eap->cmdlinep;
5039
5040 if (buf->b_sfname)
5041 {
5042 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5043 (char *)qf_title, (char *)buf->b_sfname);
5044 qf_title = IObuff;
5045 }
5046
Bram Moolenaardb552d602006-03-23 22:59:57 +00005047 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
5048 (eap->cmdidx != CMD_caddbuffer
5049 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005050 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005051 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005052 {
5053#ifdef FEAT_AUTOCMD
5054 if (au_name != NULL)
5055 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5056 curbuf->b_fname, TRUE, curbuf);
5057#endif
5058 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5059 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5060 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005061 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005062 }
5063}
5064
Bram Moolenaar1e015462005-09-25 22:16:38 +00005065#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005066/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005067 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5068 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005069 */
5070 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005071ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005072{
5073 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005074 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005075#ifdef FEAT_AUTOCMD
5076 char_u *au_name = NULL;
5077#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005078
Bram Moolenaardb552d602006-03-23 22:59:57 +00005079 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5080 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005081 {
5082 qi = ll_get_or_alloc_list(curwin);
5083 if (qi == NULL)
5084 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005085 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005086
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005087#ifdef FEAT_AUTOCMD
5088 switch (eap->cmdidx)
5089 {
5090 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5091 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5092 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5093 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5094 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5095 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5096 default: break;
5097 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005098 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5099 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005100 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005101# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005102 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005103 return;
5104# endif
5105 }
5106#endif
5107
Bram Moolenaar4770d092006-01-12 23:22:24 +00005108 /* Evaluate the expression. When the result is a string or a list we can
5109 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005110 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005111 if (tv != NULL)
5112 {
5113 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5114 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5115 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01005116 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005117 (eap->cmdidx != CMD_caddexpr
5118 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005119 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5120 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005121 {
5122#ifdef FEAT_AUTOCMD
5123 if (au_name != NULL)
5124 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5125 curbuf->b_fname, TRUE, curbuf);
5126#endif
5127 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5128 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5129 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005130 }
5131 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005132 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005133 free_tv(tv);
5134 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005135}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005136#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005137
5138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 * ":helpgrep {pattern}"
5140 */
5141 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005142ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143{
5144 regmatch_T regmatch;
5145 char_u *save_cpo;
5146 char_u *p;
5147 int fcount;
5148 char_u **fnames;
5149 FILE *fd;
5150 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005152#ifdef FEAT_MULTI_LANG
5153 char_u *lang;
5154#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005155 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005156 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005157 int new_qi = FALSE;
5158 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005159#ifdef FEAT_AUTOCMD
5160 char_u *au_name = NULL;
5161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005163#ifdef FEAT_MULTI_LANG
5164 /* Check for a specified language */
5165 lang = check_help_lang(eap->arg);
5166#endif
5167
Bram Moolenaar73633f82012-01-20 13:39:07 +01005168#ifdef FEAT_AUTOCMD
5169 switch (eap->cmdidx)
5170 {
5171 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5172 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5173 default: break;
5174 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005175 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5176 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005177 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005178# ifdef FEAT_EVAL
5179 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005180 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005181# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005182 }
5183#endif
5184
5185 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5186 save_cpo = p_cpo;
5187 p_cpo = empty_option;
5188
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005189 if (eap->cmdidx == CMD_lhelpgrep)
5190 {
5191 /* Find an existing help window */
5192 FOR_ALL_WINDOWS(wp)
5193 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5194 break;
5195
5196 if (wp == NULL) /* Help window not found */
5197 qi = NULL;
5198 else
5199 qi = wp->w_llist;
5200
5201 if (qi == NULL)
5202 {
5203 /* Allocate a new location list for help text matches */
5204 if ((qi = ll_new_list()) == NULL)
5205 return;
5206 new_qi = TRUE;
5207 }
5208 }
5209
Bram Moolenaaree85df32017-03-19 14:19:50 +01005210 /* Autocommands may change the list. Save it for later comparison */
5211 save_qi = qi;
5212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5214 regmatch.rm_ic = FALSE;
5215 if (regmatch.regprog != NULL)
5216 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005217#ifdef FEAT_MBYTE
5218 vimconv_T vc;
5219
5220 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5221 * differs. */
5222 vc.vc_type = CONV_NONE;
5223 if (!enc_utf8)
5224 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5225#endif
5226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005228 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
5230 /* Go through all directories in 'runtimepath' */
5231 p = p_rtp;
5232 while (*p != NUL && !got_int)
5233 {
5234 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5235
5236 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5237 add_pathsep(NameBuff);
5238 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5239 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5240 &fnames, EW_FILE|EW_SILENT) == OK
5241 && fcount > 0)
5242 {
5243 for (fi = 0; fi < fcount && !got_int; ++fi)
5244 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005245#ifdef FEAT_MULTI_LANG
5246 /* Skip files for a different language. */
5247 if (lang != NULL
5248 && STRNICMP(lang, fnames[fi]
5249 + STRLEN(fnames[fi]) - 3, 2) != 0
5250 && !(STRNICMP(lang, "en", 2) == 0
5251 && STRNICMP("txt", fnames[fi]
5252 + STRLEN(fnames[fi]) - 3, 3) == 0))
5253 continue;
5254#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005255 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 if (fd != NULL)
5257 {
5258 lnum = 1;
5259 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5260 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005261 char_u *line = IObuff;
5262#ifdef FEAT_MBYTE
5263 /* Convert a line if 'encoding' is not utf-8 and
5264 * the line contains a non-ASCII character. */
5265 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005266 && has_non_ascii(IObuff))
5267 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005268 line = string_convert(&vc, IObuff, NULL);
5269 if (line == NULL)
5270 line = IObuff;
5271 }
5272#endif
5273
5274 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005276 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005279 while (l > 0 && line[l - 1] <= ' ')
5280 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005282 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 NULL, /* dir */
5284 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005285 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005286 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005288 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005289 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005290 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005291 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 0, /* nr */
5293 1, /* type */
5294 TRUE /* valid */
5295 ) == FAIL)
5296 {
5297 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005298#ifdef FEAT_MBYTE
5299 if (line != IObuff)
5300 vim_free(line);
5301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 break;
5303 }
5304 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005305#ifdef FEAT_MBYTE
5306 if (line != IObuff)
5307 vim_free(line);
5308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 ++lnum;
5310 line_breakcheck();
5311 }
5312 fclose(fd);
5313 }
5314 }
5315 FreeWild(fcount, fnames);
5316 }
5317 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005318
Bram Moolenaar473de612013-06-08 18:19:48 +02005319 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005320#ifdef FEAT_MBYTE
5321 if (vc.vc_type != CONV_NONE)
5322 convert_setup(&vc, NULL, NULL);
5323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005325 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5326 qi->qf_lists[qi->qf_curlist].qf_ptr =
5327 qi->qf_lists[qi->qf_curlist].qf_start;
5328 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005331 if (p_cpo == empty_option)
5332 p_cpo = save_cpo;
5333 else
5334 /* Darn, some plugin changed the value. */
5335 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005338 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#endif
5340
Bram Moolenaar73633f82012-01-20 13:39:07 +01005341#ifdef FEAT_AUTOCMD
5342 if (au_name != NULL)
5343 {
5344 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5345 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005346 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005347 /* autocommands made "qi" invalid */
5348 return;
5349 }
5350#endif
5351
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005353 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005354 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005355 else
5356 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005357
5358 if (eap->cmdidx == CMD_lhelpgrep)
5359 {
5360 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005361 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005362 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5363 {
5364 if (new_qi)
5365 ll_free_all(&qi);
5366 }
5367 else if (curwin->w_llist == NULL)
5368 curwin->w_llist = qi;
5369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370}
5371
5372#endif /* FEAT_QUICKFIX */