blob: aa94ae69a3ee656acd9d25bb27eadb7b5bc7e5d7 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaard12f5c12006-01-25 22:10:52 +000050typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000051{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000052 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020053 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_ptr; /* pointer to the current error */
55 int qf_count; /* number of errors (0 means no error list) */
56 int qf_index; /* current index in the error list */
57 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020058 char_u *qf_title; /* title derived from the command that created
59 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000060} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaard12f5c12006-01-25 22:10:52 +000062struct qf_info_S
63{
64 /*
65 * Count of references to this list. Used only for location lists.
66 * When a location list window reference this list, qf_refcount
67 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
68 * reaches 0, the list is freed.
69 */
70 int qf_refcount;
71 int qf_listcount; /* current number of lists */
72 int qf_curlist; /* current error list */
73 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar361c8f02016-07-02 15:41:47 +020074
75 int qf_dir_curlist; /* error list for qf_dir_stack */
76 struct dir_stack_T *qf_dir_stack;
77 char_u *qf_directory;
78 struct dir_stack_T *qf_file_stack;
79 char_u *qf_currfile;
80 int qf_multiline;
81 int qf_multiignore;
82 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000083};
84
85static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaar68b76a62005-03-25 21:53:48 +000087#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89/*
90 * Structure used to hold the info of one part of 'errorformat'
91 */
Bram Moolenaar01265852006-03-20 21:50:15 +000092typedef struct efm_S efm_T;
93struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000094{
95 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000096 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
98 char_u prefix; /* prefix of this format line: */
99 /* 'D' enter directory */
100 /* 'X' leave directory */
101 /* 'A' start of multi-line message */
102 /* 'E' error message */
103 /* 'W' warning message */
104 /* 'I' informational message */
105 /* 'C' continuation line */
106 /* 'Z' end of multi-line message */
107 /* 'G' general, unspecific message */
108 /* 'P' push file (partial) message */
109 /* 'Q' pop/quit file (partial) message */
110 /* 'O' overread (partial) message */
111 char_u flags; /* additional flags given in prefix */
112 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000113 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115};
116
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title);
118static void qf_store_title(qf_info_T *qi, char_u *title);
119static void qf_new_list(qf_info_T *qi, char_u *qf_title);
120static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200121static int qf_add_entry(qf_info_T *qi, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100122static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void qf_free(qf_info_T *qi, int idx);
124static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200125static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
126static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200128static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100129static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
130static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
133static int is_qf_win(win_T *win, qf_info_T *qi);
134static win_T *qf_find_win(qf_info_T *qi);
135static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200136static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200138static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static char_u *get_mef_name(void);
141static void restore_start_dir(char_u *dirname_start);
142static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
143static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
144static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
145static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000147/* Quickfix window check helper macro */
148#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
149/* Location list window check helper macro */
150#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
151/*
152 * Return location list for window 'wp'
153 * For location list window, return the referenced location list
154 */
155#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000158 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200159 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 * Return -1 for error, number of errors for success.
161 */
162 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100163qf_init(
164 win_T *wp,
165 char_u *efile,
166 char_u *errorformat,
167 int newlist, /* TRUE: start a new error list */
168 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000170 qf_info_T *qi = &ql_info;
171
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000173 {
174 qi = ll_get_or_alloc_list(wp);
175 if (qi == NULL)
176 return FAIL;
177 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000178
179 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200180 (linenr_T)0, (linenr_T)0,
181 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182}
183
184/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200185 * Maximum number of bytes allowed per line while reading a errorfile.
186 */
187#define LINE_MAXLEN 4096
188
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200189static struct fmtpattern
190{
191 char_u convchar;
192 char *pattern;
193} fmt_pat[FMT_PATTERNS] =
194 {
195 {'f', ".\\+"}, /* only used when at end */
196 {'n', "\\d\\+"},
197 {'l', "\\d\\+"},
198 {'c', "\\d\\+"},
199 {'t', "."},
200 {'m', ".\\+"},
201 {'r', ".*"},
202 {'p', "[- .]*"},
203 {'v', "\\d\\+"},
204 {'s', ".\\+"}
205 };
206
207/*
208 * Converts a 'errorformat' string to regular expression pattern
209 */
210 static int
211efm_to_regpat(
212 char_u *efm,
213 int len,
214 efm_T *fmt_ptr,
215 char_u *regpat,
216 char_u *errmsg)
217{
218 char_u *ptr;
219 char_u *efmp;
220 char_u *srcptr;
221 int round;
222 int idx = 0;
223
224 /*
225 * Build regexp pattern from current 'errorformat' option
226 */
227 ptr = regpat;
228 *ptr++ = '^';
229 round = 0;
230 for (efmp = efm; efmp < efm + len; ++efmp)
231 {
232 if (*efmp == '%')
233 {
234 ++efmp;
235 for (idx = 0; idx < FMT_PATTERNS; ++idx)
236 if (fmt_pat[idx].convchar == *efmp)
237 break;
238 if (idx < FMT_PATTERNS)
239 {
240 if (fmt_ptr->addr[idx])
241 {
242 sprintf((char *)errmsg,
243 _("E372: Too many %%%c in format string"), *efmp);
244 EMSG(errmsg);
245 return -1;
246 }
247 if ((idx
248 && idx < 6
249 && vim_strchr((char_u *)"DXOPQ",
250 fmt_ptr->prefix) != NULL)
251 || (idx == 6
252 && vim_strchr((char_u *)"OPQ",
253 fmt_ptr->prefix) == NULL))
254 {
255 sprintf((char *)errmsg,
256 _("E373: Unexpected %%%c in format string"), *efmp);
257 EMSG(errmsg);
258 return -1;
259 }
260 fmt_ptr->addr[idx] = (char_u)++round;
261 *ptr++ = '\\';
262 *ptr++ = '(';
263#ifdef BACKSLASH_IN_FILENAME
264 if (*efmp == 'f')
265 {
266 /* Also match "c:" in the file name, even when
267 * checking for a colon next: "%f:".
268 * "\%(\a:\)\=" */
269 STRCPY(ptr, "\\%(\\a:\\)\\=");
270 ptr += 10;
271 }
272#endif
273 if (*efmp == 'f' && efmp[1] != NUL)
274 {
275 if (efmp[1] != '\\' && efmp[1] != '%')
276 {
277 /* A file name may contain spaces, but this isn't
278 * in "\f". For "%f:%l:%m" there may be a ":" in
279 * the file name. Use ".\{-1,}x" instead (x is
280 * the next character), the requirement that :999:
281 * follows should work. */
282 STRCPY(ptr, ".\\{-1,}");
283 ptr += 7;
284 }
285 else
286 {
287 /* File name followed by '\\' or '%': include as
288 * many file name chars as possible. */
289 STRCPY(ptr, "\\f\\+");
290 ptr += 4;
291 }
292 }
293 else
294 {
295 srcptr = (char_u *)fmt_pat[idx].pattern;
296 while ((*ptr = *srcptr++) != NUL)
297 ++ptr;
298 }
299 *ptr++ = '\\';
300 *ptr++ = ')';
301 }
302 else if (*efmp == '*')
303 {
304 if (*++efmp == '[' || *efmp == '\\')
305 {
306 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
307 {
308 if (efmp[1] == '^')
309 *ptr++ = *++efmp;
310 if (efmp < efm + len)
311 {
312 *ptr++ = *++efmp; /* could be ']' */
313 while (efmp < efm + len
314 && (*ptr++ = *++efmp) != ']')
315 /* skip */;
316 if (efmp == efm + len)
317 {
318 EMSG(_("E374: Missing ] in format string"));
319 return -1;
320 }
321 }
322 }
323 else if (efmp < efm + len) /* %*\D, %*\s etc. */
324 *ptr++ = *++efmp;
325 *ptr++ = '\\';
326 *ptr++ = '+';
327 }
328 else
329 {
330 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
331 sprintf((char *)errmsg,
332 _("E375: Unsupported %%%c in format string"), *efmp);
333 EMSG(errmsg);
334 return -1;
335 }
336 }
337 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
338 *ptr++ = *efmp; /* regexp magic characters */
339 else if (*efmp == '#')
340 *ptr++ = '*';
341 else if (*efmp == '>')
342 fmt_ptr->conthere = TRUE;
343 else if (efmp == efm + 1) /* analyse prefix */
344 {
345 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
346 fmt_ptr->flags = *efmp++;
347 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
348 fmt_ptr->prefix = *efmp;
349 else
350 {
351 sprintf((char *)errmsg,
352 _("E376: Invalid %%%c in format string prefix"), *efmp);
353 EMSG(errmsg);
354 return -1;
355 }
356 }
357 else
358 {
359 sprintf((char *)errmsg,
360 _("E377: Invalid %%%c in format string"), *efmp);
361 EMSG(errmsg);
362 return -1;
363 }
364 }
365 else /* copy normal character */
366 {
367 if (*efmp == '\\' && efmp + 1 < efm + len)
368 ++efmp;
369 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
370 *ptr++ = '\\'; /* escape regexp atoms */
371 if (*efmp)
372 *ptr++ = *efmp;
373 }
374 }
375 *ptr++ = '$';
376 *ptr = NUL;
377
378 return 0;
379}
380
381 static void
382free_efm_list(efm_T **efm_first)
383{
384 efm_T *efm_ptr;
385
386 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
387 {
388 *efm_first = efm_ptr->next;
389 vim_regfree(efm_ptr->prog);
390 vim_free(efm_ptr);
391 }
392}
393
394/* Parse 'errorformat' option */
395 static efm_T *
396parse_efm_option(char_u *efm)
397{
398 char_u *errmsg = NULL;
399 int errmsglen;
400 efm_T *fmt_ptr = NULL;
401 efm_T *fmt_first = NULL;
402 efm_T *fmt_last = NULL;
403 char_u *fmtstr = NULL;
404 int len;
405 int i;
406 int round;
407
408 errmsglen = CMDBUFFSIZE + 1;
409 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
410 if (errmsg == NULL)
411 goto parse_efm_end;
412
413 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200414 * Each part of the format string is copied and modified from errorformat
415 * to regex prog. Only a few % characters are allowed.
416 */
417
418 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419 * Get some space to modify the format string into.
420 */
421 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
422 for (round = FMT_PATTERNS; round > 0; )
423 i += (int)STRLEN(fmt_pat[--round].pattern);
424#ifdef COLON_IN_FILENAME
425 i += 12; /* "%f" can become twelve chars longer */
426#else
427 i += 2; /* "%f" can become two chars longer */
428#endif
429 if ((fmtstr = alloc(i)) == NULL)
430 goto parse_efm_error;
431
432 while (efm[0] != NUL)
433 {
434 /*
435 * Allocate a new eformat structure and put it at the end of the list
436 */
437 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
438 if (fmt_ptr == NULL)
439 goto parse_efm_error;
440 if (fmt_first == NULL) /* first one */
441 fmt_first = fmt_ptr;
442 else
443 fmt_last->next = fmt_ptr;
444 fmt_last = fmt_ptr;
445
446 /*
447 * Isolate one part in the 'errorformat' option
448 */
449 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
450 if (efm[len] == '\\' && efm[len + 1] != NUL)
451 ++len;
452
453 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
454 goto parse_efm_error;
455 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
456 goto parse_efm_error;
457 /*
458 * Advance to next part
459 */
460 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
461 }
462
463 if (fmt_first == NULL) /* nothing found */
464 EMSG(_("E378: 'errorformat' contains no pattern"));
465
466 goto parse_efm_end;
467
468parse_efm_error:
469 free_efm_list(&fmt_first);
470
471parse_efm_end:
472 vim_free(fmtstr);
473 vim_free(errmsg);
474
475 return fmt_first;
476}
477
Bram Moolenaare0d37972016-07-15 22:36:01 +0200478enum {
479 QF_FAIL = 0,
480 QF_OK = 1,
481 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200482 QF_NOMEM = 3,
483 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200484};
485
486typedef struct {
487 char_u *linebuf;
488 int linelen;
489 char_u *growbuf;
490 int growbufsiz;
491 FILE *fd;
492 typval_T *tv;
493 char_u *p_str;
494 listitem_T *p_li;
495 buf_T *buf;
496 linenr_T buflnum;
497 linenr_T lnumlast;
498} qfstate_T;
499
500 static char_u *
501qf_grow_linebuf(qfstate_T *state, int newsz)
502{
503 /*
504 * If the line exceeds LINE_MAXLEN exclude the last
505 * byte since it's not a NL character.
506 */
507 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
508 if (state->growbuf == NULL)
509 {
510 state->growbuf = alloc(state->linelen + 1);
511 if (state->growbuf == NULL)
512 return NULL;
513 state->growbufsiz = state->linelen;
514 }
515 else if (state->linelen > state->growbufsiz)
516 {
517 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
518 if (state->growbuf == NULL)
519 return NULL;
520 state->growbufsiz = state->linelen;
521 }
522 return state->growbuf;
523}
524
525/*
526 * Get the next string (separated by newline) from state->p_str.
527 */
528 static int
529qf_get_next_str_line(qfstate_T *state)
530{
531 /* Get the next line from the supplied string */
532 char_u *p_str = state->p_str;
533 char_u *p;
534 int len;
535
536 if (*p_str == NUL) /* Reached the end of the string */
537 return QF_END_OF_INPUT;
538
539 p = vim_strchr(p_str, '\n');
540 if (p != NULL)
541 len = (int)(p - p_str) + 1;
542 else
543 len = (int)STRLEN(p_str);
544
545 if (len > IOSIZE - 2)
546 {
547 state->linebuf = qf_grow_linebuf(state, len);
548 if (state->linebuf == NULL)
549 return QF_NOMEM;
550 }
551 else
552 {
553 state->linebuf = IObuff;
554 state->linelen = len;
555 }
556 vim_strncpy(state->linebuf, p_str, state->linelen);
557
558 /*
559 * Increment using len in order to discard the rest of the
560 * line if it exceeds LINE_MAXLEN.
561 */
562 p_str += len;
563 state->p_str = p_str;
564
565 return QF_OK;
566}
567
568/*
569 * Get the next string from state->p_Li.
570 */
571 static int
572qf_get_next_list_line(qfstate_T *state)
573{
574 listitem_T *p_li = state->p_li;
575 int len;
576
577 while (p_li != NULL
578 && (p_li->li_tv.v_type != VAR_STRING
579 || p_li->li_tv.vval.v_string == NULL))
580 p_li = p_li->li_next; /* Skip non-string items */
581
582 if (p_li == NULL) /* End of the list */
583 {
584 state->p_li = NULL;
585 return QF_END_OF_INPUT;
586 }
587
588 len = (int)STRLEN(p_li->li_tv.vval.v_string);
589 if (len > IOSIZE - 2)
590 {
591 state->linebuf = qf_grow_linebuf(state, len);
592 if (state->linebuf == NULL)
593 return QF_NOMEM;
594 }
595 else
596 {
597 state->linebuf = IObuff;
598 state->linelen = len;
599 }
600
601 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
602
603 state->p_li = p_li->li_next; /* next item */
604 return QF_OK;
605}
606
607/*
608 * Get the next string from state->buf.
609 */
610 static int
611qf_get_next_buf_line(qfstate_T *state)
612{
613 char_u *p_buf = NULL;
614 int len;
615
616 /* Get the next line from the supplied buffer */
617 if (state->buflnum > state->lnumlast)
618 return QF_END_OF_INPUT;
619
620 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
621 state->buflnum += 1;
622
623 len = (int)STRLEN(p_buf);
624 if (len > IOSIZE - 2)
625 {
626 state->linebuf = qf_grow_linebuf(state, len);
627 if (state->linebuf == NULL)
628 return QF_NOMEM;
629 }
630 else
631 {
632 state->linebuf = IObuff;
633 state->linelen = len;
634 }
635 vim_strncpy(state->linebuf, p_buf, state->linelen);
636
637 return QF_OK;
638}
639
640/*
641 * Get the next string from file state->fd.
642 */
643 static int
644qf_get_next_file_line(qfstate_T *state)
645{
646 int discard;
647 int growbuflen;
648
649 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
650 return QF_END_OF_INPUT;
651
652 discard = FALSE;
653 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200654 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200655 {
656 /*
657 * The current line exceeds IObuff, continue reading using
658 * growbuf until EOL or LINE_MAXLEN bytes is read.
659 */
660 if (state->growbuf == NULL)
661 {
662 state->growbufsiz = 2 * (IOSIZE - 1);
663 state->growbuf = alloc(state->growbufsiz);
664 if (state->growbuf == NULL)
665 return QF_NOMEM;
666 }
667
668 /* Copy the read part of the line, excluding null-terminator */
669 memcpy(state->growbuf, IObuff, IOSIZE - 1);
670 growbuflen = state->linelen;
671
672 for (;;)
673 {
674 if (fgets((char *)state->growbuf + growbuflen,
675 state->growbufsiz - growbuflen, state->fd) == NULL)
676 break;
677 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
678 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200679 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200680 break;
681 if (state->growbufsiz == LINE_MAXLEN)
682 {
683 discard = TRUE;
684 break;
685 }
686
687 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
688 ? 2 * state->growbufsiz : LINE_MAXLEN;
689 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
690 if (state->growbuf == NULL)
691 return QF_NOMEM;
692 }
693
694 while (discard)
695 {
696 /*
697 * The current line is longer than LINE_MAXLEN, continue
698 * reading but discard everything until EOL or EOF is
699 * reached.
700 */
701 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
702 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200703 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200704 break;
705 }
706
707 state->linebuf = state->growbuf;
708 state->linelen = growbuflen;
709 }
710 else
711 state->linebuf = IObuff;
712
713 return QF_OK;
714}
715
716/*
717 * Get the next string from a file/buffer/list/string.
718 */
719 static int
720qf_get_nextline(qfstate_T *state)
721{
722 int status = QF_FAIL;
723
724 if (state->fd == NULL)
725 {
726 if (state->tv != NULL)
727 {
728 if (state->tv->v_type == VAR_STRING)
729 /* Get the next line from the supplied string */
730 status = qf_get_next_str_line(state);
731 else if (state->tv->v_type == VAR_LIST)
732 /* Get the next line from the supplied list */
733 status = qf_get_next_list_line(state);
734 }
735 else
736 /* Get the next line from the supplied buffer */
737 status = qf_get_next_buf_line(state);
738 }
739 else
740 /* Get the next line from the supplied file */
741 status = qf_get_next_file_line(state);
742
743 if (status != QF_OK)
744 return status;
745
746 /* remove newline/CR from the line */
747 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200748 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200749 state->linebuf[state->linelen - 1] = NUL;
750#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200751 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
752 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200753#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200754 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200755
756#ifdef FEAT_MBYTE
757 remove_bom(state->linebuf);
758#endif
759
760 return QF_OK;
761}
762
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200763typedef struct {
764 char_u *namebuf;
765 char_u *errmsg;
766 int errmsglen;
767 long lnum;
768 int col;
769 char_u use_viscol;
770 char_u *pattern;
771 int enr;
772 int type;
773 int valid;
774} qffields_T;
775
776/*
777 * Parse a line and get the quickfix fields.
778 * Return the QF_ status.
779 */
780 static int
781qf_parse_line(
782 qf_info_T *qi,
783 char_u *linebuf,
784 int linelen,
785 efm_T *fmt_first,
786 qffields_T *fields)
787{
788 efm_T *fmt_ptr;
789 static efm_T *fmt_start = NULL; /* cached across calls */
790 char_u *ptr;
791 int len;
792 int i;
793 int idx = 0;
794 char_u *tail = NULL;
795 regmatch_T regmatch;
796
797 /* Always ignore case when looking for a matching error. */
798 regmatch.rm_ic = TRUE;
799
800 /* If there was no %> item start at the first pattern */
801 if (fmt_start == NULL)
802 fmt_ptr = fmt_first;
803 else
804 {
805 fmt_ptr = fmt_start;
806 fmt_start = NULL;
807 }
808
809 /*
810 * Try to match each part of 'errorformat' until we find a complete
811 * match or no match.
812 */
813 fields->valid = TRUE;
814restofline:
815 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
816 {
817 int r;
818
819 idx = fmt_ptr->prefix;
820 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
821 continue;
822 fields->namebuf[0] = NUL;
823 fields->pattern[0] = NUL;
824 if (!qi->qf_multiscan)
825 fields->errmsg[0] = NUL;
826 fields->lnum = 0;
827 fields->col = 0;
828 fields->use_viscol = FALSE;
829 fields->enr = -1;
830 fields->type = 0;
831 tail = NULL;
832
833 regmatch.regprog = fmt_ptr->prog;
834 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
835 fmt_ptr->prog = regmatch.regprog;
836 if (r)
837 {
838 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
839 continue;
840 if (vim_strchr((char_u *)"EWI", idx) != NULL)
841 fields->type = idx;
842 else
843 fields->type = 0;
844 /*
845 * Extract error message data from matched line.
846 * We check for an actual submatch, because "\[" and "\]" in
847 * the 'errorformat' may cause the wrong submatch to be used.
848 */
849 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
850 {
851 int c;
852
853 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
854 continue;
855
856 /* Expand ~/file and $HOME/file to full path. */
857 c = *regmatch.endp[i];
858 *regmatch.endp[i] = NUL;
859 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
860 *regmatch.endp[i] = c;
861
862 if (vim_strchr((char_u *)"OPQ", idx) != NULL
863 && mch_getperm(fields->namebuf) == -1)
864 continue;
865 }
866 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
867 {
868 if (regmatch.startp[i] == NULL)
869 continue;
870 fields->enr = (int)atol((char *)regmatch.startp[i]);
871 }
872 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
873 {
874 if (regmatch.startp[i] == NULL)
875 continue;
876 fields->lnum = atol((char *)regmatch.startp[i]);
877 }
878 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
879 {
880 if (regmatch.startp[i] == NULL)
881 continue;
882 fields->col = (int)atol((char *)regmatch.startp[i]);
883 }
884 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
885 {
886 if (regmatch.startp[i] == NULL)
887 continue;
888 fields->type = *regmatch.startp[i];
889 }
890 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
891 {
892 if (linelen > fields->errmsglen) {
893 /* linelen + null terminator */
894 if ((fields->errmsg = vim_realloc(fields->errmsg,
895 linelen + 1)) == NULL)
896 return QF_NOMEM;
897 fields->errmsglen = linelen + 1;
898 }
899 vim_strncpy(fields->errmsg, linebuf, linelen);
900 }
901 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
902 {
903 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
904 continue;
905 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
906 if (len > fields->errmsglen) {
907 /* len + null terminator */
908 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
909 == NULL)
910 return QF_NOMEM;
911 fields->errmsglen = len + 1;
912 }
913 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
914 }
915 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
916 {
917 if (regmatch.startp[i] == NULL)
918 continue;
919 tail = regmatch.startp[i];
920 }
921 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
922 {
923 char_u *match_ptr;
924
925 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
926 continue;
927 fields->col = 0;
928 for (match_ptr = regmatch.startp[i];
929 match_ptr != regmatch.endp[i]; ++match_ptr)
930 {
931 ++fields->col;
932 if (*match_ptr == TAB)
933 {
934 fields->col += 7;
935 fields->col -= fields->col % 8;
936 }
937 }
938 ++fields->col;
939 fields->use_viscol = TRUE;
940 }
941 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
942 {
943 if (regmatch.startp[i] == NULL)
944 continue;
945 fields->col = (int)atol((char *)regmatch.startp[i]);
946 fields->use_viscol = TRUE;
947 }
948 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
949 {
950 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
951 continue;
952 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
953 if (len > CMDBUFFSIZE - 5)
954 len = CMDBUFFSIZE - 5;
955 STRCPY(fields->pattern, "^\\V");
956 STRNCAT(fields->pattern, regmatch.startp[i], len);
957 fields->pattern[len + 3] = '\\';
958 fields->pattern[len + 4] = '$';
959 fields->pattern[len + 5] = NUL;
960 }
961 break;
962 }
963 }
964 qi->qf_multiscan = FALSE;
965
966 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
967 {
968 if (fmt_ptr != NULL)
969 {
970 if (idx == 'D') /* enter directory */
971 {
972 if (*fields->namebuf == NUL)
973 {
974 EMSG(_("E379: Missing or empty directory name"));
975 return QF_FAIL;
976 }
977 qi->qf_directory =
978 qf_push_dir(fields->namebuf, &qi->qf_dir_stack, FALSE);
979 if (qi->qf_directory == NULL)
980 return QF_FAIL;
981 }
982 else if (idx == 'X') /* leave directory */
983 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
984 }
985 fields->namebuf[0] = NUL; /* no match found, remove file name */
986 fields->lnum = 0; /* don't jump to this line */
987 fields->valid = FALSE;
988 if (linelen > fields->errmsglen) {
989 /* linelen + null terminator */
990 if ((fields->errmsg = vim_realloc(fields->errmsg,
991 linelen + 1)) == NULL)
992 return QF_NOMEM;
993 fields->errmsglen = linelen + 1;
994 }
995 /* copy whole line to error message */
996 vim_strncpy(fields->errmsg, linebuf, linelen);
997 if (fmt_ptr == NULL)
998 qi->qf_multiline = qi->qf_multiignore = FALSE;
999 }
1000 else if (fmt_ptr != NULL)
1001 {
1002 /* honor %> item */
1003 if (fmt_ptr->conthere)
1004 fmt_start = fmt_ptr;
1005
1006 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1007 {
1008 qi->qf_multiline = TRUE; /* start of a multi-line message */
1009 qi->qf_multiignore = FALSE; /* reset continuation */
1010 }
1011 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1012 { /* continuation of multi-line msg */
1013 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
1014
1015 if (qfprev == NULL)
1016 return QF_FAIL;
1017 if (*fields->errmsg && !qi->qf_multiignore)
1018 {
1019 len = (int)STRLEN(qfprev->qf_text);
1020 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1021 == NULL)
1022 return QF_FAIL;
1023 STRCPY(ptr, qfprev->qf_text);
1024 vim_free(qfprev->qf_text);
1025 qfprev->qf_text = ptr;
1026 *(ptr += len) = '\n';
1027 STRCPY(++ptr, fields->errmsg);
1028 }
1029 if (qfprev->qf_nr == -1)
1030 qfprev->qf_nr = fields->enr;
1031 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1032 /* only printable chars allowed */
1033 qfprev->qf_type = fields->type;
1034
1035 if (!qfprev->qf_lnum)
1036 qfprev->qf_lnum = fields->lnum;
1037 if (!qfprev->qf_col)
1038 qfprev->qf_col = fields->col;
1039 qfprev->qf_viscol = fields->use_viscol;
1040 if (!qfprev->qf_fnum)
1041 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1042 *fields->namebuf || qi->qf_directory != NULL
1043 ? fields->namebuf
1044 : qi->qf_currfile != NULL && fields->valid
1045 ? qi->qf_currfile : 0);
1046 if (idx == 'Z')
1047 qi->qf_multiline = qi->qf_multiignore = FALSE;
1048 line_breakcheck();
1049 return QF_IGNORE_LINE;
1050 }
1051 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1052 {
1053 /* global file names */
1054 fields->valid = FALSE;
1055 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1056 {
1057 if (*fields->namebuf && idx == 'P')
1058 qi->qf_currfile =
1059 qf_push_dir(fields->namebuf, &qi->qf_file_stack, TRUE);
1060 else if (idx == 'Q')
1061 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
1062 *fields->namebuf = NUL;
1063 if (tail && *tail)
1064 {
1065 STRMOVE(IObuff, skipwhite(tail));
1066 qi->qf_multiscan = TRUE;
1067 goto restofline;
1068 }
1069 }
1070 }
1071 if (fmt_ptr->flags == '-') /* generally exclude this line */
1072 {
1073 if (qi->qf_multiline)
1074 /* also exclude continuation lines */
1075 qi->qf_multiignore = TRUE;
1076 return QF_IGNORE_LINE;
1077 }
1078 }
1079
1080 return QF_OK;
1081}
1082
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001083/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001084 * Read the errorfile "efile" into memory, line by line, building the error
1085 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001086 * Alternative: when "efile" is NULL read errors from buffer "buf".
1087 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001088 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001089 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1090 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001091 * Return -1 for error, number of errors for success.
1092 */
1093 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001094qf_init_ext(
1095 qf_info_T *qi,
1096 char_u *efile,
1097 buf_T *buf,
1098 typval_T *tv,
1099 char_u *errorformat,
1100 int newlist, /* TRUE: start a new error list */
1101 linenr_T lnumfirst, /* first line number to use */
1102 linenr_T lnumlast, /* last line number to use */
1103 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001104{
Bram Moolenaare0d37972016-07-15 22:36:01 +02001105 qfstate_T state = {NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001106 NULL, 0, 0};
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001107 qffields_T fields = {NULL, NULL, 0, 0L, 0, FALSE, NULL, 0, 0, 0};
Bram Moolenaar864293a2016-06-02 13:40:04 +02001108#ifdef FEAT_WINDOWS
1109 qfline_T *old_last = NULL;
1110#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001111 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001113 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001115 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001117 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1118 fields.errmsglen = CMDBUFFSIZE + 1;
1119 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1120 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1121 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1122 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 goto qf_init_end;
1124
Bram Moolenaare0d37972016-07-15 22:36:01 +02001125 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {
1127 EMSG2(_(e_openerrf), efile);
1128 goto qf_init_end;
1129 }
1130
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001131 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001133 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001134#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001135 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02001136 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001137 /* Adding to existing list, use last entry. */
1138 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001139 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001143 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001144 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 else
1146 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001148 /*
1149 * If we are not adding or adding to another list: clear the state.
1150 */
1151 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
1152 {
1153 qi->qf_dir_curlist = qi->qf_curlist;
1154 qf_clean_dir_stack(&qi->qf_dir_stack);
1155 qi->qf_directory = NULL;
1156 qf_clean_dir_stack(&qi->qf_file_stack);
1157 qi->qf_currfile = NULL;
1158 qi->qf_multiline = FALSE;
1159 qi->qf_multiignore = FALSE;
1160 qi->qf_multiscan = FALSE;
1161 }
1162
1163 /*
1164 * If the errorformat didn't change between calls, then reuse the
1165 * previously parsed values.
1166 */
1167 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1168 {
1169 /* free the previously parsed data */
1170 vim_free(last_efm);
1171 last_efm = NULL;
1172 free_efm_list(&fmt_first);
1173
1174 /* parse the current 'efm' */
1175 fmt_first = parse_efm_option(efm);
1176 if (fmt_first != NULL)
1177 last_efm = vim_strsave(efm);
1178 }
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
1183 /*
1184 * got_int is reset here, because it was probably set when killing the
1185 * ":make" command, but we still want to read the errorfile then.
1186 */
1187 got_int = FALSE;
1188
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001189 if (tv != NULL)
1190 {
1191 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001192 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001193 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001194 state.p_li = tv->vval.v_list->lv_first;
1195 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001196 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001197 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001198 state.buflnum = lnumfirst;
1199 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 /*
1202 * Read the lines in the error file one by one.
1203 * Try to recognize one of the error formats in each line.
1204 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001205 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001207 /* Get the next line from a file/buffer/list/string */
1208 status = qf_get_nextline(&state);
1209 if (status == QF_NOMEM) /* memory alloc failure */
1210 goto qf_init_end;
1211 if (status == QF_END_OF_INPUT) /* end of input */
1212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001214 status = qf_parse_line(qi, state.linebuf, state.linelen, fmt_first,
1215 &fields);
1216 if (status == QF_FAIL)
1217 goto error2;
1218 if (status == QF_NOMEM)
1219 goto qf_init_end;
1220 if (status == QF_IGNORE_LINE)
1221 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001223 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001224 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001225 (*fields.namebuf || qi->qf_directory != NULL)
1226 ? fields.namebuf
1227 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001228 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001229 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001230 fields.errmsg,
1231 fields.lnum,
1232 fields.col,
1233 fields.use_viscol,
1234 fields.pattern,
1235 fields.enr,
1236 fields.type,
1237 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 goto error2;
1239 line_breakcheck();
1240 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001241 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001243 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001245 /* no valid entry found */
1246 qi->qf_lists[qi->qf_curlist].qf_ptr =
1247 qi->qf_lists[qi->qf_curlist].qf_start;
1248 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1249 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251 else
1252 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001253 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1254 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1255 qi->qf_lists[qi->qf_curlist].qf_ptr =
1256 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001258 /* return number of matches */
1259 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001260 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262 EMSG(_(e_readerrf));
1263error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001264 qf_free(qi, qi->qf_curlist);
1265 qi->qf_listcount--;
1266 if (qi->qf_curlist > 0)
1267 --qi->qf_curlist;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001268qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001269 if (state.fd != NULL)
1270 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001271 vim_free(fields.namebuf);
1272 vim_free(fields.errmsg);
1273 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001274 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
1276#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001277 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278#endif
1279
1280 return retval;
1281}
1282
Bram Moolenaarfb604092014-07-23 15:55:00 +02001283 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001284qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001285{
1286 if (title != NULL)
1287 {
1288 char_u *p = alloc((int)STRLEN(title) + 2);
1289
1290 qi->qf_lists[qi->qf_curlist].qf_title = p;
1291 if (p != NULL)
1292 sprintf((char *)p, ":%s", (char *)title);
1293 }
1294}
1295
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296/*
1297 * Prepare for adding a new quickfix list.
1298 */
1299 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001300qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
1302 int i;
1303
1304 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001305 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 * the current entry. This makes it possible to browse in a tree-like
1307 * way with ":grep'.
1308 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001309 while (qi->qf_listcount > qi->qf_curlist + 1)
1310 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
1312 /*
1313 * When the stack is full, remove to oldest entry
1314 * Otherwise, add a new entry.
1315 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001316 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001318 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001320 qi->qf_lists[i - 1] = qi->qf_lists[i];
1321 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001324 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001325 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001326 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327}
1328
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001329/*
1330 * Free a location list
1331 */
1332 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001333ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001334{
1335 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001336 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001337
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001338 qi = *pqi;
1339 if (qi == NULL)
1340 return;
1341 *pqi = NULL; /* Remove reference to this list */
1342
1343 qi->qf_refcount--;
1344 if (qi->qf_refcount < 1)
1345 {
1346 /* No references to this location list */
1347 for (i = 0; i < qi->qf_listcount; ++i)
1348 qf_free(qi, i);
1349 vim_free(qi);
1350 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001351}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001352
1353 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001354qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001355{
1356 int i;
1357 qf_info_T *qi = &ql_info;
1358
1359 if (wp != NULL)
1360 {
1361 /* location list */
1362 ll_free_all(&wp->w_llist);
1363 ll_free_all(&wp->w_llist_ref);
1364 }
1365 else
1366 /* quickfix list */
1367 for (i = 0; i < qi->qf_listcount; ++i)
1368 qf_free(qi, i);
1369}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371/*
1372 * Add an entry to the end of the list of errors.
1373 * Returns OK or FAIL.
1374 */
1375 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001376qf_add_entry(
1377 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001378 char_u *dir, /* optional directory name */
1379 char_u *fname, /* file name or NULL */
1380 int bufnum, /* buffer number or zero */
1381 char_u *mesg, /* message */
1382 long lnum, /* line number */
1383 int col, /* column */
1384 int vis_col, /* using visual column */
1385 char_u *pattern, /* search pattern */
1386 int nr, /* error number */
1387 int type, /* type character */
1388 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001390 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001391 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001393 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001395 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001396 {
1397 buf_T *buf = buflist_findnr(bufnum);
1398
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001399 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001400 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001401 buf->b_has_qf_entry |=
1402 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001403 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001404 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001405 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1407 {
1408 vim_free(qfp);
1409 return FAIL;
1410 }
1411 qfp->qf_lnum = lnum;
1412 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001413 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001414 if (pattern == NULL || *pattern == NUL)
1415 qfp->qf_pattern = NULL;
1416 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1417 {
1418 vim_free(qfp->qf_text);
1419 vim_free(qfp);
1420 return FAIL;
1421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 qfp->qf_nr = nr;
1423 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1424 type = 0;
1425 qfp->qf_type = type;
1426 qfp->qf_valid = valid;
1427
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001428 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001429 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1430 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001432 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001433 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1434 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001435 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437 else
1438 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001439 qfp->qf_prev = *lastp;
1440 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001442 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001444 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001445 ++qi->qf_lists[qi->qf_curlist].qf_count;
1446 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1447 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001449 qi->qf_lists[qi->qf_curlist].qf_index =
1450 qi->qf_lists[qi->qf_curlist].qf_count;
1451 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453
1454 return OK;
1455}
1456
1457/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001458 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001459 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001460 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001461ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001462{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001463 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001464
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001465 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1466 if (qi != NULL)
1467 {
1468 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1469 qi->qf_refcount++;
1470 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001471
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001472 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001473}
1474
1475/*
1476 * Return the location list for window 'wp'.
1477 * If not present, allocate a location list
1478 */
1479 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001480ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001481{
1482 if (IS_LL_WINDOW(wp))
1483 /* For a location list window, use the referenced location list */
1484 return wp->w_llist_ref;
1485
1486 /*
1487 * For a non-location list window, w_llist_ref should not point to a
1488 * location list.
1489 */
1490 ll_free_all(&wp->w_llist_ref);
1491
1492 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001493 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001494 return wp->w_llist;
1495}
1496
1497/*
1498 * Copy the location list from window "from" to window "to".
1499 */
1500 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001501copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001502{
1503 qf_info_T *qi;
1504 int idx;
1505 int i;
1506
1507 /*
1508 * When copying from a location list window, copy the referenced
1509 * location list. For other windows, copy the location list for
1510 * that window.
1511 */
1512 if (IS_LL_WINDOW(from))
1513 qi = from->w_llist_ref;
1514 else
1515 qi = from->w_llist;
1516
1517 if (qi == NULL) /* no location list to copy */
1518 return;
1519
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001520 /* allocate a new location list */
1521 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522 return;
1523
1524 to->w_llist->qf_listcount = qi->qf_listcount;
1525
1526 /* Copy the location lists one at a time */
1527 for (idx = 0; idx < qi->qf_listcount; idx++)
1528 {
1529 qf_list_T *from_qfl;
1530 qf_list_T *to_qfl;
1531
1532 to->w_llist->qf_curlist = idx;
1533
1534 from_qfl = &qi->qf_lists[idx];
1535 to_qfl = &to->w_llist->qf_lists[idx];
1536
1537 /* Some of the fields are populated by qf_add_entry() */
1538 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1539 to_qfl->qf_count = 0;
1540 to_qfl->qf_index = 0;
1541 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001542 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001544 if (from_qfl->qf_title != NULL)
1545 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1546 else
1547 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001548
1549 if (from_qfl->qf_count)
1550 {
1551 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001552 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001553
1554 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001555 for (i = 0, from_qfp = from_qfl->qf_start;
1556 i < from_qfl->qf_count && from_qfp != NULL;
1557 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001558 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001559 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001560 NULL,
1561 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001562 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001563 from_qfp->qf_text,
1564 from_qfp->qf_lnum,
1565 from_qfp->qf_col,
1566 from_qfp->qf_viscol,
1567 from_qfp->qf_pattern,
1568 from_qfp->qf_nr,
1569 0,
1570 from_qfp->qf_valid) == FAIL)
1571 {
1572 qf_free_all(to);
1573 return;
1574 }
1575 /*
1576 * qf_add_entry() will not set the qf_num field, as the
1577 * directory and file names are not supplied. So the qf_fnum
1578 * field is copied here.
1579 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001580 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001581 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1582 prevp->qf_type = from_qfp->qf_type; /* error type */
1583 if (from_qfl->qf_ptr == from_qfp)
1584 to_qfl->qf_ptr = prevp; /* current location */
1585 }
1586 }
1587
1588 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1589
1590 /* When no valid entries are present in the list, qf_ptr points to
1591 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001592 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001593 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001594 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001595 to_qfl->qf_index = 1;
1596 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001597 }
1598
1599 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1600}
1601
1602/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001603 * Looking up a buffer can be slow if there are many. Remember the last one
1604 * to make this a lot faster if there are multiple matches in the same file.
1605 */
1606static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001607static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001608
1609/*
Bram Moolenaar015102e2016-07-16 18:24:56 +02001610 * Get buffer number for file "directory.fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001611 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 */
1613 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001614qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
Bram Moolenaar82404332016-07-10 17:00:38 +02001616 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001617 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001618 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (fname == NULL || *fname == NUL) /* no file name */
1621 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaare60acc12011-05-10 16:41:25 +02001623#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001624 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001625#endif
1626#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001627 if (directory != NULL)
1628 slash_adjust(directory);
1629 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001630#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001631 if (directory != NULL && !vim_isAbsName(fname)
1632 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1633 {
1634 /*
1635 * Here we check if the file really exists.
1636 * This should normally be true, but if make works without
1637 * "leaving directory"-messages we might have missed a
1638 * directory change.
1639 */
1640 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001643 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001644 if (directory)
1645 ptr = concat_fnames(directory, fname, TRUE);
1646 else
1647 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001649 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001650 bufname = ptr;
1651 }
1652 else
1653 bufname = fname;
1654
1655 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001656 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001657 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001658 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001659 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001661 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001662 {
1663 vim_free(qf_last_bufname);
1664 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1665 if (bufname == ptr)
1666 qf_last_bufname = bufname;
1667 else
1668 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001669 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001670 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001671 if (buf == NULL)
1672 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001673
Bram Moolenaarc1542742016-07-20 21:44:37 +02001674 buf->b_has_qf_entry =
1675 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001676 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677}
1678
1679/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001680 * Push dirbuf onto the directory stack and return pointer to actual dir or
1681 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 */
1683 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001684qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 struct dir_stack_T *ds_new;
1687 struct dir_stack_T *ds_ptr;
1688
1689 /* allocate new stack element and hook it in */
1690 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1691 if (ds_new == NULL)
1692 return NULL;
1693
1694 ds_new->next = *stackptr;
1695 *stackptr = ds_new;
1696
1697 /* store directory on the stack */
1698 if (vim_isAbsName(dirbuf)
1699 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001700 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 (*stackptr)->dirname = vim_strsave(dirbuf);
1702 else
1703 {
1704 /* Okay we don't have an absolute path.
1705 * dirbuf must be a subdir of one of the directories on the stack.
1706 * Let's search...
1707 */
1708 ds_new = (*stackptr)->next;
1709 (*stackptr)->dirname = NULL;
1710 while (ds_new)
1711 {
1712 vim_free((*stackptr)->dirname);
1713 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1714 TRUE);
1715 if (mch_isdir((*stackptr)->dirname) == TRUE)
1716 break;
1717
1718 ds_new = ds_new->next;
1719 }
1720
1721 /* clean up all dirs we already left */
1722 while ((*stackptr)->next != ds_new)
1723 {
1724 ds_ptr = (*stackptr)->next;
1725 (*stackptr)->next = (*stackptr)->next->next;
1726 vim_free(ds_ptr->dirname);
1727 vim_free(ds_ptr);
1728 }
1729
1730 /* Nothing found -> it must be on top level */
1731 if (ds_new == NULL)
1732 {
1733 vim_free((*stackptr)->dirname);
1734 (*stackptr)->dirname = vim_strsave(dirbuf);
1735 }
1736 }
1737
1738 if ((*stackptr)->dirname != NULL)
1739 return (*stackptr)->dirname;
1740 else
1741 {
1742 ds_ptr = *stackptr;
1743 *stackptr = (*stackptr)->next;
1744 vim_free(ds_ptr);
1745 return NULL;
1746 }
1747}
1748
1749
1750/*
1751 * pop dirbuf from the directory stack and return previous directory or NULL if
1752 * stack is empty
1753 */
1754 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001755qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 struct dir_stack_T *ds_ptr;
1758
1759 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1760 * What to do if it isn't? */
1761
1762 /* pop top element and free it */
1763 if (*stackptr != NULL)
1764 {
1765 ds_ptr = *stackptr;
1766 *stackptr = (*stackptr)->next;
1767 vim_free(ds_ptr->dirname);
1768 vim_free(ds_ptr);
1769 }
1770
1771 /* return NEW top element as current dir or NULL if stack is empty*/
1772 return *stackptr ? (*stackptr)->dirname : NULL;
1773}
1774
1775/*
1776 * clean up directory stack
1777 */
1778 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001779qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 struct dir_stack_T *ds_ptr;
1782
1783 while ((ds_ptr = *stackptr) != NULL)
1784 {
1785 *stackptr = (*stackptr)->next;
1786 vim_free(ds_ptr->dirname);
1787 vim_free(ds_ptr);
1788 }
1789}
1790
1791/*
1792 * Check in which directory of the directory stack the given file can be
1793 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001794 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 * Cleans up intermediate directory entries.
1796 *
1797 * TODO: How to solve the following problem?
1798 * If we have the this directory tree:
1799 * ./
1800 * ./aa
1801 * ./aa/bb
1802 * ./bb
1803 * ./bb/x.c
1804 * and make says:
1805 * making all in aa
1806 * making all in bb
1807 * x.c:9: Error
1808 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1809 * qf_guess_filepath will return NULL.
1810 */
1811 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001812qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
1814 struct dir_stack_T *ds_ptr;
1815 struct dir_stack_T *ds_tmp;
1816 char_u *fullname;
1817
1818 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001819 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 return NULL;
1821
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001822 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 fullname = NULL;
1824 while (ds_ptr)
1825 {
1826 vim_free(fullname);
1827 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1828
1829 /* If concat_fnames failed, just go on. The worst thing that can happen
1830 * is that we delete the entire stack.
1831 */
1832 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1833 break;
1834
1835 ds_ptr = ds_ptr->next;
1836 }
1837
1838 vim_free(fullname);
1839
1840 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001841 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001843 ds_tmp = qi->qf_dir_stack->next;
1844 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 vim_free(ds_tmp->dirname);
1846 vim_free(ds_tmp);
1847 }
1848
1849 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850}
1851
1852/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001853 * When loading a file from the quickfix, the auto commands may modify it.
1854 * This may invalidate the current quickfix entry. This function checks
1855 * whether a entry is still present in the quickfix.
1856 * Similar to location list.
1857 */
1858 static int
1859is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1860{
1861 qf_list_T *qfl;
1862 qfline_T *qfp;
1863 int i;
1864
1865 qfl = &qi->qf_lists[qi->qf_curlist];
1866
1867 /* Search for the entry in the current list */
1868 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1869 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001870 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001871 break;
1872
1873 if (i == qfl->qf_count) /* Entry is not found */
1874 return FALSE;
1875
1876 return TRUE;
1877}
1878
1879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 * jump to a quickfix line
1881 * if dir == FORWARD go "errornr" valid entries forward
1882 * if dir == BACKWARD go "errornr" valid entries backward
1883 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1884 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1885 * else if "errornr" is zero, redisplay the same line
1886 * else go to entry "errornr"
1887 */
1888 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001889qf_jump(
1890 qf_info_T *qi,
1891 int dir,
1892 int errornr,
1893 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001895 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001896 qfline_T *qf_ptr;
1897 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 int qf_index;
1899 int old_qf_fnum;
1900 int old_qf_index;
1901 int prev_index;
1902 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1903 char_u *err = e_no_more_items;
1904 linenr_T i;
1905 buf_T *old_curbuf;
1906 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 colnr_T screen_col;
1908 colnr_T char_col;
1909 char_u *line;
1910#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001911 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001912 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int opened_window = FALSE;
1914 win_T *win;
1915 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001916 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001918 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 int print_message = TRUE;
1920 int len;
1921#ifdef FEAT_FOLDING
1922 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1923#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001924 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001925 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001927 if (qi == NULL)
1928 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001929
1930 if (qi->qf_curlist >= qi->qf_listcount
1931 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
1933 EMSG(_(e_quickfix));
1934 return;
1935 }
1936
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001937 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001939 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 old_qf_index = qf_index;
1941 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1942 {
1943 while (errornr--)
1944 {
1945 old_qf_ptr = qf_ptr;
1946 prev_index = qf_index;
1947 old_qf_fnum = qf_ptr->qf_fnum;
1948 do
1949 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001950 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 || qf_ptr->qf_next == NULL)
1952 {
1953 qf_ptr = old_qf_ptr;
1954 qf_index = prev_index;
1955 if (err != NULL)
1956 {
1957 EMSG(_(err));
1958 goto theend;
1959 }
1960 errornr = 0;
1961 break;
1962 }
1963 ++qf_index;
1964 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001965 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1966 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1968 err = NULL;
1969 }
1970 }
1971 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1972 {
1973 while (errornr--)
1974 {
1975 old_qf_ptr = qf_ptr;
1976 prev_index = qf_index;
1977 old_qf_fnum = qf_ptr->qf_fnum;
1978 do
1979 {
1980 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1981 {
1982 qf_ptr = old_qf_ptr;
1983 qf_index = prev_index;
1984 if (err != NULL)
1985 {
1986 EMSG(_(err));
1987 goto theend;
1988 }
1989 errornr = 0;
1990 break;
1991 }
1992 --qf_index;
1993 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001994 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1995 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1997 err = NULL;
1998 }
1999 }
2000 else if (errornr != 0) /* go to specified number */
2001 {
2002 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2003 {
2004 --qf_index;
2005 qf_ptr = qf_ptr->qf_prev;
2006 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002007 while (errornr > qf_index && qf_index <
2008 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 && qf_ptr->qf_next != NULL)
2010 {
2011 ++qf_index;
2012 qf_ptr = qf_ptr->qf_next;
2013 }
2014 }
2015
2016#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2018 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 /* No need to print the error message if it's visible in the error
2020 * window */
2021 print_message = FALSE;
2022
2023 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002024 * For ":helpgrep" find a help window or open one.
2025 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002026 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002027 {
2028 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002029
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002030 if (cmdmod.tab != 0)
2031 wp = NULL;
2032 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002033 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002034 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2035 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002036 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2037 win_enter(wp, TRUE);
2038 else
2039 {
2040 /*
2041 * Split off help window; put it at far top if no position
2042 * specified, the current window is vertically split and narrow.
2043 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002044 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002045 if (cmdmod.split == 0 && curwin->w_width != Columns
2046 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002047 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002048 if (qi != &ql_info)
2049 flags |= WSP_NEWLOC; /* don't copy the location list */
2050
2051 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002052 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002053 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002054
2055 if (curwin->w_height < p_hh)
2056 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002057
2058 if (qi != &ql_info) /* not a quickfix list */
2059 {
2060 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002061 curwin->w_llist = qi;
2062 qi->qf_refcount++;
2063 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002064 }
2065
2066 if (!p_im)
2067 restart_edit = 0; /* don't want insert mode in help file */
2068 }
2069
2070 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 * If currently in the quickfix window, find another window to show the
2072 * file in.
2073 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002074 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002076 win_T *usable_win_ptr = NULL;
2077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 /*
2079 * If there is no file specified, we don't know where to go.
2080 * But do advance, otherwise ":cn" gets stuck.
2081 */
2082 if (qf_ptr->qf_fnum == 0)
2083 goto theend;
2084
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002085 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002086
2087 ll_ref = curwin->w_llist_ref;
2088 if (ll_ref != NULL)
2089 {
2090 /* Find a window using the same location list that is not a
2091 * quickfix window. */
2092 FOR_ALL_WINDOWS(usable_win_ptr)
2093 if (usable_win_ptr->w_llist == ll_ref
2094 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002095 {
2096 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002097 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002098 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002099 }
2100
2101 if (!usable_win)
2102 {
2103 /* Locate a window showing a normal buffer */
2104 FOR_ALL_WINDOWS(win)
2105 if (win->w_buffer->b_p_bt[0] == NUL)
2106 {
2107 usable_win = 1;
2108 break;
2109 }
2110 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002113 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002114 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002116 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002117 {
2118 tabpage_T *tp;
2119 win_T *wp;
2120
2121 FOR_ALL_TAB_WINDOWS(tp, wp)
2122 {
2123 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2124 {
2125 goto_tabpage_win(tp, wp);
2126 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002127 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002128 }
2129 }
2130 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002131win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002132
2133 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002134 * If there is only one window and it is the quickfix window, create a
2135 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002136 */
2137 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002139 flags = WSP_ABOVE;
2140 if (ll_ref != NULL)
2141 flags |= WSP_NEWLOC;
2142 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 goto failed; /* not enough room for window */
2144 opened_window = TRUE; /* close it when fail */
2145 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002146 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002147 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002148 if (ll_ref != NULL)
2149 {
2150 /* The new window should use the location list from the
2151 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152 curwin->w_llist = ll_ref;
2153 ll_ref->qf_refcount++;
2154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 else
2157 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 if (curwin->w_llist_ref != NULL)
2159 {
2160 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002161 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002162 if (win == NULL)
2163 {
2164 /* Find the window showing the selected file */
2165 FOR_ALL_WINDOWS(win)
2166 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2167 break;
2168 if (win == NULL)
2169 {
2170 /* Find a previous usable window */
2171 win = curwin;
2172 do
2173 {
2174 if (win->w_buffer->b_p_bt[0] == NUL)
2175 break;
2176 if (win->w_prev == NULL)
2177 win = lastwin; /* wrap around the top */
2178 else
2179 win = win->w_prev; /* go to previous window */
2180 } while (win != curwin);
2181 }
2182 }
2183 win_goto(win);
2184
2185 /* If the location list for the window is not set, then set it
2186 * to the location list from the location window */
2187 if (win->w_llist == NULL)
2188 {
2189 win->w_llist = ll_ref;
2190 ll_ref->qf_refcount++;
2191 }
2192 }
2193 else
2194 {
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 /*
2197 * Try to find a window that shows the right buffer.
2198 * Default to the window just above the quickfix buffer.
2199 */
2200 win = curwin;
2201 altwin = NULL;
2202 for (;;)
2203 {
2204 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2205 break;
2206 if (win->w_prev == NULL)
2207 win = lastwin; /* wrap around the top */
2208 else
2209 win = win->w_prev; /* go to previous window */
2210
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002211 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 /* Didn't find it, go to the window before the quickfix
2214 * window. */
2215 if (altwin != NULL)
2216 win = altwin;
2217 else if (curwin->w_prev != NULL)
2218 win = curwin->w_prev;
2219 else
2220 win = curwin->w_next;
2221 break;
2222 }
2223
2224 /* Remember a usable window. */
2225 if (altwin == NULL && !win->w_p_pvw
2226 && win->w_buffer->b_p_bt[0] == NUL)
2227 altwin = win;
2228 }
2229
2230 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233 }
2234#endif
2235
2236 /*
2237 * If there is a file name,
2238 * read the wanted file if needed, and check autowrite etc.
2239 */
2240 old_curbuf = curbuf;
2241 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002242
2243 if (qf_ptr->qf_fnum != 0)
2244 {
2245 if (qf_ptr->qf_type == 1)
2246 {
2247 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2248 * set b_p_ro flag). */
2249 if (!can_abandon(curbuf, forceit))
2250 {
2251 EMSG(_(e_nowrtmsg));
2252 ok = FALSE;
2253 }
2254 else
2255 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002256 ECMD_HIDE + ECMD_SET_HELP,
2257 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002258 }
2259 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002260 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002261 int old_qf_curlist = qi->qf_curlist;
2262 int is_abort = FALSE;
2263
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002264 ok = buflist_getfile(qf_ptr->qf_fnum,
2265 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002266 if (qi != &ql_info && !win_valid(oldwin))
2267 {
2268 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002269 is_abort = TRUE;
2270 opened_window = FALSE;
2271 }
2272 else if (old_qf_curlist != qi->qf_curlist
2273 || !is_qf_entry_present(qi, qf_ptr))
2274 {
2275 if (qi == &ql_info)
2276 EMSG(_("E925: Current quickfix was changed"));
2277 else
2278 EMSG(_("E926: Current location list was changed"));
2279 is_abort = TRUE;
2280 }
2281
2282 if (is_abort)
2283 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002284 ok = FALSE;
2285 qi = NULL;
2286 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002287 }
2288 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002289 }
2290
2291 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 /* When not switched to another buffer, still need to set pc mark */
2294 if (curbuf == old_curbuf)
2295 setpcmark();
2296
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002297 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002299 /*
2300 * Go to line with error, unless qf_lnum is 0.
2301 */
2302 i = qf_ptr->qf_lnum;
2303 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002305 if (i > curbuf->b_ml.ml_line_count)
2306 i = curbuf->b_ml.ml_line_count;
2307 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002309 if (qf_ptr->qf_col > 0)
2310 {
2311 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002312#ifdef FEAT_VIRTUALEDIT
2313 curwin->w_cursor.coladd = 0;
2314#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002315 if (qf_ptr->qf_viscol == TRUE)
2316 {
2317 /*
2318 * Check each character from the beginning of the error
2319 * line up to the error column. For each tab character
2320 * found, reduce the error column value by the length of
2321 * a tab character.
2322 */
2323 line = ml_get_curline();
2324 screen_col = 0;
2325 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2326 {
2327 if (*line == NUL)
2328 break;
2329 if (*line++ == '\t')
2330 {
2331 curwin->w_cursor.col -= 7 - (screen_col % 8);
2332 screen_col += 8 - (screen_col % 8);
2333 }
2334 else
2335 ++screen_col;
2336 }
2337 }
2338 check_cursor();
2339 }
2340 else
2341 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
2343 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002344 {
2345 pos_T save_cursor;
2346
2347 /* Move the cursor to the first line in the buffer */
2348 save_cursor = curwin->w_cursor;
2349 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002350 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2351 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002352 curwin->w_cursor = save_cursor;
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355#ifdef FEAT_FOLDING
2356 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2357 foldOpenCursor();
2358#endif
2359 if (print_message)
2360 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002361 /* Update the screen before showing the message, unless the screen
2362 * scrolled up. */
2363 if (!msg_scrolled)
2364 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002366 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2368 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2369 /* Add the message, skipping leading whitespace and newlines. */
2370 len = (int)STRLEN(IObuff);
2371 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2372
2373 /* Output the message. Overwrite to avoid scrolling when the 'O'
2374 * flag is present in 'shortmess'; But when not jumping, print the
2375 * whole message. */
2376 i = msg_scroll;
2377 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2378 msg_scroll = TRUE;
2379 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2380 msg_scroll = FALSE;
2381 msg_attr_keep(IObuff, 0, TRUE);
2382 msg_scroll = i;
2383 }
2384 }
2385 else
2386 {
2387#ifdef FEAT_WINDOWS
2388 if (opened_window)
2389 win_close(curwin, TRUE); /* Close opened window */
2390#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002391 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
2393 /*
2394 * Couldn't open file, so put index back where it was. This could
2395 * happen if the file was readonly and we changed something.
2396 */
2397#ifdef FEAT_WINDOWS
2398failed:
2399#endif
2400 qf_ptr = old_qf_ptr;
2401 qf_index = old_qf_index;
2402 }
2403 }
2404theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002405 if (qi != NULL)
2406 {
2407 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2408 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410#ifdef FEAT_WINDOWS
2411 if (p_swb != old_swb && opened_window)
2412 {
2413 /* Restore old 'switchbuf' value, but not when an autocommand or
2414 * modeline has changed the value. */
2415 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002418 swb_flags = old_swb_flags;
2419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 else
2421 free_string_option(old_swb);
2422 }
2423#endif
2424}
2425
2426/*
2427 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002428 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 */
2430 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002431qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002433 buf_T *buf;
2434 char_u *fname;
2435 qfline_T *qfp;
2436 int i;
2437 int idx1 = 1;
2438 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002439 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002440 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002441 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002443 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002445 if (eap->cmdidx == CMD_llist)
2446 {
2447 qi = GET_LOC_LIST(curwin);
2448 if (qi == NULL)
2449 {
2450 EMSG(_(e_loclist));
2451 return;
2452 }
2453 }
2454
2455 if (qi->qf_curlist >= qi->qf_listcount
2456 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 EMSG(_(e_quickfix));
2459 return;
2460 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002461 if (*arg == '+')
2462 {
2463 ++arg;
2464 plus = TRUE;
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2467 {
2468 EMSG(_(e_trailing));
2469 return;
2470 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002471 if (plus)
2472 {
2473 i = qi->qf_lists[qi->qf_curlist].qf_index;
2474 idx2 = i + idx1;
2475 idx1 = i;
2476 }
2477 else
2478 {
2479 i = qi->qf_lists[qi->qf_curlist].qf_count;
2480 if (idx1 < 0)
2481 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2482 if (idx2 < 0)
2483 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002486 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002488 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2489 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2492 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002493 msg_putchar('\n');
2494 if (got_int)
2495 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002496
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002497 fname = NULL;
2498 if (qfp->qf_fnum != 0
2499 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2500 {
2501 fname = buf->b_fname;
2502 if (qfp->qf_type == 1) /* :helpgrep */
2503 fname = gettail(fname);
2504 }
2505 if (fname == NULL)
2506 sprintf((char *)IObuff, "%2d", i);
2507 else
2508 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2509 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002510 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002511 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2512 if (qfp->qf_lnum == 0)
2513 IObuff[0] = NUL;
2514 else if (qfp->qf_col == 0)
2515 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2516 else
2517 sprintf((char *)IObuff, ":%ld col %d",
2518 qfp->qf_lnum, qfp->qf_col);
2519 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2520 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2521 msg_puts_attr(IObuff, hl_attr(HLF_N));
2522 if (qfp->qf_pattern != NULL)
2523 {
2524 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2525 STRCAT(IObuff, ":");
2526 msg_puts(IObuff);
2527 }
2528 msg_puts((char_u *)" ");
2529
2530 /* Remove newlines and leading whitespace from the text. For an
2531 * unrecognized line keep the indent, the compiler may mark a word
2532 * with ^^^^. */
2533 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2535 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002536 msg_prt_line(IObuff, FALSE);
2537 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002539
2540 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002541 if (qfp == NULL)
2542 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002543 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 ui_breakcheck();
2545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546}
2547
2548/*
2549 * Remove newlines and leading whitespace from an error message.
2550 * Put the result in "buf[bufsize]".
2551 */
2552 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002553qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 int i;
2556 char_u *p = text;
2557
2558 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2559 {
2560 if (*p == '\n')
2561 {
2562 buf[i] = ' ';
2563 while (*++p != NUL)
2564 if (!vim_iswhite(*p) && *p != '\n')
2565 break;
2566 }
2567 else
2568 buf[i] = *p++;
2569 }
2570 buf[i] = NUL;
2571}
2572
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002573 static void
2574qf_msg(qf_info_T *qi, int which, char *lead)
2575{
2576 char *title = (char *)qi->qf_lists[which].qf_title;
2577 int count = qi->qf_lists[which].qf_count;
2578 char_u buf[IOSIZE];
2579
2580 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2581 lead,
2582 which + 1,
2583 qi->qf_listcount,
2584 count);
2585
2586 if (title != NULL)
2587 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002588 size_t len = STRLEN(buf);
2589
2590 if (len < 34)
2591 {
2592 vim_memset(buf + len, ' ', 34 - len);
2593 buf[34] = NUL;
2594 }
2595 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002596 }
2597 trunc_string(buf, buf, Columns - 1, IOSIZE);
2598 msg(buf);
2599}
2600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601/*
2602 * ":colder [count]": Up in the quickfix stack.
2603 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002604 * ":lolder [count]": Up in the location list stack.
2605 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 */
2607 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002608qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002610 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 int count;
2612
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002613 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2614 {
2615 qi = GET_LOC_LIST(curwin);
2616 if (qi == NULL)
2617 {
2618 EMSG(_(e_loclist));
2619 return;
2620 }
2621 }
2622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 if (eap->addr_count != 0)
2624 count = eap->line2;
2625 else
2626 count = 1;
2627 while (count--)
2628 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002629 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002631 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
2633 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002636 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
2638 else
2639 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002640 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
2642 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002643 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002645 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
2647 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002648 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002650 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651#endif
2652}
2653
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002654 void
2655qf_history(exarg_T *eap)
2656{
2657 qf_info_T *qi = &ql_info;
2658 int i;
2659
2660 if (eap->cmdidx == CMD_lhistory)
2661 qi = GET_LOC_LIST(curwin);
2662 if (qi == NULL || (qi->qf_listcount == 0
2663 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2664 MSG(_("No entries"));
2665 else
2666 for (i = 0; i < qi->qf_listcount; ++i)
2667 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2668}
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002671 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 */
2673 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002674qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002676 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002677 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002678 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002680 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002682 qfp = qi->qf_lists[idx].qf_start;
2683 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002684 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002685 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002686 vim_free(qfp->qf_text);
2687 stop = (qfp == qfpnext);
2688 vim_free(qfp->qf_pattern);
2689 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002690 if (stop)
2691 /* Somehow qf_count may have an incorrect value, set it to 1
2692 * to avoid crashing when it's wrong.
2693 * TODO: Avoid qf_count being incorrect. */
2694 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002695 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002696 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002697 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002699 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002700 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002701 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002702
2703 qf_clean_dir_stack(&qi->qf_dir_stack);
2704 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705}
2706
2707/*
2708 * qf_mark_adjust: adjust marks
2709 */
2710 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002711qf_mark_adjust(
2712 win_T *wp,
2713 linenr_T line1,
2714 linenr_T line2,
2715 long amount,
2716 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002718 int i;
2719 qfline_T *qfp;
2720 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002721 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002722 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002723 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaarc1542742016-07-20 21:44:37 +02002725 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002726 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002727 if (wp != NULL)
2728 {
2729 if (wp->w_llist == NULL)
2730 return;
2731 qi = wp->w_llist;
2732 }
2733
2734 for (idx = 0; idx < qi->qf_listcount; ++idx)
2735 if (qi->qf_lists[idx].qf_count)
2736 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002737 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2738 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 if (qfp->qf_fnum == curbuf->b_fnum)
2740 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002741 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2743 {
2744 if (amount == MAXLNUM)
2745 qfp->qf_cleared = TRUE;
2746 else
2747 qfp->qf_lnum += amount;
2748 }
2749 else if (amount_after && qfp->qf_lnum > line2)
2750 qfp->qf_lnum += amount_after;
2751 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002752
2753 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002754 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755}
2756
2757/*
2758 * Make a nice message out of the error character and the error number:
2759 * char number message
2760 * e or E 0 " error"
2761 * w or W 0 " warning"
2762 * i or I 0 " info"
2763 * 0 0 ""
2764 * other 0 " c"
2765 * e or E n " error n"
2766 * w or W n " warning n"
2767 * i or I n " info n"
2768 * 0 n " error n"
2769 * other n " c n"
2770 * 1 x "" :helpgrep
2771 */
2772 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002773qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 static char_u buf[20];
2776 static char_u cc[3];
2777 char_u *p;
2778
2779 if (c == 'W' || c == 'w')
2780 p = (char_u *)" warning";
2781 else if (c == 'I' || c == 'i')
2782 p = (char_u *)" info";
2783 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2784 p = (char_u *)" error";
2785 else if (c == 0 || c == 1)
2786 p = (char_u *)"";
2787 else
2788 {
2789 cc[0] = ' ';
2790 cc[1] = c;
2791 cc[2] = NUL;
2792 p = cc;
2793 }
2794
2795 if (nr <= 0)
2796 return p;
2797
2798 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2799 return buf;
2800}
2801
2802#if defined(FEAT_WINDOWS) || defined(PROTO)
2803/*
2804 * ":cwindow": open the quickfix window if we have errors to display,
2805 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002806 * ":lwindow": open the location list window if we have locations to display,
2807 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002810ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002812 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 win_T *win;
2814
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002815 if (eap->cmdidx == CMD_lwindow)
2816 {
2817 qi = GET_LOC_LIST(curwin);
2818 if (qi == NULL)
2819 return;
2820 }
2821
2822 /* Look for an existing quickfix window. */
2823 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824
2825 /*
2826 * If a quickfix window is open but we have no errors to display,
2827 * close the window. If a quickfix window is not open, then open
2828 * it if we have errors; otherwise, leave it closed.
2829 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002830 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002831 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002832 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 if (win != NULL)
2835 ex_cclose(eap);
2836 }
2837 else if (win == NULL)
2838 ex_copen(eap);
2839}
2840
2841/*
2842 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002843 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002846ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002848 win_T *win = NULL;
2849 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002851 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2852 {
2853 qi = GET_LOC_LIST(curwin);
2854 if (qi == NULL)
2855 return;
2856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002858 /* Find existing quickfix window and close it. */
2859 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (win != NULL)
2861 win_close(win, FALSE);
2862}
2863
2864/*
2865 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002866 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 */
2868 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002869ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002871 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002874 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002875 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002876 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002878 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2879 {
2880 qi = GET_LOC_LIST(curwin);
2881 if (qi == NULL)
2882 {
2883 EMSG(_(e_loclist));
2884 return;
2885 }
2886 }
2887
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (eap->addr_count != 0)
2889 height = eap->line2;
2890 else
2891 height = QF_WINHEIGHT;
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894#ifdef FEAT_GUI
2895 need_mouse_correct = TRUE;
2896#endif
2897
2898 /*
2899 * Find existing quickfix window, or open a new one.
2900 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002901 win = qf_find_win(qi);
2902
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002903 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002906 if (eap->addr_count != 0)
2907 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002908 if (cmdmod.split & WSP_VERT)
2909 {
2910 if (height != W_WIDTH(win))
2911 win_setwidth(height);
2912 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002913 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002914 win_setheight(height);
2915 }
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 else
2918 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002919 qf_buf = qf_find_buf(qi);
2920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 /* The current window becomes the previous window afterwards. */
2922 win = curwin;
2923
Bram Moolenaar77642c02012-11-20 17:55:10 +01002924 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2925 && cmdmod.split == 0)
2926 /* Create the new window at the very bottom, except when
2927 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002928 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002929 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002931 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002933 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002935 /*
2936 * For the location list window, create a reference to the
2937 * location list from the window 'win'.
2938 */
2939 curwin->w_llist_ref = win->w_llist;
2940 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002942
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002943 if (oldwin != curwin)
2944 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002945 if (qf_buf != NULL)
2946 /* Use the existing quickfix buffer */
2947 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002948 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002949 else
2950 {
2951 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002952 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002953 /* switch off 'swapfile' */
2954 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2955 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002956 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002957 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002958 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002959#ifdef FEAT_DIFF
2960 curwin->w_p_diff = FALSE;
2961#endif
2962#ifdef FEAT_FOLDING
2963 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2964 OPT_LOCAL);
2965#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002968 /* Only set the height when still in the same tab page and there is no
2969 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002970 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 win_setheight(height);
2972 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2973 if (win_valid(win))
2974 prevwin = win;
2975 }
2976
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002977 qf_set_title_var(qi);
2978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 /*
2980 * Fill the buffer with the quickfix list.
2981 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002982 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002984 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 curwin->w_cursor.col = 0;
2986 check_cursor();
2987 update_topline(); /* scroll to show the line */
2988}
2989
2990/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02002991 * Move the cursor in the quickfix window to "lnum".
2992 */
2993 static void
2994qf_win_goto(win_T *win, linenr_T lnum)
2995{
2996 win_T *old_curwin = curwin;
2997
2998 curwin = win;
2999 curbuf = win->w_buffer;
3000 curwin->w_cursor.lnum = lnum;
3001 curwin->w_cursor.col = 0;
3002#ifdef FEAT_VIRTUALEDIT
3003 curwin->w_cursor.coladd = 0;
3004#endif
3005 curwin->w_curswant = 0;
3006 update_topline(); /* scroll to show the line */
3007 redraw_later(VALID);
3008 curwin->w_redr_status = TRUE; /* update ruler */
3009 curwin = old_curwin;
3010 curbuf = curwin->w_buffer;
3011}
3012
3013/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003014 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003015 */
3016 void
3017ex_cbottom(exarg_T *eap UNUSED)
3018{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003019 qf_info_T *qi = &ql_info;
3020 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003021
Bram Moolenaar537ef082016-07-09 17:56:19 +02003022 if (eap->cmdidx == CMD_lbottom)
3023 {
3024 qi = GET_LOC_LIST(curwin);
3025 if (qi == NULL)
3026 {
3027 EMSG(_(e_loclist));
3028 return;
3029 }
3030 }
3031
3032 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003033 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3034 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3035}
3036
3037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 * Return the number of the current entry (line number in the quickfix
3039 * window).
3040 */
3041 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003042qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003044 qf_info_T *qi = &ql_info;
3045
3046 if (IS_LL_WINDOW(wp))
3047 /* In the location list window, use the referenced location list */
3048 qi = wp->w_llist_ref;
3049
3050 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051}
3052
3053/*
3054 * Update the cursor position in the quickfix window to the current error.
3055 * Return TRUE if there is a quickfix window.
3056 */
3057 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003058qf_win_pos_update(
3059 qf_info_T *qi,
3060 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003063 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 /*
3066 * Put the cursor on the current error in the quickfix window, so that
3067 * it's viewable.
3068 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003069 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if (win != NULL
3071 && qf_index <= win->w_buffer->b_ml.ml_line_count
3072 && old_qf_index != qf_index)
3073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 if (qf_index > old_qf_index)
3075 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003076 win->w_redraw_top = old_qf_index;
3077 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 }
3079 else
3080 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003081 win->w_redraw_top = qf_index;
3082 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003084 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
3086 return win != NULL;
3087}
3088
3089/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003090 * Check whether the given window is displaying the specified quickfix/location
3091 * list buffer
3092 */
3093 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003094is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003095{
3096 /*
3097 * A window displaying the quickfix buffer will have the w_llist_ref field
3098 * set to NULL.
3099 * A window displaying a location list buffer will have the w_llist_ref
3100 * pointing to the location list.
3101 */
3102 if (bt_quickfix(win->w_buffer))
3103 if ((qi == &ql_info && win->w_llist_ref == NULL)
3104 || (qi != &ql_info && win->w_llist_ref == qi))
3105 return TRUE;
3106
3107 return FALSE;
3108}
3109
3110/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003111 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003112 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003113 */
3114 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003115qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003116{
3117 win_T *win;
3118
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003119 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003120 if (is_qf_win(win, qi))
3121 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003122
3123 return win;
3124}
3125
3126/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003127 * Find a quickfix buffer.
3128 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 */
3130 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003131qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003133 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003134 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
Bram Moolenaar9c102382006-05-03 21:26:49 +00003136 FOR_ALL_TAB_WINDOWS(tp, win)
3137 if (is_qf_win(win, qi))
3138 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003139
Bram Moolenaar9c102382006-05-03 21:26:49 +00003140 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141}
3142
3143/*
3144 * Find the quickfix buffer. If it exists, update the contents.
3145 */
3146 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003147qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
3149 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003150 win_T *win;
3151 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
3154 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003155 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (buf != NULL)
3157 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003158 linenr_T old_line_count = buf->b_ml.ml_line_count;
3159
3160 if (old_last == NULL)
3161 /* set curwin/curbuf to buf and save a few things */
3162 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003164 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003165 {
3166 curwin_save = curwin;
3167 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02003168 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003169 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003170 }
3171
Bram Moolenaar864293a2016-06-02 13:40:04 +02003172 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003173
Bram Moolenaar864293a2016-06-02 13:40:04 +02003174 if (old_last == NULL)
3175 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003176 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003177
3178 /* restore curwin/curbuf and a few other things */
3179 aucmd_restbuf(&aco);
3180 }
3181
3182 /* Only redraw when added lines are visible. This avoids flickering
3183 * when the added lines are not visible. */
3184 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3185 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187}
3188
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003189/*
3190 * Set "w:quickfix_title" if "qi" has a title.
3191 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003192 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003193qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003194{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003195 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3196 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003197 qi->qf_lists[qi->qf_curlist].qf_title);
3198}
3199
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200/*
3201 * Fill current buffer with quickfix errors, replacing any previous contents.
3202 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003203 * If "old_last" is not NULL append the items after this one.
3204 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3205 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 */
3207 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003208qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003210 linenr_T lnum;
3211 qfline_T *qfp;
3212 buf_T *errbuf;
3213 int len;
3214 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Bram Moolenaar864293a2016-06-02 13:40:04 +02003216 if (old_last == NULL)
3217 {
3218 if (buf != curbuf)
3219 {
3220 EMSG2(_(e_intern2), "qf_fill_buffer()");
3221 return;
3222 }
3223
3224 /* delete all existing lines */
3225 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3226 (void)ml_delete((linenr_T)1, FALSE);
3227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
3229 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003230 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
3232 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003233 if (old_last == NULL)
3234 {
3235 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3236 lnum = 0;
3237 }
3238 else
3239 {
3240 qfp = old_last->qf_next;
3241 lnum = buf->b_ml.ml_line_count;
3242 }
3243 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 {
3245 if (qfp->qf_fnum != 0
3246 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3247 && errbuf->b_fname != NULL)
3248 {
3249 if (qfp->qf_type == 1) /* :helpgrep */
3250 STRCPY(IObuff, gettail(errbuf->b_fname));
3251 else
3252 STRCPY(IObuff, errbuf->b_fname);
3253 len = (int)STRLEN(IObuff);
3254 }
3255 else
3256 len = 0;
3257 IObuff[len++] = '|';
3258
3259 if (qfp->qf_lnum > 0)
3260 {
3261 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3262 len += (int)STRLEN(IObuff + len);
3263
3264 if (qfp->qf_col > 0)
3265 {
3266 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3267 len += (int)STRLEN(IObuff + len);
3268 }
3269
3270 sprintf((char *)IObuff + len, "%s",
3271 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3272 len += (int)STRLEN(IObuff + len);
3273 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003274 else if (qfp->qf_pattern != NULL)
3275 {
3276 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3277 len += (int)STRLEN(IObuff + len);
3278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 IObuff[len++] = '|';
3280 IObuff[len++] = ' ';
3281
3282 /* Remove newlines and leading whitespace from the text.
3283 * For an unrecognized line keep the indent, the compiler may
3284 * mark a word with ^^^^. */
3285 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3286 IObuff + len, IOSIZE - len);
3287
Bram Moolenaar864293a2016-06-02 13:40:04 +02003288 if (ml_append_buf(buf, lnum, IObuff,
3289 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003291 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003293 if (qfp == NULL)
3294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003296
3297 if (old_last == NULL)
3298 /* Delete the empty line which is now at the end */
3299 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301
3302 /* correct cursor position */
3303 check_lnums(TRUE);
3304
Bram Moolenaar864293a2016-06-02 13:40:04 +02003305 if (old_last == NULL)
3306 {
3307 /* Set the 'filetype' to "qf" each time after filling the buffer.
3308 * This resembles reading a file into a buffer, it's more logical when
3309 * using autocommands. */
3310 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3311 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
3313#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003314 keep_filetype = TRUE; /* don't detect 'filetype' */
3315 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003317 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003319 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003321 /* make sure it will be redrawn */
3322 redraw_curbuf_later(NOT_VALID);
3323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
3325 /* Restore KeyTyped, setting 'filetype' may reset it. */
3326 KeyTyped = old_KeyTyped;
3327}
3328
3329#endif /* FEAT_WINDOWS */
3330
3331/*
3332 * Return TRUE if "buf" is the quickfix buffer.
3333 */
3334 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003335bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003337 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338}
3339
3340/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003341 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3342 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 */
3344 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003345bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003347 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3348 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349}
3350
3351/*
3352 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3353 */
3354 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003355bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003357 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358}
3359
3360 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003361bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 if (bt_dontwrite(buf))
3364 {
3365 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3366 return TRUE;
3367 }
3368 return FALSE;
3369}
3370
3371/*
3372 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3373 * and 'bufhidden'.
3374 */
3375 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003376buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3379 switch (buf->b_p_bh[0])
3380 {
3381 case 'u': /* "unload" */
3382 case 'w': /* "wipe" */
3383 case 'd': return FALSE; /* "delete" */
3384 case 'h': return TRUE; /* "hide" */
3385 }
3386 return (p_hid || cmdmod.hide);
3387}
3388
3389/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003390 * Return TRUE when using ":vimgrep" for ":grep".
3391 */
3392 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003393grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003394{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003395 return ((cmdidx == CMD_grep
3396 || cmdidx == CMD_lgrep
3397 || cmdidx == CMD_grepadd
3398 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003399 && STRCMP("internal",
3400 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3401}
3402
3403/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003404 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 */
3406 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003407ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003409 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 char_u *cmd;
3411 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003412 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003413 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003414 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003415#ifdef FEAT_AUTOCMD
3416 char_u *au_name = NULL;
3417
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003418 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3419 if (grep_internal(eap->cmdidx))
3420 {
3421 ex_vimgrep(eap);
3422 return;
3423 }
3424
Bram Moolenaar7c626922005-02-07 22:01:03 +00003425 switch (eap->cmdidx)
3426 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003427 case CMD_make: au_name = (char_u *)"make"; break;
3428 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3429 case CMD_grep: au_name = (char_u *)"grep"; break;
3430 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3431 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3432 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003433 default: break;
3434 }
3435 if (au_name != NULL)
3436 {
3437 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3438 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003439# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003440 if (did_throw || force_abort)
3441 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003442# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003443 }
3444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaara6557602006-02-04 22:43:20 +00003446 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3447 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003448 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003451 fname = get_mef_name();
3452 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003454 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
3456 /*
3457 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3458 */
3459 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3460 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003461 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 cmd = alloc(len);
3463 if (cmd == NULL)
3464 return;
3465 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3466 (char *)p_shq);
3467 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003468 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 /*
3470 * Output a newline if there's something else than the :make command that
3471 * was typed (in which case the cursor is in column 0).
3472 */
3473 if (msg_col == 0)
3474 msg_didout = FALSE;
3475 msg_start();
3476 MSG_PUTS(":!");
3477 msg_outtrans(cmd); /* show what we are doing */
3478
3479 /* let the shell know if we are redirecting output or not */
3480 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3481
3482#ifdef AMIGA
3483 out_flush();
3484 /* read window status report and redraw before message */
3485 (void)char_avail();
3486#endif
3487
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003488 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003489 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3490 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003491 && eap->cmdidx != CMD_lgrepadd),
3492 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003493 if (wp != NULL)
3494 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003495#ifdef FEAT_AUTOCMD
3496 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003497 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003498 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3499 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003500 if (qi->qf_curlist < qi->qf_listcount)
3501 res = qi->qf_lists[qi->qf_curlist].qf_count;
3502 else
3503 res = 0;
3504 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003505#endif
3506 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003507 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
Bram Moolenaar7c626922005-02-07 22:01:03 +00003509 mch_remove(fname);
3510 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 vim_free(cmd);
3512}
3513
3514/*
3515 * Return the name for the errorfile, in allocated memory.
3516 * Find a new unique name when 'makeef' contains "##".
3517 * Returns NULL for error.
3518 */
3519 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003520get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
3522 char_u *p;
3523 char_u *name;
3524 static int start = -1;
3525 static int off = 0;
3526#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003527 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528#endif
3529
3530 if (*p_mef == NUL)
3531 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003532 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (name == NULL)
3534 EMSG(_(e_notmp));
3535 return name;
3536 }
3537
3538 for (p = p_mef; *p; ++p)
3539 if (p[0] == '#' && p[1] == '#')
3540 break;
3541
3542 if (*p == NUL)
3543 return vim_strsave(p_mef);
3544
3545 /* Keep trying until the name doesn't exist yet. */
3546 for (;;)
3547 {
3548 if (start == -1)
3549 start = mch_get_pid();
3550 else
3551 off += 19;
3552
3553 name = alloc((unsigned)STRLEN(p_mef) + 30);
3554 if (name == NULL)
3555 break;
3556 STRCPY(name, p_mef);
3557 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3558 STRCAT(name, p + 2);
3559 if (mch_getperm(name) < 0
3560#ifdef HAVE_LSTAT
3561 /* Don't accept a symbolic link, its a security risk. */
3562 && mch_lstat((char *)name, &sb) < 0
3563#endif
3564 )
3565 break;
3566 vim_free(name);
3567 }
3568 return name;
3569}
3570
3571/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003572 * Returns the number of valid entries in the current quickfix/location list.
3573 */
3574 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003575qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003576{
3577 qf_info_T *qi = &ql_info;
3578 qfline_T *qfp;
3579 int i, sz = 0;
3580 int prev_fnum = 0;
3581
3582 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3583 {
3584 /* Location list */
3585 qi = GET_LOC_LIST(curwin);
3586 if (qi == NULL)
3587 return 0;
3588 }
3589
3590 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003591 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003592 ++i, qfp = qfp->qf_next)
3593 {
3594 if (qfp->qf_valid)
3595 {
3596 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3597 sz++; /* Count all valid entries */
3598 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3599 {
3600 /* Count the number of files */
3601 sz++;
3602 prev_fnum = qfp->qf_fnum;
3603 }
3604 }
3605 }
3606
3607 return sz;
3608}
3609
3610/*
3611 * Returns the current index of the quickfix/location list.
3612 * Returns 0 if there is an error.
3613 */
3614 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003615qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003616{
3617 qf_info_T *qi = &ql_info;
3618
3619 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3620 {
3621 /* Location list */
3622 qi = GET_LOC_LIST(curwin);
3623 if (qi == NULL)
3624 return 0;
3625 }
3626
3627 return qi->qf_lists[qi->qf_curlist].qf_index;
3628}
3629
3630/*
3631 * Returns the current index in the quickfix/location list (counting only valid
3632 * entries). If no valid entries are in the list, then returns 1.
3633 */
3634 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003635qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003636{
3637 qf_info_T *qi = &ql_info;
3638 qf_list_T *qfl;
3639 qfline_T *qfp;
3640 int i, eidx = 0;
3641 int prev_fnum = 0;
3642
3643 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3644 {
3645 /* Location list */
3646 qi = GET_LOC_LIST(curwin);
3647 if (qi == NULL)
3648 return 1;
3649 }
3650
3651 qfl = &qi->qf_lists[qi->qf_curlist];
3652 qfp = qfl->qf_start;
3653
3654 /* check if the list has valid errors */
3655 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3656 return 1;
3657
3658 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3659 {
3660 if (qfp->qf_valid)
3661 {
3662 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3663 {
3664 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3665 {
3666 /* Count the number of files */
3667 eidx++;
3668 prev_fnum = qfp->qf_fnum;
3669 }
3670 }
3671 else
3672 eidx++;
3673 }
3674 }
3675
3676 return eidx ? eidx : 1;
3677}
3678
3679/*
3680 * Get the 'n'th valid error entry in the quickfix or location list.
3681 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3682 * For :cdo and :ldo returns the 'n'th valid error entry.
3683 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3684 */
3685 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003686qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003687{
3688 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3689 qfline_T *qfp = qfl->qf_start;
3690 int i, eidx;
3691 int prev_fnum = 0;
3692
3693 /* check if the list has valid errors */
3694 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3695 return 1;
3696
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003697 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003698 i++, qfp = qfp->qf_next)
3699 {
3700 if (qfp->qf_valid)
3701 {
3702 if (fdo)
3703 {
3704 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3705 {
3706 /* Count the number of files */
3707 eidx++;
3708 prev_fnum = qfp->qf_fnum;
3709 }
3710 }
3711 else
3712 eidx++;
3713 }
3714
3715 if (eidx == n)
3716 break;
3717 }
3718
3719 if (i <= qfl->qf_count)
3720 return i;
3721 else
3722 return 1;
3723}
3724
3725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003727 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003728 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 */
3730 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003731ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003733 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003734 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003735
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003736 if (eap->cmdidx == CMD_ll
3737 || eap->cmdidx == CMD_lrewind
3738 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003739 || eap->cmdidx == CMD_llast
3740 || eap->cmdidx == CMD_ldo
3741 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003742 {
3743 qi = GET_LOC_LIST(curwin);
3744 if (qi == NULL)
3745 {
3746 EMSG(_(e_loclist));
3747 return;
3748 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003749 }
3750
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003751 if (eap->addr_count > 0)
3752 errornr = (int)eap->line2;
3753 else
3754 {
3755 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3756 errornr = 0;
3757 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3758 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3759 errornr = 1;
3760 else
3761 errornr = 32767;
3762 }
3763
3764 /* For cdo and ldo commands, jump to the nth valid error.
3765 * For cfdo and lfdo commands, jump to the nth valid file entry.
3766 */
3767 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3768 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3769 errornr = qf_get_nth_valid_entry(qi,
3770 eap->addr_count > 0 ? (int)eap->line1 : 1,
3771 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3772
3773 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774}
3775
3776/*
3777 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003778 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003779 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 */
3781 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003782ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003784 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003785 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003786
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003787 if (eap->cmdidx == CMD_lnext
3788 || eap->cmdidx == CMD_lNext
3789 || eap->cmdidx == CMD_lprevious
3790 || eap->cmdidx == CMD_lnfile
3791 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003792 || eap->cmdidx == CMD_lpfile
3793 || eap->cmdidx == CMD_ldo
3794 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003795 {
3796 qi = GET_LOC_LIST(curwin);
3797 if (qi == NULL)
3798 {
3799 EMSG(_(e_loclist));
3800 return;
3801 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003802 }
3803
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003804 if (eap->addr_count > 0 &&
3805 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3806 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3807 errornr = (int)eap->line2;
3808 else
3809 errornr = 1;
3810
3811 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3812 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003814 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3815 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003817 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3818 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 ? BACKWARD_FILE
3820 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003821 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822}
3823
3824/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003825 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003826 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 */
3828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003829ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003831 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003832 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003833#ifdef FEAT_AUTOCMD
3834 char_u *au_name = NULL;
3835#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003836
3837 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003838 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003839 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003840
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003841#ifdef FEAT_AUTOCMD
3842 switch (eap->cmdidx)
3843 {
3844 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3845 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3846 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3847 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3848 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3849 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3850 default: break;
3851 }
3852 if (au_name != NULL)
3853 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3854#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003855#ifdef FEAT_BROWSE
3856 if (cmdmod.browse)
3857 {
3858 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3859 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3860 if (browse_file == NULL)
3861 return;
3862 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3863 vim_free(browse_file);
3864 }
3865 else
3866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003868 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003869
3870 /*
3871 * This function is used by the :cfile, :cgetfile and :caddfile
3872 * commands.
3873 * :cfile always creates a new quickfix list and jumps to the
3874 * first error.
3875 * :cgetfile creates a new quickfix list but doesn't jump to the
3876 * first error.
3877 * :caddfile adds to an existing quickfix list. If there is no
3878 * quickfix list then a new list is created.
3879 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003880 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003881 && eap->cmdidx != CMD_laddfile),
3882 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003883 && (eap->cmdidx == CMD_cfile
3884 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003885 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003886#ifdef FEAT_AUTOCMD
3887 if (au_name != NULL)
3888 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3889#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003890 if (wp != NULL)
3891 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003892 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003893 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003894
3895 else
3896 {
3897#ifdef FEAT_AUTOCMD
3898 if (au_name != NULL)
3899 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3900#endif
3901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902}
3903
3904/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003905 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003906 * ":vimgrepadd {pattern} file(s)"
3907 * ":lvimgrep {pattern} file(s)"
3908 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003909 */
3910 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003911ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003912{
Bram Moolenaar81695252004-12-29 20:58:21 +00003913 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003914 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003915 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003916 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003917 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003918 char_u *s;
3919 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003920 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003921 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003922#ifdef FEAT_AUTOCMD
3923 qfline_T *cur_qf_start;
3924#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003925 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003926 buf_T *buf;
3927 int duplicate_name = FALSE;
3928 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003929 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003930 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003931 buf_T *first_match_buf = NULL;
3932 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003933 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003934#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3935 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003936#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003937 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003938 int flags = 0;
3939 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003940 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003941 char_u *dirname_start = NULL;
3942 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003943 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003944#ifdef FEAT_AUTOCMD
3945 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003946
3947 switch (eap->cmdidx)
3948 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003949 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3950 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3951 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003952 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003953 case CMD_grep: au_name = (char_u *)"grep"; break;
3954 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3955 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3956 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003957 default: break;
3958 }
3959 if (au_name != NULL)
3960 {
3961 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3962 curbuf->b_fname, TRUE, curbuf);
3963 if (did_throw || force_abort)
3964 return;
3965 }
3966#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003967
Bram Moolenaar754b5602006-02-09 23:53:20 +00003968 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003969 || eap->cmdidx == CMD_lvimgrep
3970 || eap->cmdidx == CMD_lgrepadd
3971 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003972 {
3973 qi = ll_get_or_alloc_list(curwin);
3974 if (qi == NULL)
3975 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003976 }
3977
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003978 if (eap->addr_count > 0)
3979 tomatch = eap->line2;
3980 else
3981 tomatch = MAXLNUM;
3982
Bram Moolenaar81695252004-12-29 20:58:21 +00003983 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003984 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003985 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003986 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003987 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003988 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003989 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003990 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003991 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003992
3993 if (s != NULL && *s == NUL)
3994 {
3995 /* Pattern is empty, use last search pattern. */
3996 if (last_search_pat() == NULL)
3997 {
3998 EMSG(_(e_noprevre));
3999 goto theend;
4000 }
4001 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4002 }
4003 else
4004 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4005
Bram Moolenaar86b68352004-12-27 21:59:20 +00004006 if (regmatch.regprog == NULL)
4007 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004008 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004009 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004010
4011 p = skipwhite(p);
4012 if (*p == NUL)
4013 {
4014 EMSG(_("E683: File name missing or invalid pattern"));
4015 goto theend;
4016 }
4017
Bram Moolenaar754b5602006-02-09 23:53:20 +00004018 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004019 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004020 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004021 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004022 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004023
4024 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004025 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004026 goto theend;
4027 if (fcount == 0)
4028 {
4029 EMSG(_(e_nomatch));
4030 goto theend;
4031 }
4032
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004033 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4034 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004035 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004036 {
4037 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004038 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004039 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004040
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004041 /* Remember the current directory, because a BufRead autocommand that does
4042 * ":lcd %:p:h" changes the meaning of short path names. */
4043 mch_dirname(dirname_start, MAXPATHL);
4044
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004045#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004046 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004047 * changing the current quickfix list. */
4048 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4049#endif
4050
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004051 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004052 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004053 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004054 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004055 if (time(NULL) > seconds)
4056 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004057 /* Display the file name every second or so, show the user we are
4058 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004059 seconds = time(NULL);
4060 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004061 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004062 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004063 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004064 else
4065 {
4066 msg_outtrans(p);
4067 vim_free(p);
4068 }
4069 msg_clr_eos();
4070 msg_didout = FALSE; /* overwrite this message */
4071 msg_nowait = TRUE; /* don't wait for this message */
4072 msg_col = 0;
4073 out_flush();
4074 }
4075
Bram Moolenaar81695252004-12-29 20:58:21 +00004076 buf = buflist_findname_exp(fnames[fi]);
4077 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4078 {
4079 /* Remember that a buffer with this name already exists. */
4080 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004081 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004082 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004083
4084#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4085 /* Don't do Filetype autocommands to avoid loading syntax and
4086 * indent scripts, a great speed improvement. */
4087 save_ei = au_event_disable(",Filetype");
4088#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004089 /* Don't use modelines here, it's useless. */
4090 save_mls = p_mls;
4091 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004092
4093 /* Load file into a buffer, so that 'fileencoding' is detected,
4094 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004095 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004096
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004097 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004098#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4099 au_event_restore(save_ei);
4100#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004101 }
4102 else
4103 /* Use existing, loaded buffer. */
4104 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004105
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004106#ifdef FEAT_AUTOCMD
4107 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4108 {
4109 int idx;
4110
4111 /* Autocommands changed the quickfix list. Find the one we were
4112 * using and restore it. */
4113 for (idx = 0; idx < LISTCOUNT; ++idx)
4114 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4115 {
4116 qi->qf_curlist = idx;
4117 break;
4118 }
4119 if (idx == LISTCOUNT)
4120 {
4121 /* List cannot be found, create a new one. */
4122 qf_new_list(qi, *eap->cmdlinep);
4123 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4124 }
4125 }
4126#endif
4127
Bram Moolenaar81695252004-12-29 20:58:21 +00004128 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004129 {
4130 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004131 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004132 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004133 else
4134 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004135 /* Try for a match in all lines of the buffer.
4136 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004137 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004138 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4139 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004140 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004141 col = 0;
4142 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004143 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004144 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004145 /* Pass the buffer number so that it gets used even for a
4146 * dummy buffer, unless duplicate_name is set, then the
4147 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004148 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004149 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004150 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004151 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004152 ml_get_buf(buf,
4153 regmatch.startpos[0].lnum + lnum, FALSE),
4154 regmatch.startpos[0].lnum + lnum,
4155 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004156 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004157 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004158 0, /* nr */
4159 0, /* type */
4160 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004161 ) == FAIL)
4162 {
4163 got_int = TRUE;
4164 break;
4165 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004166 found_match = TRUE;
4167 if (--tomatch == 0)
4168 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004169 if ((flags & VGR_GLOBAL) == 0
4170 || regmatch.endpos[0].lnum > 0)
4171 break;
4172 col = regmatch.endpos[0].col
4173 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004174 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004175 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004176 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004177 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004178 if (got_int)
4179 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004180 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004181#ifdef FEAT_AUTOCMD
4182 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4183#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004184
4185 if (using_dummy)
4186 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004187 if (found_match && first_match_buf == NULL)
4188 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004189 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004190 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004191 /* Never keep a dummy buffer if there is another buffer
4192 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004193 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004194 buf = NULL;
4195 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004196 else if (!cmdmod.hide
4197 || buf->b_p_bh[0] == 'u' /* "unload" */
4198 || buf->b_p_bh[0] == 'w' /* "wipe" */
4199 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004200 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004201 /* When no match was found we don't need to remember the
4202 * buffer, wipe it out. If there was a match and it
4203 * wasn't the first one or we won't jump there: only
4204 * unload the buffer.
4205 * Ignore 'hidden' here, because it may lead to having too
4206 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004207 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004208 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004209 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004210 buf = NULL;
4211 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004212 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004213 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004214 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004215 /* Keeping the buffer, remove the dummy flag. */
4216 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004217 buf = NULL;
4218 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004219 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004220
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004221 if (buf != NULL)
4222 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004223 /* Keeping the buffer, remove the dummy flag. */
4224 buf->b_flags &= ~BF_DUMMY;
4225
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004226 /* If the buffer is still loaded we need to use the
4227 * directory we jumped to below. */
4228 if (buf == first_match_buf
4229 && target_dir == NULL
4230 && STRCMP(dirname_start, dirname_now) != 0)
4231 target_dir = vim_strsave(dirname_now);
4232
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004233 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004234 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004235 * need to be done (again). But not the window-local
4236 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004237 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004238#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004239 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4240 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004241#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004242 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004243 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004244 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004245 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004246 }
4247 }
4248
4249 FreeWild(fcount, fnames);
4250
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004251 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4252 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4253 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004254
4255#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004256 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004257#endif
4258
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004259#ifdef FEAT_AUTOCMD
4260 if (au_name != NULL)
4261 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4262 curbuf->b_fname, TRUE, curbuf);
4263#endif
4264
Bram Moolenaar86b68352004-12-27 21:59:20 +00004265 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004266 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004267 {
4268 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004269 {
4270 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004271 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004272 if (buf != curbuf)
4273 /* If we jumped to another buffer redrawing will already be
4274 * taken care of. */
4275 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004276
4277 /* Jump to the directory used after loading the buffer. */
4278 if (curbuf == first_match_buf && target_dir != NULL)
4279 {
4280 exarg_T ea;
4281
4282 ea.arg = target_dir;
4283 ea.cmdidx = CMD_lcd;
4284 ex_cd(&ea);
4285 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004286 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004287 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004288 else
4289 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004290
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004291 /* If we loaded a dummy buffer into the current window, the autocommands
4292 * may have messed up things, need to redraw and recompute folds. */
4293 if (redraw_for_dummy)
4294 {
4295#ifdef FEAT_FOLDING
4296 foldUpdateAll(curwin);
4297#else
4298 redraw_later(NOT_VALID);
4299#endif
4300 }
4301
Bram Moolenaar86b68352004-12-27 21:59:20 +00004302theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004303 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004304 vim_free(dirname_now);
4305 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004306 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004307 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004308}
4309
4310/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004311 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00004312 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004313 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
4314 * If "s" is not NULL terminate the pattern with a NUL.
4315 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00004316 */
4317 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004318skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004319{
4320 int c;
4321
4322 if (vim_isIDc(*p))
4323 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004324 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004325 if (s != NULL)
4326 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004327 p = skiptowhite(p);
4328 if (s != NULL && *p != NUL)
4329 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004330 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004331 else
4332 {
4333 /* ":vimgrep /pattern/[g][j] fname" */
4334 if (s != NULL)
4335 *s = p + 1;
4336 c = *p;
4337 p = skip_regexp(p + 1, c, TRUE, NULL);
4338 if (*p != c)
4339 return NULL;
4340
4341 /* Truncate the pattern. */
4342 if (s != NULL)
4343 *p = NUL;
4344 ++p;
4345
4346 /* Find the flags */
4347 while (*p == 'g' || *p == 'j')
4348 {
4349 if (flags != NULL)
4350 {
4351 if (*p == 'g')
4352 *flags |= VGR_GLOBAL;
4353 else
4354 *flags |= VGR_NOJUMP;
4355 }
4356 ++p;
4357 }
4358 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004359 return p;
4360}
4361
4362/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004363 * Restore current working directory to "dirname_start" if they differ, taking
4364 * into account whether it is set locally or globally.
4365 */
4366 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004367restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004368{
4369 char_u *dirname_now = alloc(MAXPATHL);
4370
4371 if (NULL != dirname_now)
4372 {
4373 mch_dirname(dirname_now, MAXPATHL);
4374 if (STRCMP(dirname_start, dirname_now) != 0)
4375 {
4376 /* If the directory has changed, change it back by building up an
4377 * appropriate ex command and executing it. */
4378 exarg_T ea;
4379
4380 ea.arg = dirname_start;
4381 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4382 ex_cd(&ea);
4383 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004384 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004385 }
4386}
4387
4388/*
4389 * Load file "fname" into a dummy buffer and return the buffer pointer,
4390 * placing the directory resulting from the buffer load into the
4391 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4392 * prior to calling this function. Restores directory to "dirname_start" prior
4393 * to returning, if autocmds or the 'autochdir' option have changed it.
4394 *
4395 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4396 * or wipe_dummy_buffer() later!
4397 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004398 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004399 */
4400 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004401load_dummy_buffer(
4402 char_u *fname,
4403 char_u *dirname_start, /* in: old directory */
4404 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004405{
4406 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004407 bufref_T newbufref;
4408 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004409 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004410 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004411
4412 /* Allocate a buffer without putting it in the buffer list. */
4413 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4414 if (newbuf == NULL)
4415 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004416 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004417
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004418 /* Init the options. */
4419 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4420
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004421 /* need to open the memfile before putting the buffer in a window */
4422 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004423 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004424 /* set curwin/curbuf to buf and save a few things */
4425 aucmd_prepbuf(&aco, newbuf);
4426
4427 /* Need to set the filename for autocommands. */
4428 (void)setfname(curbuf, fname, NULL, FALSE);
4429
Bram Moolenaar81695252004-12-29 20:58:21 +00004430 /* Create swap file now to avoid the ATTENTION message. */
4431 check_need_swap(TRUE);
4432
4433 /* Remove the "dummy" flag, otherwise autocommands may not
4434 * work. */
4435 curbuf->b_flags &= ~BF_DUMMY;
4436
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004437 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004438 if (readfile(fname, NULL,
4439 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4440 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004441 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004442 && !(curbuf->b_flags & BF_NEW))
4443 {
4444 failed = FALSE;
4445 if (curbuf != newbuf)
4446 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004447 /* Bloody autocommands changed the buffer! Can happen when
4448 * using netrw and editing a remote file. Use the current
4449 * buffer instead, delete the dummy one after restoring the
4450 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004451 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004452 newbuf = curbuf;
4453 }
4454 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004455
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004456 /* restore curwin/curbuf and a few other things */
4457 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004458 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4459 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004460
4461 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4462 * skip it. */
4463 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004464 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004465
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004466 /*
4467 * When autocommands/'autochdir' option changed directory: go back.
4468 * Let the caller know what the resulting dir was first, in case it is
4469 * important.
4470 */
4471 mch_dirname(resulting_dir, MAXPATHL);
4472 restore_start_dir(dirname_start);
4473
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004474 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004475 return NULL;
4476 if (failed)
4477 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004478 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004479 return NULL;
4480 }
4481 return newbuf;
4482}
4483
4484/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004485 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4486 * directory to "dirname_start" prior to returning, if autocmds or the
4487 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004488 */
4489 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004490wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004491{
4492 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004493 {
4494#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4495 cleanup_T cs;
4496
4497 /* Reset the error/interrupt/exception state here so that aborting()
4498 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4499 * work when got_int is set. */
4500 enter_cleanup(&cs);
4501#endif
4502
Bram Moolenaar81695252004-12-29 20:58:21 +00004503 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004504
4505#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4506 /* Restore the error/interrupt/exception state if not discarded by a
4507 * new aborting error, interrupt, or uncaught exception. */
4508 leave_cleanup(&cs);
4509#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004510 /* When autocommands/'autochdir' option changed directory: go back. */
4511 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004512 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004513}
4514
4515/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004516 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4517 * directory to "dirname_start" prior to returning, if autocmds or the
4518 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004519 */
4520 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004521unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004522{
4523 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004524 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004525 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004526
4527 /* When autocommands/'autochdir' option changed directory: go back. */
4528 restore_start_dir(dirname_start);
4529 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004530}
4531
Bram Moolenaar05159a02005-02-26 23:04:13 +00004532#if defined(FEAT_EVAL) || defined(PROTO)
4533/*
4534 * Add each quickfix error to list "list" as a dictionary.
4535 */
4536 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004537get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004538{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004539 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004540 dict_T *dict;
4541 char_u buf[2];
4542 qfline_T *qfp;
4543 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004544 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004545
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004546 if (wp != NULL)
4547 {
4548 qi = GET_LOC_LIST(wp);
4549 if (qi == NULL)
4550 return FAIL;
4551 }
4552
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004553 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004554 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004555 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004556
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004557 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4558 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004559 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004560 /* Handle entries with a non-existing buffer number. */
4561 bufnum = qfp->qf_fnum;
4562 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4563 bufnum = 0;
4564
Bram Moolenaar05159a02005-02-26 23:04:13 +00004565 if ((dict = dict_alloc()) == NULL)
4566 return FAIL;
4567 if (list_append_dict(list, dict) == FAIL)
4568 return FAIL;
4569
4570 buf[0] = qfp->qf_type;
4571 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004572 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004573 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4574 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4575 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4576 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004577 || dict_add_nr_str(dict, "pattern", 0L,
4578 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4579 || dict_add_nr_str(dict, "text", 0L,
4580 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004581 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4582 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4583 return FAIL;
4584
4585 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004586 if (qfp == NULL)
4587 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004588 }
4589 return OK;
4590}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004591
4592/*
4593 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004594 * of dictionaries. "title" will be copied to w:quickfix_title.
4595 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004596 */
4597 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004598set_errorlist(
4599 win_T *wp,
4600 list_T *list,
4601 int action,
4602 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004603{
4604 listitem_T *li;
4605 dict_T *d;
4606 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004607 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004608 long lnum;
4609 int col, nr;
4610 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004611#ifdef FEAT_WINDOWS
4612 qfline_T *old_last = NULL;
4613#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004614 int valid, status;
4615 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004616 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004617 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004618
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004619 if (wp != NULL)
4620 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004621 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004622 if (qi == NULL)
4623 return FAIL;
4624 }
4625
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004626 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004627 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004628 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004629#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004630 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4631 /* Adding to existing list, use last entry. */
4632 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004633#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004634 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004635 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004636 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004637 qf_store_title(qi, title);
4638 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004639
4640 for (li = list->lv_first; li != NULL; li = li->li_next)
4641 {
4642 if (li->li_tv.v_type != VAR_DICT)
4643 continue; /* Skip non-dict items */
4644
4645 d = li->li_tv.vval.v_dict;
4646 if (d == NULL)
4647 continue;
4648
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004649 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004650 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4651 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4652 col = (int)get_dict_number(d, (char_u *)"col");
4653 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4654 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004655 type = get_dict_string(d, (char_u *)"type", TRUE);
4656 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4657 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004658 if (text == NULL)
4659 text = vim_strsave((char_u *)"");
4660
4661 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004662 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004663 valid = FALSE;
4664
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004665 /* Mark entries with non-existing buffer number as not valid. Give the
4666 * error message only once. */
4667 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4668 {
4669 if (!did_bufnr_emsg)
4670 {
4671 did_bufnr_emsg = TRUE;
4672 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4673 }
4674 valid = FALSE;
4675 bufnum = 0;
4676 }
4677
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004678 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004679 NULL, /* dir */
4680 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004681 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004682 text,
4683 lnum,
4684 col,
4685 vcol, /* vis_col */
4686 pattern, /* search pattern */
4687 nr,
4688 type == NULL ? NUL : *type,
4689 valid);
4690
4691 vim_free(filename);
4692 vim_free(pattern);
4693 vim_free(text);
4694 vim_free(type);
4695
4696 if (status == FAIL)
4697 {
4698 retval = FAIL;
4699 break;
4700 }
4701 }
4702
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004703 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004704 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004705 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4706 else
4707 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004708 if (action != 'a') {
4709 qi->qf_lists[qi->qf_curlist].qf_ptr =
4710 qi->qf_lists[qi->qf_curlist].qf_start;
4711 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4712 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4713 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004714
4715#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004716 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004717 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004718#endif
4719
4720 return retval;
4721}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004722#endif
4723
Bram Moolenaar81695252004-12-29 20:58:21 +00004724/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004725 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004726 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004727 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004728 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004729 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004730 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004731 */
4732 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004733ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004734{
4735 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004736 qf_info_T *qi = &ql_info;
4737
Bram Moolenaardb552d602006-03-23 22:59:57 +00004738 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4739 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004740 {
4741 qi = ll_get_or_alloc_list(curwin);
4742 if (qi == NULL)
4743 return;
4744 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004745
4746 if (*eap->arg == NUL)
4747 buf = curbuf;
4748 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4749 buf = buflist_findnr(atoi((char *)eap->arg));
4750 if (buf == NULL)
4751 EMSG(_(e_invarg));
4752 else if (buf->b_ml.ml_mfp == NULL)
4753 EMSG(_("E681: Buffer is not loaded"));
4754 else
4755 {
4756 if (eap->addr_count == 0)
4757 {
4758 eap->line1 = 1;
4759 eap->line2 = buf->b_ml.ml_line_count;
4760 }
4761 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4762 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4763 EMSG(_(e_invrange));
4764 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004765 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004766 char_u *qf_title = *eap->cmdlinep;
4767
4768 if (buf->b_sfname)
4769 {
4770 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4771 (char *)qf_title, (char *)buf->b_sfname);
4772 qf_title = IObuff;
4773 }
4774
Bram Moolenaardb552d602006-03-23 22:59:57 +00004775 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4776 (eap->cmdidx != CMD_caddbuffer
4777 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004778 eap->line1, eap->line2,
4779 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004780 && (eap->cmdidx == CMD_cbuffer
4781 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004782 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4783 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004784 }
4785}
4786
Bram Moolenaar1e015462005-09-25 22:16:38 +00004787#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004788/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004789 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4790 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004791 */
4792 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004793ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004794{
4795 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004796 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004797
Bram Moolenaardb552d602006-03-23 22:59:57 +00004798 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4799 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004800 {
4801 qi = ll_get_or_alloc_list(curwin);
4802 if (qi == NULL)
4803 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004804 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004805
Bram Moolenaar4770d092006-01-12 23:22:24 +00004806 /* Evaluate the expression. When the result is a string or a list we can
4807 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004808 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004809 if (tv != NULL)
4810 {
4811 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4812 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4813 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004814 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004815 (eap->cmdidx != CMD_caddexpr
4816 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004817 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004818 && (eap->cmdidx == CMD_cexpr
4819 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004820 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004821 }
4822 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004823 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004824 free_tv(tv);
4825 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004826}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004827#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004828
4829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 * ":helpgrep {pattern}"
4831 */
4832 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004833ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
4835 regmatch_T regmatch;
4836 char_u *save_cpo;
4837 char_u *p;
4838 int fcount;
4839 char_u **fnames;
4840 FILE *fd;
4841 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004843#ifdef FEAT_MULTI_LANG
4844 char_u *lang;
4845#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004846 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004847 int new_qi = FALSE;
4848 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004849#ifdef FEAT_AUTOCMD
4850 char_u *au_name = NULL;
4851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004853#ifdef FEAT_MULTI_LANG
4854 /* Check for a specified language */
4855 lang = check_help_lang(eap->arg);
4856#endif
4857
Bram Moolenaar73633f82012-01-20 13:39:07 +01004858#ifdef FEAT_AUTOCMD
4859 switch (eap->cmdidx)
4860 {
4861 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4862 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4863 default: break;
4864 }
4865 if (au_name != NULL)
4866 {
4867 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4868 curbuf->b_fname, TRUE, curbuf);
4869 if (did_throw || force_abort)
4870 return;
4871 }
4872#endif
4873
4874 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4875 save_cpo = p_cpo;
4876 p_cpo = empty_option;
4877
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004878 if (eap->cmdidx == CMD_lhelpgrep)
4879 {
4880 /* Find an existing help window */
4881 FOR_ALL_WINDOWS(wp)
4882 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4883 break;
4884
4885 if (wp == NULL) /* Help window not found */
4886 qi = NULL;
4887 else
4888 qi = wp->w_llist;
4889
4890 if (qi == NULL)
4891 {
4892 /* Allocate a new location list for help text matches */
4893 if ((qi = ll_new_list()) == NULL)
4894 return;
4895 new_qi = TRUE;
4896 }
4897 }
4898
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4900 regmatch.rm_ic = FALSE;
4901 if (regmatch.regprog != NULL)
4902 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004903#ifdef FEAT_MBYTE
4904 vimconv_T vc;
4905
4906 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4907 * differs. */
4908 vc.vc_type = CONV_NONE;
4909 if (!enc_utf8)
4910 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4911#endif
4912
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004914 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
4916 /* Go through all directories in 'runtimepath' */
4917 p = p_rtp;
4918 while (*p != NUL && !got_int)
4919 {
4920 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4921
4922 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4923 add_pathsep(NameBuff);
4924 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4925 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4926 &fnames, EW_FILE|EW_SILENT) == OK
4927 && fcount > 0)
4928 {
4929 for (fi = 0; fi < fcount && !got_int; ++fi)
4930 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004931#ifdef FEAT_MULTI_LANG
4932 /* Skip files for a different language. */
4933 if (lang != NULL
4934 && STRNICMP(lang, fnames[fi]
4935 + STRLEN(fnames[fi]) - 3, 2) != 0
4936 && !(STRNICMP(lang, "en", 2) == 0
4937 && STRNICMP("txt", fnames[fi]
4938 + STRLEN(fnames[fi]) - 3, 3) == 0))
4939 continue;
4940#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004941 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (fd != NULL)
4943 {
4944 lnum = 1;
4945 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4946 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004947 char_u *line = IObuff;
4948#ifdef FEAT_MBYTE
4949 /* Convert a line if 'encoding' is not utf-8 and
4950 * the line contains a non-ASCII character. */
4951 if (vc.vc_type != CONV_NONE
4952 && has_non_ascii(IObuff)) {
4953 line = string_convert(&vc, IObuff, NULL);
4954 if (line == NULL)
4955 line = IObuff;
4956 }
4957#endif
4958
4959 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004961 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
4963 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004964 while (l > 0 && line[l - 1] <= ' ')
4965 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004967 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 NULL, /* dir */
4969 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004970 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004971 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004973 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004974 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004975 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004976 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 0, /* nr */
4978 1, /* type */
4979 TRUE /* valid */
4980 ) == FAIL)
4981 {
4982 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004983#ifdef FEAT_MBYTE
4984 if (line != IObuff)
4985 vim_free(line);
4986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 break;
4988 }
4989 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004990#ifdef FEAT_MBYTE
4991 if (line != IObuff)
4992 vim_free(line);
4993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 ++lnum;
4995 line_breakcheck();
4996 }
4997 fclose(fd);
4998 }
4999 }
5000 FreeWild(fcount, fnames);
5001 }
5002 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005003
Bram Moolenaar473de612013-06-08 18:19:48 +02005004 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005005#ifdef FEAT_MBYTE
5006 if (vc.vc_type != CONV_NONE)
5007 convert_setup(&vc, NULL, NULL);
5008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005010 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5011 qi->qf_lists[qi->qf_curlist].qf_ptr =
5012 qi->qf_lists[qi->qf_curlist].qf_start;
5013 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005016 if (p_cpo == empty_option)
5017 p_cpo = save_cpo;
5018 else
5019 /* Darn, some plugin changed the value. */
5020 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021
5022#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005023 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024#endif
5025
Bram Moolenaar73633f82012-01-20 13:39:07 +01005026#ifdef FEAT_AUTOCMD
5027 if (au_name != NULL)
5028 {
5029 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5030 curbuf->b_fname, TRUE, curbuf);
5031 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
5032 /* autocommands made "qi" invalid */
5033 return;
5034 }
5035#endif
5036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005038 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005039 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005040 else
5041 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005042
5043 if (eap->cmdidx == CMD_lhelpgrep)
5044 {
5045 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005046 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005047 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5048 {
5049 if (new_qi)
5050 ll_free_all(&qi);
5051 }
5052 else if (curwin->w_llist == NULL)
5053 curwin->w_llist = qi;
5054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055}
5056
5057#endif /* FEAT_QUICKFIX */