blob: 92a02041740488c6cec92fc114002bddb8cc7d2c [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);
123static void qf_msg(qf_info_T *qi);
124static void qf_free(qf_info_T *qi, int idx);
125static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200126static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
127static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200129static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
131static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
134static int is_qf_win(win_T *win, qf_info_T *qi);
135static win_T *qf_find_win(qf_info_T *qi);
136static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200137static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200139static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static char_u *get_mef_name(void);
142static void restore_start_dir(char_u *dirname_start);
143static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
144static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
145static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
146static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000148/* Quickfix window check helper macro */
149#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
150/* Location list window check helper macro */
151#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
152/*
153 * Return location list for window 'wp'
154 * For location list window, return the referenced location list
155 */
156#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000159 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200160 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 * Return -1 for error, number of errors for success.
162 */
163 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100164qf_init(
165 win_T *wp,
166 char_u *efile,
167 char_u *errorformat,
168 int newlist, /* TRUE: start a new error list */
169 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000171 qf_info_T *qi = &ql_info;
172
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000173 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000174 {
175 qi = ll_get_or_alloc_list(wp);
176 if (qi == NULL)
177 return FAIL;
178 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000179
180 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200181 (linenr_T)0, (linenr_T)0,
182 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000183}
184
185/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200186 * Maximum number of bytes allowed per line while reading a errorfile.
187 */
188#define LINE_MAXLEN 4096
189
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200190 static char_u *
191qf_grow_linebuf(char_u **growbuf, int *growbufsiz, int newsz, int *allocsz)
192{
193 /*
194 * If the line exceeds LINE_MAXLEN exclude the last
195 * byte since it's not a NL character.
196 */
197 *allocsz = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
198 if (*growbuf == NULL)
199 {
200 *growbuf = alloc(*allocsz + 1);
201 if (*growbuf == NULL)
202 return NULL;
203 *growbufsiz = *allocsz;
204 }
205 else if (*allocsz > *growbufsiz)
206 {
207 *growbuf = vim_realloc(*growbuf, *allocsz + 1);
208 if (*growbuf == NULL)
209 return NULL;
210 *growbufsiz = *allocsz;
211 }
212 return *growbuf;
213}
214
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200215static struct fmtpattern
216{
217 char_u convchar;
218 char *pattern;
219} fmt_pat[FMT_PATTERNS] =
220 {
221 {'f', ".\\+"}, /* only used when at end */
222 {'n', "\\d\\+"},
223 {'l', "\\d\\+"},
224 {'c', "\\d\\+"},
225 {'t', "."},
226 {'m', ".\\+"},
227 {'r', ".*"},
228 {'p', "[- .]*"},
229 {'v', "\\d\\+"},
230 {'s', ".\\+"}
231 };
232
233/*
234 * Converts a 'errorformat' string to regular expression pattern
235 */
236 static int
237efm_to_regpat(
238 char_u *efm,
239 int len,
240 efm_T *fmt_ptr,
241 char_u *regpat,
242 char_u *errmsg)
243{
244 char_u *ptr;
245 char_u *efmp;
246 char_u *srcptr;
247 int round;
248 int idx = 0;
249
250 /*
251 * Build regexp pattern from current 'errorformat' option
252 */
253 ptr = regpat;
254 *ptr++ = '^';
255 round = 0;
256 for (efmp = efm; efmp < efm + len; ++efmp)
257 {
258 if (*efmp == '%')
259 {
260 ++efmp;
261 for (idx = 0; idx < FMT_PATTERNS; ++idx)
262 if (fmt_pat[idx].convchar == *efmp)
263 break;
264 if (idx < FMT_PATTERNS)
265 {
266 if (fmt_ptr->addr[idx])
267 {
268 sprintf((char *)errmsg,
269 _("E372: Too many %%%c in format string"), *efmp);
270 EMSG(errmsg);
271 return -1;
272 }
273 if ((idx
274 && idx < 6
275 && vim_strchr((char_u *)"DXOPQ",
276 fmt_ptr->prefix) != NULL)
277 || (idx == 6
278 && vim_strchr((char_u *)"OPQ",
279 fmt_ptr->prefix) == NULL))
280 {
281 sprintf((char *)errmsg,
282 _("E373: Unexpected %%%c in format string"), *efmp);
283 EMSG(errmsg);
284 return -1;
285 }
286 fmt_ptr->addr[idx] = (char_u)++round;
287 *ptr++ = '\\';
288 *ptr++ = '(';
289#ifdef BACKSLASH_IN_FILENAME
290 if (*efmp == 'f')
291 {
292 /* Also match "c:" in the file name, even when
293 * checking for a colon next: "%f:".
294 * "\%(\a:\)\=" */
295 STRCPY(ptr, "\\%(\\a:\\)\\=");
296 ptr += 10;
297 }
298#endif
299 if (*efmp == 'f' && efmp[1] != NUL)
300 {
301 if (efmp[1] != '\\' && efmp[1] != '%')
302 {
303 /* A file name may contain spaces, but this isn't
304 * in "\f". For "%f:%l:%m" there may be a ":" in
305 * the file name. Use ".\{-1,}x" instead (x is
306 * the next character), the requirement that :999:
307 * follows should work. */
308 STRCPY(ptr, ".\\{-1,}");
309 ptr += 7;
310 }
311 else
312 {
313 /* File name followed by '\\' or '%': include as
314 * many file name chars as possible. */
315 STRCPY(ptr, "\\f\\+");
316 ptr += 4;
317 }
318 }
319 else
320 {
321 srcptr = (char_u *)fmt_pat[idx].pattern;
322 while ((*ptr = *srcptr++) != NUL)
323 ++ptr;
324 }
325 *ptr++ = '\\';
326 *ptr++ = ')';
327 }
328 else if (*efmp == '*')
329 {
330 if (*++efmp == '[' || *efmp == '\\')
331 {
332 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
333 {
334 if (efmp[1] == '^')
335 *ptr++ = *++efmp;
336 if (efmp < efm + len)
337 {
338 *ptr++ = *++efmp; /* could be ']' */
339 while (efmp < efm + len
340 && (*ptr++ = *++efmp) != ']')
341 /* skip */;
342 if (efmp == efm + len)
343 {
344 EMSG(_("E374: Missing ] in format string"));
345 return -1;
346 }
347 }
348 }
349 else if (efmp < efm + len) /* %*\D, %*\s etc. */
350 *ptr++ = *++efmp;
351 *ptr++ = '\\';
352 *ptr++ = '+';
353 }
354 else
355 {
356 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
357 sprintf((char *)errmsg,
358 _("E375: Unsupported %%%c in format string"), *efmp);
359 EMSG(errmsg);
360 return -1;
361 }
362 }
363 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
364 *ptr++ = *efmp; /* regexp magic characters */
365 else if (*efmp == '#')
366 *ptr++ = '*';
367 else if (*efmp == '>')
368 fmt_ptr->conthere = TRUE;
369 else if (efmp == efm + 1) /* analyse prefix */
370 {
371 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
372 fmt_ptr->flags = *efmp++;
373 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
374 fmt_ptr->prefix = *efmp;
375 else
376 {
377 sprintf((char *)errmsg,
378 _("E376: Invalid %%%c in format string prefix"), *efmp);
379 EMSG(errmsg);
380 return -1;
381 }
382 }
383 else
384 {
385 sprintf((char *)errmsg,
386 _("E377: Invalid %%%c in format string"), *efmp);
387 EMSG(errmsg);
388 return -1;
389 }
390 }
391 else /* copy normal character */
392 {
393 if (*efmp == '\\' && efmp + 1 < efm + len)
394 ++efmp;
395 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
396 *ptr++ = '\\'; /* escape regexp atoms */
397 if (*efmp)
398 *ptr++ = *efmp;
399 }
400 }
401 *ptr++ = '$';
402 *ptr = NUL;
403
404 return 0;
405}
406
407 static void
408free_efm_list(efm_T **efm_first)
409{
410 efm_T *efm_ptr;
411
412 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
413 {
414 *efm_first = efm_ptr->next;
415 vim_regfree(efm_ptr->prog);
416 vim_free(efm_ptr);
417 }
418}
419
420/* Parse 'errorformat' option */
421 static efm_T *
422parse_efm_option(char_u *efm)
423{
424 char_u *errmsg = NULL;
425 int errmsglen;
426 efm_T *fmt_ptr = NULL;
427 efm_T *fmt_first = NULL;
428 efm_T *fmt_last = NULL;
429 char_u *fmtstr = NULL;
430 int len;
431 int i;
432 int round;
433
434 errmsglen = CMDBUFFSIZE + 1;
435 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
436 if (errmsg == NULL)
437 goto parse_efm_end;
438
439 /*
440 * Get some space to modify the format string into.
441 */
442 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
443 for (round = FMT_PATTERNS; round > 0; )
444 i += (int)STRLEN(fmt_pat[--round].pattern);
445#ifdef COLON_IN_FILENAME
446 i += 12; /* "%f" can become twelve chars longer */
447#else
448 i += 2; /* "%f" can become two chars longer */
449#endif
450 if ((fmtstr = alloc(i)) == NULL)
451 goto parse_efm_error;
452
453 while (efm[0] != NUL)
454 {
455 /*
456 * Allocate a new eformat structure and put it at the end of the list
457 */
458 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
459 if (fmt_ptr == NULL)
460 goto parse_efm_error;
461 if (fmt_first == NULL) /* first one */
462 fmt_first = fmt_ptr;
463 else
464 fmt_last->next = fmt_ptr;
465 fmt_last = fmt_ptr;
466
467 /*
468 * Isolate one part in the 'errorformat' option
469 */
470 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
471 if (efm[len] == '\\' && efm[len + 1] != NUL)
472 ++len;
473
474 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
475 goto parse_efm_error;
476 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
477 goto parse_efm_error;
478 /*
479 * Advance to next part
480 */
481 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
482 }
483
484 if (fmt_first == NULL) /* nothing found */
485 EMSG(_("E378: 'errorformat' contains no pattern"));
486
487 goto parse_efm_end;
488
489parse_efm_error:
490 free_efm_list(&fmt_first);
491
492parse_efm_end:
493 vim_free(fmtstr);
494 vim_free(errmsg);
495
496 return fmt_first;
497}
498
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200499/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000500 * Read the errorfile "efile" into memory, line by line, building the error
501 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +0200502 * Alternative: when "efile" is NULL read errors from buffer "buf".
503 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000504 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200505 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
506 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000507 * Return -1 for error, number of errors for success.
508 */
509 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100510qf_init_ext(
511 qf_info_T *qi,
512 char_u *efile,
513 buf_T *buf,
514 typval_T *tv,
515 char_u *errorformat,
516 int newlist, /* TRUE: start a new error list */
517 linenr_T lnumfirst, /* first line number to use */
518 linenr_T lnumlast, /* last line number to use */
519 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000520{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 char_u *namebuf;
522 char_u *errmsg;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200523 int errmsglen;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000524 char_u *pattern;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200525 char_u *growbuf = NULL;
526 int growbuflen;
Bram Moolenaar9a3b3312016-05-01 20:20:49 +0200527 int growbufsiz = 0;
528 char_u *linebuf = NULL;
529 int linelen = 0;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200530 int discard;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000532 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 int type = 0;
534 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000535 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 long lnum = 0L;
537 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000538 FILE *fd = NULL;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200539#ifdef FEAT_WINDOWS
540 qfline_T *old_last = NULL;
541#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200542 static efm_T *fmt_first = NULL;
Bram Moolenaar01265852006-03-20 21:50:15 +0000543 efm_T *fmt_ptr;
544 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200546 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 int len;
549 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 int retval = -1; /* default: return error flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 char_u *tail = NULL;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200553 char_u *p_buf = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000554 char_u *p_str = NULL;
555 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100558 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200559 errmsglen = CMDBUFFSIZE + 1;
560 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100561 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000562 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 goto qf_init_end;
564
Bram Moolenaar86b68352004-12-27 21:59:20 +0000565 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
567 EMSG2(_(e_openerrf), efile);
568 goto qf_init_end;
569 }
570
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000571 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100573 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200574#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000575 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +0200576 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200577 /* Adding to existing list, use last entry. */
578 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200579 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
582/*
583 * Each part of the format string is copied and modified from errorformat to
584 * regex prog. Only a few % characters are allowed.
585 */
586 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000587 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000588 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 else
590 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200592 /*
593 * If we are not adding or adding to another list: clear the state.
594 */
595 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
596 {
597 qi->qf_dir_curlist = qi->qf_curlist;
598 qf_clean_dir_stack(&qi->qf_dir_stack);
599 qi->qf_directory = NULL;
600 qf_clean_dir_stack(&qi->qf_file_stack);
601 qi->qf_currfile = NULL;
602 qi->qf_multiline = FALSE;
603 qi->qf_multiignore = FALSE;
604 qi->qf_multiscan = FALSE;
605 }
606
607 /*
608 * If the errorformat didn't change between calls, then reuse the
609 * previously parsed values.
610 */
611 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
612 {
613 /* free the previously parsed data */
614 vim_free(last_efm);
615 last_efm = NULL;
616 free_efm_list(&fmt_first);
617
618 /* parse the current 'efm' */
619 fmt_first = parse_efm_option(efm);
620 if (fmt_first != NULL)
621 last_efm = vim_strsave(efm);
622 }
623
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627 /*
628 * got_int is reset here, because it was probably set when killing the
629 * ":make" command, but we still want to read the errorfile then.
630 */
631 got_int = FALSE;
632
633 /* Always ignore case when looking for a matching error. */
634 regmatch.rm_ic = TRUE;
635
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000636 if (tv != NULL)
637 {
638 if (tv->v_type == VAR_STRING)
639 p_str = tv->vval.v_string;
640 else if (tv->v_type == VAR_LIST)
641 p_li = tv->vval.v_list->lv_first;
642 }
643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 /*
645 * Read the lines in the error file one by one.
646 * Try to recognize one of the error formats in each line.
647 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000648 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000650 /* Get the next line. */
651 if (fd == NULL)
652 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000653 if (tv != NULL)
654 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000655 if (tv->v_type == VAR_STRING)
656 {
657 /* Get the next line from the supplied string */
658 char_u *p;
659
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200660 if (*p_str == NUL) /* Reached the end of the string */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000661 break;
662
663 p = vim_strchr(p_str, '\n');
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200664 if (p != NULL)
665 len = (int)(p - p_str) + 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000666 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000667 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000668
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200669 if (len > IOSIZE - 2)
670 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200671 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
672 &linelen);
673 if (linebuf == NULL)
674 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200675 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000676 else
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200677 {
678 linebuf = IObuff;
679 linelen = len;
680 }
681 vim_strncpy(linebuf, p_str, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000682
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200683 /*
684 * Increment using len in order to discard the rest of the
685 * line if it exceeds LINE_MAXLEN.
686 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000687 p_str += len;
688 }
689 else if (tv->v_type == VAR_LIST)
690 {
691 /* Get the next line from the supplied list */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200692 while (p_li != NULL
693 && (p_li->li_tv.v_type != VAR_STRING
694 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000695 p_li = p_li->li_next; /* Skip non-string items */
696
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200697 if (p_li == NULL) /* End of the list */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000698 break;
699
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000700 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200701 if (len > IOSIZE - 2)
702 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200703 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
704 &linelen);
705 if (linebuf == NULL)
706 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200707 }
708 else
709 {
710 linebuf = IObuff;
711 linelen = len;
712 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000713
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200714 vim_strncpy(linebuf, p_li->li_tv.vval.v_string, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000715
716 p_li = p_li->li_next; /* next item */
717 }
718 }
719 else
720 {
721 /* Get the next line from the supplied buffer */
722 if (buflnum > lnumlast)
723 break;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200724 p_buf = ml_get_buf(buf, buflnum++, FALSE);
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200725 len = (int)STRLEN(p_buf);
726 if (len > IOSIZE - 2)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200727 {
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200728 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
729 &linelen);
730 if (linebuf == NULL)
731 goto qf_init_end;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200732 }
733 else
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200734 {
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200735 linebuf = IObuff;
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200736 linelen = len;
737 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200738 vim_strncpy(linebuf, p_buf, linelen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000739 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000740 }
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200741 else
742 {
743 if (fgets((char *)IObuff, IOSIZE, fd) == NULL)
744 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000745
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200746 discard = FALSE;
747 linelen = (int)STRLEN(IObuff);
Bram Moolenaarb37662a2016-06-02 22:18:47 +0200748 if (linelen == IOSIZE - 1 && !(IObuff[linelen - 1] == '\n'
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200749#ifdef USE_CRNL
Bram Moolenaarb37662a2016-06-02 22:18:47 +0200750 || IObuff[linelen - 1] == '\r'
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200751#endif
752 ))
753 {
754 /*
755 * The current line exceeds IObuff, continue reading using
756 * growbuf until EOL or LINE_MAXLEN bytes is read.
757 */
758 if (growbuf == NULL)
759 {
760 growbufsiz = 2 * (IOSIZE - 1);
761 growbuf = alloc(growbufsiz);
762 if (growbuf == NULL)
763 goto qf_init_end;
764 }
765
766 /* Copy the read part of the line, excluding null-terminator */
767 memcpy(growbuf, IObuff, IOSIZE - 1);
768 growbuflen = linelen;
769
770 for (;;)
771 {
772 if (fgets((char *)growbuf + growbuflen,
773 growbufsiz - growbuflen, fd) == NULL)
774 break;
Bram Moolenaard9db8b42016-05-08 12:52:05 +0200775 linelen = (int)STRLEN(growbuf + growbuflen);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200776 growbuflen += linelen;
777 if (growbuf[growbuflen - 1] == '\n'
778#ifdef USE_CRNL
779 || growbuf[growbuflen - 1] == '\r'
780#endif
781 )
782 break;
783 if (growbufsiz == LINE_MAXLEN)
784 {
785 discard = TRUE;
786 break;
787 }
788
789 growbufsiz = 2 * growbufsiz < LINE_MAXLEN
790 ? 2 * growbufsiz : LINE_MAXLEN;
791 growbuf = vim_realloc(growbuf, 2 * growbufsiz);
792 if (growbuf == NULL)
793 goto qf_init_end;
794 }
795
796 while (discard)
797 {
798 /*
799 * The current line is longer than LINE_MAXLEN, continue
800 * reading but discard everything until EOL or EOF is
801 * reached.
802 */
803 if (fgets((char *)IObuff, IOSIZE, fd) == NULL
804 || (int)STRLEN(IObuff) < IOSIZE - 1
805 || IObuff[IOSIZE - 1] == '\n'
806#ifdef USE_CRNL
807 || IObuff[IOSIZE - 1] == '\r'
808#endif
809 )
810 break;
811 }
812
813 linebuf = growbuf;
814 linelen = growbuflen;
815 }
816 else
817 linebuf = IObuff;
818 }
819
820 if (linelen > 0 && linebuf[linelen - 1] == '\n')
821 linebuf[linelen - 1] = NUL;
822#ifdef USE_CRNL
823 if (linelen > 0 && linebuf[linelen - 1] == '\r')
824 linebuf[linelen - 1] = NUL;
Bram Moolenaar836082d2011-08-10 13:21:46 +0200825#endif
826
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200827#ifdef FEAT_MBYTE
828 remove_bom(linebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829#endif
830
Bram Moolenaar01265852006-03-20 21:50:15 +0000831 /* If there was no %> item start at the first pattern */
832 if (fmt_start == NULL)
833 fmt_ptr = fmt_first;
834 else
835 {
836 fmt_ptr = fmt_start;
837 fmt_start = NULL;
838 }
839
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 /*
841 * Try to match each part of 'errorformat' until we find a complete
842 * match or no match.
843 */
844 valid = TRUE;
845restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000846 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100848 int r;
849
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 idx = fmt_ptr->prefix;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200851 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 continue;
853 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000854 pattern[0] = NUL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200855 if (!qi->qf_multiscan)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 errmsg[0] = NUL;
857 lnum = 0;
858 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000859 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 enr = -1;
861 type = 0;
862 tail = NULL;
863
864 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200865 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100866 fmt_ptr->prog = regmatch.regprog;
867 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200869 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 continue;
871 if (vim_strchr((char_u *)"EWI", idx) != NULL)
872 type = idx;
873 else
874 type = 0;
875 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000876 * Extract error message data from matched line.
877 * We check for an actual submatch, because "\[" and "\]" in
878 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 */
880 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
881 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000882 int c;
883
884 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
885 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000886
887 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000888 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000889 *regmatch.endp[i] = NUL;
890 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
891 *regmatch.endp[i] = c;
892
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000894 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 continue;
896 }
897 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000898 {
899 if (regmatch.startp[i] == NULL)
900 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000904 {
905 if (regmatch.startp[i] == NULL)
906 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000910 {
911 if (regmatch.startp[i] == NULL)
912 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000916 {
917 if (regmatch.startp[i] == NULL)
918 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000920 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200921 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200922 {
923 if (linelen > errmsglen) {
924 /* linelen + null terminator */
925 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
926 goto qf_init_end;
927 errmsglen = linelen + 1;
928 }
929 vim_strncpy(errmsg, linebuf, linelen);
930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
932 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000933 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
934 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200936 if (len > errmsglen) {
937 /* len + null terminator */
938 if ((errmsg = vim_realloc(errmsg, len + 1))
939 == NULL)
940 goto qf_init_end;
941 errmsglen = len + 1;
942 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000943 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
945 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000946 {
947 if (regmatch.startp[i] == NULL)
948 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
952 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200953 char_u *match_ptr;
954
Bram Moolenaar4169da72006-06-20 18:49:32 +0000955 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
956 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200957 col = 0;
958 for (match_ptr = regmatch.startp[i];
959 match_ptr != regmatch.endp[i]; ++match_ptr)
960 {
961 ++col;
962 if (*match_ptr == TAB)
963 {
964 col += 7;
965 col -= col % 8;
966 }
967 }
968 ++col;
969 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
971 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
972 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000973 if (regmatch.startp[i] == NULL)
974 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000978 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
979 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000980 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
981 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000982 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
983 if (len > CMDBUFFSIZE - 5)
984 len = CMDBUFFSIZE - 5;
985 STRCPY(pattern, "^\\V");
986 STRNCAT(pattern, regmatch.startp[i], len);
987 pattern[len + 3] = '\\';
988 pattern[len + 4] = '$';
989 pattern[len + 5] = NUL;
990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 break;
992 }
993 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200994 qi->qf_multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000995
Bram Moolenaar4770d092006-01-12 23:22:24 +0000996 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000998 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 {
1000 if (idx == 'D') /* enter directory */
1001 {
1002 if (*namebuf == NUL)
1003 {
1004 EMSG(_("E379: Missing or empty directory name"));
1005 goto error2;
1006 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001007 qi->qf_directory =
1008 qf_push_dir(namebuf, &qi->qf_dir_stack, FALSE);
1009 if (qi->qf_directory == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 goto error2;
1011 }
1012 else if (idx == 'X') /* leave directory */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001013 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015 namebuf[0] = NUL; /* no match found, remove file name */
1016 lnum = 0; /* don't jump to this line */
1017 valid = FALSE;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001018 if (linelen > errmsglen) {
1019 /* linelen + null terminator */
1020 if ((errmsg = vim_realloc(errmsg, linelen + 1)) == NULL)
1021 goto qf_init_end;
1022 errmsglen = linelen + 1;
1023 }
1024 /* copy whole line to error message */
1025 vim_strncpy(errmsg, linebuf, linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001026 if (fmt_ptr == NULL)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001027 qi->qf_multiline = qi->qf_multiignore = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001029 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 {
Bram Moolenaar01265852006-03-20 21:50:15 +00001031 /* honor %> item */
1032 if (fmt_ptr->conthere)
1033 fmt_start = fmt_ptr;
1034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +01001036 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001037 qi->qf_multiline = TRUE; /* start of a multi-line message */
1038 qi->qf_multiignore = FALSE; /* reset continuation */
Bram Moolenaar8eded092014-03-12 19:41:55 +01001039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1041 { /* continuation of multi-line msg */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001042 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
1043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (qfprev == NULL)
1045 goto error2;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001046 if (*errmsg && !qi->qf_multiignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 {
1048 len = (int)STRLEN(qfprev->qf_text);
1049 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
1050 == NULL)
1051 goto error2;
1052 STRCPY(ptr, qfprev->qf_text);
1053 vim_free(qfprev->qf_text);
1054 qfprev->qf_text = ptr;
1055 *(ptr += len) = '\n';
1056 STRCPY(++ptr, errmsg);
1057 }
1058 if (qfprev->qf_nr == -1)
1059 qfprev->qf_nr = enr;
1060 if (vim_isprintc(type) && !qfprev->qf_type)
1061 qfprev->qf_type = type; /* only printable chars allowed */
1062 if (!qfprev->qf_lnum)
1063 qfprev->qf_lnum = lnum;
1064 if (!qfprev->qf_col)
1065 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001066 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (!qfprev->qf_fnum)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001068 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1069 *namebuf || qi->qf_directory != NULL
1070 ? namebuf
1071 : qi->qf_currfile != NULL && valid
1072 ? qi->qf_currfile : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (idx == 'Z')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001074 qi->qf_multiline = qi->qf_multiignore = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 line_breakcheck();
1076 continue;
1077 }
1078 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1079 {
1080 /* global file names */
1081 valid = FALSE;
1082 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
1083 {
1084 if (*namebuf && idx == 'P')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001085 qi->qf_currfile =
1086 qf_push_dir(namebuf, &qi->qf_file_stack, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 else if (idx == 'Q')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001088 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 *namebuf = NUL;
1090 if (tail && *tail)
1091 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00001092 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001093 qi->qf_multiscan = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 goto restofline;
1095 }
1096 }
1097 }
1098 if (fmt_ptr->flags == '-') /* generally exclude this line */
1099 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001100 if (qi->qf_multiline)
1101 /* also exclude continuation lines */
1102 qi->qf_multiignore = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 continue;
1104 }
1105 }
1106
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001107 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001108 qi->qf_directory,
1109 (*namebuf || qi->qf_directory != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 ? namebuf
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001111 : ((qi->qf_currfile != NULL && valid)
1112 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001113 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 errmsg,
1115 lnum,
1116 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001117 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001118 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 enr,
1120 type,
1121 valid) == FAIL)
1122 goto error2;
1123 line_breakcheck();
1124 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00001125 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001127 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001129 /* no valid entry found */
1130 qi->qf_lists[qi->qf_curlist].qf_ptr =
1131 qi->qf_lists[qi->qf_curlist].qf_start;
1132 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1133 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 else
1136 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001137 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1138 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1139 qi->qf_lists[qi->qf_curlist].qf_ptr =
1140 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001142 /* return number of matches */
1143 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001144 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
1146 EMSG(_(e_readerrf));
1147error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001148 qf_free(qi, qi->qf_curlist);
1149 qi->qf_listcount--;
1150 if (qi->qf_curlist > 0)
1151 --qi->qf_curlist;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001152qf_init_end:
Bram Moolenaar86b68352004-12-27 21:59:20 +00001153 if (fd != NULL)
1154 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 vim_free(namebuf);
1156 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001157 vim_free(pattern);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001158 vim_free(growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
1160#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001161 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162#endif
1163
1164 return retval;
1165}
1166
Bram Moolenaarfb604092014-07-23 15:55:00 +02001167 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001168qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001169{
1170 if (title != NULL)
1171 {
1172 char_u *p = alloc((int)STRLEN(title) + 2);
1173
1174 qi->qf_lists[qi->qf_curlist].qf_title = p;
1175 if (p != NULL)
1176 sprintf((char *)p, ":%s", (char *)title);
1177 }
1178}
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180/*
1181 * Prepare for adding a new quickfix list.
1182 */
1183 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001184qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
1186 int i;
1187
1188 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001189 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 * the current entry. This makes it possible to browse in a tree-like
1191 * way with ":grep'.
1192 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001193 while (qi->qf_listcount > qi->qf_curlist + 1)
1194 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
1196 /*
1197 * When the stack is full, remove to oldest entry
1198 * Otherwise, add a new entry.
1199 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001200 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001202 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001204 qi->qf_lists[i - 1] = qi->qf_lists[i];
1205 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001208 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001209 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001210 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211}
1212
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001213/*
1214 * Free a location list
1215 */
1216 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001217ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001218{
1219 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001220 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001221
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001222 qi = *pqi;
1223 if (qi == NULL)
1224 return;
1225 *pqi = NULL; /* Remove reference to this list */
1226
1227 qi->qf_refcount--;
1228 if (qi->qf_refcount < 1)
1229 {
1230 /* No references to this location list */
1231 for (i = 0; i < qi->qf_listcount; ++i)
1232 qf_free(qi, i);
1233 vim_free(qi);
1234 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001235}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001236
1237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001238qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001239{
1240 int i;
1241 qf_info_T *qi = &ql_info;
1242
1243 if (wp != NULL)
1244 {
1245 /* location list */
1246 ll_free_all(&wp->w_llist);
1247 ll_free_all(&wp->w_llist_ref);
1248 }
1249 else
1250 /* quickfix list */
1251 for (i = 0; i < qi->qf_listcount; ++i)
1252 qf_free(qi, i);
1253}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255/*
1256 * Add an entry to the end of the list of errors.
1257 * Returns OK or FAIL.
1258 */
1259 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001260qf_add_entry(
1261 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001262 char_u *dir, /* optional directory name */
1263 char_u *fname, /* file name or NULL */
1264 int bufnum, /* buffer number or zero */
1265 char_u *mesg, /* message */
1266 long lnum, /* line number */
1267 int col, /* column */
1268 int vis_col, /* using visual column */
1269 char_u *pattern, /* search pattern */
1270 int nr, /* error number */
1271 int type, /* type character */
1272 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001274 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001275 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001277 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001279 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001280 {
1281 buf_T *buf = buflist_findnr(bufnum);
1282
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001283 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001284 if (buf != NULL)
1285 buf->b_has_qf_entry = TRUE;
1286 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001287 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001288 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1290 {
1291 vim_free(qfp);
1292 return FAIL;
1293 }
1294 qfp->qf_lnum = lnum;
1295 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001296 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001297 if (pattern == NULL || *pattern == NUL)
1298 qfp->qf_pattern = NULL;
1299 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1300 {
1301 vim_free(qfp->qf_text);
1302 vim_free(qfp);
1303 return FAIL;
1304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 qfp->qf_nr = nr;
1306 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1307 type = 0;
1308 qfp->qf_type = type;
1309 qfp->qf_valid = valid;
1310
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001311 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001312 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1313 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001315 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001316 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1317 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001318 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 }
1320 else
1321 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001322 qfp->qf_prev = *lastp;
1323 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001325 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001327 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001328 ++qi->qf_lists[qi->qf_curlist].qf_count;
1329 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1330 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001332 qi->qf_lists[qi->qf_curlist].qf_index =
1333 qi->qf_lists[qi->qf_curlist].qf_count;
1334 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336
1337 return OK;
1338}
1339
1340/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001341 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001342 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001343 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001344ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001345{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001346 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001347
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001348 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1349 if (qi != NULL)
1350 {
1351 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1352 qi->qf_refcount++;
1353 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001354
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001355 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001356}
1357
1358/*
1359 * Return the location list for window 'wp'.
1360 * If not present, allocate a location list
1361 */
1362 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001363ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001364{
1365 if (IS_LL_WINDOW(wp))
1366 /* For a location list window, use the referenced location list */
1367 return wp->w_llist_ref;
1368
1369 /*
1370 * For a non-location list window, w_llist_ref should not point to a
1371 * location list.
1372 */
1373 ll_free_all(&wp->w_llist_ref);
1374
1375 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001376 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001377 return wp->w_llist;
1378}
1379
1380/*
1381 * Copy the location list from window "from" to window "to".
1382 */
1383 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001384copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001385{
1386 qf_info_T *qi;
1387 int idx;
1388 int i;
1389
1390 /*
1391 * When copying from a location list window, copy the referenced
1392 * location list. For other windows, copy the location list for
1393 * that window.
1394 */
1395 if (IS_LL_WINDOW(from))
1396 qi = from->w_llist_ref;
1397 else
1398 qi = from->w_llist;
1399
1400 if (qi == NULL) /* no location list to copy */
1401 return;
1402
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001403 /* allocate a new location list */
1404 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001405 return;
1406
1407 to->w_llist->qf_listcount = qi->qf_listcount;
1408
1409 /* Copy the location lists one at a time */
1410 for (idx = 0; idx < qi->qf_listcount; idx++)
1411 {
1412 qf_list_T *from_qfl;
1413 qf_list_T *to_qfl;
1414
1415 to->w_llist->qf_curlist = idx;
1416
1417 from_qfl = &qi->qf_lists[idx];
1418 to_qfl = &to->w_llist->qf_lists[idx];
1419
1420 /* Some of the fields are populated by qf_add_entry() */
1421 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1422 to_qfl->qf_count = 0;
1423 to_qfl->qf_index = 0;
1424 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001425 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001426 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001427 if (from_qfl->qf_title != NULL)
1428 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1429 else
1430 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001431
1432 if (from_qfl->qf_count)
1433 {
1434 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001435 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001436
1437 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001438 for (i = 0, from_qfp = from_qfl->qf_start;
1439 i < from_qfl->qf_count && from_qfp != NULL;
1440 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001441 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001442 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001443 NULL,
1444 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001445 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001446 from_qfp->qf_text,
1447 from_qfp->qf_lnum,
1448 from_qfp->qf_col,
1449 from_qfp->qf_viscol,
1450 from_qfp->qf_pattern,
1451 from_qfp->qf_nr,
1452 0,
1453 from_qfp->qf_valid) == FAIL)
1454 {
1455 qf_free_all(to);
1456 return;
1457 }
1458 /*
1459 * qf_add_entry() will not set the qf_num field, as the
1460 * directory and file names are not supplied. So the qf_fnum
1461 * field is copied here.
1462 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001463 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001464 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1465 prevp->qf_type = from_qfp->qf_type; /* error type */
1466 if (from_qfl->qf_ptr == from_qfp)
1467 to_qfl->qf_ptr = prevp; /* current location */
1468 }
1469 }
1470
1471 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1472
1473 /* When no valid entries are present in the list, qf_ptr points to
1474 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001475 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001476 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001477 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001478 to_qfl->qf_index = 1;
1479 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001480 }
1481
1482 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1483}
1484
1485/*
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001486 * Get buffer number for file "dir.name".
1487 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 */
1489 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001490qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491{
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001492 char_u *ptr;
1493 buf_T *buf;
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 if (fname == NULL || *fname == NUL) /* no file name */
1496 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
Bram Moolenaare60acc12011-05-10 16:41:25 +02001498#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001499 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001500#endif
1501#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001502 if (directory != NULL)
1503 slash_adjust(directory);
1504 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001505#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001506 if (directory != NULL && !vim_isAbsName(fname)
1507 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1508 {
1509 /*
1510 * Here we check if the file really exists.
1511 * This should normally be true, but if make works without
1512 * "leaving directory"-messages we might have missed a
1513 * directory change.
1514 */
1515 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001518 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001519 if (directory)
1520 ptr = concat_fnames(directory, fname, TRUE);
1521 else
1522 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001524 /* Use concatenated directory name and file name */
1525 buf = buflist_new(ptr, NULL, (linenr_T)0, 0);
1526 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001528 else
1529 buf = buflist_new(fname, NULL, (linenr_T)0, 0);
1530 if (buf == NULL)
1531 return 0;
1532 buf->b_has_qf_entry = TRUE;
1533 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534}
1535
1536/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001537 * Push dirbuf onto the directory stack and return pointer to actual dir or
1538 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
1540 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001541qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
1543 struct dir_stack_T *ds_new;
1544 struct dir_stack_T *ds_ptr;
1545
1546 /* allocate new stack element and hook it in */
1547 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1548 if (ds_new == NULL)
1549 return NULL;
1550
1551 ds_new->next = *stackptr;
1552 *stackptr = ds_new;
1553
1554 /* store directory on the stack */
1555 if (vim_isAbsName(dirbuf)
1556 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001557 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 (*stackptr)->dirname = vim_strsave(dirbuf);
1559 else
1560 {
1561 /* Okay we don't have an absolute path.
1562 * dirbuf must be a subdir of one of the directories on the stack.
1563 * Let's search...
1564 */
1565 ds_new = (*stackptr)->next;
1566 (*stackptr)->dirname = NULL;
1567 while (ds_new)
1568 {
1569 vim_free((*stackptr)->dirname);
1570 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1571 TRUE);
1572 if (mch_isdir((*stackptr)->dirname) == TRUE)
1573 break;
1574
1575 ds_new = ds_new->next;
1576 }
1577
1578 /* clean up all dirs we already left */
1579 while ((*stackptr)->next != ds_new)
1580 {
1581 ds_ptr = (*stackptr)->next;
1582 (*stackptr)->next = (*stackptr)->next->next;
1583 vim_free(ds_ptr->dirname);
1584 vim_free(ds_ptr);
1585 }
1586
1587 /* Nothing found -> it must be on top level */
1588 if (ds_new == NULL)
1589 {
1590 vim_free((*stackptr)->dirname);
1591 (*stackptr)->dirname = vim_strsave(dirbuf);
1592 }
1593 }
1594
1595 if ((*stackptr)->dirname != NULL)
1596 return (*stackptr)->dirname;
1597 else
1598 {
1599 ds_ptr = *stackptr;
1600 *stackptr = (*stackptr)->next;
1601 vim_free(ds_ptr);
1602 return NULL;
1603 }
1604}
1605
1606
1607/*
1608 * pop dirbuf from the directory stack and return previous directory or NULL if
1609 * stack is empty
1610 */
1611 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001612qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 struct dir_stack_T *ds_ptr;
1615
1616 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1617 * What to do if it isn't? */
1618
1619 /* pop top element and free it */
1620 if (*stackptr != NULL)
1621 {
1622 ds_ptr = *stackptr;
1623 *stackptr = (*stackptr)->next;
1624 vim_free(ds_ptr->dirname);
1625 vim_free(ds_ptr);
1626 }
1627
1628 /* return NEW top element as current dir or NULL if stack is empty*/
1629 return *stackptr ? (*stackptr)->dirname : NULL;
1630}
1631
1632/*
1633 * clean up directory stack
1634 */
1635 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001636qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 struct dir_stack_T *ds_ptr;
1639
1640 while ((ds_ptr = *stackptr) != NULL)
1641 {
1642 *stackptr = (*stackptr)->next;
1643 vim_free(ds_ptr->dirname);
1644 vim_free(ds_ptr);
1645 }
1646}
1647
1648/*
1649 * Check in which directory of the directory stack the given file can be
1650 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001651 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 * Cleans up intermediate directory entries.
1653 *
1654 * TODO: How to solve the following problem?
1655 * If we have the this directory tree:
1656 * ./
1657 * ./aa
1658 * ./aa/bb
1659 * ./bb
1660 * ./bb/x.c
1661 * and make says:
1662 * making all in aa
1663 * making all in bb
1664 * x.c:9: Error
1665 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1666 * qf_guess_filepath will return NULL.
1667 */
1668 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001669qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
1671 struct dir_stack_T *ds_ptr;
1672 struct dir_stack_T *ds_tmp;
1673 char_u *fullname;
1674
1675 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001676 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 return NULL;
1678
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001679 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 fullname = NULL;
1681 while (ds_ptr)
1682 {
1683 vim_free(fullname);
1684 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1685
1686 /* If concat_fnames failed, just go on. The worst thing that can happen
1687 * is that we delete the entire stack.
1688 */
1689 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1690 break;
1691
1692 ds_ptr = ds_ptr->next;
1693 }
1694
1695 vim_free(fullname);
1696
1697 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001698 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001700 ds_tmp = qi->qf_dir_stack->next;
1701 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 vim_free(ds_tmp->dirname);
1703 vim_free(ds_tmp);
1704 }
1705
1706 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707}
1708
1709/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001710 * When loading a file from the quickfix, the auto commands may modify it.
1711 * This may invalidate the current quickfix entry. This function checks
1712 * whether a entry is still present in the quickfix.
1713 * Similar to location list.
1714 */
1715 static int
1716is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1717{
1718 qf_list_T *qfl;
1719 qfline_T *qfp;
1720 int i;
1721
1722 qfl = &qi->qf_lists[qi->qf_curlist];
1723
1724 /* Search for the entry in the current list */
1725 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1726 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001727 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001728 break;
1729
1730 if (i == qfl->qf_count) /* Entry is not found */
1731 return FALSE;
1732
1733 return TRUE;
1734}
1735
1736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 * jump to a quickfix line
1738 * if dir == FORWARD go "errornr" valid entries forward
1739 * if dir == BACKWARD go "errornr" valid entries backward
1740 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1741 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1742 * else if "errornr" is zero, redisplay the same line
1743 * else go to entry "errornr"
1744 */
1745 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001746qf_jump(
1747 qf_info_T *qi,
1748 int dir,
1749 int errornr,
1750 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001752 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001753 qfline_T *qf_ptr;
1754 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 int qf_index;
1756 int old_qf_fnum;
1757 int old_qf_index;
1758 int prev_index;
1759 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1760 char_u *err = e_no_more_items;
1761 linenr_T i;
1762 buf_T *old_curbuf;
1763 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 colnr_T screen_col;
1765 colnr_T char_col;
1766 char_u *line;
1767#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001768 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001769 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 int opened_window = FALSE;
1771 win_T *win;
1772 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001773 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001775 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 int print_message = TRUE;
1777 int len;
1778#ifdef FEAT_FOLDING
1779 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1780#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001781 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001782 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001784 if (qi == NULL)
1785 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001786
1787 if (qi->qf_curlist >= qi->qf_listcount
1788 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
1790 EMSG(_(e_quickfix));
1791 return;
1792 }
1793
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001794 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001796 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 old_qf_index = qf_index;
1798 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1799 {
1800 while (errornr--)
1801 {
1802 old_qf_ptr = qf_ptr;
1803 prev_index = qf_index;
1804 old_qf_fnum = qf_ptr->qf_fnum;
1805 do
1806 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001807 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 || qf_ptr->qf_next == NULL)
1809 {
1810 qf_ptr = old_qf_ptr;
1811 qf_index = prev_index;
1812 if (err != NULL)
1813 {
1814 EMSG(_(err));
1815 goto theend;
1816 }
1817 errornr = 0;
1818 break;
1819 }
1820 ++qf_index;
1821 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001822 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1823 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1825 err = NULL;
1826 }
1827 }
1828 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1829 {
1830 while (errornr--)
1831 {
1832 old_qf_ptr = qf_ptr;
1833 prev_index = qf_index;
1834 old_qf_fnum = qf_ptr->qf_fnum;
1835 do
1836 {
1837 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1838 {
1839 qf_ptr = old_qf_ptr;
1840 qf_index = prev_index;
1841 if (err != NULL)
1842 {
1843 EMSG(_(err));
1844 goto theend;
1845 }
1846 errornr = 0;
1847 break;
1848 }
1849 --qf_index;
1850 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001851 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1852 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1854 err = NULL;
1855 }
1856 }
1857 else if (errornr != 0) /* go to specified number */
1858 {
1859 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1860 {
1861 --qf_index;
1862 qf_ptr = qf_ptr->qf_prev;
1863 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001864 while (errornr > qf_index && qf_index <
1865 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 && qf_ptr->qf_next != NULL)
1867 {
1868 ++qf_index;
1869 qf_ptr = qf_ptr->qf_next;
1870 }
1871 }
1872
1873#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001874 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1875 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 /* No need to print the error message if it's visible in the error
1877 * window */
1878 print_message = FALSE;
1879
1880 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001881 * For ":helpgrep" find a help window or open one.
1882 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001883 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001884 {
1885 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001886
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001887 if (cmdmod.tab != 0)
1888 wp = NULL;
1889 else
1890 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1891 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1892 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001893 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1894 win_enter(wp, TRUE);
1895 else
1896 {
1897 /*
1898 * Split off help window; put it at far top if no position
1899 * specified, the current window is vertically split and narrow.
1900 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001901 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001902 if (cmdmod.split == 0 && curwin->w_width != Columns
1903 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001904 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001905 if (qi != &ql_info)
1906 flags |= WSP_NEWLOC; /* don't copy the location list */
1907
1908 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001909 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001910 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001911
1912 if (curwin->w_height < p_hh)
1913 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001914
1915 if (qi != &ql_info) /* not a quickfix list */
1916 {
1917 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001918 curwin->w_llist = qi;
1919 qi->qf_refcount++;
1920 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001921 }
1922
1923 if (!p_im)
1924 restart_edit = 0; /* don't want insert mode in help file */
1925 }
1926
1927 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 * If currently in the quickfix window, find another window to show the
1929 * file in.
1930 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001931 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001933 win_T *usable_win_ptr = NULL;
1934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 /*
1936 * If there is no file specified, we don't know where to go.
1937 * But do advance, otherwise ":cn" gets stuck.
1938 */
1939 if (qf_ptr->qf_fnum == 0)
1940 goto theend;
1941
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001943
1944 ll_ref = curwin->w_llist_ref;
1945 if (ll_ref != NULL)
1946 {
1947 /* Find a window using the same location list that is not a
1948 * quickfix window. */
1949 FOR_ALL_WINDOWS(usable_win_ptr)
1950 if (usable_win_ptr->w_llist == ll_ref
1951 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001952 {
1953 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001954 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001955 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001956 }
1957
1958 if (!usable_win)
1959 {
1960 /* Locate a window showing a normal buffer */
1961 FOR_ALL_WINDOWS(win)
1962 if (win->w_buffer->b_p_bt[0] == NUL)
1963 {
1964 usable_win = 1;
1965 break;
1966 }
1967 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001970 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001971 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001973 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001974 {
1975 tabpage_T *tp;
1976 win_T *wp;
1977
1978 FOR_ALL_TAB_WINDOWS(tp, wp)
1979 {
1980 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1981 {
1982 goto_tabpage_win(tp, wp);
1983 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001984 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001985 }
1986 }
1987 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001988win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001989
1990 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001991 * If there is only one window and it is the quickfix window, create a
1992 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001993 */
1994 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001996 flags = WSP_ABOVE;
1997 if (ll_ref != NULL)
1998 flags |= WSP_NEWLOC;
1999 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 goto failed; /* not enough room for window */
2001 opened_window = TRUE; /* close it when fail */
2002 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002003 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002004 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002005 if (ll_ref != NULL)
2006 {
2007 /* The new window should use the location list from the
2008 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 curwin->w_llist = ll_ref;
2010 ll_ref->qf_refcount++;
2011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 }
2013 else
2014 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002015 if (curwin->w_llist_ref != NULL)
2016 {
2017 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002018 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002019 if (win == NULL)
2020 {
2021 /* Find the window showing the selected file */
2022 FOR_ALL_WINDOWS(win)
2023 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2024 break;
2025 if (win == NULL)
2026 {
2027 /* Find a previous usable window */
2028 win = curwin;
2029 do
2030 {
2031 if (win->w_buffer->b_p_bt[0] == NUL)
2032 break;
2033 if (win->w_prev == NULL)
2034 win = lastwin; /* wrap around the top */
2035 else
2036 win = win->w_prev; /* go to previous window */
2037 } while (win != curwin);
2038 }
2039 }
2040 win_goto(win);
2041
2042 /* If the location list for the window is not set, then set it
2043 * to the location list from the location window */
2044 if (win->w_llist == NULL)
2045 {
2046 win->w_llist = ll_ref;
2047 ll_ref->qf_refcount++;
2048 }
2049 }
2050 else
2051 {
2052
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 /*
2054 * Try to find a window that shows the right buffer.
2055 * Default to the window just above the quickfix buffer.
2056 */
2057 win = curwin;
2058 altwin = NULL;
2059 for (;;)
2060 {
2061 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2062 break;
2063 if (win->w_prev == NULL)
2064 win = lastwin; /* wrap around the top */
2065 else
2066 win = win->w_prev; /* go to previous window */
2067
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002068 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
2070 /* Didn't find it, go to the window before the quickfix
2071 * window. */
2072 if (altwin != NULL)
2073 win = altwin;
2074 else if (curwin->w_prev != NULL)
2075 win = curwin->w_prev;
2076 else
2077 win = curwin->w_next;
2078 break;
2079 }
2080
2081 /* Remember a usable window. */
2082 if (altwin == NULL && !win->w_p_pvw
2083 && win->w_buffer->b_p_bt[0] == NUL)
2084 altwin = win;
2085 }
2086
2087 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090 }
2091#endif
2092
2093 /*
2094 * If there is a file name,
2095 * read the wanted file if needed, and check autowrite etc.
2096 */
2097 old_curbuf = curbuf;
2098 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002099
2100 if (qf_ptr->qf_fnum != 0)
2101 {
2102 if (qf_ptr->qf_type == 1)
2103 {
2104 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2105 * set b_p_ro flag). */
2106 if (!can_abandon(curbuf, forceit))
2107 {
2108 EMSG(_(e_nowrtmsg));
2109 ok = FALSE;
2110 }
2111 else
2112 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002113 ECMD_HIDE + ECMD_SET_HELP,
2114 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002115 }
2116 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002117 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002118 int old_qf_curlist = qi->qf_curlist;
2119 int is_abort = FALSE;
2120
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002121 ok = buflist_getfile(qf_ptr->qf_fnum,
2122 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002123 if (qi != &ql_info && !win_valid(oldwin))
2124 {
2125 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002126 is_abort = TRUE;
2127 opened_window = FALSE;
2128 }
2129 else if (old_qf_curlist != qi->qf_curlist
2130 || !is_qf_entry_present(qi, qf_ptr))
2131 {
2132 if (qi == &ql_info)
2133 EMSG(_("E925: Current quickfix was changed"));
2134 else
2135 EMSG(_("E926: Current location list was changed"));
2136 is_abort = TRUE;
2137 }
2138
2139 if (is_abort)
2140 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002141 ok = FALSE;
2142 qi = NULL;
2143 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002144 }
2145 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002146 }
2147
2148 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
2150 /* When not switched to another buffer, still need to set pc mark */
2151 if (curbuf == old_curbuf)
2152 setpcmark();
2153
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002154 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002156 /*
2157 * Go to line with error, unless qf_lnum is 0.
2158 */
2159 i = qf_ptr->qf_lnum;
2160 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002162 if (i > curbuf->b_ml.ml_line_count)
2163 i = curbuf->b_ml.ml_line_count;
2164 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002166 if (qf_ptr->qf_col > 0)
2167 {
2168 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002169#ifdef FEAT_VIRTUALEDIT
2170 curwin->w_cursor.coladd = 0;
2171#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002172 if (qf_ptr->qf_viscol == TRUE)
2173 {
2174 /*
2175 * Check each character from the beginning of the error
2176 * line up to the error column. For each tab character
2177 * found, reduce the error column value by the length of
2178 * a tab character.
2179 */
2180 line = ml_get_curline();
2181 screen_col = 0;
2182 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2183 {
2184 if (*line == NUL)
2185 break;
2186 if (*line++ == '\t')
2187 {
2188 curwin->w_cursor.col -= 7 - (screen_col % 8);
2189 screen_col += 8 - (screen_col % 8);
2190 }
2191 else
2192 ++screen_col;
2193 }
2194 }
2195 check_cursor();
2196 }
2197 else
2198 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 }
2200 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002201 {
2202 pos_T save_cursor;
2203
2204 /* Move the cursor to the first line in the buffer */
2205 save_cursor = curwin->w_cursor;
2206 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002207 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2208 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002209 curwin->w_cursor = save_cursor;
2210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212#ifdef FEAT_FOLDING
2213 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2214 foldOpenCursor();
2215#endif
2216 if (print_message)
2217 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002218 /* Update the screen before showing the message, unless the screen
2219 * scrolled up. */
2220 if (!msg_scrolled)
2221 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002223 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2225 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2226 /* Add the message, skipping leading whitespace and newlines. */
2227 len = (int)STRLEN(IObuff);
2228 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2229
2230 /* Output the message. Overwrite to avoid scrolling when the 'O'
2231 * flag is present in 'shortmess'; But when not jumping, print the
2232 * whole message. */
2233 i = msg_scroll;
2234 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2235 msg_scroll = TRUE;
2236 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2237 msg_scroll = FALSE;
2238 msg_attr_keep(IObuff, 0, TRUE);
2239 msg_scroll = i;
2240 }
2241 }
2242 else
2243 {
2244#ifdef FEAT_WINDOWS
2245 if (opened_window)
2246 win_close(curwin, TRUE); /* Close opened window */
2247#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002248 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250 /*
2251 * Couldn't open file, so put index back where it was. This could
2252 * happen if the file was readonly and we changed something.
2253 */
2254#ifdef FEAT_WINDOWS
2255failed:
2256#endif
2257 qf_ptr = old_qf_ptr;
2258 qf_index = old_qf_index;
2259 }
2260 }
2261theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002262 if (qi != NULL)
2263 {
2264 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2265 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#ifdef FEAT_WINDOWS
2268 if (p_swb != old_swb && opened_window)
2269 {
2270 /* Restore old 'switchbuf' value, but not when an autocommand or
2271 * modeline has changed the value. */
2272 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002275 swb_flags = old_swb_flags;
2276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 else
2278 free_string_option(old_swb);
2279 }
2280#endif
2281}
2282
2283/*
2284 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002285 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 */
2287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002288qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002290 buf_T *buf;
2291 char_u *fname;
2292 qfline_T *qfp;
2293 int i;
2294 int idx1 = 1;
2295 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002296 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002297 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002298 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002300 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002302 if (eap->cmdidx == CMD_llist)
2303 {
2304 qi = GET_LOC_LIST(curwin);
2305 if (qi == NULL)
2306 {
2307 EMSG(_(e_loclist));
2308 return;
2309 }
2310 }
2311
2312 if (qi->qf_curlist >= qi->qf_listcount
2313 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 EMSG(_(e_quickfix));
2316 return;
2317 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002318 if (*arg == '+')
2319 {
2320 ++arg;
2321 plus = TRUE;
2322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2324 {
2325 EMSG(_(e_trailing));
2326 return;
2327 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002328 if (plus)
2329 {
2330 i = qi->qf_lists[qi->qf_curlist].qf_index;
2331 idx2 = i + idx1;
2332 idx1 = i;
2333 }
2334 else
2335 {
2336 i = qi->qf_lists[qi->qf_curlist].qf_count;
2337 if (idx1 < 0)
2338 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2339 if (idx2 < 0)
2340 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002343 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002345 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2346 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2349 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002350 msg_putchar('\n');
2351 if (got_int)
2352 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002353
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002354 fname = NULL;
2355 if (qfp->qf_fnum != 0
2356 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2357 {
2358 fname = buf->b_fname;
2359 if (qfp->qf_type == 1) /* :helpgrep */
2360 fname = gettail(fname);
2361 }
2362 if (fname == NULL)
2363 sprintf((char *)IObuff, "%2d", i);
2364 else
2365 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2366 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002367 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002368 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2369 if (qfp->qf_lnum == 0)
2370 IObuff[0] = NUL;
2371 else if (qfp->qf_col == 0)
2372 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2373 else
2374 sprintf((char *)IObuff, ":%ld col %d",
2375 qfp->qf_lnum, qfp->qf_col);
2376 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2377 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2378 msg_puts_attr(IObuff, hl_attr(HLF_N));
2379 if (qfp->qf_pattern != NULL)
2380 {
2381 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2382 STRCAT(IObuff, ":");
2383 msg_puts(IObuff);
2384 }
2385 msg_puts((char_u *)" ");
2386
2387 /* Remove newlines and leading whitespace from the text. For an
2388 * unrecognized line keep the indent, the compiler may mark a word
2389 * with ^^^^. */
2390 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2392 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002393 msg_prt_line(IObuff, FALSE);
2394 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002396
2397 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002398 if (qfp == NULL)
2399 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002400 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 ui_breakcheck();
2402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403}
2404
2405/*
2406 * Remove newlines and leading whitespace from an error message.
2407 * Put the result in "buf[bufsize]".
2408 */
2409 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002410qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 int i;
2413 char_u *p = text;
2414
2415 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2416 {
2417 if (*p == '\n')
2418 {
2419 buf[i] = ' ';
2420 while (*++p != NUL)
2421 if (!vim_iswhite(*p) && *p != '\n')
2422 break;
2423 }
2424 else
2425 buf[i] = *p++;
2426 }
2427 buf[i] = NUL;
2428}
2429
2430/*
2431 * ":colder [count]": Up in the quickfix stack.
2432 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 * ":lolder [count]": Up in the location list stack.
2434 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
2436 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002437qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002439 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 int count;
2441
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002442 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2443 {
2444 qi = GET_LOC_LIST(curwin);
2445 if (qi == NULL)
2446 {
2447 EMSG(_(e_loclist));
2448 return;
2449 }
2450 }
2451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (eap->addr_count != 0)
2453 count = eap->line2;
2454 else
2455 count = 1;
2456 while (count--)
2457 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002458 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002460 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
2462 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002465 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 }
2467 else
2468 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002469 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {
2471 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002472 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002474 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 }
2476 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002477 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478}
2479
2480 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002481qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002484 qi->qf_curlist + 1, qi->qf_listcount,
2485 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002487 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488#endif
2489}
2490
2491/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002492 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 */
2494 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002495qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002497 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002498 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002499 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002501 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002503 qfp = qi->qf_lists[idx].qf_start;
2504 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002505 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002506 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002507 vim_free(qfp->qf_text);
2508 stop = (qfp == qfpnext);
2509 vim_free(qfp->qf_pattern);
2510 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002511 if (stop)
2512 /* Somehow qf_count may have an incorrect value, set it to 1
2513 * to avoid crashing when it's wrong.
2514 * TODO: Avoid qf_count being incorrect. */
2515 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002516 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002517 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002518 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002520 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002521 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002522 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002523
2524 qf_clean_dir_stack(&qi->qf_dir_stack);
2525 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526}
2527
2528/*
2529 * qf_mark_adjust: adjust marks
2530 */
2531 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002532qf_mark_adjust(
2533 win_T *wp,
2534 linenr_T line1,
2535 linenr_T line2,
2536 long amount,
2537 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002539 int i;
2540 qfline_T *qfp;
2541 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002542 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002543 int found_one = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002545 if (!curbuf->b_has_qf_entry)
2546 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002547 if (wp != NULL)
2548 {
2549 if (wp->w_llist == NULL)
2550 return;
2551 qi = wp->w_llist;
2552 }
2553
2554 for (idx = 0; idx < qi->qf_listcount; ++idx)
2555 if (qi->qf_lists[idx].qf_count)
2556 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002557 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2558 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (qfp->qf_fnum == curbuf->b_fnum)
2560 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002561 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2563 {
2564 if (amount == MAXLNUM)
2565 qfp->qf_cleared = TRUE;
2566 else
2567 qfp->qf_lnum += amount;
2568 }
2569 else if (amount_after && qfp->qf_lnum > line2)
2570 qfp->qf_lnum += amount_after;
2571 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002572
2573 if (!found_one)
2574 curbuf->b_has_qf_entry = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575}
2576
2577/*
2578 * Make a nice message out of the error character and the error number:
2579 * char number message
2580 * e or E 0 " error"
2581 * w or W 0 " warning"
2582 * i or I 0 " info"
2583 * 0 0 ""
2584 * other 0 " c"
2585 * e or E n " error n"
2586 * w or W n " warning n"
2587 * i or I n " info n"
2588 * 0 n " error n"
2589 * other n " c n"
2590 * 1 x "" :helpgrep
2591 */
2592 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002593qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
2595 static char_u buf[20];
2596 static char_u cc[3];
2597 char_u *p;
2598
2599 if (c == 'W' || c == 'w')
2600 p = (char_u *)" warning";
2601 else if (c == 'I' || c == 'i')
2602 p = (char_u *)" info";
2603 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2604 p = (char_u *)" error";
2605 else if (c == 0 || c == 1)
2606 p = (char_u *)"";
2607 else
2608 {
2609 cc[0] = ' ';
2610 cc[1] = c;
2611 cc[2] = NUL;
2612 p = cc;
2613 }
2614
2615 if (nr <= 0)
2616 return p;
2617
2618 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2619 return buf;
2620}
2621
2622#if defined(FEAT_WINDOWS) || defined(PROTO)
2623/*
2624 * ":cwindow": open the quickfix window if we have errors to display,
2625 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002626 * ":lwindow": open the location list window if we have locations to display,
2627 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 */
2629 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002630ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002632 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 win_T *win;
2634
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002635 if (eap->cmdidx == CMD_lwindow)
2636 {
2637 qi = GET_LOC_LIST(curwin);
2638 if (qi == NULL)
2639 return;
2640 }
2641
2642 /* Look for an existing quickfix window. */
2643 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
2645 /*
2646 * If a quickfix window is open but we have no errors to display,
2647 * close the window. If a quickfix window is not open, then open
2648 * it if we have errors; otherwise, leave it closed.
2649 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002650 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002651 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002652 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654 if (win != NULL)
2655 ex_cclose(eap);
2656 }
2657 else if (win == NULL)
2658 ex_copen(eap);
2659}
2660
2661/*
2662 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002663 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002666ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002668 win_T *win = NULL;
2669 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002671 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2672 {
2673 qi = GET_LOC_LIST(curwin);
2674 if (qi == NULL)
2675 return;
2676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002678 /* Find existing quickfix window and close it. */
2679 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (win != NULL)
2681 win_close(win, FALSE);
2682}
2683
2684/*
2685 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002686 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 */
2688 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002689ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002691 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002694 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002695 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002696 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002698 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2699 {
2700 qi = GET_LOC_LIST(curwin);
2701 if (qi == NULL)
2702 {
2703 EMSG(_(e_loclist));
2704 return;
2705 }
2706 }
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (eap->addr_count != 0)
2709 height = eap->line2;
2710 else
2711 height = QF_WINHEIGHT;
2712
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714#ifdef FEAT_GUI
2715 need_mouse_correct = TRUE;
2716#endif
2717
2718 /*
2719 * Find existing quickfix window, or open a new one.
2720 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002721 win = qf_find_win(qi);
2722
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002723 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002726 if (eap->addr_count != 0)
2727 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002728 if (cmdmod.split & WSP_VERT)
2729 {
2730 if (height != W_WIDTH(win))
2731 win_setwidth(height);
2732 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002733 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002734 win_setheight(height);
2735 }
2736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 else
2738 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002739 qf_buf = qf_find_buf(qi);
2740
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 /* The current window becomes the previous window afterwards. */
2742 win = curwin;
2743
Bram Moolenaar77642c02012-11-20 17:55:10 +01002744 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2745 && cmdmod.split == 0)
2746 /* Create the new window at the very bottom, except when
2747 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002748 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002749 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002751 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002753 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002755 /*
2756 * For the location list window, create a reference to the
2757 * location list from the window 'win'.
2758 */
2759 curwin->w_llist_ref = win->w_llist;
2760 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002762
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002763 if (oldwin != curwin)
2764 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002765 if (qf_buf != NULL)
2766 /* Use the existing quickfix buffer */
2767 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002768 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002769 else
2770 {
2771 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002772 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002773 /* switch off 'swapfile' */
2774 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2775 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002776 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002777 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002778 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002779#ifdef FEAT_DIFF
2780 curwin->w_p_diff = FALSE;
2781#endif
2782#ifdef FEAT_FOLDING
2783 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2784 OPT_LOCAL);
2785#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002788 /* Only set the height when still in the same tab page and there is no
2789 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002790 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 win_setheight(height);
2792 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2793 if (win_valid(win))
2794 prevwin = win;
2795 }
2796
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002797 qf_set_title_var(qi);
2798
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 /*
2800 * Fill the buffer with the quickfix list.
2801 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002802 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002804 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 curwin->w_cursor.col = 0;
2806 check_cursor();
2807 update_topline(); /* scroll to show the line */
2808}
2809
2810/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02002811 * Move the cursor in the quickfix window to "lnum".
2812 */
2813 static void
2814qf_win_goto(win_T *win, linenr_T lnum)
2815{
2816 win_T *old_curwin = curwin;
2817
2818 curwin = win;
2819 curbuf = win->w_buffer;
2820 curwin->w_cursor.lnum = lnum;
2821 curwin->w_cursor.col = 0;
2822#ifdef FEAT_VIRTUALEDIT
2823 curwin->w_cursor.coladd = 0;
2824#endif
2825 curwin->w_curswant = 0;
2826 update_topline(); /* scroll to show the line */
2827 redraw_later(VALID);
2828 curwin->w_redr_status = TRUE; /* update ruler */
2829 curwin = old_curwin;
2830 curbuf = curwin->w_buffer;
2831}
2832
2833/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02002834 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02002835 */
2836 void
2837ex_cbottom(exarg_T *eap UNUSED)
2838{
Bram Moolenaar537ef082016-07-09 17:56:19 +02002839 qf_info_T *qi = &ql_info;
2840 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02002841
Bram Moolenaar537ef082016-07-09 17:56:19 +02002842 if (eap->cmdidx == CMD_lbottom)
2843 {
2844 qi = GET_LOC_LIST(curwin);
2845 if (qi == NULL)
2846 {
2847 EMSG(_(e_loclist));
2848 return;
2849 }
2850 }
2851
2852 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02002853 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
2854 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
2855}
2856
2857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 * Return the number of the current entry (line number in the quickfix
2859 * window).
2860 */
2861 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002862qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002864 qf_info_T *qi = &ql_info;
2865
2866 if (IS_LL_WINDOW(wp))
2867 /* In the location list window, use the referenced location list */
2868 qi = wp->w_llist_ref;
2869
2870 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871}
2872
2873/*
2874 * Update the cursor position in the quickfix window to the current error.
2875 * Return TRUE if there is a quickfix window.
2876 */
2877 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002878qf_win_pos_update(
2879 qf_info_T *qi,
2880 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
2882 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002883 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
2885 /*
2886 * Put the cursor on the current error in the quickfix window, so that
2887 * it's viewable.
2888 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002889 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 if (win != NULL
2891 && qf_index <= win->w_buffer->b_ml.ml_line_count
2892 && old_qf_index != qf_index)
2893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 if (qf_index > old_qf_index)
2895 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02002896 win->w_redraw_top = old_qf_index;
2897 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
2899 else
2900 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02002901 win->w_redraw_top = qf_index;
2902 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02002904 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906 return win != NULL;
2907}
2908
2909/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002910 * Check whether the given window is displaying the specified quickfix/location
2911 * list buffer
2912 */
2913 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002914is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002915{
2916 /*
2917 * A window displaying the quickfix buffer will have the w_llist_ref field
2918 * set to NULL.
2919 * A window displaying a location list buffer will have the w_llist_ref
2920 * pointing to the location list.
2921 */
2922 if (bt_quickfix(win->w_buffer))
2923 if ((qi == &ql_info && win->w_llist_ref == NULL)
2924 || (qi != &ql_info && win->w_llist_ref == qi))
2925 return TRUE;
2926
2927 return FALSE;
2928}
2929
2930/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002931 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002932 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002933 */
2934 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002935qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002936{
2937 win_T *win;
2938
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002939 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002940 if (is_qf_win(win, qi))
2941 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002942
2943 return win;
2944}
2945
2946/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002947 * Find a quickfix buffer.
2948 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
2950 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002951qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002953 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002954 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
Bram Moolenaar9c102382006-05-03 21:26:49 +00002956 FOR_ALL_TAB_WINDOWS(tp, win)
2957 if (is_qf_win(win, qi))
2958 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002959
Bram Moolenaar9c102382006-05-03 21:26:49 +00002960 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961}
2962
2963/*
2964 * Find the quickfix buffer. If it exists, update the contents.
2965 */
2966 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02002967qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002970 win_T *win;
2971 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002975 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 if (buf != NULL)
2977 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02002978 linenr_T old_line_count = buf->b_ml.ml_line_count;
2979
2980 if (old_last == NULL)
2981 /* set curwin/curbuf to buf and save a few things */
2982 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002984 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002985 {
2986 curwin_save = curwin;
2987 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002988 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002989 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002990 }
2991
Bram Moolenaar864293a2016-06-02 13:40:04 +02002992 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01002993
Bram Moolenaar864293a2016-06-02 13:40:04 +02002994 if (old_last == NULL)
2995 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02002996 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02002997
2998 /* restore curwin/curbuf and a few other things */
2999 aucmd_restbuf(&aco);
3000 }
3001
3002 /* Only redraw when added lines are visible. This avoids flickering
3003 * when the added lines are not visible. */
3004 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3005 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007}
3008
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003009/*
3010 * Set "w:quickfix_title" if "qi" has a title.
3011 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003012 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003013qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003014{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003015 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3016 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003017 qi->qf_lists[qi->qf_curlist].qf_title);
3018}
3019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020/*
3021 * Fill current buffer with quickfix errors, replacing any previous contents.
3022 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003023 * If "old_last" is not NULL append the items after this one.
3024 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3025 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 */
3027 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003028qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003030 linenr_T lnum;
3031 qfline_T *qfp;
3032 buf_T *errbuf;
3033 int len;
3034 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaar864293a2016-06-02 13:40:04 +02003036 if (old_last == NULL)
3037 {
3038 if (buf != curbuf)
3039 {
3040 EMSG2(_(e_intern2), "qf_fill_buffer()");
3041 return;
3042 }
3043
3044 /* delete all existing lines */
3045 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3046 (void)ml_delete((linenr_T)1, FALSE);
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048
3049 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003050 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 {
3052 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003053 if (old_last == NULL)
3054 {
3055 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3056 lnum = 0;
3057 }
3058 else
3059 {
3060 qfp = old_last->qf_next;
3061 lnum = buf->b_ml.ml_line_count;
3062 }
3063 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 {
3065 if (qfp->qf_fnum != 0
3066 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3067 && errbuf->b_fname != NULL)
3068 {
3069 if (qfp->qf_type == 1) /* :helpgrep */
3070 STRCPY(IObuff, gettail(errbuf->b_fname));
3071 else
3072 STRCPY(IObuff, errbuf->b_fname);
3073 len = (int)STRLEN(IObuff);
3074 }
3075 else
3076 len = 0;
3077 IObuff[len++] = '|';
3078
3079 if (qfp->qf_lnum > 0)
3080 {
3081 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3082 len += (int)STRLEN(IObuff + len);
3083
3084 if (qfp->qf_col > 0)
3085 {
3086 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3087 len += (int)STRLEN(IObuff + len);
3088 }
3089
3090 sprintf((char *)IObuff + len, "%s",
3091 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3092 len += (int)STRLEN(IObuff + len);
3093 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003094 else if (qfp->qf_pattern != NULL)
3095 {
3096 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3097 len += (int)STRLEN(IObuff + len);
3098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 IObuff[len++] = '|';
3100 IObuff[len++] = ' ';
3101
3102 /* Remove newlines and leading whitespace from the text.
3103 * For an unrecognized line keep the indent, the compiler may
3104 * mark a word with ^^^^. */
3105 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3106 IObuff + len, IOSIZE - len);
3107
Bram Moolenaar864293a2016-06-02 13:40:04 +02003108 if (ml_append_buf(buf, lnum, IObuff,
3109 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003111 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003113 if (qfp == NULL)
3114 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003116
3117 if (old_last == NULL)
3118 /* Delete the empty line which is now at the end */
3119 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 }
3121
3122 /* correct cursor position */
3123 check_lnums(TRUE);
3124
Bram Moolenaar864293a2016-06-02 13:40:04 +02003125 if (old_last == NULL)
3126 {
3127 /* Set the 'filetype' to "qf" each time after filling the buffer.
3128 * This resembles reading a file into a buffer, it's more logical when
3129 * using autocommands. */
3130 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3131 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
3133#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003134 keep_filetype = TRUE; /* don't detect 'filetype' */
3135 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003137 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003139 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003141 /* make sure it will be redrawn */
3142 redraw_curbuf_later(NOT_VALID);
3143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 /* Restore KeyTyped, setting 'filetype' may reset it. */
3146 KeyTyped = old_KeyTyped;
3147}
3148
3149#endif /* FEAT_WINDOWS */
3150
3151/*
3152 * Return TRUE if "buf" is the quickfix buffer.
3153 */
3154 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003155bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003157 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158}
3159
3160/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003161 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3162 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 */
3164 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003165bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003167 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3168 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169}
3170
3171/*
3172 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3173 */
3174 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003175bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003177 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178}
3179
3180 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003181bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 if (bt_dontwrite(buf))
3184 {
3185 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3186 return TRUE;
3187 }
3188 return FALSE;
3189}
3190
3191/*
3192 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3193 * and 'bufhidden'.
3194 */
3195 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003196buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
3198 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3199 switch (buf->b_p_bh[0])
3200 {
3201 case 'u': /* "unload" */
3202 case 'w': /* "wipe" */
3203 case 'd': return FALSE; /* "delete" */
3204 case 'h': return TRUE; /* "hide" */
3205 }
3206 return (p_hid || cmdmod.hide);
3207}
3208
3209/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003210 * Return TRUE when using ":vimgrep" for ":grep".
3211 */
3212 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003213grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003214{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003215 return ((cmdidx == CMD_grep
3216 || cmdidx == CMD_lgrep
3217 || cmdidx == CMD_grepadd
3218 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003219 && STRCMP("internal",
3220 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3221}
3222
3223/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003224 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 */
3226 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003227ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003229 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 char_u *cmd;
3231 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003232 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003233 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003234 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003235#ifdef FEAT_AUTOCMD
3236 char_u *au_name = NULL;
3237
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003238 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3239 if (grep_internal(eap->cmdidx))
3240 {
3241 ex_vimgrep(eap);
3242 return;
3243 }
3244
Bram Moolenaar7c626922005-02-07 22:01:03 +00003245 switch (eap->cmdidx)
3246 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003247 case CMD_make: au_name = (char_u *)"make"; break;
3248 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3249 case CMD_grep: au_name = (char_u *)"grep"; break;
3250 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3251 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3252 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003253 default: break;
3254 }
3255 if (au_name != NULL)
3256 {
3257 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3258 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003259# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003260 if (did_throw || force_abort)
3261 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003262# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003263 }
3264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
Bram Moolenaara6557602006-02-04 22:43:20 +00003266 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3267 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003268 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003271 fname = get_mef_name();
3272 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003274 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 /*
3277 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3278 */
3279 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3280 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003281 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 cmd = alloc(len);
3283 if (cmd == NULL)
3284 return;
3285 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3286 (char *)p_shq);
3287 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003288 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 /*
3290 * Output a newline if there's something else than the :make command that
3291 * was typed (in which case the cursor is in column 0).
3292 */
3293 if (msg_col == 0)
3294 msg_didout = FALSE;
3295 msg_start();
3296 MSG_PUTS(":!");
3297 msg_outtrans(cmd); /* show what we are doing */
3298
3299 /* let the shell know if we are redirecting output or not */
3300 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3301
3302#ifdef AMIGA
3303 out_flush();
3304 /* read window status report and redraw before message */
3305 (void)char_avail();
3306#endif
3307
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003308 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003309 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3310 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003311 && eap->cmdidx != CMD_lgrepadd),
3312 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003313 if (wp != NULL)
3314 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003315#ifdef FEAT_AUTOCMD
3316 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003317 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003318 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3319 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003320 if (qi->qf_curlist < qi->qf_listcount)
3321 res = qi->qf_lists[qi->qf_curlist].qf_count;
3322 else
3323 res = 0;
3324 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003325#endif
3326 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003327 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar7c626922005-02-07 22:01:03 +00003329 mch_remove(fname);
3330 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 vim_free(cmd);
3332}
3333
3334/*
3335 * Return the name for the errorfile, in allocated memory.
3336 * Find a new unique name when 'makeef' contains "##".
3337 * Returns NULL for error.
3338 */
3339 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003340get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 char_u *p;
3343 char_u *name;
3344 static int start = -1;
3345 static int off = 0;
3346#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003347 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#endif
3349
3350 if (*p_mef == NUL)
3351 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003352 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (name == NULL)
3354 EMSG(_(e_notmp));
3355 return name;
3356 }
3357
3358 for (p = p_mef; *p; ++p)
3359 if (p[0] == '#' && p[1] == '#')
3360 break;
3361
3362 if (*p == NUL)
3363 return vim_strsave(p_mef);
3364
3365 /* Keep trying until the name doesn't exist yet. */
3366 for (;;)
3367 {
3368 if (start == -1)
3369 start = mch_get_pid();
3370 else
3371 off += 19;
3372
3373 name = alloc((unsigned)STRLEN(p_mef) + 30);
3374 if (name == NULL)
3375 break;
3376 STRCPY(name, p_mef);
3377 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3378 STRCAT(name, p + 2);
3379 if (mch_getperm(name) < 0
3380#ifdef HAVE_LSTAT
3381 /* Don't accept a symbolic link, its a security risk. */
3382 && mch_lstat((char *)name, &sb) < 0
3383#endif
3384 )
3385 break;
3386 vim_free(name);
3387 }
3388 return name;
3389}
3390
3391/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003392 * Returns the number of valid entries in the current quickfix/location list.
3393 */
3394 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003395qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003396{
3397 qf_info_T *qi = &ql_info;
3398 qfline_T *qfp;
3399 int i, sz = 0;
3400 int prev_fnum = 0;
3401
3402 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3403 {
3404 /* Location list */
3405 qi = GET_LOC_LIST(curwin);
3406 if (qi == NULL)
3407 return 0;
3408 }
3409
3410 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003411 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003412 ++i, qfp = qfp->qf_next)
3413 {
3414 if (qfp->qf_valid)
3415 {
3416 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3417 sz++; /* Count all valid entries */
3418 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3419 {
3420 /* Count the number of files */
3421 sz++;
3422 prev_fnum = qfp->qf_fnum;
3423 }
3424 }
3425 }
3426
3427 return sz;
3428}
3429
3430/*
3431 * Returns the current index of the quickfix/location list.
3432 * Returns 0 if there is an error.
3433 */
3434 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003435qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003436{
3437 qf_info_T *qi = &ql_info;
3438
3439 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3440 {
3441 /* Location list */
3442 qi = GET_LOC_LIST(curwin);
3443 if (qi == NULL)
3444 return 0;
3445 }
3446
3447 return qi->qf_lists[qi->qf_curlist].qf_index;
3448}
3449
3450/*
3451 * Returns the current index in the quickfix/location list (counting only valid
3452 * entries). If no valid entries are in the list, then returns 1.
3453 */
3454 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003455qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003456{
3457 qf_info_T *qi = &ql_info;
3458 qf_list_T *qfl;
3459 qfline_T *qfp;
3460 int i, eidx = 0;
3461 int prev_fnum = 0;
3462
3463 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3464 {
3465 /* Location list */
3466 qi = GET_LOC_LIST(curwin);
3467 if (qi == NULL)
3468 return 1;
3469 }
3470
3471 qfl = &qi->qf_lists[qi->qf_curlist];
3472 qfp = qfl->qf_start;
3473
3474 /* check if the list has valid errors */
3475 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3476 return 1;
3477
3478 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3479 {
3480 if (qfp->qf_valid)
3481 {
3482 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3483 {
3484 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3485 {
3486 /* Count the number of files */
3487 eidx++;
3488 prev_fnum = qfp->qf_fnum;
3489 }
3490 }
3491 else
3492 eidx++;
3493 }
3494 }
3495
3496 return eidx ? eidx : 1;
3497}
3498
3499/*
3500 * Get the 'n'th valid error entry in the quickfix or location list.
3501 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3502 * For :cdo and :ldo returns the 'n'th valid error entry.
3503 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3504 */
3505 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003506qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003507{
3508 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3509 qfline_T *qfp = qfl->qf_start;
3510 int i, eidx;
3511 int prev_fnum = 0;
3512
3513 /* check if the list has valid errors */
3514 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3515 return 1;
3516
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003517 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003518 i++, qfp = qfp->qf_next)
3519 {
3520 if (qfp->qf_valid)
3521 {
3522 if (fdo)
3523 {
3524 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3525 {
3526 /* Count the number of files */
3527 eidx++;
3528 prev_fnum = qfp->qf_fnum;
3529 }
3530 }
3531 else
3532 eidx++;
3533 }
3534
3535 if (eidx == n)
3536 break;
3537 }
3538
3539 if (i <= qfl->qf_count)
3540 return i;
3541 else
3542 return 1;
3543}
3544
3545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003547 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003548 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 */
3550 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003551ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003553 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003554 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003555
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003556 if (eap->cmdidx == CMD_ll
3557 || eap->cmdidx == CMD_lrewind
3558 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003559 || eap->cmdidx == CMD_llast
3560 || eap->cmdidx == CMD_ldo
3561 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003562 {
3563 qi = GET_LOC_LIST(curwin);
3564 if (qi == NULL)
3565 {
3566 EMSG(_(e_loclist));
3567 return;
3568 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003569 }
3570
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003571 if (eap->addr_count > 0)
3572 errornr = (int)eap->line2;
3573 else
3574 {
3575 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3576 errornr = 0;
3577 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3578 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3579 errornr = 1;
3580 else
3581 errornr = 32767;
3582 }
3583
3584 /* For cdo and ldo commands, jump to the nth valid error.
3585 * For cfdo and lfdo commands, jump to the nth valid file entry.
3586 */
3587 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3588 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3589 errornr = qf_get_nth_valid_entry(qi,
3590 eap->addr_count > 0 ? (int)eap->line1 : 1,
3591 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3592
3593 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594}
3595
3596/*
3597 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003598 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003599 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 */
3601 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003602ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003604 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003605 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003606
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003607 if (eap->cmdidx == CMD_lnext
3608 || eap->cmdidx == CMD_lNext
3609 || eap->cmdidx == CMD_lprevious
3610 || eap->cmdidx == CMD_lnfile
3611 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003612 || eap->cmdidx == CMD_lpfile
3613 || eap->cmdidx == CMD_ldo
3614 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003615 {
3616 qi = GET_LOC_LIST(curwin);
3617 if (qi == NULL)
3618 {
3619 EMSG(_(e_loclist));
3620 return;
3621 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003622 }
3623
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003624 if (eap->addr_count > 0 &&
3625 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3626 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3627 errornr = (int)eap->line2;
3628 else
3629 errornr = 1;
3630
3631 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3632 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003634 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3635 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003637 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3638 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 ? BACKWARD_FILE
3640 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003641 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642}
3643
3644/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003645 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003646 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
3648 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003649ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003651 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003652 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003653#ifdef FEAT_AUTOCMD
3654 char_u *au_name = NULL;
3655#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003656
3657 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003658 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003659 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003660
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003661#ifdef FEAT_AUTOCMD
3662 switch (eap->cmdidx)
3663 {
3664 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3665 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3666 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3667 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3668 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3669 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3670 default: break;
3671 }
3672 if (au_name != NULL)
3673 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3674#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003675#ifdef FEAT_BROWSE
3676 if (cmdmod.browse)
3677 {
3678 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3679 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3680 if (browse_file == NULL)
3681 return;
3682 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3683 vim_free(browse_file);
3684 }
3685 else
3686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003688 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003689
3690 /*
3691 * This function is used by the :cfile, :cgetfile and :caddfile
3692 * commands.
3693 * :cfile always creates a new quickfix list and jumps to the
3694 * first error.
3695 * :cgetfile creates a new quickfix list but doesn't jump to the
3696 * first error.
3697 * :caddfile adds to an existing quickfix list. If there is no
3698 * quickfix list then a new list is created.
3699 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003700 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003701 && eap->cmdidx != CMD_laddfile),
3702 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003703 && (eap->cmdidx == CMD_cfile
3704 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003705 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003706#ifdef FEAT_AUTOCMD
3707 if (au_name != NULL)
3708 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3709#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003710 if (wp != NULL)
3711 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003712 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003713 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003714
3715 else
3716 {
3717#ifdef FEAT_AUTOCMD
3718 if (au_name != NULL)
3719 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3720#endif
3721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722}
3723
3724/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003725 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003726 * ":vimgrepadd {pattern} file(s)"
3727 * ":lvimgrep {pattern} file(s)"
3728 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003729 */
3730 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003731ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003732{
Bram Moolenaar81695252004-12-29 20:58:21 +00003733 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003734 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003735 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003736 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003737 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003738 char_u *s;
3739 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003740 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003741 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003742#ifdef FEAT_AUTOCMD
3743 qfline_T *cur_qf_start;
3744#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003745 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003746 buf_T *buf;
3747 int duplicate_name = FALSE;
3748 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003749 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003750 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003751 buf_T *first_match_buf = NULL;
3752 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003753 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003754#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3755 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003756#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003757 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003758 int flags = 0;
3759 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003760 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003761 char_u *dirname_start = NULL;
3762 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003763 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003764#ifdef FEAT_AUTOCMD
3765 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003766
3767 switch (eap->cmdidx)
3768 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003769 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3770 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3771 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003772 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003773 case CMD_grep: au_name = (char_u *)"grep"; break;
3774 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3775 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3776 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003777 default: break;
3778 }
3779 if (au_name != NULL)
3780 {
3781 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3782 curbuf->b_fname, TRUE, curbuf);
3783 if (did_throw || force_abort)
3784 return;
3785 }
3786#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003787
Bram Moolenaar754b5602006-02-09 23:53:20 +00003788 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003789 || eap->cmdidx == CMD_lvimgrep
3790 || eap->cmdidx == CMD_lgrepadd
3791 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003792 {
3793 qi = ll_get_or_alloc_list(curwin);
3794 if (qi == NULL)
3795 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003796 }
3797
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003798 if (eap->addr_count > 0)
3799 tomatch = eap->line2;
3800 else
3801 tomatch = MAXLNUM;
3802
Bram Moolenaar81695252004-12-29 20:58:21 +00003803 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003804 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003805 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003806 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003807 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003808 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003809 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003810 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003811 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003812
3813 if (s != NULL && *s == NUL)
3814 {
3815 /* Pattern is empty, use last search pattern. */
3816 if (last_search_pat() == NULL)
3817 {
3818 EMSG(_(e_noprevre));
3819 goto theend;
3820 }
3821 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3822 }
3823 else
3824 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3825
Bram Moolenaar86b68352004-12-27 21:59:20 +00003826 if (regmatch.regprog == NULL)
3827 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003828 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003829 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003830
3831 p = skipwhite(p);
3832 if (*p == NUL)
3833 {
3834 EMSG(_("E683: File name missing or invalid pattern"));
3835 goto theend;
3836 }
3837
Bram Moolenaar754b5602006-02-09 23:53:20 +00003838 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003839 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003840 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003841 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003842 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003843
3844 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003845 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003846 goto theend;
3847 if (fcount == 0)
3848 {
3849 EMSG(_(e_nomatch));
3850 goto theend;
3851 }
3852
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003853 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3854 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003855 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003856 {
3857 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003858 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003859 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003860
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003861 /* Remember the current directory, because a BufRead autocommand that does
3862 * ":lcd %:p:h" changes the meaning of short path names. */
3863 mch_dirname(dirname_start, MAXPATHL);
3864
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003865#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003866 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003867 * changing the current quickfix list. */
3868 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3869#endif
3870
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003871 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003872 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003873 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003874 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003875 if (time(NULL) > seconds)
3876 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003877 /* Display the file name every second or so, show the user we are
3878 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003879 seconds = time(NULL);
3880 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003881 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003882 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003883 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003884 else
3885 {
3886 msg_outtrans(p);
3887 vim_free(p);
3888 }
3889 msg_clr_eos();
3890 msg_didout = FALSE; /* overwrite this message */
3891 msg_nowait = TRUE; /* don't wait for this message */
3892 msg_col = 0;
3893 out_flush();
3894 }
3895
Bram Moolenaar81695252004-12-29 20:58:21 +00003896 buf = buflist_findname_exp(fnames[fi]);
3897 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3898 {
3899 /* Remember that a buffer with this name already exists. */
3900 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003901 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003902 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003903
3904#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3905 /* Don't do Filetype autocommands to avoid loading syntax and
3906 * indent scripts, a great speed improvement. */
3907 save_ei = au_event_disable(",Filetype");
3908#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003909 /* Don't use modelines here, it's useless. */
3910 save_mls = p_mls;
3911 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003912
3913 /* Load file into a buffer, so that 'fileencoding' is detected,
3914 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003915 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003916
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003917 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003918#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3919 au_event_restore(save_ei);
3920#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003921 }
3922 else
3923 /* Use existing, loaded buffer. */
3924 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003925
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003926#ifdef FEAT_AUTOCMD
3927 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3928 {
3929 int idx;
3930
3931 /* Autocommands changed the quickfix list. Find the one we were
3932 * using and restore it. */
3933 for (idx = 0; idx < LISTCOUNT; ++idx)
3934 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3935 {
3936 qi->qf_curlist = idx;
3937 break;
3938 }
3939 if (idx == LISTCOUNT)
3940 {
3941 /* List cannot be found, create a new one. */
3942 qf_new_list(qi, *eap->cmdlinep);
3943 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3944 }
3945 }
3946#endif
3947
Bram Moolenaar81695252004-12-29 20:58:21 +00003948 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003949 {
3950 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003951 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003952 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003953 else
3954 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003955 /* Try for a match in all lines of the buffer.
3956 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003957 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003958 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3959 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003960 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003961 col = 0;
3962 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003963 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003964 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003965 ;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003966 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003967 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003968 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003969 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003970 ml_get_buf(buf,
3971 regmatch.startpos[0].lnum + lnum, FALSE),
3972 regmatch.startpos[0].lnum + lnum,
3973 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003974 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003975 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003976 0, /* nr */
3977 0, /* type */
3978 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003979 ) == FAIL)
3980 {
3981 got_int = TRUE;
3982 break;
3983 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003984 found_match = TRUE;
3985 if (--tomatch == 0)
3986 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003987 if ((flags & VGR_GLOBAL) == 0
3988 || regmatch.endpos[0].lnum > 0)
3989 break;
3990 col = regmatch.endpos[0].col
3991 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003992 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003993 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003994 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003995 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003996 if (got_int)
3997 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003998 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003999#ifdef FEAT_AUTOCMD
4000 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4001#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004002
4003 if (using_dummy)
4004 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004005 if (found_match && first_match_buf == NULL)
4006 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004007 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004008 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004009 /* Never keep a dummy buffer if there is another buffer
4010 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004011 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004012 buf = NULL;
4013 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004014 else if (!cmdmod.hide
4015 || buf->b_p_bh[0] == 'u' /* "unload" */
4016 || buf->b_p_bh[0] == 'w' /* "wipe" */
4017 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004018 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004019 /* When no match was found we don't need to remember the
4020 * buffer, wipe it out. If there was a match and it
4021 * wasn't the first one or we won't jump there: only
4022 * unload the buffer.
4023 * Ignore 'hidden' here, because it may lead to having too
4024 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004025 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004026 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004027 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004028 buf = NULL;
4029 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004030 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004031 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004032 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004033 buf = NULL;
4034 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004035 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004036
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004037 if (buf != NULL)
4038 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004039 /* If the buffer is still loaded we need to use the
4040 * directory we jumped to below. */
4041 if (buf == first_match_buf
4042 && target_dir == NULL
4043 && STRCMP(dirname_start, dirname_now) != 0)
4044 target_dir = vim_strsave(dirname_now);
4045
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004046 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004047 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004048 * need to be done (again). But not the window-local
4049 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004050 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004051#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004052 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4053 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004054#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004055 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004056 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004057 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004058 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004059 }
4060 }
4061
4062 FreeWild(fcount, fnames);
4063
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004064 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4065 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4066 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004067
4068#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004069 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004070#endif
4071
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004072#ifdef FEAT_AUTOCMD
4073 if (au_name != NULL)
4074 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4075 curbuf->b_fname, TRUE, curbuf);
4076#endif
4077
Bram Moolenaar86b68352004-12-27 21:59:20 +00004078 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004079 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004080 {
4081 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004082 {
4083 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004084 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004085 if (buf != curbuf)
4086 /* If we jumped to another buffer redrawing will already be
4087 * taken care of. */
4088 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004089
4090 /* Jump to the directory used after loading the buffer. */
4091 if (curbuf == first_match_buf && target_dir != NULL)
4092 {
4093 exarg_T ea;
4094
4095 ea.arg = target_dir;
4096 ea.cmdidx = CMD_lcd;
4097 ex_cd(&ea);
4098 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004099 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004100 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004101 else
4102 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004103
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004104 /* If we loaded a dummy buffer into the current window, the autocommands
4105 * may have messed up things, need to redraw and recompute folds. */
4106 if (redraw_for_dummy)
4107 {
4108#ifdef FEAT_FOLDING
4109 foldUpdateAll(curwin);
4110#else
4111 redraw_later(NOT_VALID);
4112#endif
4113 }
4114
Bram Moolenaar86b68352004-12-27 21:59:20 +00004115theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004116 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004117 vim_free(dirname_now);
4118 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004119 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004120 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004121}
4122
4123/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004124 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00004125 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004126 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
4127 * If "s" is not NULL terminate the pattern with a NUL.
4128 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00004129 */
4130 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004131skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004132{
4133 int c;
4134
4135 if (vim_isIDc(*p))
4136 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004137 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004138 if (s != NULL)
4139 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004140 p = skiptowhite(p);
4141 if (s != NULL && *p != NUL)
4142 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004143 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004144 else
4145 {
4146 /* ":vimgrep /pattern/[g][j] fname" */
4147 if (s != NULL)
4148 *s = p + 1;
4149 c = *p;
4150 p = skip_regexp(p + 1, c, TRUE, NULL);
4151 if (*p != c)
4152 return NULL;
4153
4154 /* Truncate the pattern. */
4155 if (s != NULL)
4156 *p = NUL;
4157 ++p;
4158
4159 /* Find the flags */
4160 while (*p == 'g' || *p == 'j')
4161 {
4162 if (flags != NULL)
4163 {
4164 if (*p == 'g')
4165 *flags |= VGR_GLOBAL;
4166 else
4167 *flags |= VGR_NOJUMP;
4168 }
4169 ++p;
4170 }
4171 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004172 return p;
4173}
4174
4175/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004176 * Restore current working directory to "dirname_start" if they differ, taking
4177 * into account whether it is set locally or globally.
4178 */
4179 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004180restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004181{
4182 char_u *dirname_now = alloc(MAXPATHL);
4183
4184 if (NULL != dirname_now)
4185 {
4186 mch_dirname(dirname_now, MAXPATHL);
4187 if (STRCMP(dirname_start, dirname_now) != 0)
4188 {
4189 /* If the directory has changed, change it back by building up an
4190 * appropriate ex command and executing it. */
4191 exarg_T ea;
4192
4193 ea.arg = dirname_start;
4194 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4195 ex_cd(&ea);
4196 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004197 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004198 }
4199}
4200
4201/*
4202 * Load file "fname" into a dummy buffer and return the buffer pointer,
4203 * placing the directory resulting from the buffer load into the
4204 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4205 * prior to calling this function. Restores directory to "dirname_start" prior
4206 * to returning, if autocmds or the 'autochdir' option have changed it.
4207 *
4208 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4209 * or wipe_dummy_buffer() later!
4210 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004211 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004212 */
4213 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004214load_dummy_buffer(
4215 char_u *fname,
4216 char_u *dirname_start, /* in: old directory */
4217 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004218{
4219 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004220 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004221 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004222 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004223
4224 /* Allocate a buffer without putting it in the buffer list. */
4225 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4226 if (newbuf == NULL)
4227 return NULL;
4228
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004229 /* Init the options. */
4230 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4231
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004232 /* need to open the memfile before putting the buffer in a window */
4233 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004234 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004235 /* set curwin/curbuf to buf and save a few things */
4236 aucmd_prepbuf(&aco, newbuf);
4237
4238 /* Need to set the filename for autocommands. */
4239 (void)setfname(curbuf, fname, NULL, FALSE);
4240
Bram Moolenaar81695252004-12-29 20:58:21 +00004241 /* Create swap file now to avoid the ATTENTION message. */
4242 check_need_swap(TRUE);
4243
4244 /* Remove the "dummy" flag, otherwise autocommands may not
4245 * work. */
4246 curbuf->b_flags &= ~BF_DUMMY;
4247
4248 if (readfile(fname, NULL,
4249 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4250 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004251 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004252 && !(curbuf->b_flags & BF_NEW))
4253 {
4254 failed = FALSE;
4255 if (curbuf != newbuf)
4256 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004257 /* Bloody autocommands changed the buffer! Can happen when
4258 * using netrw and editing a remote file. Use the current
4259 * buffer instead, delete the dummy one after restoring the
4260 * window stuff. */
4261 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004262 newbuf = curbuf;
4263 }
4264 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004265
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004266 /* restore curwin/curbuf and a few other things */
4267 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004268 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
4269 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004270 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004271
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004272 /*
4273 * When autocommands/'autochdir' option changed directory: go back.
4274 * Let the caller know what the resulting dir was first, in case it is
4275 * important.
4276 */
4277 mch_dirname(resulting_dir, MAXPATHL);
4278 restore_start_dir(dirname_start);
4279
Bram Moolenaar81695252004-12-29 20:58:21 +00004280 if (!buf_valid(newbuf))
4281 return NULL;
4282 if (failed)
4283 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004284 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004285 return NULL;
4286 }
4287 return newbuf;
4288}
4289
4290/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004291 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4292 * directory to "dirname_start" prior to returning, if autocmds or the
4293 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004294 */
4295 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004296wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004297{
4298 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004299 {
4300#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4301 cleanup_T cs;
4302
4303 /* Reset the error/interrupt/exception state here so that aborting()
4304 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4305 * work when got_int is set. */
4306 enter_cleanup(&cs);
4307#endif
4308
Bram Moolenaar81695252004-12-29 20:58:21 +00004309 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004310
4311#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4312 /* Restore the error/interrupt/exception state if not discarded by a
4313 * new aborting error, interrupt, or uncaught exception. */
4314 leave_cleanup(&cs);
4315#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004316 /* When autocommands/'autochdir' option changed directory: go back. */
4317 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004318 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004319}
4320
4321/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004322 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4323 * directory to "dirname_start" prior to returning, if autocmds or the
4324 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004325 */
4326 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004327unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004328{
4329 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004330 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004331 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004332
4333 /* When autocommands/'autochdir' option changed directory: go back. */
4334 restore_start_dir(dirname_start);
4335 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004336}
4337
Bram Moolenaar05159a02005-02-26 23:04:13 +00004338#if defined(FEAT_EVAL) || defined(PROTO)
4339/*
4340 * Add each quickfix error to list "list" as a dictionary.
4341 */
4342 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004343get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004344{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004345 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004346 dict_T *dict;
4347 char_u buf[2];
4348 qfline_T *qfp;
4349 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004350 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004351
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004352 if (wp != NULL)
4353 {
4354 qi = GET_LOC_LIST(wp);
4355 if (qi == NULL)
4356 return FAIL;
4357 }
4358
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004359 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004360 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004361 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004362
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004363 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4364 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004365 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004366 /* Handle entries with a non-existing buffer number. */
4367 bufnum = qfp->qf_fnum;
4368 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4369 bufnum = 0;
4370
Bram Moolenaar05159a02005-02-26 23:04:13 +00004371 if ((dict = dict_alloc()) == NULL)
4372 return FAIL;
4373 if (list_append_dict(list, dict) == FAIL)
4374 return FAIL;
4375
4376 buf[0] = qfp->qf_type;
4377 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004378 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004379 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4380 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4381 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4382 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004383 || dict_add_nr_str(dict, "pattern", 0L,
4384 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4385 || dict_add_nr_str(dict, "text", 0L,
4386 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004387 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4388 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4389 return FAIL;
4390
4391 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004392 if (qfp == NULL)
4393 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004394 }
4395 return OK;
4396}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004397
4398/*
4399 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004400 * of dictionaries. "title" will be copied to w:quickfix_title.
4401 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004402 */
4403 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004404set_errorlist(
4405 win_T *wp,
4406 list_T *list,
4407 int action,
4408 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004409{
4410 listitem_T *li;
4411 dict_T *d;
4412 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004413 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004414 long lnum;
4415 int col, nr;
4416 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004417#ifdef FEAT_WINDOWS
4418 qfline_T *old_last = NULL;
4419#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004420 int valid, status;
4421 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004422 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004423 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004424
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004425 if (wp != NULL)
4426 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004427 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004428 if (qi == NULL)
4429 return FAIL;
4430 }
4431
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004432 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004433 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004434 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004435#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004436 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4437 /* Adding to existing list, use last entry. */
4438 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004439#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004440 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004441 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004442 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004443 qf_store_title(qi, title);
4444 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004445
4446 for (li = list->lv_first; li != NULL; li = li->li_next)
4447 {
4448 if (li->li_tv.v_type != VAR_DICT)
4449 continue; /* Skip non-dict items */
4450
4451 d = li->li_tv.vval.v_dict;
4452 if (d == NULL)
4453 continue;
4454
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004455 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004456 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4457 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4458 col = (int)get_dict_number(d, (char_u *)"col");
4459 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4460 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004461 type = get_dict_string(d, (char_u *)"type", TRUE);
4462 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4463 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004464 if (text == NULL)
4465 text = vim_strsave((char_u *)"");
4466
4467 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004468 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004469 valid = FALSE;
4470
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004471 /* Mark entries with non-existing buffer number as not valid. Give the
4472 * error message only once. */
4473 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4474 {
4475 if (!did_bufnr_emsg)
4476 {
4477 did_bufnr_emsg = TRUE;
4478 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4479 }
4480 valid = FALSE;
4481 bufnum = 0;
4482 }
4483
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004484 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004485 NULL, /* dir */
4486 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004487 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004488 text,
4489 lnum,
4490 col,
4491 vcol, /* vis_col */
4492 pattern, /* search pattern */
4493 nr,
4494 type == NULL ? NUL : *type,
4495 valid);
4496
4497 vim_free(filename);
4498 vim_free(pattern);
4499 vim_free(text);
4500 vim_free(type);
4501
4502 if (status == FAIL)
4503 {
4504 retval = FAIL;
4505 break;
4506 }
4507 }
4508
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004509 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004510 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004511 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4512 else
4513 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004514 if (action != 'a') {
4515 qi->qf_lists[qi->qf_curlist].qf_ptr =
4516 qi->qf_lists[qi->qf_curlist].qf_start;
4517 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4518 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4519 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004520
4521#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004522 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004523 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004524#endif
4525
4526 return retval;
4527}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004528#endif
4529
Bram Moolenaar81695252004-12-29 20:58:21 +00004530/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004531 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004532 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004533 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004534 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004535 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004536 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004537 */
4538 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004539ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004540{
4541 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004542 qf_info_T *qi = &ql_info;
4543
Bram Moolenaardb552d602006-03-23 22:59:57 +00004544 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4545 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004546 {
4547 qi = ll_get_or_alloc_list(curwin);
4548 if (qi == NULL)
4549 return;
4550 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004551
4552 if (*eap->arg == NUL)
4553 buf = curbuf;
4554 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4555 buf = buflist_findnr(atoi((char *)eap->arg));
4556 if (buf == NULL)
4557 EMSG(_(e_invarg));
4558 else if (buf->b_ml.ml_mfp == NULL)
4559 EMSG(_("E681: Buffer is not loaded"));
4560 else
4561 {
4562 if (eap->addr_count == 0)
4563 {
4564 eap->line1 = 1;
4565 eap->line2 = buf->b_ml.ml_line_count;
4566 }
4567 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4568 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4569 EMSG(_(e_invrange));
4570 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004571 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004572 char_u *qf_title = *eap->cmdlinep;
4573
4574 if (buf->b_sfname)
4575 {
4576 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4577 (char *)qf_title, (char *)buf->b_sfname);
4578 qf_title = IObuff;
4579 }
4580
Bram Moolenaardb552d602006-03-23 22:59:57 +00004581 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4582 (eap->cmdidx != CMD_caddbuffer
4583 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004584 eap->line1, eap->line2,
4585 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004586 && (eap->cmdidx == CMD_cbuffer
4587 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004588 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4589 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004590 }
4591}
4592
Bram Moolenaar1e015462005-09-25 22:16:38 +00004593#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004594/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004595 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4596 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004597 */
4598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004599ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004600{
4601 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004602 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004603
Bram Moolenaardb552d602006-03-23 22:59:57 +00004604 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4605 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004606 {
4607 qi = ll_get_or_alloc_list(curwin);
4608 if (qi == NULL)
4609 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004610 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004611
Bram Moolenaar4770d092006-01-12 23:22:24 +00004612 /* Evaluate the expression. When the result is a string or a list we can
4613 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004614 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004615 if (tv != NULL)
4616 {
4617 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4618 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4619 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004620 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004621 (eap->cmdidx != CMD_caddexpr
4622 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004623 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004624 && (eap->cmdidx == CMD_cexpr
4625 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004626 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004627 }
4628 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004629 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004630 free_tv(tv);
4631 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004632}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004633#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004634
4635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 * ":helpgrep {pattern}"
4637 */
4638 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004639ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 regmatch_T regmatch;
4642 char_u *save_cpo;
4643 char_u *p;
4644 int fcount;
4645 char_u **fnames;
4646 FILE *fd;
4647 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004649#ifdef FEAT_MULTI_LANG
4650 char_u *lang;
4651#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004652 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004653 int new_qi = FALSE;
4654 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004655#ifdef FEAT_AUTOCMD
4656 char_u *au_name = NULL;
4657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004659#ifdef FEAT_MULTI_LANG
4660 /* Check for a specified language */
4661 lang = check_help_lang(eap->arg);
4662#endif
4663
Bram Moolenaar73633f82012-01-20 13:39:07 +01004664#ifdef FEAT_AUTOCMD
4665 switch (eap->cmdidx)
4666 {
4667 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4668 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4669 default: break;
4670 }
4671 if (au_name != NULL)
4672 {
4673 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4674 curbuf->b_fname, TRUE, curbuf);
4675 if (did_throw || force_abort)
4676 return;
4677 }
4678#endif
4679
4680 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4681 save_cpo = p_cpo;
4682 p_cpo = empty_option;
4683
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004684 if (eap->cmdidx == CMD_lhelpgrep)
4685 {
4686 /* Find an existing help window */
4687 FOR_ALL_WINDOWS(wp)
4688 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4689 break;
4690
4691 if (wp == NULL) /* Help window not found */
4692 qi = NULL;
4693 else
4694 qi = wp->w_llist;
4695
4696 if (qi == NULL)
4697 {
4698 /* Allocate a new location list for help text matches */
4699 if ((qi = ll_new_list()) == NULL)
4700 return;
4701 new_qi = TRUE;
4702 }
4703 }
4704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4706 regmatch.rm_ic = FALSE;
4707 if (regmatch.regprog != NULL)
4708 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004709#ifdef FEAT_MBYTE
4710 vimconv_T vc;
4711
4712 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4713 * differs. */
4714 vc.vc_type = CONV_NONE;
4715 if (!enc_utf8)
4716 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4717#endif
4718
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004720 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721
4722 /* Go through all directories in 'runtimepath' */
4723 p = p_rtp;
4724 while (*p != NUL && !got_int)
4725 {
4726 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4727
4728 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4729 add_pathsep(NameBuff);
4730 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4731 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4732 &fnames, EW_FILE|EW_SILENT) == OK
4733 && fcount > 0)
4734 {
4735 for (fi = 0; fi < fcount && !got_int; ++fi)
4736 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004737#ifdef FEAT_MULTI_LANG
4738 /* Skip files for a different language. */
4739 if (lang != NULL
4740 && STRNICMP(lang, fnames[fi]
4741 + STRLEN(fnames[fi]) - 3, 2) != 0
4742 && !(STRNICMP(lang, "en", 2) == 0
4743 && STRNICMP("txt", fnames[fi]
4744 + STRLEN(fnames[fi]) - 3, 3) == 0))
4745 continue;
4746#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004747 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 if (fd != NULL)
4749 {
4750 lnum = 1;
4751 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4752 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004753 char_u *line = IObuff;
4754#ifdef FEAT_MBYTE
4755 /* Convert a line if 'encoding' is not utf-8 and
4756 * the line contains a non-ASCII character. */
4757 if (vc.vc_type != CONV_NONE
4758 && has_non_ascii(IObuff)) {
4759 line = string_convert(&vc, IObuff, NULL);
4760 if (line == NULL)
4761 line = IObuff;
4762 }
4763#endif
4764
4765 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004767 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768
4769 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004770 while (l > 0 && line[l - 1] <= ' ')
4771 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004773 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 NULL, /* dir */
4775 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004776 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004777 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004779 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004780 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004781 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004782 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 0, /* nr */
4784 1, /* type */
4785 TRUE /* valid */
4786 ) == FAIL)
4787 {
4788 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004789#ifdef FEAT_MBYTE
4790 if (line != IObuff)
4791 vim_free(line);
4792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 break;
4794 }
4795 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004796#ifdef FEAT_MBYTE
4797 if (line != IObuff)
4798 vim_free(line);
4799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 ++lnum;
4801 line_breakcheck();
4802 }
4803 fclose(fd);
4804 }
4805 }
4806 FreeWild(fcount, fnames);
4807 }
4808 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004809
Bram Moolenaar473de612013-06-08 18:19:48 +02004810 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004811#ifdef FEAT_MBYTE
4812 if (vc.vc_type != CONV_NONE)
4813 convert_setup(&vc, NULL, NULL);
4814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004816 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4817 qi->qf_lists[qi->qf_curlist].qf_ptr =
4818 qi->qf_lists[qi->qf_curlist].qf_start;
4819 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 }
4821
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004822 if (p_cpo == empty_option)
4823 p_cpo = save_cpo;
4824 else
4825 /* Darn, some plugin changed the value. */
4826 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
4828#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004829 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#endif
4831
Bram Moolenaar73633f82012-01-20 13:39:07 +01004832#ifdef FEAT_AUTOCMD
4833 if (au_name != NULL)
4834 {
4835 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4836 curbuf->b_fname, TRUE, curbuf);
4837 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4838 /* autocommands made "qi" invalid */
4839 return;
4840 }
4841#endif
4842
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004844 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004845 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004846 else
4847 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004848
4849 if (eap->cmdidx == CMD_lhelpgrep)
4850 {
4851 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004852 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004853 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4854 {
4855 if (new_qi)
4856 ll_free_all(&qi);
4857 }
4858 else if (curwin->w_llist == NULL)
4859 curwin->w_llist = qi;
4860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861}
4862
4863#endif /* FEAT_QUICKFIX */