blob: 8b61e10bc60bbed0cb8a2a355382968a1145a8c0 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaar8f77c5a2017-04-30 14:21:00 +020060 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaard12f5c12006-01-25 22:10:52 +000063struct qf_info_S
64{
65 /*
66 * Count of references to this list. Used only for location lists.
67 * When a location list window reference this list, qf_refcount
68 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
69 * reaches 0, the list is freed.
70 */
71 int qf_refcount;
72 int qf_listcount; /* current number of lists */
73 int qf_curlist; /* current error list */
74 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar361c8f02016-07-02 15:41:47 +020075
76 int qf_dir_curlist; /* error list for qf_dir_stack */
77 struct dir_stack_T *qf_dir_stack;
78 char_u *qf_directory;
79 struct dir_stack_T *qf_file_stack;
80 char_u *qf_currfile;
81 int qf_multiline;
82 int qf_multiignore;
83 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000084};
85
86static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
Bram Moolenaar68b76a62005-03-25 21:53:48 +000088#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
90/*
91 * Structure used to hold the info of one part of 'errorformat'
92 */
Bram Moolenaar01265852006-03-20 21:50:15 +000093typedef struct efm_S efm_T;
94struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000095{
96 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000097 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
99 char_u prefix; /* prefix of this format line: */
100 /* 'D' enter directory */
101 /* 'X' leave directory */
102 /* 'A' start of multi-line message */
103 /* 'E' error message */
104 /* 'W' warning message */
105 /* 'I' informational message */
106 /* 'C' continuation line */
107 /* 'Z' end of multi-line message */
108 /* 'G' general, unspecific message */
109 /* 'P' push file (partial) message */
110 /* 'Q' pop/quit file (partial) message */
111 /* 'O' overread (partial) message */
112 char_u flags; /* additional flags given in prefix */
113 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000114 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000115 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116};
117
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100118static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
119
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100120static 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, char_u *enc);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200121static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100122static void qf_new_list(qf_info_T *qi, char_u *qf_title);
123static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200124static int qf_add_entry(qf_info_T *qi, int qf_idx, 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 +0100125static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static void qf_free(qf_info_T *qi, int idx);
127static char_u *qf_types(int, int);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200128static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *);
129static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200131static char_u *qf_guess_filepath(qf_info_T *qi, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
133static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
136static int is_qf_win(win_T *win, qf_info_T *qi);
137static win_T *qf_find_win(qf_info_T *qi);
138static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200139static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200141static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *get_mef_name(void);
144static void restore_start_dir(char_u *dirname_start);
145static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
146static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
147static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
148static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000150/* Quickfix window check helper macro */
151#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
152/* Location list window check helper macro */
153#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
154/*
155 * Return location list for window 'wp'
156 * For location list window, return the referenced location list
157 */
158#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200161 * Looking up a buffer can be slow if there are many. Remember the last one
162 * to make this a lot faster if there are multiple matches in the same file.
163 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200164static char_u *qf_last_bufname = NULL;
165static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200166
167/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000168 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200169 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 * Return -1 for error, number of errors for success.
171 */
172 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100173qf_init(
174 win_T *wp,
175 char_u *efile,
176 char_u *errorformat,
177 int newlist, /* TRUE: start a new error list */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100178 char_u *qf_title,
179 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000181 qf_info_T *qi = &ql_info;
182
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000183 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000184 {
185 qi = ll_get_or_alloc_list(wp);
186 if (qi == NULL)
187 return FAIL;
188 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000189
190 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200191 (linenr_T)0, (linenr_T)0,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100192 qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000193}
194
195/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200196 * Maximum number of bytes allowed per line while reading a errorfile.
197 */
198#define LINE_MAXLEN 4096
199
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200200static struct fmtpattern
201{
202 char_u convchar;
203 char *pattern;
204} fmt_pat[FMT_PATTERNS] =
205 {
206 {'f', ".\\+"}, /* only used when at end */
207 {'n', "\\d\\+"},
208 {'l', "\\d\\+"},
209 {'c', "\\d\\+"},
210 {'t', "."},
211 {'m', ".\\+"},
212 {'r', ".*"},
213 {'p', "[- .]*"},
214 {'v', "\\d\\+"},
215 {'s', ".\\+"}
216 };
217
218/*
219 * Converts a 'errorformat' string to regular expression pattern
220 */
221 static int
222efm_to_regpat(
223 char_u *efm,
224 int len,
225 efm_T *fmt_ptr,
226 char_u *regpat,
227 char_u *errmsg)
228{
229 char_u *ptr;
230 char_u *efmp;
231 char_u *srcptr;
232 int round;
233 int idx = 0;
234
235 /*
236 * Build regexp pattern from current 'errorformat' option
237 */
238 ptr = regpat;
239 *ptr++ = '^';
240 round = 0;
241 for (efmp = efm; efmp < efm + len; ++efmp)
242 {
243 if (*efmp == '%')
244 {
245 ++efmp;
246 for (idx = 0; idx < FMT_PATTERNS; ++idx)
247 if (fmt_pat[idx].convchar == *efmp)
248 break;
249 if (idx < FMT_PATTERNS)
250 {
251 if (fmt_ptr->addr[idx])
252 {
253 sprintf((char *)errmsg,
254 _("E372: Too many %%%c in format string"), *efmp);
255 EMSG(errmsg);
256 return -1;
257 }
258 if ((idx
259 && idx < 6
260 && vim_strchr((char_u *)"DXOPQ",
261 fmt_ptr->prefix) != NULL)
262 || (idx == 6
263 && vim_strchr((char_u *)"OPQ",
264 fmt_ptr->prefix) == NULL))
265 {
266 sprintf((char *)errmsg,
267 _("E373: Unexpected %%%c in format string"), *efmp);
268 EMSG(errmsg);
269 return -1;
270 }
271 fmt_ptr->addr[idx] = (char_u)++round;
272 *ptr++ = '\\';
273 *ptr++ = '(';
274#ifdef BACKSLASH_IN_FILENAME
275 if (*efmp == 'f')
276 {
277 /* Also match "c:" in the file name, even when
278 * checking for a colon next: "%f:".
279 * "\%(\a:\)\=" */
280 STRCPY(ptr, "\\%(\\a:\\)\\=");
281 ptr += 10;
282 }
283#endif
284 if (*efmp == 'f' && efmp[1] != NUL)
285 {
286 if (efmp[1] != '\\' && efmp[1] != '%')
287 {
288 /* A file name may contain spaces, but this isn't
289 * in "\f". For "%f:%l:%m" there may be a ":" in
290 * the file name. Use ".\{-1,}x" instead (x is
291 * the next character), the requirement that :999:
292 * follows should work. */
293 STRCPY(ptr, ".\\{-1,}");
294 ptr += 7;
295 }
296 else
297 {
298 /* File name followed by '\\' or '%': include as
299 * many file name chars as possible. */
300 STRCPY(ptr, "\\f\\+");
301 ptr += 4;
302 }
303 }
304 else
305 {
306 srcptr = (char_u *)fmt_pat[idx].pattern;
307 while ((*ptr = *srcptr++) != NUL)
308 ++ptr;
309 }
310 *ptr++ = '\\';
311 *ptr++ = ')';
312 }
313 else if (*efmp == '*')
314 {
315 if (*++efmp == '[' || *efmp == '\\')
316 {
317 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
318 {
319 if (efmp[1] == '^')
320 *ptr++ = *++efmp;
321 if (efmp < efm + len)
322 {
323 *ptr++ = *++efmp; /* could be ']' */
324 while (efmp < efm + len
325 && (*ptr++ = *++efmp) != ']')
326 /* skip */;
327 if (efmp == efm + len)
328 {
329 EMSG(_("E374: Missing ] in format string"));
330 return -1;
331 }
332 }
333 }
334 else if (efmp < efm + len) /* %*\D, %*\s etc. */
335 *ptr++ = *++efmp;
336 *ptr++ = '\\';
337 *ptr++ = '+';
338 }
339 else
340 {
341 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
342 sprintf((char *)errmsg,
343 _("E375: Unsupported %%%c in format string"), *efmp);
344 EMSG(errmsg);
345 return -1;
346 }
347 }
348 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
349 *ptr++ = *efmp; /* regexp magic characters */
350 else if (*efmp == '#')
351 *ptr++ = '*';
352 else if (*efmp == '>')
353 fmt_ptr->conthere = TRUE;
354 else if (efmp == efm + 1) /* analyse prefix */
355 {
356 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
357 fmt_ptr->flags = *efmp++;
358 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
359 fmt_ptr->prefix = *efmp;
360 else
361 {
362 sprintf((char *)errmsg,
363 _("E376: Invalid %%%c in format string prefix"), *efmp);
364 EMSG(errmsg);
365 return -1;
366 }
367 }
368 else
369 {
370 sprintf((char *)errmsg,
371 _("E377: Invalid %%%c in format string"), *efmp);
372 EMSG(errmsg);
373 return -1;
374 }
375 }
376 else /* copy normal character */
377 {
378 if (*efmp == '\\' && efmp + 1 < efm + len)
379 ++efmp;
380 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
381 *ptr++ = '\\'; /* escape regexp atoms */
382 if (*efmp)
383 *ptr++ = *efmp;
384 }
385 }
386 *ptr++ = '$';
387 *ptr = NUL;
388
389 return 0;
390}
391
392 static void
393free_efm_list(efm_T **efm_first)
394{
395 efm_T *efm_ptr;
396
397 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
398 {
399 *efm_first = efm_ptr->next;
400 vim_regfree(efm_ptr->prog);
401 vim_free(efm_ptr);
402 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100403 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200404}
405
406/* Parse 'errorformat' option */
407 static efm_T *
408parse_efm_option(char_u *efm)
409{
410 char_u *errmsg = NULL;
411 int errmsglen;
412 efm_T *fmt_ptr = NULL;
413 efm_T *fmt_first = NULL;
414 efm_T *fmt_last = NULL;
415 char_u *fmtstr = NULL;
416 int len;
417 int i;
418 int round;
419
420 errmsglen = CMDBUFFSIZE + 1;
421 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
422 if (errmsg == NULL)
423 goto parse_efm_end;
424
425 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200426 * Each part of the format string is copied and modified from errorformat
427 * to regex prog. Only a few % characters are allowed.
428 */
429
430 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200431 * Get some space to modify the format string into.
432 */
433 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
434 for (round = FMT_PATTERNS; round > 0; )
435 i += (int)STRLEN(fmt_pat[--round].pattern);
436#ifdef COLON_IN_FILENAME
437 i += 12; /* "%f" can become twelve chars longer */
438#else
439 i += 2; /* "%f" can become two chars longer */
440#endif
441 if ((fmtstr = alloc(i)) == NULL)
442 goto parse_efm_error;
443
444 while (efm[0] != NUL)
445 {
446 /*
447 * Allocate a new eformat structure and put it at the end of the list
448 */
449 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
450 if (fmt_ptr == NULL)
451 goto parse_efm_error;
452 if (fmt_first == NULL) /* first one */
453 fmt_first = fmt_ptr;
454 else
455 fmt_last->next = fmt_ptr;
456 fmt_last = fmt_ptr;
457
458 /*
459 * Isolate one part in the 'errorformat' option
460 */
461 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
462 if (efm[len] == '\\' && efm[len + 1] != NUL)
463 ++len;
464
465 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
466 goto parse_efm_error;
467 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
468 goto parse_efm_error;
469 /*
470 * Advance to next part
471 */
472 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
473 }
474
475 if (fmt_first == NULL) /* nothing found */
476 EMSG(_("E378: 'errorformat' contains no pattern"));
477
478 goto parse_efm_end;
479
480parse_efm_error:
481 free_efm_list(&fmt_first);
482
483parse_efm_end:
484 vim_free(fmtstr);
485 vim_free(errmsg);
486
487 return fmt_first;
488}
489
Bram Moolenaare0d37972016-07-15 22:36:01 +0200490enum {
491 QF_FAIL = 0,
492 QF_OK = 1,
493 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200494 QF_NOMEM = 3,
495 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200496};
497
498typedef struct {
499 char_u *linebuf;
500 int linelen;
501 char_u *growbuf;
502 int growbufsiz;
503 FILE *fd;
504 typval_T *tv;
505 char_u *p_str;
506 listitem_T *p_li;
507 buf_T *buf;
508 linenr_T buflnum;
509 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100510 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200511} qfstate_T;
512
513 static char_u *
514qf_grow_linebuf(qfstate_T *state, int newsz)
515{
516 /*
517 * If the line exceeds LINE_MAXLEN exclude the last
518 * byte since it's not a NL character.
519 */
520 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
521 if (state->growbuf == NULL)
522 {
523 state->growbuf = alloc(state->linelen + 1);
524 if (state->growbuf == NULL)
525 return NULL;
526 state->growbufsiz = state->linelen;
527 }
528 else if (state->linelen > state->growbufsiz)
529 {
530 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
531 if (state->growbuf == NULL)
532 return NULL;
533 state->growbufsiz = state->linelen;
534 }
535 return state->growbuf;
536}
537
538/*
539 * Get the next string (separated by newline) from state->p_str.
540 */
541 static int
542qf_get_next_str_line(qfstate_T *state)
543{
544 /* Get the next line from the supplied string */
545 char_u *p_str = state->p_str;
546 char_u *p;
547 int len;
548
549 if (*p_str == NUL) /* Reached the end of the string */
550 return QF_END_OF_INPUT;
551
552 p = vim_strchr(p_str, '\n');
553 if (p != NULL)
554 len = (int)(p - p_str) + 1;
555 else
556 len = (int)STRLEN(p_str);
557
558 if (len > IOSIZE - 2)
559 {
560 state->linebuf = qf_grow_linebuf(state, len);
561 if (state->linebuf == NULL)
562 return QF_NOMEM;
563 }
564 else
565 {
566 state->linebuf = IObuff;
567 state->linelen = len;
568 }
569 vim_strncpy(state->linebuf, p_str, state->linelen);
570
571 /*
572 * Increment using len in order to discard the rest of the
573 * line if it exceeds LINE_MAXLEN.
574 */
575 p_str += len;
576 state->p_str = p_str;
577
578 return QF_OK;
579}
580
581/*
582 * Get the next string from state->p_Li.
583 */
584 static int
585qf_get_next_list_line(qfstate_T *state)
586{
587 listitem_T *p_li = state->p_li;
588 int len;
589
590 while (p_li != NULL
591 && (p_li->li_tv.v_type != VAR_STRING
592 || p_li->li_tv.vval.v_string == NULL))
593 p_li = p_li->li_next; /* Skip non-string items */
594
595 if (p_li == NULL) /* End of the list */
596 {
597 state->p_li = NULL;
598 return QF_END_OF_INPUT;
599 }
600
601 len = (int)STRLEN(p_li->li_tv.vval.v_string);
602 if (len > IOSIZE - 2)
603 {
604 state->linebuf = qf_grow_linebuf(state, len);
605 if (state->linebuf == NULL)
606 return QF_NOMEM;
607 }
608 else
609 {
610 state->linebuf = IObuff;
611 state->linelen = len;
612 }
613
614 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
615
616 state->p_li = p_li->li_next; /* next item */
617 return QF_OK;
618}
619
620/*
621 * Get the next string from state->buf.
622 */
623 static int
624qf_get_next_buf_line(qfstate_T *state)
625{
626 char_u *p_buf = NULL;
627 int len;
628
629 /* Get the next line from the supplied buffer */
630 if (state->buflnum > state->lnumlast)
631 return QF_END_OF_INPUT;
632
633 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
634 state->buflnum += 1;
635
636 len = (int)STRLEN(p_buf);
637 if (len > IOSIZE - 2)
638 {
639 state->linebuf = qf_grow_linebuf(state, len);
640 if (state->linebuf == NULL)
641 return QF_NOMEM;
642 }
643 else
644 {
645 state->linebuf = IObuff;
646 state->linelen = len;
647 }
648 vim_strncpy(state->linebuf, p_buf, state->linelen);
649
650 return QF_OK;
651}
652
653/*
654 * Get the next string from file state->fd.
655 */
656 static int
657qf_get_next_file_line(qfstate_T *state)
658{
659 int discard;
660 int growbuflen;
661
662 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
663 return QF_END_OF_INPUT;
664
665 discard = FALSE;
666 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200667 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200668 {
669 /*
670 * The current line exceeds IObuff, continue reading using
671 * growbuf until EOL or LINE_MAXLEN bytes is read.
672 */
673 if (state->growbuf == NULL)
674 {
675 state->growbufsiz = 2 * (IOSIZE - 1);
676 state->growbuf = alloc(state->growbufsiz);
677 if (state->growbuf == NULL)
678 return QF_NOMEM;
679 }
680
681 /* Copy the read part of the line, excluding null-terminator */
682 memcpy(state->growbuf, IObuff, IOSIZE - 1);
683 growbuflen = state->linelen;
684
685 for (;;)
686 {
687 if (fgets((char *)state->growbuf + growbuflen,
688 state->growbufsiz - growbuflen, state->fd) == NULL)
689 break;
690 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
691 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200692 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200693 break;
694 if (state->growbufsiz == LINE_MAXLEN)
695 {
696 discard = TRUE;
697 break;
698 }
699
700 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
701 ? 2 * state->growbufsiz : LINE_MAXLEN;
702 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
703 if (state->growbuf == NULL)
704 return QF_NOMEM;
705 }
706
707 while (discard)
708 {
709 /*
710 * The current line is longer than LINE_MAXLEN, continue
711 * reading but discard everything until EOL or EOF is
712 * reached.
713 */
714 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
715 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200716 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200717 break;
718 }
719
720 state->linebuf = state->growbuf;
721 state->linelen = growbuflen;
722 }
723 else
724 state->linebuf = IObuff;
725
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100726#ifdef FEAT_MBYTE
727 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200728 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
729 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100730 char_u *line;
731
732 line = string_convert(&state->vc, state->linebuf, &state->linelen);
733 if (line != NULL)
734 {
735 if (state->linelen < IOSIZE)
736 {
737 STRCPY(state->linebuf, line);
738 vim_free(line);
739 }
740 else
741 {
742 vim_free(state->growbuf);
743 state->linebuf = state->growbuf = line;
744 state->growbufsiz = state->linelen < LINE_MAXLEN
745 ? state->linelen : LINE_MAXLEN;
746 }
747 }
748 }
749#endif
750
Bram Moolenaare0d37972016-07-15 22:36:01 +0200751 return QF_OK;
752}
753
754/*
755 * Get the next string from a file/buffer/list/string.
756 */
757 static int
758qf_get_nextline(qfstate_T *state)
759{
760 int status = QF_FAIL;
761
762 if (state->fd == NULL)
763 {
764 if (state->tv != NULL)
765 {
766 if (state->tv->v_type == VAR_STRING)
767 /* Get the next line from the supplied string */
768 status = qf_get_next_str_line(state);
769 else if (state->tv->v_type == VAR_LIST)
770 /* Get the next line from the supplied list */
771 status = qf_get_next_list_line(state);
772 }
773 else
774 /* Get the next line from the supplied buffer */
775 status = qf_get_next_buf_line(state);
776 }
777 else
778 /* Get the next line from the supplied file */
779 status = qf_get_next_file_line(state);
780
781 if (status != QF_OK)
782 return status;
783
784 /* remove newline/CR from the line */
785 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200786 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200787 state->linebuf[state->linelen - 1] = NUL;
788#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200789 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
790 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200791#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200792 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200793
794#ifdef FEAT_MBYTE
795 remove_bom(state->linebuf);
796#endif
797
798 return QF_OK;
799}
800
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200801typedef struct {
802 char_u *namebuf;
803 char_u *errmsg;
804 int errmsglen;
805 long lnum;
806 int col;
807 char_u use_viscol;
808 char_u *pattern;
809 int enr;
810 int type;
811 int valid;
812} qffields_T;
813
814/*
815 * Parse a line and get the quickfix fields.
816 * Return the QF_ status.
817 */
818 static int
819qf_parse_line(
820 qf_info_T *qi,
821 char_u *linebuf,
822 int linelen,
823 efm_T *fmt_first,
824 qffields_T *fields)
825{
826 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200827 char_u *ptr;
828 int len;
829 int i;
830 int idx = 0;
831 char_u *tail = NULL;
832 regmatch_T regmatch;
833
834 /* Always ignore case when looking for a matching error. */
835 regmatch.rm_ic = TRUE;
836
837 /* If there was no %> item start at the first pattern */
838 if (fmt_start == NULL)
839 fmt_ptr = fmt_first;
840 else
841 {
842 fmt_ptr = fmt_start;
843 fmt_start = NULL;
844 }
845
846 /*
847 * Try to match each part of 'errorformat' until we find a complete
848 * match or no match.
849 */
850 fields->valid = TRUE;
851restofline:
852 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
853 {
854 int r;
855
856 idx = fmt_ptr->prefix;
857 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
858 continue;
859 fields->namebuf[0] = NUL;
860 fields->pattern[0] = NUL;
861 if (!qi->qf_multiscan)
862 fields->errmsg[0] = NUL;
863 fields->lnum = 0;
864 fields->col = 0;
865 fields->use_viscol = FALSE;
866 fields->enr = -1;
867 fields->type = 0;
868 tail = NULL;
869
870 regmatch.regprog = fmt_ptr->prog;
871 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
872 fmt_ptr->prog = regmatch.regprog;
873 if (r)
874 {
875 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline)
876 continue;
877 if (vim_strchr((char_u *)"EWI", idx) != NULL)
878 fields->type = idx;
879 else
880 fields->type = 0;
881 /*
882 * Extract error message data from matched line.
883 * We check for an actual submatch, because "\[" and "\]" in
884 * the 'errorformat' may cause the wrong submatch to be used.
885 */
886 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
887 {
888 int c;
889
890 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
891 continue;
892
893 /* Expand ~/file and $HOME/file to full path. */
894 c = *regmatch.endp[i];
895 *regmatch.endp[i] = NUL;
896 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
897 *regmatch.endp[i] = c;
898
899 if (vim_strchr((char_u *)"OPQ", idx) != NULL
900 && mch_getperm(fields->namebuf) == -1)
901 continue;
902 }
903 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
904 {
905 if (regmatch.startp[i] == NULL)
906 continue;
907 fields->enr = (int)atol((char *)regmatch.startp[i]);
908 }
909 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
910 {
911 if (regmatch.startp[i] == NULL)
912 continue;
913 fields->lnum = atol((char *)regmatch.startp[i]);
914 }
915 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
916 {
917 if (regmatch.startp[i] == NULL)
918 continue;
919 fields->col = (int)atol((char *)regmatch.startp[i]);
920 }
921 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
922 {
923 if (regmatch.startp[i] == NULL)
924 continue;
925 fields->type = *regmatch.startp[i];
926 }
927 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */
928 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200929 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200930 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200931 /* linelen + null terminator */
932 if ((fields->errmsg = vim_realloc(fields->errmsg,
933 linelen + 1)) == NULL)
934 return QF_NOMEM;
935 fields->errmsglen = linelen + 1;
936 }
937 vim_strncpy(fields->errmsg, linebuf, linelen);
938 }
939 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
940 {
941 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
942 continue;
943 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200944 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200945 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200946 /* len + null terminator */
947 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
948 == NULL)
949 return QF_NOMEM;
950 fields->errmsglen = len + 1;
951 }
952 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
953 }
954 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
955 {
956 if (regmatch.startp[i] == NULL)
957 continue;
958 tail = regmatch.startp[i];
959 }
960 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
961 {
962 char_u *match_ptr;
963
964 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
965 continue;
966 fields->col = 0;
967 for (match_ptr = regmatch.startp[i];
968 match_ptr != regmatch.endp[i]; ++match_ptr)
969 {
970 ++fields->col;
971 if (*match_ptr == TAB)
972 {
973 fields->col += 7;
974 fields->col -= fields->col % 8;
975 }
976 }
977 ++fields->col;
978 fields->use_viscol = TRUE;
979 }
980 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
981 {
982 if (regmatch.startp[i] == NULL)
983 continue;
984 fields->col = (int)atol((char *)regmatch.startp[i]);
985 fields->use_viscol = TRUE;
986 }
987 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
988 {
989 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
990 continue;
991 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
992 if (len > CMDBUFFSIZE - 5)
993 len = CMDBUFFSIZE - 5;
994 STRCPY(fields->pattern, "^\\V");
995 STRNCAT(fields->pattern, regmatch.startp[i], len);
996 fields->pattern[len + 3] = '\\';
997 fields->pattern[len + 4] = '$';
998 fields->pattern[len + 5] = NUL;
999 }
1000 break;
1001 }
1002 }
1003 qi->qf_multiscan = FALSE;
1004
1005 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1006 {
1007 if (fmt_ptr != NULL)
1008 {
1009 if (idx == 'D') /* enter directory */
1010 {
1011 if (*fields->namebuf == NUL)
1012 {
1013 EMSG(_("E379: Missing or empty directory name"));
1014 return QF_FAIL;
1015 }
1016 qi->qf_directory =
1017 qf_push_dir(fields->namebuf, &qi->qf_dir_stack, FALSE);
1018 if (qi->qf_directory == NULL)
1019 return QF_FAIL;
1020 }
1021 else if (idx == 'X') /* leave directory */
1022 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack);
1023 }
1024 fields->namebuf[0] = NUL; /* no match found, remove file name */
1025 fields->lnum = 0; /* don't jump to this line */
1026 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001027 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001028 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001029 /* linelen + null terminator */
1030 if ((fields->errmsg = vim_realloc(fields->errmsg,
1031 linelen + 1)) == NULL)
1032 return QF_NOMEM;
1033 fields->errmsglen = linelen + 1;
1034 }
1035 /* copy whole line to error message */
1036 vim_strncpy(fields->errmsg, linebuf, linelen);
1037 if (fmt_ptr == NULL)
1038 qi->qf_multiline = qi->qf_multiignore = FALSE;
1039 }
1040 else if (fmt_ptr != NULL)
1041 {
1042 /* honor %> item */
1043 if (fmt_ptr->conthere)
1044 fmt_start = fmt_ptr;
1045
1046 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1047 {
1048 qi->qf_multiline = TRUE; /* start of a multi-line message */
1049 qi->qf_multiignore = FALSE; /* reset continuation */
1050 }
1051 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1052 { /* continuation of multi-line msg */
Bram Moolenaar9b457942016-10-09 16:10:05 +02001053 if (!qi->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001054 {
Bram Moolenaar9b457942016-10-09 16:10:05 +02001055 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001056
Bram Moolenaar9b457942016-10-09 16:10:05 +02001057 if (qfprev == NULL)
1058 return QF_FAIL;
1059 if (*fields->errmsg && !qi->qf_multiignore)
1060 {
1061 len = (int)STRLEN(qfprev->qf_text);
1062 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1063 == NULL)
1064 return QF_FAIL;
1065 STRCPY(ptr, qfprev->qf_text);
1066 vim_free(qfprev->qf_text);
1067 qfprev->qf_text = ptr;
1068 *(ptr += len) = '\n';
1069 STRCPY(++ptr, fields->errmsg);
1070 }
1071 if (qfprev->qf_nr == -1)
1072 qfprev->qf_nr = fields->enr;
1073 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1074 /* only printable chars allowed */
1075 qfprev->qf_type = fields->type;
1076
1077 if (!qfprev->qf_lnum)
1078 qfprev->qf_lnum = fields->lnum;
1079 if (!qfprev->qf_col)
1080 qfprev->qf_col = fields->col;
1081 qfprev->qf_viscol = fields->use_viscol;
1082 if (!qfprev->qf_fnum)
1083 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory,
1084 *fields->namebuf || qi->qf_directory != NULL
1085 ? fields->namebuf
1086 : qi->qf_currfile != NULL && fields->valid
1087 ? qi->qf_currfile : 0);
1088 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001089 if (idx == 'Z')
1090 qi->qf_multiline = qi->qf_multiignore = FALSE;
1091 line_breakcheck();
1092 return QF_IGNORE_LINE;
1093 }
1094 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1095 {
1096 /* global file names */
1097 fields->valid = FALSE;
1098 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1099 {
1100 if (*fields->namebuf && idx == 'P')
1101 qi->qf_currfile =
1102 qf_push_dir(fields->namebuf, &qi->qf_file_stack, TRUE);
1103 else if (idx == 'Q')
1104 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack);
1105 *fields->namebuf = NUL;
1106 if (tail && *tail)
1107 {
1108 STRMOVE(IObuff, skipwhite(tail));
1109 qi->qf_multiscan = TRUE;
1110 goto restofline;
1111 }
1112 }
1113 }
1114 if (fmt_ptr->flags == '-') /* generally exclude this line */
1115 {
1116 if (qi->qf_multiline)
1117 /* also exclude continuation lines */
1118 qi->qf_multiignore = TRUE;
1119 return QF_IGNORE_LINE;
1120 }
1121 }
1122
1123 return QF_OK;
1124}
1125
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001126/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001127 * Read the errorfile "efile" into memory, line by line, building the error
1128 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001129 * Alternative: when "efile" is NULL read errors from buffer "buf".
1130 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001131 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001132 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1133 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001134 * Return -1 for error, number of errors for success.
1135 */
1136 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001137qf_init_ext(
1138 qf_info_T *qi,
1139 char_u *efile,
1140 buf_T *buf,
1141 typval_T *tv,
1142 char_u *errorformat,
1143 int newlist, /* TRUE: start a new error list */
1144 linenr_T lnumfirst, /* first line number to use */
1145 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001146 char_u *qf_title,
1147 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001148{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001149 qfstate_T state;
1150 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001151#ifdef FEAT_WINDOWS
1152 qfline_T *old_last = NULL;
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001153 int adding = FALSE;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001154#endif
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001155 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001157 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001159 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001161 /* Do not used the cached buffer, it may have been wiped out. */
1162 vim_free(qf_last_bufname);
1163 qf_last_bufname = NULL;
1164
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001165 vim_memset(&state, 0, sizeof(state));
1166 vim_memset(&fields, 0, sizeof(fields));
1167#ifdef FEAT_MBYTE
1168 state.vc.vc_type = CONV_NONE;
1169 if (enc != NULL && *enc != NUL)
1170 convert_setup(&state.vc, enc, p_enc);
1171#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001172 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1173 fields.errmsglen = CMDBUFFSIZE + 1;
1174 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1175 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1176 if (fields.namebuf == NULL || fields.errmsg == NULL ||
1177 fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 goto qf_init_end;
1179
Bram Moolenaare0d37972016-07-15 22:36:01 +02001180 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
1182 EMSG2(_(e_openerrf), efile);
1183 goto qf_init_end;
1184 }
1185
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001186 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001188 qf_new_list(qi, qf_title);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001189#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001190 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar864293a2016-06-02 13:40:04 +02001191 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001192 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001193 adding = TRUE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001194 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001195 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001199 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001200 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 else
1202 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001204 /*
1205 * If we are not adding or adding to another list: clear the state.
1206 */
1207 if (newlist || qi->qf_curlist != qi->qf_dir_curlist)
1208 {
1209 qi->qf_dir_curlist = qi->qf_curlist;
1210 qf_clean_dir_stack(&qi->qf_dir_stack);
1211 qi->qf_directory = NULL;
1212 qf_clean_dir_stack(&qi->qf_file_stack);
1213 qi->qf_currfile = NULL;
1214 qi->qf_multiline = FALSE;
1215 qi->qf_multiignore = FALSE;
1216 qi->qf_multiscan = FALSE;
1217 }
1218
1219 /*
1220 * If the errorformat didn't change between calls, then reuse the
1221 * previously parsed values.
1222 */
1223 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1224 {
1225 /* free the previously parsed data */
1226 vim_free(last_efm);
1227 last_efm = NULL;
1228 free_efm_list(&fmt_first);
1229
1230 /* parse the current 'efm' */
1231 fmt_first = parse_efm_option(efm);
1232 if (fmt_first != NULL)
1233 last_efm = vim_strsave(efm);
1234 }
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
1239 /*
1240 * got_int is reset here, because it was probably set when killing the
1241 * ":make" command, but we still want to read the errorfile then.
1242 */
1243 got_int = FALSE;
1244
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001245 if (tv != NULL)
1246 {
1247 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001248 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001249 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001250 state.p_li = tv->vval.v_list->lv_first;
1251 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001252 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001253 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001254 state.buflnum = lnumfirst;
1255 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 /*
1258 * Read the lines in the error file one by one.
1259 * Try to recognize one of the error formats in each line.
1260 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001261 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001263 /* Get the next line from a file/buffer/list/string */
1264 status = qf_get_nextline(&state);
1265 if (status == QF_NOMEM) /* memory alloc failure */
1266 goto qf_init_end;
1267 if (status == QF_END_OF_INPUT) /* end of input */
1268 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001270 status = qf_parse_line(qi, state.linebuf, state.linelen, fmt_first,
1271 &fields);
1272 if (status == QF_FAIL)
1273 goto error2;
1274 if (status == QF_NOMEM)
1275 goto qf_init_end;
1276 if (status == QF_IGNORE_LINE)
1277 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001279 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001280 qi->qf_curlist,
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001281 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001282 (*fields.namebuf || qi->qf_directory != NULL)
1283 ? fields.namebuf
1284 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001285 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001286 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001287 fields.errmsg,
1288 fields.lnum,
1289 fields.col,
1290 fields.use_viscol,
1291 fields.pattern,
1292 fields.enr,
1293 fields.type,
1294 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 goto error2;
1296 line_breakcheck();
1297 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001298 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001300 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001302 /* no valid entry found */
1303 qi->qf_lists[qi->qf_curlist].qf_ptr =
1304 qi->qf_lists[qi->qf_curlist].qf_start;
1305 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1306 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308 else
1309 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001310 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1311 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1312 qi->qf_lists[qi->qf_curlist].qf_ptr =
1313 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001315 /* return number of matches */
1316 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001317 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319 EMSG(_(e_readerrf));
1320error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001321 if (!adding)
1322 {
1323 qf_free(qi, qi->qf_curlist);
1324 qi->qf_listcount--;
1325 if (qi->qf_curlist > 0)
1326 --qi->qf_curlist;
1327 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001328qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001329 if (state.fd != NULL)
1330 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001331 vim_free(fields.namebuf);
1332 vim_free(fields.errmsg);
1333 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001334 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001337 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001339#ifdef FEAT_MBYTE
1340 if (state.vc.vc_type != CONV_NONE)
1341 convert_setup(&state.vc, NULL, NULL);
1342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
1344 return retval;
1345}
1346
Bram Moolenaarfb604092014-07-23 15:55:00 +02001347 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001348qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001349{
1350 if (title != NULL)
1351 {
1352 char_u *p = alloc((int)STRLEN(title) + 2);
1353
Bram Moolenaara3921f42017-06-04 15:30:34 +02001354 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001355 if (p != NULL)
1356 sprintf((char *)p, ":%s", (char *)title);
1357 }
1358}
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360/*
1361 * Prepare for adding a new quickfix list.
1362 */
1363 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001364qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 int i;
1367
1368 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001369 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 * the current entry. This makes it possible to browse in a tree-like
1371 * way with ":grep'.
1372 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001373 while (qi->qf_listcount > qi->qf_curlist + 1)
1374 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
1376 /*
1377 * When the stack is full, remove to oldest entry
1378 * Otherwise, add a new entry.
1379 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001380 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001382 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001384 qi->qf_lists[i - 1] = qi->qf_lists[i];
1385 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001388 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001389 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001390 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391}
1392
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001393/*
1394 * Free a location list
1395 */
1396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001397ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001398{
1399 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001400 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001401
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001402 qi = *pqi;
1403 if (qi == NULL)
1404 return;
1405 *pqi = NULL; /* Remove reference to this list */
1406
1407 qi->qf_refcount--;
1408 if (qi->qf_refcount < 1)
1409 {
1410 /* No references to this location list */
1411 for (i = 0; i < qi->qf_listcount; ++i)
1412 qf_free(qi, i);
1413 vim_free(qi);
1414 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001415}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001416
1417 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001418qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001419{
1420 int i;
1421 qf_info_T *qi = &ql_info;
1422
1423 if (wp != NULL)
1424 {
1425 /* location list */
1426 ll_free_all(&wp->w_llist);
1427 ll_free_all(&wp->w_llist_ref);
1428 }
1429 else
1430 /* quickfix list */
1431 for (i = 0; i < qi->qf_listcount; ++i)
1432 qf_free(qi, i);
1433}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001434
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435/*
1436 * Add an entry to the end of the list of errors.
1437 * Returns OK or FAIL.
1438 */
1439 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001440qf_add_entry(
1441 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001442 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001443 char_u *dir, /* optional directory name */
1444 char_u *fname, /* file name or NULL */
1445 int bufnum, /* buffer number or zero */
1446 char_u *mesg, /* message */
1447 long lnum, /* line number */
1448 int col, /* column */
1449 int vis_col, /* using visual column */
1450 char_u *pattern, /* search pattern */
1451 int nr, /* error number */
1452 int type, /* type character */
1453 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001455 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001456 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001458 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001460 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001461 {
1462 buf_T *buf = buflist_findnr(bufnum);
1463
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001464 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001465 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001466 buf->b_has_qf_entry |=
1467 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001468 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001469 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001470 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1472 {
1473 vim_free(qfp);
1474 return FAIL;
1475 }
1476 qfp->qf_lnum = lnum;
1477 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001478 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001479 if (pattern == NULL || *pattern == NUL)
1480 qfp->qf_pattern = NULL;
1481 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1482 {
1483 vim_free(qfp->qf_text);
1484 vim_free(qfp);
1485 return FAIL;
1486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 qfp->qf_nr = nr;
1488 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1489 type = 0;
1490 qfp->qf_type = type;
1491 qfp->qf_valid = valid;
1492
Bram Moolenaara3921f42017-06-04 15:30:34 +02001493 lastp = &qi->qf_lists[qf_idx].qf_last;
1494 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001495 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001497 qi->qf_lists[qf_idx].qf_start = qfp;
1498 qi->qf_lists[qf_idx].qf_ptr = qfp;
1499 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001500 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502 else
1503 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001504 qfp->qf_prev = *lastp;
1505 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001507 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001509 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001510 ++qi->qf_lists[qf_idx].qf_count;
1511 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001512 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001514 qi->qf_lists[qf_idx].qf_index =
1515 qi->qf_lists[qf_idx].qf_count;
1516 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518
1519 return OK;
1520}
1521
1522/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001523 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001524 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001525 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001526ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001527{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001528 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001529
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001530 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1531 if (qi != NULL)
1532 {
1533 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1534 qi->qf_refcount++;
1535 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001536
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001537 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001538}
1539
1540/*
1541 * Return the location list for window 'wp'.
1542 * If not present, allocate a location list
1543 */
1544 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001545ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001546{
1547 if (IS_LL_WINDOW(wp))
1548 /* For a location list window, use the referenced location list */
1549 return wp->w_llist_ref;
1550
1551 /*
1552 * For a non-location list window, w_llist_ref should not point to a
1553 * location list.
1554 */
1555 ll_free_all(&wp->w_llist_ref);
1556
1557 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001558 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001559 return wp->w_llist;
1560}
1561
1562/*
1563 * Copy the location list from window "from" to window "to".
1564 */
1565 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001566copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001567{
1568 qf_info_T *qi;
1569 int idx;
1570 int i;
1571
1572 /*
1573 * When copying from a location list window, copy the referenced
1574 * location list. For other windows, copy the location list for
1575 * that window.
1576 */
1577 if (IS_LL_WINDOW(from))
1578 qi = from->w_llist_ref;
1579 else
1580 qi = from->w_llist;
1581
1582 if (qi == NULL) /* no location list to copy */
1583 return;
1584
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001585 /* allocate a new location list */
1586 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001587 return;
1588
1589 to->w_llist->qf_listcount = qi->qf_listcount;
1590
1591 /* Copy the location lists one at a time */
1592 for (idx = 0; idx < qi->qf_listcount; idx++)
1593 {
1594 qf_list_T *from_qfl;
1595 qf_list_T *to_qfl;
1596
1597 to->w_llist->qf_curlist = idx;
1598
1599 from_qfl = &qi->qf_lists[idx];
1600 to_qfl = &to->w_llist->qf_lists[idx];
1601
1602 /* Some of the fields are populated by qf_add_entry() */
1603 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1604 to_qfl->qf_count = 0;
1605 to_qfl->qf_index = 0;
1606 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001607 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001608 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001609 if (from_qfl->qf_title != NULL)
1610 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1611 else
1612 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001613 if (from_qfl->qf_ctx != NULL)
1614 {
1615 to_qfl->qf_ctx = alloc_tv();
1616 if (to_qfl->qf_ctx != NULL)
1617 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1618 }
1619 else
1620 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001621
1622 if (from_qfl->qf_count)
1623 {
1624 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001625 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001626
1627 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001628 for (i = 0, from_qfp = from_qfl->qf_start;
1629 i < from_qfl->qf_count && from_qfp != NULL;
1630 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001632 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001633 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001634 NULL,
1635 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001636 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001637 from_qfp->qf_text,
1638 from_qfp->qf_lnum,
1639 from_qfp->qf_col,
1640 from_qfp->qf_viscol,
1641 from_qfp->qf_pattern,
1642 from_qfp->qf_nr,
1643 0,
1644 from_qfp->qf_valid) == FAIL)
1645 {
1646 qf_free_all(to);
1647 return;
1648 }
1649 /*
1650 * qf_add_entry() will not set the qf_num field, as the
1651 * directory and file names are not supplied. So the qf_fnum
1652 * field is copied here.
1653 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001654 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001655 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1656 prevp->qf_type = from_qfp->qf_type; /* error type */
1657 if (from_qfl->qf_ptr == from_qfp)
1658 to_qfl->qf_ptr = prevp; /* current location */
1659 }
1660 }
1661
1662 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1663
1664 /* When no valid entries are present in the list, qf_ptr points to
1665 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001666 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001667 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001668 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001669 to_qfl->qf_index = 1;
1670 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001671 }
1672
1673 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1674}
1675
1676/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001677 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001678 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 */
1680 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001681qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
Bram Moolenaar82404332016-07-10 17:00:38 +02001683 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001684 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001685 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001686
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 if (fname == NULL || *fname == NUL) /* no file name */
1688 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689
Bram Moolenaare60acc12011-05-10 16:41:25 +02001690#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001691 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001692#endif
1693#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001694 if (directory != NULL)
1695 slash_adjust(directory);
1696 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001697#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001698 if (directory != NULL && !vim_isAbsName(fname)
1699 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1700 {
1701 /*
1702 * Here we check if the file really exists.
1703 * This should normally be true, but if make works without
1704 * "leaving directory"-messages we might have missed a
1705 * directory change.
1706 */
1707 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001710 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001711 if (directory)
1712 ptr = concat_fnames(directory, fname, TRUE);
1713 else
1714 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001716 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001717 bufname = ptr;
1718 }
1719 else
1720 bufname = fname;
1721
1722 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001723 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001724 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001725 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001726 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001728 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001729 {
1730 vim_free(qf_last_bufname);
1731 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1732 if (bufname == ptr)
1733 qf_last_bufname = bufname;
1734 else
1735 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001736 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001737 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001738 if (buf == NULL)
1739 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001740
Bram Moolenaarc1542742016-07-20 21:44:37 +02001741 buf->b_has_qf_entry =
1742 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001743 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744}
1745
1746/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001747 * Push dirbuf onto the directory stack and return pointer to actual dir or
1748 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 */
1750 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001751qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752{
1753 struct dir_stack_T *ds_new;
1754 struct dir_stack_T *ds_ptr;
1755
1756 /* allocate new stack element and hook it in */
1757 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1758 if (ds_new == NULL)
1759 return NULL;
1760
1761 ds_new->next = *stackptr;
1762 *stackptr = ds_new;
1763
1764 /* store directory on the stack */
1765 if (vim_isAbsName(dirbuf)
1766 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001767 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 (*stackptr)->dirname = vim_strsave(dirbuf);
1769 else
1770 {
1771 /* Okay we don't have an absolute path.
1772 * dirbuf must be a subdir of one of the directories on the stack.
1773 * Let's search...
1774 */
1775 ds_new = (*stackptr)->next;
1776 (*stackptr)->dirname = NULL;
1777 while (ds_new)
1778 {
1779 vim_free((*stackptr)->dirname);
1780 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1781 TRUE);
1782 if (mch_isdir((*stackptr)->dirname) == TRUE)
1783 break;
1784
1785 ds_new = ds_new->next;
1786 }
1787
1788 /* clean up all dirs we already left */
1789 while ((*stackptr)->next != ds_new)
1790 {
1791 ds_ptr = (*stackptr)->next;
1792 (*stackptr)->next = (*stackptr)->next->next;
1793 vim_free(ds_ptr->dirname);
1794 vim_free(ds_ptr);
1795 }
1796
1797 /* Nothing found -> it must be on top level */
1798 if (ds_new == NULL)
1799 {
1800 vim_free((*stackptr)->dirname);
1801 (*stackptr)->dirname = vim_strsave(dirbuf);
1802 }
1803 }
1804
1805 if ((*stackptr)->dirname != NULL)
1806 return (*stackptr)->dirname;
1807 else
1808 {
1809 ds_ptr = *stackptr;
1810 *stackptr = (*stackptr)->next;
1811 vim_free(ds_ptr);
1812 return NULL;
1813 }
1814}
1815
1816
1817/*
1818 * pop dirbuf from the directory stack and return previous directory or NULL if
1819 * stack is empty
1820 */
1821 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001822qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 struct dir_stack_T *ds_ptr;
1825
1826 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1827 * What to do if it isn't? */
1828
1829 /* pop top element and free it */
1830 if (*stackptr != NULL)
1831 {
1832 ds_ptr = *stackptr;
1833 *stackptr = (*stackptr)->next;
1834 vim_free(ds_ptr->dirname);
1835 vim_free(ds_ptr);
1836 }
1837
1838 /* return NEW top element as current dir or NULL if stack is empty*/
1839 return *stackptr ? (*stackptr)->dirname : NULL;
1840}
1841
1842/*
1843 * clean up directory stack
1844 */
1845 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001846qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
1848 struct dir_stack_T *ds_ptr;
1849
1850 while ((ds_ptr = *stackptr) != NULL)
1851 {
1852 *stackptr = (*stackptr)->next;
1853 vim_free(ds_ptr->dirname);
1854 vim_free(ds_ptr);
1855 }
1856}
1857
1858/*
1859 * Check in which directory of the directory stack the given file can be
1860 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001861 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 * Cleans up intermediate directory entries.
1863 *
1864 * TODO: How to solve the following problem?
1865 * If we have the this directory tree:
1866 * ./
1867 * ./aa
1868 * ./aa/bb
1869 * ./bb
1870 * ./bb/x.c
1871 * and make says:
1872 * making all in aa
1873 * making all in bb
1874 * x.c:9: Error
1875 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1876 * qf_guess_filepath will return NULL.
1877 */
1878 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001879qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
1881 struct dir_stack_T *ds_ptr;
1882 struct dir_stack_T *ds_tmp;
1883 char_u *fullname;
1884
1885 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001886 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 return NULL;
1888
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001889 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 fullname = NULL;
1891 while (ds_ptr)
1892 {
1893 vim_free(fullname);
1894 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1895
1896 /* If concat_fnames failed, just go on. The worst thing that can happen
1897 * is that we delete the entire stack.
1898 */
1899 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1900 break;
1901
1902 ds_ptr = ds_ptr->next;
1903 }
1904
1905 vim_free(fullname);
1906
1907 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001908 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001910 ds_tmp = qi->qf_dir_stack->next;
1911 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 vim_free(ds_tmp->dirname);
1913 vim_free(ds_tmp);
1914 }
1915
1916 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917}
1918
1919/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001920 * When loading a file from the quickfix, the auto commands may modify it.
1921 * This may invalidate the current quickfix entry. This function checks
1922 * whether a entry is still present in the quickfix.
1923 * Similar to location list.
1924 */
1925 static int
1926is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1927{
1928 qf_list_T *qfl;
1929 qfline_T *qfp;
1930 int i;
1931
1932 qfl = &qi->qf_lists[qi->qf_curlist];
1933
1934 /* Search for the entry in the current list */
1935 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1936 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001937 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001938 break;
1939
1940 if (i == qfl->qf_count) /* Entry is not found */
1941 return FALSE;
1942
1943 return TRUE;
1944}
1945
1946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 * jump to a quickfix line
1948 * if dir == FORWARD go "errornr" valid entries forward
1949 * if dir == BACKWARD go "errornr" valid entries backward
1950 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1951 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1952 * else if "errornr" is zero, redisplay the same line
1953 * else go to entry "errornr"
1954 */
1955 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001956qf_jump(
1957 qf_info_T *qi,
1958 int dir,
1959 int errornr,
1960 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001962 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001963 qfline_T *qf_ptr;
1964 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 int qf_index;
1966 int old_qf_fnum;
1967 int old_qf_index;
1968 int prev_index;
1969 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1970 char_u *err = e_no_more_items;
1971 linenr_T i;
1972 buf_T *old_curbuf;
1973 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 colnr_T screen_col;
1975 colnr_T char_col;
1976 char_u *line;
1977#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001978 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001979 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 int opened_window = FALSE;
1981 win_T *win;
1982 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001983 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001985 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 int print_message = TRUE;
1987 int len;
1988#ifdef FEAT_FOLDING
1989 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1990#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001991 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001992 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001994 if (qi == NULL)
1995 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001996
1997 if (qi->qf_curlist >= qi->qf_listcount
1998 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
2000 EMSG(_(e_quickfix));
2001 return;
2002 }
2003
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002004 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002006 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 old_qf_index = qf_index;
2008 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
2009 {
2010 while (errornr--)
2011 {
2012 old_qf_ptr = qf_ptr;
2013 prev_index = qf_index;
2014 old_qf_fnum = qf_ptr->qf_fnum;
2015 do
2016 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 || qf_ptr->qf_next == NULL)
2019 {
2020 qf_ptr = old_qf_ptr;
2021 qf_index = prev_index;
2022 if (err != NULL)
2023 {
2024 EMSG(_(err));
2025 goto theend;
2026 }
2027 errornr = 0;
2028 break;
2029 }
2030 ++qf_index;
2031 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002032 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2033 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2035 err = NULL;
2036 }
2037 }
2038 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
2039 {
2040 while (errornr--)
2041 {
2042 old_qf_ptr = qf_ptr;
2043 prev_index = qf_index;
2044 old_qf_fnum = qf_ptr->qf_fnum;
2045 do
2046 {
2047 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
2048 {
2049 qf_ptr = old_qf_ptr;
2050 qf_index = prev_index;
2051 if (err != NULL)
2052 {
2053 EMSG(_(err));
2054 goto theend;
2055 }
2056 errornr = 0;
2057 break;
2058 }
2059 --qf_index;
2060 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002061 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2062 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2064 err = NULL;
2065 }
2066 }
2067 else if (errornr != 0) /* go to specified number */
2068 {
2069 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2070 {
2071 --qf_index;
2072 qf_ptr = qf_ptr->qf_prev;
2073 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002074 while (errornr > qf_index && qf_index <
2075 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 && qf_ptr->qf_next != NULL)
2077 {
2078 ++qf_index;
2079 qf_ptr = qf_ptr->qf_next;
2080 }
2081 }
2082
2083#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002084 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2085 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 /* No need to print the error message if it's visible in the error
2087 * window */
2088 print_message = FALSE;
2089
2090 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002091 * For ":helpgrep" find a help window or open one.
2092 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002093 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002094 {
2095 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002096
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002097 if (cmdmod.tab != 0)
2098 wp = NULL;
2099 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002100 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002101 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2102 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002103 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2104 win_enter(wp, TRUE);
2105 else
2106 {
2107 /*
2108 * Split off help window; put it at far top if no position
2109 * specified, the current window is vertically split and narrow.
2110 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002111 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002112 if (cmdmod.split == 0 && curwin->w_width != Columns
2113 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002114 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002115 if (qi != &ql_info)
2116 flags |= WSP_NEWLOC; /* don't copy the location list */
2117
2118 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002119 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002120 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002121
2122 if (curwin->w_height < p_hh)
2123 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002124
2125 if (qi != &ql_info) /* not a quickfix list */
2126 {
2127 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002128 curwin->w_llist = qi;
2129 qi->qf_refcount++;
2130 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002131 }
2132
2133 if (!p_im)
2134 restart_edit = 0; /* don't want insert mode in help file */
2135 }
2136
2137 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 * If currently in the quickfix window, find another window to show the
2139 * file in.
2140 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002141 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002143 win_T *usable_win_ptr = NULL;
2144
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 /*
2146 * If there is no file specified, we don't know where to go.
2147 * But do advance, otherwise ":cn" gets stuck.
2148 */
2149 if (qf_ptr->qf_fnum == 0)
2150 goto theend;
2151
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002153
2154 ll_ref = curwin->w_llist_ref;
2155 if (ll_ref != NULL)
2156 {
2157 /* Find a window using the same location list that is not a
2158 * quickfix window. */
2159 FOR_ALL_WINDOWS(usable_win_ptr)
2160 if (usable_win_ptr->w_llist == ll_ref
2161 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002162 {
2163 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002164 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002165 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002166 }
2167
2168 if (!usable_win)
2169 {
2170 /* Locate a window showing a normal buffer */
2171 FOR_ALL_WINDOWS(win)
2172 if (win->w_buffer->b_p_bt[0] == NUL)
2173 {
2174 usable_win = 1;
2175 break;
2176 }
2177 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002180 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002181 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002183 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002184 {
2185 tabpage_T *tp;
2186 win_T *wp;
2187
2188 FOR_ALL_TAB_WINDOWS(tp, wp)
2189 {
2190 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2191 {
2192 goto_tabpage_win(tp, wp);
2193 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002194 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002195 }
2196 }
2197 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002198win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002199
2200 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002201 * If there is only one window and it is the quickfix window, create a
2202 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002203 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002204 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002206 flags = WSP_ABOVE;
2207 if (ll_ref != NULL)
2208 flags |= WSP_NEWLOC;
2209 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 goto failed; /* not enough room for window */
2211 opened_window = TRUE; /* close it when fail */
2212 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002213 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002214 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002215 if (ll_ref != NULL)
2216 {
2217 /* The new window should use the location list from the
2218 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002219 curwin->w_llist = ll_ref;
2220 ll_ref->qf_refcount++;
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223 else
2224 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002225 if (curwin->w_llist_ref != NULL)
2226 {
2227 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002228 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002229 if (win == NULL)
2230 {
2231 /* Find the window showing the selected file */
2232 FOR_ALL_WINDOWS(win)
2233 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2234 break;
2235 if (win == NULL)
2236 {
2237 /* Find a previous usable window */
2238 win = curwin;
2239 do
2240 {
2241 if (win->w_buffer->b_p_bt[0] == NUL)
2242 break;
2243 if (win->w_prev == NULL)
2244 win = lastwin; /* wrap around the top */
2245 else
2246 win = win->w_prev; /* go to previous window */
2247 } while (win != curwin);
2248 }
2249 }
2250 win_goto(win);
2251
2252 /* If the location list for the window is not set, then set it
2253 * to the location list from the location window */
2254 if (win->w_llist == NULL)
2255 {
2256 win->w_llist = ll_ref;
2257 ll_ref->qf_refcount++;
2258 }
2259 }
2260 else
2261 {
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 /*
2264 * Try to find a window that shows the right buffer.
2265 * Default to the window just above the quickfix buffer.
2266 */
2267 win = curwin;
2268 altwin = NULL;
2269 for (;;)
2270 {
2271 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2272 break;
2273 if (win->w_prev == NULL)
2274 win = lastwin; /* wrap around the top */
2275 else
2276 win = win->w_prev; /* go to previous window */
2277
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002278 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
2280 /* Didn't find it, go to the window before the quickfix
2281 * window. */
2282 if (altwin != NULL)
2283 win = altwin;
2284 else if (curwin->w_prev != NULL)
2285 win = curwin->w_prev;
2286 else
2287 win = curwin->w_next;
2288 break;
2289 }
2290
2291 /* Remember a usable window. */
2292 if (altwin == NULL && !win->w_p_pvw
2293 && win->w_buffer->b_p_bt[0] == NUL)
2294 altwin = win;
2295 }
2296
2297 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 }
2300 }
2301#endif
2302
2303 /*
2304 * If there is a file name,
2305 * read the wanted file if needed, and check autowrite etc.
2306 */
2307 old_curbuf = curbuf;
2308 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002309
2310 if (qf_ptr->qf_fnum != 0)
2311 {
2312 if (qf_ptr->qf_type == 1)
2313 {
2314 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2315 * set b_p_ro flag). */
2316 if (!can_abandon(curbuf, forceit))
2317 {
2318 EMSG(_(e_nowrtmsg));
2319 ok = FALSE;
2320 }
2321 else
2322 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002323 ECMD_HIDE + ECMD_SET_HELP,
2324 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002325 }
2326 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002327 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002328 int old_qf_curlist = qi->qf_curlist;
2329 int is_abort = FALSE;
2330
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002331 ok = buflist_getfile(qf_ptr->qf_fnum,
2332 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002333 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002334 {
2335 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002336 is_abort = TRUE;
2337 opened_window = FALSE;
2338 }
2339 else if (old_qf_curlist != qi->qf_curlist
2340 || !is_qf_entry_present(qi, qf_ptr))
2341 {
2342 if (qi == &ql_info)
2343 EMSG(_("E925: Current quickfix was changed"));
2344 else
2345 EMSG(_("E926: Current location list was changed"));
2346 is_abort = TRUE;
2347 }
2348
2349 if (is_abort)
2350 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002351 ok = FALSE;
2352 qi = NULL;
2353 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002354 }
2355 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002356 }
2357
2358 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 /* When not switched to another buffer, still need to set pc mark */
2361 if (curbuf == old_curbuf)
2362 setpcmark();
2363
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002364 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002366 /*
2367 * Go to line with error, unless qf_lnum is 0.
2368 */
2369 i = qf_ptr->qf_lnum;
2370 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002372 if (i > curbuf->b_ml.ml_line_count)
2373 i = curbuf->b_ml.ml_line_count;
2374 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002376 if (qf_ptr->qf_col > 0)
2377 {
2378 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002379#ifdef FEAT_VIRTUALEDIT
2380 curwin->w_cursor.coladd = 0;
2381#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002382 if (qf_ptr->qf_viscol == TRUE)
2383 {
2384 /*
2385 * Check each character from the beginning of the error
2386 * line up to the error column. For each tab character
2387 * found, reduce the error column value by the length of
2388 * a tab character.
2389 */
2390 line = ml_get_curline();
2391 screen_col = 0;
2392 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2393 {
2394 if (*line == NUL)
2395 break;
2396 if (*line++ == '\t')
2397 {
2398 curwin->w_cursor.col -= 7 - (screen_col % 8);
2399 screen_col += 8 - (screen_col % 8);
2400 }
2401 else
2402 ++screen_col;
2403 }
2404 }
2405 check_cursor();
2406 }
2407 else
2408 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002411 {
2412 pos_T save_cursor;
2413
2414 /* Move the cursor to the first line in the buffer */
2415 save_cursor = curwin->w_cursor;
2416 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002417 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2418 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002419 curwin->w_cursor = save_cursor;
2420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422#ifdef FEAT_FOLDING
2423 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2424 foldOpenCursor();
2425#endif
2426 if (print_message)
2427 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002428 /* Update the screen before showing the message, unless the screen
2429 * scrolled up. */
2430 if (!msg_scrolled)
2431 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2435 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2436 /* Add the message, skipping leading whitespace and newlines. */
2437 len = (int)STRLEN(IObuff);
2438 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2439
2440 /* Output the message. Overwrite to avoid scrolling when the 'O'
2441 * flag is present in 'shortmess'; But when not jumping, print the
2442 * whole message. */
2443 i = msg_scroll;
2444 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2445 msg_scroll = TRUE;
2446 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2447 msg_scroll = FALSE;
2448 msg_attr_keep(IObuff, 0, TRUE);
2449 msg_scroll = i;
2450 }
2451 }
2452 else
2453 {
2454#ifdef FEAT_WINDOWS
2455 if (opened_window)
2456 win_close(curwin, TRUE); /* Close opened window */
2457#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002458 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
2460 /*
2461 * Couldn't open file, so put index back where it was. This could
2462 * happen if the file was readonly and we changed something.
2463 */
2464#ifdef FEAT_WINDOWS
2465failed:
2466#endif
2467 qf_ptr = old_qf_ptr;
2468 qf_index = old_qf_index;
2469 }
2470 }
2471theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002472 if (qi != NULL)
2473 {
2474 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2475 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477#ifdef FEAT_WINDOWS
2478 if (p_swb != old_swb && opened_window)
2479 {
2480 /* Restore old 'switchbuf' value, but not when an autocommand or
2481 * modeline has changed the value. */
2482 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002485 swb_flags = old_swb_flags;
2486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 else
2488 free_string_option(old_swb);
2489 }
2490#endif
2491}
2492
2493/*
2494 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002495 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 */
2497 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002498qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002500 buf_T *buf;
2501 char_u *fname;
2502 qfline_T *qfp;
2503 int i;
2504 int idx1 = 1;
2505 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002506 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002507 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002508 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002510 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002512 if (eap->cmdidx == CMD_llist)
2513 {
2514 qi = GET_LOC_LIST(curwin);
2515 if (qi == NULL)
2516 {
2517 EMSG(_(e_loclist));
2518 return;
2519 }
2520 }
2521
2522 if (qi->qf_curlist >= qi->qf_listcount
2523 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
2525 EMSG(_(e_quickfix));
2526 return;
2527 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002528 if (*arg == '+')
2529 {
2530 ++arg;
2531 plus = TRUE;
2532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2534 {
2535 EMSG(_(e_trailing));
2536 return;
2537 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002538 if (plus)
2539 {
2540 i = qi->qf_lists[qi->qf_curlist].qf_index;
2541 idx2 = i + idx1;
2542 idx1 = i;
2543 }
2544 else
2545 {
2546 i = qi->qf_lists[qi->qf_curlist].qf_count;
2547 if (idx1 < 0)
2548 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2549 if (idx2 < 0)
2550 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002553 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002555 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2556 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2559 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002560 msg_putchar('\n');
2561 if (got_int)
2562 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002563
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002564 fname = NULL;
2565 if (qfp->qf_fnum != 0
2566 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2567 {
2568 fname = buf->b_fname;
2569 if (qfp->qf_type == 1) /* :helpgrep */
2570 fname = gettail(fname);
2571 }
2572 if (fname == NULL)
2573 sprintf((char *)IObuff, "%2d", i);
2574 else
2575 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2576 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002577 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar8820b482017-03-16 17:23:31 +01002578 ? HL_ATTR(HLF_L) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002579 if (qfp->qf_lnum == 0)
2580 IObuff[0] = NUL;
2581 else if (qfp->qf_col == 0)
2582 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2583 else
2584 sprintf((char *)IObuff, ":%ld col %d",
2585 qfp->qf_lnum, qfp->qf_col);
2586 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2587 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002588 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002589 if (qfp->qf_pattern != NULL)
2590 {
2591 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2592 STRCAT(IObuff, ":");
2593 msg_puts(IObuff);
2594 }
2595 msg_puts((char_u *)" ");
2596
2597 /* Remove newlines and leading whitespace from the text. For an
2598 * unrecognized line keep the indent, the compiler may mark a word
2599 * with ^^^^. */
2600 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2602 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002603 msg_prt_line(IObuff, FALSE);
2604 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606
2607 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002608 if (qfp == NULL)
2609 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002610 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 ui_breakcheck();
2612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613}
2614
2615/*
2616 * Remove newlines and leading whitespace from an error message.
2617 * Put the result in "buf[bufsize]".
2618 */
2619 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002620qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
2622 int i;
2623 char_u *p = text;
2624
2625 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2626 {
2627 if (*p == '\n')
2628 {
2629 buf[i] = ' ';
2630 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002631 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 break;
2633 }
2634 else
2635 buf[i] = *p++;
2636 }
2637 buf[i] = NUL;
2638}
2639
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002640 static void
2641qf_msg(qf_info_T *qi, int which, char *lead)
2642{
2643 char *title = (char *)qi->qf_lists[which].qf_title;
2644 int count = qi->qf_lists[which].qf_count;
2645 char_u buf[IOSIZE];
2646
2647 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2648 lead,
2649 which + 1,
2650 qi->qf_listcount,
2651 count);
2652
2653 if (title != NULL)
2654 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002655 size_t len = STRLEN(buf);
2656
2657 if (len < 34)
2658 {
2659 vim_memset(buf + len, ' ', 34 - len);
2660 buf[34] = NUL;
2661 }
2662 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002663 }
2664 trunc_string(buf, buf, Columns - 1, IOSIZE);
2665 msg(buf);
2666}
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668/*
2669 * ":colder [count]": Up in the quickfix stack.
2670 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002671 * ":lolder [count]": Up in the location list stack.
2672 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 */
2674 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002675qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002677 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 int count;
2679
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002680 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2681 {
2682 qi = GET_LOC_LIST(curwin);
2683 if (qi == NULL)
2684 {
2685 EMSG(_(e_loclist));
2686 return;
2687 }
2688 }
2689
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (eap->addr_count != 0)
2691 count = eap->line2;
2692 else
2693 count = 1;
2694 while (count--)
2695 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002696 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002698 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
2700 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002701 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002703 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 }
2705 else
2706 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002707 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
2709 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002712 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002715 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002717 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718#endif
2719}
2720
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002721 void
2722qf_history(exarg_T *eap)
2723{
2724 qf_info_T *qi = &ql_info;
2725 int i;
2726
2727 if (eap->cmdidx == CMD_lhistory)
2728 qi = GET_LOC_LIST(curwin);
2729 if (qi == NULL || (qi->qf_listcount == 0
2730 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2731 MSG(_("No entries"));
2732 else
2733 for (i = 0; i < qi->qf_listcount; ++i)
2734 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2735}
2736
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002738 * Free all the entries in the error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 */
2740 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002741qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002743 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002744 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002745 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002747 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002749 qfp = qi->qf_lists[idx].qf_start;
2750 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002751 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002752 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002753 vim_free(qfp->qf_text);
2754 stop = (qfp == qfpnext);
2755 vim_free(qfp->qf_pattern);
2756 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002757 if (stop)
2758 /* Somehow qf_count may have an incorrect value, set it to 1
2759 * to avoid crashing when it's wrong.
2760 * TODO: Avoid qf_count being incorrect. */
2761 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002762 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002763 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002764 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002766 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002767 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002768 free_tv(qi->qf_lists[idx].qf_ctx);
2769 qi->qf_lists[idx].qf_ctx = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002770 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002771 qi->qf_lists[idx].qf_start = NULL;
Bram Moolenaar31bdd132017-04-15 15:22:52 +02002772 qi->qf_lists[idx].qf_last = NULL;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002773 qi->qf_lists[idx].qf_ptr = NULL;
2774 qi->qf_lists[idx].qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002775
2776 qf_clean_dir_stack(&qi->qf_dir_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002777 qi->qf_directory = NULL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002778 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002779 qi->qf_currfile = NULL;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002780 qi->qf_multiline = FALSE;
2781 qi->qf_multiignore = FALSE;
2782 qi->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783}
2784
2785/*
2786 * qf_mark_adjust: adjust marks
2787 */
2788 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002789qf_mark_adjust(
2790 win_T *wp,
2791 linenr_T line1,
2792 linenr_T line2,
2793 long amount,
2794 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002796 int i;
2797 qfline_T *qfp;
2798 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002799 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002800 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002801 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
Bram Moolenaarc1542742016-07-20 21:44:37 +02002803 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002804 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002805 if (wp != NULL)
2806 {
2807 if (wp->w_llist == NULL)
2808 return;
2809 qi = wp->w_llist;
2810 }
2811
2812 for (idx = 0; idx < qi->qf_listcount; ++idx)
2813 if (qi->qf_lists[idx].qf_count)
2814 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002815 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2816 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if (qfp->qf_fnum == curbuf->b_fnum)
2818 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002819 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2821 {
2822 if (amount == MAXLNUM)
2823 qfp->qf_cleared = TRUE;
2824 else
2825 qfp->qf_lnum += amount;
2826 }
2827 else if (amount_after && qfp->qf_lnum > line2)
2828 qfp->qf_lnum += amount_after;
2829 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002830
2831 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002832 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833}
2834
2835/*
2836 * Make a nice message out of the error character and the error number:
2837 * char number message
2838 * e or E 0 " error"
2839 * w or W 0 " warning"
2840 * i or I 0 " info"
2841 * 0 0 ""
2842 * other 0 " c"
2843 * e or E n " error n"
2844 * w or W n " warning n"
2845 * i or I n " info n"
2846 * 0 n " error n"
2847 * other n " c n"
2848 * 1 x "" :helpgrep
2849 */
2850 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002851qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 static char_u buf[20];
2854 static char_u cc[3];
2855 char_u *p;
2856
2857 if (c == 'W' || c == 'w')
2858 p = (char_u *)" warning";
2859 else if (c == 'I' || c == 'i')
2860 p = (char_u *)" info";
2861 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2862 p = (char_u *)" error";
2863 else if (c == 0 || c == 1)
2864 p = (char_u *)"";
2865 else
2866 {
2867 cc[0] = ' ';
2868 cc[1] = c;
2869 cc[2] = NUL;
2870 p = cc;
2871 }
2872
2873 if (nr <= 0)
2874 return p;
2875
2876 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2877 return buf;
2878}
2879
2880#if defined(FEAT_WINDOWS) || defined(PROTO)
2881/*
2882 * ":cwindow": open the quickfix window if we have errors to display,
2883 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002884 * ":lwindow": open the location list window if we have locations to display,
2885 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 */
2887 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002888ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 win_T *win;
2892
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002893 if (eap->cmdidx == CMD_lwindow)
2894 {
2895 qi = GET_LOC_LIST(curwin);
2896 if (qi == NULL)
2897 return;
2898 }
2899
2900 /* Look for an existing quickfix window. */
2901 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
2903 /*
2904 * If a quickfix window is open but we have no errors to display,
2905 * close the window. If a quickfix window is not open, then open
2906 * it if we have errors; otherwise, leave it closed.
2907 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002908 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002909 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002910 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
2912 if (win != NULL)
2913 ex_cclose(eap);
2914 }
2915 else if (win == NULL)
2916 ex_copen(eap);
2917}
2918
2919/*
2920 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002921 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002924ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002926 win_T *win = NULL;
2927 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002929 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2930 {
2931 qi = GET_LOC_LIST(curwin);
2932 if (qi == NULL)
2933 return;
2934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002936 /* Find existing quickfix window and close it. */
2937 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (win != NULL)
2939 win_close(win, FALSE);
2940}
2941
2942/*
2943 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002944 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 */
2946 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002947ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002949 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002952 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002953 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002954 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002956 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2957 {
2958 qi = GET_LOC_LIST(curwin);
2959 if (qi == NULL)
2960 {
2961 EMSG(_(e_loclist));
2962 return;
2963 }
2964 }
2965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 if (eap->addr_count != 0)
2967 height = eap->line2;
2968 else
2969 height = QF_WINHEIGHT;
2970
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972#ifdef FEAT_GUI
2973 need_mouse_correct = TRUE;
2974#endif
2975
2976 /*
2977 * Find existing quickfix window, or open a new one.
2978 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002979 win = qf_find_win(qi);
2980
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002981 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002984 if (eap->addr_count != 0)
2985 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002986 if (cmdmod.split & WSP_VERT)
2987 {
2988 if (height != W_WIDTH(win))
2989 win_setwidth(height);
2990 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002991 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002992 win_setheight(height);
2993 }
2994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 else
2996 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002997 qf_buf = qf_find_buf(qi);
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 /* The current window becomes the previous window afterwards. */
3000 win = curwin;
3001
Bram Moolenaar77642c02012-11-20 17:55:10 +01003002 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3003 && cmdmod.split == 0)
3004 /* Create the new window at the very bottom, except when
3005 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003006 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003007 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003009 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003011 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003013 /*
3014 * For the location list window, create a reference to the
3015 * location list from the window 'win'.
3016 */
3017 curwin->w_llist_ref = win->w_llist;
3018 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003020
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003021 if (oldwin != curwin)
3022 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003023 if (qf_buf != NULL)
3024 /* Use the existing quickfix buffer */
3025 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003026 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003027 else
3028 {
3029 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003030 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003031 /* switch off 'swapfile' */
3032 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3033 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003034 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003035 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003036 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003037#ifdef FEAT_DIFF
3038 curwin->w_p_diff = FALSE;
3039#endif
3040#ifdef FEAT_FOLDING
3041 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3042 OPT_LOCAL);
3043#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003046 /* Only set the height when still in the same tab page and there is no
3047 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003048 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 win_setheight(height);
3050 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3051 if (win_valid(win))
3052 prevwin = win;
3053 }
3054
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003055 qf_set_title_var(qi);
3056
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 /*
3058 * Fill the buffer with the quickfix list.
3059 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003060 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003062 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 curwin->w_cursor.col = 0;
3064 check_cursor();
3065 update_topline(); /* scroll to show the line */
3066}
3067
3068/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003069 * Move the cursor in the quickfix window to "lnum".
3070 */
3071 static void
3072qf_win_goto(win_T *win, linenr_T lnum)
3073{
3074 win_T *old_curwin = curwin;
3075
3076 curwin = win;
3077 curbuf = win->w_buffer;
3078 curwin->w_cursor.lnum = lnum;
3079 curwin->w_cursor.col = 0;
3080#ifdef FEAT_VIRTUALEDIT
3081 curwin->w_cursor.coladd = 0;
3082#endif
3083 curwin->w_curswant = 0;
3084 update_topline(); /* scroll to show the line */
3085 redraw_later(VALID);
3086 curwin->w_redr_status = TRUE; /* update ruler */
3087 curwin = old_curwin;
3088 curbuf = curwin->w_buffer;
3089}
3090
3091/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003092 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003093 */
3094 void
3095ex_cbottom(exarg_T *eap UNUSED)
3096{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003097 qf_info_T *qi = &ql_info;
3098 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003099
Bram Moolenaar537ef082016-07-09 17:56:19 +02003100 if (eap->cmdidx == CMD_lbottom)
3101 {
3102 qi = GET_LOC_LIST(curwin);
3103 if (qi == NULL)
3104 {
3105 EMSG(_(e_loclist));
3106 return;
3107 }
3108 }
3109
3110 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003111 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3112 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3113}
3114
3115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 * Return the number of the current entry (line number in the quickfix
3117 * window).
3118 */
3119 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003120qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003122 qf_info_T *qi = &ql_info;
3123
3124 if (IS_LL_WINDOW(wp))
3125 /* In the location list window, use the referenced location list */
3126 qi = wp->w_llist_ref;
3127
3128 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129}
3130
3131/*
3132 * Update the cursor position in the quickfix window to the current error.
3133 * Return TRUE if there is a quickfix window.
3134 */
3135 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003136qf_win_pos_update(
3137 qf_info_T *qi,
3138 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003141 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
3143 /*
3144 * Put the cursor on the current error in the quickfix window, so that
3145 * it's viewable.
3146 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003147 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (win != NULL
3149 && qf_index <= win->w_buffer->b_ml.ml_line_count
3150 && old_qf_index != qf_index)
3151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 if (qf_index > old_qf_index)
3153 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003154 win->w_redraw_top = old_qf_index;
3155 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
3157 else
3158 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003159 win->w_redraw_top = qf_index;
3160 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003162 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
3164 return win != NULL;
3165}
3166
3167/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003168 * Check whether the given window is displaying the specified quickfix/location
3169 * list buffer
3170 */
3171 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003172is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003173{
3174 /*
3175 * A window displaying the quickfix buffer will have the w_llist_ref field
3176 * set to NULL.
3177 * A window displaying a location list buffer will have the w_llist_ref
3178 * pointing to the location list.
3179 */
3180 if (bt_quickfix(win->w_buffer))
3181 if ((qi == &ql_info && win->w_llist_ref == NULL)
3182 || (qi != &ql_info && win->w_llist_ref == qi))
3183 return TRUE;
3184
3185 return FALSE;
3186}
3187
3188/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003189 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003190 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003191 */
3192 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003193qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003194{
3195 win_T *win;
3196
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003197 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003198 if (is_qf_win(win, qi))
3199 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003200
3201 return win;
3202}
3203
3204/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003205 * Find a quickfix buffer.
3206 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 */
3208 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003209qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003211 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003212 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaar9c102382006-05-03 21:26:49 +00003214 FOR_ALL_TAB_WINDOWS(tp, win)
3215 if (is_qf_win(win, qi))
3216 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003217
Bram Moolenaar9c102382006-05-03 21:26:49 +00003218 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219}
3220
3221/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003222 * Update the w:quickfix_title variable in the quickfix/location list window
3223 */
3224 static void
3225qf_update_win_titlevar(qf_info_T *qi)
3226{
3227 win_T *win;
3228 win_T *curwin_save;
3229
3230 if ((win = qf_find_win(qi)) != NULL)
3231 {
3232 curwin_save = curwin;
3233 curwin = win;
3234 qf_set_title_var(qi);
3235 curwin = curwin_save;
3236 }
3237}
3238
3239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * Find the quickfix buffer. If it exists, update the contents.
3241 */
3242 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003243qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
3245 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003246 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
3249 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003250 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (buf != NULL)
3252 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003253 linenr_T old_line_count = buf->b_ml.ml_line_count;
3254
3255 if (old_last == NULL)
3256 /* set curwin/curbuf to buf and save a few things */
3257 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaard823fa92016-08-12 16:29:27 +02003259 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003260
Bram Moolenaar864293a2016-06-02 13:40:04 +02003261 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003262
Bram Moolenaar864293a2016-06-02 13:40:04 +02003263 if (old_last == NULL)
3264 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003265 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003266
3267 /* restore curwin/curbuf and a few other things */
3268 aucmd_restbuf(&aco);
3269 }
3270
3271 /* Only redraw when added lines are visible. This avoids flickering
3272 * when the added lines are not visible. */
3273 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3274 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276}
3277
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003278/*
3279 * Set "w:quickfix_title" if "qi" has a title.
3280 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003281 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003282qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003283{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003284 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3285 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003286 qi->qf_lists[qi->qf_curlist].qf_title);
3287}
3288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289/*
3290 * Fill current buffer with quickfix errors, replacing any previous contents.
3291 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003292 * If "old_last" is not NULL append the items after this one.
3293 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3294 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 */
3296 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003297qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003299 linenr_T lnum;
3300 qfline_T *qfp;
3301 buf_T *errbuf;
3302 int len;
3303 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
Bram Moolenaar864293a2016-06-02 13:40:04 +02003305 if (old_last == NULL)
3306 {
3307 if (buf != curbuf)
3308 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003309 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003310 return;
3311 }
3312
3313 /* delete all existing lines */
3314 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3315 (void)ml_delete((linenr_T)1, FALSE);
3316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
3318 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003319 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
3321 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003322 if (old_last == NULL)
3323 {
3324 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3325 lnum = 0;
3326 }
3327 else
3328 {
3329 qfp = old_last->qf_next;
3330 lnum = buf->b_ml.ml_line_count;
3331 }
3332 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
3334 if (qfp->qf_fnum != 0
3335 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3336 && errbuf->b_fname != NULL)
3337 {
3338 if (qfp->qf_type == 1) /* :helpgrep */
3339 STRCPY(IObuff, gettail(errbuf->b_fname));
3340 else
3341 STRCPY(IObuff, errbuf->b_fname);
3342 len = (int)STRLEN(IObuff);
3343 }
3344 else
3345 len = 0;
3346 IObuff[len++] = '|';
3347
3348 if (qfp->qf_lnum > 0)
3349 {
3350 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3351 len += (int)STRLEN(IObuff + len);
3352
3353 if (qfp->qf_col > 0)
3354 {
3355 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3356 len += (int)STRLEN(IObuff + len);
3357 }
3358
3359 sprintf((char *)IObuff + len, "%s",
3360 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3361 len += (int)STRLEN(IObuff + len);
3362 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003363 else if (qfp->qf_pattern != NULL)
3364 {
3365 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3366 len += (int)STRLEN(IObuff + len);
3367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 IObuff[len++] = '|';
3369 IObuff[len++] = ' ';
3370
3371 /* Remove newlines and leading whitespace from the text.
3372 * For an unrecognized line keep the indent, the compiler may
3373 * mark a word with ^^^^. */
3374 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3375 IObuff + len, IOSIZE - len);
3376
Bram Moolenaar864293a2016-06-02 13:40:04 +02003377 if (ml_append_buf(buf, lnum, IObuff,
3378 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003380 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003382 if (qfp == NULL)
3383 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003385
3386 if (old_last == NULL)
3387 /* Delete the empty line which is now at the end */
3388 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390
3391 /* correct cursor position */
3392 check_lnums(TRUE);
3393
Bram Moolenaar864293a2016-06-02 13:40:04 +02003394 if (old_last == NULL)
3395 {
3396 /* Set the 'filetype' to "qf" each time after filling the buffer.
3397 * This resembles reading a file into a buffer, it's more logical when
3398 * using autocommands. */
3399 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3400 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
3402#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003403 keep_filetype = TRUE; /* don't detect 'filetype' */
3404 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003406 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003408 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003410 /* make sure it will be redrawn */
3411 redraw_curbuf_later(NOT_VALID);
3412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
3414 /* Restore KeyTyped, setting 'filetype' may reset it. */
3415 KeyTyped = old_KeyTyped;
3416}
3417
3418#endif /* FEAT_WINDOWS */
3419
3420/*
3421 * Return TRUE if "buf" is the quickfix buffer.
3422 */
3423 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003424bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003426 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427}
3428
3429/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003430 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3431 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
3433 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003434bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003436 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3437 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438}
3439
3440/*
3441 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3442 */
3443 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003444bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003446 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447}
3448
3449 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003450bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451{
3452 if (bt_dontwrite(buf))
3453 {
3454 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3455 return TRUE;
3456 }
3457 return FALSE;
3458}
3459
3460/*
3461 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3462 * and 'bufhidden'.
3463 */
3464 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003465buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3468 switch (buf->b_p_bh[0])
3469 {
3470 case 'u': /* "unload" */
3471 case 'w': /* "wipe" */
3472 case 'd': return FALSE; /* "delete" */
3473 case 'h': return TRUE; /* "hide" */
3474 }
3475 return (p_hid || cmdmod.hide);
3476}
3477
3478/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003479 * Return TRUE when using ":vimgrep" for ":grep".
3480 */
3481 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003482grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003483{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003484 return ((cmdidx == CMD_grep
3485 || cmdidx == CMD_lgrep
3486 || cmdidx == CMD_grepadd
3487 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003488 && STRCMP("internal",
3489 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3490}
3491
3492/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003493 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 */
3495 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003496ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003498 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003500 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003502 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003503 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003504 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003505#ifdef FEAT_AUTOCMD
3506 char_u *au_name = NULL;
3507
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003508 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3509 if (grep_internal(eap->cmdidx))
3510 {
3511 ex_vimgrep(eap);
3512 return;
3513 }
3514
Bram Moolenaar7c626922005-02-07 22:01:03 +00003515 switch (eap->cmdidx)
3516 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003517 case CMD_make: au_name = (char_u *)"make"; break;
3518 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3519 case CMD_grep: au_name = (char_u *)"grep"; break;
3520 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3521 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3522 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003523 default: break;
3524 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003525 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3526 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003527 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003528# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003529 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003530 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003531# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003532 }
3533#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003534#ifdef FEAT_MBYTE
3535 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaara6557602006-02-04 22:43:20 +00003538 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3539 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003540 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003541
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003543 fname = get_mef_name();
3544 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003546 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
3548 /*
3549 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3550 */
3551 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3552 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003553 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 cmd = alloc(len);
3555 if (cmd == NULL)
3556 return;
3557 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3558 (char *)p_shq);
3559 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003560 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 /*
3562 * Output a newline if there's something else than the :make command that
3563 * was typed (in which case the cursor is in column 0).
3564 */
3565 if (msg_col == 0)
3566 msg_didout = FALSE;
3567 msg_start();
3568 MSG_PUTS(":!");
3569 msg_outtrans(cmd); /* show what we are doing */
3570
3571 /* let the shell know if we are redirecting output or not */
3572 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3573
3574#ifdef AMIGA
3575 out_flush();
3576 /* read window status report and redraw before message */
3577 (void)char_avail();
3578#endif
3579
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003580 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003581 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3582 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003583 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003584 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003585 if (wp != NULL)
3586 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003587#ifdef FEAT_AUTOCMD
3588 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003589 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003590 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3591 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003592 if (qi->qf_curlist < qi->qf_listcount)
3593 res = qi->qf_lists[qi->qf_curlist].qf_count;
3594 else
3595 res = 0;
3596 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003597#endif
3598 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003599 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
Bram Moolenaar7c626922005-02-07 22:01:03 +00003601 mch_remove(fname);
3602 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 vim_free(cmd);
3604}
3605
3606/*
3607 * Return the name for the errorfile, in allocated memory.
3608 * Find a new unique name when 'makeef' contains "##".
3609 * Returns NULL for error.
3610 */
3611 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003612get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613{
3614 char_u *p;
3615 char_u *name;
3616 static int start = -1;
3617 static int off = 0;
3618#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003619 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620#endif
3621
3622 if (*p_mef == NUL)
3623 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003624 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (name == NULL)
3626 EMSG(_(e_notmp));
3627 return name;
3628 }
3629
3630 for (p = p_mef; *p; ++p)
3631 if (p[0] == '#' && p[1] == '#')
3632 break;
3633
3634 if (*p == NUL)
3635 return vim_strsave(p_mef);
3636
3637 /* Keep trying until the name doesn't exist yet. */
3638 for (;;)
3639 {
3640 if (start == -1)
3641 start = mch_get_pid();
3642 else
3643 off += 19;
3644
3645 name = alloc((unsigned)STRLEN(p_mef) + 30);
3646 if (name == NULL)
3647 break;
3648 STRCPY(name, p_mef);
3649 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3650 STRCAT(name, p + 2);
3651 if (mch_getperm(name) < 0
3652#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003653 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 && mch_lstat((char *)name, &sb) < 0
3655#endif
3656 )
3657 break;
3658 vim_free(name);
3659 }
3660 return name;
3661}
3662
3663/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003664 * Returns the number of valid entries in the current quickfix/location list.
3665 */
3666 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003667qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003668{
3669 qf_info_T *qi = &ql_info;
3670 qfline_T *qfp;
3671 int i, sz = 0;
3672 int prev_fnum = 0;
3673
3674 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3675 {
3676 /* Location list */
3677 qi = GET_LOC_LIST(curwin);
3678 if (qi == NULL)
3679 return 0;
3680 }
3681
3682 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003683 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003684 ++i, qfp = qfp->qf_next)
3685 {
3686 if (qfp->qf_valid)
3687 {
3688 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3689 sz++; /* Count all valid entries */
3690 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3691 {
3692 /* Count the number of files */
3693 sz++;
3694 prev_fnum = qfp->qf_fnum;
3695 }
3696 }
3697 }
3698
3699 return sz;
3700}
3701
3702/*
3703 * Returns the current index of the quickfix/location list.
3704 * Returns 0 if there is an error.
3705 */
3706 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003707qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003708{
3709 qf_info_T *qi = &ql_info;
3710
3711 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3712 {
3713 /* Location list */
3714 qi = GET_LOC_LIST(curwin);
3715 if (qi == NULL)
3716 return 0;
3717 }
3718
3719 return qi->qf_lists[qi->qf_curlist].qf_index;
3720}
3721
3722/*
3723 * Returns the current index in the quickfix/location list (counting only valid
3724 * entries). If no valid entries are in the list, then returns 1.
3725 */
3726 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003727qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003728{
3729 qf_info_T *qi = &ql_info;
3730 qf_list_T *qfl;
3731 qfline_T *qfp;
3732 int i, eidx = 0;
3733 int prev_fnum = 0;
3734
3735 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3736 {
3737 /* Location list */
3738 qi = GET_LOC_LIST(curwin);
3739 if (qi == NULL)
3740 return 1;
3741 }
3742
3743 qfl = &qi->qf_lists[qi->qf_curlist];
3744 qfp = qfl->qf_start;
3745
3746 /* check if the list has valid errors */
3747 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3748 return 1;
3749
3750 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3751 {
3752 if (qfp->qf_valid)
3753 {
3754 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3755 {
3756 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3757 {
3758 /* Count the number of files */
3759 eidx++;
3760 prev_fnum = qfp->qf_fnum;
3761 }
3762 }
3763 else
3764 eidx++;
3765 }
3766 }
3767
3768 return eidx ? eidx : 1;
3769}
3770
3771/*
3772 * Get the 'n'th valid error entry in the quickfix or location list.
3773 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3774 * For :cdo and :ldo returns the 'n'th valid error entry.
3775 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3776 */
3777 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003778qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003779{
3780 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3781 qfline_T *qfp = qfl->qf_start;
3782 int i, eidx;
3783 int prev_fnum = 0;
3784
3785 /* check if the list has valid errors */
3786 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3787 return 1;
3788
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003789 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003790 i++, qfp = qfp->qf_next)
3791 {
3792 if (qfp->qf_valid)
3793 {
3794 if (fdo)
3795 {
3796 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3797 {
3798 /* Count the number of files */
3799 eidx++;
3800 prev_fnum = qfp->qf_fnum;
3801 }
3802 }
3803 else
3804 eidx++;
3805 }
3806
3807 if (eidx == n)
3808 break;
3809 }
3810
3811 if (i <= qfl->qf_count)
3812 return i;
3813 else
3814 return 1;
3815}
3816
3817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003819 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003820 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 */
3822 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003823ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003825 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003826 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003827
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003828 if (eap->cmdidx == CMD_ll
3829 || eap->cmdidx == CMD_lrewind
3830 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003831 || eap->cmdidx == CMD_llast
3832 || eap->cmdidx == CMD_ldo
3833 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003834 {
3835 qi = GET_LOC_LIST(curwin);
3836 if (qi == NULL)
3837 {
3838 EMSG(_(e_loclist));
3839 return;
3840 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003841 }
3842
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003843 if (eap->addr_count > 0)
3844 errornr = (int)eap->line2;
3845 else
3846 {
3847 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3848 errornr = 0;
3849 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3850 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3851 errornr = 1;
3852 else
3853 errornr = 32767;
3854 }
3855
3856 /* For cdo and ldo commands, jump to the nth valid error.
3857 * For cfdo and lfdo commands, jump to the nth valid file entry.
3858 */
3859 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3860 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3861 errornr = qf_get_nth_valid_entry(qi,
3862 eap->addr_count > 0 ? (int)eap->line1 : 1,
3863 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3864
3865 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866}
3867
3868/*
3869 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003870 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003871 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 */
3873 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003874ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003876 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003877 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003878
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003879 if (eap->cmdidx == CMD_lnext
3880 || eap->cmdidx == CMD_lNext
3881 || eap->cmdidx == CMD_lprevious
3882 || eap->cmdidx == CMD_lnfile
3883 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003884 || eap->cmdidx == CMD_lpfile
3885 || eap->cmdidx == CMD_ldo
3886 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003887 {
3888 qi = GET_LOC_LIST(curwin);
3889 if (qi == NULL)
3890 {
3891 EMSG(_(e_loclist));
3892 return;
3893 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003894 }
3895
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003896 if (eap->addr_count > 0 &&
3897 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3898 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3899 errornr = (int)eap->line2;
3900 else
3901 errornr = 1;
3902
3903 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3904 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003906 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3907 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003909 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3910 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 ? BACKWARD_FILE
3912 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003913 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914}
3915
3916/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003917 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003918 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 */
3920 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003921ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003923 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003924 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003925 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003926#ifdef FEAT_AUTOCMD
3927 char_u *au_name = NULL;
3928#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003929
3930 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003931 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003932 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003933
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003934#ifdef FEAT_AUTOCMD
3935 switch (eap->cmdidx)
3936 {
3937 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3938 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3939 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3940 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3941 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3942 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3943 default: break;
3944 }
3945 if (au_name != NULL)
3946 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3947#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003948#ifdef FEAT_MBYTE
3949 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3950#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003951#ifdef FEAT_BROWSE
3952 if (cmdmod.browse)
3953 {
3954 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3955 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3956 if (browse_file == NULL)
3957 return;
3958 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3959 vim_free(browse_file);
3960 }
3961 else
3962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003964 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003965
3966 /*
3967 * This function is used by the :cfile, :cgetfile and :caddfile
3968 * commands.
3969 * :cfile always creates a new quickfix list and jumps to the
3970 * first error.
3971 * :cgetfile creates a new quickfix list but doesn't jump to the
3972 * first error.
3973 * :caddfile adds to an existing quickfix list. If there is no
3974 * quickfix list then a new list is created.
3975 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003976 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003977 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003978 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003979 && (eap->cmdidx == CMD_cfile
3980 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003981 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003982#ifdef FEAT_AUTOCMD
3983 if (au_name != NULL)
3984 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3985#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003986 if (wp != NULL)
3987 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003988 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003989 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003990
3991 else
3992 {
3993#ifdef FEAT_AUTOCMD
3994 if (au_name != NULL)
3995 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3996#endif
3997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998}
3999
4000/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004001 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004002 * ":vimgrepadd {pattern} file(s)"
4003 * ":lvimgrep {pattern} file(s)"
4004 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004005 */
4006 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004007ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004008{
Bram Moolenaar81695252004-12-29 20:58:21 +00004009 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004010 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004011 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004012 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004013 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004014 char_u *s;
4015 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004016 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004017 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004018#ifdef FEAT_AUTOCMD
4019 qfline_T *cur_qf_start;
4020#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004021 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004022 buf_T *buf;
4023 int duplicate_name = FALSE;
4024 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004025 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004026 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004027 buf_T *first_match_buf = NULL;
4028 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004029 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004030#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4031 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004032#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004033 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004034 int flags = 0;
4035 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004036 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004037 char_u *dirname_start = NULL;
4038 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004039 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004040#ifdef FEAT_AUTOCMD
4041 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004042
4043 switch (eap->cmdidx)
4044 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004045 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4046 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4047 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004048 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004049 case CMD_grep: au_name = (char_u *)"grep"; break;
4050 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4051 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4052 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004053 default: break;
4054 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004055 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4056 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004057 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004058# ifdef FEAT_EVAL
4059 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004060 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004061# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004062 }
4063#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004064
Bram Moolenaar754b5602006-02-09 23:53:20 +00004065 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004066 || eap->cmdidx == CMD_lvimgrep
4067 || eap->cmdidx == CMD_lgrepadd
4068 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004069 {
4070 qi = ll_get_or_alloc_list(curwin);
4071 if (qi == NULL)
4072 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004073 }
4074
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004075 if (eap->addr_count > 0)
4076 tomatch = eap->line2;
4077 else
4078 tomatch = MAXLNUM;
4079
Bram Moolenaar81695252004-12-29 20:58:21 +00004080 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004081 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004082 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004083 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004084 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004085 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004086 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004087 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004088 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004089
4090 if (s != NULL && *s == NUL)
4091 {
4092 /* Pattern is empty, use last search pattern. */
4093 if (last_search_pat() == NULL)
4094 {
4095 EMSG(_(e_noprevre));
4096 goto theend;
4097 }
4098 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4099 }
4100 else
4101 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4102
Bram Moolenaar86b68352004-12-27 21:59:20 +00004103 if (regmatch.regprog == NULL)
4104 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004105 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004106 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004107
4108 p = skipwhite(p);
4109 if (*p == NUL)
4110 {
4111 EMSG(_("E683: File name missing or invalid pattern"));
4112 goto theend;
4113 }
4114
Bram Moolenaar754b5602006-02-09 23:53:20 +00004115 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004116 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004117 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004118 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004119 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004120
4121 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004122 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004123 goto theend;
4124 if (fcount == 0)
4125 {
4126 EMSG(_(e_nomatch));
4127 goto theend;
4128 }
4129
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004130 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4131 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004132 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004133 {
4134 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004135 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004136 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004137
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004138 /* Remember the current directory, because a BufRead autocommand that does
4139 * ":lcd %:p:h" changes the meaning of short path names. */
4140 mch_dirname(dirname_start, MAXPATHL);
4141
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004142#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004143 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004144 * changing the current quickfix list. */
4145 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4146#endif
4147
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004148 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004149 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004150 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004151 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004152 if (time(NULL) > seconds)
4153 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004154 /* Display the file name every second or so, show the user we are
4155 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004156 seconds = time(NULL);
4157 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004158 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004159 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004160 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004161 else
4162 {
4163 msg_outtrans(p);
4164 vim_free(p);
4165 }
4166 msg_clr_eos();
4167 msg_didout = FALSE; /* overwrite this message */
4168 msg_nowait = TRUE; /* don't wait for this message */
4169 msg_col = 0;
4170 out_flush();
4171 }
4172
Bram Moolenaar81695252004-12-29 20:58:21 +00004173 buf = buflist_findname_exp(fnames[fi]);
4174 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4175 {
4176 /* Remember that a buffer with this name already exists. */
4177 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004178 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004179 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004180
4181#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4182 /* Don't do Filetype autocommands to avoid loading syntax and
4183 * indent scripts, a great speed improvement. */
4184 save_ei = au_event_disable(",Filetype");
4185#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004186 /* Don't use modelines here, it's useless. */
4187 save_mls = p_mls;
4188 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004189
4190 /* Load file into a buffer, so that 'fileencoding' is detected,
4191 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004192 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004193
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004194 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004195#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4196 au_event_restore(save_ei);
4197#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004198 }
4199 else
4200 /* Use existing, loaded buffer. */
4201 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004202
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004203#ifdef FEAT_AUTOCMD
4204 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4205 {
4206 int idx;
4207
4208 /* Autocommands changed the quickfix list. Find the one we were
4209 * using and restore it. */
4210 for (idx = 0; idx < LISTCOUNT; ++idx)
4211 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4212 {
4213 qi->qf_curlist = idx;
4214 break;
4215 }
4216 if (idx == LISTCOUNT)
4217 {
4218 /* List cannot be found, create a new one. */
4219 qf_new_list(qi, *eap->cmdlinep);
4220 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4221 }
4222 }
4223#endif
4224
Bram Moolenaar81695252004-12-29 20:58:21 +00004225 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004226 {
4227 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004228 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004229 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004230 else
4231 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004232 /* Try for a match in all lines of the buffer.
4233 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004234 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004235 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4236 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004237 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004238 col = 0;
4239 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004240 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004241 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004242 /* Pass the buffer number so that it gets used even for a
4243 * dummy buffer, unless duplicate_name is set, then the
4244 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004245 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004246 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004247 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004248 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004249 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004250 ml_get_buf(buf,
4251 regmatch.startpos[0].lnum + lnum, FALSE),
4252 regmatch.startpos[0].lnum + lnum,
4253 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004254 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004255 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004256 0, /* nr */
4257 0, /* type */
4258 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004259 ) == FAIL)
4260 {
4261 got_int = TRUE;
4262 break;
4263 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004264 found_match = TRUE;
4265 if (--tomatch == 0)
4266 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004267 if ((flags & VGR_GLOBAL) == 0
4268 || regmatch.endpos[0].lnum > 0)
4269 break;
4270 col = regmatch.endpos[0].col
4271 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004272 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004273 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004274 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004275 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004276 if (got_int)
4277 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004278 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004279#ifdef FEAT_AUTOCMD
4280 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4281#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004282
4283 if (using_dummy)
4284 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004285 if (found_match && first_match_buf == NULL)
4286 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004287 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004288 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004289 /* Never keep a dummy buffer if there is another buffer
4290 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004291 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004292 buf = NULL;
4293 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004294 else if (!cmdmod.hide
4295 || buf->b_p_bh[0] == 'u' /* "unload" */
4296 || buf->b_p_bh[0] == 'w' /* "wipe" */
4297 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004298 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004299 /* When no match was found we don't need to remember the
4300 * buffer, wipe it out. If there was a match and it
4301 * wasn't the first one or we won't jump there: only
4302 * unload the buffer.
4303 * Ignore 'hidden' here, because it may lead to having too
4304 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004305 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004306 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004307 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004308 buf = NULL;
4309 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004310 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004311 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004312 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004313 /* Keeping the buffer, remove the dummy flag. */
4314 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004315 buf = NULL;
4316 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004317 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004318
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004319 if (buf != NULL)
4320 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004321 /* Keeping the buffer, remove the dummy flag. */
4322 buf->b_flags &= ~BF_DUMMY;
4323
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004324 /* If the buffer is still loaded we need to use the
4325 * directory we jumped to below. */
4326 if (buf == first_match_buf
4327 && target_dir == NULL
4328 && STRCMP(dirname_start, dirname_now) != 0)
4329 target_dir = vim_strsave(dirname_now);
4330
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004331 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004332 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004333 * need to be done (again). But not the window-local
4334 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004335 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004336#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004337 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4338 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004339#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004340 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004341 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004342 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004343 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004344 }
4345 }
4346
4347 FreeWild(fcount, fnames);
4348
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004349 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4350 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4351 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004352
4353#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004354 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004355#endif
4356
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004357#ifdef FEAT_AUTOCMD
4358 if (au_name != NULL)
4359 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4360 curbuf->b_fname, TRUE, curbuf);
4361#endif
4362
Bram Moolenaar86b68352004-12-27 21:59:20 +00004363 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004364 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004365 {
4366 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004367 {
4368 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004369 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004370 if (buf != curbuf)
4371 /* If we jumped to another buffer redrawing will already be
4372 * taken care of. */
4373 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004374
4375 /* Jump to the directory used after loading the buffer. */
4376 if (curbuf == first_match_buf && target_dir != NULL)
4377 {
4378 exarg_T ea;
4379
4380 ea.arg = target_dir;
4381 ea.cmdidx = CMD_lcd;
4382 ex_cd(&ea);
4383 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004384 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004385 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004386 else
4387 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004388
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004389 /* If we loaded a dummy buffer into the current window, the autocommands
4390 * may have messed up things, need to redraw and recompute folds. */
4391 if (redraw_for_dummy)
4392 {
4393#ifdef FEAT_FOLDING
4394 foldUpdateAll(curwin);
4395#else
4396 redraw_later(NOT_VALID);
4397#endif
4398 }
4399
Bram Moolenaar86b68352004-12-27 21:59:20 +00004400theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004401 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004402 vim_free(dirname_now);
4403 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004404 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004405 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004406}
4407
4408/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004409 * Restore current working directory to "dirname_start" if they differ, taking
4410 * into account whether it is set locally or globally.
4411 */
4412 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004413restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004414{
4415 char_u *dirname_now = alloc(MAXPATHL);
4416
4417 if (NULL != dirname_now)
4418 {
4419 mch_dirname(dirname_now, MAXPATHL);
4420 if (STRCMP(dirname_start, dirname_now) != 0)
4421 {
4422 /* If the directory has changed, change it back by building up an
4423 * appropriate ex command and executing it. */
4424 exarg_T ea;
4425
4426 ea.arg = dirname_start;
4427 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4428 ex_cd(&ea);
4429 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004430 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004431 }
4432}
4433
4434/*
4435 * Load file "fname" into a dummy buffer and return the buffer pointer,
4436 * placing the directory resulting from the buffer load into the
4437 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4438 * prior to calling this function. Restores directory to "dirname_start" prior
4439 * to returning, if autocmds or the 'autochdir' option have changed it.
4440 *
4441 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4442 * or wipe_dummy_buffer() later!
4443 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004444 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004445 */
4446 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004447load_dummy_buffer(
4448 char_u *fname,
4449 char_u *dirname_start, /* in: old directory */
4450 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004451{
4452 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004453 bufref_T newbufref;
4454 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004455 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004456 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004457
4458 /* Allocate a buffer without putting it in the buffer list. */
4459 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4460 if (newbuf == NULL)
4461 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004462 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004463
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004464 /* Init the options. */
4465 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4466
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004467 /* need to open the memfile before putting the buffer in a window */
4468 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004469 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004470 /* set curwin/curbuf to buf and save a few things */
4471 aucmd_prepbuf(&aco, newbuf);
4472
4473 /* Need to set the filename for autocommands. */
4474 (void)setfname(curbuf, fname, NULL, FALSE);
4475
Bram Moolenaar81695252004-12-29 20:58:21 +00004476 /* Create swap file now to avoid the ATTENTION message. */
4477 check_need_swap(TRUE);
4478
4479 /* Remove the "dummy" flag, otherwise autocommands may not
4480 * work. */
4481 curbuf->b_flags &= ~BF_DUMMY;
4482
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004483 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004484 if (readfile(fname, NULL,
4485 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4486 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004487 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004488 && !(curbuf->b_flags & BF_NEW))
4489 {
4490 failed = FALSE;
4491 if (curbuf != newbuf)
4492 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004493 /* Bloody autocommands changed the buffer! Can happen when
4494 * using netrw and editing a remote file. Use the current
4495 * buffer instead, delete the dummy one after restoring the
4496 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004497 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004498 newbuf = curbuf;
4499 }
4500 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004501
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004502 /* restore curwin/curbuf and a few other things */
4503 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004504 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4505 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004506
4507 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4508 * skip it. */
4509 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004510 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004511
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004512 /*
4513 * When autocommands/'autochdir' option changed directory: go back.
4514 * Let the caller know what the resulting dir was first, in case it is
4515 * important.
4516 */
4517 mch_dirname(resulting_dir, MAXPATHL);
4518 restore_start_dir(dirname_start);
4519
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004520 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004521 return NULL;
4522 if (failed)
4523 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004524 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004525 return NULL;
4526 }
4527 return newbuf;
4528}
4529
4530/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004531 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4532 * directory to "dirname_start" prior to returning, if autocmds or the
4533 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004534 */
4535 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004536wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004537{
4538 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004539 {
4540#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4541 cleanup_T cs;
4542
4543 /* Reset the error/interrupt/exception state here so that aborting()
4544 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4545 * work when got_int is set. */
4546 enter_cleanup(&cs);
4547#endif
4548
Bram Moolenaar81695252004-12-29 20:58:21 +00004549 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004550
4551#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4552 /* Restore the error/interrupt/exception state if not discarded by a
4553 * new aborting error, interrupt, or uncaught exception. */
4554 leave_cleanup(&cs);
4555#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004556 /* When autocommands/'autochdir' option changed directory: go back. */
4557 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004558 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004559}
4560
4561/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004562 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4563 * directory to "dirname_start" prior to returning, if autocmds or the
4564 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004565 */
4566 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004567unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004568{
4569 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004570 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004571 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004572
4573 /* When autocommands/'autochdir' option changed directory: go back. */
4574 restore_start_dir(dirname_start);
4575 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004576}
4577
Bram Moolenaar05159a02005-02-26 23:04:13 +00004578#if defined(FEAT_EVAL) || defined(PROTO)
4579/*
4580 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004581 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004582 */
4583 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004584get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004585{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004586 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004587 dict_T *dict;
4588 char_u buf[2];
4589 qfline_T *qfp;
4590 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004591 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004592
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004593 if (wp != NULL)
4594 {
4595 qi = GET_LOC_LIST(wp);
4596 if (qi == NULL)
4597 return FAIL;
4598 }
4599
Bram Moolenaard823fa92016-08-12 16:29:27 +02004600 if (qf_idx == -1)
4601 qf_idx = qi->qf_curlist;
4602
4603 if (qf_idx >= qi->qf_listcount
4604 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004605 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004606
Bram Moolenaard823fa92016-08-12 16:29:27 +02004607 qfp = qi->qf_lists[qf_idx].qf_start;
4608 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004609 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004610 /* Handle entries with a non-existing buffer number. */
4611 bufnum = qfp->qf_fnum;
4612 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4613 bufnum = 0;
4614
Bram Moolenaar05159a02005-02-26 23:04:13 +00004615 if ((dict = dict_alloc()) == NULL)
4616 return FAIL;
4617 if (list_append_dict(list, dict) == FAIL)
4618 return FAIL;
4619
4620 buf[0] = qfp->qf_type;
4621 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004622 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004623 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4624 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4625 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4626 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004627 || dict_add_nr_str(dict, "pattern", 0L,
4628 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4629 || dict_add_nr_str(dict, "text", 0L,
4630 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004631 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4632 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4633 return FAIL;
4634
4635 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004636 if (qfp == NULL)
4637 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004638 }
4639 return OK;
4640}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004641
4642/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004643 * Flags used by getqflist()/getloclist() to determine which fields to return.
4644 */
4645enum {
4646 QF_GETLIST_NONE = 0x0,
4647 QF_GETLIST_TITLE = 0x1,
4648 QF_GETLIST_ITEMS = 0x2,
4649 QF_GETLIST_NR = 0x4,
4650 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004651 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004652 QF_GETLIST_ALL = 0xFF
4653};
4654
4655/*
4656 * Return quickfix/location list details (title) as a
4657 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4658 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004659 */
4660 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004661get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4662{
4663 qf_info_T *qi = &ql_info;
4664 int status = OK;
4665 int qf_idx;
4666 dictitem_T *di;
4667 int flags = QF_GETLIST_NONE;
4668
4669 if (wp != NULL)
4670 {
4671 qi = GET_LOC_LIST(wp);
4672 if (qi == NULL)
Bram Moolenaar875feea2017-06-11 16:07:51 +02004673 {
4674 /* If querying for the size of the location list, return 0 */
4675 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL) &&
4676 (di->di_tv.v_type == VAR_STRING) &&
4677 (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4678 return dict_add_nr_str(retdict, "nr", 0, NULL);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004679 return FAIL;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004680 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004681 }
4682
4683 qf_idx = qi->qf_curlist; /* default is the current list */
4684 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4685 {
4686 /* Use the specified quickfix/location list */
4687 if (di->di_tv.v_type == VAR_NUMBER)
4688 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004689 /* for zero use the current list */
4690 if (di->di_tv.vval.v_number != 0)
4691 {
4692 qf_idx = di->di_tv.vval.v_number - 1;
4693 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4694 return FAIL;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004695 } else if (qi->qf_listcount == 0) /* stack is empty */
4696 return FAIL;
4697 flags |= QF_GETLIST_NR;
4698 } else if ((di->di_tv.v_type == VAR_STRING) &&
4699 (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4700 {
4701 {
4702 /* Get the last quickfix list number */
4703 if (qi->qf_listcount > 0)
4704 qf_idx = qi->qf_listcount - 1;
4705 else
4706 qf_idx = -1; /* Quickfix stack is empty */
Bram Moolenaar890680c2016-09-27 21:28:56 +02004707 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004708 flags |= QF_GETLIST_NR;
4709 }
4710 else
4711 return FAIL;
4712 }
4713
Bram Moolenaar875feea2017-06-11 16:07:51 +02004714 if (qf_idx != -1)
4715 {
4716 if (dict_find(what, (char_u *)"all", -1) != NULL)
4717 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004718
Bram Moolenaar875feea2017-06-11 16:07:51 +02004719 if (dict_find(what, (char_u *)"title", -1) != NULL)
4720 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004721
Bram Moolenaar875feea2017-06-11 16:07:51 +02004722 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4723 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004724
Bram Moolenaar875feea2017-06-11 16:07:51 +02004725 if (dict_find(what, (char_u *)"context", -1) != NULL)
4726 flags |= QF_GETLIST_CONTEXT;
4727 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004728
Bram Moolenaard823fa92016-08-12 16:29:27 +02004729 if (flags & QF_GETLIST_TITLE)
4730 {
4731 char_u *t;
4732 t = qi->qf_lists[qf_idx].qf_title;
4733 if (t == NULL)
4734 t = (char_u *)"";
4735 status = dict_add_nr_str(retdict, "title", 0L, t);
4736 }
4737 if ((status == OK) && (flags & QF_GETLIST_NR))
4738 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4739 if ((status == OK) && (flags & QF_GETLIST_WINID))
4740 {
4741 win_T *win;
4742 win = qf_find_win(qi);
4743 if (win != NULL)
4744 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4745 }
4746
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004747 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4748 {
4749 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4750 {
4751 di = dictitem_alloc((char_u *)"context");
4752 if (di != NULL)
4753 {
4754 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004755 if (dict_add(retdict, di) == FAIL)
4756 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004757 }
4758 }
4759 else
4760 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4761 }
4762
Bram Moolenaard823fa92016-08-12 16:29:27 +02004763 return status;
4764}
4765
4766/*
4767 * Add list of entries to quickfix/location list. Each list entry is
4768 * a dictionary with item information.
4769 */
4770 static int
4771qf_add_entries(
4772 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004773 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004774 list_T *list,
4775 char_u *title,
4776 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004777{
4778 listitem_T *li;
4779 dict_T *d;
4780 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004781 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004782 long lnum;
4783 int col, nr;
4784 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004785#ifdef FEAT_WINDOWS
4786 qfline_T *old_last = NULL;
4787#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004788 int valid, status;
4789 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004790 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004791
Bram Moolenaara3921f42017-06-04 15:30:34 +02004792 if (action == ' ' || qf_idx == qi->qf_listcount)
4793 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004794 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004795 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02004796 qf_idx = qi->qf_curlist;
4797 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004798#ifdef FEAT_WINDOWS
Bram Moolenaara3921f42017-06-04 15:30:34 +02004799 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004800 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004801 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004802#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004803 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004804 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02004805 qf_free(qi, qf_idx);
4806 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004807 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004808
4809 for (li = list->lv_first; li != NULL; li = li->li_next)
4810 {
4811 if (li->li_tv.v_type != VAR_DICT)
4812 continue; /* Skip non-dict items */
4813
4814 d = li->li_tv.vval.v_dict;
4815 if (d == NULL)
4816 continue;
4817
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004818 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004819 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4820 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4821 col = (int)get_dict_number(d, (char_u *)"col");
4822 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4823 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004824 type = get_dict_string(d, (char_u *)"type", TRUE);
4825 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4826 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004827 if (text == NULL)
4828 text = vim_strsave((char_u *)"");
4829
4830 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004831 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004832 valid = FALSE;
4833
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004834 /* Mark entries with non-existing buffer number as not valid. Give the
4835 * error message only once. */
4836 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4837 {
4838 if (!did_bufnr_emsg)
4839 {
4840 did_bufnr_emsg = TRUE;
4841 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4842 }
4843 valid = FALSE;
4844 bufnum = 0;
4845 }
4846
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02004847 /* If the 'valid' field is present it overrules the detected value. */
4848 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
4849 valid = (int)get_dict_number(d, (char_u *)"valid");
4850
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004851 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004852 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004853 NULL, /* dir */
4854 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004855 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004856 text,
4857 lnum,
4858 col,
4859 vcol, /* vis_col */
4860 pattern, /* search pattern */
4861 nr,
4862 type == NULL ? NUL : *type,
4863 valid);
4864
4865 vim_free(filename);
4866 vim_free(pattern);
4867 vim_free(text);
4868 vim_free(type);
4869
4870 if (status == FAIL)
4871 {
4872 retval = FAIL;
4873 break;
4874 }
4875 }
4876
Bram Moolenaara3921f42017-06-04 15:30:34 +02004877 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004878 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02004879 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004880 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02004881 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004882 if (action != 'a')
4883 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02004884 qi->qf_lists[qf_idx].qf_ptr =
4885 qi->qf_lists[qf_idx].qf_start;
4886 if (qi->qf_lists[qf_idx].qf_count > 0)
4887 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004888 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004889
4890#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004891 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004892 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004893#endif
4894
4895 return retval;
4896}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004897
4898 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004899qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004900{
4901 dictitem_T *di;
4902 int retval = FAIL;
4903 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004904 int newlist = FALSE;
4905
4906 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4907 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004908
4909 qf_idx = qi->qf_curlist; /* default is the current list */
4910 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4911 {
4912 /* Use the specified quickfix/location list */
4913 if (di->di_tv.v_type == VAR_NUMBER)
4914 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004915 /* for zero use the current list */
4916 if (di->di_tv.vval.v_number != 0)
4917 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004918 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4919 return FAIL;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004920 } else if (di->di_tv.v_type == VAR_STRING &&
4921 STRCMP(di->di_tv.vval.v_string, "$") == 0 &&
4922 qi->qf_listcount > 0)
4923 qf_idx = qi->qf_listcount - 1;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004924 else
4925 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004926 newlist = FALSE; /* use the specified list */
4927 }
4928
4929 if (newlist)
4930 {
4931 qf_new_list(qi, NULL);
4932 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004933 }
4934
4935 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4936 {
4937 if (di->di_tv.v_type == VAR_STRING)
4938 {
4939 vim_free(qi->qf_lists[qf_idx].qf_title);
4940 qi->qf_lists[qf_idx].qf_title =
4941 get_dict_string(what, (char_u *)"title", TRUE);
4942 if (qf_idx == qi->qf_curlist)
4943 qf_update_win_titlevar(qi);
4944 retval = OK;
4945 }
4946 }
4947
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004948 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
4949 {
4950 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02004951
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004952 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004953 ctx = alloc_tv();
4954 if (ctx != NULL)
4955 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004956 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004957 }
4958
Bram Moolenaard823fa92016-08-12 16:29:27 +02004959 return retval;
4960}
4961
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004962/*
4963 * Find the non-location list window with the specified location list.
4964 */
4965 static win_T *
4966find_win_with_ll(qf_info_T *qi)
4967{
4968 win_T *wp = NULL;
4969
4970 FOR_ALL_WINDOWS(wp)
4971 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
4972 return wp;
4973
4974 return NULL;
4975}
4976
4977/*
4978 * Free the entire quickfix/location list stack.
4979 * If the quickfix/location list window is open, then clear it.
4980 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004981 static void
4982qf_free_stack(win_T *wp, qf_info_T *qi)
4983{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004984 win_T *qfwin = qf_find_win(qi);
4985 win_T *llwin = NULL;
4986 win_T *orig_wp = wp;
4987
4988 if (qfwin != NULL)
4989 {
4990 /* If the quickfix/location list window is open, then clear it */
4991 if (qi->qf_curlist < qi->qf_listcount)
4992 qf_free(qi, qi->qf_curlist);
4993 qf_update_buffer(qi, NULL);
4994 }
4995
4996 if (wp != NULL && IS_LL_WINDOW(wp))
4997 {
4998 /* If in the location list window, then use the non-location list
4999 * window with this location list (if present)
5000 */
5001 llwin = find_win_with_ll(qi);
5002 if (llwin != NULL)
5003 wp = llwin;
5004 }
5005
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005006 qf_free_all(wp);
5007 if (wp == NULL)
5008 {
5009 /* quickfix list */
5010 qi->qf_curlist = 0;
5011 qi->qf_listcount = 0;
5012 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005013 else if (IS_LL_WINDOW(orig_wp))
5014 {
5015 /* If the location list window is open, then create a new empty
5016 * location list */
5017 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005018
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005019 /* first free the list reference in the location list window */
5020 ll_free_all(&orig_wp->w_llist_ref);
5021
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005022 orig_wp->w_llist_ref = new_ll;
5023 if (llwin != NULL)
5024 {
5025 llwin->w_llist = new_ll;
5026 new_ll->qf_refcount++;
5027 }
5028 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005029}
5030
Bram Moolenaard823fa92016-08-12 16:29:27 +02005031/*
5032 * Populate the quickfix list with the items supplied in the list
5033 * of dictionaries. "title" will be copied to w:quickfix_title.
5034 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5035 */
5036 int
5037set_errorlist(
5038 win_T *wp,
5039 list_T *list,
5040 int action,
5041 char_u *title,
5042 dict_T *what)
5043{
5044 qf_info_T *qi = &ql_info;
5045 int retval = OK;
5046
5047 if (wp != NULL)
5048 {
5049 qi = ll_get_or_alloc_list(wp);
5050 if (qi == NULL)
5051 return FAIL;
5052 }
5053
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005054 if (action == 'f')
5055 {
5056 /* Free the entire quickfix or location list stack */
5057 qf_free_stack(wp, qi);
5058 }
5059 else if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005060 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005061 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005062 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005063
5064 return retval;
5065}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005066
5067 static int
5068mark_quickfix_ctx(qf_info_T *qi, int copyID)
5069{
5070 int i;
5071 int abort = FALSE;
5072 typval_T *ctx;
5073
5074 for (i = 0; i < LISTCOUNT && !abort; ++i)
5075 {
5076 ctx = qi->qf_lists[i].qf_ctx;
5077 if (ctx != NULL && ctx->v_type != VAR_NUMBER &&
5078 ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
5079 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5080 }
5081
5082 return abort;
5083}
5084
5085/*
5086 * Mark the context of the quickfix list and the location lists (if present) as
5087 * "in use". So that garabage collection doesn't free the context.
5088 */
5089 int
5090set_ref_in_quickfix(int copyID)
5091{
5092 int abort = FALSE;
5093 tabpage_T *tp;
5094 win_T *win;
5095
5096 abort = mark_quickfix_ctx(&ql_info, copyID);
5097 if (abort)
5098 return abort;
5099
5100 FOR_ALL_TAB_WINDOWS(tp, win)
5101 {
5102 if (win->w_llist != NULL)
5103 {
5104 abort = mark_quickfix_ctx(win->w_llist, copyID);
5105 if (abort)
5106 return abort;
5107 }
5108 }
5109
5110 return abort;
5111}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005112#endif
5113
Bram Moolenaar81695252004-12-29 20:58:21 +00005114/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005115 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005116 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005117 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005118 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005119 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005120 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005121 */
5122 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005123ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005124{
5125 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005126 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005127#ifdef FEAT_AUTOCMD
5128 char_u *au_name = NULL;
5129#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005130
Bram Moolenaardb552d602006-03-23 22:59:57 +00005131 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5132 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005133 {
5134 qi = ll_get_or_alloc_list(curwin);
5135 if (qi == NULL)
5136 return;
5137 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005138
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005139#ifdef FEAT_AUTOCMD
5140 switch (eap->cmdidx)
5141 {
5142 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5143 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5144 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5145 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5146 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5147 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5148 default: break;
5149 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005150 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5151 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005152 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005153# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005154 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005155 return;
5156# endif
5157 }
5158#endif
5159
Bram Moolenaar86b68352004-12-27 21:59:20 +00005160 if (*eap->arg == NUL)
5161 buf = curbuf;
5162 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5163 buf = buflist_findnr(atoi((char *)eap->arg));
5164 if (buf == NULL)
5165 EMSG(_(e_invarg));
5166 else if (buf->b_ml.ml_mfp == NULL)
5167 EMSG(_("E681: Buffer is not loaded"));
5168 else
5169 {
5170 if (eap->addr_count == 0)
5171 {
5172 eap->line1 = 1;
5173 eap->line2 = buf->b_ml.ml_line_count;
5174 }
5175 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5176 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5177 EMSG(_(e_invrange));
5178 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005179 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005180 char_u *qf_title = *eap->cmdlinep;
5181
5182 if (buf->b_sfname)
5183 {
5184 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5185 (char *)qf_title, (char *)buf->b_sfname);
5186 qf_title = IObuff;
5187 }
5188
Bram Moolenaardb552d602006-03-23 22:59:57 +00005189 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
5190 (eap->cmdidx != CMD_caddbuffer
5191 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005192 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005193 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005194 {
5195#ifdef FEAT_AUTOCMD
5196 if (au_name != NULL)
5197 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5198 curbuf->b_fname, TRUE, curbuf);
5199#endif
5200 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5201 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5202 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005203 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005204 }
5205}
5206
Bram Moolenaar1e015462005-09-25 22:16:38 +00005207#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005208/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005209 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5210 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005211 */
5212 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005213ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005214{
5215 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005216 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005217#ifdef FEAT_AUTOCMD
5218 char_u *au_name = NULL;
5219#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005220
Bram Moolenaardb552d602006-03-23 22:59:57 +00005221 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5222 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005223 {
5224 qi = ll_get_or_alloc_list(curwin);
5225 if (qi == NULL)
5226 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005227 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005228
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005229#ifdef FEAT_AUTOCMD
5230 switch (eap->cmdidx)
5231 {
5232 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5233 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5234 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5235 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5236 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5237 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5238 default: break;
5239 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005240 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5241 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005242 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005243# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005244 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005245 return;
5246# endif
5247 }
5248#endif
5249
Bram Moolenaar4770d092006-01-12 23:22:24 +00005250 /* Evaluate the expression. When the result is a string or a list we can
5251 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005252 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005253 if (tv != NULL)
5254 {
5255 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5256 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5257 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01005258 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005259 (eap->cmdidx != CMD_caddexpr
5260 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005261 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5262 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005263 {
5264#ifdef FEAT_AUTOCMD
5265 if (au_name != NULL)
5266 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5267 curbuf->b_fname, TRUE, curbuf);
5268#endif
5269 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5270 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5271 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005272 }
5273 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005274 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005275 free_tv(tv);
5276 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005277}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005278#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005279
5280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 * ":helpgrep {pattern}"
5282 */
5283 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005284ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
5286 regmatch_T regmatch;
5287 char_u *save_cpo;
5288 char_u *p;
5289 int fcount;
5290 char_u **fnames;
5291 FILE *fd;
5292 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005294#ifdef FEAT_MULTI_LANG
5295 char_u *lang;
5296#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005297 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005298 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005299 int new_qi = FALSE;
5300 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005301#ifdef FEAT_AUTOCMD
5302 char_u *au_name = NULL;
5303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005305#ifdef FEAT_MULTI_LANG
5306 /* Check for a specified language */
5307 lang = check_help_lang(eap->arg);
5308#endif
5309
Bram Moolenaar73633f82012-01-20 13:39:07 +01005310#ifdef FEAT_AUTOCMD
5311 switch (eap->cmdidx)
5312 {
5313 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5314 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5315 default: break;
5316 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005317 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5318 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005319 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005320# ifdef FEAT_EVAL
5321 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005322 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005323# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005324 }
5325#endif
5326
5327 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5328 save_cpo = p_cpo;
5329 p_cpo = empty_option;
5330
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005331 if (eap->cmdidx == CMD_lhelpgrep)
5332 {
5333 /* Find an existing help window */
5334 FOR_ALL_WINDOWS(wp)
5335 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5336 break;
5337
5338 if (wp == NULL) /* Help window not found */
5339 qi = NULL;
5340 else
5341 qi = wp->w_llist;
5342
5343 if (qi == NULL)
5344 {
5345 /* Allocate a new location list for help text matches */
5346 if ((qi = ll_new_list()) == NULL)
5347 return;
5348 new_qi = TRUE;
5349 }
5350 }
5351
Bram Moolenaaree85df32017-03-19 14:19:50 +01005352 /* Autocommands may change the list. Save it for later comparison */
5353 save_qi = qi;
5354
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5356 regmatch.rm_ic = FALSE;
5357 if (regmatch.regprog != NULL)
5358 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005359#ifdef FEAT_MBYTE
5360 vimconv_T vc;
5361
5362 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5363 * differs. */
5364 vc.vc_type = CONV_NONE;
5365 if (!enc_utf8)
5366 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5367#endif
5368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005370 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372 /* Go through all directories in 'runtimepath' */
5373 p = p_rtp;
5374 while (*p != NUL && !got_int)
5375 {
5376 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5377
5378 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5379 add_pathsep(NameBuff);
5380 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5381 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5382 &fnames, EW_FILE|EW_SILENT) == OK
5383 && fcount > 0)
5384 {
5385 for (fi = 0; fi < fcount && !got_int; ++fi)
5386 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005387#ifdef FEAT_MULTI_LANG
5388 /* Skip files for a different language. */
5389 if (lang != NULL
5390 && STRNICMP(lang, fnames[fi]
5391 + STRLEN(fnames[fi]) - 3, 2) != 0
5392 && !(STRNICMP(lang, "en", 2) == 0
5393 && STRNICMP("txt", fnames[fi]
5394 + STRLEN(fnames[fi]) - 3, 3) == 0))
5395 continue;
5396#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005397 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 if (fd != NULL)
5399 {
5400 lnum = 1;
5401 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5402 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005403 char_u *line = IObuff;
5404#ifdef FEAT_MBYTE
5405 /* Convert a line if 'encoding' is not utf-8 and
5406 * the line contains a non-ASCII character. */
5407 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005408 && has_non_ascii(IObuff))
5409 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005410 line = string_convert(&vc, IObuff, NULL);
5411 if (line == NULL)
5412 line = IObuff;
5413 }
5414#endif
5415
5416 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005418 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005421 while (l > 0 && line[l - 1] <= ' ')
5422 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005424 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005425 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 NULL, /* dir */
5427 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005428 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005429 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005431 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005432 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005433 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005434 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 0, /* nr */
5436 1, /* type */
5437 TRUE /* valid */
5438 ) == FAIL)
5439 {
5440 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005441#ifdef FEAT_MBYTE
5442 if (line != IObuff)
5443 vim_free(line);
5444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 break;
5446 }
5447 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005448#ifdef FEAT_MBYTE
5449 if (line != IObuff)
5450 vim_free(line);
5451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 ++lnum;
5453 line_breakcheck();
5454 }
5455 fclose(fd);
5456 }
5457 }
5458 FreeWild(fcount, fnames);
5459 }
5460 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005461
Bram Moolenaar473de612013-06-08 18:19:48 +02005462 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005463#ifdef FEAT_MBYTE
5464 if (vc.vc_type != CONV_NONE)
5465 convert_setup(&vc, NULL, NULL);
5466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005468 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5469 qi->qf_lists[qi->qf_curlist].qf_ptr =
5470 qi->qf_lists[qi->qf_curlist].qf_start;
5471 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005474 if (p_cpo == empty_option)
5475 p_cpo = save_cpo;
5476 else
5477 /* Darn, some plugin changed the value. */
5478 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479
5480#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005481 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482#endif
5483
Bram Moolenaar73633f82012-01-20 13:39:07 +01005484#ifdef FEAT_AUTOCMD
5485 if (au_name != NULL)
5486 {
5487 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5488 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005489 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005490 /* autocommands made "qi" invalid */
5491 return;
5492 }
5493#endif
5494
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005496 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005497 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005498 else
5499 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005500
5501 if (eap->cmdidx == CMD_lhelpgrep)
5502 {
5503 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005504 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005505 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5506 {
5507 if (new_qi)
5508 ll_free_all(&qi);
5509 }
5510 else if (curwin->w_llist == NULL)
5511 curwin->w_llist = qi;
5512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513}
5514
5515#endif /* FEAT_QUICKFIX */