blob: 689897554cdb4de11891c13370380ec06d139145 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaard12f5c12006-01-25 22:10:52 +000050typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000051{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000052 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020053 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_ptr; /* pointer to the current error */
55 int qf_count; /* number of errors (0 means no error list) */
56 int qf_index; /* current index in the error list */
57 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020058 char_u *qf_title; /* title derived from the command that created
59 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000060} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaard12f5c12006-01-25 22:10:52 +000062struct qf_info_S
63{
64 /*
65 * Count of references to this list. Used only for location lists.
66 * When a location list window reference this list, qf_refcount
67 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
68 * reaches 0, the list is freed.
69 */
70 int qf_refcount;
71 int qf_listcount; /* current number of lists */
72 int qf_curlist; /* current error list */
73 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar361c8f02016-07-02 15:41:47 +020074
75 int qf_dir_curlist; /* error list for qf_dir_stack */
76 struct dir_stack_T *qf_dir_stack;
77 char_u *qf_directory;
78 struct dir_stack_T *qf_file_stack;
79 char_u *qf_currfile;
80 int qf_multiline;
81 int qf_multiignore;
82 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000083};
84
85static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaar68b76a62005-03-25 21:53:48 +000087#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89/*
90 * Structure used to hold the info of one part of 'errorformat'
91 */
Bram Moolenaar01265852006-03-20 21:50:15 +000092typedef struct efm_S efm_T;
93struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000094{
95 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000096 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
98 char_u prefix; /* prefix of this format line: */
99 /* 'D' enter directory */
100 /* 'X' leave directory */
101 /* 'A' start of multi-line message */
102 /* 'E' error message */
103 /* 'W' warning message */
104 /* 'I' informational message */
105 /* 'C' continuation line */
106 /* 'Z' end of multi-line message */
107 /* 'G' general, unspecific message */
108 /* 'P' push file (partial) message */
109 /* 'Q' pop/quit file (partial) message */
110 /* 'O' overread (partial) message */
111 char_u flags; /* additional flags given in prefix */
112 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000113 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115};
116
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title);
118static void qf_store_title(qf_info_T *qi, char_u *title);
119static void qf_new_list(qf_info_T *qi, char_u *qf_title);
120static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200121static int qf_add_entry(qf_info_T *qi, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100122static qf_info_T *ll_new_list(void);
123static void qf_msg(qf_info_T *qi);
124static void qf_free(qf_info_T *qi, int idx);
125static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200126static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
127static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200129static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
131static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
134static int is_qf_win(win_T *win, qf_info_T *qi);
135static win_T *qf_find_win(qf_info_T *qi);
136static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200137static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200139static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static char_u *get_mef_name(void);
142static void restore_start_dir(char_u *dirname_start);
143static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
144static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
145static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
146static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000148/* Quickfix window check helper macro */
149#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
150/* Location list window check helper macro */
151#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
152/*
153 * Return location list for window 'wp'
154 * For location list window, return the referenced location list
155 */
156#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000159 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200160 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 * Return -1 for error, number of errors for success.
162 */
163 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100164qf_init(
165 win_T *wp,
166 char_u *efile,
167 char_u *errorformat,
168 int newlist, /* TRUE: start a new error list */
169 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000171 qf_info_T *qi = &ql_info;
172
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000173 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000174 {
175 qi = ll_get_or_alloc_list(wp);
176 if (qi == NULL)
177 return FAIL;
178 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000179
180 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200181 (linenr_T)0, (linenr_T)0,
182 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000183}
184
185/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200186 * Maximum number of bytes allowed per line while reading a errorfile.
187 */
188#define LINE_MAXLEN 4096
189
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200190static struct fmtpattern
191{
192 char_u convchar;
193 char *pattern;
194} fmt_pat[FMT_PATTERNS] =
195 {
196 {'f', ".\\+"}, /* only used when at end */
197 {'n', "\\d\\+"},
198 {'l', "\\d\\+"},
199 {'c', "\\d\\+"},
200 {'t', "."},
201 {'m', ".\\+"},
202 {'r', ".*"},
203 {'p', "[- .]*"},
204 {'v', "\\d\\+"},
205 {'s', ".\\+"}
206 };
207
208/*
209 * Converts a 'errorformat' string to regular expression pattern
210 */
211 static int
212efm_to_regpat(
213 char_u *efm,
214 int len,
215 efm_T *fmt_ptr,
216 char_u *regpat,
217 char_u *errmsg)
218{
219 char_u *ptr;
220 char_u *efmp;
221 char_u *srcptr;
222 int round;
223 int idx = 0;
224
225 /*
226 * Build regexp pattern from current 'errorformat' option
227 */
228 ptr = regpat;
229 *ptr++ = '^';
230 round = 0;
231 for (efmp = efm; efmp < efm + len; ++efmp)
232 {
233 if (*efmp == '%')
234 {
235 ++efmp;
236 for (idx = 0; idx < FMT_PATTERNS; ++idx)
237 if (fmt_pat[idx].convchar == *efmp)
238 break;
239 if (idx < FMT_PATTERNS)
240 {
241 if (fmt_ptr->addr[idx])
242 {
243 sprintf((char *)errmsg,
244 _("E372: Too many %%%c in format string"), *efmp);
245 EMSG(errmsg);
246 return -1;
247 }
248 if ((idx
249 && idx < 6
250 && vim_strchr((char_u *)"DXOPQ",
251 fmt_ptr->prefix) != NULL)
252 || (idx == 6
253 && vim_strchr((char_u *)"OPQ",
254 fmt_ptr->prefix) == NULL))
255 {
256 sprintf((char *)errmsg,
257 _("E373: Unexpected %%%c in format string"), *efmp);
258 EMSG(errmsg);
259 return -1;
260 }
261 fmt_ptr->addr[idx] = (char_u)++round;
262 *ptr++ = '\\';
263 *ptr++ = '(';
264#ifdef BACKSLASH_IN_FILENAME
265 if (*efmp == 'f')
266 {
267 /* Also match "c:" in the file name, even when
268 * checking for a colon next: "%f:".
269 * "\%(\a:\)\=" */
270 STRCPY(ptr, "\\%(\\a:\\)\\=");
271 ptr += 10;
272 }
273#endif
274 if (*efmp == 'f' && efmp[1] != NUL)
275 {
276 if (efmp[1] != '\\' && efmp[1] != '%')
277 {
278 /* A file name may contain spaces, but this isn't
279 * in "\f". For "%f:%l:%m" there may be a ":" in
280 * the file name. Use ".\{-1,}x" instead (x is
281 * the next character), the requirement that :999:
282 * follows should work. */
283 STRCPY(ptr, ".\\{-1,}");
284 ptr += 7;
285 }
286 else
287 {
288 /* File name followed by '\\' or '%': include as
289 * many file name chars as possible. */
290 STRCPY(ptr, "\\f\\+");
291 ptr += 4;
292 }
293 }
294 else
295 {
296 srcptr = (char_u *)fmt_pat[idx].pattern;
297 while ((*ptr = *srcptr++) != NUL)
298 ++ptr;
299 }
300 *ptr++ = '\\';
301 *ptr++ = ')';
302 }
303 else if (*efmp == '*')
304 {
305 if (*++efmp == '[' || *efmp == '\\')
306 {
307 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
308 {
309 if (efmp[1] == '^')
310 *ptr++ = *++efmp;
311 if (efmp < efm + len)
312 {
313 *ptr++ = *++efmp; /* could be ']' */
314 while (efmp < efm + len
315 && (*ptr++ = *++efmp) != ']')
316 /* skip */;
317 if (efmp == efm + len)
318 {
319 EMSG(_("E374: Missing ] in format string"));
320 return -1;
321 }
322 }
323 }
324 else if (efmp < efm + len) /* %*\D, %*\s etc. */
325 *ptr++ = *++efmp;
326 *ptr++ = '\\';
327 *ptr++ = '+';
328 }
329 else
330 {
331 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
332 sprintf((char *)errmsg,
333 _("E375: Unsupported %%%c in format string"), *efmp);
334 EMSG(errmsg);
335 return -1;
336 }
337 }
338 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
339 *ptr++ = *efmp; /* regexp magic characters */
340 else if (*efmp == '#')
341 *ptr++ = '*';
342 else if (*efmp == '>')
343 fmt_ptr->conthere = TRUE;
344 else if (efmp == efm + 1) /* analyse prefix */
345 {
346 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
347 fmt_ptr->flags = *efmp++;
348 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
349 fmt_ptr->prefix = *efmp;
350 else
351 {
352 sprintf((char *)errmsg,
353 _("E376: Invalid %%%c in format string prefix"), *efmp);
354 EMSG(errmsg);
355 return -1;
356 }
357 }
358 else
359 {
360 sprintf((char *)errmsg,
361 _("E377: Invalid %%%c in format string"), *efmp);
362 EMSG(errmsg);
363 return -1;
364 }
365 }
366 else /* copy normal character */
367 {
368 if (*efmp == '\\' && efmp + 1 < efm + len)
369 ++efmp;
370 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
371 *ptr++ = '\\'; /* escape regexp atoms */
372 if (*efmp)
373 *ptr++ = *efmp;
374 }
375 }
376 *ptr++ = '$';
377 *ptr = NUL;
378
379 return 0;
380}
381
382 static void
383free_efm_list(efm_T **efm_first)
384{
385 efm_T *efm_ptr;
386
387 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
388 {
389 *efm_first = efm_ptr->next;
390 vim_regfree(efm_ptr->prog);
391 vim_free(efm_ptr);
392 }
393}
394
395/* Parse 'errorformat' option */
396 static efm_T *
397parse_efm_option(char_u *efm)
398{
399 char_u *errmsg = NULL;
400 int errmsglen;
401 efm_T *fmt_ptr = NULL;
402 efm_T *fmt_first = NULL;
403 efm_T *fmt_last = NULL;
404 char_u *fmtstr = NULL;
405 int len;
406 int i;
407 int round;
408
409 errmsglen = CMDBUFFSIZE + 1;
410 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
411 if (errmsg == NULL)
412 goto parse_efm_end;
413
414 /*
415 * Get some space to modify the format string into.
416 */
417 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
418 for (round = FMT_PATTERNS; round > 0; )
419 i += (int)STRLEN(fmt_pat[--round].pattern);
420#ifdef COLON_IN_FILENAME
421 i += 12; /* "%f" can become twelve chars longer */
422#else
423 i += 2; /* "%f" can become two chars longer */
424#endif
425 if ((fmtstr = alloc(i)) == NULL)
426 goto parse_efm_error;
427
428 while (efm[0] != NUL)
429 {
430 /*
431 * Allocate a new eformat structure and put it at the end of the list
432 */
433 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
434 if (fmt_ptr == NULL)
435 goto parse_efm_error;
436 if (fmt_first == NULL) /* first one */
437 fmt_first = fmt_ptr;
438 else
439 fmt_last->next = fmt_ptr;
440 fmt_last = fmt_ptr;
441
442 /*
443 * Isolate one part in the 'errorformat' option
444 */
445 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
446 if (efm[len] == '\\' && efm[len + 1] != NUL)
447 ++len;
448
449 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
450 goto parse_efm_error;
451 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
452 goto parse_efm_error;
453 /*
454 * Advance to next part
455 */
456 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
457 }
458
459 if (fmt_first == NULL) /* nothing found */
460 EMSG(_("E378: 'errorformat' contains no pattern"));
461
462 goto parse_efm_end;
463
464parse_efm_error:
465 free_efm_list(&fmt_first);
466
467parse_efm_end:
468 vim_free(fmtstr);
469 vim_free(errmsg);
470
471 return fmt_first;
472}
473
Bram Moolenaare0d37972016-07-15 22:36:01 +0200474enum {
475 QF_FAIL = 0,
476 QF_OK = 1,
477 QF_END_OF_INPUT = 2,
478 QF_NOMEM = 3
479};
480
481typedef struct {
482 char_u *linebuf;
483 int linelen;
484 char_u *growbuf;
485 int growbufsiz;
486 FILE *fd;
487 typval_T *tv;
488 char_u *p_str;
489 listitem_T *p_li;
490 buf_T *buf;
491 linenr_T buflnum;
492 linenr_T lnumlast;
493} qfstate_T;
494
495 static char_u *
496qf_grow_linebuf(qfstate_T *state, int newsz)
497{
498 /*
499 * If the line exceeds LINE_MAXLEN exclude the last
500 * byte since it's not a NL character.
501 */
502 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
503 if (state->growbuf == NULL)
504 {
505 state->growbuf = alloc(state->linelen + 1);
506 if (state->growbuf == NULL)
507 return NULL;
508 state->growbufsiz = state->linelen;
509 }
510 else if (state->linelen > state->growbufsiz)
511 {
512 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
513 if (state->growbuf == NULL)
514 return NULL;
515 state->growbufsiz = state->linelen;
516 }
517 return state->growbuf;
518}
519
520/*
521 * Get the next string (separated by newline) from state->p_str.
522 */
523 static int
524qf_get_next_str_line(qfstate_T *state)
525{
526 /* Get the next line from the supplied string */
527 char_u *p_str = state->p_str;
528 char_u *p;
529 int len;
530
531 if (*p_str == NUL) /* Reached the end of the string */
532 return QF_END_OF_INPUT;
533
534 p = vim_strchr(p_str, '\n');
535 if (p != NULL)
536 len = (int)(p - p_str) + 1;
537 else
538 len = (int)STRLEN(p_str);
539
540 if (len > IOSIZE - 2)
541 {
542 state->linebuf = qf_grow_linebuf(state, len);
543 if (state->linebuf == NULL)
544 return QF_NOMEM;
545 }
546 else
547 {
548 state->linebuf = IObuff;
549 state->linelen = len;
550 }
551 vim_strncpy(state->linebuf, p_str, state->linelen);
552
553 /*
554 * Increment using len in order to discard the rest of the
555 * line if it exceeds LINE_MAXLEN.
556 */
557 p_str += len;
558 state->p_str = p_str;
559
560 return QF_OK;
561}
562
563/*
564 * Get the next string from state->p_Li.
565 */
566 static int
567qf_get_next_list_line(qfstate_T *state)
568{
569 listitem_T *p_li = state->p_li;
570 int len;
571
572 while (p_li != NULL
573 && (p_li->li_tv.v_type != VAR_STRING
574 || p_li->li_tv.vval.v_string == NULL))
575 p_li = p_li->li_next; /* Skip non-string items */
576
577 if (p_li == NULL) /* End of the list */
578 {
579 state->p_li = NULL;
580 return QF_END_OF_INPUT;
581 }
582
583 len = (int)STRLEN(p_li->li_tv.vval.v_string);
584 if (len > IOSIZE - 2)
585 {
586 state->linebuf = qf_grow_linebuf(state, len);
587 if (state->linebuf == NULL)
588 return QF_NOMEM;
589 }
590 else
591 {
592 state->linebuf = IObuff;
593 state->linelen = len;
594 }
595
596 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
597
598 state->p_li = p_li->li_next; /* next item */
599 return QF_OK;
600}
601
602/*
603 * Get the next string from state->buf.
604 */
605 static int
606qf_get_next_buf_line(qfstate_T *state)
607{
608 char_u *p_buf = NULL;
609 int len;
610
611 /* Get the next line from the supplied buffer */
612 if (state->buflnum > state->lnumlast)
613 return QF_END_OF_INPUT;
614
615 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
616 state->buflnum += 1;
617
618 len = (int)STRLEN(p_buf);
619 if (len > IOSIZE - 2)
620 {
621 state->linebuf = qf_grow_linebuf(state, len);
622 if (state->linebuf == NULL)
623 return QF_NOMEM;
624 }
625 else
626 {
627 state->linebuf = IObuff;
628 state->linelen = len;
629 }
630 vim_strncpy(state->linebuf, p_buf, state->linelen);
631
632 return QF_OK;
633}
634
635/*
636 * Get the next string from file state->fd.
637 */
638 static int
639qf_get_next_file_line(qfstate_T *state)
640{
641 int discard;
642 int growbuflen;
643
644 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
645 return QF_END_OF_INPUT;
646
647 discard = FALSE;
648 state->linelen = (int)STRLEN(IObuff);
649 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'
650#ifdef USE_CRNL
651 || IObuff[state->linelen - 1] == '\r'
652#endif
653 ))
654 {
655 /*
656 * The current line exceeds IObuff, continue reading using
657 * growbuf until EOL or LINE_MAXLEN bytes is read.
658 */
659 if (state->growbuf == NULL)
660 {
661 state->growbufsiz = 2 * (IOSIZE - 1);
662 state->growbuf = alloc(state->growbufsiz);
663 if (state->growbuf == NULL)
664 return QF_NOMEM;
665 }
666
667 /* Copy the read part of the line, excluding null-terminator */
668 memcpy(state->growbuf, IObuff, IOSIZE - 1);
669 growbuflen = state->linelen;
670
671 for (;;)
672 {
673 if (fgets((char *)state->growbuf + growbuflen,
674 state->growbufsiz - growbuflen, state->fd) == NULL)
675 break;
676 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
677 growbuflen += state->linelen;
678 if ((state->growbuf)[growbuflen - 1] == '\n'
679#ifdef USE_CRNL
680 || (state->growbuf)[growbuflen - 1] == '\r'
681#endif
682 )
683 break;
684 if (state->growbufsiz == LINE_MAXLEN)
685 {
686 discard = TRUE;
687 break;
688 }
689
690 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
691 ? 2 * state->growbufsiz : LINE_MAXLEN;
692 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
693 if (state->growbuf == NULL)
694 return QF_NOMEM;
695 }
696
697 while (discard)
698 {
699 /*
700 * The current line is longer than LINE_MAXLEN, continue
701 * reading but discard everything until EOL or EOF is
702 * reached.
703 */
704 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
705 || (int)STRLEN(IObuff) < IOSIZE - 1
706 || IObuff[IOSIZE - 1] == '\n'
707#ifdef USE_CRNL
708 || IObuff[IOSIZE - 1] == '\r'
709#endif
710 )
711 break;
712 }
713
714 state->linebuf = state->growbuf;
715 state->linelen = growbuflen;
716 }
717 else
718 state->linebuf = IObuff;
719
720 return QF_OK;
721}
722
723/*
724 * Get the next string from a file/buffer/list/string.
725 */
726 static int
727qf_get_nextline(qfstate_T *state)
728{
729 int status = QF_FAIL;
730
731 if (state->fd == NULL)
732 {
733 if (state->tv != NULL)
734 {
735 if (state->tv->v_type == VAR_STRING)
736 /* Get the next line from the supplied string */
737 status = qf_get_next_str_line(state);
738 else if (state->tv->v_type == VAR_LIST)
739 /* Get the next line from the supplied list */
740 status = qf_get_next_list_line(state);
741 }
742 else
743 /* Get the next line from the supplied buffer */
744 status = qf_get_next_buf_line(state);
745 }
746 else
747 /* Get the next line from the supplied file */
748 status = qf_get_next_file_line(state);
749
750 if (status != QF_OK)
751 return status;
752
753 /* remove newline/CR from the line */
754 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
755 state->linebuf[state->linelen - 1] = NUL;
756#ifdef USE_CRNL
757 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\r')
758 state->linebuf[state->linelen - 1] = NUL;
759#endif
760
761#ifdef FEAT_MBYTE
762 remove_bom(state->linebuf);
763#endif
764
765 return QF_OK;
766}
767
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200768/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000769 * Read the errorfile "efile" into memory, line by line, building the error
770 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +0200771 * Alternative: when "efile" is NULL read errors from buffer "buf".
772 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000773 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200774 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
775 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000776 * Return -1 for error, number of errors for success.
777 */
778 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100779qf_init_ext(
780 qf_info_T *qi,
781 char_u *efile,
782 buf_T *buf,
783 typval_T *tv,
784 char_u *errorformat,
785 int newlist, /* TRUE: start a new error list */
786 linenr_T lnumfirst, /* first line number to use */
787 linenr_T lnumlast, /* last line number to use */
788 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000789{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 char_u *namebuf;
791 char_u *errmsg;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200792 int errmsglen;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000793 char_u *pattern;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200794 qfstate_T state = {NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +0200795 NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 int type = 0;
799 int valid;
800 long lnum = 0L;
801 int enr = 0;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200802#ifdef FEAT_WINDOWS
803 qfline_T *old_last = NULL;
804#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200805 static efm_T *fmt_first = NULL;
Bram Moolenaar01265852006-03-20 21:50:15 +0000806 efm_T *fmt_ptr;
807 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200809 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 int len;
812 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200815 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 char_u *tail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100819 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200820 errmsglen = CMDBUFFSIZE + 1;
821 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100822 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000823 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 goto qf_init_end;
825
Bram Moolenaare0d37972016-07-15 22:36:01 +0200826 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
828 EMSG2(_(e_openerrf), efile);
829 goto qf_init_end;
830 }
831
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000832 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100834 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200835#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000836 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +0200837 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200838 /* Adding to existing list, use last entry. */
839 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200840 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
843/*
844 * Each part of the format string is copied and modified from errorformat to
845 * regex prog. Only a few % characters are allowed.
846 */
847 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000848 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000849 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 else
851 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200853 /*
854 * If we are not adding or adding to another list: clear the state.
855 */
856 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
857 {
858 qi->qf_dir_curlist = qi->qf_curlist;
859 qf_clean_dir_stack(&qi->qf_dir_stack);
860 qi->qf_directory = NULL;
861 qf_clean_dir_stack(&qi->qf_file_stack);
862 qi->qf_currfile = NULL;
863 qi->qf_multiline = FALSE;
864 qi->qf_multiignore = FALSE;
865 qi->qf_multiscan = FALSE;
866 }
867
868 /*
869 * If the errorformat didn't change between calls, then reuse the
870 * previously parsed values.
871 */
872 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
873 {
874 /* free the previously parsed data */
875 vim_free(last_efm);
876 last_efm = NULL;
877 free_efm_list(&fmt_first);
878
879 /* parse the current 'efm' */
880 fmt_first = parse_efm_option(efm);
881 if (fmt_first != NULL)
882 last_efm = vim_strsave(efm);
883 }
884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 /*
889 * got_int is reset here, because it was probably set when killing the
890 * ":make" command, but we still want to read the errorfile then.
891 */
892 got_int = FALSE;
893
894 /* Always ignore case when looking for a matching error. */
895 regmatch.rm_ic = TRUE;
896
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000897 if (tv != NULL)
898 {
899 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200900 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000901 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200902 state.p_li = tv->vval.v_list->lv_first;
903 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000904 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200905 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +0200906 state.buflnum = lnumfirst;
907 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000908
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 /*
910 * Read the lines in the error file one by one.
911 * Try to recognize one of the error formats in each line.
912 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000913 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200915 /* Get the next line from a file/buffer/list/string */
916 status = qf_get_nextline(&state);
917 if (status == QF_NOMEM) /* memory alloc failure */
918 goto qf_init_end;
919 if (status == QF_END_OF_INPUT) /* end of input */
920 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921
Bram Moolenaar01265852006-03-20 21:50:15 +0000922 /* If there was no %> item start at the first pattern */
923 if (fmt_start == NULL)
924 fmt_ptr = fmt_first;
925 else
926 {
927 fmt_ptr = fmt_start;
928 fmt_start = NULL;
929 }
930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 /*
932 * Try to match each part of 'errorformat' until we find a complete
933 * match or no match.
934 */
935 valid = TRUE;
936restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000937 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100939 int r;
940
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 idx = fmt_ptr->prefix;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200942 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 continue;
944 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000945 pattern[0] = NUL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200946 if (!qi->qf_multiscan)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 errmsg[0] = NUL;
948 lnum = 0;
949 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000950 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 enr = -1;
952 type = 0;
953 tail = NULL;
954
955 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200956 r = vim_regexec(&regmatch, state.linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100957 fmt_ptr->prog = regmatch.regprog;
958 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200960 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 continue;
962 if (vim_strchr((char_u *)"EWI", idx) != NULL)
963 type = idx;
964 else
965 type = 0;
966 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000967 * Extract error message data from matched line.
968 * We check for an actual submatch, because "\[" and "\]" in
969 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 */
971 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
972 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000973 int c;
974
975 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
976 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000977
978 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000979 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000980 *regmatch.endp[i] = NUL;
981 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
982 *regmatch.endp[i] = c;
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000985 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 continue;
987 }
988 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000989 {
990 if (regmatch.startp[i] == NULL)
991 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000995 {
996 if (regmatch.startp[i] == NULL)
997 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +00001001 {
1002 if (regmatch.startp[i] == NULL)
1003 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +00001005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +00001007 {
1008 if (regmatch.startp[i] == NULL)
1009 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +00001011 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001012 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001013 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001014 if (state.linelen > errmsglen) {
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001015 /* linelen + null terminator */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001016 if ((errmsg = vim_realloc(errmsg,
1017 state.linelen + 1)) == NULL)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001018 goto qf_init_end;
Bram Moolenaare0d37972016-07-15 22:36:01 +02001019 errmsglen = state.linelen + 1;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001020 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001021 vim_strncpy(errmsg, state.linebuf, state.linelen);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
1024 {
Bram Moolenaar4169da72006-06-20 18:49:32 +00001025 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1026 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001028 if (len > errmsglen) {
1029 /* len + null terminator */
1030 if ((errmsg = vim_realloc(errmsg, len + 1))
1031 == NULL)
1032 goto qf_init_end;
1033 errmsglen = len + 1;
1034 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +00001035 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
1037 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +00001038 {
1039 if (regmatch.startp[i] == NULL)
1040 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +00001042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
1044 {
Bram Moolenaarf13de072012-06-01 18:34:41 +02001045 char_u *match_ptr;
1046
Bram Moolenaar4169da72006-06-20 18:49:32 +00001047 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1048 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +02001049 col = 0;
1050 for (match_ptr = regmatch.startp[i];
1051 match_ptr != regmatch.endp[i]; ++match_ptr)
1052 {
1053 ++col;
1054 if (*match_ptr == TAB)
1055 {
1056 col += 7;
1057 col -= col % 8;
1058 }
1059 }
1060 ++col;
1061 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
1063 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
1064 {
Bram Moolenaar4169da72006-06-20 18:49:32 +00001065 if (regmatch.startp[i] == NULL)
1066 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001068 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001070 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1071 {
Bram Moolenaar4169da72006-06-20 18:49:32 +00001072 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1073 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001074 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1075 if (len > CMDBUFFSIZE - 5)
1076 len = CMDBUFFSIZE - 5;
1077 STRCPY(pattern, "^\\V");
1078 STRNCAT(pattern, regmatch.startp[i], len);
1079 pattern[len + 3] = '\\';
1080 pattern[len + 4] = '$';
1081 pattern[len + 5] = NUL;
1082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 break;
1084 }
1085 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001086 qi->qf_multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +00001087
Bram Moolenaar4770d092006-01-12 23:22:24 +00001088 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001090 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
1092 if (idx == 'D') /* enter directory */
1093 {
1094 if (*namebuf == NUL)
1095 {
1096 EMSG(_("E379: Missing or empty directory name"));
1097 goto error2;
1098 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001099 qi->qf_directory =
1100 qf_push_dir(namebuf, &qi->qf_dir_stack, FALSE);
1101 if (qi->qf_directory == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 goto error2;
1103 }
1104 else if (idx == 'X') /* leave directory */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001105 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 namebuf[0] = NUL; /* no match found, remove file name */
1108 lnum = 0; /* don't jump to this line */
1109 valid = FALSE;
Bram Moolenaare0d37972016-07-15 22:36:01 +02001110 if (state.linelen > errmsglen) {
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001111 /* linelen + null terminator */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001112 if ((errmsg = vim_realloc(errmsg, state.linelen + 1)) == NULL)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001113 goto qf_init_end;
Bram Moolenaare0d37972016-07-15 22:36:01 +02001114 errmsglen = state.linelen + 1;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001115 }
1116 /* copy whole line to error message */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001117 vim_strncpy(errmsg, state.linebuf, state.linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001118 if (fmt_ptr == NULL)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001119 qi->qf_multiline = qi->qf_multiignore = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001121 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaar01265852006-03-20 21:50:15 +00001123 /* honor %> item */
1124 if (fmt_ptr->conthere)
1125 fmt_start = fmt_ptr;
1126
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +01001128 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001129 qi->qf_multiline = TRUE; /* start of a multi-line message */
1130 qi->qf_multiignore = FALSE; /* reset continuation */
Bram Moolenaar8eded092014-03-12 19:41:55 +01001131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1133 { /* continuation of multi-line msg */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001134 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
1135
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 if (qfprev == NULL)
1137 goto error2;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001138 if (*errmsg && !qi->qf_multiignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
1140 len = (int)STRLEN(qfprev->qf_text);
1141 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
1142 == NULL)
1143 goto error2;
1144 STRCPY(ptr, qfprev->qf_text);
1145 vim_free(qfprev->qf_text);
1146 qfprev->qf_text = ptr;
1147 *(ptr += len) = '\n';
1148 STRCPY(++ptr, errmsg);
1149 }
1150 if (qfprev->qf_nr == -1)
1151 qfprev->qf_nr = enr;
1152 if (vim_isprintc(type) && !qfprev->qf_type)
1153 qfprev->qf_type = type; /* only printable chars allowed */
1154 if (!qfprev->qf_lnum)
1155 qfprev->qf_lnum = lnum;
1156 if (!qfprev->qf_col)
1157 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001158 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (!qfprev->qf_fnum)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001160 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1161 *namebuf || qi->qf_directory != NULL
1162 ? namebuf
1163 : qi->qf_currfile != NULL && valid
1164 ? qi->qf_currfile : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (idx == 'Z')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001166 qi->qf_multiline = qi->qf_multiignore = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 line_breakcheck();
1168 continue;
1169 }
1170 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1171 {
1172 /* global file names */
1173 valid = FALSE;
1174 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
1175 {
1176 if (*namebuf && idx == 'P')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001177 qi->qf_currfile =
1178 qf_push_dir(namebuf, &qi->qf_file_stack, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 else if (idx == 'Q')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001180 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 *namebuf = NUL;
1182 if (tail && *tail)
1183 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00001184 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001185 qi->qf_multiscan = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 goto restofline;
1187 }
1188 }
1189 }
1190 if (fmt_ptr->flags == '-') /* generally exclude this line */
1191 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001192 if (qi->qf_multiline)
1193 /* also exclude continuation lines */
1194 qi->qf_multiignore = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 continue;
1196 }
1197 }
1198
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001199 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001200 qi->qf_directory,
1201 (*namebuf || qi->qf_directory != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 ? namebuf
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001203 : ((qi->qf_currfile != NULL && valid)
1204 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001205 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 errmsg,
1207 lnum,
1208 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001209 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001210 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 enr,
1212 type,
1213 valid) == FAIL)
1214 goto error2;
1215 line_breakcheck();
1216 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001217 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001219 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001221 /* no valid entry found */
1222 qi->qf_lists[qi->qf_curlist].qf_ptr =
1223 qi->qf_lists[qi->qf_curlist].qf_start;
1224 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1225 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 else
1228 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001229 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1230 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1231 qi->qf_lists[qi->qf_curlist].qf_ptr =
1232 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001234 /* return number of matches */
1235 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001236 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 }
1238 EMSG(_(e_readerrf));
1239error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001240 qf_free(qi, qi->qf_curlist);
1241 qi->qf_listcount--;
1242 if (qi->qf_curlist > 0)
1243 --qi->qf_curlist;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001244qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001245 if (state.fd != NULL)
1246 fclose(state.fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 vim_free(namebuf);
1248 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001249 vim_free(pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001250 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
1252#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001253 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254#endif
1255
1256 return retval;
1257}
1258
Bram Moolenaarfb604092014-07-23 15:55:00 +02001259 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001260qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001261{
1262 if (title != NULL)
1263 {
1264 char_u *p = alloc((int)STRLEN(title) + 2);
1265
1266 qi->qf_lists[qi->qf_curlist].qf_title = p;
1267 if (p != NULL)
1268 sprintf((char *)p, ":%s", (char *)title);
1269 }
1270}
1271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272/*
1273 * Prepare for adding a new quickfix list.
1274 */
1275 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001276qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 int i;
1279
1280 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001281 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 * the current entry. This makes it possible to browse in a tree-like
1283 * way with ":grep'.
1284 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001285 while (qi->qf_listcount > qi->qf_curlist + 1)
1286 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288 /*
1289 * When the stack is full, remove to oldest entry
1290 * Otherwise, add a new entry.
1291 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001292 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001294 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001296 qi->qf_lists[i - 1] = qi->qf_lists[i];
1297 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001300 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001301 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001302 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303}
1304
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001305/*
1306 * Free a location list
1307 */
1308 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001309ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001310{
1311 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001312 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001313
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001314 qi = *pqi;
1315 if (qi == NULL)
1316 return;
1317 *pqi = NULL; /* Remove reference to this list */
1318
1319 qi->qf_refcount--;
1320 if (qi->qf_refcount < 1)
1321 {
1322 /* No references to this location list */
1323 for (i = 0; i < qi->qf_listcount; ++i)
1324 qf_free(qi, i);
1325 vim_free(qi);
1326 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001327}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001328
1329 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001330qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001331{
1332 int i;
1333 qf_info_T *qi = &ql_info;
1334
1335 if (wp != NULL)
1336 {
1337 /* location list */
1338 ll_free_all(&wp->w_llist);
1339 ll_free_all(&wp->w_llist_ref);
1340 }
1341 else
1342 /* quickfix list */
1343 for (i = 0; i < qi->qf_listcount; ++i)
1344 qf_free(qi, i);
1345}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347/*
1348 * Add an entry to the end of the list of errors.
1349 * Returns OK or FAIL.
1350 */
1351 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001352qf_add_entry(
1353 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001354 char_u *dir, /* optional directory name */
1355 char_u *fname, /* file name or NULL */
1356 int bufnum, /* buffer number or zero */
1357 char_u *mesg, /* message */
1358 long lnum, /* line number */
1359 int col, /* column */
1360 int vis_col, /* using visual column */
1361 char_u *pattern, /* search pattern */
1362 int nr, /* error number */
1363 int type, /* type character */
1364 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001366 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001367 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001369 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001371 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001372 {
1373 buf_T *buf = buflist_findnr(bufnum);
1374
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001375 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001376 if (buf != NULL)
1377 buf->b_has_qf_entry = TRUE;
1378 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001379 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001380 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1382 {
1383 vim_free(qfp);
1384 return FAIL;
1385 }
1386 qfp->qf_lnum = lnum;
1387 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001388 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001389 if (pattern == NULL || *pattern == NUL)
1390 qfp->qf_pattern = NULL;
1391 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1392 {
1393 vim_free(qfp->qf_text);
1394 vim_free(qfp);
1395 return FAIL;
1396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 qfp->qf_nr = nr;
1398 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1399 type = 0;
1400 qfp->qf_type = type;
1401 qfp->qf_valid = valid;
1402
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001403 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001404 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1405 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001407 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001408 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1409 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001410 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412 else
1413 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001414 qfp->qf_prev = *lastp;
1415 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001417 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001419 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001420 ++qi->qf_lists[qi->qf_curlist].qf_count;
1421 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1422 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001424 qi->qf_lists[qi->qf_curlist].qf_index =
1425 qi->qf_lists[qi->qf_curlist].qf_count;
1426 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428
1429 return OK;
1430}
1431
1432/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001433 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001434 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001435 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001436ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001437{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001438 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001439
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001440 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1441 if (qi != NULL)
1442 {
1443 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1444 qi->qf_refcount++;
1445 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001446
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001447 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001448}
1449
1450/*
1451 * Return the location list for window 'wp'.
1452 * If not present, allocate a location list
1453 */
1454 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001455ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001456{
1457 if (IS_LL_WINDOW(wp))
1458 /* For a location list window, use the referenced location list */
1459 return wp->w_llist_ref;
1460
1461 /*
1462 * For a non-location list window, w_llist_ref should not point to a
1463 * location list.
1464 */
1465 ll_free_all(&wp->w_llist_ref);
1466
1467 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001468 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001469 return wp->w_llist;
1470}
1471
1472/*
1473 * Copy the location list from window "from" to window "to".
1474 */
1475 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001476copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001477{
1478 qf_info_T *qi;
1479 int idx;
1480 int i;
1481
1482 /*
1483 * When copying from a location list window, copy the referenced
1484 * location list. For other windows, copy the location list for
1485 * that window.
1486 */
1487 if (IS_LL_WINDOW(from))
1488 qi = from->w_llist_ref;
1489 else
1490 qi = from->w_llist;
1491
1492 if (qi == NULL) /* no location list to copy */
1493 return;
1494
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001495 /* allocate a new location list */
1496 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001497 return;
1498
1499 to->w_llist->qf_listcount = qi->qf_listcount;
1500
1501 /* Copy the location lists one at a time */
1502 for (idx = 0; idx < qi->qf_listcount; idx++)
1503 {
1504 qf_list_T *from_qfl;
1505 qf_list_T *to_qfl;
1506
1507 to->w_llist->qf_curlist = idx;
1508
1509 from_qfl = &qi->qf_lists[idx];
1510 to_qfl = &to->w_llist->qf_lists[idx];
1511
1512 /* Some of the fields are populated by qf_add_entry() */
1513 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1514 to_qfl->qf_count = 0;
1515 to_qfl->qf_index = 0;
1516 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001517 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001518 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001519 if (from_qfl->qf_title != NULL)
1520 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1521 else
1522 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001523
1524 if (from_qfl->qf_count)
1525 {
1526 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001527 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001528
1529 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001530 for (i = 0, from_qfp = from_qfl->qf_start;
1531 i < from_qfl->qf_count && from_qfp != NULL;
1532 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001533 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001534 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001535 NULL,
1536 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001537 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001538 from_qfp->qf_text,
1539 from_qfp->qf_lnum,
1540 from_qfp->qf_col,
1541 from_qfp->qf_viscol,
1542 from_qfp->qf_pattern,
1543 from_qfp->qf_nr,
1544 0,
1545 from_qfp->qf_valid) == FAIL)
1546 {
1547 qf_free_all(to);
1548 return;
1549 }
1550 /*
1551 * qf_add_entry() will not set the qf_num field, as the
1552 * directory and file names are not supplied. So the qf_fnum
1553 * field is copied here.
1554 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001555 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001556 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1557 prevp->qf_type = from_qfp->qf_type; /* error type */
1558 if (from_qfl->qf_ptr == from_qfp)
1559 to_qfl->qf_ptr = prevp; /* current location */
1560 }
1561 }
1562
1563 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1564
1565 /* When no valid entries are present in the list, qf_ptr points to
1566 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001567 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001568 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001569 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001570 to_qfl->qf_index = 1;
1571 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001572 }
1573
1574 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1575}
1576
1577/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001578 * Looking up a buffer can be slow if there are many. Remember the last one
1579 * to make this a lot faster if there are multiple matches in the same file.
1580 */
1581static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001582static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001583
1584/*
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001585 * Get buffer number for file "dir.name".
1586 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 */
1588 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001589qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590{
Bram Moolenaar82404332016-07-10 17:00:38 +02001591 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001592 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001593 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (fname == NULL || *fname == NUL) /* no file name */
1596 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
Bram Moolenaare60acc12011-05-10 16:41:25 +02001598#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001599 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001600#endif
1601#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001602 if (directory != NULL)
1603 slash_adjust(directory);
1604 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001605#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001606 if (directory != NULL && !vim_isAbsName(fname)
1607 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1608 {
1609 /*
1610 * Here we check if the file really exists.
1611 * This should normally be true, but if make works without
1612 * "leaving directory"-messages we might have missed a
1613 * directory change.
1614 */
1615 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001618 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001619 if (directory)
1620 ptr = concat_fnames(directory, fname, TRUE);
1621 else
1622 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001624 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001625 bufname = ptr;
1626 }
1627 else
1628 bufname = fname;
1629
1630 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001631 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001632 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001633 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001634 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001636 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001637 {
1638 vim_free(qf_last_bufname);
1639 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1640 if (bufname == ptr)
1641 qf_last_bufname = bufname;
1642 else
1643 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001644 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001645 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001646 if (buf == NULL)
1647 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001648
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001649 buf->b_has_qf_entry = TRUE;
1650 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651}
1652
1653/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001654 * Push dirbuf onto the directory stack and return pointer to actual dir or
1655 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 */
1657 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001658qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
1660 struct dir_stack_T *ds_new;
1661 struct dir_stack_T *ds_ptr;
1662
1663 /* allocate new stack element and hook it in */
1664 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1665 if (ds_new == NULL)
1666 return NULL;
1667
1668 ds_new->next = *stackptr;
1669 *stackptr = ds_new;
1670
1671 /* store directory on the stack */
1672 if (vim_isAbsName(dirbuf)
1673 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001674 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 (*stackptr)->dirname = vim_strsave(dirbuf);
1676 else
1677 {
1678 /* Okay we don't have an absolute path.
1679 * dirbuf must be a subdir of one of the directories on the stack.
1680 * Let's search...
1681 */
1682 ds_new = (*stackptr)->next;
1683 (*stackptr)->dirname = NULL;
1684 while (ds_new)
1685 {
1686 vim_free((*stackptr)->dirname);
1687 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1688 TRUE);
1689 if (mch_isdir((*stackptr)->dirname) == TRUE)
1690 break;
1691
1692 ds_new = ds_new->next;
1693 }
1694
1695 /* clean up all dirs we already left */
1696 while ((*stackptr)->next != ds_new)
1697 {
1698 ds_ptr = (*stackptr)->next;
1699 (*stackptr)->next = (*stackptr)->next->next;
1700 vim_free(ds_ptr->dirname);
1701 vim_free(ds_ptr);
1702 }
1703
1704 /* Nothing found -> it must be on top level */
1705 if (ds_new == NULL)
1706 {
1707 vim_free((*stackptr)->dirname);
1708 (*stackptr)->dirname = vim_strsave(dirbuf);
1709 }
1710 }
1711
1712 if ((*stackptr)->dirname != NULL)
1713 return (*stackptr)->dirname;
1714 else
1715 {
1716 ds_ptr = *stackptr;
1717 *stackptr = (*stackptr)->next;
1718 vim_free(ds_ptr);
1719 return NULL;
1720 }
1721}
1722
1723
1724/*
1725 * pop dirbuf from the directory stack and return previous directory or NULL if
1726 * stack is empty
1727 */
1728 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001729qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730{
1731 struct dir_stack_T *ds_ptr;
1732
1733 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1734 * What to do if it isn't? */
1735
1736 /* pop top element and free it */
1737 if (*stackptr != NULL)
1738 {
1739 ds_ptr = *stackptr;
1740 *stackptr = (*stackptr)->next;
1741 vim_free(ds_ptr->dirname);
1742 vim_free(ds_ptr);
1743 }
1744
1745 /* return NEW top element as current dir or NULL if stack is empty*/
1746 return *stackptr ? (*stackptr)->dirname : NULL;
1747}
1748
1749/*
1750 * clean up directory stack
1751 */
1752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001753qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 struct dir_stack_T *ds_ptr;
1756
1757 while ((ds_ptr = *stackptr) != NULL)
1758 {
1759 *stackptr = (*stackptr)->next;
1760 vim_free(ds_ptr->dirname);
1761 vim_free(ds_ptr);
1762 }
1763}
1764
1765/*
1766 * Check in which directory of the directory stack the given file can be
1767 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001768 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 * Cleans up intermediate directory entries.
1770 *
1771 * TODO: How to solve the following problem?
1772 * If we have the this directory tree:
1773 * ./
1774 * ./aa
1775 * ./aa/bb
1776 * ./bb
1777 * ./bb/x.c
1778 * and make says:
1779 * making all in aa
1780 * making all in bb
1781 * x.c:9: Error
1782 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1783 * qf_guess_filepath will return NULL.
1784 */
1785 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001786qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 struct dir_stack_T *ds_ptr;
1789 struct dir_stack_T *ds_tmp;
1790 char_u *fullname;
1791
1792 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001793 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 return NULL;
1795
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001796 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 fullname = NULL;
1798 while (ds_ptr)
1799 {
1800 vim_free(fullname);
1801 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1802
1803 /* If concat_fnames failed, just go on. The worst thing that can happen
1804 * is that we delete the entire stack.
1805 */
1806 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1807 break;
1808
1809 ds_ptr = ds_ptr->next;
1810 }
1811
1812 vim_free(fullname);
1813
1814 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001815 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001817 ds_tmp = qi->qf_dir_stack->next;
1818 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 vim_free(ds_tmp->dirname);
1820 vim_free(ds_tmp);
1821 }
1822
1823 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824}
1825
1826/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001827 * When loading a file from the quickfix, the auto commands may modify it.
1828 * This may invalidate the current quickfix entry. This function checks
1829 * whether a entry is still present in the quickfix.
1830 * Similar to location list.
1831 */
1832 static int
1833is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1834{
1835 qf_list_T *qfl;
1836 qfline_T *qfp;
1837 int i;
1838
1839 qfl = &qi->qf_lists[qi->qf_curlist];
1840
1841 /* Search for the entry in the current list */
1842 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1843 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001844 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001845 break;
1846
1847 if (i == qfl->qf_count) /* Entry is not found */
1848 return FALSE;
1849
1850 return TRUE;
1851}
1852
1853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 * jump to a quickfix line
1855 * if dir == FORWARD go "errornr" valid entries forward
1856 * if dir == BACKWARD go "errornr" valid entries backward
1857 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1858 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1859 * else if "errornr" is zero, redisplay the same line
1860 * else go to entry "errornr"
1861 */
1862 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001863qf_jump(
1864 qf_info_T *qi,
1865 int dir,
1866 int errornr,
1867 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001869 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001870 qfline_T *qf_ptr;
1871 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 int qf_index;
1873 int old_qf_fnum;
1874 int old_qf_index;
1875 int prev_index;
1876 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1877 char_u *err = e_no_more_items;
1878 linenr_T i;
1879 buf_T *old_curbuf;
1880 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 colnr_T screen_col;
1882 colnr_T char_col;
1883 char_u *line;
1884#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001885 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001886 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 int opened_window = FALSE;
1888 win_T *win;
1889 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001890 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001892 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 int print_message = TRUE;
1894 int len;
1895#ifdef FEAT_FOLDING
1896 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1897#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001898 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001899 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001901 if (qi == NULL)
1902 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001903
1904 if (qi->qf_curlist >= qi->qf_listcount
1905 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 EMSG(_(e_quickfix));
1908 return;
1909 }
1910
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001911 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001913 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 old_qf_index = qf_index;
1915 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1916 {
1917 while (errornr--)
1918 {
1919 old_qf_ptr = qf_ptr;
1920 prev_index = qf_index;
1921 old_qf_fnum = qf_ptr->qf_fnum;
1922 do
1923 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001924 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 || qf_ptr->qf_next == NULL)
1926 {
1927 qf_ptr = old_qf_ptr;
1928 qf_index = prev_index;
1929 if (err != NULL)
1930 {
1931 EMSG(_(err));
1932 goto theend;
1933 }
1934 errornr = 0;
1935 break;
1936 }
1937 ++qf_index;
1938 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001939 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1940 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1942 err = NULL;
1943 }
1944 }
1945 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1946 {
1947 while (errornr--)
1948 {
1949 old_qf_ptr = qf_ptr;
1950 prev_index = qf_index;
1951 old_qf_fnum = qf_ptr->qf_fnum;
1952 do
1953 {
1954 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1955 {
1956 qf_ptr = old_qf_ptr;
1957 qf_index = prev_index;
1958 if (err != NULL)
1959 {
1960 EMSG(_(err));
1961 goto theend;
1962 }
1963 errornr = 0;
1964 break;
1965 }
1966 --qf_index;
1967 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001968 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1969 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1971 err = NULL;
1972 }
1973 }
1974 else if (errornr != 0) /* go to specified number */
1975 {
1976 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1977 {
1978 --qf_index;
1979 qf_ptr = qf_ptr->qf_prev;
1980 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001981 while (errornr > qf_index && qf_index <
1982 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 && qf_ptr->qf_next != NULL)
1984 {
1985 ++qf_index;
1986 qf_ptr = qf_ptr->qf_next;
1987 }
1988 }
1989
1990#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001991 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1992 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 /* No need to print the error message if it's visible in the error
1994 * window */
1995 print_message = FALSE;
1996
1997 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001998 * For ":helpgrep" find a help window or open one.
1999 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002000 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002001 {
2002 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002003
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002004 if (cmdmod.tab != 0)
2005 wp = NULL;
2006 else
2007 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2008 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2009 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002010 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2011 win_enter(wp, TRUE);
2012 else
2013 {
2014 /*
2015 * Split off help window; put it at far top if no position
2016 * specified, the current window is vertically split and narrow.
2017 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002018 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002019 if (cmdmod.split == 0 && curwin->w_width != Columns
2020 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002021 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002022 if (qi != &ql_info)
2023 flags |= WSP_NEWLOC; /* don't copy the location list */
2024
2025 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002026 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002027 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002028
2029 if (curwin->w_height < p_hh)
2030 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002031
2032 if (qi != &ql_info) /* not a quickfix list */
2033 {
2034 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002035 curwin->w_llist = qi;
2036 qi->qf_refcount++;
2037 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002038 }
2039
2040 if (!p_im)
2041 restart_edit = 0; /* don't want insert mode in help file */
2042 }
2043
2044 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 * If currently in the quickfix window, find another window to show the
2046 * file in.
2047 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002048 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002050 win_T *usable_win_ptr = NULL;
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 /*
2053 * If there is no file specified, we don't know where to go.
2054 * But do advance, otherwise ":cn" gets stuck.
2055 */
2056 if (qf_ptr->qf_fnum == 0)
2057 goto theend;
2058
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002059 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002060
2061 ll_ref = curwin->w_llist_ref;
2062 if (ll_ref != NULL)
2063 {
2064 /* Find a window using the same location list that is not a
2065 * quickfix window. */
2066 FOR_ALL_WINDOWS(usable_win_ptr)
2067 if (usable_win_ptr->w_llist == ll_ref
2068 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002069 {
2070 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002071 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002072 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002073 }
2074
2075 if (!usable_win)
2076 {
2077 /* Locate a window showing a normal buffer */
2078 FOR_ALL_WINDOWS(win)
2079 if (win->w_buffer->b_p_bt[0] == NUL)
2080 {
2081 usable_win = 1;
2082 break;
2083 }
2084 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002087 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002088 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002090 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002091 {
2092 tabpage_T *tp;
2093 win_T *wp;
2094
2095 FOR_ALL_TAB_WINDOWS(tp, wp)
2096 {
2097 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2098 {
2099 goto_tabpage_win(tp, wp);
2100 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002101 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002102 }
2103 }
2104 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002105win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002106
2107 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002108 * If there is only one window and it is the quickfix window, create a
2109 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002110 */
2111 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002113 flags = WSP_ABOVE;
2114 if (ll_ref != NULL)
2115 flags |= WSP_NEWLOC;
2116 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 goto failed; /* not enough room for window */
2118 opened_window = TRUE; /* close it when fail */
2119 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002120 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002121 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002122 if (ll_ref != NULL)
2123 {
2124 /* The new window should use the location list from the
2125 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002126 curwin->w_llist = ll_ref;
2127 ll_ref->qf_refcount++;
2128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
2130 else
2131 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002132 if (curwin->w_llist_ref != NULL)
2133 {
2134 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002135 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002136 if (win == NULL)
2137 {
2138 /* Find the window showing the selected file */
2139 FOR_ALL_WINDOWS(win)
2140 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2141 break;
2142 if (win == NULL)
2143 {
2144 /* Find a previous usable window */
2145 win = curwin;
2146 do
2147 {
2148 if (win->w_buffer->b_p_bt[0] == NUL)
2149 break;
2150 if (win->w_prev == NULL)
2151 win = lastwin; /* wrap around the top */
2152 else
2153 win = win->w_prev; /* go to previous window */
2154 } while (win != curwin);
2155 }
2156 }
2157 win_goto(win);
2158
2159 /* If the location list for the window is not set, then set it
2160 * to the location list from the location window */
2161 if (win->w_llist == NULL)
2162 {
2163 win->w_llist = ll_ref;
2164 ll_ref->qf_refcount++;
2165 }
2166 }
2167 else
2168 {
2169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 /*
2171 * Try to find a window that shows the right buffer.
2172 * Default to the window just above the quickfix buffer.
2173 */
2174 win = curwin;
2175 altwin = NULL;
2176 for (;;)
2177 {
2178 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2179 break;
2180 if (win->w_prev == NULL)
2181 win = lastwin; /* wrap around the top */
2182 else
2183 win = win->w_prev; /* go to previous window */
2184
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002185 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
2187 /* Didn't find it, go to the window before the quickfix
2188 * window. */
2189 if (altwin != NULL)
2190 win = altwin;
2191 else if (curwin->w_prev != NULL)
2192 win = curwin->w_prev;
2193 else
2194 win = curwin->w_next;
2195 break;
2196 }
2197
2198 /* Remember a usable window. */
2199 if (altwin == NULL && !win->w_p_pvw
2200 && win->w_buffer->b_p_bt[0] == NUL)
2201 altwin = win;
2202 }
2203
2204 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207 }
2208#endif
2209
2210 /*
2211 * If there is a file name,
2212 * read the wanted file if needed, and check autowrite etc.
2213 */
2214 old_curbuf = curbuf;
2215 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002216
2217 if (qf_ptr->qf_fnum != 0)
2218 {
2219 if (qf_ptr->qf_type == 1)
2220 {
2221 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2222 * set b_p_ro flag). */
2223 if (!can_abandon(curbuf, forceit))
2224 {
2225 EMSG(_(e_nowrtmsg));
2226 ok = FALSE;
2227 }
2228 else
2229 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002230 ECMD_HIDE + ECMD_SET_HELP,
2231 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002232 }
2233 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002234 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002235 int old_qf_curlist = qi->qf_curlist;
2236 int is_abort = FALSE;
2237
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002238 ok = buflist_getfile(qf_ptr->qf_fnum,
2239 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002240 if (qi != &ql_info && !win_valid(oldwin))
2241 {
2242 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002243 is_abort = TRUE;
2244 opened_window = FALSE;
2245 }
2246 else if (old_qf_curlist != qi->qf_curlist
2247 || !is_qf_entry_present(qi, qf_ptr))
2248 {
2249 if (qi == &ql_info)
2250 EMSG(_("E925: Current quickfix was changed"));
2251 else
2252 EMSG(_("E926: Current location list was changed"));
2253 is_abort = TRUE;
2254 }
2255
2256 if (is_abort)
2257 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002258 ok = FALSE;
2259 qi = NULL;
2260 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002261 }
2262 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002263 }
2264
2265 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
2267 /* When not switched to another buffer, still need to set pc mark */
2268 if (curbuf == old_curbuf)
2269 setpcmark();
2270
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002271 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002273 /*
2274 * Go to line with error, unless qf_lnum is 0.
2275 */
2276 i = qf_ptr->qf_lnum;
2277 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002279 if (i > curbuf->b_ml.ml_line_count)
2280 i = curbuf->b_ml.ml_line_count;
2281 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002283 if (qf_ptr->qf_col > 0)
2284 {
2285 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002286#ifdef FEAT_VIRTUALEDIT
2287 curwin->w_cursor.coladd = 0;
2288#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002289 if (qf_ptr->qf_viscol == TRUE)
2290 {
2291 /*
2292 * Check each character from the beginning of the error
2293 * line up to the error column. For each tab character
2294 * found, reduce the error column value by the length of
2295 * a tab character.
2296 */
2297 line = ml_get_curline();
2298 screen_col = 0;
2299 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2300 {
2301 if (*line == NUL)
2302 break;
2303 if (*line++ == '\t')
2304 {
2305 curwin->w_cursor.col -= 7 - (screen_col % 8);
2306 screen_col += 8 - (screen_col % 8);
2307 }
2308 else
2309 ++screen_col;
2310 }
2311 }
2312 check_cursor();
2313 }
2314 else
2315 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002318 {
2319 pos_T save_cursor;
2320
2321 /* Move the cursor to the first line in the buffer */
2322 save_cursor = curwin->w_cursor;
2323 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002324 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2325 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002326 curwin->w_cursor = save_cursor;
2327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
2329#ifdef FEAT_FOLDING
2330 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2331 foldOpenCursor();
2332#endif
2333 if (print_message)
2334 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002335 /* Update the screen before showing the message, unless the screen
2336 * scrolled up. */
2337 if (!msg_scrolled)
2338 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002340 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2342 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2343 /* Add the message, skipping leading whitespace and newlines. */
2344 len = (int)STRLEN(IObuff);
2345 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2346
2347 /* Output the message. Overwrite to avoid scrolling when the 'O'
2348 * flag is present in 'shortmess'; But when not jumping, print the
2349 * whole message. */
2350 i = msg_scroll;
2351 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2352 msg_scroll = TRUE;
2353 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2354 msg_scroll = FALSE;
2355 msg_attr_keep(IObuff, 0, TRUE);
2356 msg_scroll = i;
2357 }
2358 }
2359 else
2360 {
2361#ifdef FEAT_WINDOWS
2362 if (opened_window)
2363 win_close(curwin, TRUE); /* Close opened window */
2364#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002365 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
2367 /*
2368 * Couldn't open file, so put index back where it was. This could
2369 * happen if the file was readonly and we changed something.
2370 */
2371#ifdef FEAT_WINDOWS
2372failed:
2373#endif
2374 qf_ptr = old_qf_ptr;
2375 qf_index = old_qf_index;
2376 }
2377 }
2378theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002379 if (qi != NULL)
2380 {
2381 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2382 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#ifdef FEAT_WINDOWS
2385 if (p_swb != old_swb && opened_window)
2386 {
2387 /* Restore old 'switchbuf' value, but not when an autocommand or
2388 * modeline has changed the value. */
2389 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002392 swb_flags = old_swb_flags;
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 else
2395 free_string_option(old_swb);
2396 }
2397#endif
2398}
2399
2400/*
2401 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 */
2404 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002405qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002407 buf_T *buf;
2408 char_u *fname;
2409 qfline_T *qfp;
2410 int i;
2411 int idx1 = 1;
2412 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002413 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002414 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002415 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002417 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002419 if (eap->cmdidx == CMD_llist)
2420 {
2421 qi = GET_LOC_LIST(curwin);
2422 if (qi == NULL)
2423 {
2424 EMSG(_(e_loclist));
2425 return;
2426 }
2427 }
2428
2429 if (qi->qf_curlist >= qi->qf_listcount
2430 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
2432 EMSG(_(e_quickfix));
2433 return;
2434 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002435 if (*arg == '+')
2436 {
2437 ++arg;
2438 plus = TRUE;
2439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2441 {
2442 EMSG(_(e_trailing));
2443 return;
2444 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002445 if (plus)
2446 {
2447 i = qi->qf_lists[qi->qf_curlist].qf_index;
2448 idx2 = i + idx1;
2449 idx1 = i;
2450 }
2451 else
2452 {
2453 i = qi->qf_lists[qi->qf_curlist].qf_count;
2454 if (idx1 < 0)
2455 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2456 if (idx2 < 0)
2457 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002460 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002462 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2463 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
2465 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2466 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002467 msg_putchar('\n');
2468 if (got_int)
2469 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002470
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002471 fname = NULL;
2472 if (qfp->qf_fnum != 0
2473 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2474 {
2475 fname = buf->b_fname;
2476 if (qfp->qf_type == 1) /* :helpgrep */
2477 fname = gettail(fname);
2478 }
2479 if (fname == NULL)
2480 sprintf((char *)IObuff, "%2d", i);
2481 else
2482 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2483 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002484 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002485 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2486 if (qfp->qf_lnum == 0)
2487 IObuff[0] = NUL;
2488 else if (qfp->qf_col == 0)
2489 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2490 else
2491 sprintf((char *)IObuff, ":%ld col %d",
2492 qfp->qf_lnum, qfp->qf_col);
2493 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2494 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2495 msg_puts_attr(IObuff, hl_attr(HLF_N));
2496 if (qfp->qf_pattern != NULL)
2497 {
2498 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2499 STRCAT(IObuff, ":");
2500 msg_puts(IObuff);
2501 }
2502 msg_puts((char_u *)" ");
2503
2504 /* Remove newlines and leading whitespace from the text. For an
2505 * unrecognized line keep the indent, the compiler may mark a word
2506 * with ^^^^. */
2507 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2509 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002510 msg_prt_line(IObuff, FALSE);
2511 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002513
2514 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002515 if (qfp == NULL)
2516 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002517 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 ui_breakcheck();
2519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520}
2521
2522/*
2523 * Remove newlines and leading whitespace from an error message.
2524 * Put the result in "buf[bufsize]".
2525 */
2526 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002527qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
2529 int i;
2530 char_u *p = text;
2531
2532 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2533 {
2534 if (*p == '\n')
2535 {
2536 buf[i] = ' ';
2537 while (*++p != NUL)
2538 if (!vim_iswhite(*p) && *p != '\n')
2539 break;
2540 }
2541 else
2542 buf[i] = *p++;
2543 }
2544 buf[i] = NUL;
2545}
2546
2547/*
2548 * ":colder [count]": Up in the quickfix stack.
2549 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002550 * ":lolder [count]": Up in the location list stack.
2551 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 */
2553 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002554qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002556 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 int count;
2558
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002559 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2560 {
2561 qi = GET_LOC_LIST(curwin);
2562 if (qi == NULL)
2563 {
2564 EMSG(_(e_loclist));
2565 return;
2566 }
2567 }
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (eap->addr_count != 0)
2570 count = eap->line2;
2571 else
2572 count = 1;
2573 while (count--)
2574 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002575 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002577 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002580 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002582 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584 else
2585 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002586 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 {
2588 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002589 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002591 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 }
2593 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002594 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595}
2596
2597 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002598qf_msg(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002601 qi->qf_curlist + 1, qi->qf_listcount,
2602 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002604 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605#endif
2606}
2607
2608/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002609 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 */
2611 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002612qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002614 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002615 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002616 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002618 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002620 qfp = qi->qf_lists[idx].qf_start;
2621 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002622 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002623 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002624 vim_free(qfp->qf_text);
2625 stop = (qfp == qfpnext);
2626 vim_free(qfp->qf_pattern);
2627 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002628 if (stop)
2629 /* Somehow qf_count may have an incorrect value, set it to 1
2630 * to avoid crashing when it's wrong.
2631 * TODO: Avoid qf_count being incorrect. */
2632 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002633 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002634 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002635 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002637 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002638 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002639 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002640
2641 qf_clean_dir_stack(&qi->qf_dir_stack);
2642 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643}
2644
2645/*
2646 * qf_mark_adjust: adjust marks
2647 */
2648 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002649qf_mark_adjust(
2650 win_T *wp,
2651 linenr_T line1,
2652 linenr_T line2,
2653 long amount,
2654 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002656 int i;
2657 qfline_T *qfp;
2658 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002659 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002660 int found_one = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002662 if (!curbuf->b_has_qf_entry)
2663 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002664 if (wp != NULL)
2665 {
2666 if (wp->w_llist == NULL)
2667 return;
2668 qi = wp->w_llist;
2669 }
2670
2671 for (idx = 0; idx < qi->qf_listcount; ++idx)
2672 if (qi->qf_lists[idx].qf_count)
2673 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002674 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2675 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (qfp->qf_fnum == curbuf->b_fnum)
2677 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002678 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2680 {
2681 if (amount == MAXLNUM)
2682 qfp->qf_cleared = TRUE;
2683 else
2684 qfp->qf_lnum += amount;
2685 }
2686 else if (amount_after && qfp->qf_lnum > line2)
2687 qfp->qf_lnum += amount_after;
2688 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002689
2690 if (!found_one)
2691 curbuf->b_has_qf_entry = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692}
2693
2694/*
2695 * Make a nice message out of the error character and the error number:
2696 * char number message
2697 * e or E 0 " error"
2698 * w or W 0 " warning"
2699 * i or I 0 " info"
2700 * 0 0 ""
2701 * other 0 " c"
2702 * e or E n " error n"
2703 * w or W n " warning n"
2704 * i or I n " info n"
2705 * 0 n " error n"
2706 * other n " c n"
2707 * 1 x "" :helpgrep
2708 */
2709 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002710qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 static char_u buf[20];
2713 static char_u cc[3];
2714 char_u *p;
2715
2716 if (c == 'W' || c == 'w')
2717 p = (char_u *)" warning";
2718 else if (c == 'I' || c == 'i')
2719 p = (char_u *)" info";
2720 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2721 p = (char_u *)" error";
2722 else if (c == 0 || c == 1)
2723 p = (char_u *)"";
2724 else
2725 {
2726 cc[0] = ' ';
2727 cc[1] = c;
2728 cc[2] = NUL;
2729 p = cc;
2730 }
2731
2732 if (nr <= 0)
2733 return p;
2734
2735 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2736 return buf;
2737}
2738
2739#if defined(FEAT_WINDOWS) || defined(PROTO)
2740/*
2741 * ":cwindow": open the quickfix window if we have errors to display,
2742 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002743 * ":lwindow": open the location list window if we have locations to display,
2744 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 */
2746 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002747ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002749 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 win_T *win;
2751
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002752 if (eap->cmdidx == CMD_lwindow)
2753 {
2754 qi = GET_LOC_LIST(curwin);
2755 if (qi == NULL)
2756 return;
2757 }
2758
2759 /* Look for an existing quickfix window. */
2760 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762 /*
2763 * If a quickfix window is open but we have no errors to display,
2764 * close the window. If a quickfix window is not open, then open
2765 * it if we have errors; otherwise, leave it closed.
2766 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002767 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002768 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002769 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 {
2771 if (win != NULL)
2772 ex_cclose(eap);
2773 }
2774 else if (win == NULL)
2775 ex_copen(eap);
2776}
2777
2778/*
2779 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002780 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002783ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002785 win_T *win = NULL;
2786 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002788 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2789 {
2790 qi = GET_LOC_LIST(curwin);
2791 if (qi == NULL)
2792 return;
2793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002795 /* Find existing quickfix window and close it. */
2796 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (win != NULL)
2798 win_close(win, FALSE);
2799}
2800
2801/*
2802 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002803 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 */
2805 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002806ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002808 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002811 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002812 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002813 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002815 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2816 {
2817 qi = GET_LOC_LIST(curwin);
2818 if (qi == NULL)
2819 {
2820 EMSG(_(e_loclist));
2821 return;
2822 }
2823 }
2824
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 if (eap->addr_count != 0)
2826 height = eap->line2;
2827 else
2828 height = QF_WINHEIGHT;
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831#ifdef FEAT_GUI
2832 need_mouse_correct = TRUE;
2833#endif
2834
2835 /*
2836 * Find existing quickfix window, or open a new one.
2837 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002838 win = qf_find_win(qi);
2839
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002840 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002843 if (eap->addr_count != 0)
2844 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002845 if (cmdmod.split & WSP_VERT)
2846 {
2847 if (height != W_WIDTH(win))
2848 win_setwidth(height);
2849 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002850 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002851 win_setheight(height);
2852 }
2853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 else
2855 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002856 qf_buf = qf_find_buf(qi);
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 /* The current window becomes the previous window afterwards. */
2859 win = curwin;
2860
Bram Moolenaar77642c02012-11-20 17:55:10 +01002861 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2862 && cmdmod.split == 0)
2863 /* Create the new window at the very bottom, except when
2864 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002865 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002866 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002868 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002870 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002872 /*
2873 * For the location list window, create a reference to the
2874 * location list from the window 'win'.
2875 */
2876 curwin->w_llist_ref = win->w_llist;
2877 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002879
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002880 if (oldwin != curwin)
2881 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002882 if (qf_buf != NULL)
2883 /* Use the existing quickfix buffer */
2884 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002885 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002886 else
2887 {
2888 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002889 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002890 /* switch off 'swapfile' */
2891 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2892 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002893 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002894 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002895 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002896#ifdef FEAT_DIFF
2897 curwin->w_p_diff = FALSE;
2898#endif
2899#ifdef FEAT_FOLDING
2900 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2901 OPT_LOCAL);
2902#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002905 /* Only set the height when still in the same tab page and there is no
2906 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002907 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 win_setheight(height);
2909 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2910 if (win_valid(win))
2911 prevwin = win;
2912 }
2913
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002914 qf_set_title_var(qi);
2915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 /*
2917 * Fill the buffer with the quickfix list.
2918 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002919 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002921 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 curwin->w_cursor.col = 0;
2923 check_cursor();
2924 update_topline(); /* scroll to show the line */
2925}
2926
2927/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02002928 * Move the cursor in the quickfix window to "lnum".
2929 */
2930 static void
2931qf_win_goto(win_T *win, linenr_T lnum)
2932{
2933 win_T *old_curwin = curwin;
2934
2935 curwin = win;
2936 curbuf = win->w_buffer;
2937 curwin->w_cursor.lnum = lnum;
2938 curwin->w_cursor.col = 0;
2939#ifdef FEAT_VIRTUALEDIT
2940 curwin->w_cursor.coladd = 0;
2941#endif
2942 curwin->w_curswant = 0;
2943 update_topline(); /* scroll to show the line */
2944 redraw_later(VALID);
2945 curwin->w_redr_status = TRUE; /* update ruler */
2946 curwin = old_curwin;
2947 curbuf = curwin->w_buffer;
2948}
2949
2950/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02002951 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02002952 */
2953 void
2954ex_cbottom(exarg_T *eap UNUSED)
2955{
Bram Moolenaar537ef082016-07-09 17:56:19 +02002956 qf_info_T *qi = &ql_info;
2957 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02002958
Bram Moolenaar537ef082016-07-09 17:56:19 +02002959 if (eap->cmdidx == CMD_lbottom)
2960 {
2961 qi = GET_LOC_LIST(curwin);
2962 if (qi == NULL)
2963 {
2964 EMSG(_(e_loclist));
2965 return;
2966 }
2967 }
2968
2969 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02002970 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
2971 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
2972}
2973
2974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 * Return the number of the current entry (line number in the quickfix
2976 * window).
2977 */
2978 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002979qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002981 qf_info_T *qi = &ql_info;
2982
2983 if (IS_LL_WINDOW(wp))
2984 /* In the location list window, use the referenced location list */
2985 qi = wp->w_llist_ref;
2986
2987 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988}
2989
2990/*
2991 * Update the cursor position in the quickfix window to the current error.
2992 * Return TRUE if there is a quickfix window.
2993 */
2994 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002995qf_win_pos_update(
2996 qf_info_T *qi,
2997 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003000 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 /*
3003 * Put the cursor on the current error in the quickfix window, so that
3004 * it's viewable.
3005 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003006 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (win != NULL
3008 && qf_index <= win->w_buffer->b_ml.ml_line_count
3009 && old_qf_index != qf_index)
3010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (qf_index > old_qf_index)
3012 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003013 win->w_redraw_top = old_qf_index;
3014 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 }
3016 else
3017 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003018 win->w_redraw_top = qf_index;
3019 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003021 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
3023 return win != NULL;
3024}
3025
3026/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003027 * Check whether the given window is displaying the specified quickfix/location
3028 * list buffer
3029 */
3030 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003031is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003032{
3033 /*
3034 * A window displaying the quickfix buffer will have the w_llist_ref field
3035 * set to NULL.
3036 * A window displaying a location list buffer will have the w_llist_ref
3037 * pointing to the location list.
3038 */
3039 if (bt_quickfix(win->w_buffer))
3040 if ((qi == &ql_info && win->w_llist_ref == NULL)
3041 || (qi != &ql_info && win->w_llist_ref == qi))
3042 return TRUE;
3043
3044 return FALSE;
3045}
3046
3047/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003048 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003049 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003050 */
3051 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003052qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003053{
3054 win_T *win;
3055
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003056 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003057 if (is_qf_win(win, qi))
3058 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003059
3060 return win;
3061}
3062
3063/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003064 * Find a quickfix buffer.
3065 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 */
3067 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003068qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003070 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003071 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
Bram Moolenaar9c102382006-05-03 21:26:49 +00003073 FOR_ALL_TAB_WINDOWS(tp, win)
3074 if (is_qf_win(win, qi))
3075 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003076
Bram Moolenaar9c102382006-05-03 21:26:49 +00003077 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078}
3079
3080/*
3081 * Find the quickfix buffer. If it exists, update the contents.
3082 */
3083 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003084qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003087 win_T *win;
3088 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
3091 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003092 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 if (buf != NULL)
3094 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003095 linenr_T old_line_count = buf->b_ml.ml_line_count;
3096
3097 if (old_last == NULL)
3098 /* set curwin/curbuf to buf and save a few things */
3099 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003101 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003102 {
3103 curwin_save = curwin;
3104 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02003105 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003106 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003107 }
3108
Bram Moolenaar864293a2016-06-02 13:40:04 +02003109 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003110
Bram Moolenaar864293a2016-06-02 13:40:04 +02003111 if (old_last == NULL)
3112 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003113 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003114
3115 /* restore curwin/curbuf and a few other things */
3116 aucmd_restbuf(&aco);
3117 }
3118
3119 /* Only redraw when added lines are visible. This avoids flickering
3120 * when the added lines are not visible. */
3121 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3122 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
3124}
3125
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003126/*
3127 * Set "w:quickfix_title" if "qi" has a title.
3128 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003129 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003130qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003131{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003132 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3133 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003134 qi->qf_lists[qi->qf_curlist].qf_title);
3135}
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137/*
3138 * Fill current buffer with quickfix errors, replacing any previous contents.
3139 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003140 * If "old_last" is not NULL append the items after this one.
3141 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3142 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 */
3144 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003145qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003147 linenr_T lnum;
3148 qfline_T *qfp;
3149 buf_T *errbuf;
3150 int len;
3151 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaar864293a2016-06-02 13:40:04 +02003153 if (old_last == NULL)
3154 {
3155 if (buf != curbuf)
3156 {
3157 EMSG2(_(e_intern2), "qf_fill_buffer()");
3158 return;
3159 }
3160
3161 /* delete all existing lines */
3162 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3163 (void)ml_delete((linenr_T)1, FALSE);
3164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
3166 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003167 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 {
3169 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003170 if (old_last == NULL)
3171 {
3172 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3173 lnum = 0;
3174 }
3175 else
3176 {
3177 qfp = old_last->qf_next;
3178 lnum = buf->b_ml.ml_line_count;
3179 }
3180 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
3182 if (qfp->qf_fnum != 0
3183 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3184 && errbuf->b_fname != NULL)
3185 {
3186 if (qfp->qf_type == 1) /* :helpgrep */
3187 STRCPY(IObuff, gettail(errbuf->b_fname));
3188 else
3189 STRCPY(IObuff, errbuf->b_fname);
3190 len = (int)STRLEN(IObuff);
3191 }
3192 else
3193 len = 0;
3194 IObuff[len++] = '|';
3195
3196 if (qfp->qf_lnum > 0)
3197 {
3198 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3199 len += (int)STRLEN(IObuff + len);
3200
3201 if (qfp->qf_col > 0)
3202 {
3203 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3204 len += (int)STRLEN(IObuff + len);
3205 }
3206
3207 sprintf((char *)IObuff + len, "%s",
3208 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3209 len += (int)STRLEN(IObuff + len);
3210 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003211 else if (qfp->qf_pattern != NULL)
3212 {
3213 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3214 len += (int)STRLEN(IObuff + len);
3215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 IObuff[len++] = '|';
3217 IObuff[len++] = ' ';
3218
3219 /* Remove newlines and leading whitespace from the text.
3220 * For an unrecognized line keep the indent, the compiler may
3221 * mark a word with ^^^^. */
3222 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3223 IObuff + len, IOSIZE - len);
3224
Bram Moolenaar864293a2016-06-02 13:40:04 +02003225 if (ml_append_buf(buf, lnum, IObuff,
3226 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003228 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003230 if (qfp == NULL)
3231 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003233
3234 if (old_last == NULL)
3235 /* Delete the empty line which is now at the end */
3236 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 }
3238
3239 /* correct cursor position */
3240 check_lnums(TRUE);
3241
Bram Moolenaar864293a2016-06-02 13:40:04 +02003242 if (old_last == NULL)
3243 {
3244 /* Set the 'filetype' to "qf" each time after filling the buffer.
3245 * This resembles reading a file into a buffer, it's more logical when
3246 * using autocommands. */
3247 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3248 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003251 keep_filetype = TRUE; /* don't detect 'filetype' */
3252 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003254 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003256 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003258 /* make sure it will be redrawn */
3259 redraw_curbuf_later(NOT_VALID);
3260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262 /* Restore KeyTyped, setting 'filetype' may reset it. */
3263 KeyTyped = old_KeyTyped;
3264}
3265
3266#endif /* FEAT_WINDOWS */
3267
3268/*
3269 * Return TRUE if "buf" is the quickfix buffer.
3270 */
3271 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003272bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003274 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275}
3276
3277/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003278 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3279 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 */
3281 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003282bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003284 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3285 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286}
3287
3288/*
3289 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3290 */
3291 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003292bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003294 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295}
3296
3297 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003298bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 if (bt_dontwrite(buf))
3301 {
3302 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3303 return TRUE;
3304 }
3305 return FALSE;
3306}
3307
3308/*
3309 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3310 * and 'bufhidden'.
3311 */
3312 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003313buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3316 switch (buf->b_p_bh[0])
3317 {
3318 case 'u': /* "unload" */
3319 case 'w': /* "wipe" */
3320 case 'd': return FALSE; /* "delete" */
3321 case 'h': return TRUE; /* "hide" */
3322 }
3323 return (p_hid || cmdmod.hide);
3324}
3325
3326/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003327 * Return TRUE when using ":vimgrep" for ":grep".
3328 */
3329 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003330grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003331{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003332 return ((cmdidx == CMD_grep
3333 || cmdidx == CMD_lgrep
3334 || cmdidx == CMD_grepadd
3335 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003336 && STRCMP("internal",
3337 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3338}
3339
3340/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003341 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 */
3343 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003344ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003346 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 char_u *cmd;
3348 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003349 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003350 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003351 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003352#ifdef FEAT_AUTOCMD
3353 char_u *au_name = NULL;
3354
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003355 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3356 if (grep_internal(eap->cmdidx))
3357 {
3358 ex_vimgrep(eap);
3359 return;
3360 }
3361
Bram Moolenaar7c626922005-02-07 22:01:03 +00003362 switch (eap->cmdidx)
3363 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003364 case CMD_make: au_name = (char_u *)"make"; break;
3365 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3366 case CMD_grep: au_name = (char_u *)"grep"; break;
3367 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3368 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3369 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003370 default: break;
3371 }
3372 if (au_name != NULL)
3373 {
3374 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3375 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003376# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003377 if (did_throw || force_abort)
3378 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003379# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003380 }
3381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaara6557602006-02-04 22:43:20 +00003383 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3384 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003385 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003388 fname = get_mef_name();
3389 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003391 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
3393 /*
3394 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3395 */
3396 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3397 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003398 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 cmd = alloc(len);
3400 if (cmd == NULL)
3401 return;
3402 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3403 (char *)p_shq);
3404 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003405 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 /*
3407 * Output a newline if there's something else than the :make command that
3408 * was typed (in which case the cursor is in column 0).
3409 */
3410 if (msg_col == 0)
3411 msg_didout = FALSE;
3412 msg_start();
3413 MSG_PUTS(":!");
3414 msg_outtrans(cmd); /* show what we are doing */
3415
3416 /* let the shell know if we are redirecting output or not */
3417 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3418
3419#ifdef AMIGA
3420 out_flush();
3421 /* read window status report and redraw before message */
3422 (void)char_avail();
3423#endif
3424
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003425 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003426 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3427 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003428 && eap->cmdidx != CMD_lgrepadd),
3429 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003430 if (wp != NULL)
3431 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003432#ifdef FEAT_AUTOCMD
3433 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003434 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003435 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3436 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003437 if (qi->qf_curlist < qi->qf_listcount)
3438 res = qi->qf_lists[qi->qf_curlist].qf_count;
3439 else
3440 res = 0;
3441 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003442#endif
3443 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003444 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar7c626922005-02-07 22:01:03 +00003446 mch_remove(fname);
3447 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 vim_free(cmd);
3449}
3450
3451/*
3452 * Return the name for the errorfile, in allocated memory.
3453 * Find a new unique name when 'makeef' contains "##".
3454 * Returns NULL for error.
3455 */
3456 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003457get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 char_u *p;
3460 char_u *name;
3461 static int start = -1;
3462 static int off = 0;
3463#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003464 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465#endif
3466
3467 if (*p_mef == NUL)
3468 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003469 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 if (name == NULL)
3471 EMSG(_(e_notmp));
3472 return name;
3473 }
3474
3475 for (p = p_mef; *p; ++p)
3476 if (p[0] == '#' && p[1] == '#')
3477 break;
3478
3479 if (*p == NUL)
3480 return vim_strsave(p_mef);
3481
3482 /* Keep trying until the name doesn't exist yet. */
3483 for (;;)
3484 {
3485 if (start == -1)
3486 start = mch_get_pid();
3487 else
3488 off += 19;
3489
3490 name = alloc((unsigned)STRLEN(p_mef) + 30);
3491 if (name == NULL)
3492 break;
3493 STRCPY(name, p_mef);
3494 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3495 STRCAT(name, p + 2);
3496 if (mch_getperm(name) < 0
3497#ifdef HAVE_LSTAT
3498 /* Don't accept a symbolic link, its a security risk. */
3499 && mch_lstat((char *)name, &sb) < 0
3500#endif
3501 )
3502 break;
3503 vim_free(name);
3504 }
3505 return name;
3506}
3507
3508/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003509 * Returns the number of valid entries in the current quickfix/location list.
3510 */
3511 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003512qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003513{
3514 qf_info_T *qi = &ql_info;
3515 qfline_T *qfp;
3516 int i, sz = 0;
3517 int prev_fnum = 0;
3518
3519 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3520 {
3521 /* Location list */
3522 qi = GET_LOC_LIST(curwin);
3523 if (qi == NULL)
3524 return 0;
3525 }
3526
3527 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003528 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003529 ++i, qfp = qfp->qf_next)
3530 {
3531 if (qfp->qf_valid)
3532 {
3533 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3534 sz++; /* Count all valid entries */
3535 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3536 {
3537 /* Count the number of files */
3538 sz++;
3539 prev_fnum = qfp->qf_fnum;
3540 }
3541 }
3542 }
3543
3544 return sz;
3545}
3546
3547/*
3548 * Returns the current index of the quickfix/location list.
3549 * Returns 0 if there is an error.
3550 */
3551 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003552qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003553{
3554 qf_info_T *qi = &ql_info;
3555
3556 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3557 {
3558 /* Location list */
3559 qi = GET_LOC_LIST(curwin);
3560 if (qi == NULL)
3561 return 0;
3562 }
3563
3564 return qi->qf_lists[qi->qf_curlist].qf_index;
3565}
3566
3567/*
3568 * Returns the current index in the quickfix/location list (counting only valid
3569 * entries). If no valid entries are in the list, then returns 1.
3570 */
3571 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003572qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003573{
3574 qf_info_T *qi = &ql_info;
3575 qf_list_T *qfl;
3576 qfline_T *qfp;
3577 int i, eidx = 0;
3578 int prev_fnum = 0;
3579
3580 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3581 {
3582 /* Location list */
3583 qi = GET_LOC_LIST(curwin);
3584 if (qi == NULL)
3585 return 1;
3586 }
3587
3588 qfl = &qi->qf_lists[qi->qf_curlist];
3589 qfp = qfl->qf_start;
3590
3591 /* check if the list has valid errors */
3592 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3593 return 1;
3594
3595 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3596 {
3597 if (qfp->qf_valid)
3598 {
3599 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3600 {
3601 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3602 {
3603 /* Count the number of files */
3604 eidx++;
3605 prev_fnum = qfp->qf_fnum;
3606 }
3607 }
3608 else
3609 eidx++;
3610 }
3611 }
3612
3613 return eidx ? eidx : 1;
3614}
3615
3616/*
3617 * Get the 'n'th valid error entry in the quickfix or location list.
3618 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3619 * For :cdo and :ldo returns the 'n'th valid error entry.
3620 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3621 */
3622 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003623qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003624{
3625 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3626 qfline_T *qfp = qfl->qf_start;
3627 int i, eidx;
3628 int prev_fnum = 0;
3629
3630 /* check if the list has valid errors */
3631 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3632 return 1;
3633
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003634 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003635 i++, qfp = qfp->qf_next)
3636 {
3637 if (qfp->qf_valid)
3638 {
3639 if (fdo)
3640 {
3641 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3642 {
3643 /* Count the number of files */
3644 eidx++;
3645 prev_fnum = qfp->qf_fnum;
3646 }
3647 }
3648 else
3649 eidx++;
3650 }
3651
3652 if (eidx == n)
3653 break;
3654 }
3655
3656 if (i <= qfl->qf_count)
3657 return i;
3658 else
3659 return 1;
3660}
3661
3662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003664 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003665 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 */
3667 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003668ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003670 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003671 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003672
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003673 if (eap->cmdidx == CMD_ll
3674 || eap->cmdidx == CMD_lrewind
3675 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003676 || eap->cmdidx == CMD_llast
3677 || eap->cmdidx == CMD_ldo
3678 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003679 {
3680 qi = GET_LOC_LIST(curwin);
3681 if (qi == NULL)
3682 {
3683 EMSG(_(e_loclist));
3684 return;
3685 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003686 }
3687
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003688 if (eap->addr_count > 0)
3689 errornr = (int)eap->line2;
3690 else
3691 {
3692 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3693 errornr = 0;
3694 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3695 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3696 errornr = 1;
3697 else
3698 errornr = 32767;
3699 }
3700
3701 /* For cdo and ldo commands, jump to the nth valid error.
3702 * For cfdo and lfdo commands, jump to the nth valid file entry.
3703 */
3704 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3705 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3706 errornr = qf_get_nth_valid_entry(qi,
3707 eap->addr_count > 0 ? (int)eap->line1 : 1,
3708 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3709
3710 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711}
3712
3713/*
3714 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003715 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003716 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 */
3718 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003719ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003721 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003722 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003723
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003724 if (eap->cmdidx == CMD_lnext
3725 || eap->cmdidx == CMD_lNext
3726 || eap->cmdidx == CMD_lprevious
3727 || eap->cmdidx == CMD_lnfile
3728 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003729 || eap->cmdidx == CMD_lpfile
3730 || eap->cmdidx == CMD_ldo
3731 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003732 {
3733 qi = GET_LOC_LIST(curwin);
3734 if (qi == NULL)
3735 {
3736 EMSG(_(e_loclist));
3737 return;
3738 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003739 }
3740
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003741 if (eap->addr_count > 0 &&
3742 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3743 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3744 errornr = (int)eap->line2;
3745 else
3746 errornr = 1;
3747
3748 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3749 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003751 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3752 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003754 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3755 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 ? BACKWARD_FILE
3757 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003758 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759}
3760
3761/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003762 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003763 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 */
3765 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003766ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003768 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003769 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003770#ifdef FEAT_AUTOCMD
3771 char_u *au_name = NULL;
3772#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003773
3774 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003775 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003776 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003777
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003778#ifdef FEAT_AUTOCMD
3779 switch (eap->cmdidx)
3780 {
3781 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3782 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3783 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3784 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3785 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3786 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3787 default: break;
3788 }
3789 if (au_name != NULL)
3790 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3791#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003792#ifdef FEAT_BROWSE
3793 if (cmdmod.browse)
3794 {
3795 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3796 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3797 if (browse_file == NULL)
3798 return;
3799 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3800 vim_free(browse_file);
3801 }
3802 else
3803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003805 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003806
3807 /*
3808 * This function is used by the :cfile, :cgetfile and :caddfile
3809 * commands.
3810 * :cfile always creates a new quickfix list and jumps to the
3811 * first error.
3812 * :cgetfile creates a new quickfix list but doesn't jump to the
3813 * first error.
3814 * :caddfile adds to an existing quickfix list. If there is no
3815 * quickfix list then a new list is created.
3816 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003817 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003818 && eap->cmdidx != CMD_laddfile),
3819 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003820 && (eap->cmdidx == CMD_cfile
3821 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003822 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003823#ifdef FEAT_AUTOCMD
3824 if (au_name != NULL)
3825 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3826#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003827 if (wp != NULL)
3828 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003829 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003830 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003831
3832 else
3833 {
3834#ifdef FEAT_AUTOCMD
3835 if (au_name != NULL)
3836 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3837#endif
3838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839}
3840
3841/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003842 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003843 * ":vimgrepadd {pattern} file(s)"
3844 * ":lvimgrep {pattern} file(s)"
3845 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003846 */
3847 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003848ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003849{
Bram Moolenaar81695252004-12-29 20:58:21 +00003850 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003851 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003852 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003853 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003854 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003855 char_u *s;
3856 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003857 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003858 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003859#ifdef FEAT_AUTOCMD
3860 qfline_T *cur_qf_start;
3861#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003862 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003863 buf_T *buf;
3864 int duplicate_name = FALSE;
3865 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003866 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003867 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003868 buf_T *first_match_buf = NULL;
3869 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003870 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003871#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3872 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003873#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003874 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003875 int flags = 0;
3876 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003877 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003878 char_u *dirname_start = NULL;
3879 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003880 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003881#ifdef FEAT_AUTOCMD
3882 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003883
3884 switch (eap->cmdidx)
3885 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003886 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3887 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3888 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003889 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003890 case CMD_grep: au_name = (char_u *)"grep"; break;
3891 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3892 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3893 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003894 default: break;
3895 }
3896 if (au_name != NULL)
3897 {
3898 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3899 curbuf->b_fname, TRUE, curbuf);
3900 if (did_throw || force_abort)
3901 return;
3902 }
3903#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003904
Bram Moolenaar754b5602006-02-09 23:53:20 +00003905 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003906 || eap->cmdidx == CMD_lvimgrep
3907 || eap->cmdidx == CMD_lgrepadd
3908 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003909 {
3910 qi = ll_get_or_alloc_list(curwin);
3911 if (qi == NULL)
3912 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003913 }
3914
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003915 if (eap->addr_count > 0)
3916 tomatch = eap->line2;
3917 else
3918 tomatch = MAXLNUM;
3919
Bram Moolenaar81695252004-12-29 20:58:21 +00003920 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003921 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003922 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003923 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003924 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003925 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003926 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003927 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003928 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003929
3930 if (s != NULL && *s == NUL)
3931 {
3932 /* Pattern is empty, use last search pattern. */
3933 if (last_search_pat() == NULL)
3934 {
3935 EMSG(_(e_noprevre));
3936 goto theend;
3937 }
3938 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3939 }
3940 else
3941 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3942
Bram Moolenaar86b68352004-12-27 21:59:20 +00003943 if (regmatch.regprog == NULL)
3944 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003945 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003946 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003947
3948 p = skipwhite(p);
3949 if (*p == NUL)
3950 {
3951 EMSG(_("E683: File name missing or invalid pattern"));
3952 goto theend;
3953 }
3954
Bram Moolenaar754b5602006-02-09 23:53:20 +00003955 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003956 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003957 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003958 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003959 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003960
3961 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003962 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003963 goto theend;
3964 if (fcount == 0)
3965 {
3966 EMSG(_(e_nomatch));
3967 goto theend;
3968 }
3969
Bram Moolenaarb86a3432016-01-10 16:00:53 +01003970 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
3971 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003972 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003973 {
3974 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02003975 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01003976 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003977
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003978 /* Remember the current directory, because a BufRead autocommand that does
3979 * ":lcd %:p:h" changes the meaning of short path names. */
3980 mch_dirname(dirname_start, MAXPATHL);
3981
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003982#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003983 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003984 * changing the current quickfix list. */
3985 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3986#endif
3987
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003988 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003989 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003990 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003991 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003992 if (time(NULL) > seconds)
3993 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003994 /* Display the file name every second or so, show the user we are
3995 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003996 seconds = time(NULL);
3997 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003998 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003999 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004000 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004001 else
4002 {
4003 msg_outtrans(p);
4004 vim_free(p);
4005 }
4006 msg_clr_eos();
4007 msg_didout = FALSE; /* overwrite this message */
4008 msg_nowait = TRUE; /* don't wait for this message */
4009 msg_col = 0;
4010 out_flush();
4011 }
4012
Bram Moolenaar81695252004-12-29 20:58:21 +00004013 buf = buflist_findname_exp(fnames[fi]);
4014 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4015 {
4016 /* Remember that a buffer with this name already exists. */
4017 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004018 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004019 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004020
4021#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4022 /* Don't do Filetype autocommands to avoid loading syntax and
4023 * indent scripts, a great speed improvement. */
4024 save_ei = au_event_disable(",Filetype");
4025#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004026 /* Don't use modelines here, it's useless. */
4027 save_mls = p_mls;
4028 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004029
4030 /* Load file into a buffer, so that 'fileencoding' is detected,
4031 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004032 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004033
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004034 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004035#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4036 au_event_restore(save_ei);
4037#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004038 }
4039 else
4040 /* Use existing, loaded buffer. */
4041 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004042
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004043#ifdef FEAT_AUTOCMD
4044 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4045 {
4046 int idx;
4047
4048 /* Autocommands changed the quickfix list. Find the one we were
4049 * using and restore it. */
4050 for (idx = 0; idx < LISTCOUNT; ++idx)
4051 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4052 {
4053 qi->qf_curlist = idx;
4054 break;
4055 }
4056 if (idx == LISTCOUNT)
4057 {
4058 /* List cannot be found, create a new one. */
4059 qf_new_list(qi, *eap->cmdlinep);
4060 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4061 }
4062 }
4063#endif
4064
Bram Moolenaar81695252004-12-29 20:58:21 +00004065 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004066 {
4067 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004068 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004069 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004070 else
4071 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004072 /* Try for a match in all lines of the buffer.
4073 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004074 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004075 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4076 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004077 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004078 col = 0;
4079 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004080 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004081 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004082 ;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004083 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004084 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004085 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004086 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00004087 ml_get_buf(buf,
4088 regmatch.startpos[0].lnum + lnum, FALSE),
4089 regmatch.startpos[0].lnum + lnum,
4090 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004091 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004092 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004093 0, /* nr */
4094 0, /* type */
4095 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004096 ) == FAIL)
4097 {
4098 got_int = TRUE;
4099 break;
4100 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004101 found_match = TRUE;
4102 if (--tomatch == 0)
4103 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004104 if ((flags & VGR_GLOBAL) == 0
4105 || regmatch.endpos[0].lnum > 0)
4106 break;
4107 col = regmatch.endpos[0].col
4108 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004109 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004110 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004111 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004112 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004113 if (got_int)
4114 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004115 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004116#ifdef FEAT_AUTOCMD
4117 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4118#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004119
4120 if (using_dummy)
4121 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004122 if (found_match && first_match_buf == NULL)
4123 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004124 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004125 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004126 /* Never keep a dummy buffer if there is another buffer
4127 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004128 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004129 buf = NULL;
4130 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004131 else if (!cmdmod.hide
4132 || buf->b_p_bh[0] == 'u' /* "unload" */
4133 || buf->b_p_bh[0] == 'w' /* "wipe" */
4134 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004135 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004136 /* When no match was found we don't need to remember the
4137 * buffer, wipe it out. If there was a match and it
4138 * wasn't the first one or we won't jump there: only
4139 * unload the buffer.
4140 * Ignore 'hidden' here, because it may lead to having too
4141 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004142 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004143 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004144 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004145 buf = NULL;
4146 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004147 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004148 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004149 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004150 buf = NULL;
4151 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004152 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004153
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004154 if (buf != NULL)
4155 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004156 /* If the buffer is still loaded we need to use the
4157 * directory we jumped to below. */
4158 if (buf == first_match_buf
4159 && target_dir == NULL
4160 && STRCMP(dirname_start, dirname_now) != 0)
4161 target_dir = vim_strsave(dirname_now);
4162
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004163 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004164 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004165 * need to be done (again). But not the window-local
4166 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004167 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004168#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004169 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4170 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004171#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004172 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004173 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004174 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004175 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004176 }
4177 }
4178
4179 FreeWild(fcount, fnames);
4180
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004181 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4182 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4183 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004184
4185#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004186 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004187#endif
4188
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004189#ifdef FEAT_AUTOCMD
4190 if (au_name != NULL)
4191 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4192 curbuf->b_fname, TRUE, curbuf);
4193#endif
4194
Bram Moolenaar86b68352004-12-27 21:59:20 +00004195 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004196 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004197 {
4198 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004199 {
4200 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004201 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004202 if (buf != curbuf)
4203 /* If we jumped to another buffer redrawing will already be
4204 * taken care of. */
4205 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004206
4207 /* Jump to the directory used after loading the buffer. */
4208 if (curbuf == first_match_buf && target_dir != NULL)
4209 {
4210 exarg_T ea;
4211
4212 ea.arg = target_dir;
4213 ea.cmdidx = CMD_lcd;
4214 ex_cd(&ea);
4215 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004216 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004217 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004218 else
4219 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004220
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004221 /* If we loaded a dummy buffer into the current window, the autocommands
4222 * may have messed up things, need to redraw and recompute folds. */
4223 if (redraw_for_dummy)
4224 {
4225#ifdef FEAT_FOLDING
4226 foldUpdateAll(curwin);
4227#else
4228 redraw_later(NOT_VALID);
4229#endif
4230 }
4231
Bram Moolenaar86b68352004-12-27 21:59:20 +00004232theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004233 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004234 vim_free(dirname_now);
4235 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004236 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004237 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004238}
4239
4240/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004241 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00004242 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004243 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
4244 * If "s" is not NULL terminate the pattern with a NUL.
4245 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00004246 */
4247 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004248skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004249{
4250 int c;
4251
4252 if (vim_isIDc(*p))
4253 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004254 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004255 if (s != NULL)
4256 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004257 p = skiptowhite(p);
4258 if (s != NULL && *p != NUL)
4259 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004260 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004261 else
4262 {
4263 /* ":vimgrep /pattern/[g][j] fname" */
4264 if (s != NULL)
4265 *s = p + 1;
4266 c = *p;
4267 p = skip_regexp(p + 1, c, TRUE, NULL);
4268 if (*p != c)
4269 return NULL;
4270
4271 /* Truncate the pattern. */
4272 if (s != NULL)
4273 *p = NUL;
4274 ++p;
4275
4276 /* Find the flags */
4277 while (*p == 'g' || *p == 'j')
4278 {
4279 if (flags != NULL)
4280 {
4281 if (*p == 'g')
4282 *flags |= VGR_GLOBAL;
4283 else
4284 *flags |= VGR_NOJUMP;
4285 }
4286 ++p;
4287 }
4288 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004289 return p;
4290}
4291
4292/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004293 * Restore current working directory to "dirname_start" if they differ, taking
4294 * into account whether it is set locally or globally.
4295 */
4296 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004297restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004298{
4299 char_u *dirname_now = alloc(MAXPATHL);
4300
4301 if (NULL != dirname_now)
4302 {
4303 mch_dirname(dirname_now, MAXPATHL);
4304 if (STRCMP(dirname_start, dirname_now) != 0)
4305 {
4306 /* If the directory has changed, change it back by building up an
4307 * appropriate ex command and executing it. */
4308 exarg_T ea;
4309
4310 ea.arg = dirname_start;
4311 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4312 ex_cd(&ea);
4313 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004314 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004315 }
4316}
4317
4318/*
4319 * Load file "fname" into a dummy buffer and return the buffer pointer,
4320 * placing the directory resulting from the buffer load into the
4321 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4322 * prior to calling this function. Restores directory to "dirname_start" prior
4323 * to returning, if autocmds or the 'autochdir' option have changed it.
4324 *
4325 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4326 * or wipe_dummy_buffer() later!
4327 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004328 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004329 */
4330 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004331load_dummy_buffer(
4332 char_u *fname,
4333 char_u *dirname_start, /* in: old directory */
4334 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004335{
4336 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004337 bufref_T newbufref;
4338 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004339 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004340 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004341
4342 /* Allocate a buffer without putting it in the buffer list. */
4343 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4344 if (newbuf == NULL)
4345 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004346 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004347
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004348 /* Init the options. */
4349 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4350
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004351 /* need to open the memfile before putting the buffer in a window */
4352 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004353 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004354 /* set curwin/curbuf to buf and save a few things */
4355 aucmd_prepbuf(&aco, newbuf);
4356
4357 /* Need to set the filename for autocommands. */
4358 (void)setfname(curbuf, fname, NULL, FALSE);
4359
Bram Moolenaar81695252004-12-29 20:58:21 +00004360 /* Create swap file now to avoid the ATTENTION message. */
4361 check_need_swap(TRUE);
4362
4363 /* Remove the "dummy" flag, otherwise autocommands may not
4364 * work. */
4365 curbuf->b_flags &= ~BF_DUMMY;
4366
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004367 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004368 if (readfile(fname, NULL,
4369 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4370 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004371 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004372 && !(curbuf->b_flags & BF_NEW))
4373 {
4374 failed = FALSE;
4375 if (curbuf != newbuf)
4376 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004377 /* Bloody autocommands changed the buffer! Can happen when
4378 * using netrw and editing a remote file. Use the current
4379 * buffer instead, delete the dummy one after restoring the
4380 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004381 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004382 newbuf = curbuf;
4383 }
4384 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004385
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004386 /* restore curwin/curbuf and a few other things */
4387 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004388 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4389 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004390
4391 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4392 * skip it. */
4393 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004394 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004395
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004396 /*
4397 * When autocommands/'autochdir' option changed directory: go back.
4398 * Let the caller know what the resulting dir was first, in case it is
4399 * important.
4400 */
4401 mch_dirname(resulting_dir, MAXPATHL);
4402 restore_start_dir(dirname_start);
4403
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004404 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004405 return NULL;
4406 if (failed)
4407 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004408 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004409 return NULL;
4410 }
4411 return newbuf;
4412}
4413
4414/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004415 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4416 * directory to "dirname_start" prior to returning, if autocmds or the
4417 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004418 */
4419 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004420wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004421{
4422 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004423 {
4424#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4425 cleanup_T cs;
4426
4427 /* Reset the error/interrupt/exception state here so that aborting()
4428 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4429 * work when got_int is set. */
4430 enter_cleanup(&cs);
4431#endif
4432
Bram Moolenaar81695252004-12-29 20:58:21 +00004433 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004434
4435#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4436 /* Restore the error/interrupt/exception state if not discarded by a
4437 * new aborting error, interrupt, or uncaught exception. */
4438 leave_cleanup(&cs);
4439#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004440 /* When autocommands/'autochdir' option changed directory: go back. */
4441 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004442 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004443}
4444
4445/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004446 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4447 * directory to "dirname_start" prior to returning, if autocmds or the
4448 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004449 */
4450 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004451unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004452{
4453 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004454 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004455 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004456
4457 /* When autocommands/'autochdir' option changed directory: go back. */
4458 restore_start_dir(dirname_start);
4459 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004460}
4461
Bram Moolenaar05159a02005-02-26 23:04:13 +00004462#if defined(FEAT_EVAL) || defined(PROTO)
4463/*
4464 * Add each quickfix error to list "list" as a dictionary.
4465 */
4466 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004467get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004468{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004469 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004470 dict_T *dict;
4471 char_u buf[2];
4472 qfline_T *qfp;
4473 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004474 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004475
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004476 if (wp != NULL)
4477 {
4478 qi = GET_LOC_LIST(wp);
4479 if (qi == NULL)
4480 return FAIL;
4481 }
4482
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004483 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004484 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004485 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004486
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004487 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4488 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004489 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004490 /* Handle entries with a non-existing buffer number. */
4491 bufnum = qfp->qf_fnum;
4492 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4493 bufnum = 0;
4494
Bram Moolenaar05159a02005-02-26 23:04:13 +00004495 if ((dict = dict_alloc()) == NULL)
4496 return FAIL;
4497 if (list_append_dict(list, dict) == FAIL)
4498 return FAIL;
4499
4500 buf[0] = qfp->qf_type;
4501 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004502 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004503 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4504 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4505 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4506 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004507 || dict_add_nr_str(dict, "pattern", 0L,
4508 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4509 || dict_add_nr_str(dict, "text", 0L,
4510 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004511 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4512 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4513 return FAIL;
4514
4515 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004516 if (qfp == NULL)
4517 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004518 }
4519 return OK;
4520}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004521
4522/*
4523 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004524 * of dictionaries. "title" will be copied to w:quickfix_title.
4525 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004526 */
4527 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004528set_errorlist(
4529 win_T *wp,
4530 list_T *list,
4531 int action,
4532 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004533{
4534 listitem_T *li;
4535 dict_T *d;
4536 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004537 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004538 long lnum;
4539 int col, nr;
4540 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004541#ifdef FEAT_WINDOWS
4542 qfline_T *old_last = NULL;
4543#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004544 int valid, status;
4545 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004546 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004547 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004548
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004549 if (wp != NULL)
4550 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004551 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004552 if (qi == NULL)
4553 return FAIL;
4554 }
4555
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004556 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004557 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004558 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004559#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004560 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4561 /* Adding to existing list, use last entry. */
4562 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004563#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004564 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004565 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004566 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004567 qf_store_title(qi, title);
4568 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004569
4570 for (li = list->lv_first; li != NULL; li = li->li_next)
4571 {
4572 if (li->li_tv.v_type != VAR_DICT)
4573 continue; /* Skip non-dict items */
4574
4575 d = li->li_tv.vval.v_dict;
4576 if (d == NULL)
4577 continue;
4578
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004579 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004580 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4581 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4582 col = (int)get_dict_number(d, (char_u *)"col");
4583 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4584 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004585 type = get_dict_string(d, (char_u *)"type", TRUE);
4586 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4587 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004588 if (text == NULL)
4589 text = vim_strsave((char_u *)"");
4590
4591 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004592 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004593 valid = FALSE;
4594
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004595 /* Mark entries with non-existing buffer number as not valid. Give the
4596 * error message only once. */
4597 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4598 {
4599 if (!did_bufnr_emsg)
4600 {
4601 did_bufnr_emsg = TRUE;
4602 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4603 }
4604 valid = FALSE;
4605 bufnum = 0;
4606 }
4607
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004608 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004609 NULL, /* dir */
4610 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004611 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004612 text,
4613 lnum,
4614 col,
4615 vcol, /* vis_col */
4616 pattern, /* search pattern */
4617 nr,
4618 type == NULL ? NUL : *type,
4619 valid);
4620
4621 vim_free(filename);
4622 vim_free(pattern);
4623 vim_free(text);
4624 vim_free(type);
4625
4626 if (status == FAIL)
4627 {
4628 retval = FAIL;
4629 break;
4630 }
4631 }
4632
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004633 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004634 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004635 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4636 else
4637 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004638 if (action != 'a') {
4639 qi->qf_lists[qi->qf_curlist].qf_ptr =
4640 qi->qf_lists[qi->qf_curlist].qf_start;
4641 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4642 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4643 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004644
4645#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004646 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004647 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004648#endif
4649
4650 return retval;
4651}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004652#endif
4653
Bram Moolenaar81695252004-12-29 20:58:21 +00004654/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004655 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004656 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004657 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004658 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004659 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004660 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004661 */
4662 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004663ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004664{
4665 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004666 qf_info_T *qi = &ql_info;
4667
Bram Moolenaardb552d602006-03-23 22:59:57 +00004668 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4669 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004670 {
4671 qi = ll_get_or_alloc_list(curwin);
4672 if (qi == NULL)
4673 return;
4674 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004675
4676 if (*eap->arg == NUL)
4677 buf = curbuf;
4678 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4679 buf = buflist_findnr(atoi((char *)eap->arg));
4680 if (buf == NULL)
4681 EMSG(_(e_invarg));
4682 else if (buf->b_ml.ml_mfp == NULL)
4683 EMSG(_("E681: Buffer is not loaded"));
4684 else
4685 {
4686 if (eap->addr_count == 0)
4687 {
4688 eap->line1 = 1;
4689 eap->line2 = buf->b_ml.ml_line_count;
4690 }
4691 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4692 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4693 EMSG(_(e_invrange));
4694 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004695 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004696 char_u *qf_title = *eap->cmdlinep;
4697
4698 if (buf->b_sfname)
4699 {
4700 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4701 (char *)qf_title, (char *)buf->b_sfname);
4702 qf_title = IObuff;
4703 }
4704
Bram Moolenaardb552d602006-03-23 22:59:57 +00004705 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4706 (eap->cmdidx != CMD_caddbuffer
4707 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004708 eap->line1, eap->line2,
4709 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004710 && (eap->cmdidx == CMD_cbuffer
4711 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004712 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4713 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004714 }
4715}
4716
Bram Moolenaar1e015462005-09-25 22:16:38 +00004717#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004718/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004719 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4720 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004721 */
4722 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004723ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004724{
4725 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004726 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004727
Bram Moolenaardb552d602006-03-23 22:59:57 +00004728 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4729 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004730 {
4731 qi = ll_get_or_alloc_list(curwin);
4732 if (qi == NULL)
4733 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004734 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004735
Bram Moolenaar4770d092006-01-12 23:22:24 +00004736 /* Evaluate the expression. When the result is a string or a list we can
4737 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004738 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004739 if (tv != NULL)
4740 {
4741 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4742 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4743 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004744 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004745 (eap->cmdidx != CMD_caddexpr
4746 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004747 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004748 && (eap->cmdidx == CMD_cexpr
4749 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004750 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004751 }
4752 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004753 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004754 free_tv(tv);
4755 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004756}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004757#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004758
4759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 * ":helpgrep {pattern}"
4761 */
4762 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004763ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
4765 regmatch_T regmatch;
4766 char_u *save_cpo;
4767 char_u *p;
4768 int fcount;
4769 char_u **fnames;
4770 FILE *fd;
4771 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004773#ifdef FEAT_MULTI_LANG
4774 char_u *lang;
4775#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004776 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004777 int new_qi = FALSE;
4778 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004779#ifdef FEAT_AUTOCMD
4780 char_u *au_name = NULL;
4781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004783#ifdef FEAT_MULTI_LANG
4784 /* Check for a specified language */
4785 lang = check_help_lang(eap->arg);
4786#endif
4787
Bram Moolenaar73633f82012-01-20 13:39:07 +01004788#ifdef FEAT_AUTOCMD
4789 switch (eap->cmdidx)
4790 {
4791 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4792 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4793 default: break;
4794 }
4795 if (au_name != NULL)
4796 {
4797 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4798 curbuf->b_fname, TRUE, curbuf);
4799 if (did_throw || force_abort)
4800 return;
4801 }
4802#endif
4803
4804 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4805 save_cpo = p_cpo;
4806 p_cpo = empty_option;
4807
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004808 if (eap->cmdidx == CMD_lhelpgrep)
4809 {
4810 /* Find an existing help window */
4811 FOR_ALL_WINDOWS(wp)
4812 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4813 break;
4814
4815 if (wp == NULL) /* Help window not found */
4816 qi = NULL;
4817 else
4818 qi = wp->w_llist;
4819
4820 if (qi == NULL)
4821 {
4822 /* Allocate a new location list for help text matches */
4823 if ((qi = ll_new_list()) == NULL)
4824 return;
4825 new_qi = TRUE;
4826 }
4827 }
4828
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4830 regmatch.rm_ic = FALSE;
4831 if (regmatch.regprog != NULL)
4832 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004833#ifdef FEAT_MBYTE
4834 vimconv_T vc;
4835
4836 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4837 * differs. */
4838 vc.vc_type = CONV_NONE;
4839 if (!enc_utf8)
4840 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4841#endif
4842
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004844 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
4846 /* Go through all directories in 'runtimepath' */
4847 p = p_rtp;
4848 while (*p != NUL && !got_int)
4849 {
4850 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4851
4852 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4853 add_pathsep(NameBuff);
4854 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4855 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4856 &fnames, EW_FILE|EW_SILENT) == OK
4857 && fcount > 0)
4858 {
4859 for (fi = 0; fi < fcount && !got_int; ++fi)
4860 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004861#ifdef FEAT_MULTI_LANG
4862 /* Skip files for a different language. */
4863 if (lang != NULL
4864 && STRNICMP(lang, fnames[fi]
4865 + STRLEN(fnames[fi]) - 3, 2) != 0
4866 && !(STRNICMP(lang, "en", 2) == 0
4867 && STRNICMP("txt", fnames[fi]
4868 + STRLEN(fnames[fi]) - 3, 3) == 0))
4869 continue;
4870#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004871 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 if (fd != NULL)
4873 {
4874 lnum = 1;
4875 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4876 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004877 char_u *line = IObuff;
4878#ifdef FEAT_MBYTE
4879 /* Convert a line if 'encoding' is not utf-8 and
4880 * the line contains a non-ASCII character. */
4881 if (vc.vc_type != CONV_NONE
4882 && has_non_ascii(IObuff)) {
4883 line = string_convert(&vc, IObuff, NULL);
4884 if (line == NULL)
4885 line = IObuff;
4886 }
4887#endif
4888
4889 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004891 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004894 while (l > 0 && line[l - 1] <= ' ')
4895 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004897 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 NULL, /* dir */
4899 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004900 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004901 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004903 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004904 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004905 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004906 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 0, /* nr */
4908 1, /* type */
4909 TRUE /* valid */
4910 ) == FAIL)
4911 {
4912 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004913#ifdef FEAT_MBYTE
4914 if (line != IObuff)
4915 vim_free(line);
4916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 break;
4918 }
4919 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004920#ifdef FEAT_MBYTE
4921 if (line != IObuff)
4922 vim_free(line);
4923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 ++lnum;
4925 line_breakcheck();
4926 }
4927 fclose(fd);
4928 }
4929 }
4930 FreeWild(fcount, fnames);
4931 }
4932 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004933
Bram Moolenaar473de612013-06-08 18:19:48 +02004934 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004935#ifdef FEAT_MBYTE
4936 if (vc.vc_type != CONV_NONE)
4937 convert_setup(&vc, NULL, NULL);
4938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004940 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4941 qi->qf_lists[qi->qf_curlist].qf_ptr =
4942 qi->qf_lists[qi->qf_curlist].qf_start;
4943 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004946 if (p_cpo == empty_option)
4947 p_cpo = save_cpo;
4948 else
4949 /* Darn, some plugin changed the value. */
4950 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004953 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954#endif
4955
Bram Moolenaar73633f82012-01-20 13:39:07 +01004956#ifdef FEAT_AUTOCMD
4957 if (au_name != NULL)
4958 {
4959 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4960 curbuf->b_fname, TRUE, curbuf);
4961 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4962 /* autocommands made "qi" invalid */
4963 return;
4964 }
4965#endif
4966
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004968 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004969 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004970 else
4971 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004972
4973 if (eap->cmdidx == CMD_lhelpgrep)
4974 {
4975 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004976 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004977 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4978 {
4979 if (new_qi)
4980 ll_free_all(&qi);
4981 }
4982 else if (curwin->w_llist == NULL)
4983 curwin->w_llist = qi;
4984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985}
4986
4987#endif /* FEAT_QUICKFIX */