blob: 52abbebd1de3ea90925137efc878cba34908043a [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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static 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);
118static void qf_store_title(qf_info_T *qi, char_u *title);
119static void qf_new_list(qf_info_T *qi, char_u *qf_title);
120static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200121static 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 +0100122static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void qf_free(qf_info_T *qi, int idx);
124static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200125static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
126static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200128static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100129static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
130static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
133static int is_qf_win(win_T *win, qf_info_T *qi);
134static win_T *qf_find_win(qf_info_T *qi);
135static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200136static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200138static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static char_u *get_mef_name(void);
141static void restore_start_dir(char_u *dirname_start);
142static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
143static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
144static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
145static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000147/* Quickfix window check helper macro */
148#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
149/* Location list window check helper macro */
150#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
151/*
152 * Return location list for window 'wp'
153 * For location list window, return the referenced location list
154 */
155#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000158 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200159 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 * Return -1 for error, number of errors for success.
161 */
162 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100163qf_init(
164 win_T *wp,
165 char_u *efile,
166 char_u *errorformat,
167 int newlist, /* TRUE: start a new error list */
168 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000170 qf_info_T *qi = &ql_info;
171
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000173 {
174 qi = ll_get_or_alloc_list(wp);
175 if (qi == NULL)
176 return FAIL;
177 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000178
179 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200180 (linenr_T)0, (linenr_T)0,
181 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182}
183
184/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200185 * Maximum number of bytes allowed per line while reading a errorfile.
186 */
187#define LINE_MAXLEN 4096
188
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200189static struct fmtpattern
190{
191 char_u convchar;
192 char *pattern;
193} fmt_pat[FMT_PATTERNS] =
194 {
195 {'f', ".\\+"}, /* only used when at end */
196 {'n', "\\d\\+"},
197 {'l', "\\d\\+"},
198 {'c', "\\d\\+"},
199 {'t', "."},
200 {'m', ".\\+"},
201 {'r', ".*"},
202 {'p', "[- .]*"},
203 {'v', "\\d\\+"},
204 {'s', ".\\+"}
205 };
206
207/*
208 * Converts a 'errorformat' string to regular expression pattern
209 */
210 static int
211efm_to_regpat(
212 char_u *efm,
213 int len,
214 efm_T *fmt_ptr,
215 char_u *regpat,
216 char_u *errmsg)
217{
218 char_u *ptr;
219 char_u *efmp;
220 char_u *srcptr;
221 int round;
222 int idx = 0;
223
224 /*
225 * Build regexp pattern from current 'errorformat' option
226 */
227 ptr = regpat;
228 *ptr++ = '^';
229 round = 0;
230 for (efmp = efm; efmp < efm + len; ++efmp)
231 {
232 if (*efmp == '%')
233 {
234 ++efmp;
235 for (idx = 0; idx < FMT_PATTERNS; ++idx)
236 if (fmt_pat[idx].convchar == *efmp)
237 break;
238 if (idx < FMT_PATTERNS)
239 {
240 if (fmt_ptr->addr[idx])
241 {
242 sprintf((char *)errmsg,
243 _("E372: Too many %%%c in format string"), *efmp);
244 EMSG(errmsg);
245 return -1;
246 }
247 if ((idx
248 && idx < 6
249 && vim_strchr((char_u *)"DXOPQ",
250 fmt_ptr->prefix) != NULL)
251 || (idx == 6
252 && vim_strchr((char_u *)"OPQ",
253 fmt_ptr->prefix) == NULL))
254 {
255 sprintf((char *)errmsg,
256 _("E373: Unexpected %%%c in format string"), *efmp);
257 EMSG(errmsg);
258 return -1;
259 }
260 fmt_ptr->addr[idx] = (char_u)++round;
261 *ptr++ = '\\';
262 *ptr++ = '(';
263#ifdef BACKSLASH_IN_FILENAME
264 if (*efmp == 'f')
265 {
266 /* Also match "c:" in the file name, even when
267 * checking for a colon next: "%f:".
268 * "\%(\a:\)\=" */
269 STRCPY(ptr, "\\%(\\a:\\)\\=");
270 ptr += 10;
271 }
272#endif
273 if (*efmp == 'f' && efmp[1] != NUL)
274 {
275 if (efmp[1] != '\\' && efmp[1] != '%')
276 {
277 /* A file name may contain spaces, but this isn't
278 * in "\f". For "%f:%l:%m" there may be a ":" in
279 * the file name. Use ".\{-1,}x" instead (x is
280 * the next character), the requirement that :999:
281 * follows should work. */
282 STRCPY(ptr, ".\\{-1,}");
283 ptr += 7;
284 }
285 else
286 {
287 /* File name followed by '\\' or '%': include as
288 * many file name chars as possible. */
289 STRCPY(ptr, "\\f\\+");
290 ptr += 4;
291 }
292 }
293 else
294 {
295 srcptr = (char_u *)fmt_pat[idx].pattern;
296 while ((*ptr = *srcptr++) != NUL)
297 ++ptr;
298 }
299 *ptr++ = '\\';
300 *ptr++ = ')';
301 }
302 else if (*efmp == '*')
303 {
304 if (*++efmp == '[' || *efmp == '\\')
305 {
306 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
307 {
308 if (efmp[1] == '^')
309 *ptr++ = *++efmp;
310 if (efmp < efm + len)
311 {
312 *ptr++ = *++efmp; /* could be ']' */
313 while (efmp < efm + len
314 && (*ptr++ = *++efmp) != ']')
315 /* skip */;
316 if (efmp == efm + len)
317 {
318 EMSG(_("E374: Missing ] in format string"));
319 return -1;
320 }
321 }
322 }
323 else if (efmp < efm + len) /* %*\D, %*\s etc. */
324 *ptr++ = *++efmp;
325 *ptr++ = '\\';
326 *ptr++ = '+';
327 }
328 else
329 {
330 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
331 sprintf((char *)errmsg,
332 _("E375: Unsupported %%%c in format string"), *efmp);
333 EMSG(errmsg);
334 return -1;
335 }
336 }
337 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
338 *ptr++ = *efmp; /* regexp magic characters */
339 else if (*efmp == '#')
340 *ptr++ = '*';
341 else if (*efmp == '>')
342 fmt_ptr->conthere = TRUE;
343 else if (efmp == efm + 1) /* analyse prefix */
344 {
345 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
346 fmt_ptr->flags = *efmp++;
347 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
348 fmt_ptr->prefix = *efmp;
349 else
350 {
351 sprintf((char *)errmsg,
352 _("E376: Invalid %%%c in format string prefix"), *efmp);
353 EMSG(errmsg);
354 return -1;
355 }
356 }
357 else
358 {
359 sprintf((char *)errmsg,
360 _("E377: Invalid %%%c in format string"), *efmp);
361 EMSG(errmsg);
362 return -1;
363 }
364 }
365 else /* copy normal character */
366 {
367 if (*efmp == '\\' && efmp + 1 < efm + len)
368 ++efmp;
369 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
370 *ptr++ = '\\'; /* escape regexp atoms */
371 if (*efmp)
372 *ptr++ = *efmp;
373 }
374 }
375 *ptr++ = '$';
376 *ptr = NUL;
377
378 return 0;
379}
380
381 static void
382free_efm_list(efm_T **efm_first)
383{
384 efm_T *efm_ptr;
385
386 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
387 {
388 *efm_first = efm_ptr->next;
389 vim_regfree(efm_ptr->prog);
390 vim_free(efm_ptr);
391 }
392}
393
394/* Parse 'errorformat' option */
395 static efm_T *
396parse_efm_option(char_u *efm)
397{
398 char_u *errmsg = NULL;
399 int errmsglen;
400 efm_T *fmt_ptr = NULL;
401 efm_T *fmt_first = NULL;
402 efm_T *fmt_last = NULL;
403 char_u *fmtstr = NULL;
404 int len;
405 int i;
406 int round;
407
408 errmsglen = CMDBUFFSIZE + 1;
409 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
410 if (errmsg == NULL)
411 goto parse_efm_end;
412
413 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200414 * Each part of the format string is copied and modified from errorformat
415 * to regex prog. Only a few % characters are allowed.
416 */
417
418 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419 * Get some space to modify the format string into.
420 */
421 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
422 for (round = FMT_PATTERNS; round > 0; )
423 i += (int)STRLEN(fmt_pat[--round].pattern);
424#ifdef COLON_IN_FILENAME
425 i += 12; /* "%f" can become twelve chars longer */
426#else
427 i += 2; /* "%f" can become two chars longer */
428#endif
429 if ((fmtstr = alloc(i)) == NULL)
430 goto parse_efm_error;
431
432 while (efm[0] != NUL)
433 {
434 /*
435 * Allocate a new eformat structure and put it at the end of the list
436 */
437 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
438 if (fmt_ptr == NULL)
439 goto parse_efm_error;
440 if (fmt_first == NULL) /* first one */
441 fmt_first = fmt_ptr;
442 else
443 fmt_last->next = fmt_ptr;
444 fmt_last = fmt_ptr;
445
446 /*
447 * Isolate one part in the 'errorformat' option
448 */
449 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
450 if (efm[len] == '\\' && efm[len + 1] != NUL)
451 ++len;
452
453 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
454 goto parse_efm_error;
455 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
456 goto parse_efm_error;
457 /*
458 * Advance to next part
459 */
460 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
461 }
462
463 if (fmt_first == NULL) /* nothing found */
464 EMSG(_("E378: 'errorformat' contains no pattern"));
465
466 goto parse_efm_end;
467
468parse_efm_error:
469 free_efm_list(&fmt_first);
470
471parse_efm_end:
472 vim_free(fmtstr);
473 vim_free(errmsg);
474
475 return fmt_first;
476}
477
Bram Moolenaare0d37972016-07-15 22:36:01 +0200478enum {
479 QF_FAIL = 0,
480 QF_OK = 1,
481 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200482 QF_NOMEM = 3,
483 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200484};
485
486typedef struct {
487 char_u *linebuf;
488 int linelen;
489 char_u *growbuf;
490 int growbufsiz;
491 FILE *fd;
492 typval_T *tv;
493 char_u *p_str;
494 listitem_T *p_li;
495 buf_T *buf;
496 linenr_T buflnum;
497 linenr_T lnumlast;
498} qfstate_T;
499
500 static char_u *
501qf_grow_linebuf(qfstate_T *state, int newsz)
502{
503 /*
504 * If the line exceeds LINE_MAXLEN exclude the last
505 * byte since it's not a NL character.
506 */
507 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
508 if (state->growbuf == NULL)
509 {
510 state->growbuf = alloc(state->linelen + 1);
511 if (state->growbuf == NULL)
512 return NULL;
513 state->growbufsiz = state->linelen;
514 }
515 else if (state->linelen > state->growbufsiz)
516 {
517 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
518 if (state->growbuf == NULL)
519 return NULL;
520 state->growbufsiz = state->linelen;
521 }
522 return state->growbuf;
523}
524
525/*
526 * Get the next string (separated by newline) from state->p_str.
527 */
528 static int
529qf_get_next_str_line(qfstate_T *state)
530{
531 /* Get the next line from the supplied string */
532 char_u *p_str = state->p_str;
533 char_u *p;
534 int len;
535
536 if (*p_str == NUL) /* Reached the end of the string */
537 return QF_END_OF_INPUT;
538
539 p = vim_strchr(p_str, '\n');
540 if (p != NULL)
541 len = (int)(p - p_str) + 1;
542 else
543 len = (int)STRLEN(p_str);
544
545 if (len > IOSIZE - 2)
546 {
547 state->linebuf = qf_grow_linebuf(state, len);
548 if (state->linebuf == NULL)
549 return QF_NOMEM;
550 }
551 else
552 {
553 state->linebuf = IObuff;
554 state->linelen = len;
555 }
556 vim_strncpy(state->linebuf, p_str, state->linelen);
557
558 /*
559 * Increment using len in order to discard the rest of the
560 * line if it exceeds LINE_MAXLEN.
561 */
562 p_str += len;
563 state->p_str = p_str;
564
565 return QF_OK;
566}
567
568/*
569 * Get the next string from state->p_Li.
570 */
571 static int
572qf_get_next_list_line(qfstate_T *state)
573{
574 listitem_T *p_li = state->p_li;
575 int len;
576
577 while (p_li != NULL
578 && (p_li->li_tv.v_type != VAR_STRING
579 || p_li->li_tv.vval.v_string == NULL))
580 p_li = p_li->li_next; /* Skip non-string items */
581
582 if (p_li == NULL) /* End of the list */
583 {
584 state->p_li = NULL;
585 return QF_END_OF_INPUT;
586 }
587
588 len = (int)STRLEN(p_li->li_tv.vval.v_string);
589 if (len > IOSIZE - 2)
590 {
591 state->linebuf = qf_grow_linebuf(state, len);
592 if (state->linebuf == NULL)
593 return QF_NOMEM;
594 }
595 else
596 {
597 state->linebuf = IObuff;
598 state->linelen = len;
599 }
600
601 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
602
603 state->p_li = p_li->li_next; /* next item */
604 return QF_OK;
605}
606
607/*
608 * Get the next string from state->buf.
609 */
610 static int
611qf_get_next_buf_line(qfstate_T *state)
612{
613 char_u *p_buf = NULL;
614 int len;
615
616 /* Get the next line from the supplied buffer */
617 if (state->buflnum > state->lnumlast)
618 return QF_END_OF_INPUT;
619
620 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
621 state->buflnum += 1;
622
623 len = (int)STRLEN(p_buf);
624 if (len > IOSIZE - 2)
625 {
626 state->linebuf = qf_grow_linebuf(state, len);
627 if (state->linebuf == NULL)
628 return QF_NOMEM;
629 }
630 else
631 {
632 state->linebuf = IObuff;
633 state->linelen = len;
634 }
635 vim_strncpy(state->linebuf, p_buf, state->linelen);
636
637 return QF_OK;
638}
639
640/*
641 * Get the next string from file state->fd.
642 */
643 static int
644qf_get_next_file_line(qfstate_T *state)
645{
646 int discard;
647 int growbuflen;
648
649 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
650 return QF_END_OF_INPUT;
651
652 discard = FALSE;
653 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200654 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200655 {
656 /*
657 * The current line exceeds IObuff, continue reading using
658 * growbuf until EOL or LINE_MAXLEN bytes is read.
659 */
660 if (state->growbuf == NULL)
661 {
662 state->growbufsiz = 2 * (IOSIZE - 1);
663 state->growbuf = alloc(state->growbufsiz);
664 if (state->growbuf == NULL)
665 return QF_NOMEM;
666 }
667
668 /* Copy the read part of the line, excluding null-terminator */
669 memcpy(state->growbuf, IObuff, IOSIZE - 1);
670 growbuflen = state->linelen;
671
672 for (;;)
673 {
674 if (fgets((char *)state->growbuf + growbuflen,
675 state->growbufsiz - growbuflen, state->fd) == NULL)
676 break;
677 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
678 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200679 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200680 break;
681 if (state->growbufsiz == LINE_MAXLEN)
682 {
683 discard = TRUE;
684 break;
685 }
686
687 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
688 ? 2 * state->growbufsiz : LINE_MAXLEN;
689 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
690 if (state->growbuf == NULL)
691 return QF_NOMEM;
692 }
693
694 while (discard)
695 {
696 /*
697 * The current line is longer than LINE_MAXLEN, continue
698 * reading but discard everything until EOL or EOF is
699 * reached.
700 */
701 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
702 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200703 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200704 break;
705 }
706
707 state->linebuf = state->growbuf;
708 state->linelen = growbuflen;
709 }
710 else
711 state->linebuf = IObuff;
712
713 return QF_OK;
714}
715
716/*
717 * Get the next string from a file/buffer/list/string.
718 */
719 static int
720qf_get_nextline(qfstate_T *state)
721{
722 int status = QF_FAIL;
723
724 if (state->fd == NULL)
725 {
726 if (state->tv != NULL)
727 {
728 if (state->tv->v_type == VAR_STRING)
729 /* Get the next line from the supplied string */
730 status = qf_get_next_str_line(state);
731 else if (state->tv->v_type == VAR_LIST)
732 /* Get the next line from the supplied list */
733 status = qf_get_next_list_line(state);
734 }
735 else
736 /* Get the next line from the supplied buffer */
737 status = qf_get_next_buf_line(state);
738 }
739 else
740 /* Get the next line from the supplied file */
741 status = qf_get_next_file_line(state);
742
743 if (status != QF_OK)
744 return status;
745
746 /* remove newline/CR from the line */
747 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200748 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200749 state->linebuf[state->linelen - 1] = NUL;
750#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200751 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
752 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200753#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200754 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200755
756#ifdef FEAT_MBYTE
757 remove_bom(state->linebuf);
758#endif
759
760 return QF_OK;
761}
762
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200763typedef struct {
764 char_u *namebuf;
765 char_u *errmsg;
766 int errmsglen;
767 long lnum;
768 int col;
769 char_u use_viscol;
770 char_u *pattern;
771 int enr;
772 int type;
773 int valid;
774} qffields_T;
775
776/*
777 * Parse a line and get the quickfix fields.
778 * Return the QF_ status.
779 */
780 static int
781qf_parse_line(
782 qf_info_T *qi,
783 char_u *linebuf,
784 int linelen,
785 efm_T *fmt_first,
786 qffields_T *fields)
787{
788 efm_T *fmt_ptr;
789 static efm_T *fmt_start = NULL; /* cached across calls */
790 char_u *ptr;
791 int len;
792 int i;
793 int idx = 0;
794 char_u *tail = NULL;
795 regmatch_T regmatch;
796
797 /* Always ignore case when looking for a matching error. */
798 regmatch.rm_ic = TRUE;
799
800 /* If there was no %> item start at the first pattern */
801 if (fmt_start == NULL)
802 fmt_ptr = fmt_first;
803 else
804 {
805 fmt_ptr = fmt_start;
806 fmt_start = NULL;
807 }
808
809 /*
810 * Try to match each part of 'errorformat' until we find a complete
811 * match or no match.
812 */
813 fields->valid = TRUE;
814restofline:
815 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
816 {
817 int r;
818
819 idx = fmt_ptr->prefix;
820 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
821 continue;
822 fields->namebuf[0] = NUL;
823 fields->pattern[0] = NUL;
824 if (!qi->qf_multiscan)
825 fields->errmsg[0] = NUL;
826 fields->lnum = 0;
827 fields->col = 0;
828 fields->use_viscol = FALSE;
829 fields->enr = -1;
830 fields->type = 0;
831 tail = NULL;
832
833 regmatch.regprog = fmt_ptr->prog;
834 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
835 fmt_ptr->prog = regmatch.regprog;
836 if (r)
837 {
838 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
839 continue;
840 if (vim_strchr((char_u *)"EWI", idx) != NULL)
841 fields->type = idx;
842 else
843 fields->type = 0;
844 /*
845 * Extract error message data from matched line.
846 * We check for an actual submatch, because "\[" and "\]" in
847 * the 'errorformat' may cause the wrong submatch to be used.
848 */
849 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
850 {
851 int c;
852
853 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
854 continue;
855
856 /* Expand ~/file and $HOME/file to full path. */
857 c = *regmatch.endp[i];
858 *regmatch.endp[i] = NUL;
859 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
860 *regmatch.endp[i] = c;
861
862 if (vim_strchr((char_u *)"OPQ", idx) != NULL
863 && mch_getperm(fields->namebuf) == -1)
864 continue;
865 }
866 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
867 {
868 if (regmatch.startp[i] == NULL)
869 continue;
870 fields->enr = (int)atol((char *)regmatch.startp[i]);
871 }
872 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
873 {
874 if (regmatch.startp[i] == NULL)
875 continue;
876 fields->lnum = atol((char *)regmatch.startp[i]);
877 }
878 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
879 {
880 if (regmatch.startp[i] == NULL)
881 continue;
882 fields->col = (int)atol((char *)regmatch.startp[i]);
883 }
884 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
885 {
886 if (regmatch.startp[i] == NULL)
887 continue;
888 fields->type = *regmatch.startp[i];
889 }
890 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
891 {
892 if (linelen > fields->errmsglen) {
893 /* linelen + null terminator */
894 if ((fields->errmsg = vim_realloc(fields->errmsg,
895 linelen + 1)) == NULL)
896 return QF_NOMEM;
897 fields->errmsglen = linelen + 1;
898 }
899 vim_strncpy(fields->errmsg, linebuf, linelen);
900 }
901 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
902 {
903 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
904 continue;
905 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
906 if (len > fields->errmsglen) {
907 /* len + null terminator */
908 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
909 == NULL)
910 return QF_NOMEM;
911 fields->errmsglen = len + 1;
912 }
913 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
914 }
915 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
916 {
917 if (regmatch.startp[i] == NULL)
918 continue;
919 tail = regmatch.startp[i];
920 }
921 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
922 {
923 char_u *match_ptr;
924
925 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
926 continue;
927 fields->col = 0;
928 for (match_ptr = regmatch.startp[i];
929 match_ptr != regmatch.endp[i]; ++match_ptr)
930 {
931 ++fields->col;
932 if (*match_ptr == TAB)
933 {
934 fields->col += 7;
935 fields->col -= fields->col % 8;
936 }
937 }
938 ++fields->col;
939 fields->use_viscol = TRUE;
940 }
941 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
942 {
943 if (regmatch.startp[i] == NULL)
944 continue;
945 fields->col = (int)atol((char *)regmatch.startp[i]);
946 fields->use_viscol = TRUE;
947 }
948 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
949 {
950 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
951 continue;
952 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
953 if (len > CMDBUFFSIZE - 5)
954 len = CMDBUFFSIZE - 5;
955 STRCPY(fields->pattern, "^\\V");
956 STRNCAT(fields->pattern, regmatch.startp[i], len);
957 fields->pattern[len + 3] = '\\';
958 fields->pattern[len + 4] = '$';
959 fields->pattern[len + 5] = NUL;
960 }
961 break;
962 }
963 }
964 qi->qf_multiscan = FALSE;
965
966 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
967 {
968 if (fmt_ptr != NULL)
969 {
970 if (idx == 'D') /* enter directory */
971 {
972 if (*fields->namebuf == NUL)
973 {
974 EMSG(_("E379: Missing or empty directory name"));
975 return QF_FAIL;
976 }
977 qi->qf_directory =
978 qf_push_dir(fields->namebuf, &qi->qf_dir_stack, FALSE);
979 if (qi->qf_directory == NULL)
980 return QF_FAIL;
981 }
982 else if (idx == 'X') /* leave directory */
983 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
984 }
985 fields->namebuf[0] = NUL; /* no match found, remove file name */
986 fields->lnum = 0; /* don't jump to this line */
987 fields->valid = FALSE;
988 if (linelen > fields->errmsglen) {
989 /* linelen + null terminator */
990 if ((fields->errmsg = vim_realloc(fields->errmsg,
991 linelen + 1)) == NULL)
992 return QF_NOMEM;
993 fields->errmsglen = linelen + 1;
994 }
995 /* copy whole line to error message */
996 vim_strncpy(fields->errmsg, linebuf, linelen);
997 if (fmt_ptr == NULL)
998 qi->qf_multiline = qi->qf_multiignore = FALSE;
999 }
1000 else if (fmt_ptr != NULL)
1001 {
1002 /* honor %> item */
1003 if (fmt_ptr->conthere)
1004 fmt_start = fmt_ptr;
1005
1006 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1007 {
1008 qi->qf_multiline = TRUE; /* start of a multi-line message */
1009 qi->qf_multiignore = FALSE; /* reset continuation */
1010 }
1011 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1012 { /* continuation of multi-line msg */
1013 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
1014
1015 if (qfprev == NULL)
1016 return QF_FAIL;
1017 if (*fields->errmsg && !qi->qf_multiignore)
1018 {
1019 len = (int)STRLEN(qfprev->qf_text);
1020 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1021 == NULL)
1022 return QF_FAIL;
1023 STRCPY(ptr, qfprev->qf_text);
1024 vim_free(qfprev->qf_text);
1025 qfprev->qf_text = ptr;
1026 *(ptr += len) = '\n';
1027 STRCPY(++ptr, fields->errmsg);
1028 }
1029 if (qfprev->qf_nr == -1)
1030 qfprev->qf_nr = fields->enr;
1031 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1032 /* only printable chars allowed */
1033 qfprev->qf_type = fields->type;
1034
1035 if (!qfprev->qf_lnum)
1036 qfprev->qf_lnum = fields->lnum;
1037 if (!qfprev->qf_col)
1038 qfprev->qf_col = fields->col;
1039 qfprev->qf_viscol = fields->use_viscol;
1040 if (!qfprev->qf_fnum)
1041 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1042 *fields->namebuf || qi->qf_directory != NULL
1043 ? fields->namebuf
1044 : qi->qf_currfile != NULL && fields->valid
1045 ? qi->qf_currfile : 0);
1046 if (idx == 'Z')
1047 qi->qf_multiline = qi->qf_multiignore = FALSE;
1048 line_breakcheck();
1049 return QF_IGNORE_LINE;
1050 }
1051 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1052 {
1053 /* global file names */
1054 fields->valid = FALSE;
1055 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1056 {
1057 if (*fields->namebuf && idx == 'P')
1058 qi->qf_currfile =
1059 qf_push_dir(fields->namebuf, &qi->qf_file_stack, TRUE);
1060 else if (idx == 'Q')
1061 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
1062 *fields->namebuf = NUL;
1063 if (tail && *tail)
1064 {
1065 STRMOVE(IObuff, skipwhite(tail));
1066 qi->qf_multiscan = TRUE;
1067 goto restofline;
1068 }
1069 }
1070 }
1071 if (fmt_ptr->flags == '-') /* generally exclude this line */
1072 {
1073 if (qi->qf_multiline)
1074 /* also exclude continuation lines */
1075 qi->qf_multiignore = TRUE;
1076 return QF_IGNORE_LINE;
1077 }
1078 }
1079
1080 return QF_OK;
1081}
1082
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001083/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001084 * Read the errorfile "efile" into memory, line by line, building the error
1085 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001086 * Alternative: when "efile" is NULL read errors from buffer "buf".
1087 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001088 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001089 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1090 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001091 * Return -1 for error, number of errors for success.
1092 */
1093 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001094qf_init_ext(
1095 qf_info_T *qi,
1096 char_u *efile,
1097 buf_T *buf,
1098 typval_T *tv,
1099 char_u *errorformat,
1100 int newlist, /* TRUE: start a new error list */
1101 linenr_T lnumfirst, /* first line number to use */
1102 linenr_T lnumlast, /* last line number to use */
1103 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001104{
Bram Moolenaare0d37972016-07-15 22:36:01 +02001105 qfstate_T state = {NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001106 NULL, 0, 0};
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001107 qffields_T fields = {NULL, NULL, 0, 0L, 0, FALSE, NULL, 0, 0, 0};
Bram Moolenaar864293a2016-06-02 13:40:04 +02001108#ifdef FEAT_WINDOWS
1109 qfline_T *old_last = NULL;
1110#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001111 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001113 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001115 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001117 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1118 fields.errmsglen = CMDBUFFSIZE + 1;
1119 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1120 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1121 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1122 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 goto qf_init_end;
1124
Bram Moolenaare0d37972016-07-15 22:36:01 +02001125 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {
1127 EMSG2(_(e_openerrf), efile);
1128 goto qf_init_end;
1129 }
1130
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001131 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001133 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001134#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001135 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02001136 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001137 /* Adding to existing list, use last entry. */
1138 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001139 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001143 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001144 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 else
1146 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001148 /*
1149 * If we are not adding or adding to another list: clear the state.
1150 */
1151 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
1152 {
1153 qi->qf_dir_curlist = qi->qf_curlist;
1154 qf_clean_dir_stack(&qi->qf_dir_stack);
1155 qi->qf_directory = NULL;
1156 qf_clean_dir_stack(&qi->qf_file_stack);
1157 qi->qf_currfile = NULL;
1158 qi->qf_multiline = FALSE;
1159 qi->qf_multiignore = FALSE;
1160 qi->qf_multiscan = FALSE;
1161 }
1162
1163 /*
1164 * If the errorformat didn't change between calls, then reuse the
1165 * previously parsed values.
1166 */
1167 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1168 {
1169 /* free the previously parsed data */
1170 vim_free(last_efm);
1171 last_efm = NULL;
1172 free_efm_list(&fmt_first);
1173
1174 /* parse the current 'efm' */
1175 fmt_first = parse_efm_option(efm);
1176 if (fmt_first != NULL)
1177 last_efm = vim_strsave(efm);
1178 }
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
1183 /*
1184 * got_int is reset here, because it was probably set when killing the
1185 * ":make" command, but we still want to read the errorfile then.
1186 */
1187 got_int = FALSE;
1188
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001189 if (tv != NULL)
1190 {
1191 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001192 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001193 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001194 state.p_li = tv->vval.v_list->lv_first;
1195 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001196 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001197 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001198 state.buflnum = lnumfirst;
1199 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 /*
1202 * Read the lines in the error file one by one.
1203 * Try to recognize one of the error formats in each line.
1204 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001205 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001207 /* Get the next line from a file/buffer/list/string */
1208 status = qf_get_nextline(&state);
1209 if (status == QF_NOMEM) /* memory alloc failure */
1210 goto qf_init_end;
1211 if (status == QF_END_OF_INPUT) /* end of input */
1212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001214 status = qf_parse_line(qi, state.linebuf, state.linelen, fmt_first,
1215 &fields);
1216 if (status == QF_FAIL)
1217 goto error2;
1218 if (status == QF_NOMEM)
1219 goto qf_init_end;
1220 if (status == QF_IGNORE_LINE)
1221 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001223 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001224 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001225 (*fields.namebuf || qi->qf_directory != NULL)
1226 ? fields.namebuf
1227 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001228 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001229 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001230 fields.errmsg,
1231 fields.lnum,
1232 fields.col,
1233 fields.use_viscol,
1234 fields.pattern,
1235 fields.enr,
1236 fields.type,
1237 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 goto error2;
1239 line_breakcheck();
1240 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001241 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001243 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001245 /* no valid entry found */
1246 qi->qf_lists[qi->qf_curlist].qf_ptr =
1247 qi->qf_lists[qi->qf_curlist].qf_start;
1248 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1249 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251 else
1252 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001253 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1254 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1255 qi->qf_lists[qi->qf_curlist].qf_ptr =
1256 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001258 /* return number of matches */
1259 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001260 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262 EMSG(_(e_readerrf));
1263error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001264 qf_free(qi, qi->qf_curlist);
1265 qi->qf_listcount--;
1266 if (qi->qf_curlist > 0)
1267 --qi->qf_curlist;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001268qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001269 if (state.fd != NULL)
1270 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001271 vim_free(fields.namebuf);
1272 vim_free(fields.errmsg);
1273 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001274 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
1276#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001277 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278#endif
1279
1280 return retval;
1281}
1282
Bram Moolenaarfb604092014-07-23 15:55:00 +02001283 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001284qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001285{
1286 if (title != NULL)
1287 {
1288 char_u *p = alloc((int)STRLEN(title) + 2);
1289
1290 qi->qf_lists[qi->qf_curlist].qf_title = p;
1291 if (p != NULL)
1292 sprintf((char *)p, ":%s", (char *)title);
1293 }
1294}
1295
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296/*
1297 * Prepare for adding a new quickfix list.
1298 */
1299 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001300qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
1302 int i;
1303
1304 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001305 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 * the current entry. This makes it possible to browse in a tree-like
1307 * way with ":grep'.
1308 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001309 while (qi->qf_listcount > qi->qf_curlist + 1)
1310 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
1312 /*
1313 * When the stack is full, remove to oldest entry
1314 * Otherwise, add a new entry.
1315 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001316 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001318 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001320 qi->qf_lists[i - 1] = qi->qf_lists[i];
1321 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001324 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001325 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001326 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327}
1328
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001329/*
1330 * Free a location list
1331 */
1332 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001333ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001334{
1335 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001336 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001337
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001338 qi = *pqi;
1339 if (qi == NULL)
1340 return;
1341 *pqi = NULL; /* Remove reference to this list */
1342
1343 qi->qf_refcount--;
1344 if (qi->qf_refcount < 1)
1345 {
1346 /* No references to this location list */
1347 for (i = 0; i < qi->qf_listcount; ++i)
1348 qf_free(qi, i);
1349 vim_free(qi);
1350 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001351}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001352
1353 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001354qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001355{
1356 int i;
1357 qf_info_T *qi = &ql_info;
1358
1359 if (wp != NULL)
1360 {
1361 /* location list */
1362 ll_free_all(&wp->w_llist);
1363 ll_free_all(&wp->w_llist_ref);
1364 }
1365 else
1366 /* quickfix list */
1367 for (i = 0; i < qi->qf_listcount; ++i)
1368 qf_free(qi, i);
1369}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371/*
1372 * Add an entry to the end of the list of errors.
1373 * Returns OK or FAIL.
1374 */
1375 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001376qf_add_entry(
1377 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001378 char_u *dir, /* optional directory name */
1379 char_u *fname, /* file name or NULL */
1380 int bufnum, /* buffer number or zero */
1381 char_u *mesg, /* message */
1382 long lnum, /* line number */
1383 int col, /* column */
1384 int vis_col, /* using visual column */
1385 char_u *pattern, /* search pattern */
1386 int nr, /* error number */
1387 int type, /* type character */
1388 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001390 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001391 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001393 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001395 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001396 {
1397 buf_T *buf = buflist_findnr(bufnum);
1398
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001399 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001400 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001401 buf->b_has_qf_entry |=
1402 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001403 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001404 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001405 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1407 {
1408 vim_free(qfp);
1409 return FAIL;
1410 }
1411 qfp->qf_lnum = lnum;
1412 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001413 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001414 if (pattern == NULL || *pattern == NUL)
1415 qfp->qf_pattern = NULL;
1416 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1417 {
1418 vim_free(qfp->qf_text);
1419 vim_free(qfp);
1420 return FAIL;
1421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 qfp->qf_nr = nr;
1423 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1424 type = 0;
1425 qfp->qf_type = type;
1426 qfp->qf_valid = valid;
1427
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001428 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001429 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1430 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001432 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001433 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1434 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001435 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437 else
1438 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001439 qfp->qf_prev = *lastp;
1440 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001442 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001444 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001445 ++qi->qf_lists[qi->qf_curlist].qf_count;
1446 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1447 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001449 qi->qf_lists[qi->qf_curlist].qf_index =
1450 qi->qf_lists[qi->qf_curlist].qf_count;
1451 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453
1454 return OK;
1455}
1456
1457/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001458 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001459 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001460 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001461ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001462{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001463 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001464
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001465 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1466 if (qi != NULL)
1467 {
1468 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1469 qi->qf_refcount++;
1470 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001471
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001472 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001473}
1474
1475/*
1476 * Return the location list for window 'wp'.
1477 * If not present, allocate a location list
1478 */
1479 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001480ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001481{
1482 if (IS_LL_WINDOW(wp))
1483 /* For a location list window, use the referenced location list */
1484 return wp->w_llist_ref;
1485
1486 /*
1487 * For a non-location list window, w_llist_ref should not point to a
1488 * location list.
1489 */
1490 ll_free_all(&wp->w_llist_ref);
1491
1492 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001493 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001494 return wp->w_llist;
1495}
1496
1497/*
1498 * Copy the location list from window "from" to window "to".
1499 */
1500 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001501copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001502{
1503 qf_info_T *qi;
1504 int idx;
1505 int i;
1506
1507 /*
1508 * When copying from a location list window, copy the referenced
1509 * location list. For other windows, copy the location list for
1510 * that window.
1511 */
1512 if (IS_LL_WINDOW(from))
1513 qi = from->w_llist_ref;
1514 else
1515 qi = from->w_llist;
1516
1517 if (qi == NULL) /* no location list to copy */
1518 return;
1519
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001520 /* allocate a new location list */
1521 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522 return;
1523
1524 to->w_llist->qf_listcount = qi->qf_listcount;
1525
1526 /* Copy the location lists one at a time */
1527 for (idx = 0; idx < qi->qf_listcount; idx++)
1528 {
1529 qf_list_T *from_qfl;
1530 qf_list_T *to_qfl;
1531
1532 to->w_llist->qf_curlist = idx;
1533
1534 from_qfl = &qi->qf_lists[idx];
1535 to_qfl = &to->w_llist->qf_lists[idx];
1536
1537 /* Some of the fields are populated by qf_add_entry() */
1538 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1539 to_qfl->qf_count = 0;
1540 to_qfl->qf_index = 0;
1541 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001542 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001544 if (from_qfl->qf_title != NULL)
1545 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1546 else
1547 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548
1549 if (from_qfl->qf_count)
1550 {
1551 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001552 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001553
1554 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001555 for (i = 0, from_qfp = from_qfl->qf_start;
1556 i < from_qfl->qf_count && from_qfp != NULL;
1557 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001558 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001559 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001560 NULL,
1561 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001562 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001563 from_qfp->qf_text,
1564 from_qfp->qf_lnum,
1565 from_qfp->qf_col,
1566 from_qfp->qf_viscol,
1567 from_qfp->qf_pattern,
1568 from_qfp->qf_nr,
1569 0,
1570 from_qfp->qf_valid) == FAIL)
1571 {
1572 qf_free_all(to);
1573 return;
1574 }
1575 /*
1576 * qf_add_entry() will not set the qf_num field, as the
1577 * directory and file names are not supplied. So the qf_fnum
1578 * field is copied here.
1579 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001580 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001581 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1582 prevp->qf_type = from_qfp->qf_type; /* error type */
1583 if (from_qfl->qf_ptr == from_qfp)
1584 to_qfl->qf_ptr = prevp; /* current location */
1585 }
1586 }
1587
1588 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1589
1590 /* When no valid entries are present in the list, qf_ptr points to
1591 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001592 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001593 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001594 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001595 to_qfl->qf_index = 1;
1596 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001597 }
1598
1599 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1600}
1601
1602/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001603 * Looking up a buffer can be slow if there are many. Remember the last one
1604 * to make this a lot faster if there are multiple matches in the same file.
1605 */
1606static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001607static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001608
1609/*
Bram Moolenaar015102e2016-07-16 18:24:56 +02001610 * Get buffer number for file "directory.fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001611 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 */
1613 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001614qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
Bram Moolenaar82404332016-07-10 17:00:38 +02001616 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001617 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001618 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (fname == NULL || *fname == NUL) /* no file name */
1621 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaare60acc12011-05-10 16:41:25 +02001623#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001624 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001625#endif
1626#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001627 if (directory != NULL)
1628 slash_adjust(directory);
1629 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001630#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001631 if (directory != NULL && !vim_isAbsName(fname)
1632 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1633 {
1634 /*
1635 * Here we check if the file really exists.
1636 * This should normally be true, but if make works without
1637 * "leaving directory"-messages we might have missed a
1638 * directory change.
1639 */
1640 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001643 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001644 if (directory)
1645 ptr = concat_fnames(directory, fname, TRUE);
1646 else
1647 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001649 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001650 bufname = ptr;
1651 }
1652 else
1653 bufname = fname;
1654
1655 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001656 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001657 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001658 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001659 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001661 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001662 {
1663 vim_free(qf_last_bufname);
1664 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1665 if (bufname == ptr)
1666 qf_last_bufname = bufname;
1667 else
1668 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001669 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001670 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001671 if (buf == NULL)
1672 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001673
Bram Moolenaarc1542742016-07-20 21:44:37 +02001674 buf->b_has_qf_entry =
1675 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001676 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677}
1678
1679/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001680 * Push dirbuf onto the directory stack and return pointer to actual dir or
1681 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 */
1683 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001684qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 struct dir_stack_T *ds_new;
1687 struct dir_stack_T *ds_ptr;
1688
1689 /* allocate new stack element and hook it in */
1690 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1691 if (ds_new == NULL)
1692 return NULL;
1693
1694 ds_new->next = *stackptr;
1695 *stackptr = ds_new;
1696
1697 /* store directory on the stack */
1698 if (vim_isAbsName(dirbuf)
1699 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001700 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 (*stackptr)->dirname = vim_strsave(dirbuf);
1702 else
1703 {
1704 /* Okay we don't have an absolute path.
1705 * dirbuf must be a subdir of one of the directories on the stack.
1706 * Let's search...
1707 */
1708 ds_new = (*stackptr)->next;
1709 (*stackptr)->dirname = NULL;
1710 while (ds_new)
1711 {
1712 vim_free((*stackptr)->dirname);
1713 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1714 TRUE);
1715 if (mch_isdir((*stackptr)->dirname) == TRUE)
1716 break;
1717
1718 ds_new = ds_new->next;
1719 }
1720
1721 /* clean up all dirs we already left */
1722 while ((*stackptr)->next != ds_new)
1723 {
1724 ds_ptr = (*stackptr)->next;
1725 (*stackptr)->next = (*stackptr)->next->next;
1726 vim_free(ds_ptr->dirname);
1727 vim_free(ds_ptr);
1728 }
1729
1730 /* Nothing found -> it must be on top level */
1731 if (ds_new == NULL)
1732 {
1733 vim_free((*stackptr)->dirname);
1734 (*stackptr)->dirname = vim_strsave(dirbuf);
1735 }
1736 }
1737
1738 if ((*stackptr)->dirname != NULL)
1739 return (*stackptr)->dirname;
1740 else
1741 {
1742 ds_ptr = *stackptr;
1743 *stackptr = (*stackptr)->next;
1744 vim_free(ds_ptr);
1745 return NULL;
1746 }
1747}
1748
1749
1750/*
1751 * pop dirbuf from the directory stack and return previous directory or NULL if
1752 * stack is empty
1753 */
1754 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001755qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 struct dir_stack_T *ds_ptr;
1758
1759 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1760 * What to do if it isn't? */
1761
1762 /* pop top element and free it */
1763 if (*stackptr != NULL)
1764 {
1765 ds_ptr = *stackptr;
1766 *stackptr = (*stackptr)->next;
1767 vim_free(ds_ptr->dirname);
1768 vim_free(ds_ptr);
1769 }
1770
1771 /* return NEW top element as current dir or NULL if stack is empty*/
1772 return *stackptr ? (*stackptr)->dirname : NULL;
1773}
1774
1775/*
1776 * clean up directory stack
1777 */
1778 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001779qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 struct dir_stack_T *ds_ptr;
1782
1783 while ((ds_ptr = *stackptr) != NULL)
1784 {
1785 *stackptr = (*stackptr)->next;
1786 vim_free(ds_ptr->dirname);
1787 vim_free(ds_ptr);
1788 }
1789}
1790
1791/*
1792 * Check in which directory of the directory stack the given file can be
1793 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001794 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 * Cleans up intermediate directory entries.
1796 *
1797 * TODO: How to solve the following problem?
1798 * If we have the this directory tree:
1799 * ./
1800 * ./aa
1801 * ./aa/bb
1802 * ./bb
1803 * ./bb/x.c
1804 * and make says:
1805 * making all in aa
1806 * making all in bb
1807 * x.c:9: Error
1808 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1809 * qf_guess_filepath will return NULL.
1810 */
1811 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001812qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
1814 struct dir_stack_T *ds_ptr;
1815 struct dir_stack_T *ds_tmp;
1816 char_u *fullname;
1817
1818 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001819 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 return NULL;
1821
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001822 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 fullname = NULL;
1824 while (ds_ptr)
1825 {
1826 vim_free(fullname);
1827 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1828
1829 /* If concat_fnames failed, just go on. The worst thing that can happen
1830 * is that we delete the entire stack.
1831 */
1832 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1833 break;
1834
1835 ds_ptr = ds_ptr->next;
1836 }
1837
1838 vim_free(fullname);
1839
1840 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001841 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001843 ds_tmp = qi->qf_dir_stack->next;
1844 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 vim_free(ds_tmp->dirname);
1846 vim_free(ds_tmp);
1847 }
1848
1849 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850}
1851
1852/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001853 * When loading a file from the quickfix, the auto commands may modify it.
1854 * This may invalidate the current quickfix entry. This function checks
1855 * whether a entry is still present in the quickfix.
1856 * Similar to location list.
1857 */
1858 static int
1859is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1860{
1861 qf_list_T *qfl;
1862 qfline_T *qfp;
1863 int i;
1864
1865 qfl = &qi->qf_lists[qi->qf_curlist];
1866
1867 /* Search for the entry in the current list */
1868 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1869 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001870 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001871 break;
1872
1873 if (i == qfl->qf_count) /* Entry is not found */
1874 return FALSE;
1875
1876 return TRUE;
1877}
1878
1879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 * jump to a quickfix line
1881 * if dir == FORWARD go "errornr" valid entries forward
1882 * if dir == BACKWARD go "errornr" valid entries backward
1883 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1884 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1885 * else if "errornr" is zero, redisplay the same line
1886 * else go to entry "errornr"
1887 */
1888 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001889qf_jump(
1890 qf_info_T *qi,
1891 int dir,
1892 int errornr,
1893 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001895 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001896 qfline_T *qf_ptr;
1897 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 int qf_index;
1899 int old_qf_fnum;
1900 int old_qf_index;
1901 int prev_index;
1902 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1903 char_u *err = e_no_more_items;
1904 linenr_T i;
1905 buf_T *old_curbuf;
1906 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 colnr_T screen_col;
1908 colnr_T char_col;
1909 char_u *line;
1910#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001911 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001912 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int opened_window = FALSE;
1914 win_T *win;
1915 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001916 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001918 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 int print_message = TRUE;
1920 int len;
1921#ifdef FEAT_FOLDING
1922 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1923#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001924 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001925 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001927 if (qi == NULL)
1928 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001929
1930 if (qi->qf_curlist >= qi->qf_listcount
1931 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
1933 EMSG(_(e_quickfix));
1934 return;
1935 }
1936
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001937 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001939 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 old_qf_index = qf_index;
1941 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1942 {
1943 while (errornr--)
1944 {
1945 old_qf_ptr = qf_ptr;
1946 prev_index = qf_index;
1947 old_qf_fnum = qf_ptr->qf_fnum;
1948 do
1949 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001950 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 || qf_ptr->qf_next == NULL)
1952 {
1953 qf_ptr = old_qf_ptr;
1954 qf_index = prev_index;
1955 if (err != NULL)
1956 {
1957 EMSG(_(err));
1958 goto theend;
1959 }
1960 errornr = 0;
1961 break;
1962 }
1963 ++qf_index;
1964 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001965 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1966 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1968 err = NULL;
1969 }
1970 }
1971 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1972 {
1973 while (errornr--)
1974 {
1975 old_qf_ptr = qf_ptr;
1976 prev_index = qf_index;
1977 old_qf_fnum = qf_ptr->qf_fnum;
1978 do
1979 {
1980 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1981 {
1982 qf_ptr = old_qf_ptr;
1983 qf_index = prev_index;
1984 if (err != NULL)
1985 {
1986 EMSG(_(err));
1987 goto theend;
1988 }
1989 errornr = 0;
1990 break;
1991 }
1992 --qf_index;
1993 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001994 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1995 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1997 err = NULL;
1998 }
1999 }
2000 else if (errornr != 0) /* go to specified number */
2001 {
2002 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2003 {
2004 --qf_index;
2005 qf_ptr = qf_ptr->qf_prev;
2006 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002007 while (errornr > qf_index && qf_index <
2008 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 && qf_ptr->qf_next != NULL)
2010 {
2011 ++qf_index;
2012 qf_ptr = qf_ptr->qf_next;
2013 }
2014 }
2015
2016#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2018 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 /* No need to print the error message if it's visible in the error
2020 * window */
2021 print_message = FALSE;
2022
2023 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002024 * For ":helpgrep" find a help window or open one.
2025 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002026 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002027 {
2028 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002029
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002030 if (cmdmod.tab != 0)
2031 wp = NULL;
2032 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002033 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002034 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2035 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002036 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2037 win_enter(wp, TRUE);
2038 else
2039 {
2040 /*
2041 * Split off help window; put it at far top if no position
2042 * specified, the current window is vertically split and narrow.
2043 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002044 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002045 if (cmdmod.split == 0 && curwin->w_width != Columns
2046 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002047 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002048 if (qi != &ql_info)
2049 flags |= WSP_NEWLOC; /* don't copy the location list */
2050
2051 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002052 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002053 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002054
2055 if (curwin->w_height < p_hh)
2056 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002057
2058 if (qi != &ql_info) /* not a quickfix list */
2059 {
2060 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002061 curwin->w_llist = qi;
2062 qi->qf_refcount++;
2063 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002064 }
2065
2066 if (!p_im)
2067 restart_edit = 0; /* don't want insert mode in help file */
2068 }
2069
2070 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 * If currently in the quickfix window, find another window to show the
2072 * file in.
2073 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002074 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002076 win_T *usable_win_ptr = NULL;
2077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 /*
2079 * If there is no file specified, we don't know where to go.
2080 * But do advance, otherwise ":cn" gets stuck.
2081 */
2082 if (qf_ptr->qf_fnum == 0)
2083 goto theend;
2084
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002085 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002086
2087 ll_ref = curwin->w_llist_ref;
2088 if (ll_ref != NULL)
2089 {
2090 /* Find a window using the same location list that is not a
2091 * quickfix window. */
2092 FOR_ALL_WINDOWS(usable_win_ptr)
2093 if (usable_win_ptr->w_llist == ll_ref
2094 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002095 {
2096 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002097 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002098 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002099 }
2100
2101 if (!usable_win)
2102 {
2103 /* Locate a window showing a normal buffer */
2104 FOR_ALL_WINDOWS(win)
2105 if (win->w_buffer->b_p_bt[0] == NUL)
2106 {
2107 usable_win = 1;
2108 break;
2109 }
2110 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002113 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002114 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002116 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002117 {
2118 tabpage_T *tp;
2119 win_T *wp;
2120
2121 FOR_ALL_TAB_WINDOWS(tp, wp)
2122 {
2123 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2124 {
2125 goto_tabpage_win(tp, wp);
2126 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002127 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002128 }
2129 }
2130 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002131win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002132
2133 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002134 * If there is only one window and it is the quickfix window, create a
2135 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002136 */
2137 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002139 flags = WSP_ABOVE;
2140 if (ll_ref != NULL)
2141 flags |= WSP_NEWLOC;
2142 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 goto failed; /* not enough room for window */
2144 opened_window = TRUE; /* close it when fail */
2145 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002146 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002147 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002148 if (ll_ref != NULL)
2149 {
2150 /* The new window should use the location list from the
2151 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152 curwin->w_llist = ll_ref;
2153 ll_ref->qf_refcount++;
2154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 else
2157 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 if (curwin->w_llist_ref != NULL)
2159 {
2160 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002161 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002162 if (win == NULL)
2163 {
2164 /* Find the window showing the selected file */
2165 FOR_ALL_WINDOWS(win)
2166 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2167 break;
2168 if (win == NULL)
2169 {
2170 /* Find a previous usable window */
2171 win = curwin;
2172 do
2173 {
2174 if (win->w_buffer->b_p_bt[0] == NUL)
2175 break;
2176 if (win->w_prev == NULL)
2177 win = lastwin; /* wrap around the top */
2178 else
2179 win = win->w_prev; /* go to previous window */
2180 } while (win != curwin);
2181 }
2182 }
2183 win_goto(win);
2184
2185 /* If the location list for the window is not set, then set it
2186 * to the location list from the location window */
2187 if (win->w_llist == NULL)
2188 {
2189 win->w_llist = ll_ref;
2190 ll_ref->qf_refcount++;
2191 }
2192 }
2193 else
2194 {
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 /*
2197 * Try to find a window that shows the right buffer.
2198 * Default to the window just above the quickfix buffer.
2199 */
2200 win = curwin;
2201 altwin = NULL;
2202 for (;;)
2203 {
2204 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2205 break;
2206 if (win->w_prev == NULL)
2207 win = lastwin; /* wrap around the top */
2208 else
2209 win = win->w_prev; /* go to previous window */
2210
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002211 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 /* Didn't find it, go to the window before the quickfix
2214 * window. */
2215 if (altwin != NULL)
2216 win = altwin;
2217 else if (curwin->w_prev != NULL)
2218 win = curwin->w_prev;
2219 else
2220 win = curwin->w_next;
2221 break;
2222 }
2223
2224 /* Remember a usable window. */
2225 if (altwin == NULL && !win->w_p_pvw
2226 && win->w_buffer->b_p_bt[0] == NUL)
2227 altwin = win;
2228 }
2229
2230 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233 }
2234#endif
2235
2236 /*
2237 * If there is a file name,
2238 * read the wanted file if needed, and check autowrite etc.
2239 */
2240 old_curbuf = curbuf;
2241 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002242
2243 if (qf_ptr->qf_fnum != 0)
2244 {
2245 if (qf_ptr->qf_type == 1)
2246 {
2247 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2248 * set b_p_ro flag). */
2249 if (!can_abandon(curbuf, forceit))
2250 {
2251 EMSG(_(e_nowrtmsg));
2252 ok = FALSE;
2253 }
2254 else
2255 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002256 ECMD_HIDE + ECMD_SET_HELP,
2257 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002258 }
2259 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002260 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002261 int old_qf_curlist = qi->qf_curlist;
2262 int is_abort = FALSE;
2263
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002264 ok = buflist_getfile(qf_ptr->qf_fnum,
2265 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002266 if (qi != &ql_info && !win_valid(oldwin))
2267 {
2268 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002269 is_abort = TRUE;
2270 opened_window = FALSE;
2271 }
2272 else if (old_qf_curlist != qi->qf_curlist
2273 || !is_qf_entry_present(qi, qf_ptr))
2274 {
2275 if (qi == &ql_info)
2276 EMSG(_("E925: Current quickfix was changed"));
2277 else
2278 EMSG(_("E926: Current location list was changed"));
2279 is_abort = TRUE;
2280 }
2281
2282 if (is_abort)
2283 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002284 ok = FALSE;
2285 qi = NULL;
2286 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002287 }
2288 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002289 }
2290
2291 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 /* When not switched to another buffer, still need to set pc mark */
2294 if (curbuf == old_curbuf)
2295 setpcmark();
2296
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002297 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002299 /*
2300 * Go to line with error, unless qf_lnum is 0.
2301 */
2302 i = qf_ptr->qf_lnum;
2303 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002305 if (i > curbuf->b_ml.ml_line_count)
2306 i = curbuf->b_ml.ml_line_count;
2307 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002309 if (qf_ptr->qf_col > 0)
2310 {
2311 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002312#ifdef FEAT_VIRTUALEDIT
2313 curwin->w_cursor.coladd = 0;
2314#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002315 if (qf_ptr->qf_viscol == TRUE)
2316 {
2317 /*
2318 * Check each character from the beginning of the error
2319 * line up to the error column. For each tab character
2320 * found, reduce the error column value by the length of
2321 * a tab character.
2322 */
2323 line = ml_get_curline();
2324 screen_col = 0;
2325 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2326 {
2327 if (*line == NUL)
2328 break;
2329 if (*line++ == '\t')
2330 {
2331 curwin->w_cursor.col -= 7 - (screen_col % 8);
2332 screen_col += 8 - (screen_col % 8);
2333 }
2334 else
2335 ++screen_col;
2336 }
2337 }
2338 check_cursor();
2339 }
2340 else
2341 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
2343 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002344 {
2345 pos_T save_cursor;
2346
2347 /* Move the cursor to the first line in the buffer */
2348 save_cursor = curwin->w_cursor;
2349 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002350 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2351 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002352 curwin->w_cursor = save_cursor;
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355#ifdef FEAT_FOLDING
2356 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2357 foldOpenCursor();
2358#endif
2359 if (print_message)
2360 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002361 /* Update the screen before showing the message, unless the screen
2362 * scrolled up. */
2363 if (!msg_scrolled)
2364 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002366 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2368 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2369 /* Add the message, skipping leading whitespace and newlines. */
2370 len = (int)STRLEN(IObuff);
2371 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2372
2373 /* Output the message. Overwrite to avoid scrolling when the 'O'
2374 * flag is present in 'shortmess'; But when not jumping, print the
2375 * whole message. */
2376 i = msg_scroll;
2377 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2378 msg_scroll = TRUE;
2379 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2380 msg_scroll = FALSE;
2381 msg_attr_keep(IObuff, 0, TRUE);
2382 msg_scroll = i;
2383 }
2384 }
2385 else
2386 {
2387#ifdef FEAT_WINDOWS
2388 if (opened_window)
2389 win_close(curwin, TRUE); /* Close opened window */
2390#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002391 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
2393 /*
2394 * Couldn't open file, so put index back where it was. This could
2395 * happen if the file was readonly and we changed something.
2396 */
2397#ifdef FEAT_WINDOWS
2398failed:
2399#endif
2400 qf_ptr = old_qf_ptr;
2401 qf_index = old_qf_index;
2402 }
2403 }
2404theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002405 if (qi != NULL)
2406 {
2407 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2408 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410#ifdef FEAT_WINDOWS
2411 if (p_swb != old_swb && opened_window)
2412 {
2413 /* Restore old 'switchbuf' value, but not when an autocommand or
2414 * modeline has changed the value. */
2415 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002418 swb_flags = old_swb_flags;
2419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 else
2421 free_string_option(old_swb);
2422 }
2423#endif
2424}
2425
2426/*
2427 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002428 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 */
2430 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002431qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002433 buf_T *buf;
2434 char_u *fname;
2435 qfline_T *qfp;
2436 int i;
2437 int idx1 = 1;
2438 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002439 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002440 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002441 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002443 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002445 if (eap->cmdidx == CMD_llist)
2446 {
2447 qi = GET_LOC_LIST(curwin);
2448 if (qi == NULL)
2449 {
2450 EMSG(_(e_loclist));
2451 return;
2452 }
2453 }
2454
2455 if (qi->qf_curlist >= qi->qf_listcount
2456 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 EMSG(_(e_quickfix));
2459 return;
2460 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002461 if (*arg == '+')
2462 {
2463 ++arg;
2464 plus = TRUE;
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2467 {
2468 EMSG(_(e_trailing));
2469 return;
2470 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002471 if (plus)
2472 {
2473 i = qi->qf_lists[qi->qf_curlist].qf_index;
2474 idx2 = i + idx1;
2475 idx1 = i;
2476 }
2477 else
2478 {
2479 i = qi->qf_lists[qi->qf_curlist].qf_count;
2480 if (idx1 < 0)
2481 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2482 if (idx2 < 0)
2483 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002486 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002488 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2489 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2492 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002493 msg_putchar('\n');
2494 if (got_int)
2495 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002496
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002497 fname = NULL;
2498 if (qfp->qf_fnum != 0
2499 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2500 {
2501 fname = buf->b_fname;
2502 if (qfp->qf_type == 1) /* :helpgrep */
2503 fname = gettail(fname);
2504 }
2505 if (fname == NULL)
2506 sprintf((char *)IObuff, "%2d", i);
2507 else
2508 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2509 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002510 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002511 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2512 if (qfp->qf_lnum == 0)
2513 IObuff[0] = NUL;
2514 else if (qfp->qf_col == 0)
2515 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2516 else
2517 sprintf((char *)IObuff, ":%ld col %d",
2518 qfp->qf_lnum, qfp->qf_col);
2519 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2520 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2521 msg_puts_attr(IObuff, hl_attr(HLF_N));
2522 if (qfp->qf_pattern != NULL)
2523 {
2524 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2525 STRCAT(IObuff, ":");
2526 msg_puts(IObuff);
2527 }
2528 msg_puts((char_u *)" ");
2529
2530 /* Remove newlines and leading whitespace from the text. For an
2531 * unrecognized line keep the indent, the compiler may mark a word
2532 * with ^^^^. */
2533 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2535 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002536 msg_prt_line(IObuff, FALSE);
2537 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002539
2540 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002541 if (qfp == NULL)
2542 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002543 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 ui_breakcheck();
2545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546}
2547
2548/*
2549 * Remove newlines and leading whitespace from an error message.
2550 * Put the result in "buf[bufsize]".
2551 */
2552 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002553qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 int i;
2556 char_u *p = text;
2557
2558 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2559 {
2560 if (*p == '\n')
2561 {
2562 buf[i] = ' ';
2563 while (*++p != NUL)
2564 if (!vim_iswhite(*p) && *p != '\n')
2565 break;
2566 }
2567 else
2568 buf[i] = *p++;
2569 }
2570 buf[i] = NUL;
2571}
2572
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002573 static void
2574qf_msg(qf_info_T *qi, int which, char *lead)
2575{
2576 char *title = (char *)qi->qf_lists[which].qf_title;
2577 int count = qi->qf_lists[which].qf_count;
2578 char_u buf[IOSIZE];
2579
2580 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2581 lead,
2582 which + 1,
2583 qi->qf_listcount,
2584 count);
2585
2586 if (title != NULL)
2587 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002588 size_t len = STRLEN(buf);
2589
2590 if (len < 34)
2591 {
2592 vim_memset(buf + len, ' ', 34 - len);
2593 buf[34] = NUL;
2594 }
2595 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002596 }
2597 trunc_string(buf, buf, Columns - 1, IOSIZE);
2598 msg(buf);
2599}
2600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601/*
2602 * ":colder [count]": Up in the quickfix stack.
2603 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002604 * ":lolder [count]": Up in the location list stack.
2605 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 */
2607 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002608qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002610 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 int count;
2612
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002613 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2614 {
2615 qi = GET_LOC_LIST(curwin);
2616 if (qi == NULL)
2617 {
2618 EMSG(_(e_loclist));
2619 return;
2620 }
2621 }
2622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 if (eap->addr_count != 0)
2624 count = eap->line2;
2625 else
2626 count = 1;
2627 while (count--)
2628 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002629 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002631 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
2633 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002636 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
2638 else
2639 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002640 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
2642 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002643 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002645 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
2647 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002648 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002650 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651#endif
2652}
2653
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002654 void
2655qf_history(exarg_T *eap)
2656{
2657 qf_info_T *qi = &ql_info;
2658 int i;
2659
2660 if (eap->cmdidx == CMD_lhistory)
2661 qi = GET_LOC_LIST(curwin);
2662 if (qi == NULL || (qi->qf_listcount == 0
2663 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2664 MSG(_("No entries"));
2665 else
2666 for (i = 0; i < qi->qf_listcount; ++i)
2667 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2668}
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002671 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 */
2673 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002674qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002676 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002677 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002678 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002680 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002682 qfp = qi->qf_lists[idx].qf_start;
2683 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002684 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002685 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002686 vim_free(qfp->qf_text);
2687 stop = (qfp == qfpnext);
2688 vim_free(qfp->qf_pattern);
2689 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002690 if (stop)
2691 /* Somehow qf_count may have an incorrect value, set it to 1
2692 * to avoid crashing when it's wrong.
2693 * TODO: Avoid qf_count being incorrect. */
2694 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002695 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002696 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002697 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002699 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002700 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002701 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002702
2703 qf_clean_dir_stack(&qi->qf_dir_stack);
2704 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705}
2706
2707/*
2708 * qf_mark_adjust: adjust marks
2709 */
2710 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002711qf_mark_adjust(
2712 win_T *wp,
2713 linenr_T line1,
2714 linenr_T line2,
2715 long amount,
2716 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002718 int i;
2719 qfline_T *qfp;
2720 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002721 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002722 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002723 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaarc1542742016-07-20 21:44:37 +02002725 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002726 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002727 if (wp != NULL)
2728 {
2729 if (wp->w_llist == NULL)
2730 return;
2731 qi = wp->w_llist;
2732 }
2733
2734 for (idx = 0; idx < qi->qf_listcount; ++idx)
2735 if (qi->qf_lists[idx].qf_count)
2736 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002737 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2738 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 if (qfp->qf_fnum == curbuf->b_fnum)
2740 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002741 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2743 {
2744 if (amount == MAXLNUM)
2745 qfp->qf_cleared = TRUE;
2746 else
2747 qfp->qf_lnum += amount;
2748 }
2749 else if (amount_after && qfp->qf_lnum > line2)
2750 qfp->qf_lnum += amount_after;
2751 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002752
2753 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002754 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755}
2756
2757/*
2758 * Make a nice message out of the error character and the error number:
2759 * char number message
2760 * e or E 0 " error"
2761 * w or W 0 " warning"
2762 * i or I 0 " info"
2763 * 0 0 ""
2764 * other 0 " c"
2765 * e or E n " error n"
2766 * w or W n " warning n"
2767 * i or I n " info n"
2768 * 0 n " error n"
2769 * other n " c n"
2770 * 1 x "" :helpgrep
2771 */
2772 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002773qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 static char_u buf[20];
2776 static char_u cc[3];
2777 char_u *p;
2778
2779 if (c == 'W' || c == 'w')
2780 p = (char_u *)" warning";
2781 else if (c == 'I' || c == 'i')
2782 p = (char_u *)" info";
2783 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2784 p = (char_u *)" error";
2785 else if (c == 0 || c == 1)
2786 p = (char_u *)"";
2787 else
2788 {
2789 cc[0] = ' ';
2790 cc[1] = c;
2791 cc[2] = NUL;
2792 p = cc;
2793 }
2794
2795 if (nr <= 0)
2796 return p;
2797
2798 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2799 return buf;
2800}
2801
2802#if defined(FEAT_WINDOWS) || defined(PROTO)
2803/*
2804 * ":cwindow": open the quickfix window if we have errors to display,
2805 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002806 * ":lwindow": open the location list window if we have locations to display,
2807 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002810ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002812 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 win_T *win;
2814
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002815 if (eap->cmdidx == CMD_lwindow)
2816 {
2817 qi = GET_LOC_LIST(curwin);
2818 if (qi == NULL)
2819 return;
2820 }
2821
2822 /* Look for an existing quickfix window. */
2823 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824
2825 /*
2826 * If a quickfix window is open but we have no errors to display,
2827 * close the window. If a quickfix window is not open, then open
2828 * it if we have errors; otherwise, leave it closed.
2829 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002830 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002831 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002832 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 if (win != NULL)
2835 ex_cclose(eap);
2836 }
2837 else if (win == NULL)
2838 ex_copen(eap);
2839}
2840
2841/*
2842 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002843 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002846ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002848 win_T *win = NULL;
2849 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002851 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2852 {
2853 qi = GET_LOC_LIST(curwin);
2854 if (qi == NULL)
2855 return;
2856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002858 /* Find existing quickfix window and close it. */
2859 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (win != NULL)
2861 win_close(win, FALSE);
2862}
2863
2864/*
2865 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002866 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 */
2868 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002869ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002871 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002874 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002875 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002876 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002878 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2879 {
2880 qi = GET_LOC_LIST(curwin);
2881 if (qi == NULL)
2882 {
2883 EMSG(_(e_loclist));
2884 return;
2885 }
2886 }
2887
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (eap->addr_count != 0)
2889 height = eap->line2;
2890 else
2891 height = QF_WINHEIGHT;
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894#ifdef FEAT_GUI
2895 need_mouse_correct = TRUE;
2896#endif
2897
2898 /*
2899 * Find existing quickfix window, or open a new one.
2900 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002901 win = qf_find_win(qi);
2902
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002903 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002906 if (eap->addr_count != 0)
2907 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002908 if (cmdmod.split & WSP_VERT)
2909 {
2910 if (height != W_WIDTH(win))
2911 win_setwidth(height);
2912 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002913 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002914 win_setheight(height);
2915 }
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 else
2918 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002919 qf_buf = qf_find_buf(qi);
2920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 /* The current window becomes the previous window afterwards. */
2922 win = curwin;
2923
Bram Moolenaar77642c02012-11-20 17:55:10 +01002924 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2925 && cmdmod.split == 0)
2926 /* Create the new window at the very bottom, except when
2927 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002928 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002929 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002931 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002933 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002935 /*
2936 * For the location list window, create a reference to the
2937 * location list from the window 'win'.
2938 */
2939 curwin->w_llist_ref = win->w_llist;
2940 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002942
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002943 if (oldwin != curwin)
2944 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002945 if (qf_buf != NULL)
2946 /* Use the existing quickfix buffer */
2947 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002948 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002949 else
2950 {
2951 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002952 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002953 /* switch off 'swapfile' */
2954 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2955 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002956 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002957 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002958 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002959#ifdef FEAT_DIFF
2960 curwin->w_p_diff = FALSE;
2961#endif
2962#ifdef FEAT_FOLDING
2963 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2964 OPT_LOCAL);
2965#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002968 /* Only set the height when still in the same tab page and there is no
2969 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002970 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 win_setheight(height);
2972 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2973 if (win_valid(win))
2974 prevwin = win;
2975 }
2976
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002977 qf_set_title_var(qi);
2978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 /*
2980 * Fill the buffer with the quickfix list.
2981 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002982 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002984 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 curwin->w_cursor.col = 0;
2986 check_cursor();
2987 update_topline(); /* scroll to show the line */
2988}
2989
2990/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02002991 * Move the cursor in the quickfix window to "lnum".
2992 */
2993 static void
2994qf_win_goto(win_T *win, linenr_T lnum)
2995{
2996 win_T *old_curwin = curwin;
2997
2998 curwin = win;
2999 curbuf = win->w_buffer;
3000 curwin->w_cursor.lnum = lnum;
3001 curwin->w_cursor.col = 0;
3002#ifdef FEAT_VIRTUALEDIT
3003 curwin->w_cursor.coladd = 0;
3004#endif
3005 curwin->w_curswant = 0;
3006 update_topline(); /* scroll to show the line */
3007 redraw_later(VALID);
3008 curwin->w_redr_status = TRUE; /* update ruler */
3009 curwin = old_curwin;
3010 curbuf = curwin->w_buffer;
3011}
3012
3013/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003014 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003015 */
3016 void
3017ex_cbottom(exarg_T *eap UNUSED)
3018{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003019 qf_info_T *qi = &ql_info;
3020 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003021
Bram Moolenaar537ef082016-07-09 17:56:19 +02003022 if (eap->cmdidx == CMD_lbottom)
3023 {
3024 qi = GET_LOC_LIST(curwin);
3025 if (qi == NULL)
3026 {
3027 EMSG(_(e_loclist));
3028 return;
3029 }
3030 }
3031
3032 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003033 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3034 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3035}
3036
3037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 * Return the number of the current entry (line number in the quickfix
3039 * window).
3040 */
3041 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003042qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003044 qf_info_T *qi = &ql_info;
3045
3046 if (IS_LL_WINDOW(wp))
3047 /* In the location list window, use the referenced location list */
3048 qi = wp->w_llist_ref;
3049
3050 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051}
3052
3053/*
3054 * Update the cursor position in the quickfix window to the current error.
3055 * Return TRUE if there is a quickfix window.
3056 */
3057 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003058qf_win_pos_update(
3059 qf_info_T *qi,
3060 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003063 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 /*
3066 * Put the cursor on the current error in the quickfix window, so that
3067 * it's viewable.
3068 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003069 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if (win != NULL
3071 && qf_index <= win->w_buffer->b_ml.ml_line_count
3072 && old_qf_index != qf_index)
3073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 if (qf_index > old_qf_index)
3075 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003076 win->w_redraw_top = old_qf_index;
3077 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 }
3079 else
3080 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003081 win->w_redraw_top = qf_index;
3082 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003084 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
3086 return win != NULL;
3087}
3088
3089/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003090 * Check whether the given window is displaying the specified quickfix/location
3091 * list buffer
3092 */
3093 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003094is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003095{
3096 /*
3097 * A window displaying the quickfix buffer will have the w_llist_ref field
3098 * set to NULL.
3099 * A window displaying a location list buffer will have the w_llist_ref
3100 * pointing to the location list.
3101 */
3102 if (bt_quickfix(win->w_buffer))
3103 if ((qi == &ql_info && win->w_llist_ref == NULL)
3104 || (qi != &ql_info && win->w_llist_ref == qi))
3105 return TRUE;
3106
3107 return FALSE;
3108}
3109
3110/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003111 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003112 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003113 */
3114 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003115qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003116{
3117 win_T *win;
3118
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003119 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003120 if (is_qf_win(win, qi))
3121 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003122
3123 return win;
3124}
3125
3126/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003127 * Find a quickfix buffer.
3128 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 */
3130 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003131qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003133 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003134 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
Bram Moolenaar9c102382006-05-03 21:26:49 +00003136 FOR_ALL_TAB_WINDOWS(tp, win)
3137 if (is_qf_win(win, qi))
3138 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003139
Bram Moolenaar9c102382006-05-03 21:26:49 +00003140 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141}
3142
3143/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003144 * Update the w:quickfix_title variable in the quickfix/location list window
3145 */
3146 static void
3147qf_update_win_titlevar(qf_info_T *qi)
3148{
3149 win_T *win;
3150 win_T *curwin_save;
3151
3152 if ((win = qf_find_win(qi)) != NULL)
3153 {
3154 curwin_save = curwin;
3155 curwin = win;
3156 qf_set_title_var(qi);
3157 curwin = curwin_save;
3158 }
3159}
3160
3161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 * Find the quickfix buffer. If it exists, update the contents.
3163 */
3164 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003165qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
3167 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003168 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003172 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (buf != NULL)
3174 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003175 linenr_T old_line_count = buf->b_ml.ml_line_count;
3176
3177 if (old_last == NULL)
3178 /* set curwin/curbuf to buf and save a few things */
3179 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaard823fa92016-08-12 16:29:27 +02003181 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003182
Bram Moolenaar864293a2016-06-02 13:40:04 +02003183 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003184
Bram Moolenaar864293a2016-06-02 13:40:04 +02003185 if (old_last == NULL)
3186 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003187 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003188
3189 /* restore curwin/curbuf and a few other things */
3190 aucmd_restbuf(&aco);
3191 }
3192
3193 /* Only redraw when added lines are visible. This avoids flickering
3194 * when the added lines are not visible. */
3195 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3196 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 }
3198}
3199
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003200/*
3201 * Set "w:quickfix_title" if "qi" has a title.
3202 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003203 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003204qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003205{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003206 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3207 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003208 qi->qf_lists[qi->qf_curlist].qf_title);
3209}
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211/*
3212 * Fill current buffer with quickfix errors, replacing any previous contents.
3213 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003214 * If "old_last" is not NULL append the items after this one.
3215 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3216 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 */
3218 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003219qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003221 linenr_T lnum;
3222 qfline_T *qfp;
3223 buf_T *errbuf;
3224 int len;
3225 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226
Bram Moolenaar864293a2016-06-02 13:40:04 +02003227 if (old_last == NULL)
3228 {
3229 if (buf != curbuf)
3230 {
3231 EMSG2(_(e_intern2), "qf_fill_buffer()");
3232 return;
3233 }
3234
3235 /* delete all existing lines */
3236 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3237 (void)ml_delete((linenr_T)1, FALSE);
3238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003241 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 {
3243 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003244 if (old_last == NULL)
3245 {
3246 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3247 lnum = 0;
3248 }
3249 else
3250 {
3251 qfp = old_last->qf_next;
3252 lnum = buf->b_ml.ml_line_count;
3253 }
3254 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
3256 if (qfp->qf_fnum != 0
3257 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3258 && errbuf->b_fname != NULL)
3259 {
3260 if (qfp->qf_type == 1) /* :helpgrep */
3261 STRCPY(IObuff, gettail(errbuf->b_fname));
3262 else
3263 STRCPY(IObuff, errbuf->b_fname);
3264 len = (int)STRLEN(IObuff);
3265 }
3266 else
3267 len = 0;
3268 IObuff[len++] = '|';
3269
3270 if (qfp->qf_lnum > 0)
3271 {
3272 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3273 len += (int)STRLEN(IObuff + len);
3274
3275 if (qfp->qf_col > 0)
3276 {
3277 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3278 len += (int)STRLEN(IObuff + len);
3279 }
3280
3281 sprintf((char *)IObuff + len, "%s",
3282 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3283 len += (int)STRLEN(IObuff + len);
3284 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003285 else if (qfp->qf_pattern != NULL)
3286 {
3287 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3288 len += (int)STRLEN(IObuff + len);
3289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 IObuff[len++] = '|';
3291 IObuff[len++] = ' ';
3292
3293 /* Remove newlines and leading whitespace from the text.
3294 * For an unrecognized line keep the indent, the compiler may
3295 * mark a word with ^^^^. */
3296 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3297 IObuff + len, IOSIZE - len);
3298
Bram Moolenaar864293a2016-06-02 13:40:04 +02003299 if (ml_append_buf(buf, lnum, IObuff,
3300 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003302 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003304 if (qfp == NULL)
3305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003307
3308 if (old_last == NULL)
3309 /* Delete the empty line which is now at the end */
3310 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312
3313 /* correct cursor position */
3314 check_lnums(TRUE);
3315
Bram Moolenaar864293a2016-06-02 13:40:04 +02003316 if (old_last == NULL)
3317 {
3318 /* Set the 'filetype' to "qf" each time after filling the buffer.
3319 * This resembles reading a file into a buffer, it's more logical when
3320 * using autocommands. */
3321 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3322 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
3324#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003325 keep_filetype = TRUE; /* don't detect 'filetype' */
3326 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003328 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003330 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003332 /* make sure it will be redrawn */
3333 redraw_curbuf_later(NOT_VALID);
3334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336 /* Restore KeyTyped, setting 'filetype' may reset it. */
3337 KeyTyped = old_KeyTyped;
3338}
3339
3340#endif /* FEAT_WINDOWS */
3341
3342/*
3343 * Return TRUE if "buf" is the quickfix buffer.
3344 */
3345 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003346bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003348 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349}
3350
3351/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003352 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3353 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 */
3355 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003356bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003358 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3359 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360}
3361
3362/*
3363 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3364 */
3365 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003366bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003368 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369}
3370
3371 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003372bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 if (bt_dontwrite(buf))
3375 {
3376 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3377 return TRUE;
3378 }
3379 return FALSE;
3380}
3381
3382/*
3383 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3384 * and 'bufhidden'.
3385 */
3386 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003387buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3390 switch (buf->b_p_bh[0])
3391 {
3392 case 'u': /* "unload" */
3393 case 'w': /* "wipe" */
3394 case 'd': return FALSE; /* "delete" */
3395 case 'h': return TRUE; /* "hide" */
3396 }
3397 return (p_hid || cmdmod.hide);
3398}
3399
3400/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003401 * Return TRUE when using ":vimgrep" for ":grep".
3402 */
3403 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003404grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003405{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003406 return ((cmdidx == CMD_grep
3407 || cmdidx == CMD_lgrep
3408 || cmdidx == CMD_grepadd
3409 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003410 && STRCMP("internal",
3411 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3412}
3413
3414/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003415 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 */
3417 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003418ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003420 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 char_u *cmd;
3422 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003423 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003424 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003425 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003426#ifdef FEAT_AUTOCMD
3427 char_u *au_name = NULL;
3428
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003429 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3430 if (grep_internal(eap->cmdidx))
3431 {
3432 ex_vimgrep(eap);
3433 return;
3434 }
3435
Bram Moolenaar7c626922005-02-07 22:01:03 +00003436 switch (eap->cmdidx)
3437 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003438 case CMD_make: au_name = (char_u *)"make"; break;
3439 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3440 case CMD_grep: au_name = (char_u *)"grep"; break;
3441 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3442 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3443 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003444 default: break;
3445 }
3446 if (au_name != NULL)
3447 {
3448 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3449 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003450# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003451 if (did_throw || force_abort)
3452 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003453# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003454 }
3455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaara6557602006-02-04 22:43:20 +00003457 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3458 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003459 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003460
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003462 fname = get_mef_name();
3463 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003465 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467 /*
3468 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3469 */
3470 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3471 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003472 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 cmd = alloc(len);
3474 if (cmd == NULL)
3475 return;
3476 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3477 (char *)p_shq);
3478 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003479 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 /*
3481 * Output a newline if there's something else than the :make command that
3482 * was typed (in which case the cursor is in column 0).
3483 */
3484 if (msg_col == 0)
3485 msg_didout = FALSE;
3486 msg_start();
3487 MSG_PUTS(":!");
3488 msg_outtrans(cmd); /* show what we are doing */
3489
3490 /* let the shell know if we are redirecting output or not */
3491 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3492
3493#ifdef AMIGA
3494 out_flush();
3495 /* read window status report and redraw before message */
3496 (void)char_avail();
3497#endif
3498
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003499 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003500 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3501 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003502 && eap->cmdidx != CMD_lgrepadd),
3503 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003504 if (wp != NULL)
3505 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003506#ifdef FEAT_AUTOCMD
3507 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003508 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003509 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3510 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003511 if (qi->qf_curlist < qi->qf_listcount)
3512 res = qi->qf_lists[qi->qf_curlist].qf_count;
3513 else
3514 res = 0;
3515 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003516#endif
3517 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003518 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
Bram Moolenaar7c626922005-02-07 22:01:03 +00003520 mch_remove(fname);
3521 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 vim_free(cmd);
3523}
3524
3525/*
3526 * Return the name for the errorfile, in allocated memory.
3527 * Find a new unique name when 'makeef' contains "##".
3528 * Returns NULL for error.
3529 */
3530 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003531get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
3533 char_u *p;
3534 char_u *name;
3535 static int start = -1;
3536 static int off = 0;
3537#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003538 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539#endif
3540
3541 if (*p_mef == NUL)
3542 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003543 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (name == NULL)
3545 EMSG(_(e_notmp));
3546 return name;
3547 }
3548
3549 for (p = p_mef; *p; ++p)
3550 if (p[0] == '#' && p[1] == '#')
3551 break;
3552
3553 if (*p == NUL)
3554 return vim_strsave(p_mef);
3555
3556 /* Keep trying until the name doesn't exist yet. */
3557 for (;;)
3558 {
3559 if (start == -1)
3560 start = mch_get_pid();
3561 else
3562 off += 19;
3563
3564 name = alloc((unsigned)STRLEN(p_mef) + 30);
3565 if (name == NULL)
3566 break;
3567 STRCPY(name, p_mef);
3568 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3569 STRCAT(name, p + 2);
3570 if (mch_getperm(name) < 0
3571#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003572 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 && mch_lstat((char *)name, &sb) < 0
3574#endif
3575 )
3576 break;
3577 vim_free(name);
3578 }
3579 return name;
3580}
3581
3582/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003583 * Returns the number of valid entries in the current quickfix/location list.
3584 */
3585 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003586qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003587{
3588 qf_info_T *qi = &ql_info;
3589 qfline_T *qfp;
3590 int i, sz = 0;
3591 int prev_fnum = 0;
3592
3593 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3594 {
3595 /* Location list */
3596 qi = GET_LOC_LIST(curwin);
3597 if (qi == NULL)
3598 return 0;
3599 }
3600
3601 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003602 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003603 ++i, qfp = qfp->qf_next)
3604 {
3605 if (qfp->qf_valid)
3606 {
3607 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3608 sz++; /* Count all valid entries */
3609 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3610 {
3611 /* Count the number of files */
3612 sz++;
3613 prev_fnum = qfp->qf_fnum;
3614 }
3615 }
3616 }
3617
3618 return sz;
3619}
3620
3621/*
3622 * Returns the current index of the quickfix/location list.
3623 * Returns 0 if there is an error.
3624 */
3625 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003626qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003627{
3628 qf_info_T *qi = &ql_info;
3629
3630 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3631 {
3632 /* Location list */
3633 qi = GET_LOC_LIST(curwin);
3634 if (qi == NULL)
3635 return 0;
3636 }
3637
3638 return qi->qf_lists[qi->qf_curlist].qf_index;
3639}
3640
3641/*
3642 * Returns the current index in the quickfix/location list (counting only valid
3643 * entries). If no valid entries are in the list, then returns 1.
3644 */
3645 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003646qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003647{
3648 qf_info_T *qi = &ql_info;
3649 qf_list_T *qfl;
3650 qfline_T *qfp;
3651 int i, eidx = 0;
3652 int prev_fnum = 0;
3653
3654 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3655 {
3656 /* Location list */
3657 qi = GET_LOC_LIST(curwin);
3658 if (qi == NULL)
3659 return 1;
3660 }
3661
3662 qfl = &qi->qf_lists[qi->qf_curlist];
3663 qfp = qfl->qf_start;
3664
3665 /* check if the list has valid errors */
3666 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3667 return 1;
3668
3669 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3670 {
3671 if (qfp->qf_valid)
3672 {
3673 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3674 {
3675 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3676 {
3677 /* Count the number of files */
3678 eidx++;
3679 prev_fnum = qfp->qf_fnum;
3680 }
3681 }
3682 else
3683 eidx++;
3684 }
3685 }
3686
3687 return eidx ? eidx : 1;
3688}
3689
3690/*
3691 * Get the 'n'th valid error entry in the quickfix or location list.
3692 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3693 * For :cdo and :ldo returns the 'n'th valid error entry.
3694 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3695 */
3696 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003697qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003698{
3699 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3700 qfline_T *qfp = qfl->qf_start;
3701 int i, eidx;
3702 int prev_fnum = 0;
3703
3704 /* check if the list has valid errors */
3705 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3706 return 1;
3707
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003708 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003709 i++, qfp = qfp->qf_next)
3710 {
3711 if (qfp->qf_valid)
3712 {
3713 if (fdo)
3714 {
3715 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3716 {
3717 /* Count the number of files */
3718 eidx++;
3719 prev_fnum = qfp->qf_fnum;
3720 }
3721 }
3722 else
3723 eidx++;
3724 }
3725
3726 if (eidx == n)
3727 break;
3728 }
3729
3730 if (i <= qfl->qf_count)
3731 return i;
3732 else
3733 return 1;
3734}
3735
3736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003738 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003739 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 */
3741 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003742ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003744 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003745 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003746
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003747 if (eap->cmdidx == CMD_ll
3748 || eap->cmdidx == CMD_lrewind
3749 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003750 || eap->cmdidx == CMD_llast
3751 || eap->cmdidx == CMD_ldo
3752 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003753 {
3754 qi = GET_LOC_LIST(curwin);
3755 if (qi == NULL)
3756 {
3757 EMSG(_(e_loclist));
3758 return;
3759 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003760 }
3761
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003762 if (eap->addr_count > 0)
3763 errornr = (int)eap->line2;
3764 else
3765 {
3766 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3767 errornr = 0;
3768 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3769 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3770 errornr = 1;
3771 else
3772 errornr = 32767;
3773 }
3774
3775 /* For cdo and ldo commands, jump to the nth valid error.
3776 * For cfdo and lfdo commands, jump to the nth valid file entry.
3777 */
3778 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3779 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3780 errornr = qf_get_nth_valid_entry(qi,
3781 eap->addr_count > 0 ? (int)eap->line1 : 1,
3782 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3783
3784 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785}
3786
3787/*
3788 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003789 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003790 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 */
3792 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003793ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003795 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003796 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003797
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003798 if (eap->cmdidx == CMD_lnext
3799 || eap->cmdidx == CMD_lNext
3800 || eap->cmdidx == CMD_lprevious
3801 || eap->cmdidx == CMD_lnfile
3802 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003803 || eap->cmdidx == CMD_lpfile
3804 || eap->cmdidx == CMD_ldo
3805 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003806 {
3807 qi = GET_LOC_LIST(curwin);
3808 if (qi == NULL)
3809 {
3810 EMSG(_(e_loclist));
3811 return;
3812 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003813 }
3814
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003815 if (eap->addr_count > 0 &&
3816 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3817 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3818 errornr = (int)eap->line2;
3819 else
3820 errornr = 1;
3821
3822 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3823 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003825 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3826 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003828 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3829 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 ? BACKWARD_FILE
3831 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003832 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833}
3834
3835/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003836 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003837 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 */
3839 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003840ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003842 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003843 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003844#ifdef FEAT_AUTOCMD
3845 char_u *au_name = NULL;
3846#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003847
3848 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003849 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003850 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003851
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003852#ifdef FEAT_AUTOCMD
3853 switch (eap->cmdidx)
3854 {
3855 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3856 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3857 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3858 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3859 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3860 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3861 default: break;
3862 }
3863 if (au_name != NULL)
3864 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3865#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003866#ifdef FEAT_BROWSE
3867 if (cmdmod.browse)
3868 {
3869 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3870 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3871 if (browse_file == NULL)
3872 return;
3873 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3874 vim_free(browse_file);
3875 }
3876 else
3877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003879 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003880
3881 /*
3882 * This function is used by the :cfile, :cgetfile and :caddfile
3883 * commands.
3884 * :cfile always creates a new quickfix list and jumps to the
3885 * first error.
3886 * :cgetfile creates a new quickfix list but doesn't jump to the
3887 * first error.
3888 * :caddfile adds to an existing quickfix list. If there is no
3889 * quickfix list then a new list is created.
3890 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003891 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003892 && eap->cmdidx != CMD_laddfile),
3893 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003894 && (eap->cmdidx == CMD_cfile
3895 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003896 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003897#ifdef FEAT_AUTOCMD
3898 if (au_name != NULL)
3899 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3900#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003901 if (wp != NULL)
3902 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003903 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003904 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003905
3906 else
3907 {
3908#ifdef FEAT_AUTOCMD
3909 if (au_name != NULL)
3910 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3911#endif
3912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913}
3914
3915/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003916 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003917 * ":vimgrepadd {pattern} file(s)"
3918 * ":lvimgrep {pattern} file(s)"
3919 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003920 */
3921 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003922ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003923{
Bram Moolenaar81695252004-12-29 20:58:21 +00003924 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003925 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003926 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003927 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003928 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003929 char_u *s;
3930 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003931 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003932 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003933#ifdef FEAT_AUTOCMD
3934 qfline_T *cur_qf_start;
3935#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003936 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003937 buf_T *buf;
3938 int duplicate_name = FALSE;
3939 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003940 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003941 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003942 buf_T *first_match_buf = NULL;
3943 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003944 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003945#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3946 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003947#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003948 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003949 int flags = 0;
3950 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003951 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003952 char_u *dirname_start = NULL;
3953 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003954 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003955#ifdef FEAT_AUTOCMD
3956 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003957
3958 switch (eap->cmdidx)
3959 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003960 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3961 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3962 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003963 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003964 case CMD_grep: au_name = (char_u *)"grep"; break;
3965 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3966 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3967 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003968 default: break;
3969 }
3970 if (au_name != NULL)
3971 {
3972 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3973 curbuf->b_fname, TRUE, curbuf);
3974 if (did_throw || force_abort)
3975 return;
3976 }
3977#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003978
Bram Moolenaar754b5602006-02-09 23:53:20 +00003979 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003980 || eap->cmdidx == CMD_lvimgrep
3981 || eap->cmdidx == CMD_lgrepadd
3982 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003983 {
3984 qi = ll_get_or_alloc_list(curwin);
3985 if (qi == NULL)
3986 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003987 }
3988
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003989 if (eap->addr_count > 0)
3990 tomatch = eap->line2;
3991 else
3992 tomatch = MAXLNUM;
3993
Bram Moolenaar81695252004-12-29 20:58:21 +00003994 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003995 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003996 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003997 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003998 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003999 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004000 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004001 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004002 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004003
4004 if (s != NULL && *s == NUL)
4005 {
4006 /* Pattern is empty, use last search pattern. */
4007 if (last_search_pat() == NULL)
4008 {
4009 EMSG(_(e_noprevre));
4010 goto theend;
4011 }
4012 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4013 }
4014 else
4015 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4016
Bram Moolenaar86b68352004-12-27 21:59:20 +00004017 if (regmatch.regprog == NULL)
4018 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004019 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004020 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004021
4022 p = skipwhite(p);
4023 if (*p == NUL)
4024 {
4025 EMSG(_("E683: File name missing or invalid pattern"));
4026 goto theend;
4027 }
4028
Bram Moolenaar754b5602006-02-09 23:53:20 +00004029 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004030 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004031 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004032 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004033 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004034
4035 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004036 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004037 goto theend;
4038 if (fcount == 0)
4039 {
4040 EMSG(_(e_nomatch));
4041 goto theend;
4042 }
4043
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004044 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4045 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004046 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004047 {
4048 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004049 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004050 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004051
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004052 /* Remember the current directory, because a BufRead autocommand that does
4053 * ":lcd %:p:h" changes the meaning of short path names. */
4054 mch_dirname(dirname_start, MAXPATHL);
4055
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004056#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004057 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004058 * changing the current quickfix list. */
4059 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4060#endif
4061
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004062 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004063 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004064 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004065 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004066 if (time(NULL) > seconds)
4067 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004068 /* Display the file name every second or so, show the user we are
4069 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004070 seconds = time(NULL);
4071 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004072 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004073 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004074 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004075 else
4076 {
4077 msg_outtrans(p);
4078 vim_free(p);
4079 }
4080 msg_clr_eos();
4081 msg_didout = FALSE; /* overwrite this message */
4082 msg_nowait = TRUE; /* don't wait for this message */
4083 msg_col = 0;
4084 out_flush();
4085 }
4086
Bram Moolenaar81695252004-12-29 20:58:21 +00004087 buf = buflist_findname_exp(fnames[fi]);
4088 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4089 {
4090 /* Remember that a buffer with this name already exists. */
4091 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004092 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004093 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004094
4095#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4096 /* Don't do Filetype autocommands to avoid loading syntax and
4097 * indent scripts, a great speed improvement. */
4098 save_ei = au_event_disable(",Filetype");
4099#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004100 /* Don't use modelines here, it's useless. */
4101 save_mls = p_mls;
4102 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004103
4104 /* Load file into a buffer, so that 'fileencoding' is detected,
4105 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004106 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004107
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004108 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004109#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4110 au_event_restore(save_ei);
4111#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004112 }
4113 else
4114 /* Use existing, loaded buffer. */
4115 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004116
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004117#ifdef FEAT_AUTOCMD
4118 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4119 {
4120 int idx;
4121
4122 /* Autocommands changed the quickfix list. Find the one we were
4123 * using and restore it. */
4124 for (idx = 0; idx < LISTCOUNT; ++idx)
4125 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4126 {
4127 qi->qf_curlist = idx;
4128 break;
4129 }
4130 if (idx == LISTCOUNT)
4131 {
4132 /* List cannot be found, create a new one. */
4133 qf_new_list(qi, *eap->cmdlinep);
4134 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4135 }
4136 }
4137#endif
4138
Bram Moolenaar81695252004-12-29 20:58:21 +00004139 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004140 {
4141 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004142 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004143 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004144 else
4145 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004146 /* Try for a match in all lines of the buffer.
4147 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004148 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004149 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4150 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004151 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004152 col = 0;
4153 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004154 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004155 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004156 /* Pass the buffer number so that it gets used even for a
4157 * dummy buffer, unless duplicate_name is set, then the
4158 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004159 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004160 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004161 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004162 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004163 ml_get_buf(buf,
4164 regmatch.startpos[0].lnum + lnum, FALSE),
4165 regmatch.startpos[0].lnum + lnum,
4166 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004167 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004168 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004169 0, /* nr */
4170 0, /* type */
4171 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004172 ) == FAIL)
4173 {
4174 got_int = TRUE;
4175 break;
4176 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004177 found_match = TRUE;
4178 if (--tomatch == 0)
4179 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004180 if ((flags & VGR_GLOBAL) == 0
4181 || regmatch.endpos[0].lnum > 0)
4182 break;
4183 col = regmatch.endpos[0].col
4184 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004185 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004186 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004187 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004188 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004189 if (got_int)
4190 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004191 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004192#ifdef FEAT_AUTOCMD
4193 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4194#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004195
4196 if (using_dummy)
4197 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004198 if (found_match && first_match_buf == NULL)
4199 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004200 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004201 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004202 /* Never keep a dummy buffer if there is another buffer
4203 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004204 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004205 buf = NULL;
4206 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004207 else if (!cmdmod.hide
4208 || buf->b_p_bh[0] == 'u' /* "unload" */
4209 || buf->b_p_bh[0] == 'w' /* "wipe" */
4210 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004211 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004212 /* When no match was found we don't need to remember the
4213 * buffer, wipe it out. If there was a match and it
4214 * wasn't the first one or we won't jump there: only
4215 * unload the buffer.
4216 * Ignore 'hidden' here, because it may lead to having too
4217 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004218 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004219 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004220 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004221 buf = NULL;
4222 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004223 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004224 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004225 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004226 /* Keeping the buffer, remove the dummy flag. */
4227 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004228 buf = NULL;
4229 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004230 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004231
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004232 if (buf != NULL)
4233 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004234 /* Keeping the buffer, remove the dummy flag. */
4235 buf->b_flags &= ~BF_DUMMY;
4236
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004237 /* If the buffer is still loaded we need to use the
4238 * directory we jumped to below. */
4239 if (buf == first_match_buf
4240 && target_dir == NULL
4241 && STRCMP(dirname_start, dirname_now) != 0)
4242 target_dir = vim_strsave(dirname_now);
4243
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004244 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004245 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004246 * need to be done (again). But not the window-local
4247 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004248 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004249#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004250 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4251 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004252#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004253 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004254 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004255 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004256 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004257 }
4258 }
4259
4260 FreeWild(fcount, fnames);
4261
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004262 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4263 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4264 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004265
4266#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004267 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004268#endif
4269
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004270#ifdef FEAT_AUTOCMD
4271 if (au_name != NULL)
4272 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4273 curbuf->b_fname, TRUE, curbuf);
4274#endif
4275
Bram Moolenaar86b68352004-12-27 21:59:20 +00004276 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004277 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004278 {
4279 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004280 {
4281 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004282 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004283 if (buf != curbuf)
4284 /* If we jumped to another buffer redrawing will already be
4285 * taken care of. */
4286 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004287
4288 /* Jump to the directory used after loading the buffer. */
4289 if (curbuf == first_match_buf && target_dir != NULL)
4290 {
4291 exarg_T ea;
4292
4293 ea.arg = target_dir;
4294 ea.cmdidx = CMD_lcd;
4295 ex_cd(&ea);
4296 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004297 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004298 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004299 else
4300 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004301
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004302 /* If we loaded a dummy buffer into the current window, the autocommands
4303 * may have messed up things, need to redraw and recompute folds. */
4304 if (redraw_for_dummy)
4305 {
4306#ifdef FEAT_FOLDING
4307 foldUpdateAll(curwin);
4308#else
4309 redraw_later(NOT_VALID);
4310#endif
4311 }
4312
Bram Moolenaar86b68352004-12-27 21:59:20 +00004313theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004314 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004315 vim_free(dirname_now);
4316 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004317 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004318 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004319}
4320
4321/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004322 * Restore current working directory to "dirname_start" if they differ, taking
4323 * into account whether it is set locally or globally.
4324 */
4325 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004326restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004327{
4328 char_u *dirname_now = alloc(MAXPATHL);
4329
4330 if (NULL != dirname_now)
4331 {
4332 mch_dirname(dirname_now, MAXPATHL);
4333 if (STRCMP(dirname_start, dirname_now) != 0)
4334 {
4335 /* If the directory has changed, change it back by building up an
4336 * appropriate ex command and executing it. */
4337 exarg_T ea;
4338
4339 ea.arg = dirname_start;
4340 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4341 ex_cd(&ea);
4342 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004343 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004344 }
4345}
4346
4347/*
4348 * Load file "fname" into a dummy buffer and return the buffer pointer,
4349 * placing the directory resulting from the buffer load into the
4350 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4351 * prior to calling this function. Restores directory to "dirname_start" prior
4352 * to returning, if autocmds or the 'autochdir' option have changed it.
4353 *
4354 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4355 * or wipe_dummy_buffer() later!
4356 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004357 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004358 */
4359 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004360load_dummy_buffer(
4361 char_u *fname,
4362 char_u *dirname_start, /* in: old directory */
4363 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004364{
4365 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004366 bufref_T newbufref;
4367 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004368 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004369 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004370
4371 /* Allocate a buffer without putting it in the buffer list. */
4372 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4373 if (newbuf == NULL)
4374 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004375 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004376
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004377 /* Init the options. */
4378 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4379
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004380 /* need to open the memfile before putting the buffer in a window */
4381 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004382 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004383 /* set curwin/curbuf to buf and save a few things */
4384 aucmd_prepbuf(&aco, newbuf);
4385
4386 /* Need to set the filename for autocommands. */
4387 (void)setfname(curbuf, fname, NULL, FALSE);
4388
Bram Moolenaar81695252004-12-29 20:58:21 +00004389 /* Create swap file now to avoid the ATTENTION message. */
4390 check_need_swap(TRUE);
4391
4392 /* Remove the "dummy" flag, otherwise autocommands may not
4393 * work. */
4394 curbuf->b_flags &= ~BF_DUMMY;
4395
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004396 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004397 if (readfile(fname, NULL,
4398 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4399 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004400 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004401 && !(curbuf->b_flags & BF_NEW))
4402 {
4403 failed = FALSE;
4404 if (curbuf != newbuf)
4405 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004406 /* Bloody autocommands changed the buffer! Can happen when
4407 * using netrw and editing a remote file. Use the current
4408 * buffer instead, delete the dummy one after restoring the
4409 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004410 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004411 newbuf = curbuf;
4412 }
4413 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004414
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004415 /* restore curwin/curbuf and a few other things */
4416 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004417 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4418 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004419
4420 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4421 * skip it. */
4422 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004423 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004424
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004425 /*
4426 * When autocommands/'autochdir' option changed directory: go back.
4427 * Let the caller know what the resulting dir was first, in case it is
4428 * important.
4429 */
4430 mch_dirname(resulting_dir, MAXPATHL);
4431 restore_start_dir(dirname_start);
4432
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004433 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004434 return NULL;
4435 if (failed)
4436 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004437 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004438 return NULL;
4439 }
4440 return newbuf;
4441}
4442
4443/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004444 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4445 * directory to "dirname_start" prior to returning, if autocmds or the
4446 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004447 */
4448 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004449wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004450{
4451 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004452 {
4453#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4454 cleanup_T cs;
4455
4456 /* Reset the error/interrupt/exception state here so that aborting()
4457 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4458 * work when got_int is set. */
4459 enter_cleanup(&cs);
4460#endif
4461
Bram Moolenaar81695252004-12-29 20:58:21 +00004462 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004463
4464#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4465 /* Restore the error/interrupt/exception state if not discarded by a
4466 * new aborting error, interrupt, or uncaught exception. */
4467 leave_cleanup(&cs);
4468#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004469 /* When autocommands/'autochdir' option changed directory: go back. */
4470 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004471 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004472}
4473
4474/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004475 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4476 * directory to "dirname_start" prior to returning, if autocmds or the
4477 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004478 */
4479 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004480unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004481{
4482 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004483 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004484 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004485
4486 /* When autocommands/'autochdir' option changed directory: go back. */
4487 restore_start_dir(dirname_start);
4488 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004489}
4490
Bram Moolenaar05159a02005-02-26 23:04:13 +00004491#if defined(FEAT_EVAL) || defined(PROTO)
4492/*
4493 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004494 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004495 */
4496 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004497get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004498{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004499 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004500 dict_T *dict;
4501 char_u buf[2];
4502 qfline_T *qfp;
4503 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004504 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004505
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004506 if (wp != NULL)
4507 {
4508 qi = GET_LOC_LIST(wp);
4509 if (qi == NULL)
4510 return FAIL;
4511 }
4512
Bram Moolenaard823fa92016-08-12 16:29:27 +02004513 if (qf_idx == -1)
4514 qf_idx = qi->qf_curlist;
4515
4516 if (qf_idx >= qi->qf_listcount
4517 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004518 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004519
Bram Moolenaard823fa92016-08-12 16:29:27 +02004520 qfp = qi->qf_lists[qf_idx].qf_start;
4521 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004522 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004523 /* Handle entries with a non-existing buffer number. */
4524 bufnum = qfp->qf_fnum;
4525 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4526 bufnum = 0;
4527
Bram Moolenaar05159a02005-02-26 23:04:13 +00004528 if ((dict = dict_alloc()) == NULL)
4529 return FAIL;
4530 if (list_append_dict(list, dict) == FAIL)
4531 return FAIL;
4532
4533 buf[0] = qfp->qf_type;
4534 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004535 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004536 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4537 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4538 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4539 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004540 || dict_add_nr_str(dict, "pattern", 0L,
4541 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4542 || dict_add_nr_str(dict, "text", 0L,
4543 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004544 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4545 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4546 return FAIL;
4547
4548 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004549 if (qfp == NULL)
4550 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004551 }
4552 return OK;
4553}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004554
4555/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004556 * Flags used by getqflist()/getloclist() to determine which fields to return.
4557 */
4558enum {
4559 QF_GETLIST_NONE = 0x0,
4560 QF_GETLIST_TITLE = 0x1,
4561 QF_GETLIST_ITEMS = 0x2,
4562 QF_GETLIST_NR = 0x4,
4563 QF_GETLIST_WINID = 0x8,
4564 QF_GETLIST_ALL = 0xFF
4565};
4566
4567/*
4568 * Return quickfix/location list details (title) as a
4569 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4570 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004571 */
4572 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004573get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4574{
4575 qf_info_T *qi = &ql_info;
4576 int status = OK;
4577 int qf_idx;
4578 dictitem_T *di;
4579 int flags = QF_GETLIST_NONE;
4580
4581 if (wp != NULL)
4582 {
4583 qi = GET_LOC_LIST(wp);
4584 if (qi == NULL)
4585 return FAIL;
4586 }
4587
4588 qf_idx = qi->qf_curlist; /* default is the current list */
4589 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4590 {
4591 /* Use the specified quickfix/location list */
4592 if (di->di_tv.v_type == VAR_NUMBER)
4593 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004594 /* for zero use the current list */
4595 if (di->di_tv.vval.v_number != 0)
4596 {
4597 qf_idx = di->di_tv.vval.v_number - 1;
4598 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4599 return FAIL;
4600 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004601 flags |= QF_GETLIST_NR;
4602 }
4603 else
4604 return FAIL;
4605 }
4606
4607 if (dict_find(what, (char_u *)"all", -1) != NULL)
4608 flags |= QF_GETLIST_ALL;
4609
4610 if (dict_find(what, (char_u *)"title", -1) != NULL)
4611 flags |= QF_GETLIST_TITLE;
4612
4613 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4614 flags |= QF_GETLIST_WINID;
4615
4616 if (flags & QF_GETLIST_TITLE)
4617 {
4618 char_u *t;
4619 t = qi->qf_lists[qf_idx].qf_title;
4620 if (t == NULL)
4621 t = (char_u *)"";
4622 status = dict_add_nr_str(retdict, "title", 0L, t);
4623 }
4624 if ((status == OK) && (flags & QF_GETLIST_NR))
4625 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4626 if ((status == OK) && (flags & QF_GETLIST_WINID))
4627 {
4628 win_T *win;
4629 win = qf_find_win(qi);
4630 if (win != NULL)
4631 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4632 }
4633
4634 return status;
4635}
4636
4637/*
4638 * Add list of entries to quickfix/location list. Each list entry is
4639 * a dictionary with item information.
4640 */
4641 static int
4642qf_add_entries(
4643 qf_info_T *qi,
4644 list_T *list,
4645 char_u *title,
4646 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004647{
4648 listitem_T *li;
4649 dict_T *d;
4650 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004651 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004652 long lnum;
4653 int col, nr;
4654 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004655#ifdef FEAT_WINDOWS
4656 qfline_T *old_last = NULL;
4657#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004658 int valid, status;
4659 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004660 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004661
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004662 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004663 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004664 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004665#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004666 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4667 /* Adding to existing list, use last entry. */
4668 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004669#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004670 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004671 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004672 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004673 qf_store_title(qi, title);
4674 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004675
4676 for (li = list->lv_first; li != NULL; li = li->li_next)
4677 {
4678 if (li->li_tv.v_type != VAR_DICT)
4679 continue; /* Skip non-dict items */
4680
4681 d = li->li_tv.vval.v_dict;
4682 if (d == NULL)
4683 continue;
4684
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004685 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004686 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4687 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4688 col = (int)get_dict_number(d, (char_u *)"col");
4689 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4690 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004691 type = get_dict_string(d, (char_u *)"type", TRUE);
4692 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4693 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004694 if (text == NULL)
4695 text = vim_strsave((char_u *)"");
4696
4697 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004698 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004699 valid = FALSE;
4700
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004701 /* Mark entries with non-existing buffer number as not valid. Give the
4702 * error message only once. */
4703 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4704 {
4705 if (!did_bufnr_emsg)
4706 {
4707 did_bufnr_emsg = TRUE;
4708 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4709 }
4710 valid = FALSE;
4711 bufnum = 0;
4712 }
4713
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004714 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004715 NULL, /* dir */
4716 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004717 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004718 text,
4719 lnum,
4720 col,
4721 vcol, /* vis_col */
4722 pattern, /* search pattern */
4723 nr,
4724 type == NULL ? NUL : *type,
4725 valid);
4726
4727 vim_free(filename);
4728 vim_free(pattern);
4729 vim_free(text);
4730 vim_free(type);
4731
4732 if (status == FAIL)
4733 {
4734 retval = FAIL;
4735 break;
4736 }
4737 }
4738
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004739 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004740 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004741 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4742 else
4743 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004744 if (action != 'a') {
4745 qi->qf_lists[qi->qf_curlist].qf_ptr =
4746 qi->qf_lists[qi->qf_curlist].qf_start;
4747 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4748 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4749 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004750
4751#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004752 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004753 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004754#endif
4755
4756 return retval;
4757}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004758
4759 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004760qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004761{
4762 dictitem_T *di;
4763 int retval = FAIL;
4764 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004765 int newlist = FALSE;
4766
4767 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4768 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004769
4770 qf_idx = qi->qf_curlist; /* default is the current list */
4771 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4772 {
4773 /* Use the specified quickfix/location list */
4774 if (di->di_tv.v_type == VAR_NUMBER)
4775 {
4776 qf_idx = di->di_tv.vval.v_number - 1;
4777 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4778 return FAIL;
4779 }
4780 else
4781 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004782 newlist = FALSE; /* use the specified list */
4783 }
4784
4785 if (newlist)
4786 {
4787 qf_new_list(qi, NULL);
4788 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004789 }
4790
4791 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4792 {
4793 if (di->di_tv.v_type == VAR_STRING)
4794 {
4795 vim_free(qi->qf_lists[qf_idx].qf_title);
4796 qi->qf_lists[qf_idx].qf_title =
4797 get_dict_string(what, (char_u *)"title", TRUE);
4798 if (qf_idx == qi->qf_curlist)
4799 qf_update_win_titlevar(qi);
4800 retval = OK;
4801 }
4802 }
4803
4804 return retval;
4805}
4806
4807/*
4808 * Populate the quickfix list with the items supplied in the list
4809 * of dictionaries. "title" will be copied to w:quickfix_title.
4810 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
4811 */
4812 int
4813set_errorlist(
4814 win_T *wp,
4815 list_T *list,
4816 int action,
4817 char_u *title,
4818 dict_T *what)
4819{
4820 qf_info_T *qi = &ql_info;
4821 int retval = OK;
4822
4823 if (wp != NULL)
4824 {
4825 qi = ll_get_or_alloc_list(wp);
4826 if (qi == NULL)
4827 return FAIL;
4828 }
4829
4830 if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004831 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004832 else
4833 retval = qf_add_entries(qi, list, title, action);
4834
4835 return retval;
4836}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004837#endif
4838
Bram Moolenaar81695252004-12-29 20:58:21 +00004839/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004840 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004841 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004842 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004843 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004844 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004845 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004846 */
4847 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004848ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004849{
4850 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004851 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004852#ifdef FEAT_AUTOCMD
4853 char_u *au_name = NULL;
4854#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004855
Bram Moolenaardb552d602006-03-23 22:59:57 +00004856 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4857 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004858 {
4859 qi = ll_get_or_alloc_list(curwin);
4860 if (qi == NULL)
4861 return;
4862 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004863
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004864#ifdef FEAT_AUTOCMD
4865 switch (eap->cmdidx)
4866 {
4867 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
4868 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
4869 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
4870 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
4871 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
4872 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
4873 default: break;
4874 }
4875 if (au_name != NULL)
4876 {
4877 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4878 curbuf->b_fname, TRUE, curbuf);
4879# ifdef FEAT_EVAL
4880 if (did_throw || force_abort)
4881 return;
4882# endif
4883 }
4884#endif
4885
Bram Moolenaar86b68352004-12-27 21:59:20 +00004886 if (*eap->arg == NUL)
4887 buf = curbuf;
4888 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4889 buf = buflist_findnr(atoi((char *)eap->arg));
4890 if (buf == NULL)
4891 EMSG(_(e_invarg));
4892 else if (buf->b_ml.ml_mfp == NULL)
4893 EMSG(_("E681: Buffer is not loaded"));
4894 else
4895 {
4896 if (eap->addr_count == 0)
4897 {
4898 eap->line1 = 1;
4899 eap->line2 = buf->b_ml.ml_line_count;
4900 }
4901 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4902 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4903 EMSG(_(e_invrange));
4904 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004905 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004906 char_u *qf_title = *eap->cmdlinep;
4907
4908 if (buf->b_sfname)
4909 {
4910 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4911 (char *)qf_title, (char *)buf->b_sfname);
4912 qf_title = IObuff;
4913 }
4914
Bram Moolenaardb552d602006-03-23 22:59:57 +00004915 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4916 (eap->cmdidx != CMD_caddbuffer
4917 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004918 eap->line1, eap->line2,
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004919 qf_title) > 0)
4920 {
4921#ifdef FEAT_AUTOCMD
4922 if (au_name != NULL)
4923 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4924 curbuf->b_fname, TRUE, curbuf);
4925#endif
4926 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
4927 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4928 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00004929 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004930 }
4931}
4932
Bram Moolenaar1e015462005-09-25 22:16:38 +00004933#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004934/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004935 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4936 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004937 */
4938 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004939ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004940{
4941 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004942 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004943#ifdef FEAT_AUTOCMD
4944 char_u *au_name = NULL;
4945#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004946
Bram Moolenaardb552d602006-03-23 22:59:57 +00004947 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4948 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004949 {
4950 qi = ll_get_or_alloc_list(curwin);
4951 if (qi == NULL)
4952 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004953 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004954
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004955#ifdef FEAT_AUTOCMD
4956 switch (eap->cmdidx)
4957 {
4958 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
4959 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
4960 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
4961 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
4962 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
4963 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
4964 default: break;
4965 }
4966 if (au_name != NULL)
4967 {
4968 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4969 curbuf->b_fname, TRUE, curbuf);
4970# ifdef FEAT_EVAL
4971 if (did_throw || force_abort)
4972 return;
4973# endif
4974 }
4975#endif
4976
Bram Moolenaar4770d092006-01-12 23:22:24 +00004977 /* Evaluate the expression. When the result is a string or a list we can
4978 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004979 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004980 if (tv != NULL)
4981 {
4982 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4983 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4984 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004985 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004986 (eap->cmdidx != CMD_caddexpr
4987 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004988 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0)
4989 {
4990#ifdef FEAT_AUTOCMD
4991 if (au_name != NULL)
4992 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4993 curbuf->b_fname, TRUE, curbuf);
4994#endif
4995 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
4996 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4997 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004998 }
4999 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005000 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005001 free_tv(tv);
5002 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005003}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005004#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005005
5006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 * ":helpgrep {pattern}"
5008 */
5009 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005010ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
5012 regmatch_T regmatch;
5013 char_u *save_cpo;
5014 char_u *p;
5015 int fcount;
5016 char_u **fnames;
5017 FILE *fd;
5018 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005020#ifdef FEAT_MULTI_LANG
5021 char_u *lang;
5022#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005023 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005024 int new_qi = FALSE;
5025 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005026#ifdef FEAT_AUTOCMD
5027 char_u *au_name = NULL;
5028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005030#ifdef FEAT_MULTI_LANG
5031 /* Check for a specified language */
5032 lang = check_help_lang(eap->arg);
5033#endif
5034
Bram Moolenaar73633f82012-01-20 13:39:07 +01005035#ifdef FEAT_AUTOCMD
5036 switch (eap->cmdidx)
5037 {
5038 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5039 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5040 default: break;
5041 }
5042 if (au_name != NULL)
5043 {
5044 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5045 curbuf->b_fname, TRUE, curbuf);
5046 if (did_throw || force_abort)
5047 return;
5048 }
5049#endif
5050
5051 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5052 save_cpo = p_cpo;
5053 p_cpo = empty_option;
5054
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005055 if (eap->cmdidx == CMD_lhelpgrep)
5056 {
5057 /* Find an existing help window */
5058 FOR_ALL_WINDOWS(wp)
5059 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5060 break;
5061
5062 if (wp == NULL) /* Help window not found */
5063 qi = NULL;
5064 else
5065 qi = wp->w_llist;
5066
5067 if (qi == NULL)
5068 {
5069 /* Allocate a new location list for help text matches */
5070 if ((qi = ll_new_list()) == NULL)
5071 return;
5072 new_qi = TRUE;
5073 }
5074 }
5075
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5077 regmatch.rm_ic = FALSE;
5078 if (regmatch.regprog != NULL)
5079 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005080#ifdef FEAT_MBYTE
5081 vimconv_T vc;
5082
5083 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5084 * differs. */
5085 vc.vc_type = CONV_NONE;
5086 if (!enc_utf8)
5087 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5088#endif
5089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005091 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
5093 /* Go through all directories in 'runtimepath' */
5094 p = p_rtp;
5095 while (*p != NUL && !got_int)
5096 {
5097 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5098
5099 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5100 add_pathsep(NameBuff);
5101 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5102 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5103 &fnames, EW_FILE|EW_SILENT) == OK
5104 && fcount > 0)
5105 {
5106 for (fi = 0; fi < fcount && !got_int; ++fi)
5107 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005108#ifdef FEAT_MULTI_LANG
5109 /* Skip files for a different language. */
5110 if (lang != NULL
5111 && STRNICMP(lang, fnames[fi]
5112 + STRLEN(fnames[fi]) - 3, 2) != 0
5113 && !(STRNICMP(lang, "en", 2) == 0
5114 && STRNICMP("txt", fnames[fi]
5115 + STRLEN(fnames[fi]) - 3, 3) == 0))
5116 continue;
5117#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005118 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 if (fd != NULL)
5120 {
5121 lnum = 1;
5122 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5123 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005124 char_u *line = IObuff;
5125#ifdef FEAT_MBYTE
5126 /* Convert a line if 'encoding' is not utf-8 and
5127 * the line contains a non-ASCII character. */
5128 if (vc.vc_type != CONV_NONE
5129 && has_non_ascii(IObuff)) {
5130 line = string_convert(&vc, IObuff, NULL);
5131 if (line == NULL)
5132 line = IObuff;
5133 }
5134#endif
5135
5136 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005138 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
5140 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005141 while (l > 0 && line[l - 1] <= ' ')
5142 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005144 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 NULL, /* dir */
5146 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005147 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005148 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005150 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005151 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005152 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005153 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 0, /* nr */
5155 1, /* type */
5156 TRUE /* valid */
5157 ) == FAIL)
5158 {
5159 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005160#ifdef FEAT_MBYTE
5161 if (line != IObuff)
5162 vim_free(line);
5163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165 }
5166 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005167#ifdef FEAT_MBYTE
5168 if (line != IObuff)
5169 vim_free(line);
5170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 ++lnum;
5172 line_breakcheck();
5173 }
5174 fclose(fd);
5175 }
5176 }
5177 FreeWild(fcount, fnames);
5178 }
5179 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005180
Bram Moolenaar473de612013-06-08 18:19:48 +02005181 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005182#ifdef FEAT_MBYTE
5183 if (vc.vc_type != CONV_NONE)
5184 convert_setup(&vc, NULL, NULL);
5185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005187 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5188 qi->qf_lists[qi->qf_curlist].qf_ptr =
5189 qi->qf_lists[qi->qf_curlist].qf_start;
5190 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005193 if (p_cpo == empty_option)
5194 p_cpo = save_cpo;
5195 else
5196 /* Darn, some plugin changed the value. */
5197 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005200 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#endif
5202
Bram Moolenaar73633f82012-01-20 13:39:07 +01005203#ifdef FEAT_AUTOCMD
5204 if (au_name != NULL)
5205 {
5206 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5207 curbuf->b_fname, TRUE, curbuf);
5208 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
5209 /* autocommands made "qi" invalid */
5210 return;
5211 }
5212#endif
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005215 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005216 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005217 else
5218 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005219
5220 if (eap->cmdidx == CMD_lhelpgrep)
5221 {
5222 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005223 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005224 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5225 {
5226 if (new_qi)
5227 ll_free_all(&qi);
5228 }
5229 else if (curwin->w_llist == NULL)
5230 curwin->w_llist = qi;
5231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232}
5233
5234#endif /* FEAT_QUICKFIX */