blob: 1145896e1ee99e8ead310bb0963c60b15a6fed60 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
36 char_u *qf_pattern; /* search pattern for the error */
37 char_u *qf_text; /* description of the error */
38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared; /* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000042 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000043};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
Bram Moolenaard12f5c12006-01-25 22:10:52 +000050typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000051{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000052 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020053 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_ptr; /* pointer to the current error */
55 int qf_count; /* number of errors (0 means no error list) */
56 int qf_index; /* current index in the error list */
57 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020058 char_u *qf_title; /* title derived from the command that created
59 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000060} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaard12f5c12006-01-25 22:10:52 +000062struct qf_info_S
63{
64 /*
65 * Count of references to this list. Used only for location lists.
66 * When a location list window reference this list, qf_refcount
67 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
68 * reaches 0, the list is freed.
69 */
70 int qf_refcount;
71 int qf_listcount; /* current number of lists */
72 int qf_curlist; /* current error list */
73 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar361c8f02016-07-02 15:41:47 +020074
75 int qf_dir_curlist; /* error list for qf_dir_stack */
76 struct dir_stack_T *qf_dir_stack;
77 char_u *qf_directory;
78 struct dir_stack_T *qf_file_stack;
79 char_u *qf_currfile;
80 int qf_multiline;
81 int qf_multiignore;
82 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000083};
84
85static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaar68b76a62005-03-25 21:53:48 +000087#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89/*
90 * Structure used to hold the info of one part of 'errorformat'
91 */
Bram Moolenaar01265852006-03-20 21:50:15 +000092typedef struct efm_S efm_T;
93struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000094{
95 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000096 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
98 char_u prefix; /* prefix of this format line: */
99 /* 'D' enter directory */
100 /* 'X' leave directory */
101 /* 'A' start of multi-line message */
102 /* 'E' error message */
103 /* 'W' warning message */
104 /* 'I' informational message */
105 /* 'C' continuation line */
106 /* 'Z' end of multi-line message */
107 /* 'G' general, unspecific message */
108 /* 'P' push file (partial) message */
109 /* 'Q' pop/quit file (partial) message */
110 /* 'O' overread (partial) message */
111 char_u flags; /* additional flags given in prefix */
112 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000113 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115};
116
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title);
118static void qf_store_title(qf_info_T *qi, char_u *title);
119static void qf_new_list(qf_info_T *qi, char_u *qf_title);
120static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200121static int qf_add_entry(qf_info_T *qi, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100122static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void qf_free(qf_info_T *qi, int idx);
124static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200125static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
126static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200128static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100129static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
130static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
133static int is_qf_win(win_T *win, qf_info_T *qi);
134static win_T *qf_find_win(qf_info_T *qi);
135static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200136static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200138static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static char_u *get_mef_name(void);
141static void restore_start_dir(char_u *dirname_start);
142static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
143static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
144static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
145static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000147/* Quickfix window check helper macro */
148#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
149/* Location list window check helper macro */
150#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
151/*
152 * Return location list for window 'wp'
153 * For location list window, return the referenced location list
154 */
155#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000158 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200159 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 * Return -1 for error, number of errors for success.
161 */
162 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100163qf_init(
164 win_T *wp,
165 char_u *efile,
166 char_u *errorformat,
167 int newlist, /* TRUE: start a new error list */
168 char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000170 qf_info_T *qi = &ql_info;
171
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000173 {
174 qi = ll_get_or_alloc_list(wp);
175 if (qi == NULL)
176 return FAIL;
177 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000178
179 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200180 (linenr_T)0, (linenr_T)0,
181 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182}
183
184/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200185 * Maximum number of bytes allowed per line while reading a errorfile.
186 */
187#define LINE_MAXLEN 4096
188
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200189static struct fmtpattern
190{
191 char_u convchar;
192 char *pattern;
193} fmt_pat[FMT_PATTERNS] =
194 {
195 {'f', ".\\+"}, /* only used when at end */
196 {'n', "\\d\\+"},
197 {'l', "\\d\\+"},
198 {'c', "\\d\\+"},
199 {'t', "."},
200 {'m', ".\\+"},
201 {'r', ".*"},
202 {'p', "[- .]*"},
203 {'v', "\\d\\+"},
204 {'s', ".\\+"}
205 };
206
207/*
208 * Converts a 'errorformat' string to regular expression pattern
209 */
210 static int
211efm_to_regpat(
212 char_u *efm,
213 int len,
214 efm_T *fmt_ptr,
215 char_u *regpat,
216 char_u *errmsg)
217{
218 char_u *ptr;
219 char_u *efmp;
220 char_u *srcptr;
221 int round;
222 int idx = 0;
223
224 /*
225 * Build regexp pattern from current 'errorformat' option
226 */
227 ptr = regpat;
228 *ptr++ = '^';
229 round = 0;
230 for (efmp = efm; efmp < efm + len; ++efmp)
231 {
232 if (*efmp == '%')
233 {
234 ++efmp;
235 for (idx = 0; idx < FMT_PATTERNS; ++idx)
236 if (fmt_pat[idx].convchar == *efmp)
237 break;
238 if (idx < FMT_PATTERNS)
239 {
240 if (fmt_ptr->addr[idx])
241 {
242 sprintf((char *)errmsg,
243 _("E372: Too many %%%c in format string"), *efmp);
244 EMSG(errmsg);
245 return -1;
246 }
247 if ((idx
248 && idx < 6
249 && vim_strchr((char_u *)"DXOPQ",
250 fmt_ptr->prefix) != NULL)
251 || (idx == 6
252 && vim_strchr((char_u *)"OPQ",
253 fmt_ptr->prefix) == NULL))
254 {
255 sprintf((char *)errmsg,
256 _("E373: Unexpected %%%c in format string"), *efmp);
257 EMSG(errmsg);
258 return -1;
259 }
260 fmt_ptr->addr[idx] = (char_u)++round;
261 *ptr++ = '\\';
262 *ptr++ = '(';
263#ifdef BACKSLASH_IN_FILENAME
264 if (*efmp == 'f')
265 {
266 /* Also match "c:" in the file name, even when
267 * checking for a colon next: "%f:".
268 * "\%(\a:\)\=" */
269 STRCPY(ptr, "\\%(\\a:\\)\\=");
270 ptr += 10;
271 }
272#endif
273 if (*efmp == 'f' && efmp[1] != NUL)
274 {
275 if (efmp[1] != '\\' && efmp[1] != '%')
276 {
277 /* A file name may contain spaces, but this isn't
278 * in "\f". For "%f:%l:%m" there may be a ":" in
279 * the file name. Use ".\{-1,}x" instead (x is
280 * the next character), the requirement that :999:
281 * follows should work. */
282 STRCPY(ptr, ".\\{-1,}");
283 ptr += 7;
284 }
285 else
286 {
287 /* File name followed by '\\' or '%': include as
288 * many file name chars as possible. */
289 STRCPY(ptr, "\\f\\+");
290 ptr += 4;
291 }
292 }
293 else
294 {
295 srcptr = (char_u *)fmt_pat[idx].pattern;
296 while ((*ptr = *srcptr++) != NUL)
297 ++ptr;
298 }
299 *ptr++ = '\\';
300 *ptr++ = ')';
301 }
302 else if (*efmp == '*')
303 {
304 if (*++efmp == '[' || *efmp == '\\')
305 {
306 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
307 {
308 if (efmp[1] == '^')
309 *ptr++ = *++efmp;
310 if (efmp < efm + len)
311 {
312 *ptr++ = *++efmp; /* could be ']' */
313 while (efmp < efm + len
314 && (*ptr++ = *++efmp) != ']')
315 /* skip */;
316 if (efmp == efm + len)
317 {
318 EMSG(_("E374: Missing ] in format string"));
319 return -1;
320 }
321 }
322 }
323 else if (efmp < efm + len) /* %*\D, %*\s etc. */
324 *ptr++ = *++efmp;
325 *ptr++ = '\\';
326 *ptr++ = '+';
327 }
328 else
329 {
330 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
331 sprintf((char *)errmsg,
332 _("E375: Unsupported %%%c in format string"), *efmp);
333 EMSG(errmsg);
334 return -1;
335 }
336 }
337 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
338 *ptr++ = *efmp; /* regexp magic characters */
339 else if (*efmp == '#')
340 *ptr++ = '*';
341 else if (*efmp == '>')
342 fmt_ptr->conthere = TRUE;
343 else if (efmp == efm + 1) /* analyse prefix */
344 {
345 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
346 fmt_ptr->flags = *efmp++;
347 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
348 fmt_ptr->prefix = *efmp;
349 else
350 {
351 sprintf((char *)errmsg,
352 _("E376: Invalid %%%c in format string prefix"), *efmp);
353 EMSG(errmsg);
354 return -1;
355 }
356 }
357 else
358 {
359 sprintf((char *)errmsg,
360 _("E377: Invalid %%%c in format string"), *efmp);
361 EMSG(errmsg);
362 return -1;
363 }
364 }
365 else /* copy normal character */
366 {
367 if (*efmp == '\\' && efmp + 1 < efm + len)
368 ++efmp;
369 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
370 *ptr++ = '\\'; /* escape regexp atoms */
371 if (*efmp)
372 *ptr++ = *efmp;
373 }
374 }
375 *ptr++ = '$';
376 *ptr = NUL;
377
378 return 0;
379}
380
381 static void
382free_efm_list(efm_T **efm_first)
383{
384 efm_T *efm_ptr;
385
386 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
387 {
388 *efm_first = efm_ptr->next;
389 vim_regfree(efm_ptr->prog);
390 vim_free(efm_ptr);
391 }
392}
393
394/* Parse 'errorformat' option */
395 static efm_T *
396parse_efm_option(char_u *efm)
397{
398 char_u *errmsg = NULL;
399 int errmsglen;
400 efm_T *fmt_ptr = NULL;
401 efm_T *fmt_first = NULL;
402 efm_T *fmt_last = NULL;
403 char_u *fmtstr = NULL;
404 int len;
405 int i;
406 int round;
407
408 errmsglen = CMDBUFFSIZE + 1;
409 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
410 if (errmsg == NULL)
411 goto parse_efm_end;
412
413 /*
414 * Get some space to modify the format string into.
415 */
416 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
417 for (round = FMT_PATTERNS; round > 0; )
418 i += (int)STRLEN(fmt_pat[--round].pattern);
419#ifdef COLON_IN_FILENAME
420 i += 12; /* "%f" can become twelve chars longer */
421#else
422 i += 2; /* "%f" can become two chars longer */
423#endif
424 if ((fmtstr = alloc(i)) == NULL)
425 goto parse_efm_error;
426
427 while (efm[0] != NUL)
428 {
429 /*
430 * Allocate a new eformat structure and put it at the end of the list
431 */
432 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
433 if (fmt_ptr == NULL)
434 goto parse_efm_error;
435 if (fmt_first == NULL) /* first one */
436 fmt_first = fmt_ptr;
437 else
438 fmt_last->next = fmt_ptr;
439 fmt_last = fmt_ptr;
440
441 /*
442 * Isolate one part in the 'errorformat' option
443 */
444 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
445 if (efm[len] == '\\' && efm[len + 1] != NUL)
446 ++len;
447
448 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
449 goto parse_efm_error;
450 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
451 goto parse_efm_error;
452 /*
453 * Advance to next part
454 */
455 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
456 }
457
458 if (fmt_first == NULL) /* nothing found */
459 EMSG(_("E378: 'errorformat' contains no pattern"));
460
461 goto parse_efm_end;
462
463parse_efm_error:
464 free_efm_list(&fmt_first);
465
466parse_efm_end:
467 vim_free(fmtstr);
468 vim_free(errmsg);
469
470 return fmt_first;
471}
472
Bram Moolenaare0d37972016-07-15 22:36:01 +0200473enum {
474 QF_FAIL = 0,
475 QF_OK = 1,
476 QF_END_OF_INPUT = 2,
477 QF_NOMEM = 3
478};
479
480typedef struct {
481 char_u *linebuf;
482 int linelen;
483 char_u *growbuf;
484 int growbufsiz;
485 FILE *fd;
486 typval_T *tv;
487 char_u *p_str;
488 listitem_T *p_li;
489 buf_T *buf;
490 linenr_T buflnum;
491 linenr_T lnumlast;
492} qfstate_T;
493
494 static char_u *
495qf_grow_linebuf(qfstate_T *state, int newsz)
496{
497 /*
498 * If the line exceeds LINE_MAXLEN exclude the last
499 * byte since it's not a NL character.
500 */
501 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
502 if (state->growbuf == NULL)
503 {
504 state->growbuf = alloc(state->linelen + 1);
505 if (state->growbuf == NULL)
506 return NULL;
507 state->growbufsiz = state->linelen;
508 }
509 else if (state->linelen > state->growbufsiz)
510 {
511 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
512 if (state->growbuf == NULL)
513 return NULL;
514 state->growbufsiz = state->linelen;
515 }
516 return state->growbuf;
517}
518
519/*
520 * Get the next string (separated by newline) from state->p_str.
521 */
522 static int
523qf_get_next_str_line(qfstate_T *state)
524{
525 /* Get the next line from the supplied string */
526 char_u *p_str = state->p_str;
527 char_u *p;
528 int len;
529
530 if (*p_str == NUL) /* Reached the end of the string */
531 return QF_END_OF_INPUT;
532
533 p = vim_strchr(p_str, '\n');
534 if (p != NULL)
535 len = (int)(p - p_str) + 1;
536 else
537 len = (int)STRLEN(p_str);
538
539 if (len > IOSIZE - 2)
540 {
541 state->linebuf = qf_grow_linebuf(state, len);
542 if (state->linebuf == NULL)
543 return QF_NOMEM;
544 }
545 else
546 {
547 state->linebuf = IObuff;
548 state->linelen = len;
549 }
550 vim_strncpy(state->linebuf, p_str, state->linelen);
551
552 /*
553 * Increment using len in order to discard the rest of the
554 * line if it exceeds LINE_MAXLEN.
555 */
556 p_str += len;
557 state->p_str = p_str;
558
559 return QF_OK;
560}
561
562/*
563 * Get the next string from state->p_Li.
564 */
565 static int
566qf_get_next_list_line(qfstate_T *state)
567{
568 listitem_T *p_li = state->p_li;
569 int len;
570
571 while (p_li != NULL
572 && (p_li->li_tv.v_type != VAR_STRING
573 || p_li->li_tv.vval.v_string == NULL))
574 p_li = p_li->li_next; /* Skip non-string items */
575
576 if (p_li == NULL) /* End of the list */
577 {
578 state->p_li = NULL;
579 return QF_END_OF_INPUT;
580 }
581
582 len = (int)STRLEN(p_li->li_tv.vval.v_string);
583 if (len > IOSIZE - 2)
584 {
585 state->linebuf = qf_grow_linebuf(state, len);
586 if (state->linebuf == NULL)
587 return QF_NOMEM;
588 }
589 else
590 {
591 state->linebuf = IObuff;
592 state->linelen = len;
593 }
594
595 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
596
597 state->p_li = p_li->li_next; /* next item */
598 return QF_OK;
599}
600
601/*
602 * Get the next string from state->buf.
603 */
604 static int
605qf_get_next_buf_line(qfstate_T *state)
606{
607 char_u *p_buf = NULL;
608 int len;
609
610 /* Get the next line from the supplied buffer */
611 if (state->buflnum > state->lnumlast)
612 return QF_END_OF_INPUT;
613
614 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
615 state->buflnum += 1;
616
617 len = (int)STRLEN(p_buf);
618 if (len > IOSIZE - 2)
619 {
620 state->linebuf = qf_grow_linebuf(state, len);
621 if (state->linebuf == NULL)
622 return QF_NOMEM;
623 }
624 else
625 {
626 state->linebuf = IObuff;
627 state->linelen = len;
628 }
629 vim_strncpy(state->linebuf, p_buf, state->linelen);
630
631 return QF_OK;
632}
633
634/*
635 * Get the next string from file state->fd.
636 */
637 static int
638qf_get_next_file_line(qfstate_T *state)
639{
640 int discard;
641 int growbuflen;
642
643 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
644 return QF_END_OF_INPUT;
645
646 discard = FALSE;
647 state->linelen = (int)STRLEN(IObuff);
648 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'
649#ifdef USE_CRNL
650 || IObuff[state->linelen - 1] == '\r'
651#endif
652 ))
653 {
654 /*
655 * The current line exceeds IObuff, continue reading using
656 * growbuf until EOL or LINE_MAXLEN bytes is read.
657 */
658 if (state->growbuf == NULL)
659 {
660 state->growbufsiz = 2 * (IOSIZE - 1);
661 state->growbuf = alloc(state->growbufsiz);
662 if (state->growbuf == NULL)
663 return QF_NOMEM;
664 }
665
666 /* Copy the read part of the line, excluding null-terminator */
667 memcpy(state->growbuf, IObuff, IOSIZE - 1);
668 growbuflen = state->linelen;
669
670 for (;;)
671 {
672 if (fgets((char *)state->growbuf + growbuflen,
673 state->growbufsiz - growbuflen, state->fd) == NULL)
674 break;
675 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
676 growbuflen += state->linelen;
677 if ((state->growbuf)[growbuflen - 1] == '\n'
678#ifdef USE_CRNL
679 || (state->growbuf)[growbuflen - 1] == '\r'
680#endif
681 )
682 break;
683 if (state->growbufsiz == LINE_MAXLEN)
684 {
685 discard = TRUE;
686 break;
687 }
688
689 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
690 ? 2 * state->growbufsiz : LINE_MAXLEN;
691 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
692 if (state->growbuf == NULL)
693 return QF_NOMEM;
694 }
695
696 while (discard)
697 {
698 /*
699 * The current line is longer than LINE_MAXLEN, continue
700 * reading but discard everything until EOL or EOF is
701 * reached.
702 */
703 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
704 || (int)STRLEN(IObuff) < IOSIZE - 1
705 || IObuff[IOSIZE - 1] == '\n'
706#ifdef USE_CRNL
707 || IObuff[IOSIZE - 1] == '\r'
708#endif
709 )
710 break;
711 }
712
713 state->linebuf = state->growbuf;
714 state->linelen = growbuflen;
715 }
716 else
717 state->linebuf = IObuff;
718
719 return QF_OK;
720}
721
722/*
723 * Get the next string from a file/buffer/list/string.
724 */
725 static int
726qf_get_nextline(qfstate_T *state)
727{
728 int status = QF_FAIL;
729
730 if (state->fd == NULL)
731 {
732 if (state->tv != NULL)
733 {
734 if (state->tv->v_type == VAR_STRING)
735 /* Get the next line from the supplied string */
736 status = qf_get_next_str_line(state);
737 else if (state->tv->v_type == VAR_LIST)
738 /* Get the next line from the supplied list */
739 status = qf_get_next_list_line(state);
740 }
741 else
742 /* Get the next line from the supplied buffer */
743 status = qf_get_next_buf_line(state);
744 }
745 else
746 /* Get the next line from the supplied file */
747 status = qf_get_next_file_line(state);
748
749 if (status != QF_OK)
750 return status;
751
752 /* remove newline/CR from the line */
753 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
754 state->linebuf[state->linelen - 1] = NUL;
755#ifdef USE_CRNL
756 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\r')
757 state->linebuf[state->linelen - 1] = NUL;
758#endif
759
760#ifdef FEAT_MBYTE
761 remove_bom(state->linebuf);
762#endif
763
764 return QF_OK;
765}
766
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200767/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000768 * Read the errorfile "efile" into memory, line by line, building the error
769 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +0200770 * Alternative: when "efile" is NULL read errors from buffer "buf".
771 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000772 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200773 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
774 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000775 * Return -1 for error, number of errors for success.
776 */
777 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100778qf_init_ext(
779 qf_info_T *qi,
780 char_u *efile,
781 buf_T *buf,
782 typval_T *tv,
783 char_u *errorformat,
784 int newlist, /* TRUE: start a new error list */
785 linenr_T lnumfirst, /* first line number to use */
786 linenr_T lnumlast, /* last line number to use */
787 char_u *qf_title)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000788{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 char_u *namebuf;
790 char_u *errmsg;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200791 int errmsglen;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000792 char_u *pattern;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200793 qfstate_T state = {NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +0200794 NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 int type = 0;
798 int valid;
799 long lnum = 0L;
800 int enr = 0;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200801#ifdef FEAT_WINDOWS
802 qfline_T *old_last = NULL;
803#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200804 static efm_T *fmt_first = NULL;
Bram Moolenaar01265852006-03-20 21:50:15 +0000805 efm_T *fmt_ptr;
806 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200808 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 int len;
811 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200814 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 char_u *tail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100818 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200819 errmsglen = CMDBUFFSIZE + 1;
820 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
Bram Moolenaarb86a3432016-01-10 16:00:53 +0100821 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000822 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 goto qf_init_end;
824
Bram Moolenaare0d37972016-07-15 22:36:01 +0200825 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
827 EMSG2(_(e_openerrf), efile);
828 goto qf_init_end;
829 }
830
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000831 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100833 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200834#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000835 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +0200836 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200837 /* Adding to existing list, use last entry. */
838 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +0200839 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842/*
843 * Each part of the format string is copied and modified from errorformat to
844 * regex prog. Only a few % characters are allowed.
845 */
846 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000847 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000848 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 else
850 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200852 /*
853 * If we are not adding or adding to another list: clear the state.
854 */
855 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
856 {
857 qi->qf_dir_curlist = qi->qf_curlist;
858 qf_clean_dir_stack(&qi->qf_dir_stack);
859 qi->qf_directory = NULL;
860 qf_clean_dir_stack(&qi->qf_file_stack);
861 qi->qf_currfile = NULL;
862 qi->qf_multiline = FALSE;
863 qi->qf_multiignore = FALSE;
864 qi->qf_multiscan = FALSE;
865 }
866
867 /*
868 * If the errorformat didn't change between calls, then reuse the
869 * previously parsed values.
870 */
871 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
872 {
873 /* free the previously parsed data */
874 vim_free(last_efm);
875 last_efm = NULL;
876 free_efm_list(&fmt_first);
877
878 /* parse the current 'efm' */
879 fmt_first = parse_efm_option(efm);
880 if (fmt_first != NULL)
881 last_efm = vim_strsave(efm);
882 }
883
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
887 /*
888 * got_int is reset here, because it was probably set when killing the
889 * ":make" command, but we still want to read the errorfile then.
890 */
891 got_int = FALSE;
892
893 /* Always ignore case when looking for a matching error. */
894 regmatch.rm_ic = TRUE;
895
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000896 if (tv != NULL)
897 {
898 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200899 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000900 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200901 state.p_li = tv->vval.v_list->lv_first;
902 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000903 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200904 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +0200905 state.buflnum = lnumfirst;
906 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000907
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 /*
909 * Read the lines in the error file one by one.
910 * Try to recognize one of the error formats in each line.
911 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000912 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200914 /* Get the next line from a file/buffer/list/string */
915 status = qf_get_nextline(&state);
916 if (status == QF_NOMEM) /* memory alloc failure */
917 goto qf_init_end;
918 if (status == QF_END_OF_INPUT) /* end of input */
919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
Bram Moolenaar01265852006-03-20 21:50:15 +0000921 /* If there was no %> item start at the first pattern */
922 if (fmt_start == NULL)
923 fmt_ptr = fmt_first;
924 else
925 {
926 fmt_ptr = fmt_start;
927 fmt_start = NULL;
928 }
929
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 /*
931 * Try to match each part of 'errorformat' until we find a complete
932 * match or no match.
933 */
934 valid = TRUE;
935restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000936 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100938 int r;
939
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 idx = fmt_ptr->prefix;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200941 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 continue;
943 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000944 pattern[0] = NUL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200945 if (!qi->qf_multiscan)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 errmsg[0] = NUL;
947 lnum = 0;
948 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000949 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 enr = -1;
951 type = 0;
952 tail = NULL;
953
954 regmatch.regprog = fmt_ptr->prog;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200955 r = vim_regexec(&regmatch, state.linebuf, (colnr_T)0);
Bram Moolenaar6f2dd9e2014-12-17 14:41:10 +0100956 fmt_ptr->prog = regmatch.regprog;
957 if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200959 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 continue;
961 if (vim_strchr((char_u *)"EWI", idx) != NULL)
962 type = idx;
963 else
964 type = 0;
965 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000966 * Extract error message data from matched line.
967 * We check for an actual submatch, because "\[" and "\]" in
968 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 */
970 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
971 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000972 int c;
973
974 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
975 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000976
977 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000978 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000979 *regmatch.endp[i] = NUL;
980 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
981 *regmatch.endp[i] = c;
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000984 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 continue;
986 }
987 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000988 {
989 if (regmatch.startp[i] == NULL)
990 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000994 {
995 if (regmatch.startp[i] == NULL)
996 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +00001000 {
1001 if (regmatch.startp[i] == NULL)
1002 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +00001004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +00001006 {
1007 if (regmatch.startp[i] == NULL)
1008 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +00001010 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001011 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001012 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001013 if (state.linelen > errmsglen) {
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001014 /* linelen + null terminator */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001015 if ((errmsg = vim_realloc(errmsg,
1016 state.linelen + 1)) == NULL)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001017 goto qf_init_end;
Bram Moolenaare0d37972016-07-15 22:36:01 +02001018 errmsglen = state.linelen + 1;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001019 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001020 vim_strncpy(errmsg, state.linebuf, state.linelen);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
1023 {
Bram Moolenaar4169da72006-06-20 18:49:32 +00001024 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1025 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001027 if (len > errmsglen) {
1028 /* len + null terminator */
1029 if ((errmsg = vim_realloc(errmsg, len + 1))
1030 == NULL)
1031 goto qf_init_end;
1032 errmsglen = len + 1;
1033 }
Bram Moolenaarbbebc852005-07-18 21:47:53 +00001034 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
1036 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +00001037 {
1038 if (regmatch.startp[i] == NULL)
1039 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +00001041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
1043 {
Bram Moolenaarf13de072012-06-01 18:34:41 +02001044 char_u *match_ptr;
1045
Bram Moolenaar4169da72006-06-20 18:49:32 +00001046 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1047 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +02001048 col = 0;
1049 for (match_ptr = regmatch.startp[i];
1050 match_ptr != regmatch.endp[i]; ++match_ptr)
1051 {
1052 ++col;
1053 if (*match_ptr == TAB)
1054 {
1055 col += 7;
1056 col -= col % 8;
1057 }
1058 }
1059 ++col;
1060 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 }
1062 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
1063 {
Bram Moolenaar4169da72006-06-20 18:49:32 +00001064 if (regmatch.startp[i] == NULL)
1065 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001067 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001069 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1070 {
Bram Moolenaar4169da72006-06-20 18:49:32 +00001071 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1072 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001073 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1074 if (len > CMDBUFFSIZE - 5)
1075 len = CMDBUFFSIZE - 5;
1076 STRCPY(pattern, "^\\V");
1077 STRNCAT(pattern, regmatch.startp[i], len);
1078 pattern[len + 3] = '\\';
1079 pattern[len + 4] = '$';
1080 pattern[len + 5] = NUL;
1081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 break;
1083 }
1084 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001085 qi->qf_multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +00001086
Bram Moolenaar4770d092006-01-12 23:22:24 +00001087 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001089 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 {
1091 if (idx == 'D') /* enter directory */
1092 {
1093 if (*namebuf == NUL)
1094 {
1095 EMSG(_("E379: Missing or empty directory name"));
1096 goto error2;
1097 }
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001098 qi->qf_directory =
1099 qf_push_dir(namebuf, &qi->qf_dir_stack, FALSE);
1100 if (qi->qf_directory == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 goto error2;
1102 }
1103 else if (idx == 'X') /* leave directory */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001104 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 }
1106 namebuf[0] = NUL; /* no match found, remove file name */
1107 lnum = 0; /* don't jump to this line */
1108 valid = FALSE;
Bram Moolenaare0d37972016-07-15 22:36:01 +02001109 if (state.linelen > errmsglen) {
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001110 /* linelen + null terminator */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001111 if ((errmsg = vim_realloc(errmsg, state.linelen + 1)) == NULL)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001112 goto qf_init_end;
Bram Moolenaare0d37972016-07-15 22:36:01 +02001113 errmsglen = state.linelen + 1;
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001114 }
1115 /* copy whole line to error message */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001116 vim_strncpy(errmsg, state.linebuf, state.linelen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001117 if (fmt_ptr == NULL)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001118 qi->qf_multiline = qi->qf_multiignore = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001120 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
Bram Moolenaar01265852006-03-20 21:50:15 +00001122 /* honor %> item */
1123 if (fmt_ptr->conthere)
1124 fmt_start = fmt_ptr;
1125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +01001127 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001128 qi->qf_multiline = TRUE; /* start of a multi-line message */
1129 qi->qf_multiignore = FALSE; /* reset continuation */
Bram Moolenaar8eded092014-03-12 19:41:55 +01001130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1132 { /* continuation of multi-line msg */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001133 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
1134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 if (qfprev == NULL)
1136 goto error2;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001137 if (*errmsg && !qi->qf_multiignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
1139 len = (int)STRLEN(qfprev->qf_text);
1140 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
1141 == NULL)
1142 goto error2;
1143 STRCPY(ptr, qfprev->qf_text);
1144 vim_free(qfprev->qf_text);
1145 qfprev->qf_text = ptr;
1146 *(ptr += len) = '\n';
1147 STRCPY(++ptr, errmsg);
1148 }
1149 if (qfprev->qf_nr == -1)
1150 qfprev->qf_nr = enr;
1151 if (vim_isprintc(type) && !qfprev->qf_type)
1152 qfprev->qf_type = type; /* only printable chars allowed */
1153 if (!qfprev->qf_lnum)
1154 qfprev->qf_lnum = lnum;
1155 if (!qfprev->qf_col)
1156 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001157 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (!qfprev->qf_fnum)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001159 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1160 *namebuf || qi->qf_directory != NULL
1161 ? namebuf
1162 : qi->qf_currfile != NULL && valid
1163 ? qi->qf_currfile : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (idx == 'Z')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001165 qi->qf_multiline = qi->qf_multiignore = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 line_breakcheck();
1167 continue;
1168 }
1169 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1170 {
1171 /* global file names */
1172 valid = FALSE;
1173 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
1174 {
1175 if (*namebuf && idx == 'P')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001176 qi->qf_currfile =
1177 qf_push_dir(namebuf, &qi->qf_file_stack, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 else if (idx == 'Q')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001179 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 *namebuf = NUL;
1181 if (tail && *tail)
1182 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00001183 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001184 qi->qf_multiscan = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 goto restofline;
1186 }
1187 }
1188 }
1189 if (fmt_ptr->flags == '-') /* generally exclude this line */
1190 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001191 if (qi->qf_multiline)
1192 /* also exclude continuation lines */
1193 qi->qf_multiignore = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 continue;
1195 }
1196 }
1197
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001198 if (qf_add_entry(qi,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001199 qi->qf_directory,
1200 (*namebuf || qi->qf_directory != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 ? namebuf
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001202 : ((qi->qf_currfile != NULL && valid)
1203 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001204 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 errmsg,
1206 lnum,
1207 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +00001208 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001209 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 enr,
1211 type,
1212 valid) == FAIL)
1213 goto error2;
1214 line_breakcheck();
1215 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001216 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001218 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001220 /* no valid entry found */
1221 qi->qf_lists[qi->qf_curlist].qf_ptr =
1222 qi->qf_lists[qi->qf_curlist].qf_start;
1223 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1224 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 }
1226 else
1227 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001228 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1229 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1230 qi->qf_lists[qi->qf_curlist].qf_ptr =
1231 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001233 /* return number of matches */
1234 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001235 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 }
1237 EMSG(_(e_readerrf));
1238error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001239 qf_free(qi, qi->qf_curlist);
1240 qi->qf_listcount--;
1241 if (qi->qf_curlist > 0)
1242 --qi->qf_curlist;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001243qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001244 if (state.fd != NULL)
1245 fclose(state.fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 vim_free(namebuf);
1247 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001248 vim_free(pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001249 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001252 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#endif
1254
1255 return retval;
1256}
1257
Bram Moolenaarfb604092014-07-23 15:55:00 +02001258 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001259qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001260{
1261 if (title != NULL)
1262 {
1263 char_u *p = alloc((int)STRLEN(title) + 2);
1264
1265 qi->qf_lists[qi->qf_curlist].qf_title = p;
1266 if (p != NULL)
1267 sprintf((char *)p, ":%s", (char *)title);
1268 }
1269}
1270
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271/*
1272 * Prepare for adding a new quickfix list.
1273 */
1274 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001275qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276{
1277 int i;
1278
1279 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001280 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 * the current entry. This makes it possible to browse in a tree-like
1282 * way with ":grep'.
1283 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001284 while (qi->qf_listcount > qi->qf_curlist + 1)
1285 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
1287 /*
1288 * When the stack is full, remove to oldest entry
1289 * Otherwise, add a new entry.
1290 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001291 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001293 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001295 qi->qf_lists[i - 1] = qi->qf_lists[i];
1296 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001299 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001300 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001301 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302}
1303
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001304/*
1305 * Free a location list
1306 */
1307 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001308ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001309{
1310 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001311 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001312
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001313 qi = *pqi;
1314 if (qi == NULL)
1315 return;
1316 *pqi = NULL; /* Remove reference to this list */
1317
1318 qi->qf_refcount--;
1319 if (qi->qf_refcount < 1)
1320 {
1321 /* No references to this location list */
1322 for (i = 0; i < qi->qf_listcount; ++i)
1323 qf_free(qi, i);
1324 vim_free(qi);
1325 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001326}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001327
1328 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001329qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001330{
1331 int i;
1332 qf_info_T *qi = &ql_info;
1333
1334 if (wp != NULL)
1335 {
1336 /* location list */
1337 ll_free_all(&wp->w_llist);
1338 ll_free_all(&wp->w_llist_ref);
1339 }
1340 else
1341 /* quickfix list */
1342 for (i = 0; i < qi->qf_listcount; ++i)
1343 qf_free(qi, i);
1344}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346/*
1347 * Add an entry to the end of the list of errors.
1348 * Returns OK or FAIL.
1349 */
1350 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001351qf_add_entry(
1352 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001353 char_u *dir, /* optional directory name */
1354 char_u *fname, /* file name or NULL */
1355 int bufnum, /* buffer number or zero */
1356 char_u *mesg, /* message */
1357 long lnum, /* line number */
1358 int col, /* column */
1359 int vis_col, /* using visual column */
1360 char_u *pattern, /* search pattern */
1361 int nr, /* error number */
1362 int type, /* type character */
1363 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001365 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001366 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001368 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001370 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001371 {
1372 buf_T *buf = buflist_findnr(bufnum);
1373
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001374 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001375 if (buf != NULL)
1376 buf->b_has_qf_entry = TRUE;
1377 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001378 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001379 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1381 {
1382 vim_free(qfp);
1383 return FAIL;
1384 }
1385 qfp->qf_lnum = lnum;
1386 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001387 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001388 if (pattern == NULL || *pattern == NUL)
1389 qfp->qf_pattern = NULL;
1390 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1391 {
1392 vim_free(qfp->qf_text);
1393 vim_free(qfp);
1394 return FAIL;
1395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 qfp->qf_nr = nr;
1397 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1398 type = 0;
1399 qfp->qf_type = type;
1400 qfp->qf_valid = valid;
1401
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001402 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001403 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1404 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001406 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001407 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1408 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001409 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411 else
1412 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001413 qfp->qf_prev = *lastp;
1414 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001416 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001418 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001419 ++qi->qf_lists[qi->qf_curlist].qf_count;
1420 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1421 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001423 qi->qf_lists[qi->qf_curlist].qf_index =
1424 qi->qf_lists[qi->qf_curlist].qf_count;
1425 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427
1428 return OK;
1429}
1430
1431/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001432 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001433 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001434 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001435ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001436{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001437 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001438
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001439 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1440 if (qi != NULL)
1441 {
1442 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1443 qi->qf_refcount++;
1444 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001445
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001446 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001447}
1448
1449/*
1450 * Return the location list for window 'wp'.
1451 * If not present, allocate a location list
1452 */
1453 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001454ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001455{
1456 if (IS_LL_WINDOW(wp))
1457 /* For a location list window, use the referenced location list */
1458 return wp->w_llist_ref;
1459
1460 /*
1461 * For a non-location list window, w_llist_ref should not point to a
1462 * location list.
1463 */
1464 ll_free_all(&wp->w_llist_ref);
1465
1466 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001467 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001468 return wp->w_llist;
1469}
1470
1471/*
1472 * Copy the location list from window "from" to window "to".
1473 */
1474 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001475copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001476{
1477 qf_info_T *qi;
1478 int idx;
1479 int i;
1480
1481 /*
1482 * When copying from a location list window, copy the referenced
1483 * location list. For other windows, copy the location list for
1484 * that window.
1485 */
1486 if (IS_LL_WINDOW(from))
1487 qi = from->w_llist_ref;
1488 else
1489 qi = from->w_llist;
1490
1491 if (qi == NULL) /* no location list to copy */
1492 return;
1493
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001494 /* allocate a new location list */
1495 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001496 return;
1497
1498 to->w_llist->qf_listcount = qi->qf_listcount;
1499
1500 /* Copy the location lists one at a time */
1501 for (idx = 0; idx < qi->qf_listcount; idx++)
1502 {
1503 qf_list_T *from_qfl;
1504 qf_list_T *to_qfl;
1505
1506 to->w_llist->qf_curlist = idx;
1507
1508 from_qfl = &qi->qf_lists[idx];
1509 to_qfl = &to->w_llist->qf_lists[idx];
1510
1511 /* Some of the fields are populated by qf_add_entry() */
1512 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1513 to_qfl->qf_count = 0;
1514 to_qfl->qf_index = 0;
1515 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001516 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001517 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001518 if (from_qfl->qf_title != NULL)
1519 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1520 else
1521 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522
1523 if (from_qfl->qf_count)
1524 {
1525 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001526 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001527
1528 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001529 for (i = 0, from_qfp = from_qfl->qf_start;
1530 i < from_qfl->qf_count && from_qfp != NULL;
1531 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001532 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001533 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534 NULL,
1535 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001536 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001537 from_qfp->qf_text,
1538 from_qfp->qf_lnum,
1539 from_qfp->qf_col,
1540 from_qfp->qf_viscol,
1541 from_qfp->qf_pattern,
1542 from_qfp->qf_nr,
1543 0,
1544 from_qfp->qf_valid) == FAIL)
1545 {
1546 qf_free_all(to);
1547 return;
1548 }
1549 /*
1550 * qf_add_entry() will not set the qf_num field, as the
1551 * directory and file names are not supplied. So the qf_fnum
1552 * field is copied here.
1553 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001554 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001555 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1556 prevp->qf_type = from_qfp->qf_type; /* error type */
1557 if (from_qfl->qf_ptr == from_qfp)
1558 to_qfl->qf_ptr = prevp; /* current location */
1559 }
1560 }
1561
1562 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1563
1564 /* When no valid entries are present in the list, qf_ptr points to
1565 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001566 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001567 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001568 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001569 to_qfl->qf_index = 1;
1570 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001571 }
1572
1573 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1574}
1575
1576/*
Bram Moolenaar82404332016-07-10 17:00:38 +02001577 * Looking up a buffer can be slow if there are many. Remember the last one
1578 * to make this a lot faster if there are multiple matches in the same file.
1579 */
1580static char_u *qf_last_bufname = NULL;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001581static bufref_T qf_last_bufref = {NULL, 0};
Bram Moolenaar82404332016-07-10 17:00:38 +02001582
1583/*
Bram Moolenaar015102e2016-07-16 18:24:56 +02001584 * Get buffer number for file "directory.fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001585 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 */
1587 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001588qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
Bram Moolenaar82404332016-07-10 17:00:38 +02001590 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001591 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001592 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (fname == NULL || *fname == NUL) /* no file name */
1595 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaare60acc12011-05-10 16:41:25 +02001597#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001598 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001599#endif
1600#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001601 if (directory != NULL)
1602 slash_adjust(directory);
1603 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001604#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001605 if (directory != NULL && !vim_isAbsName(fname)
1606 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1607 {
1608 /*
1609 * Here we check if the file really exists.
1610 * This should normally be true, but if make works without
1611 * "leaving directory"-messages we might have missed a
1612 * directory change.
1613 */
1614 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001617 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001618 if (directory)
1619 ptr = concat_fnames(directory, fname, TRUE);
1620 else
1621 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001623 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001624 bufname = ptr;
1625 }
1626 else
1627 bufname = fname;
1628
1629 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001630 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001631 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001632 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001633 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001635 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001636 {
1637 vim_free(qf_last_bufname);
1638 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1639 if (bufname == ptr)
1640 qf_last_bufname = bufname;
1641 else
1642 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001643 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001644 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001645 if (buf == NULL)
1646 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001647
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001648 buf->b_has_qf_entry = TRUE;
1649 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650}
1651
1652/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001653 * Push dirbuf onto the directory stack and return pointer to actual dir or
1654 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 */
1656 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001657qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658{
1659 struct dir_stack_T *ds_new;
1660 struct dir_stack_T *ds_ptr;
1661
1662 /* allocate new stack element and hook it in */
1663 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1664 if (ds_new == NULL)
1665 return NULL;
1666
1667 ds_new->next = *stackptr;
1668 *stackptr = ds_new;
1669
1670 /* store directory on the stack */
1671 if (vim_isAbsName(dirbuf)
1672 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001673 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 (*stackptr)->dirname = vim_strsave(dirbuf);
1675 else
1676 {
1677 /* Okay we don't have an absolute path.
1678 * dirbuf must be a subdir of one of the directories on the stack.
1679 * Let's search...
1680 */
1681 ds_new = (*stackptr)->next;
1682 (*stackptr)->dirname = NULL;
1683 while (ds_new)
1684 {
1685 vim_free((*stackptr)->dirname);
1686 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1687 TRUE);
1688 if (mch_isdir((*stackptr)->dirname) == TRUE)
1689 break;
1690
1691 ds_new = ds_new->next;
1692 }
1693
1694 /* clean up all dirs we already left */
1695 while ((*stackptr)->next != ds_new)
1696 {
1697 ds_ptr = (*stackptr)->next;
1698 (*stackptr)->next = (*stackptr)->next->next;
1699 vim_free(ds_ptr->dirname);
1700 vim_free(ds_ptr);
1701 }
1702
1703 /* Nothing found -> it must be on top level */
1704 if (ds_new == NULL)
1705 {
1706 vim_free((*stackptr)->dirname);
1707 (*stackptr)->dirname = vim_strsave(dirbuf);
1708 }
1709 }
1710
1711 if ((*stackptr)->dirname != NULL)
1712 return (*stackptr)->dirname;
1713 else
1714 {
1715 ds_ptr = *stackptr;
1716 *stackptr = (*stackptr)->next;
1717 vim_free(ds_ptr);
1718 return NULL;
1719 }
1720}
1721
1722
1723/*
1724 * pop dirbuf from the directory stack and return previous directory or NULL if
1725 * stack is empty
1726 */
1727 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001728qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
1730 struct dir_stack_T *ds_ptr;
1731
1732 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1733 * What to do if it isn't? */
1734
1735 /* pop top element and free it */
1736 if (*stackptr != NULL)
1737 {
1738 ds_ptr = *stackptr;
1739 *stackptr = (*stackptr)->next;
1740 vim_free(ds_ptr->dirname);
1741 vim_free(ds_ptr);
1742 }
1743
1744 /* return NEW top element as current dir or NULL if stack is empty*/
1745 return *stackptr ? (*stackptr)->dirname : NULL;
1746}
1747
1748/*
1749 * clean up directory stack
1750 */
1751 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001752qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 struct dir_stack_T *ds_ptr;
1755
1756 while ((ds_ptr = *stackptr) != NULL)
1757 {
1758 *stackptr = (*stackptr)->next;
1759 vim_free(ds_ptr->dirname);
1760 vim_free(ds_ptr);
1761 }
1762}
1763
1764/*
1765 * Check in which directory of the directory stack the given file can be
1766 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001767 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 * Cleans up intermediate directory entries.
1769 *
1770 * TODO: How to solve the following problem?
1771 * If we have the this directory tree:
1772 * ./
1773 * ./aa
1774 * ./aa/bb
1775 * ./bb
1776 * ./bb/x.c
1777 * and make says:
1778 * making all in aa
1779 * making all in bb
1780 * x.c:9: Error
1781 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1782 * qf_guess_filepath will return NULL.
1783 */
1784 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001785qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 struct dir_stack_T *ds_ptr;
1788 struct dir_stack_T *ds_tmp;
1789 char_u *fullname;
1790
1791 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001792 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 return NULL;
1794
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001795 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 fullname = NULL;
1797 while (ds_ptr)
1798 {
1799 vim_free(fullname);
1800 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1801
1802 /* If concat_fnames failed, just go on. The worst thing that can happen
1803 * is that we delete the entire stack.
1804 */
1805 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1806 break;
1807
1808 ds_ptr = ds_ptr->next;
1809 }
1810
1811 vim_free(fullname);
1812
1813 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001814 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001816 ds_tmp = qi->qf_dir_stack->next;
1817 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 vim_free(ds_tmp->dirname);
1819 vim_free(ds_tmp);
1820 }
1821
1822 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823}
1824
1825/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001826 * When loading a file from the quickfix, the auto commands may modify it.
1827 * This may invalidate the current quickfix entry. This function checks
1828 * whether a entry is still present in the quickfix.
1829 * Similar to location list.
1830 */
1831 static int
1832is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1833{
1834 qf_list_T *qfl;
1835 qfline_T *qfp;
1836 int i;
1837
1838 qfl = &qi->qf_lists[qi->qf_curlist];
1839
1840 /* Search for the entry in the current list */
1841 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1842 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001843 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001844 break;
1845
1846 if (i == qfl->qf_count) /* Entry is not found */
1847 return FALSE;
1848
1849 return TRUE;
1850}
1851
1852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 * jump to a quickfix line
1854 * if dir == FORWARD go "errornr" valid entries forward
1855 * if dir == BACKWARD go "errornr" valid entries backward
1856 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1857 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1858 * else if "errornr" is zero, redisplay the same line
1859 * else go to entry "errornr"
1860 */
1861 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001862qf_jump(
1863 qf_info_T *qi,
1864 int dir,
1865 int errornr,
1866 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001868 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001869 qfline_T *qf_ptr;
1870 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 int qf_index;
1872 int old_qf_fnum;
1873 int old_qf_index;
1874 int prev_index;
1875 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1876 char_u *err = e_no_more_items;
1877 linenr_T i;
1878 buf_T *old_curbuf;
1879 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 colnr_T screen_col;
1881 colnr_T char_col;
1882 char_u *line;
1883#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001884 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001885 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 int opened_window = FALSE;
1887 win_T *win;
1888 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001889 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001891 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 int print_message = TRUE;
1893 int len;
1894#ifdef FEAT_FOLDING
1895 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1896#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001897 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001900 if (qi == NULL)
1901 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001902
1903 if (qi->qf_curlist >= qi->qf_listcount
1904 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 EMSG(_(e_quickfix));
1907 return;
1908 }
1909
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001910 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001912 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 old_qf_index = qf_index;
1914 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1915 {
1916 while (errornr--)
1917 {
1918 old_qf_ptr = qf_ptr;
1919 prev_index = qf_index;
1920 old_qf_fnum = qf_ptr->qf_fnum;
1921 do
1922 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001923 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 || qf_ptr->qf_next == NULL)
1925 {
1926 qf_ptr = old_qf_ptr;
1927 qf_index = prev_index;
1928 if (err != NULL)
1929 {
1930 EMSG(_(err));
1931 goto theend;
1932 }
1933 errornr = 0;
1934 break;
1935 }
1936 ++qf_index;
1937 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001938 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1939 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1941 err = NULL;
1942 }
1943 }
1944 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1945 {
1946 while (errornr--)
1947 {
1948 old_qf_ptr = qf_ptr;
1949 prev_index = qf_index;
1950 old_qf_fnum = qf_ptr->qf_fnum;
1951 do
1952 {
1953 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1954 {
1955 qf_ptr = old_qf_ptr;
1956 qf_index = prev_index;
1957 if (err != NULL)
1958 {
1959 EMSG(_(err));
1960 goto theend;
1961 }
1962 errornr = 0;
1963 break;
1964 }
1965 --qf_index;
1966 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001967 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1968 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1970 err = NULL;
1971 }
1972 }
1973 else if (errornr != 0) /* go to specified number */
1974 {
1975 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1976 {
1977 --qf_index;
1978 qf_ptr = qf_ptr->qf_prev;
1979 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 while (errornr > qf_index && qf_index <
1981 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 && qf_ptr->qf_next != NULL)
1983 {
1984 ++qf_index;
1985 qf_ptr = qf_ptr->qf_next;
1986 }
1987 }
1988
1989#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001990 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1991 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 /* No need to print the error message if it's visible in the error
1993 * window */
1994 print_message = FALSE;
1995
1996 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001997 * For ":helpgrep" find a help window or open one.
1998 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001999 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002000 {
2001 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002002
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002003 if (cmdmod.tab != 0)
2004 wp = NULL;
2005 else
2006 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2007 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2008 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002009 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2010 win_enter(wp, TRUE);
2011 else
2012 {
2013 /*
2014 * Split off help window; put it at far top if no position
2015 * specified, the current window is vertically split and narrow.
2016 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002017 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002018 if (cmdmod.split == 0 && curwin->w_width != Columns
2019 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002020 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002021 if (qi != &ql_info)
2022 flags |= WSP_NEWLOC; /* don't copy the location list */
2023
2024 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002025 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002026 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002027
2028 if (curwin->w_height < p_hh)
2029 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002030
2031 if (qi != &ql_info) /* not a quickfix list */
2032 {
2033 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002034 curwin->w_llist = qi;
2035 qi->qf_refcount++;
2036 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002037 }
2038
2039 if (!p_im)
2040 restart_edit = 0; /* don't want insert mode in help file */
2041 }
2042
2043 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * If currently in the quickfix window, find another window to show the
2045 * file in.
2046 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002047 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002049 win_T *usable_win_ptr = NULL;
2050
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 /*
2052 * If there is no file specified, we don't know where to go.
2053 * But do advance, otherwise ":cn" gets stuck.
2054 */
2055 if (qf_ptr->qf_fnum == 0)
2056 goto theend;
2057
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002058 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002059
2060 ll_ref = curwin->w_llist_ref;
2061 if (ll_ref != NULL)
2062 {
2063 /* Find a window using the same location list that is not a
2064 * quickfix window. */
2065 FOR_ALL_WINDOWS(usable_win_ptr)
2066 if (usable_win_ptr->w_llist == ll_ref
2067 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002068 {
2069 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002070 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002071 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002072 }
2073
2074 if (!usable_win)
2075 {
2076 /* Locate a window showing a normal buffer */
2077 FOR_ALL_WINDOWS(win)
2078 if (win->w_buffer->b_p_bt[0] == NUL)
2079 {
2080 usable_win = 1;
2081 break;
2082 }
2083 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002084
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002086 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002087 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002089 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002090 {
2091 tabpage_T *tp;
2092 win_T *wp;
2093
2094 FOR_ALL_TAB_WINDOWS(tp, wp)
2095 {
2096 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2097 {
2098 goto_tabpage_win(tp, wp);
2099 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002100 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002101 }
2102 }
2103 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002104win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002105
2106 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002107 * If there is only one window and it is the quickfix window, create a
2108 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002109 */
2110 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002112 flags = WSP_ABOVE;
2113 if (ll_ref != NULL)
2114 flags |= WSP_NEWLOC;
2115 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 goto failed; /* not enough room for window */
2117 opened_window = TRUE; /* close it when fail */
2118 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002119 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002120 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002121 if (ll_ref != NULL)
2122 {
2123 /* The new window should use the location list from the
2124 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002125 curwin->w_llist = ll_ref;
2126 ll_ref->qf_refcount++;
2127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 }
2129 else
2130 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002131 if (curwin->w_llist_ref != NULL)
2132 {
2133 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002134 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002135 if (win == NULL)
2136 {
2137 /* Find the window showing the selected file */
2138 FOR_ALL_WINDOWS(win)
2139 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2140 break;
2141 if (win == NULL)
2142 {
2143 /* Find a previous usable window */
2144 win = curwin;
2145 do
2146 {
2147 if (win->w_buffer->b_p_bt[0] == NUL)
2148 break;
2149 if (win->w_prev == NULL)
2150 win = lastwin; /* wrap around the top */
2151 else
2152 win = win->w_prev; /* go to previous window */
2153 } while (win != curwin);
2154 }
2155 }
2156 win_goto(win);
2157
2158 /* If the location list for the window is not set, then set it
2159 * to the location list from the location window */
2160 if (win->w_llist == NULL)
2161 {
2162 win->w_llist = ll_ref;
2163 ll_ref->qf_refcount++;
2164 }
2165 }
2166 else
2167 {
2168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 /*
2170 * Try to find a window that shows the right buffer.
2171 * Default to the window just above the quickfix buffer.
2172 */
2173 win = curwin;
2174 altwin = NULL;
2175 for (;;)
2176 {
2177 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2178 break;
2179 if (win->w_prev == NULL)
2180 win = lastwin; /* wrap around the top */
2181 else
2182 win = win->w_prev; /* go to previous window */
2183
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002184 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
2186 /* Didn't find it, go to the window before the quickfix
2187 * window. */
2188 if (altwin != NULL)
2189 win = altwin;
2190 else if (curwin->w_prev != NULL)
2191 win = curwin->w_prev;
2192 else
2193 win = curwin->w_next;
2194 break;
2195 }
2196
2197 /* Remember a usable window. */
2198 if (altwin == NULL && !win->w_p_pvw
2199 && win->w_buffer->b_p_bt[0] == NUL)
2200 altwin = win;
2201 }
2202
2203 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
2206 }
2207#endif
2208
2209 /*
2210 * If there is a file name,
2211 * read the wanted file if needed, and check autowrite etc.
2212 */
2213 old_curbuf = curbuf;
2214 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002215
2216 if (qf_ptr->qf_fnum != 0)
2217 {
2218 if (qf_ptr->qf_type == 1)
2219 {
2220 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2221 * set b_p_ro flag). */
2222 if (!can_abandon(curbuf, forceit))
2223 {
2224 EMSG(_(e_nowrtmsg));
2225 ok = FALSE;
2226 }
2227 else
2228 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002229 ECMD_HIDE + ECMD_SET_HELP,
2230 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002231 }
2232 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002233 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002234 int old_qf_curlist = qi->qf_curlist;
2235 int is_abort = FALSE;
2236
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002237 ok = buflist_getfile(qf_ptr->qf_fnum,
2238 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0899d692016-03-19 13:35:03 +01002239 if (qi != &ql_info && !win_valid(oldwin))
2240 {
2241 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002242 is_abort = TRUE;
2243 opened_window = FALSE;
2244 }
2245 else if (old_qf_curlist != qi->qf_curlist
2246 || !is_qf_entry_present(qi, qf_ptr))
2247 {
2248 if (qi == &ql_info)
2249 EMSG(_("E925: Current quickfix was changed"));
2250 else
2251 EMSG(_("E926: Current location list was changed"));
2252 is_abort = TRUE;
2253 }
2254
2255 if (is_abort)
2256 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002257 ok = FALSE;
2258 qi = NULL;
2259 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002260 }
2261 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002262 }
2263
2264 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
2266 /* When not switched to another buffer, still need to set pc mark */
2267 if (curbuf == old_curbuf)
2268 setpcmark();
2269
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002270 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002272 /*
2273 * Go to line with error, unless qf_lnum is 0.
2274 */
2275 i = qf_ptr->qf_lnum;
2276 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002278 if (i > curbuf->b_ml.ml_line_count)
2279 i = curbuf->b_ml.ml_line_count;
2280 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002282 if (qf_ptr->qf_col > 0)
2283 {
2284 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002285#ifdef FEAT_VIRTUALEDIT
2286 curwin->w_cursor.coladd = 0;
2287#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002288 if (qf_ptr->qf_viscol == TRUE)
2289 {
2290 /*
2291 * Check each character from the beginning of the error
2292 * line up to the error column. For each tab character
2293 * found, reduce the error column value by the length of
2294 * a tab character.
2295 */
2296 line = ml_get_curline();
2297 screen_col = 0;
2298 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2299 {
2300 if (*line == NUL)
2301 break;
2302 if (*line++ == '\t')
2303 {
2304 curwin->w_cursor.col -= 7 - (screen_col % 8);
2305 screen_col += 8 - (screen_col % 8);
2306 }
2307 else
2308 ++screen_col;
2309 }
2310 }
2311 check_cursor();
2312 }
2313 else
2314 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002317 {
2318 pos_T save_cursor;
2319
2320 /* Move the cursor to the first line in the buffer */
2321 save_cursor = curwin->w_cursor;
2322 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002323 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2324 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002325 curwin->w_cursor = save_cursor;
2326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328#ifdef FEAT_FOLDING
2329 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2330 foldOpenCursor();
2331#endif
2332 if (print_message)
2333 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002334 /* Update the screen before showing the message, unless the screen
2335 * scrolled up. */
2336 if (!msg_scrolled)
2337 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002339 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2341 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2342 /* Add the message, skipping leading whitespace and newlines. */
2343 len = (int)STRLEN(IObuff);
2344 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2345
2346 /* Output the message. Overwrite to avoid scrolling when the 'O'
2347 * flag is present in 'shortmess'; But when not jumping, print the
2348 * whole message. */
2349 i = msg_scroll;
2350 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2351 msg_scroll = TRUE;
2352 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2353 msg_scroll = FALSE;
2354 msg_attr_keep(IObuff, 0, TRUE);
2355 msg_scroll = i;
2356 }
2357 }
2358 else
2359 {
2360#ifdef FEAT_WINDOWS
2361 if (opened_window)
2362 win_close(curwin, TRUE); /* Close opened window */
2363#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002364 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 /*
2367 * Couldn't open file, so put index back where it was. This could
2368 * happen if the file was readonly and we changed something.
2369 */
2370#ifdef FEAT_WINDOWS
2371failed:
2372#endif
2373 qf_ptr = old_qf_ptr;
2374 qf_index = old_qf_index;
2375 }
2376 }
2377theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002378 if (qi != NULL)
2379 {
2380 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2381 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383#ifdef FEAT_WINDOWS
2384 if (p_swb != old_swb && opened_window)
2385 {
2386 /* Restore old 'switchbuf' value, but not when an autocommand or
2387 * modeline has changed the value. */
2388 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002391 swb_flags = old_swb_flags;
2392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 else
2394 free_string_option(old_swb);
2395 }
2396#endif
2397}
2398
2399/*
2400 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002401 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 */
2403 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002404qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002406 buf_T *buf;
2407 char_u *fname;
2408 qfline_T *qfp;
2409 int i;
2410 int idx1 = 1;
2411 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002412 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002413 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002414 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002416 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002418 if (eap->cmdidx == CMD_llist)
2419 {
2420 qi = GET_LOC_LIST(curwin);
2421 if (qi == NULL)
2422 {
2423 EMSG(_(e_loclist));
2424 return;
2425 }
2426 }
2427
2428 if (qi->qf_curlist >= qi->qf_listcount
2429 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
2431 EMSG(_(e_quickfix));
2432 return;
2433 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002434 if (*arg == '+')
2435 {
2436 ++arg;
2437 plus = TRUE;
2438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2440 {
2441 EMSG(_(e_trailing));
2442 return;
2443 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002444 if (plus)
2445 {
2446 i = qi->qf_lists[qi->qf_curlist].qf_index;
2447 idx2 = i + idx1;
2448 idx1 = i;
2449 }
2450 else
2451 {
2452 i = qi->qf_lists[qi->qf_curlist].qf_count;
2453 if (idx1 < 0)
2454 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2455 if (idx2 < 0)
2456 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002459 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002461 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2462 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
2464 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2465 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002466 msg_putchar('\n');
2467 if (got_int)
2468 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002469
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002470 fname = NULL;
2471 if (qfp->qf_fnum != 0
2472 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2473 {
2474 fname = buf->b_fname;
2475 if (qfp->qf_type == 1) /* :helpgrep */
2476 fname = gettail(fname);
2477 }
2478 if (fname == NULL)
2479 sprintf((char *)IObuff, "%2d", i);
2480 else
2481 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2482 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002483 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002484 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2485 if (qfp->qf_lnum == 0)
2486 IObuff[0] = NUL;
2487 else if (qfp->qf_col == 0)
2488 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2489 else
2490 sprintf((char *)IObuff, ":%ld col %d",
2491 qfp->qf_lnum, qfp->qf_col);
2492 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2493 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2494 msg_puts_attr(IObuff, hl_attr(HLF_N));
2495 if (qfp->qf_pattern != NULL)
2496 {
2497 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2498 STRCAT(IObuff, ":");
2499 msg_puts(IObuff);
2500 }
2501 msg_puts((char_u *)" ");
2502
2503 /* Remove newlines and leading whitespace from the text. For an
2504 * unrecognized line keep the indent, the compiler may mark a word
2505 * with ^^^^. */
2506 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2508 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002509 msg_prt_line(IObuff, FALSE);
2510 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002512
2513 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002514 if (qfp == NULL)
2515 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002516 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 ui_breakcheck();
2518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519}
2520
2521/*
2522 * Remove newlines and leading whitespace from an error message.
2523 * Put the result in "buf[bufsize]".
2524 */
2525 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002526qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528 int i;
2529 char_u *p = text;
2530
2531 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2532 {
2533 if (*p == '\n')
2534 {
2535 buf[i] = ' ';
2536 while (*++p != NUL)
2537 if (!vim_iswhite(*p) && *p != '\n')
2538 break;
2539 }
2540 else
2541 buf[i] = *p++;
2542 }
2543 buf[i] = NUL;
2544}
2545
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002546 static void
2547qf_msg(qf_info_T *qi, int which, char *lead)
2548{
2549 char *title = (char *)qi->qf_lists[which].qf_title;
2550 int count = qi->qf_lists[which].qf_count;
2551 char_u buf[IOSIZE];
2552
2553 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2554 lead,
2555 which + 1,
2556 qi->qf_listcount,
2557 count);
2558
2559 if (title != NULL)
2560 {
2561 while (STRLEN(buf) < 34)
2562 STRCAT(buf, " ");
2563 STRCAT(buf, title);
2564 }
2565 trunc_string(buf, buf, Columns - 1, IOSIZE);
2566 msg(buf);
2567}
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569/*
2570 * ":colder [count]": Up in the quickfix stack.
2571 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002572 * ":lolder [count]": Up in the location list stack.
2573 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 */
2575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002576qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002578 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 int count;
2580
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002581 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2582 {
2583 qi = GET_LOC_LIST(curwin);
2584 if (qi == NULL)
2585 {
2586 EMSG(_(e_loclist));
2587 return;
2588 }
2589 }
2590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (eap->addr_count != 0)
2592 count = eap->line2;
2593 else
2594 count = 1;
2595 while (count--)
2596 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002597 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002599 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
2601 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002602 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002604 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606 else
2607 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002608 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
2610 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002611 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002613 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
2615 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002616 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002618 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619#endif
2620}
2621
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002622 void
2623qf_history(exarg_T *eap)
2624{
2625 qf_info_T *qi = &ql_info;
2626 int i;
2627
2628 if (eap->cmdidx == CMD_lhistory)
2629 qi = GET_LOC_LIST(curwin);
2630 if (qi == NULL || (qi->qf_listcount == 0
2631 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2632 MSG(_("No entries"));
2633 else
2634 for (i = 0; i < qi->qf_listcount; ++i)
2635 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2636}
2637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002639 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 */
2641 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002642qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002644 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002645 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002646 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002648 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002650 qfp = qi->qf_lists[idx].qf_start;
2651 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002652 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002653 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002654 vim_free(qfp->qf_text);
2655 stop = (qfp == qfpnext);
2656 vim_free(qfp->qf_pattern);
2657 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002658 if (stop)
2659 /* Somehow qf_count may have an incorrect value, set it to 1
2660 * to avoid crashing when it's wrong.
2661 * TODO: Avoid qf_count being incorrect. */
2662 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002663 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002664 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002665 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002667 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002668 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002669 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002670
2671 qf_clean_dir_stack(&qi->qf_dir_stack);
2672 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673}
2674
2675/*
2676 * qf_mark_adjust: adjust marks
2677 */
2678 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002679qf_mark_adjust(
2680 win_T *wp,
2681 linenr_T line1,
2682 linenr_T line2,
2683 long amount,
2684 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002686 int i;
2687 qfline_T *qfp;
2688 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002689 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002690 int found_one = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002692 if (!curbuf->b_has_qf_entry)
2693 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002694 if (wp != NULL)
2695 {
2696 if (wp->w_llist == NULL)
2697 return;
2698 qi = wp->w_llist;
2699 }
2700
2701 for (idx = 0; idx < qi->qf_listcount; ++idx)
2702 if (qi->qf_lists[idx].qf_count)
2703 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002704 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2705 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (qfp->qf_fnum == curbuf->b_fnum)
2707 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002708 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2710 {
2711 if (amount == MAXLNUM)
2712 qfp->qf_cleared = TRUE;
2713 else
2714 qfp->qf_lnum += amount;
2715 }
2716 else if (amount_after && qfp->qf_lnum > line2)
2717 qfp->qf_lnum += amount_after;
2718 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002719
2720 if (!found_one)
2721 curbuf->b_has_qf_entry = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722}
2723
2724/*
2725 * Make a nice message out of the error character and the error number:
2726 * char number message
2727 * e or E 0 " error"
2728 * w or W 0 " warning"
2729 * i or I 0 " info"
2730 * 0 0 ""
2731 * other 0 " c"
2732 * e or E n " error n"
2733 * w or W n " warning n"
2734 * i or I n " info n"
2735 * 0 n " error n"
2736 * other n " c n"
2737 * 1 x "" :helpgrep
2738 */
2739 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002740qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
2742 static char_u buf[20];
2743 static char_u cc[3];
2744 char_u *p;
2745
2746 if (c == 'W' || c == 'w')
2747 p = (char_u *)" warning";
2748 else if (c == 'I' || c == 'i')
2749 p = (char_u *)" info";
2750 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2751 p = (char_u *)" error";
2752 else if (c == 0 || c == 1)
2753 p = (char_u *)"";
2754 else
2755 {
2756 cc[0] = ' ';
2757 cc[1] = c;
2758 cc[2] = NUL;
2759 p = cc;
2760 }
2761
2762 if (nr <= 0)
2763 return p;
2764
2765 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2766 return buf;
2767}
2768
2769#if defined(FEAT_WINDOWS) || defined(PROTO)
2770/*
2771 * ":cwindow": open the quickfix window if we have errors to display,
2772 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002773 * ":lwindow": open the location list window if we have locations to display,
2774 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 */
2776 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002777ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002779 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 win_T *win;
2781
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002782 if (eap->cmdidx == CMD_lwindow)
2783 {
2784 qi = GET_LOC_LIST(curwin);
2785 if (qi == NULL)
2786 return;
2787 }
2788
2789 /* Look for an existing quickfix window. */
2790 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791
2792 /*
2793 * If a quickfix window is open but we have no errors to display,
2794 * close the window. If a quickfix window is not open, then open
2795 * it if we have errors; otherwise, leave it closed.
2796 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002797 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002798 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002799 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
2801 if (win != NULL)
2802 ex_cclose(eap);
2803 }
2804 else if (win == NULL)
2805 ex_copen(eap);
2806}
2807
2808/*
2809 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002810 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002813ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002815 win_T *win = NULL;
2816 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002818 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2819 {
2820 qi = GET_LOC_LIST(curwin);
2821 if (qi == NULL)
2822 return;
2823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002825 /* Find existing quickfix window and close it. */
2826 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 if (win != NULL)
2828 win_close(win, FALSE);
2829}
2830
2831/*
2832 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002833 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 */
2835 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002836ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002838 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002841 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002842 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002843 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002845 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2846 {
2847 qi = GET_LOC_LIST(curwin);
2848 if (qi == NULL)
2849 {
2850 EMSG(_(e_loclist));
2851 return;
2852 }
2853 }
2854
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 if (eap->addr_count != 0)
2856 height = eap->line2;
2857 else
2858 height = QF_WINHEIGHT;
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861#ifdef FEAT_GUI
2862 need_mouse_correct = TRUE;
2863#endif
2864
2865 /*
2866 * Find existing quickfix window, or open a new one.
2867 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002868 win = qf_find_win(qi);
2869
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002870 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002873 if (eap->addr_count != 0)
2874 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002875 if (cmdmod.split & WSP_VERT)
2876 {
2877 if (height != W_WIDTH(win))
2878 win_setwidth(height);
2879 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002880 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002881 win_setheight(height);
2882 }
2883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 else
2885 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002886 qf_buf = qf_find_buf(qi);
2887
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 /* The current window becomes the previous window afterwards. */
2889 win = curwin;
2890
Bram Moolenaar77642c02012-11-20 17:55:10 +01002891 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2892 && cmdmod.split == 0)
2893 /* Create the new window at the very bottom, except when
2894 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002895 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002896 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002898 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002900 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002902 /*
2903 * For the location list window, create a reference to the
2904 * location list from the window 'win'.
2905 */
2906 curwin->w_llist_ref = win->w_llist;
2907 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002909
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002910 if (oldwin != curwin)
2911 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002912 if (qf_buf != NULL)
2913 /* Use the existing quickfix buffer */
2914 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002915 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002916 else
2917 {
2918 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002919 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002920 /* switch off 'swapfile' */
2921 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2922 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002923 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002924 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002925 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002926#ifdef FEAT_DIFF
2927 curwin->w_p_diff = FALSE;
2928#endif
2929#ifdef FEAT_FOLDING
2930 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2931 OPT_LOCAL);
2932#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002935 /* Only set the height when still in the same tab page and there is no
2936 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002937 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 win_setheight(height);
2939 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2940 if (win_valid(win))
2941 prevwin = win;
2942 }
2943
Bram Moolenaar81278ef2015-05-04 12:34:22 +02002944 qf_set_title_var(qi);
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 /*
2947 * Fill the buffer with the quickfix list.
2948 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02002949 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002951 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 curwin->w_cursor.col = 0;
2953 check_cursor();
2954 update_topline(); /* scroll to show the line */
2955}
2956
2957/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02002958 * Move the cursor in the quickfix window to "lnum".
2959 */
2960 static void
2961qf_win_goto(win_T *win, linenr_T lnum)
2962{
2963 win_T *old_curwin = curwin;
2964
2965 curwin = win;
2966 curbuf = win->w_buffer;
2967 curwin->w_cursor.lnum = lnum;
2968 curwin->w_cursor.col = 0;
2969#ifdef FEAT_VIRTUALEDIT
2970 curwin->w_cursor.coladd = 0;
2971#endif
2972 curwin->w_curswant = 0;
2973 update_topline(); /* scroll to show the line */
2974 redraw_later(VALID);
2975 curwin->w_redr_status = TRUE; /* update ruler */
2976 curwin = old_curwin;
2977 curbuf = curwin->w_buffer;
2978}
2979
2980/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02002981 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02002982 */
2983 void
2984ex_cbottom(exarg_T *eap UNUSED)
2985{
Bram Moolenaar537ef082016-07-09 17:56:19 +02002986 qf_info_T *qi = &ql_info;
2987 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02002988
Bram Moolenaar537ef082016-07-09 17:56:19 +02002989 if (eap->cmdidx == CMD_lbottom)
2990 {
2991 qi = GET_LOC_LIST(curwin);
2992 if (qi == NULL)
2993 {
2994 EMSG(_(e_loclist));
2995 return;
2996 }
2997 }
2998
2999 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003000 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3001 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3002}
3003
3004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 * Return the number of the current entry (line number in the quickfix
3006 * window).
3007 */
3008 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003009qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003011 qf_info_T *qi = &ql_info;
3012
3013 if (IS_LL_WINDOW(wp))
3014 /* In the location list window, use the referenced location list */
3015 qi = wp->w_llist_ref;
3016
3017 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018}
3019
3020/*
3021 * Update the cursor position in the quickfix window to the current error.
3022 * Return TRUE if there is a quickfix window.
3023 */
3024 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003025qf_win_pos_update(
3026 qf_info_T *qi,
3027 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003030 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
3032 /*
3033 * Put the cursor on the current error in the quickfix window, so that
3034 * it's viewable.
3035 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003036 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 if (win != NULL
3038 && qf_index <= win->w_buffer->b_ml.ml_line_count
3039 && old_qf_index != qf_index)
3040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 if (qf_index > old_qf_index)
3042 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003043 win->w_redraw_top = old_qf_index;
3044 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 }
3046 else
3047 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003048 win->w_redraw_top = qf_index;
3049 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003051 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
3053 return win != NULL;
3054}
3055
3056/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003057 * Check whether the given window is displaying the specified quickfix/location
3058 * list buffer
3059 */
3060 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003061is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003062{
3063 /*
3064 * A window displaying the quickfix buffer will have the w_llist_ref field
3065 * set to NULL.
3066 * A window displaying a location list buffer will have the w_llist_ref
3067 * pointing to the location list.
3068 */
3069 if (bt_quickfix(win->w_buffer))
3070 if ((qi == &ql_info && win->w_llist_ref == NULL)
3071 || (qi != &ql_info && win->w_llist_ref == qi))
3072 return TRUE;
3073
3074 return FALSE;
3075}
3076
3077/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003078 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003079 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003080 */
3081 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003082qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003083{
3084 win_T *win;
3085
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003086 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003087 if (is_qf_win(win, qi))
3088 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003089
3090 return win;
3091}
3092
3093/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003094 * Find a quickfix buffer.
3095 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 */
3097 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003098qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003100 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003101 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102
Bram Moolenaar9c102382006-05-03 21:26:49 +00003103 FOR_ALL_TAB_WINDOWS(tp, win)
3104 if (is_qf_win(win, qi))
3105 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003106
Bram Moolenaar9c102382006-05-03 21:26:49 +00003107 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108}
3109
3110/*
3111 * Find the quickfix buffer. If it exists, update the contents.
3112 */
3113 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003114qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003117 win_T *win;
3118 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
3121 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003122 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 if (buf != NULL)
3124 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003125 linenr_T old_line_count = buf->b_ml.ml_line_count;
3126
3127 if (old_last == NULL)
3128 /* set curwin/curbuf to buf and save a few things */
3129 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003131 if ((win = qf_find_win(qi)) != NULL)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003132 {
3133 curwin_save = curwin;
3134 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02003135 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003136 curwin = curwin_save;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003137 }
3138
Bram Moolenaar864293a2016-06-02 13:40:04 +02003139 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003140
Bram Moolenaar864293a2016-06-02 13:40:04 +02003141 if (old_last == NULL)
3142 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003143 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003144
3145 /* restore curwin/curbuf and a few other things */
3146 aucmd_restbuf(&aco);
3147 }
3148
3149 /* Only redraw when added lines are visible. This avoids flickering
3150 * when the added lines are not visible. */
3151 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3152 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 }
3154}
3155
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003156/*
3157 * Set "w:quickfix_title" if "qi" has a title.
3158 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003159 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003160qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003161{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003162 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3163 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003164 qi->qf_lists[qi->qf_curlist].qf_title);
3165}
3166
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167/*
3168 * Fill current buffer with quickfix errors, replacing any previous contents.
3169 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003170 * If "old_last" is not NULL append the items after this one.
3171 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3172 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 */
3174 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003175qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003177 linenr_T lnum;
3178 qfline_T *qfp;
3179 buf_T *errbuf;
3180 int len;
3181 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
Bram Moolenaar864293a2016-06-02 13:40:04 +02003183 if (old_last == NULL)
3184 {
3185 if (buf != curbuf)
3186 {
3187 EMSG2(_(e_intern2), "qf_fill_buffer()");
3188 return;
3189 }
3190
3191 /* delete all existing lines */
3192 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3193 (void)ml_delete((linenr_T)1, FALSE);
3194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003197 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
3199 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003200 if (old_last == NULL)
3201 {
3202 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3203 lnum = 0;
3204 }
3205 else
3206 {
3207 qfp = old_last->qf_next;
3208 lnum = buf->b_ml.ml_line_count;
3209 }
3210 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
3212 if (qfp->qf_fnum != 0
3213 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3214 && errbuf->b_fname != NULL)
3215 {
3216 if (qfp->qf_type == 1) /* :helpgrep */
3217 STRCPY(IObuff, gettail(errbuf->b_fname));
3218 else
3219 STRCPY(IObuff, errbuf->b_fname);
3220 len = (int)STRLEN(IObuff);
3221 }
3222 else
3223 len = 0;
3224 IObuff[len++] = '|';
3225
3226 if (qfp->qf_lnum > 0)
3227 {
3228 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3229 len += (int)STRLEN(IObuff + len);
3230
3231 if (qfp->qf_col > 0)
3232 {
3233 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3234 len += (int)STRLEN(IObuff + len);
3235 }
3236
3237 sprintf((char *)IObuff + len, "%s",
3238 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3239 len += (int)STRLEN(IObuff + len);
3240 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003241 else if (qfp->qf_pattern != NULL)
3242 {
3243 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3244 len += (int)STRLEN(IObuff + len);
3245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 IObuff[len++] = '|';
3247 IObuff[len++] = ' ';
3248
3249 /* Remove newlines and leading whitespace from the text.
3250 * For an unrecognized line keep the indent, the compiler may
3251 * mark a word with ^^^^. */
3252 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3253 IObuff + len, IOSIZE - len);
3254
Bram Moolenaar864293a2016-06-02 13:40:04 +02003255 if (ml_append_buf(buf, lnum, IObuff,
3256 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003258 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003260 if (qfp == NULL)
3261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003263
3264 if (old_last == NULL)
3265 /* Delete the empty line which is now at the end */
3266 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268
3269 /* correct cursor position */
3270 check_lnums(TRUE);
3271
Bram Moolenaar864293a2016-06-02 13:40:04 +02003272 if (old_last == NULL)
3273 {
3274 /* Set the 'filetype' to "qf" each time after filling the buffer.
3275 * This resembles reading a file into a buffer, it's more logical when
3276 * using autocommands. */
3277 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3278 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
3280#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003281 keep_filetype = TRUE; /* don't detect 'filetype' */
3282 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003284 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003286 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003288 /* make sure it will be redrawn */
3289 redraw_curbuf_later(NOT_VALID);
3290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
3292 /* Restore KeyTyped, setting 'filetype' may reset it. */
3293 KeyTyped = old_KeyTyped;
3294}
3295
3296#endif /* FEAT_WINDOWS */
3297
3298/*
3299 * Return TRUE if "buf" is the quickfix buffer.
3300 */
3301 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003302bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003304 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305}
3306
3307/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003308 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3309 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 */
3311 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003312bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003314 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3315 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316}
3317
3318/*
3319 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3320 */
3321 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003322bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003324 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325}
3326
3327 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003328bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
3330 if (bt_dontwrite(buf))
3331 {
3332 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3333 return TRUE;
3334 }
3335 return FALSE;
3336}
3337
3338/*
3339 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3340 * and 'bufhidden'.
3341 */
3342 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003343buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3346 switch (buf->b_p_bh[0])
3347 {
3348 case 'u': /* "unload" */
3349 case 'w': /* "wipe" */
3350 case 'd': return FALSE; /* "delete" */
3351 case 'h': return TRUE; /* "hide" */
3352 }
3353 return (p_hid || cmdmod.hide);
3354}
3355
3356/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003357 * Return TRUE when using ":vimgrep" for ":grep".
3358 */
3359 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003360grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003361{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003362 return ((cmdidx == CMD_grep
3363 || cmdidx == CMD_lgrep
3364 || cmdidx == CMD_grepadd
3365 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003366 && STRCMP("internal",
3367 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3368}
3369
3370/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003371 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 */
3373 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003374ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003376 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 char_u *cmd;
3378 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003379 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003380 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003381 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003382#ifdef FEAT_AUTOCMD
3383 char_u *au_name = NULL;
3384
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003385 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3386 if (grep_internal(eap->cmdidx))
3387 {
3388 ex_vimgrep(eap);
3389 return;
3390 }
3391
Bram Moolenaar7c626922005-02-07 22:01:03 +00003392 switch (eap->cmdidx)
3393 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003394 case CMD_make: au_name = (char_u *)"make"; break;
3395 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3396 case CMD_grep: au_name = (char_u *)"grep"; break;
3397 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3398 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3399 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003400 default: break;
3401 }
3402 if (au_name != NULL)
3403 {
3404 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3405 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00003406# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00003407 if (did_throw || force_abort)
3408 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003409# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003410 }
3411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaara6557602006-02-04 22:43:20 +00003413 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3414 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003415 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003418 fname = get_mef_name();
3419 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003421 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 /*
3424 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3425 */
3426 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3427 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003428 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 cmd = alloc(len);
3430 if (cmd == NULL)
3431 return;
3432 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3433 (char *)p_shq);
3434 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003435 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 /*
3437 * Output a newline if there's something else than the :make command that
3438 * was typed (in which case the cursor is in column 0).
3439 */
3440 if (msg_col == 0)
3441 msg_didout = FALSE;
3442 msg_start();
3443 MSG_PUTS(":!");
3444 msg_outtrans(cmd); /* show what we are doing */
3445
3446 /* let the shell know if we are redirecting output or not */
3447 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3448
3449#ifdef AMIGA
3450 out_flush();
3451 /* read window status report and redraw before message */
3452 (void)char_avail();
3453#endif
3454
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003455 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003456 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3457 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003458 && eap->cmdidx != CMD_lgrepadd),
3459 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003460 if (wp != NULL)
3461 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003462#ifdef FEAT_AUTOCMD
3463 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003464 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003465 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3466 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003467 if (qi->qf_curlist < qi->qf_listcount)
3468 res = qi->qf_lists[qi->qf_curlist].qf_count;
3469 else
3470 res = 0;
3471 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003472#endif
3473 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003474 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
Bram Moolenaar7c626922005-02-07 22:01:03 +00003476 mch_remove(fname);
3477 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 vim_free(cmd);
3479}
3480
3481/*
3482 * Return the name for the errorfile, in allocated memory.
3483 * Find a new unique name when 'makeef' contains "##".
3484 * Returns NULL for error.
3485 */
3486 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003487get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
3489 char_u *p;
3490 char_u *name;
3491 static int start = -1;
3492 static int off = 0;
3493#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003494 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495#endif
3496
3497 if (*p_mef == NUL)
3498 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003499 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (name == NULL)
3501 EMSG(_(e_notmp));
3502 return name;
3503 }
3504
3505 for (p = p_mef; *p; ++p)
3506 if (p[0] == '#' && p[1] == '#')
3507 break;
3508
3509 if (*p == NUL)
3510 return vim_strsave(p_mef);
3511
3512 /* Keep trying until the name doesn't exist yet. */
3513 for (;;)
3514 {
3515 if (start == -1)
3516 start = mch_get_pid();
3517 else
3518 off += 19;
3519
3520 name = alloc((unsigned)STRLEN(p_mef) + 30);
3521 if (name == NULL)
3522 break;
3523 STRCPY(name, p_mef);
3524 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3525 STRCAT(name, p + 2);
3526 if (mch_getperm(name) < 0
3527#ifdef HAVE_LSTAT
3528 /* Don't accept a symbolic link, its a security risk. */
3529 && mch_lstat((char *)name, &sb) < 0
3530#endif
3531 )
3532 break;
3533 vim_free(name);
3534 }
3535 return name;
3536}
3537
3538/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003539 * Returns the number of valid entries in the current quickfix/location list.
3540 */
3541 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003542qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003543{
3544 qf_info_T *qi = &ql_info;
3545 qfline_T *qfp;
3546 int i, sz = 0;
3547 int prev_fnum = 0;
3548
3549 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3550 {
3551 /* Location list */
3552 qi = GET_LOC_LIST(curwin);
3553 if (qi == NULL)
3554 return 0;
3555 }
3556
3557 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003558 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003559 ++i, qfp = qfp->qf_next)
3560 {
3561 if (qfp->qf_valid)
3562 {
3563 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3564 sz++; /* Count all valid entries */
3565 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3566 {
3567 /* Count the number of files */
3568 sz++;
3569 prev_fnum = qfp->qf_fnum;
3570 }
3571 }
3572 }
3573
3574 return sz;
3575}
3576
3577/*
3578 * Returns the current index of the quickfix/location list.
3579 * Returns 0 if there is an error.
3580 */
3581 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003582qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003583{
3584 qf_info_T *qi = &ql_info;
3585
3586 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3587 {
3588 /* Location list */
3589 qi = GET_LOC_LIST(curwin);
3590 if (qi == NULL)
3591 return 0;
3592 }
3593
3594 return qi->qf_lists[qi->qf_curlist].qf_index;
3595}
3596
3597/*
3598 * Returns the current index in the quickfix/location list (counting only valid
3599 * entries). If no valid entries are in the list, then returns 1.
3600 */
3601 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003602qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003603{
3604 qf_info_T *qi = &ql_info;
3605 qf_list_T *qfl;
3606 qfline_T *qfp;
3607 int i, eidx = 0;
3608 int prev_fnum = 0;
3609
3610 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3611 {
3612 /* Location list */
3613 qi = GET_LOC_LIST(curwin);
3614 if (qi == NULL)
3615 return 1;
3616 }
3617
3618 qfl = &qi->qf_lists[qi->qf_curlist];
3619 qfp = qfl->qf_start;
3620
3621 /* check if the list has valid errors */
3622 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3623 return 1;
3624
3625 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3626 {
3627 if (qfp->qf_valid)
3628 {
3629 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3630 {
3631 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3632 {
3633 /* Count the number of files */
3634 eidx++;
3635 prev_fnum = qfp->qf_fnum;
3636 }
3637 }
3638 else
3639 eidx++;
3640 }
3641 }
3642
3643 return eidx ? eidx : 1;
3644}
3645
3646/*
3647 * Get the 'n'th valid error entry in the quickfix or location list.
3648 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3649 * For :cdo and :ldo returns the 'n'th valid error entry.
3650 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3651 */
3652 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003653qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003654{
3655 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3656 qfline_T *qfp = qfl->qf_start;
3657 int i, eidx;
3658 int prev_fnum = 0;
3659
3660 /* check if the list has valid errors */
3661 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3662 return 1;
3663
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003664 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003665 i++, qfp = qfp->qf_next)
3666 {
3667 if (qfp->qf_valid)
3668 {
3669 if (fdo)
3670 {
3671 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3672 {
3673 /* Count the number of files */
3674 eidx++;
3675 prev_fnum = qfp->qf_fnum;
3676 }
3677 }
3678 else
3679 eidx++;
3680 }
3681
3682 if (eidx == n)
3683 break;
3684 }
3685
3686 if (i <= qfl->qf_count)
3687 return i;
3688 else
3689 return 1;
3690}
3691
3692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003694 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003695 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 */
3697 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003698ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003700 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003701 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003702
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003703 if (eap->cmdidx == CMD_ll
3704 || eap->cmdidx == CMD_lrewind
3705 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003706 || eap->cmdidx == CMD_llast
3707 || eap->cmdidx == CMD_ldo
3708 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003709 {
3710 qi = GET_LOC_LIST(curwin);
3711 if (qi == NULL)
3712 {
3713 EMSG(_(e_loclist));
3714 return;
3715 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003716 }
3717
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003718 if (eap->addr_count > 0)
3719 errornr = (int)eap->line2;
3720 else
3721 {
3722 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3723 errornr = 0;
3724 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3725 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3726 errornr = 1;
3727 else
3728 errornr = 32767;
3729 }
3730
3731 /* For cdo and ldo commands, jump to the nth valid error.
3732 * For cfdo and lfdo commands, jump to the nth valid file entry.
3733 */
3734 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3735 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3736 errornr = qf_get_nth_valid_entry(qi,
3737 eap->addr_count > 0 ? (int)eap->line1 : 1,
3738 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3739
3740 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741}
3742
3743/*
3744 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003745 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003746 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 */
3748 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003749ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003751 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003752 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003753
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003754 if (eap->cmdidx == CMD_lnext
3755 || eap->cmdidx == CMD_lNext
3756 || eap->cmdidx == CMD_lprevious
3757 || eap->cmdidx == CMD_lnfile
3758 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003759 || eap->cmdidx == CMD_lpfile
3760 || eap->cmdidx == CMD_ldo
3761 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003762 {
3763 qi = GET_LOC_LIST(curwin);
3764 if (qi == NULL)
3765 {
3766 EMSG(_(e_loclist));
3767 return;
3768 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003769 }
3770
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003771 if (eap->addr_count > 0 &&
3772 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3773 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3774 errornr = (int)eap->line2;
3775 else
3776 errornr = 1;
3777
3778 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3779 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003781 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3782 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003784 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3785 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 ? BACKWARD_FILE
3787 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003788 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789}
3790
3791/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003792 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003793 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
3795 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003796ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003798 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003799 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003800#ifdef FEAT_AUTOCMD
3801 char_u *au_name = NULL;
3802#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003803
3804 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003805 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003806 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003807
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003808#ifdef FEAT_AUTOCMD
3809 switch (eap->cmdidx)
3810 {
3811 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3812 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3813 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3814 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3815 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3816 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3817 default: break;
3818 }
3819 if (au_name != NULL)
3820 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3821#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003822#ifdef FEAT_BROWSE
3823 if (cmdmod.browse)
3824 {
3825 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3826 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3827 if (browse_file == NULL)
3828 return;
3829 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3830 vim_free(browse_file);
3831 }
3832 else
3833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003835 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003836
3837 /*
3838 * This function is used by the :cfile, :cgetfile and :caddfile
3839 * commands.
3840 * :cfile always creates a new quickfix list and jumps to the
3841 * first error.
3842 * :cgetfile creates a new quickfix list but doesn't jump to the
3843 * first error.
3844 * :caddfile adds to an existing quickfix list. If there is no
3845 * quickfix list then a new list is created.
3846 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003847 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003848 && eap->cmdidx != CMD_laddfile),
3849 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003850 && (eap->cmdidx == CMD_cfile
3851 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003852 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003853#ifdef FEAT_AUTOCMD
3854 if (au_name != NULL)
3855 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3856#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003857 if (wp != NULL)
3858 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003859 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003860 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003861
3862 else
3863 {
3864#ifdef FEAT_AUTOCMD
3865 if (au_name != NULL)
3866 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3867#endif
3868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869}
3870
3871/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003872 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003873 * ":vimgrepadd {pattern} file(s)"
3874 * ":lvimgrep {pattern} file(s)"
3875 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003876 */
3877 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003878ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003879{
Bram Moolenaar81695252004-12-29 20:58:21 +00003880 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003881 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003882 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003883 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003884 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003885 char_u *s;
3886 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003887 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003888 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003889#ifdef FEAT_AUTOCMD
3890 qfline_T *cur_qf_start;
3891#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003892 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003893 buf_T *buf;
3894 int duplicate_name = FALSE;
3895 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003896 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003897 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003898 buf_T *first_match_buf = NULL;
3899 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003900 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003901#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3902 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003903#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003904 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003905 int flags = 0;
3906 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003907 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003908 char_u *dirname_start = NULL;
3909 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003910 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003911#ifdef FEAT_AUTOCMD
3912 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003913
3914 switch (eap->cmdidx)
3915 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003916 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3917 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3918 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003919 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003920 case CMD_grep: au_name = (char_u *)"grep"; break;
3921 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3922 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3923 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003924 default: break;
3925 }
3926 if (au_name != NULL)
3927 {
3928 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3929 curbuf->b_fname, TRUE, curbuf);
3930 if (did_throw || force_abort)
3931 return;
3932 }
3933#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003934
Bram Moolenaar754b5602006-02-09 23:53:20 +00003935 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003936 || eap->cmdidx == CMD_lvimgrep
3937 || eap->cmdidx == CMD_lgrepadd
3938 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003939 {
3940 qi = ll_get_or_alloc_list(curwin);
3941 if (qi == NULL)
3942 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003943 }
3944
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003945 if (eap->addr_count > 0)
3946 tomatch = eap->line2;
3947 else
3948 tomatch = MAXLNUM;
3949
Bram Moolenaar81695252004-12-29 20:58:21 +00003950 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003951 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01003952 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003953 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003954 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003955 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003956 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003957 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003958 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003959
3960 if (s != NULL && *s == NUL)
3961 {
3962 /* Pattern is empty, use last search pattern. */
3963 if (last_search_pat() == NULL)
3964 {
3965 EMSG(_(e_noprevre));
3966 goto theend;
3967 }
3968 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3969 }
3970 else
3971 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3972
Bram Moolenaar86b68352004-12-27 21:59:20 +00003973 if (regmatch.regprog == NULL)
3974 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003975 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003976 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003977
3978 p = skipwhite(p);
3979 if (*p == NUL)
3980 {
3981 EMSG(_("E683: File name missing or invalid pattern"));
3982 goto theend;
3983 }
3984
Bram Moolenaar754b5602006-02-09 23:53:20 +00003985 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003986 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003987 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003988 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01003989 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003990
3991 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003992 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003993 goto theend;
3994 if (fcount == 0)
3995 {
3996 EMSG(_(e_nomatch));
3997 goto theend;
3998 }
3999
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004000 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4001 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004002 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004003 {
4004 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004005 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004006 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004007
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004008 /* Remember the current directory, because a BufRead autocommand that does
4009 * ":lcd %:p:h" changes the meaning of short path names. */
4010 mch_dirname(dirname_start, MAXPATHL);
4011
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004012#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004013 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004014 * changing the current quickfix list. */
4015 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4016#endif
4017
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004018 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004019 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004020 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004021 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004022 if (time(NULL) > seconds)
4023 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004024 /* Display the file name every second or so, show the user we are
4025 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004026 seconds = time(NULL);
4027 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004028 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004029 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004030 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004031 else
4032 {
4033 msg_outtrans(p);
4034 vim_free(p);
4035 }
4036 msg_clr_eos();
4037 msg_didout = FALSE; /* overwrite this message */
4038 msg_nowait = TRUE; /* don't wait for this message */
4039 msg_col = 0;
4040 out_flush();
4041 }
4042
Bram Moolenaar81695252004-12-29 20:58:21 +00004043 buf = buflist_findname_exp(fnames[fi]);
4044 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4045 {
4046 /* Remember that a buffer with this name already exists. */
4047 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004048 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004049 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004050
4051#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4052 /* Don't do Filetype autocommands to avoid loading syntax and
4053 * indent scripts, a great speed improvement. */
4054 save_ei = au_event_disable(",Filetype");
4055#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004056 /* Don't use modelines here, it's useless. */
4057 save_mls = p_mls;
4058 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004059
4060 /* Load file into a buffer, so that 'fileencoding' is detected,
4061 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004062 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004063
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004064 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004065#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4066 au_event_restore(save_ei);
4067#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004068 }
4069 else
4070 /* Use existing, loaded buffer. */
4071 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004072
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004073#ifdef FEAT_AUTOCMD
4074 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4075 {
4076 int idx;
4077
4078 /* Autocommands changed the quickfix list. Find the one we were
4079 * using and restore it. */
4080 for (idx = 0; idx < LISTCOUNT; ++idx)
4081 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4082 {
4083 qi->qf_curlist = idx;
4084 break;
4085 }
4086 if (idx == LISTCOUNT)
4087 {
4088 /* List cannot be found, create a new one. */
4089 qf_new_list(qi, *eap->cmdlinep);
4090 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4091 }
4092 }
4093#endif
4094
Bram Moolenaar81695252004-12-29 20:58:21 +00004095 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004096 {
4097 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004098 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004099 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004100 else
4101 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004102 /* Try for a match in all lines of the buffer.
4103 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004104 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004105 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4106 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004107 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004108 col = 0;
4109 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004110 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004111 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004112 /* Pass the buffer number so that it gets used even for a
4113 * dummy buffer, unless duplicate_name is set, then the
4114 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004115 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004116 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004117 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004118 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004119 ml_get_buf(buf,
4120 regmatch.startpos[0].lnum + lnum, FALSE),
4121 regmatch.startpos[0].lnum + lnum,
4122 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004123 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004124 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004125 0, /* nr */
4126 0, /* type */
4127 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004128 ) == FAIL)
4129 {
4130 got_int = TRUE;
4131 break;
4132 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004133 found_match = TRUE;
4134 if (--tomatch == 0)
4135 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004136 if ((flags & VGR_GLOBAL) == 0
4137 || regmatch.endpos[0].lnum > 0)
4138 break;
4139 col = regmatch.endpos[0].col
4140 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004141 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004142 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004143 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004144 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004145 if (got_int)
4146 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004147 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004148#ifdef FEAT_AUTOCMD
4149 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4150#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004151
4152 if (using_dummy)
4153 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004154 if (found_match && first_match_buf == NULL)
4155 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004156 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004157 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004158 /* Never keep a dummy buffer if there is another buffer
4159 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004160 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004161 buf = NULL;
4162 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004163 else if (!cmdmod.hide
4164 || buf->b_p_bh[0] == 'u' /* "unload" */
4165 || buf->b_p_bh[0] == 'w' /* "wipe" */
4166 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004167 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004168 /* When no match was found we don't need to remember the
4169 * buffer, wipe it out. If there was a match and it
4170 * wasn't the first one or we won't jump there: only
4171 * unload the buffer.
4172 * Ignore 'hidden' here, because it may lead to having too
4173 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004174 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004175 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004176 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004177 buf = NULL;
4178 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004179 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004180 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004181 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004182 /* Keeping the buffer, remove the dummy flag. */
4183 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004184 buf = NULL;
4185 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004186 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004187
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004188 if (buf != NULL)
4189 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004190 /* Keeping the buffer, remove the dummy flag. */
4191 buf->b_flags &= ~BF_DUMMY;
4192
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004193 /* If the buffer is still loaded we need to use the
4194 * directory we jumped to below. */
4195 if (buf == first_match_buf
4196 && target_dir == NULL
4197 && STRCMP(dirname_start, dirname_now) != 0)
4198 target_dir = vim_strsave(dirname_now);
4199
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004200 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004201 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004202 * need to be done (again). But not the window-local
4203 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004204 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004205#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004206 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4207 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004208#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004209 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004210 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004211 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004212 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004213 }
4214 }
4215
4216 FreeWild(fcount, fnames);
4217
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004218 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4219 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4220 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004221
4222#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004223 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004224#endif
4225
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004226#ifdef FEAT_AUTOCMD
4227 if (au_name != NULL)
4228 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4229 curbuf->b_fname, TRUE, curbuf);
4230#endif
4231
Bram Moolenaar86b68352004-12-27 21:59:20 +00004232 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004233 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004234 {
4235 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004236 {
4237 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004238 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004239 if (buf != curbuf)
4240 /* If we jumped to another buffer redrawing will already be
4241 * taken care of. */
4242 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004243
4244 /* Jump to the directory used after loading the buffer. */
4245 if (curbuf == first_match_buf && target_dir != NULL)
4246 {
4247 exarg_T ea;
4248
4249 ea.arg = target_dir;
4250 ea.cmdidx = CMD_lcd;
4251 ex_cd(&ea);
4252 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004253 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004254 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004255 else
4256 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004257
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004258 /* If we loaded a dummy buffer into the current window, the autocommands
4259 * may have messed up things, need to redraw and recompute folds. */
4260 if (redraw_for_dummy)
4261 {
4262#ifdef FEAT_FOLDING
4263 foldUpdateAll(curwin);
4264#else
4265 redraw_later(NOT_VALID);
4266#endif
4267 }
4268
Bram Moolenaar86b68352004-12-27 21:59:20 +00004269theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004270 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004271 vim_free(dirname_now);
4272 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004273 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004274 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004275}
4276
4277/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00004278 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00004279 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004280 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
4281 * If "s" is not NULL terminate the pattern with a NUL.
4282 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00004283 */
4284 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004285skip_vimgrep_pat(char_u *p, char_u **s, int *flags)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004286{
4287 int c;
4288
4289 if (vim_isIDc(*p))
4290 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004291 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004292 if (s != NULL)
4293 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004294 p = skiptowhite(p);
4295 if (s != NULL && *p != NUL)
4296 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004297 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004298 else
4299 {
4300 /* ":vimgrep /pattern/[g][j] fname" */
4301 if (s != NULL)
4302 *s = p + 1;
4303 c = *p;
4304 p = skip_regexp(p + 1, c, TRUE, NULL);
4305 if (*p != c)
4306 return NULL;
4307
4308 /* Truncate the pattern. */
4309 if (s != NULL)
4310 *p = NUL;
4311 ++p;
4312
4313 /* Find the flags */
4314 while (*p == 'g' || *p == 'j')
4315 {
4316 if (flags != NULL)
4317 {
4318 if (*p == 'g')
4319 *flags |= VGR_GLOBAL;
4320 else
4321 *flags |= VGR_NOJUMP;
4322 }
4323 ++p;
4324 }
4325 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00004326 return p;
4327}
4328
4329/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004330 * Restore current working directory to "dirname_start" if they differ, taking
4331 * into account whether it is set locally or globally.
4332 */
4333 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004334restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004335{
4336 char_u *dirname_now = alloc(MAXPATHL);
4337
4338 if (NULL != dirname_now)
4339 {
4340 mch_dirname(dirname_now, MAXPATHL);
4341 if (STRCMP(dirname_start, dirname_now) != 0)
4342 {
4343 /* If the directory has changed, change it back by building up an
4344 * appropriate ex command and executing it. */
4345 exarg_T ea;
4346
4347 ea.arg = dirname_start;
4348 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4349 ex_cd(&ea);
4350 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004351 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004352 }
4353}
4354
4355/*
4356 * Load file "fname" into a dummy buffer and return the buffer pointer,
4357 * placing the directory resulting from the buffer load into the
4358 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4359 * prior to calling this function. Restores directory to "dirname_start" prior
4360 * to returning, if autocmds or the 'autochdir' option have changed it.
4361 *
4362 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4363 * or wipe_dummy_buffer() later!
4364 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004365 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004366 */
4367 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004368load_dummy_buffer(
4369 char_u *fname,
4370 char_u *dirname_start, /* in: old directory */
4371 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004372{
4373 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004374 bufref_T newbufref;
4375 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004376 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004377 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004378
4379 /* Allocate a buffer without putting it in the buffer list. */
4380 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4381 if (newbuf == NULL)
4382 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004383 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004384
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004385 /* Init the options. */
4386 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4387
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004388 /* need to open the memfile before putting the buffer in a window */
4389 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004390 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004391 /* set curwin/curbuf to buf and save a few things */
4392 aucmd_prepbuf(&aco, newbuf);
4393
4394 /* Need to set the filename for autocommands. */
4395 (void)setfname(curbuf, fname, NULL, FALSE);
4396
Bram Moolenaar81695252004-12-29 20:58:21 +00004397 /* Create swap file now to avoid the ATTENTION message. */
4398 check_need_swap(TRUE);
4399
4400 /* Remove the "dummy" flag, otherwise autocommands may not
4401 * work. */
4402 curbuf->b_flags &= ~BF_DUMMY;
4403
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004404 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004405 if (readfile(fname, NULL,
4406 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4407 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004408 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004409 && !(curbuf->b_flags & BF_NEW))
4410 {
4411 failed = FALSE;
4412 if (curbuf != newbuf)
4413 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004414 /* Bloody autocommands changed the buffer! Can happen when
4415 * using netrw and editing a remote file. Use the current
4416 * buffer instead, delete the dummy one after restoring the
4417 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004418 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004419 newbuf = curbuf;
4420 }
4421 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004422
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004423 /* restore curwin/curbuf and a few other things */
4424 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004425 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4426 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004427
4428 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4429 * skip it. */
4430 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004431 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004432
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004433 /*
4434 * When autocommands/'autochdir' option changed directory: go back.
4435 * Let the caller know what the resulting dir was first, in case it is
4436 * important.
4437 */
4438 mch_dirname(resulting_dir, MAXPATHL);
4439 restore_start_dir(dirname_start);
4440
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004441 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004442 return NULL;
4443 if (failed)
4444 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004445 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004446 return NULL;
4447 }
4448 return newbuf;
4449}
4450
4451/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004452 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4453 * directory to "dirname_start" prior to returning, if autocmds or the
4454 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004455 */
4456 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004457wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004458{
4459 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004460 {
4461#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4462 cleanup_T cs;
4463
4464 /* Reset the error/interrupt/exception state here so that aborting()
4465 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4466 * work when got_int is set. */
4467 enter_cleanup(&cs);
4468#endif
4469
Bram Moolenaar81695252004-12-29 20:58:21 +00004470 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004471
4472#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4473 /* Restore the error/interrupt/exception state if not discarded by a
4474 * new aborting error, interrupt, or uncaught exception. */
4475 leave_cleanup(&cs);
4476#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004477 /* When autocommands/'autochdir' option changed directory: go back. */
4478 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004479 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004480}
4481
4482/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004483 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4484 * directory to "dirname_start" prior to returning, if autocmds or the
4485 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004486 */
4487 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004488unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004489{
4490 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004491 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004492 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004493
4494 /* When autocommands/'autochdir' option changed directory: go back. */
4495 restore_start_dir(dirname_start);
4496 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004497}
4498
Bram Moolenaar05159a02005-02-26 23:04:13 +00004499#if defined(FEAT_EVAL) || defined(PROTO)
4500/*
4501 * Add each quickfix error to list "list" as a dictionary.
4502 */
4503 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004504get_errorlist(win_T *wp, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004505{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004506 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004507 dict_T *dict;
4508 char_u buf[2];
4509 qfline_T *qfp;
4510 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004511 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004512
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004513 if (wp != NULL)
4514 {
4515 qi = GET_LOC_LIST(wp);
4516 if (qi == NULL)
4517 return FAIL;
4518 }
4519
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004520 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004521 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004522 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004523
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004524 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4525 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004526 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004527 /* Handle entries with a non-existing buffer number. */
4528 bufnum = qfp->qf_fnum;
4529 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4530 bufnum = 0;
4531
Bram Moolenaar05159a02005-02-26 23:04:13 +00004532 if ((dict = dict_alloc()) == NULL)
4533 return FAIL;
4534 if (list_append_dict(list, dict) == FAIL)
4535 return FAIL;
4536
4537 buf[0] = qfp->qf_type;
4538 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004539 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004540 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4541 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4542 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4543 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004544 || dict_add_nr_str(dict, "pattern", 0L,
4545 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4546 || dict_add_nr_str(dict, "text", 0L,
4547 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004548 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4549 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4550 return FAIL;
4551
4552 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004553 if (qfp == NULL)
4554 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004555 }
4556 return OK;
4557}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004558
4559/*
4560 * Populate the quickfix list with the items supplied in the list
Bram Moolenaar864293a2016-06-02 13:40:04 +02004561 * of dictionaries. "title" will be copied to w:quickfix_title.
4562 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004563 */
4564 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004565set_errorlist(
4566 win_T *wp,
4567 list_T *list,
4568 int action,
4569 char_u *title)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004570{
4571 listitem_T *li;
4572 dict_T *d;
4573 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004574 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004575 long lnum;
4576 int col, nr;
4577 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004578#ifdef FEAT_WINDOWS
4579 qfline_T *old_last = NULL;
4580#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004581 int valid, status;
4582 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004583 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004584 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004585
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004586 if (wp != NULL)
4587 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00004588 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004589 if (qi == NULL)
4590 return FAIL;
4591 }
4592
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004593 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004594 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004595 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004596#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004597 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4598 /* Adding to existing list, use last entry. */
4599 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004600#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004601 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004602 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004603 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004604 qf_store_title(qi, title);
4605 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004606
4607 for (li = list->lv_first; li != NULL; li = li->li_next)
4608 {
4609 if (li->li_tv.v_type != VAR_DICT)
4610 continue; /* Skip non-dict items */
4611
4612 d = li->li_tv.vval.v_dict;
4613 if (d == NULL)
4614 continue;
4615
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004616 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004617 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4618 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4619 col = (int)get_dict_number(d, (char_u *)"col");
4620 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4621 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004622 type = get_dict_string(d, (char_u *)"type", TRUE);
4623 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4624 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004625 if (text == NULL)
4626 text = vim_strsave((char_u *)"");
4627
4628 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004629 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004630 valid = FALSE;
4631
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004632 /* Mark entries with non-existing buffer number as not valid. Give the
4633 * error message only once. */
4634 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4635 {
4636 if (!did_bufnr_emsg)
4637 {
4638 did_bufnr_emsg = TRUE;
4639 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4640 }
4641 valid = FALSE;
4642 bufnum = 0;
4643 }
4644
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004645 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004646 NULL, /* dir */
4647 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004648 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004649 text,
4650 lnum,
4651 col,
4652 vcol, /* vis_col */
4653 pattern, /* search pattern */
4654 nr,
4655 type == NULL ? NUL : *type,
4656 valid);
4657
4658 vim_free(filename);
4659 vim_free(pattern);
4660 vim_free(text);
4661 vim_free(type);
4662
4663 if (status == FAIL)
4664 {
4665 retval = FAIL;
4666 break;
4667 }
4668 }
4669
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004670 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004671 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004672 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4673 else
4674 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004675 if (action != 'a') {
4676 qi->qf_lists[qi->qf_curlist].qf_ptr =
4677 qi->qf_lists[qi->qf_curlist].qf_start;
4678 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4679 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4680 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004681
4682#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004683 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004684 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004685#endif
4686
4687 return retval;
4688}
Bram Moolenaar05159a02005-02-26 23:04:13 +00004689#endif
4690
Bram Moolenaar81695252004-12-29 20:58:21 +00004691/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004692 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004693 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004694 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004695 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00004696 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00004697 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00004698 */
4699 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004700ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004701{
4702 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004703 qf_info_T *qi = &ql_info;
4704
Bram Moolenaardb552d602006-03-23 22:59:57 +00004705 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
4706 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004707 {
4708 qi = ll_get_or_alloc_list(curwin);
4709 if (qi == NULL)
4710 return;
4711 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004712
4713 if (*eap->arg == NUL)
4714 buf = curbuf;
4715 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
4716 buf = buflist_findnr(atoi((char *)eap->arg));
4717 if (buf == NULL)
4718 EMSG(_(e_invarg));
4719 else if (buf->b_ml.ml_mfp == NULL)
4720 EMSG(_("E681: Buffer is not loaded"));
4721 else
4722 {
4723 if (eap->addr_count == 0)
4724 {
4725 eap->line1 = 1;
4726 eap->line2 = buf->b_ml.ml_line_count;
4727 }
4728 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
4729 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
4730 EMSG(_(e_invrange));
4731 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00004732 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004733 char_u *qf_title = *eap->cmdlinep;
4734
4735 if (buf->b_sfname)
4736 {
4737 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
4738 (char *)qf_title, (char *)buf->b_sfname);
4739 qf_title = IObuff;
4740 }
4741
Bram Moolenaardb552d602006-03-23 22:59:57 +00004742 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
4743 (eap->cmdidx != CMD_caddbuffer
4744 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004745 eap->line1, eap->line2,
4746 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004747 && (eap->cmdidx == CMD_cbuffer
4748 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004749 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4750 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004751 }
4752}
4753
Bram Moolenaar1e015462005-09-25 22:16:38 +00004754#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004755/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004756 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4757 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004758 */
4759 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004760ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004761{
4762 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004763 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004764
Bram Moolenaardb552d602006-03-23 22:59:57 +00004765 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4766 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004767 {
4768 qi = ll_get_or_alloc_list(curwin);
4769 if (qi == NULL)
4770 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004771 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004772
Bram Moolenaar4770d092006-01-12 23:22:24 +00004773 /* Evaluate the expression. When the result is a string or a list we can
4774 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004775 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004776 if (tv != NULL)
4777 {
4778 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4779 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4780 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01004781 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004782 (eap->cmdidx != CMD_caddexpr
4783 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004784 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004785 && (eap->cmdidx == CMD_cexpr
4786 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004787 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004788 }
4789 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004790 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004791 free_tv(tv);
4792 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004793}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004794#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004795
4796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 * ":helpgrep {pattern}"
4798 */
4799 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004800ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
4802 regmatch_T regmatch;
4803 char_u *save_cpo;
4804 char_u *p;
4805 int fcount;
4806 char_u **fnames;
4807 FILE *fd;
4808 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004810#ifdef FEAT_MULTI_LANG
4811 char_u *lang;
4812#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004813 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004814 int new_qi = FALSE;
4815 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004816#ifdef FEAT_AUTOCMD
4817 char_u *au_name = NULL;
4818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004820#ifdef FEAT_MULTI_LANG
4821 /* Check for a specified language */
4822 lang = check_help_lang(eap->arg);
4823#endif
4824
Bram Moolenaar73633f82012-01-20 13:39:07 +01004825#ifdef FEAT_AUTOCMD
4826 switch (eap->cmdidx)
4827 {
4828 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4829 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4830 default: break;
4831 }
4832 if (au_name != NULL)
4833 {
4834 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4835 curbuf->b_fname, TRUE, curbuf);
4836 if (did_throw || force_abort)
4837 return;
4838 }
4839#endif
4840
4841 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4842 save_cpo = p_cpo;
4843 p_cpo = empty_option;
4844
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004845 if (eap->cmdidx == CMD_lhelpgrep)
4846 {
4847 /* Find an existing help window */
4848 FOR_ALL_WINDOWS(wp)
4849 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4850 break;
4851
4852 if (wp == NULL) /* Help window not found */
4853 qi = NULL;
4854 else
4855 qi = wp->w_llist;
4856
4857 if (qi == NULL)
4858 {
4859 /* Allocate a new location list for help text matches */
4860 if ((qi = ll_new_list()) == NULL)
4861 return;
4862 new_qi = TRUE;
4863 }
4864 }
4865
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4867 regmatch.rm_ic = FALSE;
4868 if (regmatch.regprog != NULL)
4869 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004870#ifdef FEAT_MBYTE
4871 vimconv_T vc;
4872
4873 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4874 * differs. */
4875 vc.vc_type = CONV_NONE;
4876 if (!enc_utf8)
4877 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4878#endif
4879
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004881 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882
4883 /* Go through all directories in 'runtimepath' */
4884 p = p_rtp;
4885 while (*p != NUL && !got_int)
4886 {
4887 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4888
4889 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4890 add_pathsep(NameBuff);
4891 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4892 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4893 &fnames, EW_FILE|EW_SILENT) == OK
4894 && fcount > 0)
4895 {
4896 for (fi = 0; fi < fcount && !got_int; ++fi)
4897 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004898#ifdef FEAT_MULTI_LANG
4899 /* Skip files for a different language. */
4900 if (lang != NULL
4901 && STRNICMP(lang, fnames[fi]
4902 + STRLEN(fnames[fi]) - 3, 2) != 0
4903 && !(STRNICMP(lang, "en", 2) == 0
4904 && STRNICMP("txt", fnames[fi]
4905 + STRLEN(fnames[fi]) - 3, 3) == 0))
4906 continue;
4907#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004908 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (fd != NULL)
4910 {
4911 lnum = 1;
4912 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4913 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004914 char_u *line = IObuff;
4915#ifdef FEAT_MBYTE
4916 /* Convert a line if 'encoding' is not utf-8 and
4917 * the line contains a non-ASCII character. */
4918 if (vc.vc_type != CONV_NONE
4919 && has_non_ascii(IObuff)) {
4920 line = string_convert(&vc, IObuff, NULL);
4921 if (line == NULL)
4922 line = IObuff;
4923 }
4924#endif
4925
4926 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004928 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
4930 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004931 while (l > 0 && line[l - 1] <= ' ')
4932 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004934 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 NULL, /* dir */
4936 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004937 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004938 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004940 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004941 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004942 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004943 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 0, /* nr */
4945 1, /* type */
4946 TRUE /* valid */
4947 ) == FAIL)
4948 {
4949 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004950#ifdef FEAT_MBYTE
4951 if (line != IObuff)
4952 vim_free(line);
4953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 break;
4955 }
4956 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004957#ifdef FEAT_MBYTE
4958 if (line != IObuff)
4959 vim_free(line);
4960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 ++lnum;
4962 line_breakcheck();
4963 }
4964 fclose(fd);
4965 }
4966 }
4967 FreeWild(fcount, fnames);
4968 }
4969 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004970
Bram Moolenaar473de612013-06-08 18:19:48 +02004971 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004972#ifdef FEAT_MBYTE
4973 if (vc.vc_type != CONV_NONE)
4974 convert_setup(&vc, NULL, NULL);
4975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004977 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4978 qi->qf_lists[qi->qf_curlist].qf_ptr =
4979 qi->qf_lists[qi->qf_curlist].qf_start;
4980 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004983 if (p_cpo == empty_option)
4984 p_cpo = save_cpo;
4985 else
4986 /* Darn, some plugin changed the value. */
4987 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004990 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991#endif
4992
Bram Moolenaar73633f82012-01-20 13:39:07 +01004993#ifdef FEAT_AUTOCMD
4994 if (au_name != NULL)
4995 {
4996 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4997 curbuf->b_fname, TRUE, curbuf);
4998 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4999 /* autocommands made "qi" invalid */
5000 return;
5001 }
5002#endif
5003
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005005 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005006 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005007 else
5008 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005009
5010 if (eap->cmdidx == CMD_lhelpgrep)
5011 {
5012 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005013 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005014 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5015 {
5016 if (new_qi)
5017 ll_free_all(&qi);
5018 }
5019 else if (curwin->w_llist == NULL)
5020 curwin->w_llist = qi;
5021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022}
5023
5024#endif /* FEAT_QUICKFIX */