blob: b28a321f3cc8accf7f4345249053848e5e43c209 [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 Moolenaar99895ea2017-04-20 22:44:47 +02002753 qi->qf_lists[idx].qf_start = NULL;
Bram Moolenaar31bdd132017-04-15 15:22:52 +02002754 qi->qf_lists[idx].qf_last = NULL;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002755 qi->qf_lists[idx].qf_ptr = NULL;
2756 qi->qf_lists[idx].qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002757
2758 qf_clean_dir_stack(&qi->qf_dir_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002759 qi->qf_directory = NULL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002760 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002761 qi->qf_currfile = NULL;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002762 qi->qf_multiline = FALSE;
2763 qi->qf_multiignore = FALSE;
2764 qi->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765}
2766
2767/*
2768 * qf_mark_adjust: adjust marks
2769 */
2770 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002771qf_mark_adjust(
2772 win_T *wp,
2773 linenr_T line1,
2774 linenr_T line2,
2775 long amount,
2776 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002778 int i;
2779 qfline_T *qfp;
2780 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002781 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002782 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002783 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
Bram Moolenaarc1542742016-07-20 21:44:37 +02002785 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002786 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002787 if (wp != NULL)
2788 {
2789 if (wp->w_llist == NULL)
2790 return;
2791 qi = wp->w_llist;
2792 }
2793
2794 for (idx = 0; idx < qi->qf_listcount; ++idx)
2795 if (qi->qf_lists[idx].qf_count)
2796 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002797 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2798 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 if (qfp->qf_fnum == curbuf->b_fnum)
2800 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002801 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2803 {
2804 if (amount == MAXLNUM)
2805 qfp->qf_cleared = TRUE;
2806 else
2807 qfp->qf_lnum += amount;
2808 }
2809 else if (amount_after && qfp->qf_lnum > line2)
2810 qfp->qf_lnum += amount_after;
2811 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002812
2813 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002814 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815}
2816
2817/*
2818 * Make a nice message out of the error character and the error number:
2819 * char number message
2820 * e or E 0 " error"
2821 * w or W 0 " warning"
2822 * i or I 0 " info"
2823 * 0 0 ""
2824 * other 0 " c"
2825 * e or E n " error n"
2826 * w or W n " warning n"
2827 * i or I n " info n"
2828 * 0 n " error n"
2829 * other n " c n"
2830 * 1 x "" :helpgrep
2831 */
2832 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002833qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 static char_u buf[20];
2836 static char_u cc[3];
2837 char_u *p;
2838
2839 if (c == 'W' || c == 'w')
2840 p = (char_u *)" warning";
2841 else if (c == 'I' || c == 'i')
2842 p = (char_u *)" info";
2843 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2844 p = (char_u *)" error";
2845 else if (c == 0 || c == 1)
2846 p = (char_u *)"";
2847 else
2848 {
2849 cc[0] = ' ';
2850 cc[1] = c;
2851 cc[2] = NUL;
2852 p = cc;
2853 }
2854
2855 if (nr <= 0)
2856 return p;
2857
2858 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2859 return buf;
2860}
2861
2862#if defined(FEAT_WINDOWS) || defined(PROTO)
2863/*
2864 * ":cwindow": open the quickfix window if we have errors to display,
2865 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002866 * ":lwindow": open the location list window if we have locations to display,
2867 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 */
2869 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002870ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002872 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 win_T *win;
2874
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002875 if (eap->cmdidx == CMD_lwindow)
2876 {
2877 qi = GET_LOC_LIST(curwin);
2878 if (qi == NULL)
2879 return;
2880 }
2881
2882 /* Look for an existing quickfix window. */
2883 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
2885 /*
2886 * If a quickfix window is open but we have no errors to display,
2887 * close the window. If a quickfix window is not open, then open
2888 * it if we have errors; otherwise, leave it closed.
2889 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002891 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002892 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
2894 if (win != NULL)
2895 ex_cclose(eap);
2896 }
2897 else if (win == NULL)
2898 ex_copen(eap);
2899}
2900
2901/*
2902 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002903 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002906ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002908 win_T *win = NULL;
2909 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2912 {
2913 qi = GET_LOC_LIST(curwin);
2914 if (qi == NULL)
2915 return;
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002918 /* Find existing quickfix window and close it. */
2919 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 if (win != NULL)
2921 win_close(win, FALSE);
2922}
2923
2924/*
2925 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002926 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 */
2928 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002929ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002931 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002934 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002935 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002936 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002938 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2939 {
2940 qi = GET_LOC_LIST(curwin);
2941 if (qi == NULL)
2942 {
2943 EMSG(_(e_loclist));
2944 return;
2945 }
2946 }
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (eap->addr_count != 0)
2949 height = eap->line2;
2950 else
2951 height = QF_WINHEIGHT;
2952
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954#ifdef FEAT_GUI
2955 need_mouse_correct = TRUE;
2956#endif
2957
2958 /*
2959 * Find existing quickfix window, or open a new one.
2960 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002961 win = qf_find_win(qi);
2962
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002963 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002966 if (eap->addr_count != 0)
2967 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002968 if (cmdmod.split & WSP_VERT)
2969 {
2970 if (height != W_WIDTH(win))
2971 win_setwidth(height);
2972 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002973 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002974 win_setheight(height);
2975 }
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 else
2978 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002979 qf_buf = qf_find_buf(qi);
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 /* The current window becomes the previous window afterwards. */
2982 win = curwin;
2983
Bram Moolenaar77642c02012-11-20 17:55:10 +01002984 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2985 && cmdmod.split == 0)
2986 /* Create the new window at the very bottom, except when
2987 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002988 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002989 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002991 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002993 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002995 /*
2996 * For the location list window, create a reference to the
2997 * location list from the window 'win'.
2998 */
2999 curwin->w_llist_ref = win->w_llist;
3000 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003002
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003003 if (oldwin != curwin)
3004 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003005 if (qf_buf != NULL)
3006 /* Use the existing quickfix buffer */
3007 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003008 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003009 else
3010 {
3011 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003012 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003013 /* switch off 'swapfile' */
3014 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3015 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003016 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003017 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003018 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003019#ifdef FEAT_DIFF
3020 curwin->w_p_diff = FALSE;
3021#endif
3022#ifdef FEAT_FOLDING
3023 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3024 OPT_LOCAL);
3025#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003028 /* Only set the height when still in the same tab page and there is no
3029 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003030 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 win_setheight(height);
3032 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3033 if (win_valid(win))
3034 prevwin = win;
3035 }
3036
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003037 qf_set_title_var(qi);
3038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 /*
3040 * Fill the buffer with the quickfix list.
3041 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003042 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003044 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 curwin->w_cursor.col = 0;
3046 check_cursor();
3047 update_topline(); /* scroll to show the line */
3048}
3049
3050/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003051 * Move the cursor in the quickfix window to "lnum".
3052 */
3053 static void
3054qf_win_goto(win_T *win, linenr_T lnum)
3055{
3056 win_T *old_curwin = curwin;
3057
3058 curwin = win;
3059 curbuf = win->w_buffer;
3060 curwin->w_cursor.lnum = lnum;
3061 curwin->w_cursor.col = 0;
3062#ifdef FEAT_VIRTUALEDIT
3063 curwin->w_cursor.coladd = 0;
3064#endif
3065 curwin->w_curswant = 0;
3066 update_topline(); /* scroll to show the line */
3067 redraw_later(VALID);
3068 curwin->w_redr_status = TRUE; /* update ruler */
3069 curwin = old_curwin;
3070 curbuf = curwin->w_buffer;
3071}
3072
3073/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003074 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003075 */
3076 void
3077ex_cbottom(exarg_T *eap UNUSED)
3078{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003079 qf_info_T *qi = &ql_info;
3080 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003081
Bram Moolenaar537ef082016-07-09 17:56:19 +02003082 if (eap->cmdidx == CMD_lbottom)
3083 {
3084 qi = GET_LOC_LIST(curwin);
3085 if (qi == NULL)
3086 {
3087 EMSG(_(e_loclist));
3088 return;
3089 }
3090 }
3091
3092 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003093 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3094 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3095}
3096
3097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 * Return the number of the current entry (line number in the quickfix
3099 * window).
3100 */
3101 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003102qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003104 qf_info_T *qi = &ql_info;
3105
3106 if (IS_LL_WINDOW(wp))
3107 /* In the location list window, use the referenced location list */
3108 qi = wp->w_llist_ref;
3109
3110 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111}
3112
3113/*
3114 * Update the cursor position in the quickfix window to the current error.
3115 * Return TRUE if there is a quickfix window.
3116 */
3117 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003118qf_win_pos_update(
3119 qf_info_T *qi,
3120 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003123 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
3125 /*
3126 * Put the cursor on the current error in the quickfix window, so that
3127 * it's viewable.
3128 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003129 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (win != NULL
3131 && qf_index <= win->w_buffer->b_ml.ml_line_count
3132 && old_qf_index != qf_index)
3133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 if (qf_index > old_qf_index)
3135 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003136 win->w_redraw_top = old_qf_index;
3137 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 }
3139 else
3140 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003141 win->w_redraw_top = qf_index;
3142 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003144 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
3146 return win != NULL;
3147}
3148
3149/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003150 * Check whether the given window is displaying the specified quickfix/location
3151 * list buffer
3152 */
3153 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003154is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003155{
3156 /*
3157 * A window displaying the quickfix buffer will have the w_llist_ref field
3158 * set to NULL.
3159 * A window displaying a location list buffer will have the w_llist_ref
3160 * pointing to the location list.
3161 */
3162 if (bt_quickfix(win->w_buffer))
3163 if ((qi == &ql_info && win->w_llist_ref == NULL)
3164 || (qi != &ql_info && win->w_llist_ref == qi))
3165 return TRUE;
3166
3167 return FALSE;
3168}
3169
3170/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003171 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003172 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003173 */
3174 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003175qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003176{
3177 win_T *win;
3178
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003179 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003180 if (is_qf_win(win, qi))
3181 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003182
3183 return win;
3184}
3185
3186/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003187 * Find a quickfix buffer.
3188 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 */
3190 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003191qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003193 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003194 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
Bram Moolenaar9c102382006-05-03 21:26:49 +00003196 FOR_ALL_TAB_WINDOWS(tp, win)
3197 if (is_qf_win(win, qi))
3198 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003199
Bram Moolenaar9c102382006-05-03 21:26:49 +00003200 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201}
3202
3203/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003204 * Update the w:quickfix_title variable in the quickfix/location list window
3205 */
3206 static void
3207qf_update_win_titlevar(qf_info_T *qi)
3208{
3209 win_T *win;
3210 win_T *curwin_save;
3211
3212 if ((win = qf_find_win(qi)) != NULL)
3213 {
3214 curwin_save = curwin;
3215 curwin = win;
3216 qf_set_title_var(qi);
3217 curwin = curwin_save;
3218 }
3219}
3220
3221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 * Find the quickfix buffer. If it exists, update the contents.
3223 */
3224 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003225qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003228 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003232 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (buf != NULL)
3234 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003235 linenr_T old_line_count = buf->b_ml.ml_line_count;
3236
3237 if (old_last == NULL)
3238 /* set curwin/curbuf to buf and save a few things */
3239 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
Bram Moolenaard823fa92016-08-12 16:29:27 +02003241 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003242
Bram Moolenaar864293a2016-06-02 13:40:04 +02003243 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003244
Bram Moolenaar864293a2016-06-02 13:40:04 +02003245 if (old_last == NULL)
3246 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003247 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003248
3249 /* restore curwin/curbuf and a few other things */
3250 aucmd_restbuf(&aco);
3251 }
3252
3253 /* Only redraw when added lines are visible. This avoids flickering
3254 * when the added lines are not visible. */
3255 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3256 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258}
3259
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003260/*
3261 * Set "w:quickfix_title" if "qi" has a title.
3262 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003263 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003264qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003265{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003266 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3267 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003268 qi->qf_lists[qi->qf_curlist].qf_title);
3269}
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271/*
3272 * Fill current buffer with quickfix errors, replacing any previous contents.
3273 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003274 * If "old_last" is not NULL append the items after this one.
3275 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3276 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 */
3278 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003279qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003281 linenr_T lnum;
3282 qfline_T *qfp;
3283 buf_T *errbuf;
3284 int len;
3285 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaar864293a2016-06-02 13:40:04 +02003287 if (old_last == NULL)
3288 {
3289 if (buf != curbuf)
3290 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003291 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003292 return;
3293 }
3294
3295 /* delete all existing lines */
3296 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3297 (void)ml_delete((linenr_T)1, FALSE);
3298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299
3300 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003301 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
3303 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003304 if (old_last == NULL)
3305 {
3306 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3307 lnum = 0;
3308 }
3309 else
3310 {
3311 qfp = old_last->qf_next;
3312 lnum = buf->b_ml.ml_line_count;
3313 }
3314 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
3316 if (qfp->qf_fnum != 0
3317 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3318 && errbuf->b_fname != NULL)
3319 {
3320 if (qfp->qf_type == 1) /* :helpgrep */
3321 STRCPY(IObuff, gettail(errbuf->b_fname));
3322 else
3323 STRCPY(IObuff, errbuf->b_fname);
3324 len = (int)STRLEN(IObuff);
3325 }
3326 else
3327 len = 0;
3328 IObuff[len++] = '|';
3329
3330 if (qfp->qf_lnum > 0)
3331 {
3332 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3333 len += (int)STRLEN(IObuff + len);
3334
3335 if (qfp->qf_col > 0)
3336 {
3337 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3338 len += (int)STRLEN(IObuff + len);
3339 }
3340
3341 sprintf((char *)IObuff + len, "%s",
3342 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3343 len += (int)STRLEN(IObuff + len);
3344 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003345 else if (qfp->qf_pattern != NULL)
3346 {
3347 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3348 len += (int)STRLEN(IObuff + len);
3349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 IObuff[len++] = '|';
3351 IObuff[len++] = ' ';
3352
3353 /* Remove newlines and leading whitespace from the text.
3354 * For an unrecognized line keep the indent, the compiler may
3355 * mark a word with ^^^^. */
3356 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3357 IObuff + len, IOSIZE - len);
3358
Bram Moolenaar864293a2016-06-02 13:40:04 +02003359 if (ml_append_buf(buf, lnum, IObuff,
3360 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003362 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003364 if (qfp == NULL)
3365 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003367
3368 if (old_last == NULL)
3369 /* Delete the empty line which is now at the end */
3370 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
3372
3373 /* correct cursor position */
3374 check_lnums(TRUE);
3375
Bram Moolenaar864293a2016-06-02 13:40:04 +02003376 if (old_last == NULL)
3377 {
3378 /* Set the 'filetype' to "qf" each time after filling the buffer.
3379 * This resembles reading a file into a buffer, it's more logical when
3380 * using autocommands. */
3381 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3382 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003385 keep_filetype = TRUE; /* don't detect 'filetype' */
3386 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003388 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003390 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003392 /* make sure it will be redrawn */
3393 redraw_curbuf_later(NOT_VALID);
3394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 /* Restore KeyTyped, setting 'filetype' may reset it. */
3397 KeyTyped = old_KeyTyped;
3398}
3399
3400#endif /* FEAT_WINDOWS */
3401
3402/*
3403 * Return TRUE if "buf" is the quickfix buffer.
3404 */
3405 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003406bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003408 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409}
3410
3411/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003412 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3413 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 */
3415 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003416bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003418 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3419 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420}
3421
3422/*
3423 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3424 */
3425 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003426bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003428 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429}
3430
3431 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003432bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 if (bt_dontwrite(buf))
3435 {
3436 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3437 return TRUE;
3438 }
3439 return FALSE;
3440}
3441
3442/*
3443 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3444 * and 'bufhidden'.
3445 */
3446 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003447buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3450 switch (buf->b_p_bh[0])
3451 {
3452 case 'u': /* "unload" */
3453 case 'w': /* "wipe" */
3454 case 'd': return FALSE; /* "delete" */
3455 case 'h': return TRUE; /* "hide" */
3456 }
3457 return (p_hid || cmdmod.hide);
3458}
3459
3460/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003461 * Return TRUE when using ":vimgrep" for ":grep".
3462 */
3463 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003464grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003465{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003466 return ((cmdidx == CMD_grep
3467 || cmdidx == CMD_lgrep
3468 || cmdidx == CMD_grepadd
3469 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003470 && STRCMP("internal",
3471 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3472}
3473
3474/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003475 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 */
3477 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003478ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003480 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003482 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003484 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003485 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003486 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003487#ifdef FEAT_AUTOCMD
3488 char_u *au_name = NULL;
3489
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003490 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3491 if (grep_internal(eap->cmdidx))
3492 {
3493 ex_vimgrep(eap);
3494 return;
3495 }
3496
Bram Moolenaar7c626922005-02-07 22:01:03 +00003497 switch (eap->cmdidx)
3498 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003499 case CMD_make: au_name = (char_u *)"make"; break;
3500 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3501 case CMD_grep: au_name = (char_u *)"grep"; break;
3502 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3503 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3504 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003505 default: break;
3506 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003507 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3508 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003509 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003510# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003511 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003512 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003513# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003514 }
3515#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003516#ifdef FEAT_MBYTE
3517 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
Bram Moolenaara6557602006-02-04 22:43:20 +00003520 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3521 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003522 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003525 fname = get_mef_name();
3526 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003528 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 /*
3531 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3532 */
3533 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3534 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003535 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 cmd = alloc(len);
3537 if (cmd == NULL)
3538 return;
3539 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3540 (char *)p_shq);
3541 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003542 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /*
3544 * Output a newline if there's something else than the :make command that
3545 * was typed (in which case the cursor is in column 0).
3546 */
3547 if (msg_col == 0)
3548 msg_didout = FALSE;
3549 msg_start();
3550 MSG_PUTS(":!");
3551 msg_outtrans(cmd); /* show what we are doing */
3552
3553 /* let the shell know if we are redirecting output or not */
3554 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3555
3556#ifdef AMIGA
3557 out_flush();
3558 /* read window status report and redraw before message */
3559 (void)char_avail();
3560#endif
3561
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003562 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003563 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3564 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003565 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003566 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003567 if (wp != NULL)
3568 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003569#ifdef FEAT_AUTOCMD
3570 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003571 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003572 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3573 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003574 if (qi->qf_curlist < qi->qf_listcount)
3575 res = qi->qf_lists[qi->qf_curlist].qf_count;
3576 else
3577 res = 0;
3578 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003579#endif
3580 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003581 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
Bram Moolenaar7c626922005-02-07 22:01:03 +00003583 mch_remove(fname);
3584 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 vim_free(cmd);
3586}
3587
3588/*
3589 * Return the name for the errorfile, in allocated memory.
3590 * Find a new unique name when 'makeef' contains "##".
3591 * Returns NULL for error.
3592 */
3593 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003594get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
3596 char_u *p;
3597 char_u *name;
3598 static int start = -1;
3599 static int off = 0;
3600#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003601 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602#endif
3603
3604 if (*p_mef == NUL)
3605 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003606 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (name == NULL)
3608 EMSG(_(e_notmp));
3609 return name;
3610 }
3611
3612 for (p = p_mef; *p; ++p)
3613 if (p[0] == '#' && p[1] == '#')
3614 break;
3615
3616 if (*p == NUL)
3617 return vim_strsave(p_mef);
3618
3619 /* Keep trying until the name doesn't exist yet. */
3620 for (;;)
3621 {
3622 if (start == -1)
3623 start = mch_get_pid();
3624 else
3625 off += 19;
3626
3627 name = alloc((unsigned)STRLEN(p_mef) + 30);
3628 if (name == NULL)
3629 break;
3630 STRCPY(name, p_mef);
3631 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3632 STRCAT(name, p + 2);
3633 if (mch_getperm(name) < 0
3634#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003635 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 && mch_lstat((char *)name, &sb) < 0
3637#endif
3638 )
3639 break;
3640 vim_free(name);
3641 }
3642 return name;
3643}
3644
3645/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003646 * Returns the number of valid entries in the current quickfix/location list.
3647 */
3648 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003649qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003650{
3651 qf_info_T *qi = &ql_info;
3652 qfline_T *qfp;
3653 int i, sz = 0;
3654 int prev_fnum = 0;
3655
3656 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3657 {
3658 /* Location list */
3659 qi = GET_LOC_LIST(curwin);
3660 if (qi == NULL)
3661 return 0;
3662 }
3663
3664 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003665 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003666 ++i, qfp = qfp->qf_next)
3667 {
3668 if (qfp->qf_valid)
3669 {
3670 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3671 sz++; /* Count all valid entries */
3672 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3673 {
3674 /* Count the number of files */
3675 sz++;
3676 prev_fnum = qfp->qf_fnum;
3677 }
3678 }
3679 }
3680
3681 return sz;
3682}
3683
3684/*
3685 * Returns the current index of the quickfix/location list.
3686 * Returns 0 if there is an error.
3687 */
3688 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003689qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003690{
3691 qf_info_T *qi = &ql_info;
3692
3693 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3694 {
3695 /* Location list */
3696 qi = GET_LOC_LIST(curwin);
3697 if (qi == NULL)
3698 return 0;
3699 }
3700
3701 return qi->qf_lists[qi->qf_curlist].qf_index;
3702}
3703
3704/*
3705 * Returns the current index in the quickfix/location list (counting only valid
3706 * entries). If no valid entries are in the list, then returns 1.
3707 */
3708 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003709qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003710{
3711 qf_info_T *qi = &ql_info;
3712 qf_list_T *qfl;
3713 qfline_T *qfp;
3714 int i, eidx = 0;
3715 int prev_fnum = 0;
3716
3717 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3718 {
3719 /* Location list */
3720 qi = GET_LOC_LIST(curwin);
3721 if (qi == NULL)
3722 return 1;
3723 }
3724
3725 qfl = &qi->qf_lists[qi->qf_curlist];
3726 qfp = qfl->qf_start;
3727
3728 /* check if the list has valid errors */
3729 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3730 return 1;
3731
3732 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3733 {
3734 if (qfp->qf_valid)
3735 {
3736 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3737 {
3738 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3739 {
3740 /* Count the number of files */
3741 eidx++;
3742 prev_fnum = qfp->qf_fnum;
3743 }
3744 }
3745 else
3746 eidx++;
3747 }
3748 }
3749
3750 return eidx ? eidx : 1;
3751}
3752
3753/*
3754 * Get the 'n'th valid error entry in the quickfix or location list.
3755 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3756 * For :cdo and :ldo returns the 'n'th valid error entry.
3757 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3758 */
3759 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003760qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003761{
3762 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3763 qfline_T *qfp = qfl->qf_start;
3764 int i, eidx;
3765 int prev_fnum = 0;
3766
3767 /* check if the list has valid errors */
3768 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3769 return 1;
3770
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003771 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003772 i++, qfp = qfp->qf_next)
3773 {
3774 if (qfp->qf_valid)
3775 {
3776 if (fdo)
3777 {
3778 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3779 {
3780 /* Count the number of files */
3781 eidx++;
3782 prev_fnum = qfp->qf_fnum;
3783 }
3784 }
3785 else
3786 eidx++;
3787 }
3788
3789 if (eidx == n)
3790 break;
3791 }
3792
3793 if (i <= qfl->qf_count)
3794 return i;
3795 else
3796 return 1;
3797}
3798
3799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003801 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003802 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 */
3804 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003805ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003807 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003808 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003809
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003810 if (eap->cmdidx == CMD_ll
3811 || eap->cmdidx == CMD_lrewind
3812 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003813 || eap->cmdidx == CMD_llast
3814 || eap->cmdidx == CMD_ldo
3815 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003816 {
3817 qi = GET_LOC_LIST(curwin);
3818 if (qi == NULL)
3819 {
3820 EMSG(_(e_loclist));
3821 return;
3822 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003823 }
3824
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003825 if (eap->addr_count > 0)
3826 errornr = (int)eap->line2;
3827 else
3828 {
3829 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3830 errornr = 0;
3831 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3832 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3833 errornr = 1;
3834 else
3835 errornr = 32767;
3836 }
3837
3838 /* For cdo and ldo commands, jump to the nth valid error.
3839 * For cfdo and lfdo commands, jump to the nth valid file entry.
3840 */
3841 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3842 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3843 errornr = qf_get_nth_valid_entry(qi,
3844 eap->addr_count > 0 ? (int)eap->line1 : 1,
3845 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3846
3847 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848}
3849
3850/*
3851 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003852 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003853 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 */
3855 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003856ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003858 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003859 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003860
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003861 if (eap->cmdidx == CMD_lnext
3862 || eap->cmdidx == CMD_lNext
3863 || eap->cmdidx == CMD_lprevious
3864 || eap->cmdidx == CMD_lnfile
3865 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003866 || eap->cmdidx == CMD_lpfile
3867 || eap->cmdidx == CMD_ldo
3868 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003869 {
3870 qi = GET_LOC_LIST(curwin);
3871 if (qi == NULL)
3872 {
3873 EMSG(_(e_loclist));
3874 return;
3875 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003876 }
3877
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003878 if (eap->addr_count > 0 &&
3879 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3880 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3881 errornr = (int)eap->line2;
3882 else
3883 errornr = 1;
3884
3885 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3886 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003888 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3889 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003891 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3892 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 ? BACKWARD_FILE
3894 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003895 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896}
3897
3898/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003899 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003900 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 */
3902 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003903ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003905 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003906 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003907 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003908#ifdef FEAT_AUTOCMD
3909 char_u *au_name = NULL;
3910#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003911
3912 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003913 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003914 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003915
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003916#ifdef FEAT_AUTOCMD
3917 switch (eap->cmdidx)
3918 {
3919 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3920 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3921 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3922 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3923 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3924 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3925 default: break;
3926 }
3927 if (au_name != NULL)
3928 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3929#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003930#ifdef FEAT_MBYTE
3931 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3932#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003933#ifdef FEAT_BROWSE
3934 if (cmdmod.browse)
3935 {
3936 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3937 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3938 if (browse_file == NULL)
3939 return;
3940 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3941 vim_free(browse_file);
3942 }
3943 else
3944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003946 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003947
3948 /*
3949 * This function is used by the :cfile, :cgetfile and :caddfile
3950 * commands.
3951 * :cfile always creates a new quickfix list and jumps to the
3952 * first error.
3953 * :cgetfile creates a new quickfix list but doesn't jump to the
3954 * first error.
3955 * :caddfile adds to an existing quickfix list. If there is no
3956 * quickfix list then a new list is created.
3957 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003958 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003959 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003960 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003961 && (eap->cmdidx == CMD_cfile
3962 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003963 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003964#ifdef FEAT_AUTOCMD
3965 if (au_name != NULL)
3966 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3967#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003968 if (wp != NULL)
3969 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003970 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003971 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003972
3973 else
3974 {
3975#ifdef FEAT_AUTOCMD
3976 if (au_name != NULL)
3977 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3978#endif
3979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980}
3981
3982/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003983 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003984 * ":vimgrepadd {pattern} file(s)"
3985 * ":lvimgrep {pattern} file(s)"
3986 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003987 */
3988 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003989ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003990{
Bram Moolenaar81695252004-12-29 20:58:21 +00003991 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003992 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003993 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003994 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003995 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003996 char_u *s;
3997 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003998 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003999 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004000#ifdef FEAT_AUTOCMD
4001 qfline_T *cur_qf_start;
4002#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004003 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004004 buf_T *buf;
4005 int duplicate_name = FALSE;
4006 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004007 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004008 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004009 buf_T *first_match_buf = NULL;
4010 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004011 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004012#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4013 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004014#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004015 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004016 int flags = 0;
4017 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004018 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004019 char_u *dirname_start = NULL;
4020 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004021 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004022#ifdef FEAT_AUTOCMD
4023 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004024
4025 switch (eap->cmdidx)
4026 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004027 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4028 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4029 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004030 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004031 case CMD_grep: au_name = (char_u *)"grep"; break;
4032 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4033 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4034 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004035 default: break;
4036 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004037 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4038 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004039 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004040# ifdef FEAT_EVAL
4041 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004042 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004043# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004044 }
4045#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004046
Bram Moolenaar754b5602006-02-09 23:53:20 +00004047 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004048 || eap->cmdidx == CMD_lvimgrep
4049 || eap->cmdidx == CMD_lgrepadd
4050 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004051 {
4052 qi = ll_get_or_alloc_list(curwin);
4053 if (qi == NULL)
4054 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004055 }
4056
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004057 if (eap->addr_count > 0)
4058 tomatch = eap->line2;
4059 else
4060 tomatch = MAXLNUM;
4061
Bram Moolenaar81695252004-12-29 20:58:21 +00004062 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004063 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004064 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004065 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004066 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004067 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004068 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004069 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004070 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004071
4072 if (s != NULL && *s == NUL)
4073 {
4074 /* Pattern is empty, use last search pattern. */
4075 if (last_search_pat() == NULL)
4076 {
4077 EMSG(_(e_noprevre));
4078 goto theend;
4079 }
4080 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4081 }
4082 else
4083 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4084
Bram Moolenaar86b68352004-12-27 21:59:20 +00004085 if (regmatch.regprog == NULL)
4086 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004087 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004088 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004089
4090 p = skipwhite(p);
4091 if (*p == NUL)
4092 {
4093 EMSG(_("E683: File name missing or invalid pattern"));
4094 goto theend;
4095 }
4096
Bram Moolenaar754b5602006-02-09 23:53:20 +00004097 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004098 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004099 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004100 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004101 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004102
4103 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004104 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004105 goto theend;
4106 if (fcount == 0)
4107 {
4108 EMSG(_(e_nomatch));
4109 goto theend;
4110 }
4111
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004112 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4113 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004114 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004115 {
4116 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004117 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004118 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004119
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004120 /* Remember the current directory, because a BufRead autocommand that does
4121 * ":lcd %:p:h" changes the meaning of short path names. */
4122 mch_dirname(dirname_start, MAXPATHL);
4123
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004124#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004125 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004126 * changing the current quickfix list. */
4127 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4128#endif
4129
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004130 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004131 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004132 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004133 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004134 if (time(NULL) > seconds)
4135 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004136 /* Display the file name every second or so, show the user we are
4137 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004138 seconds = time(NULL);
4139 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004140 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004141 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004142 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004143 else
4144 {
4145 msg_outtrans(p);
4146 vim_free(p);
4147 }
4148 msg_clr_eos();
4149 msg_didout = FALSE; /* overwrite this message */
4150 msg_nowait = TRUE; /* don't wait for this message */
4151 msg_col = 0;
4152 out_flush();
4153 }
4154
Bram Moolenaar81695252004-12-29 20:58:21 +00004155 buf = buflist_findname_exp(fnames[fi]);
4156 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4157 {
4158 /* Remember that a buffer with this name already exists. */
4159 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004160 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004161 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004162
4163#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4164 /* Don't do Filetype autocommands to avoid loading syntax and
4165 * indent scripts, a great speed improvement. */
4166 save_ei = au_event_disable(",Filetype");
4167#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004168 /* Don't use modelines here, it's useless. */
4169 save_mls = p_mls;
4170 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004171
4172 /* Load file into a buffer, so that 'fileencoding' is detected,
4173 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004174 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004175
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004176 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004177#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4178 au_event_restore(save_ei);
4179#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004180 }
4181 else
4182 /* Use existing, loaded buffer. */
4183 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004184
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004185#ifdef FEAT_AUTOCMD
4186 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4187 {
4188 int idx;
4189
4190 /* Autocommands changed the quickfix list. Find the one we were
4191 * using and restore it. */
4192 for (idx = 0; idx < LISTCOUNT; ++idx)
4193 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4194 {
4195 qi->qf_curlist = idx;
4196 break;
4197 }
4198 if (idx == LISTCOUNT)
4199 {
4200 /* List cannot be found, create a new one. */
4201 qf_new_list(qi, *eap->cmdlinep);
4202 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4203 }
4204 }
4205#endif
4206
Bram Moolenaar81695252004-12-29 20:58:21 +00004207 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004208 {
4209 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004210 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004211 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004212 else
4213 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004214 /* Try for a match in all lines of the buffer.
4215 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004216 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004217 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4218 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004219 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004220 col = 0;
4221 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004222 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004223 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004224 /* Pass the buffer number so that it gets used even for a
4225 * dummy buffer, unless duplicate_name is set, then the
4226 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004227 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004228 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004229 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004230 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004231 ml_get_buf(buf,
4232 regmatch.startpos[0].lnum + lnum, FALSE),
4233 regmatch.startpos[0].lnum + lnum,
4234 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004235 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004236 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004237 0, /* nr */
4238 0, /* type */
4239 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004240 ) == FAIL)
4241 {
4242 got_int = TRUE;
4243 break;
4244 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004245 found_match = TRUE;
4246 if (--tomatch == 0)
4247 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004248 if ((flags & VGR_GLOBAL) == 0
4249 || regmatch.endpos[0].lnum > 0)
4250 break;
4251 col = regmatch.endpos[0].col
4252 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004253 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004254 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004255 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004256 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004257 if (got_int)
4258 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004259 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004260#ifdef FEAT_AUTOCMD
4261 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4262#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004263
4264 if (using_dummy)
4265 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004266 if (found_match && first_match_buf == NULL)
4267 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004268 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004269 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004270 /* Never keep a dummy buffer if there is another buffer
4271 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004272 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004273 buf = NULL;
4274 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004275 else if (!cmdmod.hide
4276 || buf->b_p_bh[0] == 'u' /* "unload" */
4277 || buf->b_p_bh[0] == 'w' /* "wipe" */
4278 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004279 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004280 /* When no match was found we don't need to remember the
4281 * buffer, wipe it out. If there was a match and it
4282 * wasn't the first one or we won't jump there: only
4283 * unload the buffer.
4284 * Ignore 'hidden' here, because it may lead to having too
4285 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004286 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004287 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004288 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004289 buf = NULL;
4290 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004291 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004292 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004293 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004294 /* Keeping the buffer, remove the dummy flag. */
4295 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004296 buf = NULL;
4297 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004298 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004299
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004300 if (buf != NULL)
4301 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004302 /* Keeping the buffer, remove the dummy flag. */
4303 buf->b_flags &= ~BF_DUMMY;
4304
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004305 /* If the buffer is still loaded we need to use the
4306 * directory we jumped to below. */
4307 if (buf == first_match_buf
4308 && target_dir == NULL
4309 && STRCMP(dirname_start, dirname_now) != 0)
4310 target_dir = vim_strsave(dirname_now);
4311
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004312 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004313 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004314 * need to be done (again). But not the window-local
4315 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004316 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004317#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004318 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4319 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004320#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004321 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004322 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004323 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004324 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004325 }
4326 }
4327
4328 FreeWild(fcount, fnames);
4329
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004330 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4331 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4332 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004333
4334#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004335 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004336#endif
4337
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004338#ifdef FEAT_AUTOCMD
4339 if (au_name != NULL)
4340 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4341 curbuf->b_fname, TRUE, curbuf);
4342#endif
4343
Bram Moolenaar86b68352004-12-27 21:59:20 +00004344 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004345 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004346 {
4347 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004348 {
4349 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004350 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004351 if (buf != curbuf)
4352 /* If we jumped to another buffer redrawing will already be
4353 * taken care of. */
4354 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004355
4356 /* Jump to the directory used after loading the buffer. */
4357 if (curbuf == first_match_buf && target_dir != NULL)
4358 {
4359 exarg_T ea;
4360
4361 ea.arg = target_dir;
4362 ea.cmdidx = CMD_lcd;
4363 ex_cd(&ea);
4364 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004365 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004366 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004367 else
4368 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004369
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004370 /* If we loaded a dummy buffer into the current window, the autocommands
4371 * may have messed up things, need to redraw and recompute folds. */
4372 if (redraw_for_dummy)
4373 {
4374#ifdef FEAT_FOLDING
4375 foldUpdateAll(curwin);
4376#else
4377 redraw_later(NOT_VALID);
4378#endif
4379 }
4380
Bram Moolenaar86b68352004-12-27 21:59:20 +00004381theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004382 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004383 vim_free(dirname_now);
4384 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004385 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004386 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004387}
4388
4389/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004390 * Restore current working directory to "dirname_start" if they differ, taking
4391 * into account whether it is set locally or globally.
4392 */
4393 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004394restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004395{
4396 char_u *dirname_now = alloc(MAXPATHL);
4397
4398 if (NULL != dirname_now)
4399 {
4400 mch_dirname(dirname_now, MAXPATHL);
4401 if (STRCMP(dirname_start, dirname_now) != 0)
4402 {
4403 /* If the directory has changed, change it back by building up an
4404 * appropriate ex command and executing it. */
4405 exarg_T ea;
4406
4407 ea.arg = dirname_start;
4408 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4409 ex_cd(&ea);
4410 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004411 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004412 }
4413}
4414
4415/*
4416 * Load file "fname" into a dummy buffer and return the buffer pointer,
4417 * placing the directory resulting from the buffer load into the
4418 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4419 * prior to calling this function. Restores directory to "dirname_start" prior
4420 * to returning, if autocmds or the 'autochdir' option have changed it.
4421 *
4422 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4423 * or wipe_dummy_buffer() later!
4424 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004425 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004426 */
4427 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004428load_dummy_buffer(
4429 char_u *fname,
4430 char_u *dirname_start, /* in: old directory */
4431 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004432{
4433 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004434 bufref_T newbufref;
4435 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004436 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004437 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004438
4439 /* Allocate a buffer without putting it in the buffer list. */
4440 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4441 if (newbuf == NULL)
4442 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004443 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004444
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004445 /* Init the options. */
4446 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4447
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004448 /* need to open the memfile before putting the buffer in a window */
4449 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004450 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004451 /* set curwin/curbuf to buf and save a few things */
4452 aucmd_prepbuf(&aco, newbuf);
4453
4454 /* Need to set the filename for autocommands. */
4455 (void)setfname(curbuf, fname, NULL, FALSE);
4456
Bram Moolenaar81695252004-12-29 20:58:21 +00004457 /* Create swap file now to avoid the ATTENTION message. */
4458 check_need_swap(TRUE);
4459
4460 /* Remove the "dummy" flag, otherwise autocommands may not
4461 * work. */
4462 curbuf->b_flags &= ~BF_DUMMY;
4463
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004464 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004465 if (readfile(fname, NULL,
4466 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4467 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004468 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004469 && !(curbuf->b_flags & BF_NEW))
4470 {
4471 failed = FALSE;
4472 if (curbuf != newbuf)
4473 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004474 /* Bloody autocommands changed the buffer! Can happen when
4475 * using netrw and editing a remote file. Use the current
4476 * buffer instead, delete the dummy one after restoring the
4477 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004478 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004479 newbuf = curbuf;
4480 }
4481 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004482
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004483 /* restore curwin/curbuf and a few other things */
4484 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004485 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4486 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004487
4488 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4489 * skip it. */
4490 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004491 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004492
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004493 /*
4494 * When autocommands/'autochdir' option changed directory: go back.
4495 * Let the caller know what the resulting dir was first, in case it is
4496 * important.
4497 */
4498 mch_dirname(resulting_dir, MAXPATHL);
4499 restore_start_dir(dirname_start);
4500
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004501 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004502 return NULL;
4503 if (failed)
4504 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004505 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004506 return NULL;
4507 }
4508 return newbuf;
4509}
4510
4511/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004512 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4513 * directory to "dirname_start" prior to returning, if autocmds or the
4514 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004515 */
4516 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004517wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004518{
4519 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004520 {
4521#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4522 cleanup_T cs;
4523
4524 /* Reset the error/interrupt/exception state here so that aborting()
4525 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4526 * work when got_int is set. */
4527 enter_cleanup(&cs);
4528#endif
4529
Bram Moolenaar81695252004-12-29 20:58:21 +00004530 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004531
4532#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4533 /* Restore the error/interrupt/exception state if not discarded by a
4534 * new aborting error, interrupt, or uncaught exception. */
4535 leave_cleanup(&cs);
4536#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004537 /* When autocommands/'autochdir' option changed directory: go back. */
4538 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004539 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004540}
4541
4542/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004543 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4544 * directory to "dirname_start" prior to returning, if autocmds or the
4545 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004546 */
4547 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004548unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004549{
4550 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004551 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004552 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004553
4554 /* When autocommands/'autochdir' option changed directory: go back. */
4555 restore_start_dir(dirname_start);
4556 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004557}
4558
Bram Moolenaar05159a02005-02-26 23:04:13 +00004559#if defined(FEAT_EVAL) || defined(PROTO)
4560/*
4561 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004562 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004563 */
4564 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004565get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004566{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004567 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004568 dict_T *dict;
4569 char_u buf[2];
4570 qfline_T *qfp;
4571 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004572 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004573
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004574 if (wp != NULL)
4575 {
4576 qi = GET_LOC_LIST(wp);
4577 if (qi == NULL)
4578 return FAIL;
4579 }
4580
Bram Moolenaard823fa92016-08-12 16:29:27 +02004581 if (qf_idx == -1)
4582 qf_idx = qi->qf_curlist;
4583
4584 if (qf_idx >= qi->qf_listcount
4585 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004586 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004587
Bram Moolenaard823fa92016-08-12 16:29:27 +02004588 qfp = qi->qf_lists[qf_idx].qf_start;
4589 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004590 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004591 /* Handle entries with a non-existing buffer number. */
4592 bufnum = qfp->qf_fnum;
4593 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4594 bufnum = 0;
4595
Bram Moolenaar05159a02005-02-26 23:04:13 +00004596 if ((dict = dict_alloc()) == NULL)
4597 return FAIL;
4598 if (list_append_dict(list, dict) == FAIL)
4599 return FAIL;
4600
4601 buf[0] = qfp->qf_type;
4602 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004603 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004604 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4605 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4606 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4607 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004608 || dict_add_nr_str(dict, "pattern", 0L,
4609 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4610 || dict_add_nr_str(dict, "text", 0L,
4611 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004612 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4613 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4614 return FAIL;
4615
4616 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004617 if (qfp == NULL)
4618 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004619 }
4620 return OK;
4621}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004622
4623/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004624 * Flags used by getqflist()/getloclist() to determine which fields to return.
4625 */
4626enum {
4627 QF_GETLIST_NONE = 0x0,
4628 QF_GETLIST_TITLE = 0x1,
4629 QF_GETLIST_ITEMS = 0x2,
4630 QF_GETLIST_NR = 0x4,
4631 QF_GETLIST_WINID = 0x8,
4632 QF_GETLIST_ALL = 0xFF
4633};
4634
4635/*
4636 * Return quickfix/location list details (title) as a
4637 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4638 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004639 */
4640 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004641get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4642{
4643 qf_info_T *qi = &ql_info;
4644 int status = OK;
4645 int qf_idx;
4646 dictitem_T *di;
4647 int flags = QF_GETLIST_NONE;
4648
4649 if (wp != NULL)
4650 {
4651 qi = GET_LOC_LIST(wp);
4652 if (qi == NULL)
4653 return FAIL;
4654 }
4655
4656 qf_idx = qi->qf_curlist; /* default is the current list */
4657 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4658 {
4659 /* Use the specified quickfix/location list */
4660 if (di->di_tv.v_type == VAR_NUMBER)
4661 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004662 /* for zero use the current list */
4663 if (di->di_tv.vval.v_number != 0)
4664 {
4665 qf_idx = di->di_tv.vval.v_number - 1;
4666 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4667 return FAIL;
4668 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004669 flags |= QF_GETLIST_NR;
4670 }
4671 else
4672 return FAIL;
4673 }
4674
4675 if (dict_find(what, (char_u *)"all", -1) != NULL)
4676 flags |= QF_GETLIST_ALL;
4677
4678 if (dict_find(what, (char_u *)"title", -1) != NULL)
4679 flags |= QF_GETLIST_TITLE;
4680
4681 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4682 flags |= QF_GETLIST_WINID;
4683
4684 if (flags & QF_GETLIST_TITLE)
4685 {
4686 char_u *t;
4687 t = qi->qf_lists[qf_idx].qf_title;
4688 if (t == NULL)
4689 t = (char_u *)"";
4690 status = dict_add_nr_str(retdict, "title", 0L, t);
4691 }
4692 if ((status == OK) && (flags & QF_GETLIST_NR))
4693 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4694 if ((status == OK) && (flags & QF_GETLIST_WINID))
4695 {
4696 win_T *win;
4697 win = qf_find_win(qi);
4698 if (win != NULL)
4699 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4700 }
4701
4702 return status;
4703}
4704
4705/*
4706 * Add list of entries to quickfix/location list. Each list entry is
4707 * a dictionary with item information.
4708 */
4709 static int
4710qf_add_entries(
4711 qf_info_T *qi,
4712 list_T *list,
4713 char_u *title,
4714 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004715{
4716 listitem_T *li;
4717 dict_T *d;
4718 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004719 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004720 long lnum;
4721 int col, nr;
4722 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004723#ifdef FEAT_WINDOWS
4724 qfline_T *old_last = NULL;
4725#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004726 int valid, status;
4727 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004728 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004729
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004730 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004731 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004732 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004733#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004734 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4735 /* Adding to existing list, use last entry. */
4736 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004737#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004738 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004739 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004740 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004741 qf_store_title(qi, title);
4742 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004743
4744 for (li = list->lv_first; li != NULL; li = li->li_next)
4745 {
4746 if (li->li_tv.v_type != VAR_DICT)
4747 continue; /* Skip non-dict items */
4748
4749 d = li->li_tv.vval.v_dict;
4750 if (d == NULL)
4751 continue;
4752
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004753 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004754 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4755 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4756 col = (int)get_dict_number(d, (char_u *)"col");
4757 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4758 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004759 type = get_dict_string(d, (char_u *)"type", TRUE);
4760 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4761 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004762 if (text == NULL)
4763 text = vim_strsave((char_u *)"");
4764
4765 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004766 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004767 valid = FALSE;
4768
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004769 /* Mark entries with non-existing buffer number as not valid. Give the
4770 * error message only once. */
4771 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4772 {
4773 if (!did_bufnr_emsg)
4774 {
4775 did_bufnr_emsg = TRUE;
4776 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4777 }
4778 valid = FALSE;
4779 bufnum = 0;
4780 }
4781
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004782 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004783 NULL, /* dir */
4784 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004785 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004786 text,
4787 lnum,
4788 col,
4789 vcol, /* vis_col */
4790 pattern, /* search pattern */
4791 nr,
4792 type == NULL ? NUL : *type,
4793 valid);
4794
4795 vim_free(filename);
4796 vim_free(pattern);
4797 vim_free(text);
4798 vim_free(type);
4799
4800 if (status == FAIL)
4801 {
4802 retval = FAIL;
4803 break;
4804 }
4805 }
4806
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004807 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004808 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004809 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4810 else
4811 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004812 if (action != 'a')
4813 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004814 qi->qf_lists[qi->qf_curlist].qf_ptr =
4815 qi->qf_lists[qi->qf_curlist].qf_start;
4816 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4817 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4818 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004819
4820#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004821 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004822 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004823#endif
4824
4825 return retval;
4826}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004827
4828 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004829qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004830{
4831 dictitem_T *di;
4832 int retval = FAIL;
4833 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004834 int newlist = FALSE;
4835
4836 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4837 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004838
4839 qf_idx = qi->qf_curlist; /* default is the current list */
4840 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4841 {
4842 /* Use the specified quickfix/location list */
4843 if (di->di_tv.v_type == VAR_NUMBER)
4844 {
4845 qf_idx = di->di_tv.vval.v_number - 1;
4846 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4847 return FAIL;
4848 }
4849 else
4850 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004851 newlist = FALSE; /* use the specified list */
4852 }
4853
4854 if (newlist)
4855 {
4856 qf_new_list(qi, NULL);
4857 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004858 }
4859
4860 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4861 {
4862 if (di->di_tv.v_type == VAR_STRING)
4863 {
4864 vim_free(qi->qf_lists[qf_idx].qf_title);
4865 qi->qf_lists[qf_idx].qf_title =
4866 get_dict_string(what, (char_u *)"title", TRUE);
4867 if (qf_idx == qi->qf_curlist)
4868 qf_update_win_titlevar(qi);
4869 retval = OK;
4870 }
4871 }
4872
4873 return retval;
4874}
4875
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004876/*
4877 * Find the non-location list window with the specified location list.
4878 */
4879 static win_T *
4880find_win_with_ll(qf_info_T *qi)
4881{
4882 win_T *wp = NULL;
4883
4884 FOR_ALL_WINDOWS(wp)
4885 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
4886 return wp;
4887
4888 return NULL;
4889}
4890
4891/*
4892 * Free the entire quickfix/location list stack.
4893 * If the quickfix/location list window is open, then clear it.
4894 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004895 static void
4896qf_free_stack(win_T *wp, qf_info_T *qi)
4897{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004898 win_T *qfwin = qf_find_win(qi);
4899 win_T *llwin = NULL;
4900 win_T *orig_wp = wp;
4901
4902 if (qfwin != NULL)
4903 {
4904 /* If the quickfix/location list window is open, then clear it */
4905 if (qi->qf_curlist < qi->qf_listcount)
4906 qf_free(qi, qi->qf_curlist);
4907 qf_update_buffer(qi, NULL);
4908 }
4909
4910 if (wp != NULL && IS_LL_WINDOW(wp))
4911 {
4912 /* If in the location list window, then use the non-location list
4913 * window with this location list (if present)
4914 */
4915 llwin = find_win_with_ll(qi);
4916 if (llwin != NULL)
4917 wp = llwin;
4918 }
4919
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004920 qf_free_all(wp);
4921 if (wp == NULL)
4922 {
4923 /* quickfix list */
4924 qi->qf_curlist = 0;
4925 qi->qf_listcount = 0;
4926 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004927 else if (IS_LL_WINDOW(orig_wp))
4928 {
4929 /* If the location list window is open, then create a new empty
4930 * location list */
4931 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02004932
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004933 orig_wp->w_llist_ref = new_ll;
4934 if (llwin != NULL)
4935 {
4936 llwin->w_llist = new_ll;
4937 new_ll->qf_refcount++;
4938 }
4939 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004940}
4941
Bram Moolenaard823fa92016-08-12 16:29:27 +02004942/*
4943 * Populate the quickfix list with the items supplied in the list
4944 * of dictionaries. "title" will be copied to w:quickfix_title.
4945 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
4946 */
4947 int
4948set_errorlist(
4949 win_T *wp,
4950 list_T *list,
4951 int action,
4952 char_u *title,
4953 dict_T *what)
4954{
4955 qf_info_T *qi = &ql_info;
4956 int retval = OK;
4957
4958 if (wp != NULL)
4959 {
4960 qi = ll_get_or_alloc_list(wp);
4961 if (qi == NULL)
4962 return FAIL;
4963 }
4964
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004965 if (action == 'f')
4966 {
4967 /* Free the entire quickfix or location list stack */
4968 qf_free_stack(wp, qi);
4969 }
4970 else if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004971 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004972 else
4973 retval = qf_add_entries(qi, list, title, action);
4974
4975 return retval;
4976}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004977#endif
4978
Bram Moolenaar81695252004-12-29 20:58:21 +00004979/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004980 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004981 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004982 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004983 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004984 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004985 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004986 */
4987 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004988ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004989{
4990 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004991 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004992#ifdef FEAT_AUTOCMD
4993 char_u *au_name = NULL;
4994#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004995
Bram Moolenaardb552d602006-03-23 22:59:57 +00004996 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4997 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004998 {
4999 qi = ll_get_or_alloc_list(curwin);
5000 if (qi == NULL)
5001 return;
5002 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005003
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005004#ifdef FEAT_AUTOCMD
5005 switch (eap->cmdidx)
5006 {
5007 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5008 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5009 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5010 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5011 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5012 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5013 default: break;
5014 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005015 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5016 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005017 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005018# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005019 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005020 return;
5021# endif
5022 }
5023#endif
5024
Bram Moolenaar86b68352004-12-27 21:59:20 +00005025 if (*eap->arg == NUL)
5026 buf = curbuf;
5027 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5028 buf = buflist_findnr(atoi((char *)eap->arg));
5029 if (buf == NULL)
5030 EMSG(_(e_invarg));
5031 else if (buf->b_ml.ml_mfp == NULL)
5032 EMSG(_("E681: Buffer is not loaded"));
5033 else
5034 {
5035 if (eap->addr_count == 0)
5036 {
5037 eap->line1 = 1;
5038 eap->line2 = buf->b_ml.ml_line_count;
5039 }
5040 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5041 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5042 EMSG(_(e_invrange));
5043 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005044 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005045 char_u *qf_title = *eap->cmdlinep;
5046
5047 if (buf->b_sfname)
5048 {
5049 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5050 (char *)qf_title, (char *)buf->b_sfname);
5051 qf_title = IObuff;
5052 }
5053
Bram Moolenaardb552d602006-03-23 22:59:57 +00005054 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
5055 (eap->cmdidx != CMD_caddbuffer
5056 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005057 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005058 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005059 {
5060#ifdef FEAT_AUTOCMD
5061 if (au_name != NULL)
5062 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5063 curbuf->b_fname, TRUE, curbuf);
5064#endif
5065 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5066 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5067 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005068 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005069 }
5070}
5071
Bram Moolenaar1e015462005-09-25 22:16:38 +00005072#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005073/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005074 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5075 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005076 */
5077 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005078ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005079{
5080 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005081 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005082#ifdef FEAT_AUTOCMD
5083 char_u *au_name = NULL;
5084#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005085
Bram Moolenaardb552d602006-03-23 22:59:57 +00005086 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5087 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005088 {
5089 qi = ll_get_or_alloc_list(curwin);
5090 if (qi == NULL)
5091 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005092 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005093
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005094#ifdef FEAT_AUTOCMD
5095 switch (eap->cmdidx)
5096 {
5097 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5098 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5099 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5100 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5101 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5102 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5103 default: break;
5104 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005105 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5106 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005107 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005108# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005109 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005110 return;
5111# endif
5112 }
5113#endif
5114
Bram Moolenaar4770d092006-01-12 23:22:24 +00005115 /* Evaluate the expression. When the result is a string or a list we can
5116 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005117 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005118 if (tv != NULL)
5119 {
5120 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5121 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5122 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01005123 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005124 (eap->cmdidx != CMD_caddexpr
5125 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005126 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5127 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005128 {
5129#ifdef FEAT_AUTOCMD
5130 if (au_name != NULL)
5131 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5132 curbuf->b_fname, TRUE, curbuf);
5133#endif
5134 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5135 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5136 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005137 }
5138 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005139 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005140 free_tv(tv);
5141 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005142}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005143#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005144
5145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 * ":helpgrep {pattern}"
5147 */
5148 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005149ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150{
5151 regmatch_T regmatch;
5152 char_u *save_cpo;
5153 char_u *p;
5154 int fcount;
5155 char_u **fnames;
5156 FILE *fd;
5157 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005159#ifdef FEAT_MULTI_LANG
5160 char_u *lang;
5161#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005162 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005163 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005164 int new_qi = FALSE;
5165 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005166#ifdef FEAT_AUTOCMD
5167 char_u *au_name = NULL;
5168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005170#ifdef FEAT_MULTI_LANG
5171 /* Check for a specified language */
5172 lang = check_help_lang(eap->arg);
5173#endif
5174
Bram Moolenaar73633f82012-01-20 13:39:07 +01005175#ifdef FEAT_AUTOCMD
5176 switch (eap->cmdidx)
5177 {
5178 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5179 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5180 default: break;
5181 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005182 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5183 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005184 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005185# ifdef FEAT_EVAL
5186 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005187 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005188# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005189 }
5190#endif
5191
5192 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5193 save_cpo = p_cpo;
5194 p_cpo = empty_option;
5195
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005196 if (eap->cmdidx == CMD_lhelpgrep)
5197 {
5198 /* Find an existing help window */
5199 FOR_ALL_WINDOWS(wp)
5200 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5201 break;
5202
5203 if (wp == NULL) /* Help window not found */
5204 qi = NULL;
5205 else
5206 qi = wp->w_llist;
5207
5208 if (qi == NULL)
5209 {
5210 /* Allocate a new location list for help text matches */
5211 if ((qi = ll_new_list()) == NULL)
5212 return;
5213 new_qi = TRUE;
5214 }
5215 }
5216
Bram Moolenaaree85df32017-03-19 14:19:50 +01005217 /* Autocommands may change the list. Save it for later comparison */
5218 save_qi = qi;
5219
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5221 regmatch.rm_ic = FALSE;
5222 if (regmatch.regprog != NULL)
5223 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005224#ifdef FEAT_MBYTE
5225 vimconv_T vc;
5226
5227 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5228 * differs. */
5229 vc.vc_type = CONV_NONE;
5230 if (!enc_utf8)
5231 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5232#endif
5233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005235 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237 /* Go through all directories in 'runtimepath' */
5238 p = p_rtp;
5239 while (*p != NUL && !got_int)
5240 {
5241 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5242
5243 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5244 add_pathsep(NameBuff);
5245 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5246 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5247 &fnames, EW_FILE|EW_SILENT) == OK
5248 && fcount > 0)
5249 {
5250 for (fi = 0; fi < fcount && !got_int; ++fi)
5251 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005252#ifdef FEAT_MULTI_LANG
5253 /* Skip files for a different language. */
5254 if (lang != NULL
5255 && STRNICMP(lang, fnames[fi]
5256 + STRLEN(fnames[fi]) - 3, 2) != 0
5257 && !(STRNICMP(lang, "en", 2) == 0
5258 && STRNICMP("txt", fnames[fi]
5259 + STRLEN(fnames[fi]) - 3, 3) == 0))
5260 continue;
5261#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005262 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 if (fd != NULL)
5264 {
5265 lnum = 1;
5266 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5267 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005268 char_u *line = IObuff;
5269#ifdef FEAT_MBYTE
5270 /* Convert a line if 'encoding' is not utf-8 and
5271 * the line contains a non-ASCII character. */
5272 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005273 && has_non_ascii(IObuff))
5274 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005275 line = string_convert(&vc, IObuff, NULL);
5276 if (line == NULL)
5277 line = IObuff;
5278 }
5279#endif
5280
5281 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005283 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
5285 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005286 while (l > 0 && line[l - 1] <= ' ')
5287 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005289 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 NULL, /* dir */
5291 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005292 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005293 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005295 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005296 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005297 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005298 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 0, /* nr */
5300 1, /* type */
5301 TRUE /* valid */
5302 ) == FAIL)
5303 {
5304 got_int = TRUE;
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 break;
5310 }
5311 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005312#ifdef FEAT_MBYTE
5313 if (line != IObuff)
5314 vim_free(line);
5315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 ++lnum;
5317 line_breakcheck();
5318 }
5319 fclose(fd);
5320 }
5321 }
5322 FreeWild(fcount, fnames);
5323 }
5324 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005325
Bram Moolenaar473de612013-06-08 18:19:48 +02005326 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005327#ifdef FEAT_MBYTE
5328 if (vc.vc_type != CONV_NONE)
5329 convert_setup(&vc, NULL, NULL);
5330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005332 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5333 qi->qf_lists[qi->qf_curlist].qf_ptr =
5334 qi->qf_lists[qi->qf_curlist].qf_start;
5335 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005338 if (p_cpo == empty_option)
5339 p_cpo = save_cpo;
5340 else
5341 /* Darn, some plugin changed the value. */
5342 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005345 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346#endif
5347
Bram Moolenaar73633f82012-01-20 13:39:07 +01005348#ifdef FEAT_AUTOCMD
5349 if (au_name != NULL)
5350 {
5351 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5352 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005353 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005354 /* autocommands made "qi" invalid */
5355 return;
5356 }
5357#endif
5358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005360 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005361 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005362 else
5363 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005364
5365 if (eap->cmdidx == CMD_lhelpgrep)
5366 {
5367 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005368 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005369 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5370 {
5371 if (new_qi)
5372 ll_free_all(&qi);
5373 }
5374 else if (curwin->w_llist == NULL)
5375 curwin->w_llist = qi;
5376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377}
5378
5379#endif /* FEAT_QUICKFIX */