blob: 5bd1257200d1f6c95f17be5019273812492548cf [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 */
Bram Moolenaar9b457942016-10-09 16:10:05 +02001013 if (!qi->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001014 {
Bram Moolenaar9b457942016-10-09 16:10:05 +02001015 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001016
Bram Moolenaar9b457942016-10-09 16:10:05 +02001017 if (qfprev == NULL)
1018 return QF_FAIL;
1019 if (*fields->errmsg && !qi->qf_multiignore)
1020 {
1021 len = (int)STRLEN(qfprev->qf_text);
1022 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1023 == NULL)
1024 return QF_FAIL;
1025 STRCPY(ptr, qfprev->qf_text);
1026 vim_free(qfprev->qf_text);
1027 qfprev->qf_text = ptr;
1028 *(ptr += len) = '\n';
1029 STRCPY(++ptr, fields->errmsg);
1030 }
1031 if (qfprev->qf_nr == -1)
1032 qfprev->qf_nr = fields->enr;
1033 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1034 /* only printable chars allowed */
1035 qfprev->qf_type = fields->type;
1036
1037 if (!qfprev->qf_lnum)
1038 qfprev->qf_lnum = fields->lnum;
1039 if (!qfprev->qf_col)
1040 qfprev->qf_col = fields->col;
1041 qfprev->qf_viscol = fields->use_viscol;
1042 if (!qfprev->qf_fnum)
1043 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1044 *fields->namebuf || qi->qf_directory != NULL
1045 ? fields->namebuf
1046 : qi->qf_currfile != NULL && fields->valid
1047 ? qi->qf_currfile : 0);
1048 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001049 if (idx == 'Z')
1050 qi->qf_multiline = qi->qf_multiignore = FALSE;
1051 line_breakcheck();
1052 return QF_IGNORE_LINE;
1053 }
1054 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1055 {
1056 /* global file names */
1057 fields->valid = FALSE;
1058 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1059 {
1060 if (*fields->namebuf && idx == 'P')
1061 qi->qf_currfile =
1062 qf_push_dir(fields->namebuf, &qi->qf_file_stack, TRUE);
1063 else if (idx == 'Q')
1064 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
1065 *fields->namebuf = NUL;
1066 if (tail && *tail)
1067 {
1068 STRMOVE(IObuff, skipwhite(tail));
1069 qi->qf_multiscan = TRUE;
1070 goto restofline;
1071 }
1072 }
1073 }
1074 if (fmt_ptr->flags == '-') /* generally exclude this line */
1075 {
1076 if (qi->qf_multiline)
1077 /* also exclude continuation lines */
1078 qi->qf_multiignore = TRUE;
1079 return QF_IGNORE_LINE;
1080 }
1081 }
1082
1083 return QF_OK;
1084}
1085
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001086/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001087 * Read the errorfile "efile" into memory, line by line, building the error
1088 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001089 * Alternative: when "efile" is NULL read errors from buffer "buf".
1090 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001091 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001092 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1093 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001094 * Return -1 for error, number of errors for success.
1095 */
1096 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001097qf_init_ext(
1098 qf_info_T *qi,
1099 char_u *efile,
1100 buf_T *buf,
1101 typval_T *tv,
1102 char_u *errorformat,
1103 int newlist, /* TRUE: start a new error list */
1104 linenr_T lnumfirst, /* first line number to use */
1105 linenr_T lnumlast, /* last line number to use */
1106 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001107{
Bram Moolenaare0d37972016-07-15 22:36:01 +02001108 qfstate_T state = {NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001109 NULL, 0, 0};
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001110 qffields_T fields = {NULL, NULL, 0, 0L, 0, FALSE, NULL, 0, 0, 0};
Bram Moolenaar864293a2016-06-02 13:40:04 +02001111#ifdef FEAT_WINDOWS
1112 qfline_T *old_last = NULL;
1113#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001114 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001116 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001118 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001120 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1121 fields.errmsglen = CMDBUFFSIZE + 1;
1122 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1123 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1124 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1125 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 goto qf_init_end;
1127
Bram Moolenaare0d37972016-07-15 22:36:01 +02001128 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
1130 EMSG2(_(e_openerrf), efile);
1131 goto qf_init_end;
1132 }
1133
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001134 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001136 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001137#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001138 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02001139 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001140 /* Adding to existing list, use last entry. */
1141 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001142 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001146 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001147 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 else
1149 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001151 /*
1152 * If we are not adding or adding to another list: clear the state.
1153 */
1154 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
1155 {
1156 qi->qf_dir_curlist = qi->qf_curlist;
1157 qf_clean_dir_stack(&qi->qf_dir_stack);
1158 qi->qf_directory = NULL;
1159 qf_clean_dir_stack(&qi->qf_file_stack);
1160 qi->qf_currfile = NULL;
1161 qi->qf_multiline = FALSE;
1162 qi->qf_multiignore = FALSE;
1163 qi->qf_multiscan = FALSE;
1164 }
1165
1166 /*
1167 * If the errorformat didn't change between calls, then reuse the
1168 * previously parsed values.
1169 */
1170 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1171 {
1172 /* free the previously parsed data */
1173 vim_free(last_efm);
1174 last_efm = NULL;
1175 free_efm_list(&fmt_first);
1176
1177 /* parse the current 'efm' */
1178 fmt_first = parse_efm_option(efm);
1179 if (fmt_first != NULL)
1180 last_efm = vim_strsave(efm);
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
1186 /*
1187 * got_int is reset here, because it was probably set when killing the
1188 * ":make" command, but we still want to read the errorfile then.
1189 */
1190 got_int = FALSE;
1191
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001192 if (tv != NULL)
1193 {
1194 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001195 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001196 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001197 state.p_li = tv->vval.v_list->lv_first;
1198 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001199 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001200 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001201 state.buflnum = lnumfirst;
1202 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 /*
1205 * Read the lines in the error file one by one.
1206 * Try to recognize one of the error formats in each line.
1207 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001208 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001210 /* Get the next line from a file/buffer/list/string */
1211 status = qf_get_nextline(&state);
1212 if (status == QF_NOMEM) /* memory alloc failure */
1213 goto qf_init_end;
1214 if (status == QF_END_OF_INPUT) /* end of input */
1215 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001217 status = qf_parse_line(qi, state.linebuf, state.linelen, fmt_first,
1218 &fields);
1219 if (status == QF_FAIL)
1220 goto error2;
1221 if (status == QF_NOMEM)
1222 goto qf_init_end;
1223 if (status == QF_IGNORE_LINE)
1224 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001226 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001227 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001228 (*fields.namebuf || qi->qf_directory != NULL)
1229 ? fields.namebuf
1230 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001231 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001232 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001233 fields.errmsg,
1234 fields.lnum,
1235 fields.col,
1236 fields.use_viscol,
1237 fields.pattern,
1238 fields.enr,
1239 fields.type,
1240 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 goto error2;
1242 line_breakcheck();
1243 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001244 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001246 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001248 /* no valid entry found */
1249 qi->qf_lists[qi->qf_curlist].qf_ptr =
1250 qi->qf_lists[qi->qf_curlist].qf_start;
1251 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1252 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254 else
1255 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001256 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1257 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1258 qi->qf_lists[qi->qf_curlist].qf_ptr =
1259 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001261 /* return number of matches */
1262 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001263 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265 EMSG(_(e_readerrf));
1266error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001267 qf_free(qi, qi->qf_curlist);
1268 qi->qf_listcount--;
1269 if (qi->qf_curlist > 0)
1270 --qi->qf_curlist;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001271qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001272 if (state.fd != NULL)
1273 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001274 vim_free(fields.namebuf);
1275 vim_free(fields.errmsg);
1276 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001277 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
1279#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001280 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281#endif
1282
1283 return retval;
1284}
1285
Bram Moolenaarfb604092014-07-23 15:55:00 +02001286 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001287qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001288{
1289 if (title != NULL)
1290 {
1291 char_u *p = alloc((int)STRLEN(title) + 2);
1292
1293 qi->qf_lists[qi->qf_curlist].qf_title = p;
1294 if (p != NULL)
1295 sprintf((char *)p, ":%s", (char *)title);
1296 }
1297}
1298
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299/*
1300 * Prepare for adding a new quickfix list.
1301 */
1302 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001303qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
1305 int i;
1306
1307 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001308 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 * the current entry. This makes it possible to browse in a tree-like
1310 * way with ":grep'.
1311 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001312 while (qi->qf_listcount > qi->qf_curlist + 1)
1313 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
1315 /*
1316 * When the stack is full, remove to oldest entry
1317 * Otherwise, add a new entry.
1318 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001319 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001321 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001323 qi->qf_lists[i - 1] = qi->qf_lists[i];
1324 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001327 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001328 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001329 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330}
1331
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001332/*
1333 * Free a location list
1334 */
1335 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001336ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001337{
1338 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001339 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001340
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001341 qi = *pqi;
1342 if (qi == NULL)
1343 return;
1344 *pqi = NULL; /* Remove reference to this list */
1345
1346 qi->qf_refcount--;
1347 if (qi->qf_refcount < 1)
1348 {
1349 /* No references to this location list */
1350 for (i = 0; i < qi->qf_listcount; ++i)
1351 qf_free(qi, i);
1352 vim_free(qi);
1353 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001354}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001355
1356 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001357qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001358{
1359 int i;
1360 qf_info_T *qi = &ql_info;
1361
1362 if (wp != NULL)
1363 {
1364 /* location list */
1365 ll_free_all(&wp->w_llist);
1366 ll_free_all(&wp->w_llist_ref);
1367 }
1368 else
1369 /* quickfix list */
1370 for (i = 0; i < qi->qf_listcount; ++i)
1371 qf_free(qi, i);
1372}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374/*
1375 * Add an entry to the end of the list of errors.
1376 * Returns OK or FAIL.
1377 */
1378 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001379qf_add_entry(
1380 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001381 char_u *dir, /* optional directory name */
1382 char_u *fname, /* file name or NULL */
1383 int bufnum, /* buffer number or zero */
1384 char_u *mesg, /* message */
1385 long lnum, /* line number */
1386 int col, /* column */
1387 int vis_col, /* using visual column */
1388 char_u *pattern, /* search pattern */
1389 int nr, /* error number */
1390 int type, /* type character */
1391 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001393 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001394 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001396 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001398 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001399 {
1400 buf_T *buf = buflist_findnr(bufnum);
1401
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001402 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001403 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001404 buf->b_has_qf_entry |=
1405 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001406 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001407 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001408 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1410 {
1411 vim_free(qfp);
1412 return FAIL;
1413 }
1414 qfp->qf_lnum = lnum;
1415 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001416 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001417 if (pattern == NULL || *pattern == NUL)
1418 qfp->qf_pattern = NULL;
1419 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1420 {
1421 vim_free(qfp->qf_text);
1422 vim_free(qfp);
1423 return FAIL;
1424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 qfp->qf_nr = nr;
1426 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1427 type = 0;
1428 qfp->qf_type = type;
1429 qfp->qf_valid = valid;
1430
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001431 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001432 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1433 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001435 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001436 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1437 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001438 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 }
1440 else
1441 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001442 qfp->qf_prev = *lastp;
1443 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001445 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001447 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001448 ++qi->qf_lists[qi->qf_curlist].qf_count;
1449 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1450 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001452 qi->qf_lists[qi->qf_curlist].qf_index =
1453 qi->qf_lists[qi->qf_curlist].qf_count;
1454 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 }
1456
1457 return OK;
1458}
1459
1460/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001461 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001462 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001463 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001464ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001465{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001466 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001467
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001468 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1469 if (qi != NULL)
1470 {
1471 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1472 qi->qf_refcount++;
1473 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001474
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001475 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001476}
1477
1478/*
1479 * Return the location list for window 'wp'.
1480 * If not present, allocate a location list
1481 */
1482 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001483ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001484{
1485 if (IS_LL_WINDOW(wp))
1486 /* For a location list window, use the referenced location list */
1487 return wp->w_llist_ref;
1488
1489 /*
1490 * For a non-location list window, w_llist_ref should not point to a
1491 * location list.
1492 */
1493 ll_free_all(&wp->w_llist_ref);
1494
1495 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001496 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001497 return wp->w_llist;
1498}
1499
1500/*
1501 * Copy the location list from window "from" to window "to".
1502 */
1503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001504copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001505{
1506 qf_info_T *qi;
1507 int idx;
1508 int i;
1509
1510 /*
1511 * When copying from a location list window, copy the referenced
1512 * location list. For other windows, copy the location list for
1513 * that window.
1514 */
1515 if (IS_LL_WINDOW(from))
1516 qi = from->w_llist_ref;
1517 else
1518 qi = from->w_llist;
1519
1520 if (qi == NULL) /* no location list to copy */
1521 return;
1522
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001523 /* allocate a new location list */
1524 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001525 return;
1526
1527 to->w_llist->qf_listcount = qi->qf_listcount;
1528
1529 /* Copy the location lists one at a time */
1530 for (idx = 0; idx < qi->qf_listcount; idx++)
1531 {
1532 qf_list_T *from_qfl;
1533 qf_list_T *to_qfl;
1534
1535 to->w_llist->qf_curlist = idx;
1536
1537 from_qfl = &qi->qf_lists[idx];
1538 to_qfl = &to->w_llist->qf_lists[idx];
1539
1540 /* Some of the fields are populated by qf_add_entry() */
1541 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1542 to_qfl->qf_count = 0;
1543 to_qfl->qf_index = 0;
1544 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001545 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001546 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001547 if (from_qfl->qf_title != NULL)
1548 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1549 else
1550 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001551
1552 if (from_qfl->qf_count)
1553 {
1554 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001555 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001556
1557 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001558 for (i = 0, from_qfp = from_qfl->qf_start;
1559 i < from_qfl->qf_count && from_qfp != NULL;
1560 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001561 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001562 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001563 NULL,
1564 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001565 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001566 from_qfp->qf_text,
1567 from_qfp->qf_lnum,
1568 from_qfp->qf_col,
1569 from_qfp->qf_viscol,
1570 from_qfp->qf_pattern,
1571 from_qfp->qf_nr,
1572 0,
1573 from_qfp->qf_valid) == FAIL)
1574 {
1575 qf_free_all(to);
1576 return;
1577 }
1578 /*
1579 * qf_add_entry() will not set the qf_num field, as the
1580 * directory and file names are not supplied. So the qf_fnum
1581 * field is copied here.
1582 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001583 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001584 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1585 prevp->qf_type = from_qfp->qf_type; /* error type */
1586 if (from_qfl->qf_ptr == from_qfp)
1587 to_qfl->qf_ptr = prevp; /* current location */
1588 }
1589 }
1590
1591 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1592
1593 /* When no valid entries are present in the list, qf_ptr points to
1594 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001595 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001596 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001597 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001598 to_qfl->qf_index = 1;
1599 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001600 }
1601
1602 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1603}
1604
1605/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001606 * Looking up a buffer can be slow if there are many. Remember the last one
1607 * to make this a lot faster if there are multiple matches in the same file.
1608 */
1609static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001610static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001611
1612/*
Bram Moolenaar015102e2016-07-16 18:24:56 +02001613 * Get buffer number for file "directory.fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001614 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 */
1616 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001617qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
Bram Moolenaar82404332016-07-10 17:00:38 +02001619 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001620 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001621 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (fname == NULL || *fname == NUL) /* no file name */
1624 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
Bram Moolenaare60acc12011-05-10 16:41:25 +02001626#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001627 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001628#endif
1629#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001630 if (directory != NULL)
1631 slash_adjust(directory);
1632 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001633#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001634 if (directory != NULL && !vim_isAbsName(fname)
1635 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1636 {
1637 /*
1638 * Here we check if the file really exists.
1639 * This should normally be true, but if make works without
1640 * "leaving directory"-messages we might have missed a
1641 * directory change.
1642 */
1643 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001646 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001647 if (directory)
1648 ptr = concat_fnames(directory, fname, TRUE);
1649 else
1650 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001652 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001653 bufname = ptr;
1654 }
1655 else
1656 bufname = fname;
1657
1658 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001659 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001660 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001661 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001662 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001664 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001665 {
1666 vim_free(qf_last_bufname);
1667 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1668 if (bufname == ptr)
1669 qf_last_bufname = bufname;
1670 else
1671 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001672 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001673 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001674 if (buf == NULL)
1675 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001676
Bram Moolenaarc1542742016-07-20 21:44:37 +02001677 buf->b_has_qf_entry =
1678 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001679 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680}
1681
1682/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001683 * Push dirbuf onto the directory stack and return pointer to actual dir or
1684 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 */
1686 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001687qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
1689 struct dir_stack_T *ds_new;
1690 struct dir_stack_T *ds_ptr;
1691
1692 /* allocate new stack element and hook it in */
1693 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1694 if (ds_new == NULL)
1695 return NULL;
1696
1697 ds_new->next = *stackptr;
1698 *stackptr = ds_new;
1699
1700 /* store directory on the stack */
1701 if (vim_isAbsName(dirbuf)
1702 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001703 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 (*stackptr)->dirname = vim_strsave(dirbuf);
1705 else
1706 {
1707 /* Okay we don't have an absolute path.
1708 * dirbuf must be a subdir of one of the directories on the stack.
1709 * Let's search...
1710 */
1711 ds_new = (*stackptr)->next;
1712 (*stackptr)->dirname = NULL;
1713 while (ds_new)
1714 {
1715 vim_free((*stackptr)->dirname);
1716 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1717 TRUE);
1718 if (mch_isdir((*stackptr)->dirname) == TRUE)
1719 break;
1720
1721 ds_new = ds_new->next;
1722 }
1723
1724 /* clean up all dirs we already left */
1725 while ((*stackptr)->next != ds_new)
1726 {
1727 ds_ptr = (*stackptr)->next;
1728 (*stackptr)->next = (*stackptr)->next->next;
1729 vim_free(ds_ptr->dirname);
1730 vim_free(ds_ptr);
1731 }
1732
1733 /* Nothing found -> it must be on top level */
1734 if (ds_new == NULL)
1735 {
1736 vim_free((*stackptr)->dirname);
1737 (*stackptr)->dirname = vim_strsave(dirbuf);
1738 }
1739 }
1740
1741 if ((*stackptr)->dirname != NULL)
1742 return (*stackptr)->dirname;
1743 else
1744 {
1745 ds_ptr = *stackptr;
1746 *stackptr = (*stackptr)->next;
1747 vim_free(ds_ptr);
1748 return NULL;
1749 }
1750}
1751
1752
1753/*
1754 * pop dirbuf from the directory stack and return previous directory or NULL if
1755 * stack is empty
1756 */
1757 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001758qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 struct dir_stack_T *ds_ptr;
1761
1762 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1763 * What to do if it isn't? */
1764
1765 /* pop top element and free it */
1766 if (*stackptr != NULL)
1767 {
1768 ds_ptr = *stackptr;
1769 *stackptr = (*stackptr)->next;
1770 vim_free(ds_ptr->dirname);
1771 vim_free(ds_ptr);
1772 }
1773
1774 /* return NEW top element as current dir or NULL if stack is empty*/
1775 return *stackptr ? (*stackptr)->dirname : NULL;
1776}
1777
1778/*
1779 * clean up directory stack
1780 */
1781 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001782qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
1784 struct dir_stack_T *ds_ptr;
1785
1786 while ((ds_ptr = *stackptr) != NULL)
1787 {
1788 *stackptr = (*stackptr)->next;
1789 vim_free(ds_ptr->dirname);
1790 vim_free(ds_ptr);
1791 }
1792}
1793
1794/*
1795 * Check in which directory of the directory stack the given file can be
1796 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001797 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 * Cleans up intermediate directory entries.
1799 *
1800 * TODO: How to solve the following problem?
1801 * If we have the this directory tree:
1802 * ./
1803 * ./aa
1804 * ./aa/bb
1805 * ./bb
1806 * ./bb/x.c
1807 * and make says:
1808 * making all in aa
1809 * making all in bb
1810 * x.c:9: Error
1811 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1812 * qf_guess_filepath will return NULL.
1813 */
1814 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001815qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
1817 struct dir_stack_T *ds_ptr;
1818 struct dir_stack_T *ds_tmp;
1819 char_u *fullname;
1820
1821 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001822 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 return NULL;
1824
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001825 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 fullname = NULL;
1827 while (ds_ptr)
1828 {
1829 vim_free(fullname);
1830 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1831
1832 /* If concat_fnames failed, just go on. The worst thing that can happen
1833 * is that we delete the entire stack.
1834 */
1835 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1836 break;
1837
1838 ds_ptr = ds_ptr->next;
1839 }
1840
1841 vim_free(fullname);
1842
1843 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001844 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001846 ds_tmp = qi->qf_dir_stack->next;
1847 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 vim_free(ds_tmp->dirname);
1849 vim_free(ds_tmp);
1850 }
1851
1852 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853}
1854
1855/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001856 * When loading a file from the quickfix, the auto commands may modify it.
1857 * This may invalidate the current quickfix entry. This function checks
1858 * whether a entry is still present in the quickfix.
1859 * Similar to location list.
1860 */
1861 static int
1862is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1863{
1864 qf_list_T *qfl;
1865 qfline_T *qfp;
1866 int i;
1867
1868 qfl = &qi->qf_lists[qi->qf_curlist];
1869
1870 /* Search for the entry in the current list */
1871 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1872 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001873 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001874 break;
1875
1876 if (i == qfl->qf_count) /* Entry is not found */
1877 return FALSE;
1878
1879 return TRUE;
1880}
1881
1882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 * jump to a quickfix line
1884 * if dir == FORWARD go "errornr" valid entries forward
1885 * if dir == BACKWARD go "errornr" valid entries backward
1886 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1887 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1888 * else if "errornr" is zero, redisplay the same line
1889 * else go to entry "errornr"
1890 */
1891 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001892qf_jump(
1893 qf_info_T *qi,
1894 int dir,
1895 int errornr,
1896 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001899 qfline_T *qf_ptr;
1900 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 int qf_index;
1902 int old_qf_fnum;
1903 int old_qf_index;
1904 int prev_index;
1905 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1906 char_u *err = e_no_more_items;
1907 linenr_T i;
1908 buf_T *old_curbuf;
1909 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 colnr_T screen_col;
1911 colnr_T char_col;
1912 char_u *line;
1913#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001914 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001915 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 int opened_window = FALSE;
1917 win_T *win;
1918 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001919 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001921 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 int print_message = TRUE;
1923 int len;
1924#ifdef FEAT_FOLDING
1925 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1926#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001927 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001928 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001930 if (qi == NULL)
1931 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001932
1933 if (qi->qf_curlist >= qi->qf_listcount
1934 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
1936 EMSG(_(e_quickfix));
1937 return;
1938 }
1939
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001940 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 old_qf_index = qf_index;
1944 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1945 {
1946 while (errornr--)
1947 {
1948 old_qf_ptr = qf_ptr;
1949 prev_index = qf_index;
1950 old_qf_fnum = qf_ptr->qf_fnum;
1951 do
1952 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001953 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 || qf_ptr->qf_next == NULL)
1955 {
1956 qf_ptr = old_qf_ptr;
1957 qf_index = prev_index;
1958 if (err != NULL)
1959 {
1960 EMSG(_(err));
1961 goto theend;
1962 }
1963 errornr = 0;
1964 break;
1965 }
1966 ++qf_index;
1967 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001968 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1969 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1971 err = NULL;
1972 }
1973 }
1974 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1975 {
1976 while (errornr--)
1977 {
1978 old_qf_ptr = qf_ptr;
1979 prev_index = qf_index;
1980 old_qf_fnum = qf_ptr->qf_fnum;
1981 do
1982 {
1983 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1984 {
1985 qf_ptr = old_qf_ptr;
1986 qf_index = prev_index;
1987 if (err != NULL)
1988 {
1989 EMSG(_(err));
1990 goto theend;
1991 }
1992 errornr = 0;
1993 break;
1994 }
1995 --qf_index;
1996 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001997 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1998 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2000 err = NULL;
2001 }
2002 }
2003 else if (errornr != 0) /* go to specified number */
2004 {
2005 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2006 {
2007 --qf_index;
2008 qf_ptr = qf_ptr->qf_prev;
2009 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002010 while (errornr > qf_index && qf_index <
2011 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 && qf_ptr->qf_next != NULL)
2013 {
2014 ++qf_index;
2015 qf_ptr = qf_ptr->qf_next;
2016 }
2017 }
2018
2019#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002020 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2021 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 /* No need to print the error message if it's visible in the error
2023 * window */
2024 print_message = FALSE;
2025
2026 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002027 * For ":helpgrep" find a help window or open one.
2028 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002029 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002030 {
2031 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002032
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002033 if (cmdmod.tab != 0)
2034 wp = NULL;
2035 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002036 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002037 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2038 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002039 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2040 win_enter(wp, TRUE);
2041 else
2042 {
2043 /*
2044 * Split off help window; put it at far top if no position
2045 * specified, the current window is vertically split and narrow.
2046 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002047 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002048 if (cmdmod.split == 0 && curwin->w_width != Columns
2049 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002050 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002051 if (qi != &ql_info)
2052 flags |= WSP_NEWLOC; /* don't copy the location list */
2053
2054 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002055 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002056 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002057
2058 if (curwin->w_height < p_hh)
2059 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002060
2061 if (qi != &ql_info) /* not a quickfix list */
2062 {
2063 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002064 curwin->w_llist = qi;
2065 qi->qf_refcount++;
2066 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002067 }
2068
2069 if (!p_im)
2070 restart_edit = 0; /* don't want insert mode in help file */
2071 }
2072
2073 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 * If currently in the quickfix window, find another window to show the
2075 * file in.
2076 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002077 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002079 win_T *usable_win_ptr = NULL;
2080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 /*
2082 * If there is no file specified, we don't know where to go.
2083 * But do advance, otherwise ":cn" gets stuck.
2084 */
2085 if (qf_ptr->qf_fnum == 0)
2086 goto theend;
2087
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002089
2090 ll_ref = curwin->w_llist_ref;
2091 if (ll_ref != NULL)
2092 {
2093 /* Find a window using the same location list that is not a
2094 * quickfix window. */
2095 FOR_ALL_WINDOWS(usable_win_ptr)
2096 if (usable_win_ptr->w_llist == ll_ref
2097 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002098 {
2099 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002100 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002101 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002102 }
2103
2104 if (!usable_win)
2105 {
2106 /* Locate a window showing a normal buffer */
2107 FOR_ALL_WINDOWS(win)
2108 if (win->w_buffer->b_p_bt[0] == NUL)
2109 {
2110 usable_win = 1;
2111 break;
2112 }
2113 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002116 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002117 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002119 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002120 {
2121 tabpage_T *tp;
2122 win_T *wp;
2123
2124 FOR_ALL_TAB_WINDOWS(tp, wp)
2125 {
2126 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2127 {
2128 goto_tabpage_win(tp, wp);
2129 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002130 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002131 }
2132 }
2133 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002134win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002135
2136 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002137 * If there is only one window and it is the quickfix window, create a
2138 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002139 */
2140 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002142 flags = WSP_ABOVE;
2143 if (ll_ref != NULL)
2144 flags |= WSP_NEWLOC;
2145 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 goto failed; /* not enough room for window */
2147 opened_window = TRUE; /* close it when fail */
2148 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002149 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002150 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002151 if (ll_ref != NULL)
2152 {
2153 /* The new window should use the location list from the
2154 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002155 curwin->w_llist = ll_ref;
2156 ll_ref->qf_refcount++;
2157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159 else
2160 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002161 if (curwin->w_llist_ref != NULL)
2162 {
2163 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002164 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002165 if (win == NULL)
2166 {
2167 /* Find the window showing the selected file */
2168 FOR_ALL_WINDOWS(win)
2169 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2170 break;
2171 if (win == NULL)
2172 {
2173 /* Find a previous usable window */
2174 win = curwin;
2175 do
2176 {
2177 if (win->w_buffer->b_p_bt[0] == NUL)
2178 break;
2179 if (win->w_prev == NULL)
2180 win = lastwin; /* wrap around the top */
2181 else
2182 win = win->w_prev; /* go to previous window */
2183 } while (win != curwin);
2184 }
2185 }
2186 win_goto(win);
2187
2188 /* If the location list for the window is not set, then set it
2189 * to the location list from the location window */
2190 if (win->w_llist == NULL)
2191 {
2192 win->w_llist = ll_ref;
2193 ll_ref->qf_refcount++;
2194 }
2195 }
2196 else
2197 {
2198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 /*
2200 * Try to find a window that shows the right buffer.
2201 * Default to the window just above the quickfix buffer.
2202 */
2203 win = curwin;
2204 altwin = NULL;
2205 for (;;)
2206 {
2207 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2208 break;
2209 if (win->w_prev == NULL)
2210 win = lastwin; /* wrap around the top */
2211 else
2212 win = win->w_prev; /* go to previous window */
2213
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002214 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 {
2216 /* Didn't find it, go to the window before the quickfix
2217 * window. */
2218 if (altwin != NULL)
2219 win = altwin;
2220 else if (curwin->w_prev != NULL)
2221 win = curwin->w_prev;
2222 else
2223 win = curwin->w_next;
2224 break;
2225 }
2226
2227 /* Remember a usable window. */
2228 if (altwin == NULL && !win->w_p_pvw
2229 && win->w_buffer->b_p_bt[0] == NUL)
2230 altwin = win;
2231 }
2232
2233 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 }
2236 }
2237#endif
2238
2239 /*
2240 * If there is a file name,
2241 * read the wanted file if needed, and check autowrite etc.
2242 */
2243 old_curbuf = curbuf;
2244 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002245
2246 if (qf_ptr->qf_fnum != 0)
2247 {
2248 if (qf_ptr->qf_type == 1)
2249 {
2250 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2251 * set b_p_ro flag). */
2252 if (!can_abandon(curbuf, forceit))
2253 {
2254 EMSG(_(e_nowrtmsg));
2255 ok = FALSE;
2256 }
2257 else
2258 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002259 ECMD_HIDE + ECMD_SET_HELP,
2260 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002261 }
2262 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002263 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002264 int old_qf_curlist = qi->qf_curlist;
2265 int is_abort = FALSE;
2266
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002267 ok = buflist_getfile(qf_ptr->qf_fnum,
2268 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002269 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002270 {
2271 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002272 is_abort = TRUE;
2273 opened_window = FALSE;
2274 }
2275 else if (old_qf_curlist != qi->qf_curlist
2276 || !is_qf_entry_present(qi, qf_ptr))
2277 {
2278 if (qi == &ql_info)
2279 EMSG(_("E925: Current quickfix was changed"));
2280 else
2281 EMSG(_("E926: Current location list was changed"));
2282 is_abort = TRUE;
2283 }
2284
2285 if (is_abort)
2286 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002287 ok = FALSE;
2288 qi = NULL;
2289 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002290 }
2291 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002292 }
2293
2294 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 /* When not switched to another buffer, still need to set pc mark */
2297 if (curbuf == old_curbuf)
2298 setpcmark();
2299
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002300 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002302 /*
2303 * Go to line with error, unless qf_lnum is 0.
2304 */
2305 i = qf_ptr->qf_lnum;
2306 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002308 if (i > curbuf->b_ml.ml_line_count)
2309 i = curbuf->b_ml.ml_line_count;
2310 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002312 if (qf_ptr->qf_col > 0)
2313 {
2314 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002315#ifdef FEAT_VIRTUALEDIT
2316 curwin->w_cursor.coladd = 0;
2317#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002318 if (qf_ptr->qf_viscol == TRUE)
2319 {
2320 /*
2321 * Check each character from the beginning of the error
2322 * line up to the error column. For each tab character
2323 * found, reduce the error column value by the length of
2324 * a tab character.
2325 */
2326 line = ml_get_curline();
2327 screen_col = 0;
2328 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2329 {
2330 if (*line == NUL)
2331 break;
2332 if (*line++ == '\t')
2333 {
2334 curwin->w_cursor.col -= 7 - (screen_col % 8);
2335 screen_col += 8 - (screen_col % 8);
2336 }
2337 else
2338 ++screen_col;
2339 }
2340 }
2341 check_cursor();
2342 }
2343 else
2344 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002347 {
2348 pos_T save_cursor;
2349
2350 /* Move the cursor to the first line in the buffer */
2351 save_cursor = curwin->w_cursor;
2352 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002353 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2354 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002355 curwin->w_cursor = save_cursor;
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358#ifdef FEAT_FOLDING
2359 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2360 foldOpenCursor();
2361#endif
2362 if (print_message)
2363 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002364 /* Update the screen before showing the message, unless the screen
2365 * scrolled up. */
2366 if (!msg_scrolled)
2367 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002369 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2371 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2372 /* Add the message, skipping leading whitespace and newlines. */
2373 len = (int)STRLEN(IObuff);
2374 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2375
2376 /* Output the message. Overwrite to avoid scrolling when the 'O'
2377 * flag is present in 'shortmess'; But when not jumping, print the
2378 * whole message. */
2379 i = msg_scroll;
2380 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2381 msg_scroll = TRUE;
2382 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2383 msg_scroll = FALSE;
2384 msg_attr_keep(IObuff, 0, TRUE);
2385 msg_scroll = i;
2386 }
2387 }
2388 else
2389 {
2390#ifdef FEAT_WINDOWS
2391 if (opened_window)
2392 win_close(curwin, TRUE); /* Close opened window */
2393#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002394 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
2396 /*
2397 * Couldn't open file, so put index back where it was. This could
2398 * happen if the file was readonly and we changed something.
2399 */
2400#ifdef FEAT_WINDOWS
2401failed:
2402#endif
2403 qf_ptr = old_qf_ptr;
2404 qf_index = old_qf_index;
2405 }
2406 }
2407theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002408 if (qi != NULL)
2409 {
2410 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2411 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#ifdef FEAT_WINDOWS
2414 if (p_swb != old_swb && opened_window)
2415 {
2416 /* Restore old 'switchbuf' value, but not when an autocommand or
2417 * modeline has changed the value. */
2418 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002421 swb_flags = old_swb_flags;
2422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 else
2424 free_string_option(old_swb);
2425 }
2426#endif
2427}
2428
2429/*
2430 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002431 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
2433 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002434qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002436 buf_T *buf;
2437 char_u *fname;
2438 qfline_T *qfp;
2439 int i;
2440 int idx1 = 1;
2441 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002442 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002443 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002444 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002446 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002448 if (eap->cmdidx == CMD_llist)
2449 {
2450 qi = GET_LOC_LIST(curwin);
2451 if (qi == NULL)
2452 {
2453 EMSG(_(e_loclist));
2454 return;
2455 }
2456 }
2457
2458 if (qi->qf_curlist >= qi->qf_listcount
2459 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
2461 EMSG(_(e_quickfix));
2462 return;
2463 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002464 if (*arg == '+')
2465 {
2466 ++arg;
2467 plus = TRUE;
2468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2470 {
2471 EMSG(_(e_trailing));
2472 return;
2473 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002474 if (plus)
2475 {
2476 i = qi->qf_lists[qi->qf_curlist].qf_index;
2477 idx2 = i + idx1;
2478 idx1 = i;
2479 }
2480 else
2481 {
2482 i = qi->qf_lists[qi->qf_curlist].qf_count;
2483 if (idx1 < 0)
2484 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2485 if (idx2 < 0)
2486 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002489 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002491 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2492 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2495 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002496 msg_putchar('\n');
2497 if (got_int)
2498 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002499
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002500 fname = NULL;
2501 if (qfp->qf_fnum != 0
2502 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2503 {
2504 fname = buf->b_fname;
2505 if (qfp->qf_type == 1) /* :helpgrep */
2506 fname = gettail(fname);
2507 }
2508 if (fname == NULL)
2509 sprintf((char *)IObuff, "%2d", i);
2510 else
2511 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2512 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002513 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002514 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2515 if (qfp->qf_lnum == 0)
2516 IObuff[0] = NUL;
2517 else if (qfp->qf_col == 0)
2518 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2519 else
2520 sprintf((char *)IObuff, ":%ld col %d",
2521 qfp->qf_lnum, qfp->qf_col);
2522 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2523 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2524 msg_puts_attr(IObuff, hl_attr(HLF_N));
2525 if (qfp->qf_pattern != NULL)
2526 {
2527 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2528 STRCAT(IObuff, ":");
2529 msg_puts(IObuff);
2530 }
2531 msg_puts((char_u *)" ");
2532
2533 /* Remove newlines and leading whitespace from the text. For an
2534 * unrecognized line keep the indent, the compiler may mark a word
2535 * with ^^^^. */
2536 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2538 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002539 msg_prt_line(IObuff, FALSE);
2540 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002542
2543 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002544 if (qfp == NULL)
2545 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002546 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 ui_breakcheck();
2548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549}
2550
2551/*
2552 * Remove newlines and leading whitespace from an error message.
2553 * Put the result in "buf[bufsize]".
2554 */
2555 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002556qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 int i;
2559 char_u *p = text;
2560
2561 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2562 {
2563 if (*p == '\n')
2564 {
2565 buf[i] = ' ';
2566 while (*++p != NUL)
2567 if (!vim_iswhite(*p) && *p != '\n')
2568 break;
2569 }
2570 else
2571 buf[i] = *p++;
2572 }
2573 buf[i] = NUL;
2574}
2575
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002576 static void
2577qf_msg(qf_info_T *qi, int which, char *lead)
2578{
2579 char *title = (char *)qi->qf_lists[which].qf_title;
2580 int count = qi->qf_lists[which].qf_count;
2581 char_u buf[IOSIZE];
2582
2583 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2584 lead,
2585 which + 1,
2586 qi->qf_listcount,
2587 count);
2588
2589 if (title != NULL)
2590 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002591 size_t len = STRLEN(buf);
2592
2593 if (len < 34)
2594 {
2595 vim_memset(buf + len, ' ', 34 - len);
2596 buf[34] = NUL;
2597 }
2598 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002599 }
2600 trunc_string(buf, buf, Columns - 1, IOSIZE);
2601 msg(buf);
2602}
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604/*
2605 * ":colder [count]": Up in the quickfix stack.
2606 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002607 * ":lolder [count]": Up in the location list stack.
2608 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 */
2610 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002611qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002613 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 int count;
2615
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002616 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2617 {
2618 qi = GET_LOC_LIST(curwin);
2619 if (qi == NULL)
2620 {
2621 EMSG(_(e_loclist));
2622 return;
2623 }
2624 }
2625
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (eap->addr_count != 0)
2627 count = eap->line2;
2628 else
2629 count = 1;
2630 while (count--)
2631 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002632 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002634 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002637 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002639 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
2641 else
2642 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002643 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 {
2645 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002646 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002648 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002651 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002653 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654#endif
2655}
2656
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002657 void
2658qf_history(exarg_T *eap)
2659{
2660 qf_info_T *qi = &ql_info;
2661 int i;
2662
2663 if (eap->cmdidx == CMD_lhistory)
2664 qi = GET_LOC_LIST(curwin);
2665 if (qi == NULL || (qi->qf_listcount == 0
2666 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2667 MSG(_("No entries"));
2668 else
2669 for (i = 0; i < qi->qf_listcount; ++i)
2670 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2671}
2672
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002674 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 */
2676 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002677qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002679 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002680 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002681 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002683 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002685 qfp = qi->qf_lists[idx].qf_start;
2686 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002687 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002688 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002689 vim_free(qfp->qf_text);
2690 stop = (qfp == qfpnext);
2691 vim_free(qfp->qf_pattern);
2692 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002693 if (stop)
2694 /* Somehow qf_count may have an incorrect value, set it to 1
2695 * to avoid crashing when it's wrong.
2696 * TODO: Avoid qf_count being incorrect. */
2697 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002698 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002699 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002700 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002702 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002703 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002704 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002705
2706 qf_clean_dir_stack(&qi->qf_dir_stack);
2707 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708}
2709
2710/*
2711 * qf_mark_adjust: adjust marks
2712 */
2713 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002714qf_mark_adjust(
2715 win_T *wp,
2716 linenr_T line1,
2717 linenr_T line2,
2718 long amount,
2719 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002721 int i;
2722 qfline_T *qfp;
2723 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002724 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002725 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002726 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
Bram Moolenaarc1542742016-07-20 21:44:37 +02002728 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002729 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002730 if (wp != NULL)
2731 {
2732 if (wp->w_llist == NULL)
2733 return;
2734 qi = wp->w_llist;
2735 }
2736
2737 for (idx = 0; idx < qi->qf_listcount; ++idx)
2738 if (qi->qf_lists[idx].qf_count)
2739 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002740 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2741 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (qfp->qf_fnum == curbuf->b_fnum)
2743 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002744 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2746 {
2747 if (amount == MAXLNUM)
2748 qfp->qf_cleared = TRUE;
2749 else
2750 qfp->qf_lnum += amount;
2751 }
2752 else if (amount_after && qfp->qf_lnum > line2)
2753 qfp->qf_lnum += amount_after;
2754 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002755
2756 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002757 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758}
2759
2760/*
2761 * Make a nice message out of the error character and the error number:
2762 * char number message
2763 * e or E 0 " error"
2764 * w or W 0 " warning"
2765 * i or I 0 " info"
2766 * 0 0 ""
2767 * other 0 " c"
2768 * e or E n " error n"
2769 * w or W n " warning n"
2770 * i or I n " info n"
2771 * 0 n " error n"
2772 * other n " c n"
2773 * 1 x "" :helpgrep
2774 */
2775 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002776qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
2778 static char_u buf[20];
2779 static char_u cc[3];
2780 char_u *p;
2781
2782 if (c == 'W' || c == 'w')
2783 p = (char_u *)" warning";
2784 else if (c == 'I' || c == 'i')
2785 p = (char_u *)" info";
2786 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2787 p = (char_u *)" error";
2788 else if (c == 0 || c == 1)
2789 p = (char_u *)"";
2790 else
2791 {
2792 cc[0] = ' ';
2793 cc[1] = c;
2794 cc[2] = NUL;
2795 p = cc;
2796 }
2797
2798 if (nr <= 0)
2799 return p;
2800
2801 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2802 return buf;
2803}
2804
2805#if defined(FEAT_WINDOWS) || defined(PROTO)
2806/*
2807 * ":cwindow": open the quickfix window if we have errors to display,
2808 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002809 * ":lwindow": open the location list window if we have locations to display,
2810 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 */
2812 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002813ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002815 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 win_T *win;
2817
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002818 if (eap->cmdidx == CMD_lwindow)
2819 {
2820 qi = GET_LOC_LIST(curwin);
2821 if (qi == NULL)
2822 return;
2823 }
2824
2825 /* Look for an existing quickfix window. */
2826 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
2828 /*
2829 * If a quickfix window is open but we have no errors to display,
2830 * close the window. If a quickfix window is not open, then open
2831 * it if we have errors; otherwise, leave it closed.
2832 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002833 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002834 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002835 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 {
2837 if (win != NULL)
2838 ex_cclose(eap);
2839 }
2840 else if (win == NULL)
2841 ex_copen(eap);
2842}
2843
2844/*
2845 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002846 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002849ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002851 win_T *win = NULL;
2852 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002854 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2855 {
2856 qi = GET_LOC_LIST(curwin);
2857 if (qi == NULL)
2858 return;
2859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002861 /* Find existing quickfix window and close it. */
2862 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 if (win != NULL)
2864 win_close(win, FALSE);
2865}
2866
2867/*
2868 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002869 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 */
2871 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002872ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002874 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002877 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002878 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002879 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002881 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2882 {
2883 qi = GET_LOC_LIST(curwin);
2884 if (qi == NULL)
2885 {
2886 EMSG(_(e_loclist));
2887 return;
2888 }
2889 }
2890
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 if (eap->addr_count != 0)
2892 height = eap->line2;
2893 else
2894 height = QF_WINHEIGHT;
2895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#ifdef FEAT_GUI
2898 need_mouse_correct = TRUE;
2899#endif
2900
2901 /*
2902 * Find existing quickfix window, or open a new one.
2903 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002904 win = qf_find_win(qi);
2905
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002906 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002909 if (eap->addr_count != 0)
2910 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002911 if (cmdmod.split & WSP_VERT)
2912 {
2913 if (height != W_WIDTH(win))
2914 win_setwidth(height);
2915 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002916 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002917 win_setheight(height);
2918 }
2919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 else
2921 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002922 qf_buf = qf_find_buf(qi);
2923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 /* The current window becomes the previous window afterwards. */
2925 win = curwin;
2926
Bram Moolenaar77642c02012-11-20 17:55:10 +01002927 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2928 && cmdmod.split == 0)
2929 /* Create the new window at the very bottom, except when
2930 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002931 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002932 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002934 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002936 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002938 /*
2939 * For the location list window, create a reference to the
2940 * location list from the window 'win'.
2941 */
2942 curwin->w_llist_ref = win->w_llist;
2943 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002945
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002946 if (oldwin != curwin)
2947 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002948 if (qf_buf != NULL)
2949 /* Use the existing quickfix buffer */
2950 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002951 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002952 else
2953 {
2954 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002955 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002956 /* switch off 'swapfile' */
2957 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2958 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002959 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002960 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002961 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002962#ifdef FEAT_DIFF
2963 curwin->w_p_diff = FALSE;
2964#endif
2965#ifdef FEAT_FOLDING
2966 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2967 OPT_LOCAL);
2968#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002971 /* Only set the height when still in the same tab page and there is no
2972 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002973 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 win_setheight(height);
2975 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2976 if (win_valid(win))
2977 prevwin = win;
2978 }
2979
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002980 qf_set_title_var(qi);
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 /*
2983 * Fill the buffer with the quickfix list.
2984 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002985 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002987 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 curwin->w_cursor.col = 0;
2989 check_cursor();
2990 update_topline(); /* scroll to show the line */
2991}
2992
2993/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02002994 * Move the cursor in the quickfix window to "lnum".
2995 */
2996 static void
2997qf_win_goto(win_T *win, linenr_T lnum)
2998{
2999 win_T *old_curwin = curwin;
3000
3001 curwin = win;
3002 curbuf = win->w_buffer;
3003 curwin->w_cursor.lnum = lnum;
3004 curwin->w_cursor.col = 0;
3005#ifdef FEAT_VIRTUALEDIT
3006 curwin->w_cursor.coladd = 0;
3007#endif
3008 curwin->w_curswant = 0;
3009 update_topline(); /* scroll to show the line */
3010 redraw_later(VALID);
3011 curwin->w_redr_status = TRUE; /* update ruler */
3012 curwin = old_curwin;
3013 curbuf = curwin->w_buffer;
3014}
3015
3016/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003017 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003018 */
3019 void
3020ex_cbottom(exarg_T *eap UNUSED)
3021{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003022 qf_info_T *qi = &ql_info;
3023 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003024
Bram Moolenaar537ef082016-07-09 17:56:19 +02003025 if (eap->cmdidx == CMD_lbottom)
3026 {
3027 qi = GET_LOC_LIST(curwin);
3028 if (qi == NULL)
3029 {
3030 EMSG(_(e_loclist));
3031 return;
3032 }
3033 }
3034
3035 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003036 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3037 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3038}
3039
3040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 * Return the number of the current entry (line number in the quickfix
3042 * window).
3043 */
3044 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003045qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003047 qf_info_T *qi = &ql_info;
3048
3049 if (IS_LL_WINDOW(wp))
3050 /* In the location list window, use the referenced location list */
3051 qi = wp->w_llist_ref;
3052
3053 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054}
3055
3056/*
3057 * Update the cursor position in the quickfix window to the current error.
3058 * Return TRUE if there is a quickfix window.
3059 */
3060 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003061qf_win_pos_update(
3062 qf_info_T *qi,
3063 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003066 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
3068 /*
3069 * Put the cursor on the current error in the quickfix window, so that
3070 * it's viewable.
3071 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003072 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 if (win != NULL
3074 && qf_index <= win->w_buffer->b_ml.ml_line_count
3075 && old_qf_index != qf_index)
3076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 if (qf_index > old_qf_index)
3078 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003079 win->w_redraw_top = old_qf_index;
3080 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 }
3082 else
3083 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003084 win->w_redraw_top = qf_index;
3085 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003087 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 }
3089 return win != NULL;
3090}
3091
3092/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003093 * Check whether the given window is displaying the specified quickfix/location
3094 * list buffer
3095 */
3096 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003097is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003098{
3099 /*
3100 * A window displaying the quickfix buffer will have the w_llist_ref field
3101 * set to NULL.
3102 * A window displaying a location list buffer will have the w_llist_ref
3103 * pointing to the location list.
3104 */
3105 if (bt_quickfix(win->w_buffer))
3106 if ((qi == &ql_info && win->w_llist_ref == NULL)
3107 || (qi != &ql_info && win->w_llist_ref == qi))
3108 return TRUE;
3109
3110 return FALSE;
3111}
3112
3113/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003114 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003115 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003116 */
3117 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003118qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003119{
3120 win_T *win;
3121
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003122 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003123 if (is_qf_win(win, qi))
3124 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003125
3126 return win;
3127}
3128
3129/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003130 * Find a quickfix buffer.
3131 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 */
3133 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003134qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003136 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003137 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
Bram Moolenaar9c102382006-05-03 21:26:49 +00003139 FOR_ALL_TAB_WINDOWS(tp, win)
3140 if (is_qf_win(win, qi))
3141 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003142
Bram Moolenaar9c102382006-05-03 21:26:49 +00003143 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144}
3145
3146/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003147 * Update the w:quickfix_title variable in the quickfix/location list window
3148 */
3149 static void
3150qf_update_win_titlevar(qf_info_T *qi)
3151{
3152 win_T *win;
3153 win_T *curwin_save;
3154
3155 if ((win = qf_find_win(qi)) != NULL)
3156 {
3157 curwin_save = curwin;
3158 curwin = win;
3159 qf_set_title_var(qi);
3160 curwin = curwin_save;
3161 }
3162}
3163
3164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 * Find the quickfix buffer. If it exists, update the contents.
3166 */
3167 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003168qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
3170 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003171 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
3174 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003175 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 if (buf != NULL)
3177 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003178 linenr_T old_line_count = buf->b_ml.ml_line_count;
3179
3180 if (old_last == NULL)
3181 /* set curwin/curbuf to buf and save a few things */
3182 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaard823fa92016-08-12 16:29:27 +02003184 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003185
Bram Moolenaar864293a2016-06-02 13:40:04 +02003186 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003187
Bram Moolenaar864293a2016-06-02 13:40:04 +02003188 if (old_last == NULL)
3189 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003190 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003191
3192 /* restore curwin/curbuf and a few other things */
3193 aucmd_restbuf(&aco);
3194 }
3195
3196 /* Only redraw when added lines are visible. This avoids flickering
3197 * when the added lines are not visible. */
3198 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3199 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201}
3202
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003203/*
3204 * Set "w:quickfix_title" if "qi" has a title.
3205 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003206 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003207qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003208{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003209 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3210 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003211 qi->qf_lists[qi->qf_curlist].qf_title);
3212}
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214/*
3215 * Fill current buffer with quickfix errors, replacing any previous contents.
3216 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003217 * If "old_last" is not NULL append the items after this one.
3218 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3219 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 */
3221 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003222qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003224 linenr_T lnum;
3225 qfline_T *qfp;
3226 buf_T *errbuf;
3227 int len;
3228 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaar864293a2016-06-02 13:40:04 +02003230 if (old_last == NULL)
3231 {
3232 if (buf != curbuf)
3233 {
3234 EMSG2(_(e_intern2), "qf_fill_buffer()");
3235 return;
3236 }
3237
3238 /* delete all existing lines */
3239 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3240 (void)ml_delete((linenr_T)1, FALSE);
3241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
3243 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003244 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 {
3246 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003247 if (old_last == NULL)
3248 {
3249 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3250 lnum = 0;
3251 }
3252 else
3253 {
3254 qfp = old_last->qf_next;
3255 lnum = buf->b_ml.ml_line_count;
3256 }
3257 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
3259 if (qfp->qf_fnum != 0
3260 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3261 && errbuf->b_fname != NULL)
3262 {
3263 if (qfp->qf_type == 1) /* :helpgrep */
3264 STRCPY(IObuff, gettail(errbuf->b_fname));
3265 else
3266 STRCPY(IObuff, errbuf->b_fname);
3267 len = (int)STRLEN(IObuff);
3268 }
3269 else
3270 len = 0;
3271 IObuff[len++] = '|';
3272
3273 if (qfp->qf_lnum > 0)
3274 {
3275 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3276 len += (int)STRLEN(IObuff + len);
3277
3278 if (qfp->qf_col > 0)
3279 {
3280 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3281 len += (int)STRLEN(IObuff + len);
3282 }
3283
3284 sprintf((char *)IObuff + len, "%s",
3285 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3286 len += (int)STRLEN(IObuff + len);
3287 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003288 else if (qfp->qf_pattern != NULL)
3289 {
3290 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3291 len += (int)STRLEN(IObuff + len);
3292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 IObuff[len++] = '|';
3294 IObuff[len++] = ' ';
3295
3296 /* Remove newlines and leading whitespace from the text.
3297 * For an unrecognized line keep the indent, the compiler may
3298 * mark a word with ^^^^. */
3299 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3300 IObuff + len, IOSIZE - len);
3301
Bram Moolenaar864293a2016-06-02 13:40:04 +02003302 if (ml_append_buf(buf, lnum, IObuff,
3303 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003305 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003307 if (qfp == NULL)
3308 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003310
3311 if (old_last == NULL)
3312 /* Delete the empty line which is now at the end */
3313 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315
3316 /* correct cursor position */
3317 check_lnums(TRUE);
3318
Bram Moolenaar864293a2016-06-02 13:40:04 +02003319 if (old_last == NULL)
3320 {
3321 /* Set the 'filetype' to "qf" each time after filling the buffer.
3322 * This resembles reading a file into a buffer, it's more logical when
3323 * using autocommands. */
3324 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3325 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
3327#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003328 keep_filetype = TRUE; /* don't detect 'filetype' */
3329 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003331 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003333 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003335 /* make sure it will be redrawn */
3336 redraw_curbuf_later(NOT_VALID);
3337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
3339 /* Restore KeyTyped, setting 'filetype' may reset it. */
3340 KeyTyped = old_KeyTyped;
3341}
3342
3343#endif /* FEAT_WINDOWS */
3344
3345/*
3346 * Return TRUE if "buf" is the quickfix buffer.
3347 */
3348 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003349bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003351 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352}
3353
3354/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003355 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3356 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 */
3358 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003359bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003361 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3362 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363}
3364
3365/*
3366 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3367 */
3368 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003369bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003371 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372}
3373
3374 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003375bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
3377 if (bt_dontwrite(buf))
3378 {
3379 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3380 return TRUE;
3381 }
3382 return FALSE;
3383}
3384
3385/*
3386 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3387 * and 'bufhidden'.
3388 */
3389 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003390buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3393 switch (buf->b_p_bh[0])
3394 {
3395 case 'u': /* "unload" */
3396 case 'w': /* "wipe" */
3397 case 'd': return FALSE; /* "delete" */
3398 case 'h': return TRUE; /* "hide" */
3399 }
3400 return (p_hid || cmdmod.hide);
3401}
3402
3403/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003404 * Return TRUE when using ":vimgrep" for ":grep".
3405 */
3406 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003407grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003408{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003409 return ((cmdidx == CMD_grep
3410 || cmdidx == CMD_lgrep
3411 || cmdidx == CMD_grepadd
3412 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003413 && STRCMP("internal",
3414 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3415}
3416
3417/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003418 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 */
3420 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003421ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003423 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 char_u *cmd;
3425 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003426 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003427 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003428 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003429#ifdef FEAT_AUTOCMD
3430 char_u *au_name = NULL;
3431
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003432 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3433 if (grep_internal(eap->cmdidx))
3434 {
3435 ex_vimgrep(eap);
3436 return;
3437 }
3438
Bram Moolenaar7c626922005-02-07 22:01:03 +00003439 switch (eap->cmdidx)
3440 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003441 case CMD_make: au_name = (char_u *)"make"; break;
3442 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3443 case CMD_grep: au_name = (char_u *)"grep"; break;
3444 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3445 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3446 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003447 default: break;
3448 }
3449 if (au_name != NULL)
3450 {
3451 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3452 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003453# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003454 if (did_throw || force_abort)
3455 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003456# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003457 }
3458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaara6557602006-02-04 22:43:20 +00003460 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3461 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003462 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003463
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003465 fname = get_mef_name();
3466 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003468 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
3470 /*
3471 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3472 */
3473 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3474 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003475 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 cmd = alloc(len);
3477 if (cmd == NULL)
3478 return;
3479 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3480 (char *)p_shq);
3481 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003482 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 /*
3484 * Output a newline if there's something else than the :make command that
3485 * was typed (in which case the cursor is in column 0).
3486 */
3487 if (msg_col == 0)
3488 msg_didout = FALSE;
3489 msg_start();
3490 MSG_PUTS(":!");
3491 msg_outtrans(cmd); /* show what we are doing */
3492
3493 /* let the shell know if we are redirecting output or not */
3494 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3495
3496#ifdef AMIGA
3497 out_flush();
3498 /* read window status report and redraw before message */
3499 (void)char_avail();
3500#endif
3501
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003502 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003503 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3504 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003505 && eap->cmdidx != CMD_lgrepadd),
3506 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003507 if (wp != NULL)
3508 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003509#ifdef FEAT_AUTOCMD
3510 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003511 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003512 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3513 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003514 if (qi->qf_curlist < qi->qf_listcount)
3515 res = qi->qf_lists[qi->qf_curlist].qf_count;
3516 else
3517 res = 0;
3518 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003519#endif
3520 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003521 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
Bram Moolenaar7c626922005-02-07 22:01:03 +00003523 mch_remove(fname);
3524 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 vim_free(cmd);
3526}
3527
3528/*
3529 * Return the name for the errorfile, in allocated memory.
3530 * Find a new unique name when 'makeef' contains "##".
3531 * Returns NULL for error.
3532 */
3533 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003534get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535{
3536 char_u *p;
3537 char_u *name;
3538 static int start = -1;
3539 static int off = 0;
3540#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003541 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#endif
3543
3544 if (*p_mef == NUL)
3545 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003546 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (name == NULL)
3548 EMSG(_(e_notmp));
3549 return name;
3550 }
3551
3552 for (p = p_mef; *p; ++p)
3553 if (p[0] == '#' && p[1] == '#')
3554 break;
3555
3556 if (*p == NUL)
3557 return vim_strsave(p_mef);
3558
3559 /* Keep trying until the name doesn't exist yet. */
3560 for (;;)
3561 {
3562 if (start == -1)
3563 start = mch_get_pid();
3564 else
3565 off += 19;
3566
3567 name = alloc((unsigned)STRLEN(p_mef) + 30);
3568 if (name == NULL)
3569 break;
3570 STRCPY(name, p_mef);
3571 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3572 STRCAT(name, p + 2);
3573 if (mch_getperm(name) < 0
3574#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003575 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 && mch_lstat((char *)name, &sb) < 0
3577#endif
3578 )
3579 break;
3580 vim_free(name);
3581 }
3582 return name;
3583}
3584
3585/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003586 * Returns the number of valid entries in the current quickfix/location list.
3587 */
3588 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003589qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003590{
3591 qf_info_T *qi = &ql_info;
3592 qfline_T *qfp;
3593 int i, sz = 0;
3594 int prev_fnum = 0;
3595
3596 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3597 {
3598 /* Location list */
3599 qi = GET_LOC_LIST(curwin);
3600 if (qi == NULL)
3601 return 0;
3602 }
3603
3604 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003605 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003606 ++i, qfp = qfp->qf_next)
3607 {
3608 if (qfp->qf_valid)
3609 {
3610 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3611 sz++; /* Count all valid entries */
3612 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3613 {
3614 /* Count the number of files */
3615 sz++;
3616 prev_fnum = qfp->qf_fnum;
3617 }
3618 }
3619 }
3620
3621 return sz;
3622}
3623
3624/*
3625 * Returns the current index of the quickfix/location list.
3626 * Returns 0 if there is an error.
3627 */
3628 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003629qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003630{
3631 qf_info_T *qi = &ql_info;
3632
3633 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3634 {
3635 /* Location list */
3636 qi = GET_LOC_LIST(curwin);
3637 if (qi == NULL)
3638 return 0;
3639 }
3640
3641 return qi->qf_lists[qi->qf_curlist].qf_index;
3642}
3643
3644/*
3645 * Returns the current index in the quickfix/location list (counting only valid
3646 * entries). If no valid entries are in the list, then returns 1.
3647 */
3648 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003649qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003650{
3651 qf_info_T *qi = &ql_info;
3652 qf_list_T *qfl;
3653 qfline_T *qfp;
3654 int i, eidx = 0;
3655 int prev_fnum = 0;
3656
3657 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3658 {
3659 /* Location list */
3660 qi = GET_LOC_LIST(curwin);
3661 if (qi == NULL)
3662 return 1;
3663 }
3664
3665 qfl = &qi->qf_lists[qi->qf_curlist];
3666 qfp = qfl->qf_start;
3667
3668 /* check if the list has valid errors */
3669 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3670 return 1;
3671
3672 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3673 {
3674 if (qfp->qf_valid)
3675 {
3676 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3677 {
3678 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3679 {
3680 /* Count the number of files */
3681 eidx++;
3682 prev_fnum = qfp->qf_fnum;
3683 }
3684 }
3685 else
3686 eidx++;
3687 }
3688 }
3689
3690 return eidx ? eidx : 1;
3691}
3692
3693/*
3694 * Get the 'n'th valid error entry in the quickfix or location list.
3695 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3696 * For :cdo and :ldo returns the 'n'th valid error entry.
3697 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3698 */
3699 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003700qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003701{
3702 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3703 qfline_T *qfp = qfl->qf_start;
3704 int i, eidx;
3705 int prev_fnum = 0;
3706
3707 /* check if the list has valid errors */
3708 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3709 return 1;
3710
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003711 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003712 i++, qfp = qfp->qf_next)
3713 {
3714 if (qfp->qf_valid)
3715 {
3716 if (fdo)
3717 {
3718 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3719 {
3720 /* Count the number of files */
3721 eidx++;
3722 prev_fnum = qfp->qf_fnum;
3723 }
3724 }
3725 else
3726 eidx++;
3727 }
3728
3729 if (eidx == n)
3730 break;
3731 }
3732
3733 if (i <= qfl->qf_count)
3734 return i;
3735 else
3736 return 1;
3737}
3738
3739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003741 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003742 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 */
3744 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003745ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003747 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003748 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003749
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003750 if (eap->cmdidx == CMD_ll
3751 || eap->cmdidx == CMD_lrewind
3752 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003753 || eap->cmdidx == CMD_llast
3754 || eap->cmdidx == CMD_ldo
3755 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003756 {
3757 qi = GET_LOC_LIST(curwin);
3758 if (qi == NULL)
3759 {
3760 EMSG(_(e_loclist));
3761 return;
3762 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003763 }
3764
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003765 if (eap->addr_count > 0)
3766 errornr = (int)eap->line2;
3767 else
3768 {
3769 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3770 errornr = 0;
3771 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3772 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3773 errornr = 1;
3774 else
3775 errornr = 32767;
3776 }
3777
3778 /* For cdo and ldo commands, jump to the nth valid error.
3779 * For cfdo and lfdo commands, jump to the nth valid file entry.
3780 */
3781 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3782 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3783 errornr = qf_get_nth_valid_entry(qi,
3784 eap->addr_count > 0 ? (int)eap->line1 : 1,
3785 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3786
3787 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788}
3789
3790/*
3791 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003792 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003793 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
3795 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003796ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003798 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003799 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003800
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003801 if (eap->cmdidx == CMD_lnext
3802 || eap->cmdidx == CMD_lNext
3803 || eap->cmdidx == CMD_lprevious
3804 || eap->cmdidx == CMD_lnfile
3805 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003806 || eap->cmdidx == CMD_lpfile
3807 || eap->cmdidx == CMD_ldo
3808 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003809 {
3810 qi = GET_LOC_LIST(curwin);
3811 if (qi == NULL)
3812 {
3813 EMSG(_(e_loclist));
3814 return;
3815 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003816 }
3817
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003818 if (eap->addr_count > 0 &&
3819 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3820 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3821 errornr = (int)eap->line2;
3822 else
3823 errornr = 1;
3824
3825 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3826 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003828 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3829 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003831 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3832 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 ? BACKWARD_FILE
3834 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003835 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836}
3837
3838/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003839 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003840 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 */
3842 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003843ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003845 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003846 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003847#ifdef FEAT_AUTOCMD
3848 char_u *au_name = NULL;
3849#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003850
3851 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003852 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003853 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003854
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003855#ifdef FEAT_AUTOCMD
3856 switch (eap->cmdidx)
3857 {
3858 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3859 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3860 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3861 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3862 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3863 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3864 default: break;
3865 }
3866 if (au_name != NULL)
3867 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3868#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003869#ifdef FEAT_BROWSE
3870 if (cmdmod.browse)
3871 {
3872 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3873 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3874 if (browse_file == NULL)
3875 return;
3876 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3877 vim_free(browse_file);
3878 }
3879 else
3880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003882 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003883
3884 /*
3885 * This function is used by the :cfile, :cgetfile and :caddfile
3886 * commands.
3887 * :cfile always creates a new quickfix list and jumps to the
3888 * first error.
3889 * :cgetfile creates a new quickfix list but doesn't jump to the
3890 * first error.
3891 * :caddfile adds to an existing quickfix list. If there is no
3892 * quickfix list then a new list is created.
3893 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003894 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003895 && eap->cmdidx != CMD_laddfile),
3896 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003897 && (eap->cmdidx == CMD_cfile
3898 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003899 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003900#ifdef FEAT_AUTOCMD
3901 if (au_name != NULL)
3902 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3903#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003904 if (wp != NULL)
3905 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003906 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003907 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003908
3909 else
3910 {
3911#ifdef FEAT_AUTOCMD
3912 if (au_name != NULL)
3913 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3914#endif
3915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916}
3917
3918/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003919 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003920 * ":vimgrepadd {pattern} file(s)"
3921 * ":lvimgrep {pattern} file(s)"
3922 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003923 */
3924 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003925ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003926{
Bram Moolenaar81695252004-12-29 20:58:21 +00003927 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003928 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003929 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003930 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003931 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003932 char_u *s;
3933 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003934 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003935 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003936#ifdef FEAT_AUTOCMD
3937 qfline_T *cur_qf_start;
3938#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003939 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003940 buf_T *buf;
3941 int duplicate_name = FALSE;
3942 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003943 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003944 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003945 buf_T *first_match_buf = NULL;
3946 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003947 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003948#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3949 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003950#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003951 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003952 int flags = 0;
3953 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003954 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003955 char_u *dirname_start = NULL;
3956 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003957 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003958#ifdef FEAT_AUTOCMD
3959 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003960
3961 switch (eap->cmdidx)
3962 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003963 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3964 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3965 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003966 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003967 case CMD_grep: au_name = (char_u *)"grep"; break;
3968 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3969 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3970 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003971 default: break;
3972 }
3973 if (au_name != NULL)
3974 {
3975 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3976 curbuf->b_fname, TRUE, curbuf);
3977 if (did_throw || force_abort)
3978 return;
3979 }
3980#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003981
Bram Moolenaar754b5602006-02-09 23:53:20 +00003982 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003983 || eap->cmdidx == CMD_lvimgrep
3984 || eap->cmdidx == CMD_lgrepadd
3985 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003986 {
3987 qi = ll_get_or_alloc_list(curwin);
3988 if (qi == NULL)
3989 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003990 }
3991
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003992 if (eap->addr_count > 0)
3993 tomatch = eap->line2;
3994 else
3995 tomatch = MAXLNUM;
3996
Bram Moolenaar81695252004-12-29 20:58:21 +00003997 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003998 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003999 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004000 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004001 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004002 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004003 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004004 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004005 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004006
4007 if (s != NULL && *s == NUL)
4008 {
4009 /* Pattern is empty, use last search pattern. */
4010 if (last_search_pat() == NULL)
4011 {
4012 EMSG(_(e_noprevre));
4013 goto theend;
4014 }
4015 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4016 }
4017 else
4018 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4019
Bram Moolenaar86b68352004-12-27 21:59:20 +00004020 if (regmatch.regprog == NULL)
4021 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004022 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004023 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004024
4025 p = skipwhite(p);
4026 if (*p == NUL)
4027 {
4028 EMSG(_("E683: File name missing or invalid pattern"));
4029 goto theend;
4030 }
4031
Bram Moolenaar754b5602006-02-09 23:53:20 +00004032 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004033 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004034 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004035 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004036 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004037
4038 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004039 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004040 goto theend;
4041 if (fcount == 0)
4042 {
4043 EMSG(_(e_nomatch));
4044 goto theend;
4045 }
4046
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004047 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4048 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004049 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004050 {
4051 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004052 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004053 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004054
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004055 /* Remember the current directory, because a BufRead autocommand that does
4056 * ":lcd %:p:h" changes the meaning of short path names. */
4057 mch_dirname(dirname_start, MAXPATHL);
4058
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004059#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004060 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004061 * changing the current quickfix list. */
4062 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4063#endif
4064
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004065 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004066 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004067 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004068 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004069 if (time(NULL) > seconds)
4070 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004071 /* Display the file name every second or so, show the user we are
4072 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004073 seconds = time(NULL);
4074 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004075 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004076 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004077 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004078 else
4079 {
4080 msg_outtrans(p);
4081 vim_free(p);
4082 }
4083 msg_clr_eos();
4084 msg_didout = FALSE; /* overwrite this message */
4085 msg_nowait = TRUE; /* don't wait for this message */
4086 msg_col = 0;
4087 out_flush();
4088 }
4089
Bram Moolenaar81695252004-12-29 20:58:21 +00004090 buf = buflist_findname_exp(fnames[fi]);
4091 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4092 {
4093 /* Remember that a buffer with this name already exists. */
4094 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004095 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004096 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004097
4098#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4099 /* Don't do Filetype autocommands to avoid loading syntax and
4100 * indent scripts, a great speed improvement. */
4101 save_ei = au_event_disable(",Filetype");
4102#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004103 /* Don't use modelines here, it's useless. */
4104 save_mls = p_mls;
4105 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004106
4107 /* Load file into a buffer, so that 'fileencoding' is detected,
4108 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004109 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004110
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004111 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004112#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4113 au_event_restore(save_ei);
4114#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004115 }
4116 else
4117 /* Use existing, loaded buffer. */
4118 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004119
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004120#ifdef FEAT_AUTOCMD
4121 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4122 {
4123 int idx;
4124
4125 /* Autocommands changed the quickfix list. Find the one we were
4126 * using and restore it. */
4127 for (idx = 0; idx < LISTCOUNT; ++idx)
4128 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4129 {
4130 qi->qf_curlist = idx;
4131 break;
4132 }
4133 if (idx == LISTCOUNT)
4134 {
4135 /* List cannot be found, create a new one. */
4136 qf_new_list(qi, *eap->cmdlinep);
4137 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4138 }
4139 }
4140#endif
4141
Bram Moolenaar81695252004-12-29 20:58:21 +00004142 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004143 {
4144 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004145 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004146 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004147 else
4148 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004149 /* Try for a match in all lines of the buffer.
4150 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004151 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004152 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4153 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004154 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004155 col = 0;
4156 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004157 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004158 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004159 /* Pass the buffer number so that it gets used even for a
4160 * dummy buffer, unless duplicate_name is set, then the
4161 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004162 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004163 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004164 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004165 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004166 ml_get_buf(buf,
4167 regmatch.startpos[0].lnum + lnum, FALSE),
4168 regmatch.startpos[0].lnum + lnum,
4169 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004170 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004171 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004172 0, /* nr */
4173 0, /* type */
4174 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004175 ) == FAIL)
4176 {
4177 got_int = TRUE;
4178 break;
4179 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004180 found_match = TRUE;
4181 if (--tomatch == 0)
4182 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004183 if ((flags & VGR_GLOBAL) == 0
4184 || regmatch.endpos[0].lnum > 0)
4185 break;
4186 col = regmatch.endpos[0].col
4187 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004188 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004189 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004190 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004191 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004192 if (got_int)
4193 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004194 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004195#ifdef FEAT_AUTOCMD
4196 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4197#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004198
4199 if (using_dummy)
4200 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004201 if (found_match && first_match_buf == NULL)
4202 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004203 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004204 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004205 /* Never keep a dummy buffer if there is another buffer
4206 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004207 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004208 buf = NULL;
4209 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004210 else if (!cmdmod.hide
4211 || buf->b_p_bh[0] == 'u' /* "unload" */
4212 || buf->b_p_bh[0] == 'w' /* "wipe" */
4213 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004214 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004215 /* When no match was found we don't need to remember the
4216 * buffer, wipe it out. If there was a match and it
4217 * wasn't the first one or we won't jump there: only
4218 * unload the buffer.
4219 * Ignore 'hidden' here, because it may lead to having too
4220 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004221 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004222 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004223 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004224 buf = NULL;
4225 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004226 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004227 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004228 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004229 /* Keeping the buffer, remove the dummy flag. */
4230 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004231 buf = NULL;
4232 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004233 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004234
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004235 if (buf != NULL)
4236 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004237 /* Keeping the buffer, remove the dummy flag. */
4238 buf->b_flags &= ~BF_DUMMY;
4239
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004240 /* If the buffer is still loaded we need to use the
4241 * directory we jumped to below. */
4242 if (buf == first_match_buf
4243 && target_dir == NULL
4244 && STRCMP(dirname_start, dirname_now) != 0)
4245 target_dir = vim_strsave(dirname_now);
4246
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004247 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004248 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004249 * need to be done (again). But not the window-local
4250 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004251 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004252#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004253 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4254 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004255#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004256 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004257 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004258 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004259 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004260 }
4261 }
4262
4263 FreeWild(fcount, fnames);
4264
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004265 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4266 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4267 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004268
4269#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004270 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004271#endif
4272
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004273#ifdef FEAT_AUTOCMD
4274 if (au_name != NULL)
4275 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4276 curbuf->b_fname, TRUE, curbuf);
4277#endif
4278
Bram Moolenaar86b68352004-12-27 21:59:20 +00004279 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004280 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004281 {
4282 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004283 {
4284 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004285 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004286 if (buf != curbuf)
4287 /* If we jumped to another buffer redrawing will already be
4288 * taken care of. */
4289 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004290
4291 /* Jump to the directory used after loading the buffer. */
4292 if (curbuf == first_match_buf && target_dir != NULL)
4293 {
4294 exarg_T ea;
4295
4296 ea.arg = target_dir;
4297 ea.cmdidx = CMD_lcd;
4298 ex_cd(&ea);
4299 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004300 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004301 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004302 else
4303 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004304
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004305 /* If we loaded a dummy buffer into the current window, the autocommands
4306 * may have messed up things, need to redraw and recompute folds. */
4307 if (redraw_for_dummy)
4308 {
4309#ifdef FEAT_FOLDING
4310 foldUpdateAll(curwin);
4311#else
4312 redraw_later(NOT_VALID);
4313#endif
4314 }
4315
Bram Moolenaar86b68352004-12-27 21:59:20 +00004316theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004317 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004318 vim_free(dirname_now);
4319 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004320 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004321 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004322}
4323
4324/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004325 * Restore current working directory to "dirname_start" if they differ, taking
4326 * into account whether it is set locally or globally.
4327 */
4328 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004329restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004330{
4331 char_u *dirname_now = alloc(MAXPATHL);
4332
4333 if (NULL != dirname_now)
4334 {
4335 mch_dirname(dirname_now, MAXPATHL);
4336 if (STRCMP(dirname_start, dirname_now) != 0)
4337 {
4338 /* If the directory has changed, change it back by building up an
4339 * appropriate ex command and executing it. */
4340 exarg_T ea;
4341
4342 ea.arg = dirname_start;
4343 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4344 ex_cd(&ea);
4345 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004346 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004347 }
4348}
4349
4350/*
4351 * Load file "fname" into a dummy buffer and return the buffer pointer,
4352 * placing the directory resulting from the buffer load into the
4353 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4354 * prior to calling this function. Restores directory to "dirname_start" prior
4355 * to returning, if autocmds or the 'autochdir' option have changed it.
4356 *
4357 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4358 * or wipe_dummy_buffer() later!
4359 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004360 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004361 */
4362 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004363load_dummy_buffer(
4364 char_u *fname,
4365 char_u *dirname_start, /* in: old directory */
4366 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004367{
4368 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004369 bufref_T newbufref;
4370 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004371 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004372 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004373
4374 /* Allocate a buffer without putting it in the buffer list. */
4375 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4376 if (newbuf == NULL)
4377 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004378 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004379
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004380 /* Init the options. */
4381 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4382
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004383 /* need to open the memfile before putting the buffer in a window */
4384 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004385 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004386 /* set curwin/curbuf to buf and save a few things */
4387 aucmd_prepbuf(&aco, newbuf);
4388
4389 /* Need to set the filename for autocommands. */
4390 (void)setfname(curbuf, fname, NULL, FALSE);
4391
Bram Moolenaar81695252004-12-29 20:58:21 +00004392 /* Create swap file now to avoid the ATTENTION message. */
4393 check_need_swap(TRUE);
4394
4395 /* Remove the "dummy" flag, otherwise autocommands may not
4396 * work. */
4397 curbuf->b_flags &= ~BF_DUMMY;
4398
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004399 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004400 if (readfile(fname, NULL,
4401 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4402 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004403 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004404 && !(curbuf->b_flags & BF_NEW))
4405 {
4406 failed = FALSE;
4407 if (curbuf != newbuf)
4408 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004409 /* Bloody autocommands changed the buffer! Can happen when
4410 * using netrw and editing a remote file. Use the current
4411 * buffer instead, delete the dummy one after restoring the
4412 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004413 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004414 newbuf = curbuf;
4415 }
4416 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004417
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004418 /* restore curwin/curbuf and a few other things */
4419 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004420 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4421 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004422
4423 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4424 * skip it. */
4425 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004426 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004427
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004428 /*
4429 * When autocommands/'autochdir' option changed directory: go back.
4430 * Let the caller know what the resulting dir was first, in case it is
4431 * important.
4432 */
4433 mch_dirname(resulting_dir, MAXPATHL);
4434 restore_start_dir(dirname_start);
4435
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004436 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004437 return NULL;
4438 if (failed)
4439 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004440 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004441 return NULL;
4442 }
4443 return newbuf;
4444}
4445
4446/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004447 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4448 * directory to "dirname_start" prior to returning, if autocmds or the
4449 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004450 */
4451 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004452wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004453{
4454 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004455 {
4456#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4457 cleanup_T cs;
4458
4459 /* Reset the error/interrupt/exception state here so that aborting()
4460 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4461 * work when got_int is set. */
4462 enter_cleanup(&cs);
4463#endif
4464
Bram Moolenaar81695252004-12-29 20:58:21 +00004465 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004466
4467#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4468 /* Restore the error/interrupt/exception state if not discarded by a
4469 * new aborting error, interrupt, or uncaught exception. */
4470 leave_cleanup(&cs);
4471#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004472 /* When autocommands/'autochdir' option changed directory: go back. */
4473 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004474 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004475}
4476
4477/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004478 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4479 * directory to "dirname_start" prior to returning, if autocmds or the
4480 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004481 */
4482 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004483unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004484{
4485 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004486 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004487 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004488
4489 /* When autocommands/'autochdir' option changed directory: go back. */
4490 restore_start_dir(dirname_start);
4491 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004492}
4493
Bram Moolenaar05159a02005-02-26 23:04:13 +00004494#if defined(FEAT_EVAL) || defined(PROTO)
4495/*
4496 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004497 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004498 */
4499 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004500get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004501{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004502 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004503 dict_T *dict;
4504 char_u buf[2];
4505 qfline_T *qfp;
4506 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004507 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004508
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004509 if (wp != NULL)
4510 {
4511 qi = GET_LOC_LIST(wp);
4512 if (qi == NULL)
4513 return FAIL;
4514 }
4515
Bram Moolenaard823fa92016-08-12 16:29:27 +02004516 if (qf_idx == -1)
4517 qf_idx = qi->qf_curlist;
4518
4519 if (qf_idx >= qi->qf_listcount
4520 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004521 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004522
Bram Moolenaard823fa92016-08-12 16:29:27 +02004523 qfp = qi->qf_lists[qf_idx].qf_start;
4524 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004525 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004526 /* Handle entries with a non-existing buffer number. */
4527 bufnum = qfp->qf_fnum;
4528 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4529 bufnum = 0;
4530
Bram Moolenaar05159a02005-02-26 23:04:13 +00004531 if ((dict = dict_alloc()) == NULL)
4532 return FAIL;
4533 if (list_append_dict(list, dict) == FAIL)
4534 return FAIL;
4535
4536 buf[0] = qfp->qf_type;
4537 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004538 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004539 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4540 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4541 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4542 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004543 || dict_add_nr_str(dict, "pattern", 0L,
4544 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4545 || dict_add_nr_str(dict, "text", 0L,
4546 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004547 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4548 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4549 return FAIL;
4550
4551 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004552 if (qfp == NULL)
4553 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004554 }
4555 return OK;
4556}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004557
4558/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004559 * Flags used by getqflist()/getloclist() to determine which fields to return.
4560 */
4561enum {
4562 QF_GETLIST_NONE = 0x0,
4563 QF_GETLIST_TITLE = 0x1,
4564 QF_GETLIST_ITEMS = 0x2,
4565 QF_GETLIST_NR = 0x4,
4566 QF_GETLIST_WINID = 0x8,
4567 QF_GETLIST_ALL = 0xFF
4568};
4569
4570/*
4571 * Return quickfix/location list details (title) as a
4572 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4573 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004574 */
4575 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004576get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4577{
4578 qf_info_T *qi = &ql_info;
4579 int status = OK;
4580 int qf_idx;
4581 dictitem_T *di;
4582 int flags = QF_GETLIST_NONE;
4583
4584 if (wp != NULL)
4585 {
4586 qi = GET_LOC_LIST(wp);
4587 if (qi == NULL)
4588 return FAIL;
4589 }
4590
4591 qf_idx = qi->qf_curlist; /* default is the current list */
4592 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4593 {
4594 /* Use the specified quickfix/location list */
4595 if (di->di_tv.v_type == VAR_NUMBER)
4596 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004597 /* for zero use the current list */
4598 if (di->di_tv.vval.v_number != 0)
4599 {
4600 qf_idx = di->di_tv.vval.v_number - 1;
4601 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4602 return FAIL;
4603 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004604 flags |= QF_GETLIST_NR;
4605 }
4606 else
4607 return FAIL;
4608 }
4609
4610 if (dict_find(what, (char_u *)"all", -1) != NULL)
4611 flags |= QF_GETLIST_ALL;
4612
4613 if (dict_find(what, (char_u *)"title", -1) != NULL)
4614 flags |= QF_GETLIST_TITLE;
4615
4616 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4617 flags |= QF_GETLIST_WINID;
4618
4619 if (flags & QF_GETLIST_TITLE)
4620 {
4621 char_u *t;
4622 t = qi->qf_lists[qf_idx].qf_title;
4623 if (t == NULL)
4624 t = (char_u *)"";
4625 status = dict_add_nr_str(retdict, "title", 0L, t);
4626 }
4627 if ((status == OK) && (flags & QF_GETLIST_NR))
4628 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4629 if ((status == OK) && (flags & QF_GETLIST_WINID))
4630 {
4631 win_T *win;
4632 win = qf_find_win(qi);
4633 if (win != NULL)
4634 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4635 }
4636
4637 return status;
4638}
4639
4640/*
4641 * Add list of entries to quickfix/location list. Each list entry is
4642 * a dictionary with item information.
4643 */
4644 static int
4645qf_add_entries(
4646 qf_info_T *qi,
4647 list_T *list,
4648 char_u *title,
4649 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004650{
4651 listitem_T *li;
4652 dict_T *d;
4653 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004654 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004655 long lnum;
4656 int col, nr;
4657 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004658#ifdef FEAT_WINDOWS
4659 qfline_T *old_last = NULL;
4660#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004661 int valid, status;
4662 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004663 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004664
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004665 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004666 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004667 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004668#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004669 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4670 /* Adding to existing list, use last entry. */
4671 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004672#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004673 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004674 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004675 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004676 qf_store_title(qi, title);
4677 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004678
4679 for (li = list->lv_first; li != NULL; li = li->li_next)
4680 {
4681 if (li->li_tv.v_type != VAR_DICT)
4682 continue; /* Skip non-dict items */
4683
4684 d = li->li_tv.vval.v_dict;
4685 if (d == NULL)
4686 continue;
4687
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004688 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004689 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4690 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4691 col = (int)get_dict_number(d, (char_u *)"col");
4692 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4693 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004694 type = get_dict_string(d, (char_u *)"type", TRUE);
4695 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4696 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004697 if (text == NULL)
4698 text = vim_strsave((char_u *)"");
4699
4700 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004701 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004702 valid = FALSE;
4703
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004704 /* Mark entries with non-existing buffer number as not valid. Give the
4705 * error message only once. */
4706 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4707 {
4708 if (!did_bufnr_emsg)
4709 {
4710 did_bufnr_emsg = TRUE;
4711 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4712 }
4713 valid = FALSE;
4714 bufnum = 0;
4715 }
4716
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004717 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004718 NULL, /* dir */
4719 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004720 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004721 text,
4722 lnum,
4723 col,
4724 vcol, /* vis_col */
4725 pattern, /* search pattern */
4726 nr,
4727 type == NULL ? NUL : *type,
4728 valid);
4729
4730 vim_free(filename);
4731 vim_free(pattern);
4732 vim_free(text);
4733 vim_free(type);
4734
4735 if (status == FAIL)
4736 {
4737 retval = FAIL;
4738 break;
4739 }
4740 }
4741
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004742 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004743 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004744 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4745 else
4746 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004747 if (action != 'a') {
4748 qi->qf_lists[qi->qf_curlist].qf_ptr =
4749 qi->qf_lists[qi->qf_curlist].qf_start;
4750 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4751 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4752 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004753
4754#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004755 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004756 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004757#endif
4758
4759 return retval;
4760}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004761
4762 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004763qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004764{
4765 dictitem_T *di;
4766 int retval = FAIL;
4767 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004768 int newlist = FALSE;
4769
4770 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4771 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004772
4773 qf_idx = qi->qf_curlist; /* default is the current list */
4774 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4775 {
4776 /* Use the specified quickfix/location list */
4777 if (di->di_tv.v_type == VAR_NUMBER)
4778 {
4779 qf_idx = di->di_tv.vval.v_number - 1;
4780 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4781 return FAIL;
4782 }
4783 else
4784 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004785 newlist = FALSE; /* use the specified list */
4786 }
4787
4788 if (newlist)
4789 {
4790 qf_new_list(qi, NULL);
4791 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004792 }
4793
4794 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4795 {
4796 if (di->di_tv.v_type == VAR_STRING)
4797 {
4798 vim_free(qi->qf_lists[qf_idx].qf_title);
4799 qi->qf_lists[qf_idx].qf_title =
4800 get_dict_string(what, (char_u *)"title", TRUE);
4801 if (qf_idx == qi->qf_curlist)
4802 qf_update_win_titlevar(qi);
4803 retval = OK;
4804 }
4805 }
4806
4807 return retval;
4808}
4809
4810/*
4811 * Populate the quickfix list with the items supplied in the list
4812 * of dictionaries. "title" will be copied to w:quickfix_title.
4813 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
4814 */
4815 int
4816set_errorlist(
4817 win_T *wp,
4818 list_T *list,
4819 int action,
4820 char_u *title,
4821 dict_T *what)
4822{
4823 qf_info_T *qi = &ql_info;
4824 int retval = OK;
4825
4826 if (wp != NULL)
4827 {
4828 qi = ll_get_or_alloc_list(wp);
4829 if (qi == NULL)
4830 return FAIL;
4831 }
4832
4833 if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004834 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004835 else
4836 retval = qf_add_entries(qi, list, title, action);
4837
4838 return retval;
4839}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004840#endif
4841
Bram Moolenaar81695252004-12-29 20:58:21 +00004842/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004843 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004844 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004845 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004846 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004847 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004848 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004849 */
4850 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004851ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004852{
4853 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004854 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004855#ifdef FEAT_AUTOCMD
4856 char_u *au_name = NULL;
4857#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004858
Bram Moolenaardb552d602006-03-23 22:59:57 +00004859 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4860 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004861 {
4862 qi = ll_get_or_alloc_list(curwin);
4863 if (qi == NULL)
4864 return;
4865 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004866
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004867#ifdef FEAT_AUTOCMD
4868 switch (eap->cmdidx)
4869 {
4870 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
4871 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
4872 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
4873 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
4874 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
4875 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
4876 default: break;
4877 }
4878 if (au_name != NULL)
4879 {
4880 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4881 curbuf->b_fname, TRUE, curbuf);
4882# ifdef FEAT_EVAL
4883 if (did_throw || force_abort)
4884 return;
4885# endif
4886 }
4887#endif
4888
Bram Moolenaar86b68352004-12-27 21:59:20 +00004889 if (*eap->arg == NUL)
4890 buf = curbuf;
4891 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4892 buf = buflist_findnr(atoi((char *)eap->arg));
4893 if (buf == NULL)
4894 EMSG(_(e_invarg));
4895 else if (buf->b_ml.ml_mfp == NULL)
4896 EMSG(_("E681: Buffer is not loaded"));
4897 else
4898 {
4899 if (eap->addr_count == 0)
4900 {
4901 eap->line1 = 1;
4902 eap->line2 = buf->b_ml.ml_line_count;
4903 }
4904 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4905 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4906 EMSG(_(e_invrange));
4907 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004908 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004909 char_u *qf_title = *eap->cmdlinep;
4910
4911 if (buf->b_sfname)
4912 {
4913 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4914 (char *)qf_title, (char *)buf->b_sfname);
4915 qf_title = IObuff;
4916 }
4917
Bram Moolenaardb552d602006-03-23 22:59:57 +00004918 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4919 (eap->cmdidx != CMD_caddbuffer
4920 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004921 eap->line1, eap->line2,
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004922 qf_title) > 0)
4923 {
4924#ifdef FEAT_AUTOCMD
4925 if (au_name != NULL)
4926 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4927 curbuf->b_fname, TRUE, curbuf);
4928#endif
4929 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
4930 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4931 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00004932 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004933 }
4934}
4935
Bram Moolenaar1e015462005-09-25 22:16:38 +00004936#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004937/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004938 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4939 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004940 */
4941 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004942ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004943{
4944 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004945 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004946#ifdef FEAT_AUTOCMD
4947 char_u *au_name = NULL;
4948#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004949
Bram Moolenaardb552d602006-03-23 22:59:57 +00004950 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4951 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004952 {
4953 qi = ll_get_or_alloc_list(curwin);
4954 if (qi == NULL)
4955 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004956 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004957
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004958#ifdef FEAT_AUTOCMD
4959 switch (eap->cmdidx)
4960 {
4961 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
4962 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
4963 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
4964 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
4965 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
4966 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
4967 default: break;
4968 }
4969 if (au_name != NULL)
4970 {
4971 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4972 curbuf->b_fname, TRUE, curbuf);
4973# ifdef FEAT_EVAL
4974 if (did_throw || force_abort)
4975 return;
4976# endif
4977 }
4978#endif
4979
Bram Moolenaar4770d092006-01-12 23:22:24 +00004980 /* Evaluate the expression. When the result is a string or a list we can
4981 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004982 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004983 if (tv != NULL)
4984 {
4985 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4986 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4987 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004988 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004989 (eap->cmdidx != CMD_caddexpr
4990 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02004991 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0)
4992 {
4993#ifdef FEAT_AUTOCMD
4994 if (au_name != NULL)
4995 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4996 curbuf->b_fname, TRUE, curbuf);
4997#endif
4998 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
4999 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5000 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005001 }
5002 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005003 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005004 free_tv(tv);
5005 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005006}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005007#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005008
5009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 * ":helpgrep {pattern}"
5011 */
5012 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005013ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
5015 regmatch_T regmatch;
5016 char_u *save_cpo;
5017 char_u *p;
5018 int fcount;
5019 char_u **fnames;
5020 FILE *fd;
5021 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005023#ifdef FEAT_MULTI_LANG
5024 char_u *lang;
5025#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005026 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005027 int new_qi = FALSE;
5028 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005029#ifdef FEAT_AUTOCMD
5030 char_u *au_name = NULL;
5031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005033#ifdef FEAT_MULTI_LANG
5034 /* Check for a specified language */
5035 lang = check_help_lang(eap->arg);
5036#endif
5037
Bram Moolenaar73633f82012-01-20 13:39:07 +01005038#ifdef FEAT_AUTOCMD
5039 switch (eap->cmdidx)
5040 {
5041 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5042 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5043 default: break;
5044 }
5045 if (au_name != NULL)
5046 {
5047 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5048 curbuf->b_fname, TRUE, curbuf);
5049 if (did_throw || force_abort)
5050 return;
5051 }
5052#endif
5053
5054 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5055 save_cpo = p_cpo;
5056 p_cpo = empty_option;
5057
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005058 if (eap->cmdidx == CMD_lhelpgrep)
5059 {
5060 /* Find an existing help window */
5061 FOR_ALL_WINDOWS(wp)
5062 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5063 break;
5064
5065 if (wp == NULL) /* Help window not found */
5066 qi = NULL;
5067 else
5068 qi = wp->w_llist;
5069
5070 if (qi == NULL)
5071 {
5072 /* Allocate a new location list for help text matches */
5073 if ((qi = ll_new_list()) == NULL)
5074 return;
5075 new_qi = TRUE;
5076 }
5077 }
5078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5080 regmatch.rm_ic = FALSE;
5081 if (regmatch.regprog != NULL)
5082 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005083#ifdef FEAT_MBYTE
5084 vimconv_T vc;
5085
5086 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5087 * differs. */
5088 vc.vc_type = CONV_NONE;
5089 if (!enc_utf8)
5090 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5091#endif
5092
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005094 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
5096 /* Go through all directories in 'runtimepath' */
5097 p = p_rtp;
5098 while (*p != NUL && !got_int)
5099 {
5100 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5101
5102 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5103 add_pathsep(NameBuff);
5104 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5105 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5106 &fnames, EW_FILE|EW_SILENT) == OK
5107 && fcount > 0)
5108 {
5109 for (fi = 0; fi < fcount && !got_int; ++fi)
5110 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005111#ifdef FEAT_MULTI_LANG
5112 /* Skip files for a different language. */
5113 if (lang != NULL
5114 && STRNICMP(lang, fnames[fi]
5115 + STRLEN(fnames[fi]) - 3, 2) != 0
5116 && !(STRNICMP(lang, "en", 2) == 0
5117 && STRNICMP("txt", fnames[fi]
5118 + STRLEN(fnames[fi]) - 3, 3) == 0))
5119 continue;
5120#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005121 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (fd != NULL)
5123 {
5124 lnum = 1;
5125 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5126 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005127 char_u *line = IObuff;
5128#ifdef FEAT_MBYTE
5129 /* Convert a line if 'encoding' is not utf-8 and
5130 * the line contains a non-ASCII character. */
5131 if (vc.vc_type != CONV_NONE
5132 && has_non_ascii(IObuff)) {
5133 line = string_convert(&vc, IObuff, NULL);
5134 if (line == NULL)
5135 line = IObuff;
5136 }
5137#endif
5138
5139 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005141 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005144 while (l > 0 && line[l - 1] <= ' ')
5145 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005147 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 NULL, /* dir */
5149 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005150 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005151 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005153 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005154 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005155 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005156 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 0, /* nr */
5158 1, /* type */
5159 TRUE /* valid */
5160 ) == FAIL)
5161 {
5162 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005163#ifdef FEAT_MBYTE
5164 if (line != IObuff)
5165 vim_free(line);
5166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 break;
5168 }
5169 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005170#ifdef FEAT_MBYTE
5171 if (line != IObuff)
5172 vim_free(line);
5173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 ++lnum;
5175 line_breakcheck();
5176 }
5177 fclose(fd);
5178 }
5179 }
5180 FreeWild(fcount, fnames);
5181 }
5182 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005183
Bram Moolenaar473de612013-06-08 18:19:48 +02005184 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005185#ifdef FEAT_MBYTE
5186 if (vc.vc_type != CONV_NONE)
5187 convert_setup(&vc, NULL, NULL);
5188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005190 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5191 qi->qf_lists[qi->qf_curlist].qf_ptr =
5192 qi->qf_lists[qi->qf_curlist].qf_start;
5193 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005196 if (p_cpo == empty_option)
5197 p_cpo = save_cpo;
5198 else
5199 /* Darn, some plugin changed the value. */
5200 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201
5202#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005203 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204#endif
5205
Bram Moolenaar73633f82012-01-20 13:39:07 +01005206#ifdef FEAT_AUTOCMD
5207 if (au_name != NULL)
5208 {
5209 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5210 curbuf->b_fname, TRUE, curbuf);
5211 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
5212 /* autocommands made "qi" invalid */
5213 return;
5214 }
5215#endif
5216
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005218 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005219 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005220 else
5221 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005222
5223 if (eap->cmdidx == CMD_lhelpgrep)
5224 {
5225 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005226 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005227 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5228 {
5229 if (new_qi)
5230 ll_free_all(&qi);
5231 }
5232 else if (curwin->w_llist == NULL)
5233 curwin->w_llist = qi;
5234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235}
5236
5237#endif /* FEAT_QUICKFIX */