blob: fecc2c9eb05f1809b6151e6f6955ebc412469f04 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaard12f5c12006-01-25 22:10:52 +000050typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000051{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000052 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020053 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_ptr; /* pointer to the current error */
55 int qf_count; /* number of errors (0 means no error list) */
56 int qf_index; /* current index in the error list */
57 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020058 char_u *qf_title; /* title derived from the command that created
59 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000060} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaard12f5c12006-01-25 22:10:52 +000062struct qf_info_S
63{
64 /*
65 * Count of references to this list. Used only for location lists.
66 * When a location list window reference this list, qf_refcount
67 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
68 * reaches 0, the list is freed.
69 */
70 int qf_refcount;
71 int qf_listcount; /* current number of lists */
72 int qf_curlist; /* current error list */
73 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar361c8f02016-07-02 15:41:47 +020074
75 int qf_dir_curlist; /* error list for qf_dir_stack */
76 struct dir_stack_T *qf_dir_stack;
77 char_u *qf_directory;
78 struct dir_stack_T *qf_file_stack;
79 char_u *qf_currfile;
80 int qf_multiline;
81 int qf_multiignore;
82 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000083};
84
85static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaar68b76a62005-03-25 21:53:48 +000087#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89/*
90 * Structure used to hold the info of one part of 'errorformat'
91 */
Bram Moolenaar01265852006-03-20 21:50:15 +000092typedef struct efm_S efm_T;
93struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000094{
95 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000096 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
98 char_u prefix; /* prefix of this format line: */
99 /* 'D' enter directory */
100 /* 'X' leave directory */
101 /* 'A' start of multi-line message */
102 /* 'E' error message */
103 /* 'W' warning message */
104 /* 'I' informational message */
105 /* 'C' continuation line */
106 /* 'Z' end of multi-line message */
107 /* 'G' general, unspecific message */
108 /* 'P' push file (partial) message */
109 /* 'Q' pop/quit file (partial) message */
110 /* 'O' overread (partial) message */
111 char_u flags; /* additional flags given in prefix */
112 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000113 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115};
116
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title);
118static void qf_store_title(qf_info_T *qi, char_u *title);
119static void qf_new_list(qf_info_T *qi, char_u *qf_title);
120static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200121static int qf_add_entry(qf_info_T *qi, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100122static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void qf_free(qf_info_T *qi, int idx);
124static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200125static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
126static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200128static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100129static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
130static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
133static int is_qf_win(win_T *win, qf_info_T *qi);
134static win_T *qf_find_win(qf_info_T *qi);
135static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200136static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200138static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static char_u *get_mef_name(void);
141static void restore_start_dir(char_u *dirname_start);
142static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
143static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
144static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
145static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000147/* Quickfix window check helper macro */
148#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
149/* Location list window check helper macro */
150#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
151/*
152 * Return location list for window 'wp'
153 * For location list window, return the referenced location list
154 */
155#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000158 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200159 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 * Return -1 for error, number of errors for success.
161 */
162 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100163qf_init(
164 win_T *wp,
165 char_u *efile,
166 char_u *errorformat,
167 int newlist, /* TRUE: start a new error list */
168 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000170 qf_info_T *qi = &ql_info;
171
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000173 {
174 qi = ll_get_or_alloc_list(wp);
175 if (qi == NULL)
176 return FAIL;
177 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000178
179 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200180 (linenr_T)0, (linenr_T)0,
181 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182}
183
184/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200185 * Maximum number of bytes allowed per line while reading a errorfile.
186 */
187#define LINE_MAXLEN 4096
188
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200189static struct fmtpattern
190{
191 char_u convchar;
192 char *pattern;
193} fmt_pat[FMT_PATTERNS] =
194 {
195 {'f', ".\\+"}, /* only used when at end */
196 {'n', "\\d\\+"},
197 {'l', "\\d\\+"},
198 {'c', "\\d\\+"},
199 {'t', "."},
200 {'m', ".\\+"},
201 {'r', ".*"},
202 {'p', "[- .]*"},
203 {'v', "\\d\\+"},
204 {'s', ".\\+"}
205 };
206
207/*
208 * Converts a 'errorformat' string to regular expression pattern
209 */
210 static int
211efm_to_regpat(
212 char_u *efm,
213 int len,
214 efm_T *fmt_ptr,
215 char_u *regpat,
216 char_u *errmsg)
217{
218 char_u *ptr;
219 char_u *efmp;
220 char_u *srcptr;
221 int round;
222 int idx = 0;
223
224 /*
225 * Build regexp pattern from current 'errorformat' option
226 */
227 ptr = regpat;
228 *ptr++ = '^';
229 round = 0;
230 for (efmp = efm; efmp < efm + len; ++efmp)
231 {
232 if (*efmp == '%')
233 {
234 ++efmp;
235 for (idx = 0; idx < FMT_PATTERNS; ++idx)
236 if (fmt_pat[idx].convchar == *efmp)
237 break;
238 if (idx < FMT_PATTERNS)
239 {
240 if (fmt_ptr->addr[idx])
241 {
242 sprintf((char *)errmsg,
243 _("E372: Too many %%%c in format string"), *efmp);
244 EMSG(errmsg);
245 return -1;
246 }
247 if ((idx
248 && idx < 6
249 && vim_strchr((char_u *)"DXOPQ",
250 fmt_ptr->prefix) != NULL)
251 || (idx == 6
252 && vim_strchr((char_u *)"OPQ",
253 fmt_ptr->prefix) == NULL))
254 {
255 sprintf((char *)errmsg,
256 _("E373: Unexpected %%%c in format string"), *efmp);
257 EMSG(errmsg);
258 return -1;
259 }
260 fmt_ptr->addr[idx] = (char_u)++round;
261 *ptr++ = '\\';
262 *ptr++ = '(';
263#ifdef BACKSLASH_IN_FILENAME
264 if (*efmp == 'f')
265 {
266 /* Also match "c:" in the file name, even when
267 * checking for a colon next: "%f:".
268 * "\%(\a:\)\=" */
269 STRCPY(ptr, "\\%(\\a:\\)\\=");
270 ptr += 10;
271 }
272#endif
273 if (*efmp == 'f' && efmp[1] != NUL)
274 {
275 if (efmp[1] != '\\' && efmp[1] != '%')
276 {
277 /* A file name may contain spaces, but this isn't
278 * in "\f". For "%f:%l:%m" there may be a ":" in
279 * the file name. Use ".\{-1,}x" instead (x is
280 * the next character), the requirement that :999:
281 * follows should work. */
282 STRCPY(ptr, ".\\{-1,}");
283 ptr += 7;
284 }
285 else
286 {
287 /* File name followed by '\\' or '%': include as
288 * many file name chars as possible. */
289 STRCPY(ptr, "\\f\\+");
290 ptr += 4;
291 }
292 }
293 else
294 {
295 srcptr = (char_u *)fmt_pat[idx].pattern;
296 while ((*ptr = *srcptr++) != NUL)
297 ++ptr;
298 }
299 *ptr++ = '\\';
300 *ptr++ = ')';
301 }
302 else if (*efmp == '*')
303 {
304 if (*++efmp == '[' || *efmp == '\\')
305 {
306 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
307 {
308 if (efmp[1] == '^')
309 *ptr++ = *++efmp;
310 if (efmp < efm + len)
311 {
312 *ptr++ = *++efmp; /* could be ']' */
313 while (efmp < efm + len
314 && (*ptr++ = *++efmp) != ']')
315 /* skip */;
316 if (efmp == efm + len)
317 {
318 EMSG(_("E374: Missing ] in format string"));
319 return -1;
320 }
321 }
322 }
323 else if (efmp < efm + len) /* %*\D, %*\s etc. */
324 *ptr++ = *++efmp;
325 *ptr++ = '\\';
326 *ptr++ = '+';
327 }
328 else
329 {
330 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
331 sprintf((char *)errmsg,
332 _("E375: Unsupported %%%c in format string"), *efmp);
333 EMSG(errmsg);
334 return -1;
335 }
336 }
337 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
338 *ptr++ = *efmp; /* regexp magic characters */
339 else if (*efmp == '#')
340 *ptr++ = '*';
341 else if (*efmp == '>')
342 fmt_ptr->conthere = TRUE;
343 else if (efmp == efm + 1) /* analyse prefix */
344 {
345 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
346 fmt_ptr->flags = *efmp++;
347 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
348 fmt_ptr->prefix = *efmp;
349 else
350 {
351 sprintf((char *)errmsg,
352 _("E376: Invalid %%%c in format string prefix"), *efmp);
353 EMSG(errmsg);
354 return -1;
355 }
356 }
357 else
358 {
359 sprintf((char *)errmsg,
360 _("E377: Invalid %%%c in format string"), *efmp);
361 EMSG(errmsg);
362 return -1;
363 }
364 }
365 else /* copy normal character */
366 {
367 if (*efmp == '\\' && efmp + 1 < efm + len)
368 ++efmp;
369 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
370 *ptr++ = '\\'; /* escape regexp atoms */
371 if (*efmp)
372 *ptr++ = *efmp;
373 }
374 }
375 *ptr++ = '$';
376 *ptr = NUL;
377
378 return 0;
379}
380
381 static void
382free_efm_list(efm_T **efm_first)
383{
384 efm_T *efm_ptr;
385
386 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
387 {
388 *efm_first = efm_ptr->next;
389 vim_regfree(efm_ptr->prog);
390 vim_free(efm_ptr);
391 }
392}
393
394/* Parse 'errorformat' option */
395 static efm_T *
396parse_efm_option(char_u *efm)
397{
398 char_u *errmsg = NULL;
399 int errmsglen;
400 efm_T *fmt_ptr = NULL;
401 efm_T *fmt_first = NULL;
402 efm_T *fmt_last = NULL;
403 char_u *fmtstr = NULL;
404 int len;
405 int i;
406 int round;
407
408 errmsglen = CMDBUFFSIZE + 1;
409 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
410 if (errmsg == NULL)
411 goto parse_efm_end;
412
413 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200414 * Each part of the format string is copied and modified from errorformat
415 * to regex prog. Only a few % characters are allowed.
416 */
417
418 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419 * Get some space to modify the format string into.
420 */
421 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
422 for (round = FMT_PATTERNS; round > 0; )
423 i += (int)STRLEN(fmt_pat[--round].pattern);
424#ifdef COLON_IN_FILENAME
425 i += 12; /* "%f" can become twelve chars longer */
426#else
427 i += 2; /* "%f" can become two chars longer */
428#endif
429 if ((fmtstr = alloc(i)) == NULL)
430 goto parse_efm_error;
431
432 while (efm[0] != NUL)
433 {
434 /*
435 * Allocate a new eformat structure and put it at the end of the list
436 */
437 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
438 if (fmt_ptr == NULL)
439 goto parse_efm_error;
440 if (fmt_first == NULL) /* first one */
441 fmt_first = fmt_ptr;
442 else
443 fmt_last->next = fmt_ptr;
444 fmt_last = fmt_ptr;
445
446 /*
447 * Isolate one part in the 'errorformat' option
448 */
449 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
450 if (efm[len] == '\\' && efm[len + 1] != NUL)
451 ++len;
452
453 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
454 goto parse_efm_error;
455 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
456 goto parse_efm_error;
457 /*
458 * Advance to next part
459 */
460 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
461 }
462
463 if (fmt_first == NULL) /* nothing found */
464 EMSG(_("E378: 'errorformat' contains no pattern"));
465
466 goto parse_efm_end;
467
468parse_efm_error:
469 free_efm_list(&fmt_first);
470
471parse_efm_end:
472 vim_free(fmtstr);
473 vim_free(errmsg);
474
475 return fmt_first;
476}
477
Bram Moolenaare0d37972016-07-15 22:36:01 +0200478enum {
479 QF_FAIL = 0,
480 QF_OK = 1,
481 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200482 QF_NOMEM = 3,
483 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200484};
485
486typedef struct {
487 char_u *linebuf;
488 int linelen;
489 char_u *growbuf;
490 int growbufsiz;
491 FILE *fd;
492 typval_T *tv;
493 char_u *p_str;
494 listitem_T *p_li;
495 buf_T *buf;
496 linenr_T buflnum;
497 linenr_T lnumlast;
498} qfstate_T;
499
500 static char_u *
501qf_grow_linebuf(qfstate_T *state, int newsz)
502{
503 /*
504 * If the line exceeds LINE_MAXLEN exclude the last
505 * byte since it's not a NL character.
506 */
507 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
508 if (state->growbuf == NULL)
509 {
510 state->growbuf = alloc(state->linelen + 1);
511 if (state->growbuf == NULL)
512 return NULL;
513 state->growbufsiz = state->linelen;
514 }
515 else if (state->linelen > state->growbufsiz)
516 {
517 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
518 if (state->growbuf == NULL)
519 return NULL;
520 state->growbufsiz = state->linelen;
521 }
522 return state->growbuf;
523}
524
525/*
526 * Get the next string (separated by newline) from state->p_str.
527 */
528 static int
529qf_get_next_str_line(qfstate_T *state)
530{
531 /* Get the next line from the supplied string */
532 char_u *p_str = state->p_str;
533 char_u *p;
534 int len;
535
536 if (*p_str == NUL) /* Reached the end of the string */
537 return QF_END_OF_INPUT;
538
539 p = vim_strchr(p_str, '\n');
540 if (p != NULL)
541 len = (int)(p - p_str) + 1;
542 else
543 len = (int)STRLEN(p_str);
544
545 if (len > IOSIZE - 2)
546 {
547 state->linebuf = qf_grow_linebuf(state, len);
548 if (state->linebuf == NULL)
549 return QF_NOMEM;
550 }
551 else
552 {
553 state->linebuf = IObuff;
554 state->linelen = len;
555 }
556 vim_strncpy(state->linebuf, p_str, state->linelen);
557
558 /*
559 * Increment using len in order to discard the rest of the
560 * line if it exceeds LINE_MAXLEN.
561 */
562 p_str += len;
563 state->p_str = p_str;
564
565 return QF_OK;
566}
567
568/*
569 * Get the next string from state->p_Li.
570 */
571 static int
572qf_get_next_list_line(qfstate_T *state)
573{
574 listitem_T *p_li = state->p_li;
575 int len;
576
577 while (p_li != NULL
578 && (p_li->li_tv.v_type != VAR_STRING
579 || p_li->li_tv.vval.v_string == NULL))
580 p_li = p_li->li_next; /* Skip non-string items */
581
582 if (p_li == NULL) /* End of the list */
583 {
584 state->p_li = NULL;
585 return QF_END_OF_INPUT;
586 }
587
588 len = (int)STRLEN(p_li->li_tv.vval.v_string);
589 if (len > IOSIZE - 2)
590 {
591 state->linebuf = qf_grow_linebuf(state, len);
592 if (state->linebuf == NULL)
593 return QF_NOMEM;
594 }
595 else
596 {
597 state->linebuf = IObuff;
598 state->linelen = len;
599 }
600
601 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
602
603 state->p_li = p_li->li_next; /* next item */
604 return QF_OK;
605}
606
607/*
608 * Get the next string from state->buf.
609 */
610 static int
611qf_get_next_buf_line(qfstate_T *state)
612{
613 char_u *p_buf = NULL;
614 int len;
615
616 /* Get the next line from the supplied buffer */
617 if (state->buflnum > state->lnumlast)
618 return QF_END_OF_INPUT;
619
620 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
621 state->buflnum += 1;
622
623 len = (int)STRLEN(p_buf);
624 if (len > IOSIZE - 2)
625 {
626 state->linebuf = qf_grow_linebuf(state, len);
627 if (state->linebuf == NULL)
628 return QF_NOMEM;
629 }
630 else
631 {
632 state->linebuf = IObuff;
633 state->linelen = len;
634 }
635 vim_strncpy(state->linebuf, p_buf, state->linelen);
636
637 return QF_OK;
638}
639
640/*
641 * Get the next string from file state->fd.
642 */
643 static int
644qf_get_next_file_line(qfstate_T *state)
645{
646 int discard;
647 int growbuflen;
648
649 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
650 return QF_END_OF_INPUT;
651
652 discard = FALSE;
653 state->linelen = (int)STRLEN(IObuff);
654 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'
655#ifdef USE_CRNL
656 || IObuff[state->linelen - 1] == '\r'
657#endif
658 ))
659 {
660 /*
661 * The current line exceeds IObuff, continue reading using
662 * growbuf until EOL or LINE_MAXLEN bytes is read.
663 */
664 if (state->growbuf == NULL)
665 {
666 state->growbufsiz = 2 * (IOSIZE - 1);
667 state->growbuf = alloc(state->growbufsiz);
668 if (state->growbuf == NULL)
669 return QF_NOMEM;
670 }
671
672 /* Copy the read part of the line, excluding null-terminator */
673 memcpy(state->growbuf, IObuff, IOSIZE - 1);
674 growbuflen = state->linelen;
675
676 for (;;)
677 {
678 if (fgets((char *)state->growbuf + growbuflen,
679 state->growbufsiz - growbuflen, state->fd) == NULL)
680 break;
681 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
682 growbuflen += state->linelen;
683 if ((state->growbuf)[growbuflen - 1] == '\n'
684#ifdef USE_CRNL
685 || (state->growbuf)[growbuflen - 1] == '\r'
686#endif
687 )
688 break;
689 if (state->growbufsiz == LINE_MAXLEN)
690 {
691 discard = TRUE;
692 break;
693 }
694
695 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
696 ? 2 * state->growbufsiz : LINE_MAXLEN;
697 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
698 if (state->growbuf == NULL)
699 return QF_NOMEM;
700 }
701
702 while (discard)
703 {
704 /*
705 * The current line is longer than LINE_MAXLEN, continue
706 * reading but discard everything until EOL or EOF is
707 * reached.
708 */
709 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
710 || (int)STRLEN(IObuff) < IOSIZE - 1
711 || IObuff[IOSIZE - 1] == '\n'
712#ifdef USE_CRNL
713 || IObuff[IOSIZE - 1] == '\r'
714#endif
715 )
716 break;
717 }
718
719 state->linebuf = state->growbuf;
720 state->linelen = growbuflen;
721 }
722 else
723 state->linebuf = IObuff;
724
725 return QF_OK;
726}
727
728/*
729 * Get the next string from a file/buffer/list/string.
730 */
731 static int
732qf_get_nextline(qfstate_T *state)
733{
734 int status = QF_FAIL;
735
736 if (state->fd == NULL)
737 {
738 if (state->tv != NULL)
739 {
740 if (state->tv->v_type == VAR_STRING)
741 /* Get the next line from the supplied string */
742 status = qf_get_next_str_line(state);
743 else if (state->tv->v_type == VAR_LIST)
744 /* Get the next line from the supplied list */
745 status = qf_get_next_list_line(state);
746 }
747 else
748 /* Get the next line from the supplied buffer */
749 status = qf_get_next_buf_line(state);
750 }
751 else
752 /* Get the next line from the supplied file */
753 status = qf_get_next_file_line(state);
754
755 if (status != QF_OK)
756 return status;
757
758 /* remove newline/CR from the line */
759 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
760 state->linebuf[state->linelen - 1] = NUL;
761#ifdef USE_CRNL
762 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\r')
763 state->linebuf[state->linelen - 1] = NUL;
764#endif
765
766#ifdef FEAT_MBYTE
767 remove_bom(state->linebuf);
768#endif
769
770 return QF_OK;
771}
772
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200773typedef struct {
774 char_u *namebuf;
775 char_u *errmsg;
776 int errmsglen;
777 long lnum;
778 int col;
779 char_u use_viscol;
780 char_u *pattern;
781 int enr;
782 int type;
783 int valid;
784} qffields_T;
785
786/*
787 * Parse a line and get the quickfix fields.
788 * Return the QF_ status.
789 */
790 static int
791qf_parse_line(
792 qf_info_T *qi,
793 char_u *linebuf,
794 int linelen,
795 efm_T *fmt_first,
796 qffields_T *fields)
797{
798 efm_T *fmt_ptr;
799 static efm_T *fmt_start = NULL; /* cached across calls */
800 char_u *ptr;
801 int len;
802 int i;
803 int idx = 0;
804 char_u *tail = NULL;
805 regmatch_T regmatch;
806
807 /* Always ignore case when looking for a matching error. */
808 regmatch.rm_ic = TRUE;
809
810 /* If there was no %> item start at the first pattern */
811 if (fmt_start == NULL)
812 fmt_ptr = fmt_first;
813 else
814 {
815 fmt_ptr = fmt_start;
816 fmt_start = NULL;
817 }
818
819 /*
820 * Try to match each part of 'errorformat' until we find a complete
821 * match or no match.
822 */
823 fields->valid = TRUE;
824restofline:
825 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
826 {
827 int r;
828
829 idx = fmt_ptr->prefix;
830 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
831 continue;
832 fields->namebuf[0] = NUL;
833 fields->pattern[0] = NUL;
834 if (!qi->qf_multiscan)
835 fields->errmsg[0] = NUL;
836 fields->lnum = 0;
837 fields->col = 0;
838 fields->use_viscol = FALSE;
839 fields->enr = -1;
840 fields->type = 0;
841 tail = NULL;
842
843 regmatch.regprog = fmt_ptr->prog;
844 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
845 fmt_ptr->prog = regmatch.regprog;
846 if (r)
847 {
848 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
849 continue;
850 if (vim_strchr((char_u *)"EWI", idx) != NULL)
851 fields->type = idx;
852 else
853 fields->type = 0;
854 /*
855 * Extract error message data from matched line.
856 * We check for an actual submatch, because "\[" and "\]" in
857 * the 'errorformat' may cause the wrong submatch to be used.
858 */
859 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
860 {
861 int c;
862
863 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
864 continue;
865
866 /* Expand ~/file and $HOME/file to full path. */
867 c = *regmatch.endp[i];
868 *regmatch.endp[i] = NUL;
869 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
870 *regmatch.endp[i] = c;
871
872 if (vim_strchr((char_u *)"OPQ", idx) != NULL
873 && mch_getperm(fields->namebuf) == -1)
874 continue;
875 }
876 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
877 {
878 if (regmatch.startp[i] == NULL)
879 continue;
880 fields->enr = (int)atol((char *)regmatch.startp[i]);
881 }
882 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
883 {
884 if (regmatch.startp[i] == NULL)
885 continue;
886 fields->lnum = atol((char *)regmatch.startp[i]);
887 }
888 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
889 {
890 if (regmatch.startp[i] == NULL)
891 continue;
892 fields->col = (int)atol((char *)regmatch.startp[i]);
893 }
894 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
895 {
896 if (regmatch.startp[i] == NULL)
897 continue;
898 fields->type = *regmatch.startp[i];
899 }
900 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
901 {
902 if (linelen > fields->errmsglen) {
903 /* linelen + null terminator */
904 if ((fields->errmsg = vim_realloc(fields->errmsg,
905 linelen + 1)) == NULL)
906 return QF_NOMEM;
907 fields->errmsglen = linelen + 1;
908 }
909 vim_strncpy(fields->errmsg, linebuf, linelen);
910 }
911 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
912 {
913 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
914 continue;
915 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
916 if (len > fields->errmsglen) {
917 /* len + null terminator */
918 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
919 == NULL)
920 return QF_NOMEM;
921 fields->errmsglen = len + 1;
922 }
923 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
924 }
925 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
926 {
927 if (regmatch.startp[i] == NULL)
928 continue;
929 tail = regmatch.startp[i];
930 }
931 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
932 {
933 char_u *match_ptr;
934
935 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
936 continue;
937 fields->col = 0;
938 for (match_ptr = regmatch.startp[i];
939 match_ptr != regmatch.endp[i]; ++match_ptr)
940 {
941 ++fields->col;
942 if (*match_ptr == TAB)
943 {
944 fields->col += 7;
945 fields->col -= fields->col % 8;
946 }
947 }
948 ++fields->col;
949 fields->use_viscol = TRUE;
950 }
951 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
952 {
953 if (regmatch.startp[i] == NULL)
954 continue;
955 fields->col = (int)atol((char *)regmatch.startp[i]);
956 fields->use_viscol = TRUE;
957 }
958 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
959 {
960 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
961 continue;
962 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
963 if (len > CMDBUFFSIZE - 5)
964 len = CMDBUFFSIZE - 5;
965 STRCPY(fields->pattern, "^\\V");
966 STRNCAT(fields->pattern, regmatch.startp[i], len);
967 fields->pattern[len + 3] = '\\';
968 fields->pattern[len + 4] = '$';
969 fields->pattern[len + 5] = NUL;
970 }
971 break;
972 }
973 }
974 qi->qf_multiscan = FALSE;
975
976 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
977 {
978 if (fmt_ptr != NULL)
979 {
980 if (idx == 'D') /* enter directory */
981 {
982 if (*fields->namebuf == NUL)
983 {
984 EMSG(_("E379: Missing or empty directory name"));
985 return QF_FAIL;
986 }
987 qi->qf_directory =
988 qf_push_dir(fields->namebuf, &qi->qf_dir_stack, FALSE);
989 if (qi->qf_directory == NULL)
990 return QF_FAIL;
991 }
992 else if (idx == 'X') /* leave directory */
993 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
994 }
995 fields->namebuf[0] = NUL; /* no match found, remove file name */
996 fields->lnum = 0; /* don't jump to this line */
997 fields->valid = FALSE;
998 if (linelen > fields->errmsglen) {
999 /* linelen + null terminator */
1000 if ((fields->errmsg = vim_realloc(fields->errmsg,
1001 linelen + 1)) == NULL)
1002 return QF_NOMEM;
1003 fields->errmsglen = linelen + 1;
1004 }
1005 /* copy whole line to error message */
1006 vim_strncpy(fields->errmsg, linebuf, linelen);
1007 if (fmt_ptr == NULL)
1008 qi->qf_multiline = qi->qf_multiignore = FALSE;
1009 }
1010 else if (fmt_ptr != NULL)
1011 {
1012 /* honor %> item */
1013 if (fmt_ptr->conthere)
1014 fmt_start = fmt_ptr;
1015
1016 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1017 {
1018 qi->qf_multiline = TRUE; /* start of a multi-line message */
1019 qi->qf_multiignore = FALSE; /* reset continuation */
1020 }
1021 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1022 { /* continuation of multi-line msg */
1023 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
1024
1025 if (qfprev == NULL)
1026 return QF_FAIL;
1027 if (*fields->errmsg && !qi->qf_multiignore)
1028 {
1029 len = (int)STRLEN(qfprev->qf_text);
1030 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1031 == NULL)
1032 return QF_FAIL;
1033 STRCPY(ptr, qfprev->qf_text);
1034 vim_free(qfprev->qf_text);
1035 qfprev->qf_text = ptr;
1036 *(ptr += len) = '\n';
1037 STRCPY(++ptr, fields->errmsg);
1038 }
1039 if (qfprev->qf_nr == -1)
1040 qfprev->qf_nr = fields->enr;
1041 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1042 /* only printable chars allowed */
1043 qfprev->qf_type = fields->type;
1044
1045 if (!qfprev->qf_lnum)
1046 qfprev->qf_lnum = fields->lnum;
1047 if (!qfprev->qf_col)
1048 qfprev->qf_col = fields->col;
1049 qfprev->qf_viscol = fields->use_viscol;
1050 if (!qfprev->qf_fnum)
1051 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1052 *fields->namebuf || qi->qf_directory != NULL
1053 ? fields->namebuf
1054 : qi->qf_currfile != NULL && fields->valid
1055 ? qi->qf_currfile : 0);
1056 if (idx == 'Z')
1057 qi->qf_multiline = qi->qf_multiignore = FALSE;
1058 line_breakcheck();
1059 return QF_IGNORE_LINE;
1060 }
1061 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1062 {
1063 /* global file names */
1064 fields->valid = FALSE;
1065 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1066 {
1067 if (*fields->namebuf && idx == 'P')
1068 qi->qf_currfile =
1069 qf_push_dir(fields->namebuf, &qi->qf_file_stack, TRUE);
1070 else if (idx == 'Q')
1071 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
1072 *fields->namebuf = NUL;
1073 if (tail && *tail)
1074 {
1075 STRMOVE(IObuff, skipwhite(tail));
1076 qi->qf_multiscan = TRUE;
1077 goto restofline;
1078 }
1079 }
1080 }
1081 if (fmt_ptr->flags == '-') /* generally exclude this line */
1082 {
1083 if (qi->qf_multiline)
1084 /* also exclude continuation lines */
1085 qi->qf_multiignore = TRUE;
1086 return QF_IGNORE_LINE;
1087 }
1088 }
1089
1090 return QF_OK;
1091}
1092
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001093/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001094 * Read the errorfile "efile" into memory, line by line, building the error
1095 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001096 * Alternative: when "efile" is NULL read errors from buffer "buf".
1097 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001098 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001099 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1100 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001101 * Return -1 for error, number of errors for success.
1102 */
1103 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001104qf_init_ext(
1105 qf_info_T *qi,
1106 char_u *efile,
1107 buf_T *buf,
1108 typval_T *tv,
1109 char_u *errorformat,
1110 int newlist, /* TRUE: start a new error list */
1111 linenr_T lnumfirst, /* first line number to use */
1112 linenr_T lnumlast, /* last line number to use */
1113 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001114{
Bram Moolenaare0d37972016-07-15 22:36:01 +02001115 qfstate_T state = {NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001116 NULL, 0, 0};
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001117 qffields_T fields = {NULL, NULL, 0, 0L, 0, FALSE, NULL, 0, 0, 0};
Bram Moolenaar864293a2016-06-02 13:40:04 +02001118#ifdef FEAT_WINDOWS
1119 qfline_T *old_last = NULL;
1120#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001121 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001123 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001125 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001127 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1128 fields.errmsglen = CMDBUFFSIZE + 1;
1129 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1130 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1131 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1132 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 goto qf_init_end;
1134
Bram Moolenaare0d37972016-07-15 22:36:01 +02001135 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {
1137 EMSG2(_(e_openerrf), efile);
1138 goto qf_init_end;
1139 }
1140
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001141 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001143 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001144#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001145 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02001146 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001147 /* Adding to existing list, use last entry. */
1148 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001149 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001153 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001154 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 else
1156 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001158 /*
1159 * If we are not adding or adding to another list: clear the state.
1160 */
1161 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
1162 {
1163 qi->qf_dir_curlist = qi->qf_curlist;
1164 qf_clean_dir_stack(&qi->qf_dir_stack);
1165 qi->qf_directory = NULL;
1166 qf_clean_dir_stack(&qi->qf_file_stack);
1167 qi->qf_currfile = NULL;
1168 qi->qf_multiline = FALSE;
1169 qi->qf_multiignore = FALSE;
1170 qi->qf_multiscan = FALSE;
1171 }
1172
1173 /*
1174 * If the errorformat didn't change between calls, then reuse the
1175 * previously parsed values.
1176 */
1177 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1178 {
1179 /* free the previously parsed data */
1180 vim_free(last_efm);
1181 last_efm = NULL;
1182 free_efm_list(&fmt_first);
1183
1184 /* parse the current 'efm' */
1185 fmt_first = parse_efm_option(efm);
1186 if (fmt_first != NULL)
1187 last_efm = vim_strsave(efm);
1188 }
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
1193 /*
1194 * got_int is reset here, because it was probably set when killing the
1195 * ":make" command, but we still want to read the errorfile then.
1196 */
1197 got_int = FALSE;
1198
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001199 if (tv != NULL)
1200 {
1201 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001202 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001203 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001204 state.p_li = tv->vval.v_list->lv_first;
1205 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001206 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001207 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001208 state.buflnum = lnumfirst;
1209 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 /*
1212 * Read the lines in the error file one by one.
1213 * Try to recognize one of the error formats in each line.
1214 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001215 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001217 /* Get the next line from a file/buffer/list/string */
1218 status = qf_get_nextline(&state);
1219 if (status == QF_NOMEM) /* memory alloc failure */
1220 goto qf_init_end;
1221 if (status == QF_END_OF_INPUT) /* end of input */
1222 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001224 status = qf_parse_line(qi, state.linebuf, state.linelen, fmt_first,
1225 &fields);
1226 if (status == QF_FAIL)
1227 goto error2;
1228 if (status == QF_NOMEM)
1229 goto qf_init_end;
1230 if (status == QF_IGNORE_LINE)
1231 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001233 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001234 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001235 (*fields.namebuf || qi->qf_directory != NULL)
1236 ? fields.namebuf
1237 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001238 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001239 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001240 fields.errmsg,
1241 fields.lnum,
1242 fields.col,
1243 fields.use_viscol,
1244 fields.pattern,
1245 fields.enr,
1246 fields.type,
1247 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 goto error2;
1249 line_breakcheck();
1250 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001251 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001253 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001255 /* no valid entry found */
1256 qi->qf_lists[qi->qf_curlist].qf_ptr =
1257 qi->qf_lists[qi->qf_curlist].qf_start;
1258 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1259 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 else
1262 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001263 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1264 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1265 qi->qf_lists[qi->qf_curlist].qf_ptr =
1266 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001268 /* return number of matches */
1269 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001270 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 EMSG(_(e_readerrf));
1273error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001274 qf_free(qi, qi->qf_curlist);
1275 qi->qf_listcount--;
1276 if (qi->qf_curlist > 0)
1277 --qi->qf_curlist;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001278qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001279 if (state.fd != NULL)
1280 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001281 vim_free(fields.namebuf);
1282 vim_free(fields.errmsg);
1283 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001284 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
1286#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001287 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
1289
1290 return retval;
1291}
1292
Bram Moolenaarfb604092014-07-23 15:55:00 +02001293 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001294qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001295{
1296 if (title != NULL)
1297 {
1298 char_u *p = alloc((int)STRLEN(title) + 2);
1299
1300 qi->qf_lists[qi->qf_curlist].qf_title = p;
1301 if (p != NULL)
1302 sprintf((char *)p, ":%s", (char *)title);
1303 }
1304}
1305
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306/*
1307 * Prepare for adding a new quickfix list.
1308 */
1309 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001310qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 int i;
1313
1314 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001315 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 * the current entry. This makes it possible to browse in a tree-like
1317 * way with ":grep'.
1318 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001319 while (qi->qf_listcount > qi->qf_curlist + 1)
1320 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 /*
1323 * When the stack is full, remove to oldest entry
1324 * Otherwise, add a new entry.
1325 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001326 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001328 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001330 qi->qf_lists[i - 1] = qi->qf_lists[i];
1331 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001334 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001335 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001336 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337}
1338
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001339/*
1340 * Free a location list
1341 */
1342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001343ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001344{
1345 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001346 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001347
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001348 qi = *pqi;
1349 if (qi == NULL)
1350 return;
1351 *pqi = NULL; /* Remove reference to this list */
1352
1353 qi->qf_refcount--;
1354 if (qi->qf_refcount < 1)
1355 {
1356 /* No references to this location list */
1357 for (i = 0; i < qi->qf_listcount; ++i)
1358 qf_free(qi, i);
1359 vim_free(qi);
1360 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001361}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001362
1363 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001364qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001365{
1366 int i;
1367 qf_info_T *qi = &ql_info;
1368
1369 if (wp != NULL)
1370 {
1371 /* location list */
1372 ll_free_all(&wp->w_llist);
1373 ll_free_all(&wp->w_llist_ref);
1374 }
1375 else
1376 /* quickfix list */
1377 for (i = 0; i < qi->qf_listcount; ++i)
1378 qf_free(qi, i);
1379}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381/*
1382 * Add an entry to the end of the list of errors.
1383 * Returns OK or FAIL.
1384 */
1385 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001386qf_add_entry(
1387 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001388 char_u *dir, /* optional directory name */
1389 char_u *fname, /* file name or NULL */
1390 int bufnum, /* buffer number or zero */
1391 char_u *mesg, /* message */
1392 long lnum, /* line number */
1393 int col, /* column */
1394 int vis_col, /* using visual column */
1395 char_u *pattern, /* search pattern */
1396 int nr, /* error number */
1397 int type, /* type character */
1398 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001400 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001401 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001403 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001405 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001406 {
1407 buf_T *buf = buflist_findnr(bufnum);
1408
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001409 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001410 if (buf != NULL)
1411 buf->b_has_qf_entry = TRUE;
1412 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001413 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001414 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1416 {
1417 vim_free(qfp);
1418 return FAIL;
1419 }
1420 qfp->qf_lnum = lnum;
1421 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001422 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001423 if (pattern == NULL || *pattern == NUL)
1424 qfp->qf_pattern = NULL;
1425 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1426 {
1427 vim_free(qfp->qf_text);
1428 vim_free(qfp);
1429 return FAIL;
1430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 qfp->qf_nr = nr;
1432 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1433 type = 0;
1434 qfp->qf_type = type;
1435 qfp->qf_valid = valid;
1436
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001437 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001438 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1439 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001441 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001442 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1443 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001444 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 else
1447 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001448 qfp->qf_prev = *lastp;
1449 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001451 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001453 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001454 ++qi->qf_lists[qi->qf_curlist].qf_count;
1455 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1456 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001458 qi->qf_lists[qi->qf_curlist].qf_index =
1459 qi->qf_lists[qi->qf_curlist].qf_count;
1460 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462
1463 return OK;
1464}
1465
1466/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001467 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001468 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001469 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001470ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001471{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001472 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001473
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001474 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1475 if (qi != NULL)
1476 {
1477 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1478 qi->qf_refcount++;
1479 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001480
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001481 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001482}
1483
1484/*
1485 * Return the location list for window 'wp'.
1486 * If not present, allocate a location list
1487 */
1488 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001489ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001490{
1491 if (IS_LL_WINDOW(wp))
1492 /* For a location list window, use the referenced location list */
1493 return wp->w_llist_ref;
1494
1495 /*
1496 * For a non-location list window, w_llist_ref should not point to a
1497 * location list.
1498 */
1499 ll_free_all(&wp->w_llist_ref);
1500
1501 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001502 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001503 return wp->w_llist;
1504}
1505
1506/*
1507 * Copy the location list from window "from" to window "to".
1508 */
1509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001510copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001511{
1512 qf_info_T *qi;
1513 int idx;
1514 int i;
1515
1516 /*
1517 * When copying from a location list window, copy the referenced
1518 * location list. For other windows, copy the location list for
1519 * that window.
1520 */
1521 if (IS_LL_WINDOW(from))
1522 qi = from->w_llist_ref;
1523 else
1524 qi = from->w_llist;
1525
1526 if (qi == NULL) /* no location list to copy */
1527 return;
1528
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001529 /* allocate a new location list */
1530 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001531 return;
1532
1533 to->w_llist->qf_listcount = qi->qf_listcount;
1534
1535 /* Copy the location lists one at a time */
1536 for (idx = 0; idx < qi->qf_listcount; idx++)
1537 {
1538 qf_list_T *from_qfl;
1539 qf_list_T *to_qfl;
1540
1541 to->w_llist->qf_curlist = idx;
1542
1543 from_qfl = &qi->qf_lists[idx];
1544 to_qfl = &to->w_llist->qf_lists[idx];
1545
1546 /* Some of the fields are populated by qf_add_entry() */
1547 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1548 to_qfl->qf_count = 0;
1549 to_qfl->qf_index = 0;
1550 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001551 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001552 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001553 if (from_qfl->qf_title != NULL)
1554 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1555 else
1556 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001557
1558 if (from_qfl->qf_count)
1559 {
1560 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001561 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001562
1563 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001564 for (i = 0, from_qfp = from_qfl->qf_start;
1565 i < from_qfl->qf_count && from_qfp != NULL;
1566 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001567 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001568 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001569 NULL,
1570 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001571 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001572 from_qfp->qf_text,
1573 from_qfp->qf_lnum,
1574 from_qfp->qf_col,
1575 from_qfp->qf_viscol,
1576 from_qfp->qf_pattern,
1577 from_qfp->qf_nr,
1578 0,
1579 from_qfp->qf_valid) == FAIL)
1580 {
1581 qf_free_all(to);
1582 return;
1583 }
1584 /*
1585 * qf_add_entry() will not set the qf_num field, as the
1586 * directory and file names are not supplied. So the qf_fnum
1587 * field is copied here.
1588 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001589 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001590 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1591 prevp->qf_type = from_qfp->qf_type; /* error type */
1592 if (from_qfl->qf_ptr == from_qfp)
1593 to_qfl->qf_ptr = prevp; /* current location */
1594 }
1595 }
1596
1597 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1598
1599 /* When no valid entries are present in the list, qf_ptr points to
1600 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001601 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001602 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001603 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001604 to_qfl->qf_index = 1;
1605 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001606 }
1607
1608 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1609}
1610
1611/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001612 * Looking up a buffer can be slow if there are many. Remember the last one
1613 * to make this a lot faster if there are multiple matches in the same file.
1614 */
1615static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001616static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001617
1618/*
Bram Moolenaar015102e2016-07-16 18:24:56 +02001619 * Get buffer number for file "directory.fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001620 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
1622 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001623qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
Bram Moolenaar82404332016-07-10 17:00:38 +02001625 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001626 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001627 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (fname == NULL || *fname == NUL) /* no file name */
1630 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaare60acc12011-05-10 16:41:25 +02001632#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001633 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001634#endif
1635#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001636 if (directory != NULL)
1637 slash_adjust(directory);
1638 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001639#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001640 if (directory != NULL && !vim_isAbsName(fname)
1641 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1642 {
1643 /*
1644 * Here we check if the file really exists.
1645 * This should normally be true, but if make works without
1646 * "leaving directory"-messages we might have missed a
1647 * directory change.
1648 */
1649 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001652 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001653 if (directory)
1654 ptr = concat_fnames(directory, fname, TRUE);
1655 else
1656 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001658 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001659 bufname = ptr;
1660 }
1661 else
1662 bufname = fname;
1663
1664 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001665 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001666 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001667 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001668 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001670 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001671 {
1672 vim_free(qf_last_bufname);
1673 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1674 if (bufname == ptr)
1675 qf_last_bufname = bufname;
1676 else
1677 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001678 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001679 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001680 if (buf == NULL)
1681 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001682
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001683 buf->b_has_qf_entry = TRUE;
1684 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685}
1686
1687/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001688 * Push dirbuf onto the directory stack and return pointer to actual dir or
1689 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 */
1691 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001692qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 struct dir_stack_T *ds_new;
1695 struct dir_stack_T *ds_ptr;
1696
1697 /* allocate new stack element and hook it in */
1698 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1699 if (ds_new == NULL)
1700 return NULL;
1701
1702 ds_new->next = *stackptr;
1703 *stackptr = ds_new;
1704
1705 /* store directory on the stack */
1706 if (vim_isAbsName(dirbuf)
1707 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001708 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 (*stackptr)->dirname = vim_strsave(dirbuf);
1710 else
1711 {
1712 /* Okay we don't have an absolute path.
1713 * dirbuf must be a subdir of one of the directories on the stack.
1714 * Let's search...
1715 */
1716 ds_new = (*stackptr)->next;
1717 (*stackptr)->dirname = NULL;
1718 while (ds_new)
1719 {
1720 vim_free((*stackptr)->dirname);
1721 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1722 TRUE);
1723 if (mch_isdir((*stackptr)->dirname) == TRUE)
1724 break;
1725
1726 ds_new = ds_new->next;
1727 }
1728
1729 /* clean up all dirs we already left */
1730 while ((*stackptr)->next != ds_new)
1731 {
1732 ds_ptr = (*stackptr)->next;
1733 (*stackptr)->next = (*stackptr)->next->next;
1734 vim_free(ds_ptr->dirname);
1735 vim_free(ds_ptr);
1736 }
1737
1738 /* Nothing found -> it must be on top level */
1739 if (ds_new == NULL)
1740 {
1741 vim_free((*stackptr)->dirname);
1742 (*stackptr)->dirname = vim_strsave(dirbuf);
1743 }
1744 }
1745
1746 if ((*stackptr)->dirname != NULL)
1747 return (*stackptr)->dirname;
1748 else
1749 {
1750 ds_ptr = *stackptr;
1751 *stackptr = (*stackptr)->next;
1752 vim_free(ds_ptr);
1753 return NULL;
1754 }
1755}
1756
1757
1758/*
1759 * pop dirbuf from the directory stack and return previous directory or NULL if
1760 * stack is empty
1761 */
1762 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001763qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
1765 struct dir_stack_T *ds_ptr;
1766
1767 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1768 * What to do if it isn't? */
1769
1770 /* pop top element and free it */
1771 if (*stackptr != NULL)
1772 {
1773 ds_ptr = *stackptr;
1774 *stackptr = (*stackptr)->next;
1775 vim_free(ds_ptr->dirname);
1776 vim_free(ds_ptr);
1777 }
1778
1779 /* return NEW top element as current dir or NULL if stack is empty*/
1780 return *stackptr ? (*stackptr)->dirname : NULL;
1781}
1782
1783/*
1784 * clean up directory stack
1785 */
1786 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001787qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
1789 struct dir_stack_T *ds_ptr;
1790
1791 while ((ds_ptr = *stackptr) != NULL)
1792 {
1793 *stackptr = (*stackptr)->next;
1794 vim_free(ds_ptr->dirname);
1795 vim_free(ds_ptr);
1796 }
1797}
1798
1799/*
1800 * Check in which directory of the directory stack the given file can be
1801 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001802 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 * Cleans up intermediate directory entries.
1804 *
1805 * TODO: How to solve the following problem?
1806 * If we have the this directory tree:
1807 * ./
1808 * ./aa
1809 * ./aa/bb
1810 * ./bb
1811 * ./bb/x.c
1812 * and make says:
1813 * making all in aa
1814 * making all in bb
1815 * x.c:9: Error
1816 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1817 * qf_guess_filepath will return NULL.
1818 */
1819 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001820qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
1822 struct dir_stack_T *ds_ptr;
1823 struct dir_stack_T *ds_tmp;
1824 char_u *fullname;
1825
1826 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001827 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 return NULL;
1829
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001830 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 fullname = NULL;
1832 while (ds_ptr)
1833 {
1834 vim_free(fullname);
1835 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1836
1837 /* If concat_fnames failed, just go on. The worst thing that can happen
1838 * is that we delete the entire stack.
1839 */
1840 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1841 break;
1842
1843 ds_ptr = ds_ptr->next;
1844 }
1845
1846 vim_free(fullname);
1847
1848 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001849 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001851 ds_tmp = qi->qf_dir_stack->next;
1852 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 vim_free(ds_tmp->dirname);
1854 vim_free(ds_tmp);
1855 }
1856
1857 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858}
1859
1860/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001861 * When loading a file from the quickfix, the auto commands may modify it.
1862 * This may invalidate the current quickfix entry. This function checks
1863 * whether a entry is still present in the quickfix.
1864 * Similar to location list.
1865 */
1866 static int
1867is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1868{
1869 qf_list_T *qfl;
1870 qfline_T *qfp;
1871 int i;
1872
1873 qfl = &qi->qf_lists[qi->qf_curlist];
1874
1875 /* Search for the entry in the current list */
1876 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1877 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001878 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001879 break;
1880
1881 if (i == qfl->qf_count) /* Entry is not found */
1882 return FALSE;
1883
1884 return TRUE;
1885}
1886
1887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 * jump to a quickfix line
1889 * if dir == FORWARD go "errornr" valid entries forward
1890 * if dir == BACKWARD go "errornr" valid entries backward
1891 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1892 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1893 * else if "errornr" is zero, redisplay the same line
1894 * else go to entry "errornr"
1895 */
1896 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001897qf_jump(
1898 qf_info_T *qi,
1899 int dir,
1900 int errornr,
1901 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001903 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001904 qfline_T *qf_ptr;
1905 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 int qf_index;
1907 int old_qf_fnum;
1908 int old_qf_index;
1909 int prev_index;
1910 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1911 char_u *err = e_no_more_items;
1912 linenr_T i;
1913 buf_T *old_curbuf;
1914 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 colnr_T screen_col;
1916 colnr_T char_col;
1917 char_u *line;
1918#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001919 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001920 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 int opened_window = FALSE;
1922 win_T *win;
1923 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001924 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001926 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 int print_message = TRUE;
1928 int len;
1929#ifdef FEAT_FOLDING
1930 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1931#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001932 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001933 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001935 if (qi == NULL)
1936 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001937
1938 if (qi->qf_curlist >= qi->qf_listcount
1939 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 EMSG(_(e_quickfix));
1942 return;
1943 }
1944
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001945 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001947 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 old_qf_index = qf_index;
1949 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1950 {
1951 while (errornr--)
1952 {
1953 old_qf_ptr = qf_ptr;
1954 prev_index = qf_index;
1955 old_qf_fnum = qf_ptr->qf_fnum;
1956 do
1957 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001958 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 || qf_ptr->qf_next == NULL)
1960 {
1961 qf_ptr = old_qf_ptr;
1962 qf_index = prev_index;
1963 if (err != NULL)
1964 {
1965 EMSG(_(err));
1966 goto theend;
1967 }
1968 errornr = 0;
1969 break;
1970 }
1971 ++qf_index;
1972 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1974 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1976 err = NULL;
1977 }
1978 }
1979 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1980 {
1981 while (errornr--)
1982 {
1983 old_qf_ptr = qf_ptr;
1984 prev_index = qf_index;
1985 old_qf_fnum = qf_ptr->qf_fnum;
1986 do
1987 {
1988 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1989 {
1990 qf_ptr = old_qf_ptr;
1991 qf_index = prev_index;
1992 if (err != NULL)
1993 {
1994 EMSG(_(err));
1995 goto theend;
1996 }
1997 errornr = 0;
1998 break;
1999 }
2000 --qf_index;
2001 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002002 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2003 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2005 err = NULL;
2006 }
2007 }
2008 else if (errornr != 0) /* go to specified number */
2009 {
2010 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2011 {
2012 --qf_index;
2013 qf_ptr = qf_ptr->qf_prev;
2014 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002015 while (errornr > qf_index && qf_index <
2016 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 && qf_ptr->qf_next != NULL)
2018 {
2019 ++qf_index;
2020 qf_ptr = qf_ptr->qf_next;
2021 }
2022 }
2023
2024#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002025 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2026 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 /* No need to print the error message if it's visible in the error
2028 * window */
2029 print_message = FALSE;
2030
2031 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002032 * For ":helpgrep" find a help window or open one.
2033 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002034 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002035 {
2036 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002037
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002038 if (cmdmod.tab != 0)
2039 wp = NULL;
2040 else
2041 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2042 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2043 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002044 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2045 win_enter(wp, TRUE);
2046 else
2047 {
2048 /*
2049 * Split off help window; put it at far top if no position
2050 * specified, the current window is vertically split and narrow.
2051 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002052 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002053 if (cmdmod.split == 0 && curwin->w_width != Columns
2054 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002055 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002056 if (qi != &ql_info)
2057 flags |= WSP_NEWLOC; /* don't copy the location list */
2058
2059 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002060 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002061 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002062
2063 if (curwin->w_height < p_hh)
2064 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002065
2066 if (qi != &ql_info) /* not a quickfix list */
2067 {
2068 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002069 curwin->w_llist = qi;
2070 qi->qf_refcount++;
2071 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002072 }
2073
2074 if (!p_im)
2075 restart_edit = 0; /* don't want insert mode in help file */
2076 }
2077
2078 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 * If currently in the quickfix window, find another window to show the
2080 * file in.
2081 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002082 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002084 win_T *usable_win_ptr = NULL;
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 /*
2087 * If there is no file specified, we don't know where to go.
2088 * But do advance, otherwise ":cn" gets stuck.
2089 */
2090 if (qf_ptr->qf_fnum == 0)
2091 goto theend;
2092
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002093 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002094
2095 ll_ref = curwin->w_llist_ref;
2096 if (ll_ref != NULL)
2097 {
2098 /* Find a window using the same location list that is not a
2099 * quickfix window. */
2100 FOR_ALL_WINDOWS(usable_win_ptr)
2101 if (usable_win_ptr->w_llist == ll_ref
2102 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002103 {
2104 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002105 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002106 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002107 }
2108
2109 if (!usable_win)
2110 {
2111 /* Locate a window showing a normal buffer */
2112 FOR_ALL_WINDOWS(win)
2113 if (win->w_buffer->b_p_bt[0] == NUL)
2114 {
2115 usable_win = 1;
2116 break;
2117 }
2118 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002121 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002122 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002124 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002125 {
2126 tabpage_T *tp;
2127 win_T *wp;
2128
2129 FOR_ALL_TAB_WINDOWS(tp, wp)
2130 {
2131 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2132 {
2133 goto_tabpage_win(tp, wp);
2134 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002135 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002136 }
2137 }
2138 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002139win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002140
2141 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002142 * If there is only one window and it is the quickfix window, create a
2143 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002144 */
2145 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002147 flags = WSP_ABOVE;
2148 if (ll_ref != NULL)
2149 flags |= WSP_NEWLOC;
2150 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 goto failed; /* not enough room for window */
2152 opened_window = TRUE; /* close it when fail */
2153 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002154 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002155 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002156 if (ll_ref != NULL)
2157 {
2158 /* The new window should use the location list from the
2159 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160 curwin->w_llist = ll_ref;
2161 ll_ref->qf_refcount++;
2162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 else
2165 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002166 if (curwin->w_llist_ref != NULL)
2167 {
2168 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002169 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002170 if (win == NULL)
2171 {
2172 /* Find the window showing the selected file */
2173 FOR_ALL_WINDOWS(win)
2174 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2175 break;
2176 if (win == NULL)
2177 {
2178 /* Find a previous usable window */
2179 win = curwin;
2180 do
2181 {
2182 if (win->w_buffer->b_p_bt[0] == NUL)
2183 break;
2184 if (win->w_prev == NULL)
2185 win = lastwin; /* wrap around the top */
2186 else
2187 win = win->w_prev; /* go to previous window */
2188 } while (win != curwin);
2189 }
2190 }
2191 win_goto(win);
2192
2193 /* If the location list for the window is not set, then set it
2194 * to the location list from the location window */
2195 if (win->w_llist == NULL)
2196 {
2197 win->w_llist = ll_ref;
2198 ll_ref->qf_refcount++;
2199 }
2200 }
2201 else
2202 {
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 /*
2205 * Try to find a window that shows the right buffer.
2206 * Default to the window just above the quickfix buffer.
2207 */
2208 win = curwin;
2209 altwin = NULL;
2210 for (;;)
2211 {
2212 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2213 break;
2214 if (win->w_prev == NULL)
2215 win = lastwin; /* wrap around the top */
2216 else
2217 win = win->w_prev; /* go to previous window */
2218
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002219 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
2221 /* Didn't find it, go to the window before the quickfix
2222 * window. */
2223 if (altwin != NULL)
2224 win = altwin;
2225 else if (curwin->w_prev != NULL)
2226 win = curwin->w_prev;
2227 else
2228 win = curwin->w_next;
2229 break;
2230 }
2231
2232 /* Remember a usable window. */
2233 if (altwin == NULL && !win->w_p_pvw
2234 && win->w_buffer->b_p_bt[0] == NUL)
2235 altwin = win;
2236 }
2237
2238 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241 }
2242#endif
2243
2244 /*
2245 * If there is a file name,
2246 * read the wanted file if needed, and check autowrite etc.
2247 */
2248 old_curbuf = curbuf;
2249 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002250
2251 if (qf_ptr->qf_fnum != 0)
2252 {
2253 if (qf_ptr->qf_type == 1)
2254 {
2255 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2256 * set b_p_ro flag). */
2257 if (!can_abandon(curbuf, forceit))
2258 {
2259 EMSG(_(e_nowrtmsg));
2260 ok = FALSE;
2261 }
2262 else
2263 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002264 ECMD_HIDE + ECMD_SET_HELP,
2265 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002266 }
2267 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002268 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002269 int old_qf_curlist = qi->qf_curlist;
2270 int is_abort = FALSE;
2271
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002272 ok = buflist_getfile(qf_ptr->qf_fnum,
2273 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002274 if (qi != &ql_info && !win_valid(oldwin))
2275 {
2276 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002277 is_abort = TRUE;
2278 opened_window = FALSE;
2279 }
2280 else if (old_qf_curlist != qi->qf_curlist
2281 || !is_qf_entry_present(qi, qf_ptr))
2282 {
2283 if (qi == &ql_info)
2284 EMSG(_("E925: Current quickfix was changed"));
2285 else
2286 EMSG(_("E926: Current location list was changed"));
2287 is_abort = TRUE;
2288 }
2289
2290 if (is_abort)
2291 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002292 ok = FALSE;
2293 qi = NULL;
2294 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002295 }
2296 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002297 }
2298
2299 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
2301 /* When not switched to another buffer, still need to set pc mark */
2302 if (curbuf == old_curbuf)
2303 setpcmark();
2304
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002305 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002307 /*
2308 * Go to line with error, unless qf_lnum is 0.
2309 */
2310 i = qf_ptr->qf_lnum;
2311 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002313 if (i > curbuf->b_ml.ml_line_count)
2314 i = curbuf->b_ml.ml_line_count;
2315 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002317 if (qf_ptr->qf_col > 0)
2318 {
2319 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002320#ifdef FEAT_VIRTUALEDIT
2321 curwin->w_cursor.coladd = 0;
2322#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002323 if (qf_ptr->qf_viscol == TRUE)
2324 {
2325 /*
2326 * Check each character from the beginning of the error
2327 * line up to the error column. For each tab character
2328 * found, reduce the error column value by the length of
2329 * a tab character.
2330 */
2331 line = ml_get_curline();
2332 screen_col = 0;
2333 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2334 {
2335 if (*line == NUL)
2336 break;
2337 if (*line++ == '\t')
2338 {
2339 curwin->w_cursor.col -= 7 - (screen_col % 8);
2340 screen_col += 8 - (screen_col % 8);
2341 }
2342 else
2343 ++screen_col;
2344 }
2345 }
2346 check_cursor();
2347 }
2348 else
2349 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002352 {
2353 pos_T save_cursor;
2354
2355 /* Move the cursor to the first line in the buffer */
2356 save_cursor = curwin->w_cursor;
2357 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002358 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2359 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002360 curwin->w_cursor = save_cursor;
2361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363#ifdef FEAT_FOLDING
2364 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2365 foldOpenCursor();
2366#endif
2367 if (print_message)
2368 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002369 /* Update the screen before showing the message, unless the screen
2370 * scrolled up. */
2371 if (!msg_scrolled)
2372 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002374 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2376 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2377 /* Add the message, skipping leading whitespace and newlines. */
2378 len = (int)STRLEN(IObuff);
2379 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2380
2381 /* Output the message. Overwrite to avoid scrolling when the 'O'
2382 * flag is present in 'shortmess'; But when not jumping, print the
2383 * whole message. */
2384 i = msg_scroll;
2385 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2386 msg_scroll = TRUE;
2387 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2388 msg_scroll = FALSE;
2389 msg_attr_keep(IObuff, 0, TRUE);
2390 msg_scroll = i;
2391 }
2392 }
2393 else
2394 {
2395#ifdef FEAT_WINDOWS
2396 if (opened_window)
2397 win_close(curwin, TRUE); /* Close opened window */
2398#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002399 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
2401 /*
2402 * Couldn't open file, so put index back where it was. This could
2403 * happen if the file was readonly and we changed something.
2404 */
2405#ifdef FEAT_WINDOWS
2406failed:
2407#endif
2408 qf_ptr = old_qf_ptr;
2409 qf_index = old_qf_index;
2410 }
2411 }
2412theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002413 if (qi != NULL)
2414 {
2415 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2416 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418#ifdef FEAT_WINDOWS
2419 if (p_swb != old_swb && opened_window)
2420 {
2421 /* Restore old 'switchbuf' value, but not when an autocommand or
2422 * modeline has changed the value. */
2423 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002426 swb_flags = old_swb_flags;
2427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 else
2429 free_string_option(old_swb);
2430 }
2431#endif
2432}
2433
2434/*
2435 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002436 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 */
2438 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002439qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002441 buf_T *buf;
2442 char_u *fname;
2443 qfline_T *qfp;
2444 int i;
2445 int idx1 = 1;
2446 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002447 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002448 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002449 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002451 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002453 if (eap->cmdidx == CMD_llist)
2454 {
2455 qi = GET_LOC_LIST(curwin);
2456 if (qi == NULL)
2457 {
2458 EMSG(_(e_loclist));
2459 return;
2460 }
2461 }
2462
2463 if (qi->qf_curlist >= qi->qf_listcount
2464 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
2466 EMSG(_(e_quickfix));
2467 return;
2468 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002469 if (*arg == '+')
2470 {
2471 ++arg;
2472 plus = TRUE;
2473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2475 {
2476 EMSG(_(e_trailing));
2477 return;
2478 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002479 if (plus)
2480 {
2481 i = qi->qf_lists[qi->qf_curlist].qf_index;
2482 idx2 = i + idx1;
2483 idx1 = i;
2484 }
2485 else
2486 {
2487 i = qi->qf_lists[qi->qf_curlist].qf_count;
2488 if (idx1 < 0)
2489 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2490 if (idx2 < 0)
2491 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002494 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002496 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2497 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2500 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002501 msg_putchar('\n');
2502 if (got_int)
2503 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002504
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002505 fname = NULL;
2506 if (qfp->qf_fnum != 0
2507 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2508 {
2509 fname = buf->b_fname;
2510 if (qfp->qf_type == 1) /* :helpgrep */
2511 fname = gettail(fname);
2512 }
2513 if (fname == NULL)
2514 sprintf((char *)IObuff, "%2d", i);
2515 else
2516 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2517 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002518 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2520 if (qfp->qf_lnum == 0)
2521 IObuff[0] = NUL;
2522 else if (qfp->qf_col == 0)
2523 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2524 else
2525 sprintf((char *)IObuff, ":%ld col %d",
2526 qfp->qf_lnum, qfp->qf_col);
2527 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2528 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2529 msg_puts_attr(IObuff, hl_attr(HLF_N));
2530 if (qfp->qf_pattern != NULL)
2531 {
2532 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2533 STRCAT(IObuff, ":");
2534 msg_puts(IObuff);
2535 }
2536 msg_puts((char_u *)" ");
2537
2538 /* Remove newlines and leading whitespace from the text. For an
2539 * unrecognized line keep the indent, the compiler may mark a word
2540 * with ^^^^. */
2541 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2543 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002544 msg_prt_line(IObuff, FALSE);
2545 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002547
2548 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002549 if (qfp == NULL)
2550 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002551 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 ui_breakcheck();
2553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554}
2555
2556/*
2557 * Remove newlines and leading whitespace from an error message.
2558 * Put the result in "buf[bufsize]".
2559 */
2560 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002561qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 int i;
2564 char_u *p = text;
2565
2566 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2567 {
2568 if (*p == '\n')
2569 {
2570 buf[i] = ' ';
2571 while (*++p != NUL)
2572 if (!vim_iswhite(*p) && *p != '\n')
2573 break;
2574 }
2575 else
2576 buf[i] = *p++;
2577 }
2578 buf[i] = NUL;
2579}
2580
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002581 static void
2582qf_msg(qf_info_T *qi, int which, char *lead)
2583{
2584 char *title = (char *)qi->qf_lists[which].qf_title;
2585 int count = qi->qf_lists[which].qf_count;
2586 char_u buf[IOSIZE];
2587
2588 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2589 lead,
2590 which + 1,
2591 qi->qf_listcount,
2592 count);
2593
2594 if (title != NULL)
2595 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002596 size_t len = STRLEN(buf);
2597
2598 if (len < 34)
2599 {
2600 vim_memset(buf + len, ' ', 34 - len);
2601 buf[34] = NUL;
2602 }
2603 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002604 }
2605 trunc_string(buf, buf, Columns - 1, IOSIZE);
2606 msg(buf);
2607}
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609/*
2610 * ":colder [count]": Up in the quickfix stack.
2611 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002612 * ":lolder [count]": Up in the location list stack.
2613 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 */
2615 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002616qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002618 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 int count;
2620
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002621 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2622 {
2623 qi = GET_LOC_LIST(curwin);
2624 if (qi == NULL)
2625 {
2626 EMSG(_(e_loclist));
2627 return;
2628 }
2629 }
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if (eap->addr_count != 0)
2632 count = eap->line2;
2633 else
2634 count = 1;
2635 while (count--)
2636 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002637 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002639 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 {
2641 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002642 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002644 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646 else
2647 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002648 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
2650 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002651 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002653 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
2655 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002656 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002658 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#endif
2660}
2661
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002662 void
2663qf_history(exarg_T *eap)
2664{
2665 qf_info_T *qi = &ql_info;
2666 int i;
2667
2668 if (eap->cmdidx == CMD_lhistory)
2669 qi = GET_LOC_LIST(curwin);
2670 if (qi == NULL || (qi->qf_listcount == 0
2671 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2672 MSG(_("No entries"));
2673 else
2674 for (i = 0; i < qi->qf_listcount; ++i)
2675 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2676}
2677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002679 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
2681 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002682qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002684 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002685 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002686 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002688 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002690 qfp = qi->qf_lists[idx].qf_start;
2691 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002692 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002693 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002694 vim_free(qfp->qf_text);
2695 stop = (qfp == qfpnext);
2696 vim_free(qfp->qf_pattern);
2697 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002698 if (stop)
2699 /* Somehow qf_count may have an incorrect value, set it to 1
2700 * to avoid crashing when it's wrong.
2701 * TODO: Avoid qf_count being incorrect. */
2702 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002703 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002704 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002705 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002707 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002708 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002709 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002710
2711 qf_clean_dir_stack(&qi->qf_dir_stack);
2712 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713}
2714
2715/*
2716 * qf_mark_adjust: adjust marks
2717 */
2718 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002719qf_mark_adjust(
2720 win_T *wp,
2721 linenr_T line1,
2722 linenr_T line2,
2723 long amount,
2724 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002726 int i;
2727 qfline_T *qfp;
2728 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002729 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002730 int found_one = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002732 if (!curbuf->b_has_qf_entry)
2733 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002734 if (wp != NULL)
2735 {
2736 if (wp->w_llist == NULL)
2737 return;
2738 qi = wp->w_llist;
2739 }
2740
2741 for (idx = 0; idx < qi->qf_listcount; ++idx)
2742 if (qi->qf_lists[idx].qf_count)
2743 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002744 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2745 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (qfp->qf_fnum == curbuf->b_fnum)
2747 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002748 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2750 {
2751 if (amount == MAXLNUM)
2752 qfp->qf_cleared = TRUE;
2753 else
2754 qfp->qf_lnum += amount;
2755 }
2756 else if (amount_after && qfp->qf_lnum > line2)
2757 qfp->qf_lnum += amount_after;
2758 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002759
2760 if (!found_one)
2761 curbuf->b_has_qf_entry = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762}
2763
2764/*
2765 * Make a nice message out of the error character and the error number:
2766 * char number message
2767 * e or E 0 " error"
2768 * w or W 0 " warning"
2769 * i or I 0 " info"
2770 * 0 0 ""
2771 * other 0 " c"
2772 * e or E n " error n"
2773 * w or W n " warning n"
2774 * i or I n " info n"
2775 * 0 n " error n"
2776 * other n " c n"
2777 * 1 x "" :helpgrep
2778 */
2779 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002780qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 static char_u buf[20];
2783 static char_u cc[3];
2784 char_u *p;
2785
2786 if (c == 'W' || c == 'w')
2787 p = (char_u *)" warning";
2788 else if (c == 'I' || c == 'i')
2789 p = (char_u *)" info";
2790 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2791 p = (char_u *)" error";
2792 else if (c == 0 || c == 1)
2793 p = (char_u *)"";
2794 else
2795 {
2796 cc[0] = ' ';
2797 cc[1] = c;
2798 cc[2] = NUL;
2799 p = cc;
2800 }
2801
2802 if (nr <= 0)
2803 return p;
2804
2805 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2806 return buf;
2807}
2808
2809#if defined(FEAT_WINDOWS) || defined(PROTO)
2810/*
2811 * ":cwindow": open the quickfix window if we have errors to display,
2812 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002813 * ":lwindow": open the location list window if we have locations to display,
2814 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 */
2816 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002817ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002819 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 win_T *win;
2821
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002822 if (eap->cmdidx == CMD_lwindow)
2823 {
2824 qi = GET_LOC_LIST(curwin);
2825 if (qi == NULL)
2826 return;
2827 }
2828
2829 /* Look for an existing quickfix window. */
2830 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
2832 /*
2833 * If a quickfix window is open but we have no errors to display,
2834 * close the window. If a quickfix window is not open, then open
2835 * it if we have errors; otherwise, leave it closed.
2836 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002837 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002838 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002839 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
2841 if (win != NULL)
2842 ex_cclose(eap);
2843 }
2844 else if (win == NULL)
2845 ex_copen(eap);
2846}
2847
2848/*
2849 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002850 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002853ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002855 win_T *win = NULL;
2856 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002858 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2859 {
2860 qi = GET_LOC_LIST(curwin);
2861 if (qi == NULL)
2862 return;
2863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002865 /* Find existing quickfix window and close it. */
2866 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 if (win != NULL)
2868 win_close(win, FALSE);
2869}
2870
2871/*
2872 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002873 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 */
2875 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002876ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002878 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002881 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002882 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002883 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002885 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2886 {
2887 qi = GET_LOC_LIST(curwin);
2888 if (qi == NULL)
2889 {
2890 EMSG(_(e_loclist));
2891 return;
2892 }
2893 }
2894
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 if (eap->addr_count != 0)
2896 height = eap->line2;
2897 else
2898 height = QF_WINHEIGHT;
2899
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901#ifdef FEAT_GUI
2902 need_mouse_correct = TRUE;
2903#endif
2904
2905 /*
2906 * Find existing quickfix window, or open a new one.
2907 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002908 win = qf_find_win(qi);
2909
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002910 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002913 if (eap->addr_count != 0)
2914 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002915 if (cmdmod.split & WSP_VERT)
2916 {
2917 if (height != W_WIDTH(win))
2918 win_setwidth(height);
2919 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002920 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002921 win_setheight(height);
2922 }
2923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 else
2925 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002926 qf_buf = qf_find_buf(qi);
2927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 /* The current window becomes the previous window afterwards. */
2929 win = curwin;
2930
Bram Moolenaar77642c02012-11-20 17:55:10 +01002931 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2932 && cmdmod.split == 0)
2933 /* Create the new window at the very bottom, except when
2934 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002935 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002936 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002938 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002940 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002942 /*
2943 * For the location list window, create a reference to the
2944 * location list from the window 'win'.
2945 */
2946 curwin->w_llist_ref = win->w_llist;
2947 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002949
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002950 if (oldwin != curwin)
2951 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002952 if (qf_buf != NULL)
2953 /* Use the existing quickfix buffer */
2954 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002955 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002956 else
2957 {
2958 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002959 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002960 /* switch off 'swapfile' */
2961 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2962 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002963 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002964 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002965 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002966#ifdef FEAT_DIFF
2967 curwin->w_p_diff = FALSE;
2968#endif
2969#ifdef FEAT_FOLDING
2970 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2971 OPT_LOCAL);
2972#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002975 /* Only set the height when still in the same tab page and there is no
2976 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002977 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 win_setheight(height);
2979 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2980 if (win_valid(win))
2981 prevwin = win;
2982 }
2983
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002984 qf_set_title_var(qi);
2985
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 /*
2987 * Fill the buffer with the quickfix list.
2988 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002989 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002991 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 curwin->w_cursor.col = 0;
2993 check_cursor();
2994 update_topline(); /* scroll to show the line */
2995}
2996
2997/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02002998 * Move the cursor in the quickfix window to "lnum".
2999 */
3000 static void
3001qf_win_goto(win_T *win, linenr_T lnum)
3002{
3003 win_T *old_curwin = curwin;
3004
3005 curwin = win;
3006 curbuf = win->w_buffer;
3007 curwin->w_cursor.lnum = lnum;
3008 curwin->w_cursor.col = 0;
3009#ifdef FEAT_VIRTUALEDIT
3010 curwin->w_cursor.coladd = 0;
3011#endif
3012 curwin->w_curswant = 0;
3013 update_topline(); /* scroll to show the line */
3014 redraw_later(VALID);
3015 curwin->w_redr_status = TRUE; /* update ruler */
3016 curwin = old_curwin;
3017 curbuf = curwin->w_buffer;
3018}
3019
3020/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003021 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003022 */
3023 void
3024ex_cbottom(exarg_T *eap UNUSED)
3025{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003026 qf_info_T *qi = &ql_info;
3027 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003028
Bram Moolenaar537ef082016-07-09 17:56:19 +02003029 if (eap->cmdidx == CMD_lbottom)
3030 {
3031 qi = GET_LOC_LIST(curwin);
3032 if (qi == NULL)
3033 {
3034 EMSG(_(e_loclist));
3035 return;
3036 }
3037 }
3038
3039 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003040 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3041 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3042}
3043
3044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 * Return the number of the current entry (line number in the quickfix
3046 * window).
3047 */
3048 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003049qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003051 qf_info_T *qi = &ql_info;
3052
3053 if (IS_LL_WINDOW(wp))
3054 /* In the location list window, use the referenced location list */
3055 qi = wp->w_llist_ref;
3056
3057 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058}
3059
3060/*
3061 * Update the cursor position in the quickfix window to the current error.
3062 * Return TRUE if there is a quickfix window.
3063 */
3064 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003065qf_win_pos_update(
3066 qf_info_T *qi,
3067 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003070 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071
3072 /*
3073 * Put the cursor on the current error in the quickfix window, so that
3074 * it's viewable.
3075 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003076 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 if (win != NULL
3078 && qf_index <= win->w_buffer->b_ml.ml_line_count
3079 && old_qf_index != qf_index)
3080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 if (qf_index > old_qf_index)
3082 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003083 win->w_redraw_top = old_qf_index;
3084 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
3086 else
3087 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003088 win->w_redraw_top = qf_index;
3089 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003091 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 }
3093 return win != NULL;
3094}
3095
3096/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003097 * Check whether the given window is displaying the specified quickfix/location
3098 * list buffer
3099 */
3100 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003101is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003102{
3103 /*
3104 * A window displaying the quickfix buffer will have the w_llist_ref field
3105 * set to NULL.
3106 * A window displaying a location list buffer will have the w_llist_ref
3107 * pointing to the location list.
3108 */
3109 if (bt_quickfix(win->w_buffer))
3110 if ((qi == &ql_info && win->w_llist_ref == NULL)
3111 || (qi != &ql_info && win->w_llist_ref == qi))
3112 return TRUE;
3113
3114 return FALSE;
3115}
3116
3117/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003118 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003119 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003120 */
3121 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003122qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003123{
3124 win_T *win;
3125
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003126 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003127 if (is_qf_win(win, qi))
3128 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003129
3130 return win;
3131}
3132
3133/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003134 * Find a quickfix buffer.
3135 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 */
3137 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003138qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003140 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003141 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
Bram Moolenaar9c102382006-05-03 21:26:49 +00003143 FOR_ALL_TAB_WINDOWS(tp, win)
3144 if (is_qf_win(win, qi))
3145 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003146
Bram Moolenaar9c102382006-05-03 21:26:49 +00003147 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148}
3149
3150/*
3151 * Find the quickfix buffer. If it exists, update the contents.
3152 */
3153 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003154qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
3156 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003157 win_T *win;
3158 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
3161 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003162 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 if (buf != NULL)
3164 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003165 linenr_T old_line_count = buf->b_ml.ml_line_count;
3166
3167 if (old_last == NULL)
3168 /* set curwin/curbuf to buf and save a few things */
3169 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003171 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003172 {
3173 curwin_save = curwin;
3174 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02003175 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003176 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003177 }
3178
Bram Moolenaar864293a2016-06-02 13:40:04 +02003179 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003180
Bram Moolenaar864293a2016-06-02 13:40:04 +02003181 if (old_last == NULL)
3182 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003183 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003184
3185 /* restore curwin/curbuf and a few other things */
3186 aucmd_restbuf(&aco);
3187 }
3188
3189 /* Only redraw when added lines are visible. This avoids flickering
3190 * when the added lines are not visible. */
3191 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3192 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
3194}
3195
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003196/*
3197 * Set "w:quickfix_title" if "qi" has a title.
3198 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003199 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003200qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003201{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003202 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3203 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003204 qi->qf_lists[qi->qf_curlist].qf_title);
3205}
3206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207/*
3208 * Fill current buffer with quickfix errors, replacing any previous contents.
3209 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003210 * If "old_last" is not NULL append the items after this one.
3211 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3212 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 */
3214 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003215qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003217 linenr_T lnum;
3218 qfline_T *qfp;
3219 buf_T *errbuf;
3220 int len;
3221 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
Bram Moolenaar864293a2016-06-02 13:40:04 +02003223 if (old_last == NULL)
3224 {
3225 if (buf != curbuf)
3226 {
3227 EMSG2(_(e_intern2), "qf_fill_buffer()");
3228 return;
3229 }
3230
3231 /* delete all existing lines */
3232 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3233 (void)ml_delete((linenr_T)1, FALSE);
3234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003237 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
3239 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003240 if (old_last == NULL)
3241 {
3242 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3243 lnum = 0;
3244 }
3245 else
3246 {
3247 qfp = old_last->qf_next;
3248 lnum = buf->b_ml.ml_line_count;
3249 }
3250 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 {
3252 if (qfp->qf_fnum != 0
3253 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3254 && errbuf->b_fname != NULL)
3255 {
3256 if (qfp->qf_type == 1) /* :helpgrep */
3257 STRCPY(IObuff, gettail(errbuf->b_fname));
3258 else
3259 STRCPY(IObuff, errbuf->b_fname);
3260 len = (int)STRLEN(IObuff);
3261 }
3262 else
3263 len = 0;
3264 IObuff[len++] = '|';
3265
3266 if (qfp->qf_lnum > 0)
3267 {
3268 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3269 len += (int)STRLEN(IObuff + len);
3270
3271 if (qfp->qf_col > 0)
3272 {
3273 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3274 len += (int)STRLEN(IObuff + len);
3275 }
3276
3277 sprintf((char *)IObuff + len, "%s",
3278 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3279 len += (int)STRLEN(IObuff + len);
3280 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003281 else if (qfp->qf_pattern != NULL)
3282 {
3283 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3284 len += (int)STRLEN(IObuff + len);
3285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 IObuff[len++] = '|';
3287 IObuff[len++] = ' ';
3288
3289 /* Remove newlines and leading whitespace from the text.
3290 * For an unrecognized line keep the indent, the compiler may
3291 * mark a word with ^^^^. */
3292 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3293 IObuff + len, IOSIZE - len);
3294
Bram Moolenaar864293a2016-06-02 13:40:04 +02003295 if (ml_append_buf(buf, lnum, IObuff,
3296 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003298 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003300 if (qfp == NULL)
3301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003303
3304 if (old_last == NULL)
3305 /* Delete the empty line which is now at the end */
3306 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308
3309 /* correct cursor position */
3310 check_lnums(TRUE);
3311
Bram Moolenaar864293a2016-06-02 13:40:04 +02003312 if (old_last == NULL)
3313 {
3314 /* Set the 'filetype' to "qf" each time after filling the buffer.
3315 * This resembles reading a file into a buffer, it's more logical when
3316 * using autocommands. */
3317 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3318 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
3320#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003321 keep_filetype = TRUE; /* don't detect 'filetype' */
3322 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003324 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003326 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003328 /* make sure it will be redrawn */
3329 redraw_curbuf_later(NOT_VALID);
3330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
3332 /* Restore KeyTyped, setting 'filetype' may reset it. */
3333 KeyTyped = old_KeyTyped;
3334}
3335
3336#endif /* FEAT_WINDOWS */
3337
3338/*
3339 * Return TRUE if "buf" is the quickfix buffer.
3340 */
3341 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003342bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003344 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345}
3346
3347/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003348 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3349 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 */
3351 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003352bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003354 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3355 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356}
3357
3358/*
3359 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3360 */
3361 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003362bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003364 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365}
3366
3367 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003368bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 if (bt_dontwrite(buf))
3371 {
3372 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3373 return TRUE;
3374 }
3375 return FALSE;
3376}
3377
3378/*
3379 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3380 * and 'bufhidden'.
3381 */
3382 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003383buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
3385 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3386 switch (buf->b_p_bh[0])
3387 {
3388 case 'u': /* "unload" */
3389 case 'w': /* "wipe" */
3390 case 'd': return FALSE; /* "delete" */
3391 case 'h': return TRUE; /* "hide" */
3392 }
3393 return (p_hid || cmdmod.hide);
3394}
3395
3396/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003397 * Return TRUE when using ":vimgrep" for ":grep".
3398 */
3399 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003400grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003401{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003402 return ((cmdidx == CMD_grep
3403 || cmdidx == CMD_lgrep
3404 || cmdidx == CMD_grepadd
3405 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003406 && STRCMP("internal",
3407 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3408}
3409
3410/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003411 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 */
3413 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003414ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003416 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 char_u *cmd;
3418 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003419 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003420 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003421 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003422#ifdef FEAT_AUTOCMD
3423 char_u *au_name = NULL;
3424
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003425 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3426 if (grep_internal(eap->cmdidx))
3427 {
3428 ex_vimgrep(eap);
3429 return;
3430 }
3431
Bram Moolenaar7c626922005-02-07 22:01:03 +00003432 switch (eap->cmdidx)
3433 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003434 case CMD_make: au_name = (char_u *)"make"; break;
3435 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3436 case CMD_grep: au_name = (char_u *)"grep"; break;
3437 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3438 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3439 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003440 default: break;
3441 }
3442 if (au_name != NULL)
3443 {
3444 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3445 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003446# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003447 if (did_throw || force_abort)
3448 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003449# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003450 }
3451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
Bram Moolenaara6557602006-02-04 22:43:20 +00003453 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3454 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003455 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003456
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003458 fname = get_mef_name();
3459 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003461 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
3463 /*
3464 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3465 */
3466 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3467 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003468 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 cmd = alloc(len);
3470 if (cmd == NULL)
3471 return;
3472 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3473 (char *)p_shq);
3474 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003475 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 /*
3477 * Output a newline if there's something else than the :make command that
3478 * was typed (in which case the cursor is in column 0).
3479 */
3480 if (msg_col == 0)
3481 msg_didout = FALSE;
3482 msg_start();
3483 MSG_PUTS(":!");
3484 msg_outtrans(cmd); /* show what we are doing */
3485
3486 /* let the shell know if we are redirecting output or not */
3487 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3488
3489#ifdef AMIGA
3490 out_flush();
3491 /* read window status report and redraw before message */
3492 (void)char_avail();
3493#endif
3494
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003495 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003496 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3497 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003498 && eap->cmdidx != CMD_lgrepadd),
3499 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003500 if (wp != NULL)
3501 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003502#ifdef FEAT_AUTOCMD
3503 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003504 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003505 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3506 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003507 if (qi->qf_curlist < qi->qf_listcount)
3508 res = qi->qf_lists[qi->qf_curlist].qf_count;
3509 else
3510 res = 0;
3511 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003512#endif
3513 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003514 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515
Bram Moolenaar7c626922005-02-07 22:01:03 +00003516 mch_remove(fname);
3517 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 vim_free(cmd);
3519}
3520
3521/*
3522 * Return the name for the errorfile, in allocated memory.
3523 * Find a new unique name when 'makeef' contains "##".
3524 * Returns NULL for error.
3525 */
3526 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003527get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
3529 char_u *p;
3530 char_u *name;
3531 static int start = -1;
3532 static int off = 0;
3533#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003534 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535#endif
3536
3537 if (*p_mef == NUL)
3538 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003539 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (name == NULL)
3541 EMSG(_(e_notmp));
3542 return name;
3543 }
3544
3545 for (p = p_mef; *p; ++p)
3546 if (p[0] == '#' && p[1] == '#')
3547 break;
3548
3549 if (*p == NUL)
3550 return vim_strsave(p_mef);
3551
3552 /* Keep trying until the name doesn't exist yet. */
3553 for (;;)
3554 {
3555 if (start == -1)
3556 start = mch_get_pid();
3557 else
3558 off += 19;
3559
3560 name = alloc((unsigned)STRLEN(p_mef) + 30);
3561 if (name == NULL)
3562 break;
3563 STRCPY(name, p_mef);
3564 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3565 STRCAT(name, p + 2);
3566 if (mch_getperm(name) < 0
3567#ifdef HAVE_LSTAT
3568 /* Don't accept a symbolic link, its a security risk. */
3569 && mch_lstat((char *)name, &sb) < 0
3570#endif
3571 )
3572 break;
3573 vim_free(name);
3574 }
3575 return name;
3576}
3577
3578/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003579 * Returns the number of valid entries in the current quickfix/location list.
3580 */
3581 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003582qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003583{
3584 qf_info_T *qi = &ql_info;
3585 qfline_T *qfp;
3586 int i, sz = 0;
3587 int prev_fnum = 0;
3588
3589 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3590 {
3591 /* Location list */
3592 qi = GET_LOC_LIST(curwin);
3593 if (qi == NULL)
3594 return 0;
3595 }
3596
3597 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003598 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003599 ++i, qfp = qfp->qf_next)
3600 {
3601 if (qfp->qf_valid)
3602 {
3603 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3604 sz++; /* Count all valid entries */
3605 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3606 {
3607 /* Count the number of files */
3608 sz++;
3609 prev_fnum = qfp->qf_fnum;
3610 }
3611 }
3612 }
3613
3614 return sz;
3615}
3616
3617/*
3618 * Returns the current index of the quickfix/location list.
3619 * Returns 0 if there is an error.
3620 */
3621 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003622qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003623{
3624 qf_info_T *qi = &ql_info;
3625
3626 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3627 {
3628 /* Location list */
3629 qi = GET_LOC_LIST(curwin);
3630 if (qi == NULL)
3631 return 0;
3632 }
3633
3634 return qi->qf_lists[qi->qf_curlist].qf_index;
3635}
3636
3637/*
3638 * Returns the current index in the quickfix/location list (counting only valid
3639 * entries). If no valid entries are in the list, then returns 1.
3640 */
3641 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003642qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003643{
3644 qf_info_T *qi = &ql_info;
3645 qf_list_T *qfl;
3646 qfline_T *qfp;
3647 int i, eidx = 0;
3648 int prev_fnum = 0;
3649
3650 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3651 {
3652 /* Location list */
3653 qi = GET_LOC_LIST(curwin);
3654 if (qi == NULL)
3655 return 1;
3656 }
3657
3658 qfl = &qi->qf_lists[qi->qf_curlist];
3659 qfp = qfl->qf_start;
3660
3661 /* check if the list has valid errors */
3662 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3663 return 1;
3664
3665 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3666 {
3667 if (qfp->qf_valid)
3668 {
3669 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3670 {
3671 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3672 {
3673 /* Count the number of files */
3674 eidx++;
3675 prev_fnum = qfp->qf_fnum;
3676 }
3677 }
3678 else
3679 eidx++;
3680 }
3681 }
3682
3683 return eidx ? eidx : 1;
3684}
3685
3686/*
3687 * Get the 'n'th valid error entry in the quickfix or location list.
3688 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3689 * For :cdo and :ldo returns the 'n'th valid error entry.
3690 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3691 */
3692 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003693qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003694{
3695 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3696 qfline_T *qfp = qfl->qf_start;
3697 int i, eidx;
3698 int prev_fnum = 0;
3699
3700 /* check if the list has valid errors */
3701 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3702 return 1;
3703
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003704 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003705 i++, qfp = qfp->qf_next)
3706 {
3707 if (qfp->qf_valid)
3708 {
3709 if (fdo)
3710 {
3711 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3712 {
3713 /* Count the number of files */
3714 eidx++;
3715 prev_fnum = qfp->qf_fnum;
3716 }
3717 }
3718 else
3719 eidx++;
3720 }
3721
3722 if (eidx == n)
3723 break;
3724 }
3725
3726 if (i <= qfl->qf_count)
3727 return i;
3728 else
3729 return 1;
3730}
3731
3732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003734 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003735 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 */
3737 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003738ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003740 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003741 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003742
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003743 if (eap->cmdidx == CMD_ll
3744 || eap->cmdidx == CMD_lrewind
3745 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003746 || eap->cmdidx == CMD_llast
3747 || eap->cmdidx == CMD_ldo
3748 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003749 {
3750 qi = GET_LOC_LIST(curwin);
3751 if (qi == NULL)
3752 {
3753 EMSG(_(e_loclist));
3754 return;
3755 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003756 }
3757
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003758 if (eap->addr_count > 0)
3759 errornr = (int)eap->line2;
3760 else
3761 {
3762 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3763 errornr = 0;
3764 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3765 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3766 errornr = 1;
3767 else
3768 errornr = 32767;
3769 }
3770
3771 /* For cdo and ldo commands, jump to the nth valid error.
3772 * For cfdo and lfdo commands, jump to the nth valid file entry.
3773 */
3774 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3775 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3776 errornr = qf_get_nth_valid_entry(qi,
3777 eap->addr_count > 0 ? (int)eap->line1 : 1,
3778 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3779
3780 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781}
3782
3783/*
3784 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003785 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003786 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 */
3788 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003789ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003791 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003792 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003793
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003794 if (eap->cmdidx == CMD_lnext
3795 || eap->cmdidx == CMD_lNext
3796 || eap->cmdidx == CMD_lprevious
3797 || eap->cmdidx == CMD_lnfile
3798 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003799 || eap->cmdidx == CMD_lpfile
3800 || eap->cmdidx == CMD_ldo
3801 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003802 {
3803 qi = GET_LOC_LIST(curwin);
3804 if (qi == NULL)
3805 {
3806 EMSG(_(e_loclist));
3807 return;
3808 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003809 }
3810
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003811 if (eap->addr_count > 0 &&
3812 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3813 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3814 errornr = (int)eap->line2;
3815 else
3816 errornr = 1;
3817
3818 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3819 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003821 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3822 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003824 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3825 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 ? BACKWARD_FILE
3827 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003828 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829}
3830
3831/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003832 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003833 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 */
3835 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003836ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003838 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003839 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003840#ifdef FEAT_AUTOCMD
3841 char_u *au_name = NULL;
3842#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003843
3844 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003845 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003846 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003847
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003848#ifdef FEAT_AUTOCMD
3849 switch (eap->cmdidx)
3850 {
3851 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3852 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3853 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3854 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3855 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3856 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3857 default: break;
3858 }
3859 if (au_name != NULL)
3860 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3861#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003862#ifdef FEAT_BROWSE
3863 if (cmdmod.browse)
3864 {
3865 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3866 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3867 if (browse_file == NULL)
3868 return;
3869 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3870 vim_free(browse_file);
3871 }
3872 else
3873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003875 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003876
3877 /*
3878 * This function is used by the :cfile, :cgetfile and :caddfile
3879 * commands.
3880 * :cfile always creates a new quickfix list and jumps to the
3881 * first error.
3882 * :cgetfile creates a new quickfix list but doesn't jump to the
3883 * first error.
3884 * :caddfile adds to an existing quickfix list. If there is no
3885 * quickfix list then a new list is created.
3886 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003887 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003888 && eap->cmdidx != CMD_laddfile),
3889 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003890 && (eap->cmdidx == CMD_cfile
3891 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003892 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003893#ifdef FEAT_AUTOCMD
3894 if (au_name != NULL)
3895 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3896#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003897 if (wp != NULL)
3898 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003899 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003900 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003901
3902 else
3903 {
3904#ifdef FEAT_AUTOCMD
3905 if (au_name != NULL)
3906 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3907#endif
3908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909}
3910
3911/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003912 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003913 * ":vimgrepadd {pattern} file(s)"
3914 * ":lvimgrep {pattern} file(s)"
3915 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003916 */
3917 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003918ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003919{
Bram Moolenaar81695252004-12-29 20:58:21 +00003920 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003921 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003922 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003923 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003924 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003925 char_u *s;
3926 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003927 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003928 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003929#ifdef FEAT_AUTOCMD
3930 qfline_T *cur_qf_start;
3931#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003932 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003933 buf_T *buf;
3934 int duplicate_name = FALSE;
3935 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003936 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003937 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003938 buf_T *first_match_buf = NULL;
3939 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003940 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003941#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3942 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003943#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003944 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003945 int flags = 0;
3946 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003947 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003948 char_u *dirname_start = NULL;
3949 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003950 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003951#ifdef FEAT_AUTOCMD
3952 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003953
3954 switch (eap->cmdidx)
3955 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003956 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3957 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3958 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003959 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003960 case CMD_grep: au_name = (char_u *)"grep"; break;
3961 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3962 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3963 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003964 default: break;
3965 }
3966 if (au_name != NULL)
3967 {
3968 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3969 curbuf->b_fname, TRUE, curbuf);
3970 if (did_throw || force_abort)
3971 return;
3972 }
3973#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003974
Bram Moolenaar754b5602006-02-09 23:53:20 +00003975 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003976 || eap->cmdidx == CMD_lvimgrep
3977 || eap->cmdidx == CMD_lgrepadd
3978 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003979 {
3980 qi = ll_get_or_alloc_list(curwin);
3981 if (qi == NULL)
3982 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003983 }
3984
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003985 if (eap->addr_count > 0)
3986 tomatch = eap->line2;
3987 else
3988 tomatch = MAXLNUM;
3989
Bram Moolenaar81695252004-12-29 20:58:21 +00003990 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003991 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003992 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003993 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003994 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003995 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003996 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003997 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003998 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003999
4000 if (s != NULL && *s == NUL)
4001 {
4002 /* Pattern is empty, use last search pattern. */
4003 if (last_search_pat() == NULL)
4004 {
4005 EMSG(_(e_noprevre));
4006 goto theend;
4007 }
4008 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4009 }
4010 else
4011 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4012
Bram Moolenaar86b68352004-12-27 21:59:20 +00004013 if (regmatch.regprog == NULL)
4014 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004015 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004016 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004017
4018 p = skipwhite(p);
4019 if (*p == NUL)
4020 {
4021 EMSG(_("E683: File name missing or invalid pattern"));
4022 goto theend;
4023 }
4024
Bram Moolenaar754b5602006-02-09 23:53:20 +00004025 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004026 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004027 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004028 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004029 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004030
4031 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004032 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004033 goto theend;
4034 if (fcount == 0)
4035 {
4036 EMSG(_(e_nomatch));
4037 goto theend;
4038 }
4039
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004040 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4041 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004042 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004043 {
4044 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004045 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004046 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004047
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004048 /* Remember the current directory, because a BufRead autocommand that does
4049 * ":lcd %:p:h" changes the meaning of short path names. */
4050 mch_dirname(dirname_start, MAXPATHL);
4051
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004052#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004053 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004054 * changing the current quickfix list. */
4055 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4056#endif
4057
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004058 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004059 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004060 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004061 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004062 if (time(NULL) > seconds)
4063 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004064 /* Display the file name every second or so, show the user we are
4065 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004066 seconds = time(NULL);
4067 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004068 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004069 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004070 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004071 else
4072 {
4073 msg_outtrans(p);
4074 vim_free(p);
4075 }
4076 msg_clr_eos();
4077 msg_didout = FALSE; /* overwrite this message */
4078 msg_nowait = TRUE; /* don't wait for this message */
4079 msg_col = 0;
4080 out_flush();
4081 }
4082
Bram Moolenaar81695252004-12-29 20:58:21 +00004083 buf = buflist_findname_exp(fnames[fi]);
4084 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4085 {
4086 /* Remember that a buffer with this name already exists. */
4087 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004088 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004089 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004090
4091#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4092 /* Don't do Filetype autocommands to avoid loading syntax and
4093 * indent scripts, a great speed improvement. */
4094 save_ei = au_event_disable(",Filetype");
4095#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004096 /* Don't use modelines here, it's useless. */
4097 save_mls = p_mls;
4098 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004099
4100 /* Load file into a buffer, so that 'fileencoding' is detected,
4101 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004102 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004103
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004104 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004105#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4106 au_event_restore(save_ei);
4107#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004108 }
4109 else
4110 /* Use existing, loaded buffer. */
4111 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004112
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004113#ifdef FEAT_AUTOCMD
4114 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4115 {
4116 int idx;
4117
4118 /* Autocommands changed the quickfix list. Find the one we were
4119 * using and restore it. */
4120 for (idx = 0; idx < LISTCOUNT; ++idx)
4121 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4122 {
4123 qi->qf_curlist = idx;
4124 break;
4125 }
4126 if (idx == LISTCOUNT)
4127 {
4128 /* List cannot be found, create a new one. */
4129 qf_new_list(qi, *eap->cmdlinep);
4130 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4131 }
4132 }
4133#endif
4134
Bram Moolenaar81695252004-12-29 20:58:21 +00004135 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004136 {
4137 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004138 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004139 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004140 else
4141 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004142 /* Try for a match in all lines of the buffer.
4143 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004144 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004145 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4146 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004147 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004148 col = 0;
4149 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004150 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004151 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004152 /* Pass the buffer number so that it gets used even for a
4153 * dummy buffer, unless duplicate_name is set, then the
4154 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004155 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004156 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004157 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004158 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004159 ml_get_buf(buf,
4160 regmatch.startpos[0].lnum + lnum, FALSE),
4161 regmatch.startpos[0].lnum + lnum,
4162 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004163 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004164 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004165 0, /* nr */
4166 0, /* type */
4167 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004168 ) == FAIL)
4169 {
4170 got_int = TRUE;
4171 break;
4172 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004173 found_match = TRUE;
4174 if (--tomatch == 0)
4175 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004176 if ((flags & VGR_GLOBAL) == 0
4177 || regmatch.endpos[0].lnum > 0)
4178 break;
4179 col = regmatch.endpos[0].col
4180 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004181 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004182 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004183 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004184 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004185 if (got_int)
4186 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004187 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004188#ifdef FEAT_AUTOCMD
4189 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4190#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004191
4192 if (using_dummy)
4193 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004194 if (found_match && first_match_buf == NULL)
4195 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004196 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004197 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004198 /* Never keep a dummy buffer if there is another buffer
4199 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004200 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004201 buf = NULL;
4202 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004203 else if (!cmdmod.hide
4204 || buf->b_p_bh[0] == 'u' /* "unload" */
4205 || buf->b_p_bh[0] == 'w' /* "wipe" */
4206 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004207 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004208 /* When no match was found we don't need to remember the
4209 * buffer, wipe it out. If there was a match and it
4210 * wasn't the first one or we won't jump there: only
4211 * unload the buffer.
4212 * Ignore 'hidden' here, because it may lead to having too
4213 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004214 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004215 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004216 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004217 buf = NULL;
4218 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004219 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004220 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004221 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004222 /* Keeping the buffer, remove the dummy flag. */
4223 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004224 buf = NULL;
4225 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004226 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004227
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004228 if (buf != NULL)
4229 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004230 /* Keeping the buffer, remove the dummy flag. */
4231 buf->b_flags &= ~BF_DUMMY;
4232
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004233 /* If the buffer is still loaded we need to use the
4234 * directory we jumped to below. */
4235 if (buf == first_match_buf
4236 && target_dir == NULL
4237 && STRCMP(dirname_start, dirname_now) != 0)
4238 target_dir = vim_strsave(dirname_now);
4239
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004240 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004241 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004242 * need to be done (again). But not the window-local
4243 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004244 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004245#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004246 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4247 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004248#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004249 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004250 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004251 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004252 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004253 }
4254 }
4255
4256 FreeWild(fcount, fnames);
4257
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004258 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4259 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4260 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004261
4262#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004263 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004264#endif
4265
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004266#ifdef FEAT_AUTOCMD
4267 if (au_name != NULL)
4268 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4269 curbuf->b_fname, TRUE, curbuf);
4270#endif
4271
Bram Moolenaar86b68352004-12-27 21:59:20 +00004272 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004273 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004274 {
4275 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004276 {
4277 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004278 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004279 if (buf != curbuf)
4280 /* If we jumped to another buffer redrawing will already be
4281 * taken care of. */
4282 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004283
4284 /* Jump to the directory used after loading the buffer. */
4285 if (curbuf == first_match_buf && target_dir != NULL)
4286 {
4287 exarg_T ea;
4288
4289 ea.arg = target_dir;
4290 ea.cmdidx = CMD_lcd;
4291 ex_cd(&ea);
4292 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004293 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004294 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004295 else
4296 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004297
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004298 /* If we loaded a dummy buffer into the current window, the autocommands
4299 * may have messed up things, need to redraw and recompute folds. */
4300 if (redraw_for_dummy)
4301 {
4302#ifdef FEAT_FOLDING
4303 foldUpdateAll(curwin);
4304#else
4305 redraw_later(NOT_VALID);
4306#endif
4307 }
4308
Bram Moolenaar86b68352004-12-27 21:59:20 +00004309theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004310 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004311 vim_free(dirname_now);
4312 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004313 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004314 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004315}
4316
4317/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004318 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00004319 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004320 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
4321 * If "s" is not NULL terminate the pattern with a NUL.
4322 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00004323 */
4324 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004325skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004326{
4327 int c;
4328
4329 if (vim_isIDc(*p))
4330 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004331 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004332 if (s != NULL)
4333 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004334 p = skiptowhite(p);
4335 if (s != NULL && *p != NUL)
4336 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004337 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004338 else
4339 {
4340 /* ":vimgrep /pattern/[g][j] fname" */
4341 if (s != NULL)
4342 *s = p + 1;
4343 c = *p;
4344 p = skip_regexp(p + 1, c, TRUE, NULL);
4345 if (*p != c)
4346 return NULL;
4347
4348 /* Truncate the pattern. */
4349 if (s != NULL)
4350 *p = NUL;
4351 ++p;
4352
4353 /* Find the flags */
4354 while (*p == 'g' || *p == 'j')
4355 {
4356 if (flags != NULL)
4357 {
4358 if (*p == 'g')
4359 *flags |= VGR_GLOBAL;
4360 else
4361 *flags |= VGR_NOJUMP;
4362 }
4363 ++p;
4364 }
4365 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004366 return p;
4367}
4368
4369/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004370 * Restore current working directory to "dirname_start" if they differ, taking
4371 * into account whether it is set locally or globally.
4372 */
4373 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004374restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004375{
4376 char_u *dirname_now = alloc(MAXPATHL);
4377
4378 if (NULL != dirname_now)
4379 {
4380 mch_dirname(dirname_now, MAXPATHL);
4381 if (STRCMP(dirname_start, dirname_now) != 0)
4382 {
4383 /* If the directory has changed, change it back by building up an
4384 * appropriate ex command and executing it. */
4385 exarg_T ea;
4386
4387 ea.arg = dirname_start;
4388 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4389 ex_cd(&ea);
4390 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004391 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004392 }
4393}
4394
4395/*
4396 * Load file "fname" into a dummy buffer and return the buffer pointer,
4397 * placing the directory resulting from the buffer load into the
4398 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4399 * prior to calling this function. Restores directory to "dirname_start" prior
4400 * to returning, if autocmds or the 'autochdir' option have changed it.
4401 *
4402 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4403 * or wipe_dummy_buffer() later!
4404 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004405 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004406 */
4407 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004408load_dummy_buffer(
4409 char_u *fname,
4410 char_u *dirname_start, /* in: old directory */
4411 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004412{
4413 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004414 bufref_T newbufref;
4415 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004416 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004417 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004418
4419 /* Allocate a buffer without putting it in the buffer list. */
4420 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4421 if (newbuf == NULL)
4422 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004423 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004424
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004425 /* Init the options. */
4426 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4427
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004428 /* need to open the memfile before putting the buffer in a window */
4429 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004430 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004431 /* set curwin/curbuf to buf and save a few things */
4432 aucmd_prepbuf(&aco, newbuf);
4433
4434 /* Need to set the filename for autocommands. */
4435 (void)setfname(curbuf, fname, NULL, FALSE);
4436
Bram Moolenaar81695252004-12-29 20:58:21 +00004437 /* Create swap file now to avoid the ATTENTION message. */
4438 check_need_swap(TRUE);
4439
4440 /* Remove the "dummy" flag, otherwise autocommands may not
4441 * work. */
4442 curbuf->b_flags &= ~BF_DUMMY;
4443
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004444 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004445 if (readfile(fname, NULL,
4446 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4447 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004448 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004449 && !(curbuf->b_flags & BF_NEW))
4450 {
4451 failed = FALSE;
4452 if (curbuf != newbuf)
4453 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004454 /* Bloody autocommands changed the buffer! Can happen when
4455 * using netrw and editing a remote file. Use the current
4456 * buffer instead, delete the dummy one after restoring the
4457 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004458 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004459 newbuf = curbuf;
4460 }
4461 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004462
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004463 /* restore curwin/curbuf and a few other things */
4464 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004465 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4466 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004467
4468 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4469 * skip it. */
4470 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004471 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004472
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004473 /*
4474 * When autocommands/'autochdir' option changed directory: go back.
4475 * Let the caller know what the resulting dir was first, in case it is
4476 * important.
4477 */
4478 mch_dirname(resulting_dir, MAXPATHL);
4479 restore_start_dir(dirname_start);
4480
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004481 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004482 return NULL;
4483 if (failed)
4484 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004485 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004486 return NULL;
4487 }
4488 return newbuf;
4489}
4490
4491/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004492 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4493 * directory to "dirname_start" prior to returning, if autocmds or the
4494 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004495 */
4496 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004497wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004498{
4499 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004500 {
4501#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4502 cleanup_T cs;
4503
4504 /* Reset the error/interrupt/exception state here so that aborting()
4505 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4506 * work when got_int is set. */
4507 enter_cleanup(&cs);
4508#endif
4509
Bram Moolenaar81695252004-12-29 20:58:21 +00004510 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004511
4512#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4513 /* Restore the error/interrupt/exception state if not discarded by a
4514 * new aborting error, interrupt, or uncaught exception. */
4515 leave_cleanup(&cs);
4516#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004517 /* When autocommands/'autochdir' option changed directory: go back. */
4518 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004519 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004520}
4521
4522/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004523 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4524 * directory to "dirname_start" prior to returning, if autocmds or the
4525 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004526 */
4527 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004528unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004529{
4530 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004531 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004532 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004533
4534 /* When autocommands/'autochdir' option changed directory: go back. */
4535 restore_start_dir(dirname_start);
4536 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004537}
4538
Bram Moolenaar05159a02005-02-26 23:04:13 +00004539#if defined(FEAT_EVAL) || defined(PROTO)
4540/*
4541 * Add each quickfix error to list "list" as a dictionary.
4542 */
4543 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004544get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004545{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004546 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004547 dict_T *dict;
4548 char_u buf[2];
4549 qfline_T *qfp;
4550 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004551 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004552
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004553 if (wp != NULL)
4554 {
4555 qi = GET_LOC_LIST(wp);
4556 if (qi == NULL)
4557 return FAIL;
4558 }
4559
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004560 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004561 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004562 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004563
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004564 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4565 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004566 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004567 /* Handle entries with a non-existing buffer number. */
4568 bufnum = qfp->qf_fnum;
4569 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4570 bufnum = 0;
4571
Bram Moolenaar05159a02005-02-26 23:04:13 +00004572 if ((dict = dict_alloc()) == NULL)
4573 return FAIL;
4574 if (list_append_dict(list, dict) == FAIL)
4575 return FAIL;
4576
4577 buf[0] = qfp->qf_type;
4578 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004579 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004580 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4581 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4582 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4583 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004584 || dict_add_nr_str(dict, "pattern", 0L,
4585 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4586 || dict_add_nr_str(dict, "text", 0L,
4587 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004588 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4589 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4590 return FAIL;
4591
4592 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004593 if (qfp == NULL)
4594 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004595 }
4596 return OK;
4597}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004598
4599/*
4600 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004601 * of dictionaries. "title" will be copied to w:quickfix_title.
4602 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004603 */
4604 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004605set_errorlist(
4606 win_T *wp,
4607 list_T *list,
4608 int action,
4609 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004610{
4611 listitem_T *li;
4612 dict_T *d;
4613 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004614 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004615 long lnum;
4616 int col, nr;
4617 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004618#ifdef FEAT_WINDOWS
4619 qfline_T *old_last = NULL;
4620#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004621 int valid, status;
4622 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004623 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004624 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004625
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004626 if (wp != NULL)
4627 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004628 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004629 if (qi == NULL)
4630 return FAIL;
4631 }
4632
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004633 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004634 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004635 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004636#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004637 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4638 /* Adding to existing list, use last entry. */
4639 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004640#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004641 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004642 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004643 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004644 qf_store_title(qi, title);
4645 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004646
4647 for (li = list->lv_first; li != NULL; li = li->li_next)
4648 {
4649 if (li->li_tv.v_type != VAR_DICT)
4650 continue; /* Skip non-dict items */
4651
4652 d = li->li_tv.vval.v_dict;
4653 if (d == NULL)
4654 continue;
4655
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004656 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004657 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4658 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4659 col = (int)get_dict_number(d, (char_u *)"col");
4660 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4661 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004662 type = get_dict_string(d, (char_u *)"type", TRUE);
4663 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4664 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004665 if (text == NULL)
4666 text = vim_strsave((char_u *)"");
4667
4668 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004669 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004670 valid = FALSE;
4671
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004672 /* Mark entries with non-existing buffer number as not valid. Give the
4673 * error message only once. */
4674 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4675 {
4676 if (!did_bufnr_emsg)
4677 {
4678 did_bufnr_emsg = TRUE;
4679 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4680 }
4681 valid = FALSE;
4682 bufnum = 0;
4683 }
4684
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004685 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004686 NULL, /* dir */
4687 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004688 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004689 text,
4690 lnum,
4691 col,
4692 vcol, /* vis_col */
4693 pattern, /* search pattern */
4694 nr,
4695 type == NULL ? NUL : *type,
4696 valid);
4697
4698 vim_free(filename);
4699 vim_free(pattern);
4700 vim_free(text);
4701 vim_free(type);
4702
4703 if (status == FAIL)
4704 {
4705 retval = FAIL;
4706 break;
4707 }
4708 }
4709
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004710 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004711 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004712 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4713 else
4714 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004715 if (action != 'a') {
4716 qi->qf_lists[qi->qf_curlist].qf_ptr =
4717 qi->qf_lists[qi->qf_curlist].qf_start;
4718 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4719 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4720 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004721
4722#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004723 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004724 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004725#endif
4726
4727 return retval;
4728}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004729#endif
4730
Bram Moolenaar81695252004-12-29 20:58:21 +00004731/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004732 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004733 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004734 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004735 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004736 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004737 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004738 */
4739 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004740ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004741{
4742 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004743 qf_info_T *qi = &ql_info;
4744
Bram Moolenaardb552d602006-03-23 22:59:57 +00004745 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4746 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004747 {
4748 qi = ll_get_or_alloc_list(curwin);
4749 if (qi == NULL)
4750 return;
4751 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004752
4753 if (*eap->arg == NUL)
4754 buf = curbuf;
4755 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4756 buf = buflist_findnr(atoi((char *)eap->arg));
4757 if (buf == NULL)
4758 EMSG(_(e_invarg));
4759 else if (buf->b_ml.ml_mfp == NULL)
4760 EMSG(_("E681: Buffer is not loaded"));
4761 else
4762 {
4763 if (eap->addr_count == 0)
4764 {
4765 eap->line1 = 1;
4766 eap->line2 = buf->b_ml.ml_line_count;
4767 }
4768 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4769 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4770 EMSG(_(e_invrange));
4771 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004772 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004773 char_u *qf_title = *eap->cmdlinep;
4774
4775 if (buf->b_sfname)
4776 {
4777 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4778 (char *)qf_title, (char *)buf->b_sfname);
4779 qf_title = IObuff;
4780 }
4781
Bram Moolenaardb552d602006-03-23 22:59:57 +00004782 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4783 (eap->cmdidx != CMD_caddbuffer
4784 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004785 eap->line1, eap->line2,
4786 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004787 && (eap->cmdidx == CMD_cbuffer
4788 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004789 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4790 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004791 }
4792}
4793
Bram Moolenaar1e015462005-09-25 22:16:38 +00004794#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004795/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004796 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4797 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004798 */
4799 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004800ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004801{
4802 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004803 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004804
Bram Moolenaardb552d602006-03-23 22:59:57 +00004805 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4806 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004807 {
4808 qi = ll_get_or_alloc_list(curwin);
4809 if (qi == NULL)
4810 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004811 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004812
Bram Moolenaar4770d092006-01-12 23:22:24 +00004813 /* Evaluate the expression. When the result is a string or a list we can
4814 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004815 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004816 if (tv != NULL)
4817 {
4818 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4819 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4820 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004821 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004822 (eap->cmdidx != CMD_caddexpr
4823 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004824 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004825 && (eap->cmdidx == CMD_cexpr
4826 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004827 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004828 }
4829 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004830 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004831 free_tv(tv);
4832 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004833}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004834#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004835
4836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 * ":helpgrep {pattern}"
4838 */
4839 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004840ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
4842 regmatch_T regmatch;
4843 char_u *save_cpo;
4844 char_u *p;
4845 int fcount;
4846 char_u **fnames;
4847 FILE *fd;
4848 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004850#ifdef FEAT_MULTI_LANG
4851 char_u *lang;
4852#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004853 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004854 int new_qi = FALSE;
4855 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004856#ifdef FEAT_AUTOCMD
4857 char_u *au_name = NULL;
4858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004860#ifdef FEAT_MULTI_LANG
4861 /* Check for a specified language */
4862 lang = check_help_lang(eap->arg);
4863#endif
4864
Bram Moolenaar73633f82012-01-20 13:39:07 +01004865#ifdef FEAT_AUTOCMD
4866 switch (eap->cmdidx)
4867 {
4868 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4869 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4870 default: break;
4871 }
4872 if (au_name != NULL)
4873 {
4874 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4875 curbuf->b_fname, TRUE, curbuf);
4876 if (did_throw || force_abort)
4877 return;
4878 }
4879#endif
4880
4881 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4882 save_cpo = p_cpo;
4883 p_cpo = empty_option;
4884
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004885 if (eap->cmdidx == CMD_lhelpgrep)
4886 {
4887 /* Find an existing help window */
4888 FOR_ALL_WINDOWS(wp)
4889 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4890 break;
4891
4892 if (wp == NULL) /* Help window not found */
4893 qi = NULL;
4894 else
4895 qi = wp->w_llist;
4896
4897 if (qi == NULL)
4898 {
4899 /* Allocate a new location list for help text matches */
4900 if ((qi = ll_new_list()) == NULL)
4901 return;
4902 new_qi = TRUE;
4903 }
4904 }
4905
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4907 regmatch.rm_ic = FALSE;
4908 if (regmatch.regprog != NULL)
4909 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004910#ifdef FEAT_MBYTE
4911 vimconv_T vc;
4912
4913 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4914 * differs. */
4915 vc.vc_type = CONV_NONE;
4916 if (!enc_utf8)
4917 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4918#endif
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004921 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 /* Go through all directories in 'runtimepath' */
4924 p = p_rtp;
4925 while (*p != NUL && !got_int)
4926 {
4927 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4928
4929 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4930 add_pathsep(NameBuff);
4931 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4932 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4933 &fnames, EW_FILE|EW_SILENT) == OK
4934 && fcount > 0)
4935 {
4936 for (fi = 0; fi < fcount && !got_int; ++fi)
4937 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004938#ifdef FEAT_MULTI_LANG
4939 /* Skip files for a different language. */
4940 if (lang != NULL
4941 && STRNICMP(lang, fnames[fi]
4942 + STRLEN(fnames[fi]) - 3, 2) != 0
4943 && !(STRNICMP(lang, "en", 2) == 0
4944 && STRNICMP("txt", fnames[fi]
4945 + STRLEN(fnames[fi]) - 3, 3) == 0))
4946 continue;
4947#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004948 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 if (fd != NULL)
4950 {
4951 lnum = 1;
4952 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4953 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004954 char_u *line = IObuff;
4955#ifdef FEAT_MBYTE
4956 /* Convert a line if 'encoding' is not utf-8 and
4957 * the line contains a non-ASCII character. */
4958 if (vc.vc_type != CONV_NONE
4959 && has_non_ascii(IObuff)) {
4960 line = string_convert(&vc, IObuff, NULL);
4961 if (line == NULL)
4962 line = IObuff;
4963 }
4964#endif
4965
4966 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004968 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004971 while (l > 0 && line[l - 1] <= ' ')
4972 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004974 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 NULL, /* dir */
4976 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004977 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004978 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004980 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004981 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004982 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004983 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 0, /* nr */
4985 1, /* type */
4986 TRUE /* valid */
4987 ) == FAIL)
4988 {
4989 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004990#ifdef FEAT_MBYTE
4991 if (line != IObuff)
4992 vim_free(line);
4993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 break;
4995 }
4996 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004997#ifdef FEAT_MBYTE
4998 if (line != IObuff)
4999 vim_free(line);
5000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 ++lnum;
5002 line_breakcheck();
5003 }
5004 fclose(fd);
5005 }
5006 }
5007 FreeWild(fcount, fnames);
5008 }
5009 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005010
Bram Moolenaar473de612013-06-08 18:19:48 +02005011 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005012#ifdef FEAT_MBYTE
5013 if (vc.vc_type != CONV_NONE)
5014 convert_setup(&vc, NULL, NULL);
5015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005017 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5018 qi->qf_lists[qi->qf_curlist].qf_ptr =
5019 qi->qf_lists[qi->qf_curlist].qf_start;
5020 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005023 if (p_cpo == empty_option)
5024 p_cpo = save_cpo;
5025 else
5026 /* Darn, some plugin changed the value. */
5027 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
5029#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005030 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031#endif
5032
Bram Moolenaar73633f82012-01-20 13:39:07 +01005033#ifdef FEAT_AUTOCMD
5034 if (au_name != NULL)
5035 {
5036 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5037 curbuf->b_fname, TRUE, curbuf);
5038 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
5039 /* autocommands made "qi" invalid */
5040 return;
5041 }
5042#endif
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005045 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005046 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005047 else
5048 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005049
5050 if (eap->cmdidx == CMD_lhelpgrep)
5051 {
5052 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005053 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005054 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5055 {
5056 if (new_qi)
5057 ll_free_all(&qi);
5058 }
5059 else if (curwin->w_llist == NULL)
5060 curwin->w_llist = qi;
5061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062}
5063
5064#endif /* FEAT_QUICKFIX */