blob: e330e6e9befa38a01dd9501952cdd9247e1853cb [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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100121static void qf_store_title(qf_info_T *qi, char_u *title);
122static void qf_new_list(qf_info_T *qi, char_u *qf_title);
123static void ll_free_all(qf_info_T **pqi);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +0200124static int qf_add_entry(qf_info_T *qi, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +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 Moolenaar361c8f02016-07-02 15:41:47 +02001280 qi->qf_directory,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001281 (*fields.namebuf || qi->qf_directory != NULL)
1282 ? fields.namebuf
1283 : ((qi->qf_currfile != NULL && fields.valid)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001284 ? qi->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001285 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001286 fields.errmsg,
1287 fields.lnum,
1288 fields.col,
1289 fields.use_viscol,
1290 fields.pattern,
1291 fields.enr,
1292 fields.type,
1293 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 goto error2;
1295 line_breakcheck();
1296 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001297 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001299 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001301 /* no valid entry found */
1302 qi->qf_lists[qi->qf_curlist].qf_ptr =
1303 qi->qf_lists[qi->qf_curlist].qf_start;
1304 qi->qf_lists[qi->qf_curlist].qf_index = 1;
1305 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 else
1308 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001309 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
1310 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
1311 qi->qf_lists[qi->qf_curlist].qf_ptr =
1312 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001314 /* return number of matches */
1315 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001316 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318 EMSG(_(e_readerrf));
1319error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001320 if (!adding)
1321 {
1322 qf_free(qi, qi->qf_curlist);
1323 qi->qf_listcount--;
1324 if (qi->qf_curlist > 0)
1325 --qi->qf_curlist;
1326 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001327qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001328 if (state.fd != NULL)
1329 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001330 vim_free(fields.namebuf);
1331 vim_free(fields.errmsg);
1332 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001333 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
1335#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02001336 qf_update_buffer(qi, old_last);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001338#ifdef FEAT_MBYTE
1339 if (state.vc.vc_type != CONV_NONE)
1340 convert_setup(&state.vc, NULL, NULL);
1341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 return retval;
1344}
1345
Bram Moolenaarfb604092014-07-23 15:55:00 +02001346 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001347qf_store_title(qf_info_T *qi, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001348{
1349 if (title != NULL)
1350 {
1351 char_u *p = alloc((int)STRLEN(title) + 2);
1352
1353 qi->qf_lists[qi->qf_curlist].qf_title = p;
1354 if (p != NULL)
1355 sprintf((char *)p, ":%s", (char *)title);
1356 }
1357}
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359/*
1360 * Prepare for adding a new quickfix list.
1361 */
1362 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001363qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 int i;
1366
1367 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001368 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * the current entry. This makes it possible to browse in a tree-like
1370 * way with ":grep'.
1371 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001372 while (qi->qf_listcount > qi->qf_curlist + 1)
1373 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375 /*
1376 * When the stack is full, remove to oldest entry
1377 * Otherwise, add a new entry.
1378 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001379 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001381 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001383 qi->qf_lists[i - 1] = qi->qf_lists[i];
1384 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001387 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001388 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +02001389 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390}
1391
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001392/*
1393 * Free a location list
1394 */
1395 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001396ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001397{
1398 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001399 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001400
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001401 qi = *pqi;
1402 if (qi == NULL)
1403 return;
1404 *pqi = NULL; /* Remove reference to this list */
1405
1406 qi->qf_refcount--;
1407 if (qi->qf_refcount < 1)
1408 {
1409 /* No references to this location list */
1410 for (i = 0; i < qi->qf_listcount; ++i)
1411 qf_free(qi, i);
1412 vim_free(qi);
1413 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001414}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001415
1416 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001417qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001418{
1419 int i;
1420 qf_info_T *qi = &ql_info;
1421
1422 if (wp != NULL)
1423 {
1424 /* location list */
1425 ll_free_all(&wp->w_llist);
1426 ll_free_all(&wp->w_llist_ref);
1427 }
1428 else
1429 /* quickfix list */
1430 for (i = 0; i < qi->qf_listcount; ++i)
1431 qf_free(qi, i);
1432}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001433
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434/*
1435 * Add an entry to the end of the list of errors.
1436 * Returns OK or FAIL.
1437 */
1438 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001439qf_add_entry(
1440 qf_info_T *qi, /* quickfix list */
Bram Moolenaar05540972016-01-30 20:31:25 +01001441 char_u *dir, /* optional directory name */
1442 char_u *fname, /* file name or NULL */
1443 int bufnum, /* buffer number or zero */
1444 char_u *mesg, /* message */
1445 long lnum, /* line number */
1446 int col, /* column */
1447 int vis_col, /* using visual column */
1448 char_u *pattern, /* search pattern */
1449 int nr, /* error number */
1450 int type, /* type character */
1451 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001453 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001454 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001456 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001458 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001459 {
1460 buf_T *buf = buflist_findnr(bufnum);
1461
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001462 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001463 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001464 buf->b_has_qf_entry |=
1465 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001466 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001467 else
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001468 qfp->qf_fnum = qf_get_fnum(qi, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1470 {
1471 vim_free(qfp);
1472 return FAIL;
1473 }
1474 qfp->qf_lnum = lnum;
1475 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001476 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001477 if (pattern == NULL || *pattern == NUL)
1478 qfp->qf_pattern = NULL;
1479 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1480 {
1481 vim_free(qfp->qf_text);
1482 vim_free(qfp);
1483 return FAIL;
1484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 qfp->qf_nr = nr;
1486 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1487 type = 0;
1488 qfp->qf_type = type;
1489 qfp->qf_valid = valid;
1490
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001491 lastp = &qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001492 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1493 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001495 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar8b201792016-03-25 15:01:10 +01001496 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1497 qi->qf_lists[qi->qf_curlist].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001498 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500 else
1501 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001502 qfp->qf_prev = *lastp;
1503 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001505 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001507 *lastp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001508 ++qi->qf_lists[qi->qf_curlist].qf_count;
1509 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1510 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001512 qi->qf_lists[qi->qf_curlist].qf_index =
1513 qi->qf_lists[qi->qf_curlist].qf_count;
1514 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 }
1516
1517 return OK;
1518}
1519
1520/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001521 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001523 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001524ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001525{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001526 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001527
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001528 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1529 if (qi != NULL)
1530 {
1531 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1532 qi->qf_refcount++;
1533 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001535 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001536}
1537
1538/*
1539 * Return the location list for window 'wp'.
1540 * If not present, allocate a location list
1541 */
1542 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001543ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001544{
1545 if (IS_LL_WINDOW(wp))
1546 /* For a location list window, use the referenced location list */
1547 return wp->w_llist_ref;
1548
1549 /*
1550 * For a non-location list window, w_llist_ref should not point to a
1551 * location list.
1552 */
1553 ll_free_all(&wp->w_llist_ref);
1554
1555 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001556 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001557 return wp->w_llist;
1558}
1559
1560/*
1561 * Copy the location list from window "from" to window "to".
1562 */
1563 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001564copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001565{
1566 qf_info_T *qi;
1567 int idx;
1568 int i;
1569
1570 /*
1571 * When copying from a location list window, copy the referenced
1572 * location list. For other windows, copy the location list for
1573 * that window.
1574 */
1575 if (IS_LL_WINDOW(from))
1576 qi = from->w_llist_ref;
1577 else
1578 qi = from->w_llist;
1579
1580 if (qi == NULL) /* no location list to copy */
1581 return;
1582
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001583 /* allocate a new location list */
1584 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001585 return;
1586
1587 to->w_llist->qf_listcount = qi->qf_listcount;
1588
1589 /* Copy the location lists one at a time */
1590 for (idx = 0; idx < qi->qf_listcount; idx++)
1591 {
1592 qf_list_T *from_qfl;
1593 qf_list_T *to_qfl;
1594
1595 to->w_llist->qf_curlist = idx;
1596
1597 from_qfl = &qi->qf_lists[idx];
1598 to_qfl = &to->w_llist->qf_lists[idx];
1599
1600 /* Some of the fields are populated by qf_add_entry() */
1601 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1602 to_qfl->qf_count = 0;
1603 to_qfl->qf_index = 0;
1604 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001605 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001606 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001607 if (from_qfl->qf_title != NULL)
1608 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1609 else
1610 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001611 if (from_qfl->qf_ctx != NULL)
1612 {
1613 to_qfl->qf_ctx = alloc_tv();
1614 if (to_qfl->qf_ctx != NULL)
1615 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1616 }
1617 else
1618 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001619
1620 if (from_qfl->qf_count)
1621 {
1622 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001623 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001624
1625 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001626 for (i = 0, from_qfp = from_qfl->qf_start;
1627 i < from_qfl->qf_count && from_qfp != NULL;
1628 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001629 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001630 if (qf_add_entry(to->w_llist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631 NULL,
1632 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001633 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001634 from_qfp->qf_text,
1635 from_qfp->qf_lnum,
1636 from_qfp->qf_col,
1637 from_qfp->qf_viscol,
1638 from_qfp->qf_pattern,
1639 from_qfp->qf_nr,
1640 0,
1641 from_qfp->qf_valid) == FAIL)
1642 {
1643 qf_free_all(to);
1644 return;
1645 }
1646 /*
1647 * qf_add_entry() will not set the qf_num field, as the
1648 * directory and file names are not supplied. So the qf_fnum
1649 * field is copied here.
1650 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001651 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001652 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1653 prevp->qf_type = from_qfp->qf_type; /* error type */
1654 if (from_qfl->qf_ptr == from_qfp)
1655 to_qfl->qf_ptr = prevp; /* current location */
1656 }
1657 }
1658
1659 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1660
1661 /* When no valid entries are present in the list, qf_ptr points to
1662 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001663 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001664 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001665 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001666 to_qfl->qf_index = 1;
1667 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001668 }
1669
1670 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1671}
1672
1673/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001674 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001675 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 */
1677 static int
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001678qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
Bram Moolenaar82404332016-07-10 17:00:38 +02001680 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001681 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001682 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (fname == NULL || *fname == NUL) /* no file name */
1685 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaare60acc12011-05-10 16:41:25 +02001687#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001688 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001689#endif
1690#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001691 if (directory != NULL)
1692 slash_adjust(directory);
1693 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001694#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001695 if (directory != NULL && !vim_isAbsName(fname)
1696 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1697 {
1698 /*
1699 * Here we check if the file really exists.
1700 * This should normally be true, but if make works without
1701 * "leaving directory"-messages we might have missed a
1702 * directory change.
1703 */
1704 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 vim_free(ptr);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001707 directory = qf_guess_filepath(qi, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001708 if (directory)
1709 ptr = concat_fnames(directory, fname, TRUE);
1710 else
1711 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001713 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001714 bufname = ptr;
1715 }
1716 else
1717 bufname = fname;
1718
1719 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001720 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001721 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001722 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001723 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001725 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001726 {
1727 vim_free(qf_last_bufname);
1728 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1729 if (bufname == ptr)
1730 qf_last_bufname = bufname;
1731 else
1732 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001733 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001734 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001735 if (buf == NULL)
1736 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001737
Bram Moolenaarc1542742016-07-20 21:44:37 +02001738 buf->b_has_qf_entry =
1739 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001740 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741}
1742
1743/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001744 * Push dirbuf onto the directory stack and return pointer to actual dir or
1745 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 */
1747 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001748qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 struct dir_stack_T *ds_new;
1751 struct dir_stack_T *ds_ptr;
1752
1753 /* allocate new stack element and hook it in */
1754 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1755 if (ds_new == NULL)
1756 return NULL;
1757
1758 ds_new->next = *stackptr;
1759 *stackptr = ds_new;
1760
1761 /* store directory on the stack */
1762 if (vim_isAbsName(dirbuf)
1763 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001764 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 (*stackptr)->dirname = vim_strsave(dirbuf);
1766 else
1767 {
1768 /* Okay we don't have an absolute path.
1769 * dirbuf must be a subdir of one of the directories on the stack.
1770 * Let's search...
1771 */
1772 ds_new = (*stackptr)->next;
1773 (*stackptr)->dirname = NULL;
1774 while (ds_new)
1775 {
1776 vim_free((*stackptr)->dirname);
1777 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1778 TRUE);
1779 if (mch_isdir((*stackptr)->dirname) == TRUE)
1780 break;
1781
1782 ds_new = ds_new->next;
1783 }
1784
1785 /* clean up all dirs we already left */
1786 while ((*stackptr)->next != ds_new)
1787 {
1788 ds_ptr = (*stackptr)->next;
1789 (*stackptr)->next = (*stackptr)->next->next;
1790 vim_free(ds_ptr->dirname);
1791 vim_free(ds_ptr);
1792 }
1793
1794 /* Nothing found -> it must be on top level */
1795 if (ds_new == NULL)
1796 {
1797 vim_free((*stackptr)->dirname);
1798 (*stackptr)->dirname = vim_strsave(dirbuf);
1799 }
1800 }
1801
1802 if ((*stackptr)->dirname != NULL)
1803 return (*stackptr)->dirname;
1804 else
1805 {
1806 ds_ptr = *stackptr;
1807 *stackptr = (*stackptr)->next;
1808 vim_free(ds_ptr);
1809 return NULL;
1810 }
1811}
1812
1813
1814/*
1815 * pop dirbuf from the directory stack and return previous directory or NULL if
1816 * stack is empty
1817 */
1818 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001819qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820{
1821 struct dir_stack_T *ds_ptr;
1822
1823 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1824 * What to do if it isn't? */
1825
1826 /* pop top element and free it */
1827 if (*stackptr != NULL)
1828 {
1829 ds_ptr = *stackptr;
1830 *stackptr = (*stackptr)->next;
1831 vim_free(ds_ptr->dirname);
1832 vim_free(ds_ptr);
1833 }
1834
1835 /* return NEW top element as current dir or NULL if stack is empty*/
1836 return *stackptr ? (*stackptr)->dirname : NULL;
1837}
1838
1839/*
1840 * clean up directory stack
1841 */
1842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001843qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
1845 struct dir_stack_T *ds_ptr;
1846
1847 while ((ds_ptr = *stackptr) != NULL)
1848 {
1849 *stackptr = (*stackptr)->next;
1850 vim_free(ds_ptr->dirname);
1851 vim_free(ds_ptr);
1852 }
1853}
1854
1855/*
1856 * Check in which directory of the directory stack the given file can be
1857 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001858 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 * Cleans up intermediate directory entries.
1860 *
1861 * TODO: How to solve the following problem?
1862 * If we have the this directory tree:
1863 * ./
1864 * ./aa
1865 * ./aa/bb
1866 * ./bb
1867 * ./bb/x.c
1868 * and make says:
1869 * making all in aa
1870 * making all in bb
1871 * x.c:9: Error
1872 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1873 * qf_guess_filepath will return NULL.
1874 */
1875 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001876qf_guess_filepath(qf_info_T *qi, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
1878 struct dir_stack_T *ds_ptr;
1879 struct dir_stack_T *ds_tmp;
1880 char_u *fullname;
1881
1882 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001883 if (qi->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 return NULL;
1885
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001886 ds_ptr = qi->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 fullname = NULL;
1888 while (ds_ptr)
1889 {
1890 vim_free(fullname);
1891 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1892
1893 /* If concat_fnames failed, just go on. The worst thing that can happen
1894 * is that we delete the entire stack.
1895 */
1896 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1897 break;
1898
1899 ds_ptr = ds_ptr->next;
1900 }
1901
1902 vim_free(fullname);
1903
1904 /* clean up all dirs we already left */
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001905 while (qi->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001907 ds_tmp = qi->qf_dir_stack->next;
1908 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 vim_free(ds_tmp->dirname);
1910 vim_free(ds_tmp);
1911 }
1912
1913 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914}
1915
1916/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001917 * When loading a file from the quickfix, the auto commands may modify it.
1918 * This may invalidate the current quickfix entry. This function checks
1919 * whether a entry is still present in the quickfix.
1920 * Similar to location list.
1921 */
1922 static int
1923is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1924{
1925 qf_list_T *qfl;
1926 qfline_T *qfp;
1927 int i;
1928
1929 qfl = &qi->qf_lists[qi->qf_curlist];
1930
1931 /* Search for the entry in the current list */
1932 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1933 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001934 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001935 break;
1936
1937 if (i == qfl->qf_count) /* Entry is not found */
1938 return FALSE;
1939
1940 return TRUE;
1941}
1942
1943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 * jump to a quickfix line
1945 * if dir == FORWARD go "errornr" valid entries forward
1946 * if dir == BACKWARD go "errornr" valid entries backward
1947 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1948 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1949 * else if "errornr" is zero, redisplay the same line
1950 * else go to entry "errornr"
1951 */
1952 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001953qf_jump(
1954 qf_info_T *qi,
1955 int dir,
1956 int errornr,
1957 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001959 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001960 qfline_T *qf_ptr;
1961 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 int qf_index;
1963 int old_qf_fnum;
1964 int old_qf_index;
1965 int prev_index;
1966 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1967 char_u *err = e_no_more_items;
1968 linenr_T i;
1969 buf_T *old_curbuf;
1970 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 colnr_T screen_col;
1972 colnr_T char_col;
1973 char_u *line;
1974#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001975 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001976 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 int opened_window = FALSE;
1978 win_T *win;
1979 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001980 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001982 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 int print_message = TRUE;
1984 int len;
1985#ifdef FEAT_FOLDING
1986 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1987#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001988 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001989 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001991 if (qi == NULL)
1992 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001993
1994 if (qi->qf_curlist >= qi->qf_listcount
1995 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
1997 EMSG(_(e_quickfix));
1998 return;
1999 }
2000
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002001 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002003 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 old_qf_index = qf_index;
2005 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
2006 {
2007 while (errornr--)
2008 {
2009 old_qf_ptr = qf_ptr;
2010 prev_index = qf_index;
2011 old_qf_fnum = qf_ptr->qf_fnum;
2012 do
2013 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002014 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 || qf_ptr->qf_next == NULL)
2016 {
2017 qf_ptr = old_qf_ptr;
2018 qf_index = prev_index;
2019 if (err != NULL)
2020 {
2021 EMSG(_(err));
2022 goto theend;
2023 }
2024 errornr = 0;
2025 break;
2026 }
2027 ++qf_index;
2028 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002029 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2030 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2032 err = NULL;
2033 }
2034 }
2035 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
2036 {
2037 while (errornr--)
2038 {
2039 old_qf_ptr = qf_ptr;
2040 prev_index = qf_index;
2041 old_qf_fnum = qf_ptr->qf_fnum;
2042 do
2043 {
2044 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
2045 {
2046 qf_ptr = old_qf_ptr;
2047 qf_index = prev_index;
2048 if (err != NULL)
2049 {
2050 EMSG(_(err));
2051 goto theend;
2052 }
2053 errornr = 0;
2054 break;
2055 }
2056 --qf_index;
2057 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002058 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2059 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2061 err = NULL;
2062 }
2063 }
2064 else if (errornr != 0) /* go to specified number */
2065 {
2066 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
2067 {
2068 --qf_index;
2069 qf_ptr = qf_ptr->qf_prev;
2070 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002071 while (errornr > qf_index && qf_index <
2072 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 && qf_ptr->qf_next != NULL)
2074 {
2075 ++qf_index;
2076 qf_ptr = qf_ptr->qf_next;
2077 }
2078 }
2079
2080#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002081 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2082 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 /* No need to print the error message if it's visible in the error
2084 * window */
2085 print_message = FALSE;
2086
2087 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002088 * For ":helpgrep" find a help window or open one.
2089 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002090 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002091 {
2092 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002093
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002094 if (cmdmod.tab != 0)
2095 wp = NULL;
2096 else
Bram Moolenaar29323592016-07-24 22:04:11 +02002097 FOR_ALL_WINDOWS(wp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002098 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
2099 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002100 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2101 win_enter(wp, TRUE);
2102 else
2103 {
2104 /*
2105 * Split off help window; put it at far top if no position
2106 * specified, the current window is vertically split and narrow.
2107 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00002108 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002109 if (cmdmod.split == 0 && curwin->w_width != Columns
2110 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00002111 flags |= WSP_TOP;
Bram Moolenaar884ae642009-02-22 01:37:59 +00002112 if (qi != &ql_info)
2113 flags |= WSP_NEWLOC; /* don't copy the location list */
2114
2115 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002116 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002117 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002118
2119 if (curwin->w_height < p_hh)
2120 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002121
2122 if (qi != &ql_info) /* not a quickfix list */
2123 {
2124 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002125 curwin->w_llist = qi;
2126 qi->qf_refcount++;
2127 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002128 }
2129
2130 if (!p_im)
2131 restart_edit = 0; /* don't want insert mode in help file */
2132 }
2133
2134 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 * If currently in the quickfix window, find another window to show the
2136 * file in.
2137 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002138 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaar24862852013-06-30 13:57:45 +02002140 win_T *usable_win_ptr = NULL;
2141
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 /*
2143 * If there is no file specified, we don't know where to go.
2144 * But do advance, otherwise ":cn" gets stuck.
2145 */
2146 if (qf_ptr->qf_fnum == 0)
2147 goto theend;
2148
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02002150
2151 ll_ref = curwin->w_llist_ref;
2152 if (ll_ref != NULL)
2153 {
2154 /* Find a window using the same location list that is not a
2155 * quickfix window. */
2156 FOR_ALL_WINDOWS(usable_win_ptr)
2157 if (usable_win_ptr->w_llist == ll_ref
2158 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002159 {
2160 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02002161 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02002162 }
Bram Moolenaar24862852013-06-30 13:57:45 +02002163 }
2164
2165 if (!usable_win)
2166 {
2167 /* Locate a window showing a normal buffer */
2168 FOR_ALL_WINDOWS(win)
2169 if (win->w_buffer->b_p_bt[0] == NUL)
2170 {
2171 usable_win = 1;
2172 break;
2173 }
2174 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00002177 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002178 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002180 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002181 {
2182 tabpage_T *tp;
2183 win_T *wp;
2184
2185 FOR_ALL_TAB_WINDOWS(tp, wp)
2186 {
2187 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
2188 {
2189 goto_tabpage_win(tp, wp);
2190 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002191 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002192 }
2193 }
2194 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002195win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002196
2197 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002198 * If there is only one window and it is the quickfix window, create a
2199 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00002200 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002201 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00002203 flags = WSP_ABOVE;
2204 if (ll_ref != NULL)
2205 flags |= WSP_NEWLOC;
2206 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 goto failed; /* not enough room for window */
2208 opened_window = TRUE; /* close it when fail */
2209 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00002210 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002211 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002212 if (ll_ref != NULL)
2213 {
2214 /* The new window should use the location list from the
2215 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002216 curwin->w_llist = ll_ref;
2217 ll_ref->qf_refcount++;
2218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 }
2220 else
2221 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002222 if (curwin->w_llist_ref != NULL)
2223 {
2224 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02002225 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002226 if (win == NULL)
2227 {
2228 /* Find the window showing the selected file */
2229 FOR_ALL_WINDOWS(win)
2230 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2231 break;
2232 if (win == NULL)
2233 {
2234 /* Find a previous usable window */
2235 win = curwin;
2236 do
2237 {
2238 if (win->w_buffer->b_p_bt[0] == NUL)
2239 break;
2240 if (win->w_prev == NULL)
2241 win = lastwin; /* wrap around the top */
2242 else
2243 win = win->w_prev; /* go to previous window */
2244 } while (win != curwin);
2245 }
2246 }
2247 win_goto(win);
2248
2249 /* If the location list for the window is not set, then set it
2250 * to the location list from the location window */
2251 if (win->w_llist == NULL)
2252 {
2253 win->w_llist = ll_ref;
2254 ll_ref->qf_refcount++;
2255 }
2256 }
2257 else
2258 {
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 /*
2261 * Try to find a window that shows the right buffer.
2262 * Default to the window just above the quickfix buffer.
2263 */
2264 win = curwin;
2265 altwin = NULL;
2266 for (;;)
2267 {
2268 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
2269 break;
2270 if (win->w_prev == NULL)
2271 win = lastwin; /* wrap around the top */
2272 else
2273 win = win->w_prev; /* go to previous window */
2274
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002275 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
2277 /* Didn't find it, go to the window before the quickfix
2278 * window. */
2279 if (altwin != NULL)
2280 win = altwin;
2281 else if (curwin->w_prev != NULL)
2282 win = curwin->w_prev;
2283 else
2284 win = curwin->w_next;
2285 break;
2286 }
2287
2288 /* Remember a usable window. */
2289 if (altwin == NULL && !win->w_p_pvw
2290 && win->w_buffer->b_p_bt[0] == NUL)
2291 altwin = win;
2292 }
2293
2294 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 }
2298#endif
2299
2300 /*
2301 * If there is a file name,
2302 * read the wanted file if needed, and check autowrite etc.
2303 */
2304 old_curbuf = curbuf;
2305 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002306
2307 if (qf_ptr->qf_fnum != 0)
2308 {
2309 if (qf_ptr->qf_type == 1)
2310 {
2311 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2312 * set b_p_ro flag). */
2313 if (!can_abandon(curbuf, forceit))
2314 {
2315 EMSG(_(e_nowrtmsg));
2316 ok = FALSE;
2317 }
2318 else
2319 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002320 ECMD_HIDE + ECMD_SET_HELP,
2321 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002322 }
2323 else
Bram Moolenaar0899d692016-03-19 13:35:03 +01002324 {
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002325 int old_qf_curlist = qi->qf_curlist;
2326 int is_abort = FALSE;
2327
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002328 ok = buflist_getfile(qf_ptr->qf_fnum,
2329 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02002330 if (qi != &ql_info && !win_valid_any_tab(oldwin))
Bram Moolenaar0899d692016-03-19 13:35:03 +01002331 {
2332 EMSG(_("E924: Current window was closed"));
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002333 is_abort = TRUE;
2334 opened_window = FALSE;
2335 }
2336 else if (old_qf_curlist != qi->qf_curlist
2337 || !is_qf_entry_present(qi, qf_ptr))
2338 {
2339 if (qi == &ql_info)
2340 EMSG(_("E925: Current quickfix was changed"));
2341 else
2342 EMSG(_("E926: Current location list was changed"));
2343 is_abort = TRUE;
2344 }
2345
2346 if (is_abort)
2347 {
Bram Moolenaar0899d692016-03-19 13:35:03 +01002348 ok = FALSE;
2349 qi = NULL;
2350 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002351 }
2352 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002353 }
2354
2355 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
2357 /* When not switched to another buffer, still need to set pc mark */
2358 if (curbuf == old_curbuf)
2359 setpcmark();
2360
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002361 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002363 /*
2364 * Go to line with error, unless qf_lnum is 0.
2365 */
2366 i = qf_ptr->qf_lnum;
2367 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002369 if (i > curbuf->b_ml.ml_line_count)
2370 i = curbuf->b_ml.ml_line_count;
2371 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002373 if (qf_ptr->qf_col > 0)
2374 {
2375 curwin->w_cursor.col = qf_ptr->qf_col - 1;
Bram Moolenaarb8c89002015-06-19 18:35:34 +02002376#ifdef FEAT_VIRTUALEDIT
2377 curwin->w_cursor.coladd = 0;
2378#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002379 if (qf_ptr->qf_viscol == TRUE)
2380 {
2381 /*
2382 * Check each character from the beginning of the error
2383 * line up to the error column. For each tab character
2384 * found, reduce the error column value by the length of
2385 * a tab character.
2386 */
2387 line = ml_get_curline();
2388 screen_col = 0;
2389 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2390 {
2391 if (*line == NUL)
2392 break;
2393 if (*line++ == '\t')
2394 {
2395 curwin->w_cursor.col -= 7 - (screen_col % 8);
2396 screen_col += 8 - (screen_col % 8);
2397 }
2398 else
2399 ++screen_col;
2400 }
2401 }
2402 check_cursor();
2403 }
2404 else
2405 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002408 {
2409 pos_T save_cursor;
2410
2411 /* Move the cursor to the first line in the buffer */
2412 save_cursor = curwin->w_cursor;
2413 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002414 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
2415 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002416 curwin->w_cursor = save_cursor;
2417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
2419#ifdef FEAT_FOLDING
2420 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2421 foldOpenCursor();
2422#endif
2423 if (print_message)
2424 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01002425 /* Update the screen before showing the message, unless the screen
2426 * scrolled up. */
2427 if (!msg_scrolled)
2428 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002430 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2432 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2433 /* Add the message, skipping leading whitespace and newlines. */
2434 len = (int)STRLEN(IObuff);
2435 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2436
2437 /* Output the message. Overwrite to avoid scrolling when the 'O'
2438 * flag is present in 'shortmess'; But when not jumping, print the
2439 * whole message. */
2440 i = msg_scroll;
2441 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2442 msg_scroll = TRUE;
2443 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2444 msg_scroll = FALSE;
2445 msg_attr_keep(IObuff, 0, TRUE);
2446 msg_scroll = i;
2447 }
2448 }
2449 else
2450 {
2451#ifdef FEAT_WINDOWS
2452 if (opened_window)
2453 win_close(curwin, TRUE); /* Close opened window */
2454#endif
Bram Moolenaar0899d692016-03-19 13:35:03 +01002455 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
2457 /*
2458 * Couldn't open file, so put index back where it was. This could
2459 * happen if the file was readonly and we changed something.
2460 */
2461#ifdef FEAT_WINDOWS
2462failed:
2463#endif
2464 qf_ptr = old_qf_ptr;
2465 qf_index = old_qf_index;
2466 }
2467 }
2468theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002469 if (qi != NULL)
2470 {
2471 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2472 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#ifdef FEAT_WINDOWS
2475 if (p_swb != old_swb && opened_window)
2476 {
2477 /* Restore old 'switchbuf' value, but not when an autocommand or
2478 * modeline has changed the value. */
2479 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002482 swb_flags = old_swb_flags;
2483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 else
2485 free_string_option(old_swb);
2486 }
2487#endif
2488}
2489
2490/*
2491 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002492 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 */
2494 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002495qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002497 buf_T *buf;
2498 char_u *fname;
2499 qfline_T *qfp;
2500 int i;
2501 int idx1 = 1;
2502 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002503 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002504 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002505 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002507 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002509 if (eap->cmdidx == CMD_llist)
2510 {
2511 qi = GET_LOC_LIST(curwin);
2512 if (qi == NULL)
2513 {
2514 EMSG(_(e_loclist));
2515 return;
2516 }
2517 }
2518
2519 if (qi->qf_curlist >= qi->qf_listcount
2520 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 EMSG(_(e_quickfix));
2523 return;
2524 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002525 if (*arg == '+')
2526 {
2527 ++arg;
2528 plus = TRUE;
2529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2531 {
2532 EMSG(_(e_trailing));
2533 return;
2534 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002535 if (plus)
2536 {
2537 i = qi->qf_lists[qi->qf_curlist].qf_index;
2538 idx2 = i + idx1;
2539 idx1 = i;
2540 }
2541 else
2542 {
2543 i = qi->qf_lists[qi->qf_curlist].qf_count;
2544 if (idx1 < 0)
2545 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2546 if (idx2 < 0)
2547 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002550 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002552 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2553 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2556 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002557 msg_putchar('\n');
2558 if (got_int)
2559 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002560
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002561 fname = NULL;
2562 if (qfp->qf_fnum != 0
2563 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2564 {
2565 fname = buf->b_fname;
2566 if (qfp->qf_type == 1) /* :helpgrep */
2567 fname = gettail(fname);
2568 }
2569 if (fname == NULL)
2570 sprintf((char *)IObuff, "%2d", i);
2571 else
2572 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2573 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002574 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar8820b482017-03-16 17:23:31 +01002575 ? HL_ATTR(HLF_L) : HL_ATTR(HLF_D));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002576 if (qfp->qf_lnum == 0)
2577 IObuff[0] = NUL;
2578 else if (qfp->qf_col == 0)
2579 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2580 else
2581 sprintf((char *)IObuff, ":%ld col %d",
2582 qfp->qf_lnum, qfp->qf_col);
2583 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2584 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar8820b482017-03-16 17:23:31 +01002585 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002586 if (qfp->qf_pattern != NULL)
2587 {
2588 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2589 STRCAT(IObuff, ":");
2590 msg_puts(IObuff);
2591 }
2592 msg_puts((char_u *)" ");
2593
2594 /* Remove newlines and leading whitespace from the text. For an
2595 * unrecognized line keep the indent, the compiler may mark a word
2596 * with ^^^^. */
2597 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2599 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002600 msg_prt_line(IObuff, FALSE);
2601 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002603
2604 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002605 if (qfp == NULL)
2606 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002607 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 ui_breakcheck();
2609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610}
2611
2612/*
2613 * Remove newlines and leading whitespace from an error message.
2614 * Put the result in "buf[bufsize]".
2615 */
2616 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002617qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
2619 int i;
2620 char_u *p = text;
2621
2622 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2623 {
2624 if (*p == '\n')
2625 {
2626 buf[i] = ' ';
2627 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002628 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 break;
2630 }
2631 else
2632 buf[i] = *p++;
2633 }
2634 buf[i] = NUL;
2635}
2636
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002637 static void
2638qf_msg(qf_info_T *qi, int which, char *lead)
2639{
2640 char *title = (char *)qi->qf_lists[which].qf_title;
2641 int count = qi->qf_lists[which].qf_count;
2642 char_u buf[IOSIZE];
2643
2644 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2645 lead,
2646 which + 1,
2647 qi->qf_listcount,
2648 count);
2649
2650 if (title != NULL)
2651 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002652 size_t len = STRLEN(buf);
2653
2654 if (len < 34)
2655 {
2656 vim_memset(buf + len, ' ', 34 - len);
2657 buf[34] = NUL;
2658 }
2659 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002660 }
2661 trunc_string(buf, buf, Columns - 1, IOSIZE);
2662 msg(buf);
2663}
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665/*
2666 * ":colder [count]": Up in the quickfix stack.
2667 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002668 * ":lolder [count]": Up in the location list stack.
2669 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 */
2671 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002672qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002674 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 int count;
2676
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002677 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2678 {
2679 qi = GET_LOC_LIST(curwin);
2680 if (qi == NULL)
2681 {
2682 EMSG(_(e_loclist));
2683 return;
2684 }
2685 }
2686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (eap->addr_count != 0)
2688 count = eap->line2;
2689 else
2690 count = 1;
2691 while (count--)
2692 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002693 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002695 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
2697 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002698 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002700 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702 else
2703 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002704 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {
2706 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002707 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002709 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002712 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02002714 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715#endif
2716}
2717
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002718 void
2719qf_history(exarg_T *eap)
2720{
2721 qf_info_T *qi = &ql_info;
2722 int i;
2723
2724 if (eap->cmdidx == CMD_lhistory)
2725 qi = GET_LOC_LIST(curwin);
2726 if (qi == NULL || (qi->qf_listcount == 0
2727 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2728 MSG(_("No entries"));
2729 else
2730 for (i = 0; i < qi->qf_listcount; ++i)
2731 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2732}
2733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002735 * Free all the entries in the error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 */
2737 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002738qf_free(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002740 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002741 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002742 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002744 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002746 qfp = qi->qf_lists[idx].qf_start;
2747 qfpnext = qfp->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002748 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002749 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002750 vim_free(qfp->qf_text);
2751 stop = (qfp == qfpnext);
2752 vim_free(qfp->qf_pattern);
2753 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002754 if (stop)
2755 /* Somehow qf_count may have an incorrect value, set it to 1
2756 * to avoid crashing when it's wrong.
2757 * TODO: Avoid qf_count being incorrect. */
2758 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002759 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002760 qi->qf_lists[idx].qf_start = qfpnext;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002761 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002763 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002764 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002765 free_tv(qi->qf_lists[idx].qf_ctx);
2766 qi->qf_lists[idx].qf_ctx = NULL;
Bram Moolenaar158a1b02014-07-23 16:33:07 +02002767 qi->qf_lists[idx].qf_index = 0;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002768 qi->qf_lists[idx].qf_start = NULL;
Bram Moolenaar31bdd132017-04-15 15:22:52 +02002769 qi->qf_lists[idx].qf_last = NULL;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002770 qi->qf_lists[idx].qf_ptr = NULL;
2771 qi->qf_lists[idx].qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002772
2773 qf_clean_dir_stack(&qi->qf_dir_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002774 qi->qf_directory = NULL;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002775 qf_clean_dir_stack(&qi->qf_file_stack);
Bram Moolenaar7618e002016-11-13 15:09:26 +01002776 qi->qf_currfile = NULL;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002777 qi->qf_multiline = FALSE;
2778 qi->qf_multiignore = FALSE;
2779 qi->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780}
2781
2782/*
2783 * qf_mark_adjust: adjust marks
2784 */
2785 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002786qf_mark_adjust(
2787 win_T *wp,
2788 linenr_T line1,
2789 linenr_T line2,
2790 long amount,
2791 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002793 int i;
2794 qfline_T *qfp;
2795 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002796 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002797 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002798 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
Bram Moolenaarc1542742016-07-20 21:44:37 +02002800 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002801 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002802 if (wp != NULL)
2803 {
2804 if (wp->w_llist == NULL)
2805 return;
2806 qi = wp->w_llist;
2807 }
2808
2809 for (idx = 0; idx < qi->qf_listcount; ++idx)
2810 if (qi->qf_lists[idx].qf_count)
2811 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002812 i < qi->qf_lists[idx].qf_count && qfp != NULL;
2813 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 if (qfp->qf_fnum == curbuf->b_fnum)
2815 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002816 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2818 {
2819 if (amount == MAXLNUM)
2820 qfp->qf_cleared = TRUE;
2821 else
2822 qfp->qf_lnum += amount;
2823 }
2824 else if (amount_after && qfp->qf_lnum > line2)
2825 qfp->qf_lnum += amount_after;
2826 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002827
2828 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002829 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830}
2831
2832/*
2833 * Make a nice message out of the error character and the error number:
2834 * char number message
2835 * e or E 0 " error"
2836 * w or W 0 " warning"
2837 * i or I 0 " info"
2838 * 0 0 ""
2839 * other 0 " c"
2840 * e or E n " error n"
2841 * w or W n " warning n"
2842 * i or I n " info n"
2843 * 0 n " error n"
2844 * other n " c n"
2845 * 1 x "" :helpgrep
2846 */
2847 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002848qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850 static char_u buf[20];
2851 static char_u cc[3];
2852 char_u *p;
2853
2854 if (c == 'W' || c == 'w')
2855 p = (char_u *)" warning";
2856 else if (c == 'I' || c == 'i')
2857 p = (char_u *)" info";
2858 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2859 p = (char_u *)" error";
2860 else if (c == 0 || c == 1)
2861 p = (char_u *)"";
2862 else
2863 {
2864 cc[0] = ' ';
2865 cc[1] = c;
2866 cc[2] = NUL;
2867 p = cc;
2868 }
2869
2870 if (nr <= 0)
2871 return p;
2872
2873 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2874 return buf;
2875}
2876
2877#if defined(FEAT_WINDOWS) || defined(PROTO)
2878/*
2879 * ":cwindow": open the quickfix window if we have errors to display,
2880 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002881 * ":lwindow": open the location list window if we have locations to display,
2882 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 */
2884 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002885ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002887 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 win_T *win;
2889
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 if (eap->cmdidx == CMD_lwindow)
2891 {
2892 qi = GET_LOC_LIST(curwin);
2893 if (qi == NULL)
2894 return;
2895 }
2896
2897 /* Look for an existing quickfix window. */
2898 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
2900 /*
2901 * If a quickfix window is open but we have no errors to display,
2902 * close the window. If a quickfix window is not open, then open
2903 * it if we have errors; otherwise, leave it closed.
2904 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002905 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002906 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002907 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
2909 if (win != NULL)
2910 ex_cclose(eap);
2911 }
2912 else if (win == NULL)
2913 ex_copen(eap);
2914}
2915
2916/*
2917 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002918 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002921ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002923 win_T *win = NULL;
2924 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002926 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2927 {
2928 qi = GET_LOC_LIST(curwin);
2929 if (qi == NULL)
2930 return;
2931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002933 /* Find existing quickfix window and close it. */
2934 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 if (win != NULL)
2936 win_close(win, FALSE);
2937}
2938
2939/*
2940 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002941 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 */
2943 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002944ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002946 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002949 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002950 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002951 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002953 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2954 {
2955 qi = GET_LOC_LIST(curwin);
2956 if (qi == NULL)
2957 {
2958 EMSG(_(e_loclist));
2959 return;
2960 }
2961 }
2962
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if (eap->addr_count != 0)
2964 height = eap->line2;
2965 else
2966 height = QF_WINHEIGHT;
2967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#ifdef FEAT_GUI
2970 need_mouse_correct = TRUE;
2971#endif
2972
2973 /*
2974 * Find existing quickfix window, or open a new one.
2975 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002976 win = qf_find_win(qi);
2977
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002978 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002981 if (eap->addr_count != 0)
2982 {
Bram Moolenaar15886412014-03-27 17:02:27 +01002983 if (cmdmod.split & WSP_VERT)
2984 {
2985 if (height != W_WIDTH(win))
2986 win_setwidth(height);
2987 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002988 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01002989 win_setheight(height);
2990 }
2991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 else
2993 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002994 qf_buf = qf_find_buf(qi);
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 /* The current window becomes the previous window afterwards. */
2997 win = curwin;
2998
Bram Moolenaar77642c02012-11-20 17:55:10 +01002999 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3000 && cmdmod.split == 0)
3001 /* Create the new window at the very bottom, except when
3002 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003003 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003004 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003006 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003008 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003010 /*
3011 * For the location list window, create a reference to the
3012 * location list from the window 'win'.
3013 */
3014 curwin->w_llist_ref = win->w_llist;
3015 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003017
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003018 if (oldwin != curwin)
3019 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003020 if (qf_buf != NULL)
3021 /* Use the existing quickfix buffer */
3022 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003023 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003024 else
3025 {
3026 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003027 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003028 /* switch off 'swapfile' */
3029 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3030 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003031 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003032 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003033 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003034#ifdef FEAT_DIFF
3035 curwin->w_p_diff = FALSE;
3036#endif
3037#ifdef FEAT_FOLDING
3038 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3039 OPT_LOCAL);
3040#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003043 /* Only set the height when still in the same tab page and there is no
3044 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003045 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 win_setheight(height);
3047 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3048 if (win_valid(win))
3049 prevwin = win;
3050 }
3051
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003052 qf_set_title_var(qi);
3053
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 /*
3055 * Fill the buffer with the quickfix list.
3056 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003057 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003059 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 curwin->w_cursor.col = 0;
3061 check_cursor();
3062 update_topline(); /* scroll to show the line */
3063}
3064
3065/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003066 * Move the cursor in the quickfix window to "lnum".
3067 */
3068 static void
3069qf_win_goto(win_T *win, linenr_T lnum)
3070{
3071 win_T *old_curwin = curwin;
3072
3073 curwin = win;
3074 curbuf = win->w_buffer;
3075 curwin->w_cursor.lnum = lnum;
3076 curwin->w_cursor.col = 0;
3077#ifdef FEAT_VIRTUALEDIT
3078 curwin->w_cursor.coladd = 0;
3079#endif
3080 curwin->w_curswant = 0;
3081 update_topline(); /* scroll to show the line */
3082 redraw_later(VALID);
3083 curwin->w_redr_status = TRUE; /* update ruler */
3084 curwin = old_curwin;
3085 curbuf = curwin->w_buffer;
3086}
3087
3088/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003089 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003090 */
3091 void
3092ex_cbottom(exarg_T *eap UNUSED)
3093{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003094 qf_info_T *qi = &ql_info;
3095 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003096
Bram Moolenaar537ef082016-07-09 17:56:19 +02003097 if (eap->cmdidx == CMD_lbottom)
3098 {
3099 qi = GET_LOC_LIST(curwin);
3100 if (qi == NULL)
3101 {
3102 EMSG(_(e_loclist));
3103 return;
3104 }
3105 }
3106
3107 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003108 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3109 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3110}
3111
3112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 * Return the number of the current entry (line number in the quickfix
3114 * window).
3115 */
3116 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003117qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003119 qf_info_T *qi = &ql_info;
3120
3121 if (IS_LL_WINDOW(wp))
3122 /* In the location list window, use the referenced location list */
3123 qi = wp->w_llist_ref;
3124
3125 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126}
3127
3128/*
3129 * Update the cursor position in the quickfix window to the current error.
3130 * Return TRUE if there is a quickfix window.
3131 */
3132 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003133qf_win_pos_update(
3134 qf_info_T *qi,
3135 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
3137 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003138 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 /*
3141 * Put the cursor on the current error in the quickfix window, so that
3142 * it's viewable.
3143 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003144 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (win != NULL
3146 && qf_index <= win->w_buffer->b_ml.ml_line_count
3147 && old_qf_index != qf_index)
3148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (qf_index > old_qf_index)
3150 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003151 win->w_redraw_top = old_qf_index;
3152 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 }
3154 else
3155 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003156 win->w_redraw_top = qf_index;
3157 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003159 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
3161 return win != NULL;
3162}
3163
3164/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003165 * Check whether the given window is displaying the specified quickfix/location
3166 * list buffer
3167 */
3168 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003169is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003170{
3171 /*
3172 * A window displaying the quickfix buffer will have the w_llist_ref field
3173 * set to NULL.
3174 * A window displaying a location list buffer will have the w_llist_ref
3175 * pointing to the location list.
3176 */
3177 if (bt_quickfix(win->w_buffer))
3178 if ((qi == &ql_info && win->w_llist_ref == NULL)
3179 || (qi != &ql_info && win->w_llist_ref == qi))
3180 return TRUE;
3181
3182 return FALSE;
3183}
3184
3185/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003186 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003187 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003188 */
3189 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003190qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003191{
3192 win_T *win;
3193
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003194 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003195 if (is_qf_win(win, qi))
3196 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003197
3198 return win;
3199}
3200
3201/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003202 * Find a quickfix buffer.
3203 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 */
3205 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003206qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003208 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003209 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
Bram Moolenaar9c102382006-05-03 21:26:49 +00003211 FOR_ALL_TAB_WINDOWS(tp, win)
3212 if (is_qf_win(win, qi))
3213 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003214
Bram Moolenaar9c102382006-05-03 21:26:49 +00003215 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216}
3217
3218/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003219 * Update the w:quickfix_title variable in the quickfix/location list window
3220 */
3221 static void
3222qf_update_win_titlevar(qf_info_T *qi)
3223{
3224 win_T *win;
3225 win_T *curwin_save;
3226
3227 if ((win = qf_find_win(qi)) != NULL)
3228 {
3229 curwin_save = curwin;
3230 curwin = win;
3231 qf_set_title_var(qi);
3232 curwin = curwin_save;
3233 }
3234}
3235
3236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 * Find the quickfix buffer. If it exists, update the contents.
3238 */
3239 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003240qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
3242 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003243 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
3246 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003247 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (buf != NULL)
3249 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003250 linenr_T old_line_count = buf->b_ml.ml_line_count;
3251
3252 if (old_last == NULL)
3253 /* set curwin/curbuf to buf and save a few things */
3254 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaard823fa92016-08-12 16:29:27 +02003256 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003257
Bram Moolenaar864293a2016-06-02 13:40:04 +02003258 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003259
Bram Moolenaar864293a2016-06-02 13:40:04 +02003260 if (old_last == NULL)
3261 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003262 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003263
3264 /* restore curwin/curbuf and a few other things */
3265 aucmd_restbuf(&aco);
3266 }
3267
3268 /* Only redraw when added lines are visible. This avoids flickering
3269 * when the added lines are not visible. */
3270 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3271 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 }
3273}
3274
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003275/*
3276 * Set "w:quickfix_title" if "qi" has a title.
3277 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003278 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003279qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003280{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003281 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3282 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003283 qi->qf_lists[qi->qf_curlist].qf_title);
3284}
3285
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286/*
3287 * Fill current buffer with quickfix errors, replacing any previous contents.
3288 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003289 * If "old_last" is not NULL append the items after this one.
3290 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3291 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 */
3293 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003294qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003296 linenr_T lnum;
3297 qfline_T *qfp;
3298 buf_T *errbuf;
3299 int len;
3300 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301
Bram Moolenaar864293a2016-06-02 13:40:04 +02003302 if (old_last == NULL)
3303 {
3304 if (buf != curbuf)
3305 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003306 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003307 return;
3308 }
3309
3310 /* delete all existing lines */
3311 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3312 (void)ml_delete((linenr_T)1, FALSE);
3313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
3315 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003316 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
3318 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003319 if (old_last == NULL)
3320 {
3321 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3322 lnum = 0;
3323 }
3324 else
3325 {
3326 qfp = old_last->qf_next;
3327 lnum = buf->b_ml.ml_line_count;
3328 }
3329 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
3331 if (qfp->qf_fnum != 0
3332 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3333 && errbuf->b_fname != NULL)
3334 {
3335 if (qfp->qf_type == 1) /* :helpgrep */
3336 STRCPY(IObuff, gettail(errbuf->b_fname));
3337 else
3338 STRCPY(IObuff, errbuf->b_fname);
3339 len = (int)STRLEN(IObuff);
3340 }
3341 else
3342 len = 0;
3343 IObuff[len++] = '|';
3344
3345 if (qfp->qf_lnum > 0)
3346 {
3347 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3348 len += (int)STRLEN(IObuff + len);
3349
3350 if (qfp->qf_col > 0)
3351 {
3352 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3353 len += (int)STRLEN(IObuff + len);
3354 }
3355
3356 sprintf((char *)IObuff + len, "%s",
3357 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3358 len += (int)STRLEN(IObuff + len);
3359 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003360 else if (qfp->qf_pattern != NULL)
3361 {
3362 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3363 len += (int)STRLEN(IObuff + len);
3364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 IObuff[len++] = '|';
3366 IObuff[len++] = ' ';
3367
3368 /* Remove newlines and leading whitespace from the text.
3369 * For an unrecognized line keep the indent, the compiler may
3370 * mark a word with ^^^^. */
3371 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3372 IObuff + len, IOSIZE - len);
3373
Bram Moolenaar864293a2016-06-02 13:40:04 +02003374 if (ml_append_buf(buf, lnum, IObuff,
3375 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003377 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003379 if (qfp == NULL)
3380 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003382
3383 if (old_last == NULL)
3384 /* Delete the empty line which is now at the end */
3385 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387
3388 /* correct cursor position */
3389 check_lnums(TRUE);
3390
Bram Moolenaar864293a2016-06-02 13:40:04 +02003391 if (old_last == NULL)
3392 {
3393 /* Set the 'filetype' to "qf" each time after filling the buffer.
3394 * This resembles reading a file into a buffer, it's more logical when
3395 * using autocommands. */
3396 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3397 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
3399#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003400 keep_filetype = TRUE; /* don't detect 'filetype' */
3401 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003403 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003405 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003407 /* make sure it will be redrawn */
3408 redraw_curbuf_later(NOT_VALID);
3409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
3411 /* Restore KeyTyped, setting 'filetype' may reset it. */
3412 KeyTyped = old_KeyTyped;
3413}
3414
3415#endif /* FEAT_WINDOWS */
3416
3417/*
3418 * Return TRUE if "buf" is the quickfix buffer.
3419 */
3420 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003421bt_quickfix(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003423 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424}
3425
3426/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003427 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
3428 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 */
3430 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003431bt_nofile(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003433 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
3434 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435}
3436
3437/*
3438 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
3439 */
3440 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003441bt_dontwrite(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
Bram Moolenaarfc573802011-12-30 15:01:59 +01003443 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444}
3445
3446 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003447bt_dontwrite_msg(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 if (bt_dontwrite(buf))
3450 {
3451 EMSG(_("E382: Cannot write, 'buftype' option is set"));
3452 return TRUE;
3453 }
3454 return FALSE;
3455}
3456
3457/*
3458 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
3459 * and 'bufhidden'.
3460 */
3461 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003462buf_hide(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
3464 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
3465 switch (buf->b_p_bh[0])
3466 {
3467 case 'u': /* "unload" */
3468 case 'w': /* "wipe" */
3469 case 'd': return FALSE; /* "delete" */
3470 case 'h': return TRUE; /* "hide" */
3471 }
3472 return (p_hid || cmdmod.hide);
3473}
3474
3475/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003476 * Return TRUE when using ":vimgrep" for ":grep".
3477 */
3478 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003479grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003480{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003481 return ((cmdidx == CMD_grep
3482 || cmdidx == CMD_lgrep
3483 || cmdidx == CMD_grepadd
3484 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003485 && STRCMP("internal",
3486 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3487}
3488
3489/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003490 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
3492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003493ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003495 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003497 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003499 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003500 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003501 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003502#ifdef FEAT_AUTOCMD
3503 char_u *au_name = NULL;
3504
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003505 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3506 if (grep_internal(eap->cmdidx))
3507 {
3508 ex_vimgrep(eap);
3509 return;
3510 }
3511
Bram Moolenaar7c626922005-02-07 22:01:03 +00003512 switch (eap->cmdidx)
3513 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003514 case CMD_make: au_name = (char_u *)"make"; break;
3515 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3516 case CMD_grep: au_name = (char_u *)"grep"; break;
3517 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3518 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3519 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003520 default: break;
3521 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003522 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3523 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003524 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003525# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003526 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003527 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003528# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003529 }
3530#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003531#ifdef FEAT_MBYTE
3532 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaara6557602006-02-04 22:43:20 +00003535 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3536 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003537 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003540 fname = get_mef_name();
3541 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003543 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
3545 /*
3546 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3547 */
3548 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3549 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003550 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 cmd = alloc(len);
3552 if (cmd == NULL)
3553 return;
3554 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3555 (char *)p_shq);
3556 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003557 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 /*
3559 * Output a newline if there's something else than the :make command that
3560 * was typed (in which case the cursor is in column 0).
3561 */
3562 if (msg_col == 0)
3563 msg_didout = FALSE;
3564 msg_start();
3565 MSG_PUTS(":!");
3566 msg_outtrans(cmd); /* show what we are doing */
3567
3568 /* let the shell know if we are redirecting output or not */
3569 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3570
3571#ifdef AMIGA
3572 out_flush();
3573 /* read window status report and redraw before message */
3574 (void)char_avail();
3575#endif
3576
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003577 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003578 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3579 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003580 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003581 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003582 if (wp != NULL)
3583 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003584#ifdef FEAT_AUTOCMD
3585 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003586 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003587 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3588 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003589 if (qi->qf_curlist < qi->qf_listcount)
3590 res = qi->qf_lists[qi->qf_curlist].qf_count;
3591 else
3592 res = 0;
3593 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003594#endif
3595 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003596 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar7c626922005-02-07 22:01:03 +00003598 mch_remove(fname);
3599 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 vim_free(cmd);
3601}
3602
3603/*
3604 * Return the name for the errorfile, in allocated memory.
3605 * Find a new unique name when 'makeef' contains "##".
3606 * Returns NULL for error.
3607 */
3608 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003609get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611 char_u *p;
3612 char_u *name;
3613 static int start = -1;
3614 static int off = 0;
3615#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003616 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#endif
3618
3619 if (*p_mef == NUL)
3620 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003621 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 if (name == NULL)
3623 EMSG(_(e_notmp));
3624 return name;
3625 }
3626
3627 for (p = p_mef; *p; ++p)
3628 if (p[0] == '#' && p[1] == '#')
3629 break;
3630
3631 if (*p == NUL)
3632 return vim_strsave(p_mef);
3633
3634 /* Keep trying until the name doesn't exist yet. */
3635 for (;;)
3636 {
3637 if (start == -1)
3638 start = mch_get_pid();
3639 else
3640 off += 19;
3641
3642 name = alloc((unsigned)STRLEN(p_mef) + 30);
3643 if (name == NULL)
3644 break;
3645 STRCPY(name, p_mef);
3646 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3647 STRCAT(name, p + 2);
3648 if (mch_getperm(name) < 0
3649#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003650 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 && mch_lstat((char *)name, &sb) < 0
3652#endif
3653 )
3654 break;
3655 vim_free(name);
3656 }
3657 return name;
3658}
3659
3660/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003661 * Returns the number of valid entries in the current quickfix/location list.
3662 */
3663 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003664qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003665{
3666 qf_info_T *qi = &ql_info;
3667 qfline_T *qfp;
3668 int i, sz = 0;
3669 int prev_fnum = 0;
3670
3671 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3672 {
3673 /* Location list */
3674 qi = GET_LOC_LIST(curwin);
3675 if (qi == NULL)
3676 return 0;
3677 }
3678
3679 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003680 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003681 ++i, qfp = qfp->qf_next)
3682 {
3683 if (qfp->qf_valid)
3684 {
3685 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3686 sz++; /* Count all valid entries */
3687 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3688 {
3689 /* Count the number of files */
3690 sz++;
3691 prev_fnum = qfp->qf_fnum;
3692 }
3693 }
3694 }
3695
3696 return sz;
3697}
3698
3699/*
3700 * Returns the current index of the quickfix/location list.
3701 * Returns 0 if there is an error.
3702 */
3703 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003704qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003705{
3706 qf_info_T *qi = &ql_info;
3707
3708 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3709 {
3710 /* Location list */
3711 qi = GET_LOC_LIST(curwin);
3712 if (qi == NULL)
3713 return 0;
3714 }
3715
3716 return qi->qf_lists[qi->qf_curlist].qf_index;
3717}
3718
3719/*
3720 * Returns the current index in the quickfix/location list (counting only valid
3721 * entries). If no valid entries are in the list, then returns 1.
3722 */
3723 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003724qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003725{
3726 qf_info_T *qi = &ql_info;
3727 qf_list_T *qfl;
3728 qfline_T *qfp;
3729 int i, eidx = 0;
3730 int prev_fnum = 0;
3731
3732 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3733 {
3734 /* Location list */
3735 qi = GET_LOC_LIST(curwin);
3736 if (qi == NULL)
3737 return 1;
3738 }
3739
3740 qfl = &qi->qf_lists[qi->qf_curlist];
3741 qfp = qfl->qf_start;
3742
3743 /* check if the list has valid errors */
3744 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3745 return 1;
3746
3747 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3748 {
3749 if (qfp->qf_valid)
3750 {
3751 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3752 {
3753 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3754 {
3755 /* Count the number of files */
3756 eidx++;
3757 prev_fnum = qfp->qf_fnum;
3758 }
3759 }
3760 else
3761 eidx++;
3762 }
3763 }
3764
3765 return eidx ? eidx : 1;
3766}
3767
3768/*
3769 * Get the 'n'th valid error entry in the quickfix or location list.
3770 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3771 * For :cdo and :ldo returns the 'n'th valid error entry.
3772 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3773 */
3774 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003775qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003776{
3777 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3778 qfline_T *qfp = qfl->qf_start;
3779 int i, eidx;
3780 int prev_fnum = 0;
3781
3782 /* check if the list has valid errors */
3783 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3784 return 1;
3785
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003786 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003787 i++, qfp = qfp->qf_next)
3788 {
3789 if (qfp->qf_valid)
3790 {
3791 if (fdo)
3792 {
3793 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3794 {
3795 /* Count the number of files */
3796 eidx++;
3797 prev_fnum = qfp->qf_fnum;
3798 }
3799 }
3800 else
3801 eidx++;
3802 }
3803
3804 if (eidx == n)
3805 break;
3806 }
3807
3808 if (i <= qfl->qf_count)
3809 return i;
3810 else
3811 return 1;
3812}
3813
3814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003816 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003817 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 */
3819 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003820ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003822 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003823 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003824
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003825 if (eap->cmdidx == CMD_ll
3826 || eap->cmdidx == CMD_lrewind
3827 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003828 || eap->cmdidx == CMD_llast
3829 || eap->cmdidx == CMD_ldo
3830 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003831 {
3832 qi = GET_LOC_LIST(curwin);
3833 if (qi == NULL)
3834 {
3835 EMSG(_(e_loclist));
3836 return;
3837 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003838 }
3839
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003840 if (eap->addr_count > 0)
3841 errornr = (int)eap->line2;
3842 else
3843 {
3844 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3845 errornr = 0;
3846 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3847 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3848 errornr = 1;
3849 else
3850 errornr = 32767;
3851 }
3852
3853 /* For cdo and ldo commands, jump to the nth valid error.
3854 * For cfdo and lfdo commands, jump to the nth valid file entry.
3855 */
3856 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
3857 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3858 errornr = qf_get_nth_valid_entry(qi,
3859 eap->addr_count > 0 ? (int)eap->line1 : 1,
3860 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3861
3862 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863}
3864
3865/*
3866 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003867 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003868 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 */
3870 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003871ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003873 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003874 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003875
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003876 if (eap->cmdidx == CMD_lnext
3877 || eap->cmdidx == CMD_lNext
3878 || eap->cmdidx == CMD_lprevious
3879 || eap->cmdidx == CMD_lnfile
3880 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003881 || eap->cmdidx == CMD_lpfile
3882 || eap->cmdidx == CMD_ldo
3883 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003884 {
3885 qi = GET_LOC_LIST(curwin);
3886 if (qi == NULL)
3887 {
3888 EMSG(_(e_loclist));
3889 return;
3890 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003891 }
3892
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003893 if (eap->addr_count > 0 &&
3894 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo &&
3895 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
3896 errornr = (int)eap->line2;
3897 else
3898 errornr = 1;
3899
3900 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
3901 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003903 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
3904 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003906 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3907 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 ? BACKWARD_FILE
3909 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003910 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911}
3912
3913/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003914 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003915 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 */
3917 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003918ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003920 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003921 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003922 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003923#ifdef FEAT_AUTOCMD
3924 char_u *au_name = NULL;
3925#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003926
3927 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003928 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003929 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003930
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003931#ifdef FEAT_AUTOCMD
3932 switch (eap->cmdidx)
3933 {
3934 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3935 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3936 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3937 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3938 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3939 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3940 default: break;
3941 }
3942 if (au_name != NULL)
3943 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3944#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003945#ifdef FEAT_MBYTE
3946 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3947#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003948#ifdef FEAT_BROWSE
3949 if (cmdmod.browse)
3950 {
3951 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3952 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3953 if (browse_file == NULL)
3954 return;
3955 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3956 vim_free(browse_file);
3957 }
3958 else
3959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003961 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003962
3963 /*
3964 * This function is used by the :cfile, :cgetfile and :caddfile
3965 * commands.
3966 * :cfile always creates a new quickfix list and jumps to the
3967 * first error.
3968 * :cgetfile creates a new quickfix list but doesn't jump to the
3969 * first error.
3970 * :caddfile adds to an existing quickfix list. If there is no
3971 * quickfix list then a new list is created.
3972 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003973 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003974 && eap->cmdidx != CMD_laddfile),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003975 *eap->cmdlinep, enc) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003976 && (eap->cmdidx == CMD_cfile
3977 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003978 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003979#ifdef FEAT_AUTOCMD
3980 if (au_name != NULL)
3981 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3982#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003983 if (wp != NULL)
3984 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003985 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003986 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003987
3988 else
3989 {
3990#ifdef FEAT_AUTOCMD
3991 if (au_name != NULL)
3992 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3993#endif
3994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995}
3996
3997/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003998 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003999 * ":vimgrepadd {pattern} file(s)"
4000 * ":lvimgrep {pattern} file(s)"
4001 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004002 */
4003 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004004ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004005{
Bram Moolenaar81695252004-12-29 20:58:21 +00004006 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004007 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004008 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004009 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004010 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004011 char_u *s;
4012 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004013 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004014 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004015#ifdef FEAT_AUTOCMD
4016 qfline_T *cur_qf_start;
4017#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004018 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004019 buf_T *buf;
4020 int duplicate_name = FALSE;
4021 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004022 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004023 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004024 buf_T *first_match_buf = NULL;
4025 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004026 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004027#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4028 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004029#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004030 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004031 int flags = 0;
4032 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004033 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004034 char_u *dirname_start = NULL;
4035 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004036 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004037#ifdef FEAT_AUTOCMD
4038 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004039
4040 switch (eap->cmdidx)
4041 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004042 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4043 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4044 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004045 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004046 case CMD_grep: au_name = (char_u *)"grep"; break;
4047 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4048 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4049 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004050 default: break;
4051 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004052 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4053 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004054 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004055# ifdef FEAT_EVAL
4056 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004057 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004058# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004059 }
4060#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004061
Bram Moolenaar754b5602006-02-09 23:53:20 +00004062 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004063 || eap->cmdidx == CMD_lvimgrep
4064 || eap->cmdidx == CMD_lgrepadd
4065 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004066 {
4067 qi = ll_get_or_alloc_list(curwin);
4068 if (qi == NULL)
4069 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004070 }
4071
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004072 if (eap->addr_count > 0)
4073 tomatch = eap->line2;
4074 else
4075 tomatch = MAXLNUM;
4076
Bram Moolenaar81695252004-12-29 20:58:21 +00004077 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004078 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004079 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004080 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004081 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004082 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004083 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004084 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004085 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004086
4087 if (s != NULL && *s == NUL)
4088 {
4089 /* Pattern is empty, use last search pattern. */
4090 if (last_search_pat() == NULL)
4091 {
4092 EMSG(_(e_noprevre));
4093 goto theend;
4094 }
4095 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4096 }
4097 else
4098 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4099
Bram Moolenaar86b68352004-12-27 21:59:20 +00004100 if (regmatch.regprog == NULL)
4101 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004102 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004103 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004104
4105 p = skipwhite(p);
4106 if (*p == NUL)
4107 {
4108 EMSG(_("E683: File name missing or invalid pattern"));
4109 goto theend;
4110 }
4111
Bram Moolenaar754b5602006-02-09 23:53:20 +00004112 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00004113 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004114 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004115 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004116 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004117
4118 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004119 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004120 goto theend;
4121 if (fcount == 0)
4122 {
4123 EMSG(_(e_nomatch));
4124 goto theend;
4125 }
4126
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004127 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4128 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004129 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004130 {
4131 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004132 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004133 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004134
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004135 /* Remember the current directory, because a BufRead autocommand that does
4136 * ":lcd %:p:h" changes the meaning of short path names. */
4137 mch_dirname(dirname_start, MAXPATHL);
4138
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004139#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004140 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004141 * changing the current quickfix list. */
4142 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4143#endif
4144
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004145 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004146 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004147 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004148 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004149 if (time(NULL) > seconds)
4150 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004151 /* Display the file name every second or so, show the user we are
4152 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004153 seconds = time(NULL);
4154 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004155 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004156 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004157 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004158 else
4159 {
4160 msg_outtrans(p);
4161 vim_free(p);
4162 }
4163 msg_clr_eos();
4164 msg_didout = FALSE; /* overwrite this message */
4165 msg_nowait = TRUE; /* don't wait for this message */
4166 msg_col = 0;
4167 out_flush();
4168 }
4169
Bram Moolenaar81695252004-12-29 20:58:21 +00004170 buf = buflist_findname_exp(fnames[fi]);
4171 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4172 {
4173 /* Remember that a buffer with this name already exists. */
4174 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004175 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004176 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004177
4178#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4179 /* Don't do Filetype autocommands to avoid loading syntax and
4180 * indent scripts, a great speed improvement. */
4181 save_ei = au_event_disable(",Filetype");
4182#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004183 /* Don't use modelines here, it's useless. */
4184 save_mls = p_mls;
4185 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004186
4187 /* Load file into a buffer, so that 'fileencoding' is detected,
4188 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004189 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004190
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004191 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004192#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4193 au_event_restore(save_ei);
4194#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004195 }
4196 else
4197 /* Use existing, loaded buffer. */
4198 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004199
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004200#ifdef FEAT_AUTOCMD
4201 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4202 {
4203 int idx;
4204
4205 /* Autocommands changed the quickfix list. Find the one we were
4206 * using and restore it. */
4207 for (idx = 0; idx < LISTCOUNT; ++idx)
4208 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4209 {
4210 qi->qf_curlist = idx;
4211 break;
4212 }
4213 if (idx == LISTCOUNT)
4214 {
4215 /* List cannot be found, create a new one. */
4216 qf_new_list(qi, *eap->cmdlinep);
4217 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4218 }
4219 }
4220#endif
4221
Bram Moolenaar81695252004-12-29 20:58:21 +00004222 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004223 {
4224 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004225 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004226 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004227 else
4228 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004229 /* Try for a match in all lines of the buffer.
4230 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004231 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004232 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4233 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004234 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004235 col = 0;
4236 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00004237 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004238 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004239 /* Pass the buffer number so that it gets used even for a
4240 * dummy buffer, unless duplicate_name is set, then the
4241 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004242 if (qf_add_entry(qi,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004243 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004244 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004245 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004246 ml_get_buf(buf,
4247 regmatch.startpos[0].lnum + lnum, FALSE),
4248 regmatch.startpos[0].lnum + lnum,
4249 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004250 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004251 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004252 0, /* nr */
4253 0, /* type */
4254 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004255 ) == FAIL)
4256 {
4257 got_int = TRUE;
4258 break;
4259 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004260 found_match = TRUE;
4261 if (--tomatch == 0)
4262 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004263 if ((flags & VGR_GLOBAL) == 0
4264 || regmatch.endpos[0].lnum > 0)
4265 break;
4266 col = regmatch.endpos[0].col
4267 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004268 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004269 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004270 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004271 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004272 if (got_int)
4273 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004274 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004275#ifdef FEAT_AUTOCMD
4276 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4277#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004278
4279 if (using_dummy)
4280 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004281 if (found_match && first_match_buf == NULL)
4282 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004283 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004284 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004285 /* Never keep a dummy buffer if there is another buffer
4286 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004287 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004288 buf = NULL;
4289 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004290 else if (!cmdmod.hide
4291 || buf->b_p_bh[0] == 'u' /* "unload" */
4292 || buf->b_p_bh[0] == 'w' /* "wipe" */
4293 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004294 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004295 /* When no match was found we don't need to remember the
4296 * buffer, wipe it out. If there was a match and it
4297 * wasn't the first one or we won't jump there: only
4298 * unload the buffer.
4299 * Ignore 'hidden' here, because it may lead to having too
4300 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004301 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004302 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004303 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004304 buf = NULL;
4305 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004306 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004307 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004308 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004309 /* Keeping the buffer, remove the dummy flag. */
4310 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004311 buf = NULL;
4312 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004313 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004314
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004315 if (buf != NULL)
4316 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004317 /* Keeping the buffer, remove the dummy flag. */
4318 buf->b_flags &= ~BF_DUMMY;
4319
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004320 /* If the buffer is still loaded we need to use the
4321 * directory we jumped to below. */
4322 if (buf == first_match_buf
4323 && target_dir == NULL
4324 && STRCMP(dirname_start, dirname_now) != 0)
4325 target_dir = vim_strsave(dirname_now);
4326
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004327 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004328 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004329 * need to be done (again). But not the window-local
4330 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004331 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004332#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004333 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4334 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004335#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004336 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004337 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004338 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004339 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004340 }
4341 }
4342
4343 FreeWild(fcount, fnames);
4344
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004345 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4346 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4347 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004348
4349#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02004350 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004351#endif
4352
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004353#ifdef FEAT_AUTOCMD
4354 if (au_name != NULL)
4355 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4356 curbuf->b_fname, TRUE, curbuf);
4357#endif
4358
Bram Moolenaar86b68352004-12-27 21:59:20 +00004359 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004360 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004361 {
4362 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004363 {
4364 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004365 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004366 if (buf != curbuf)
4367 /* If we jumped to another buffer redrawing will already be
4368 * taken care of. */
4369 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004370
4371 /* Jump to the directory used after loading the buffer. */
4372 if (curbuf == first_match_buf && target_dir != NULL)
4373 {
4374 exarg_T ea;
4375
4376 ea.arg = target_dir;
4377 ea.cmdidx = CMD_lcd;
4378 ex_cd(&ea);
4379 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004380 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004381 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004382 else
4383 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004384
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004385 /* If we loaded a dummy buffer into the current window, the autocommands
4386 * may have messed up things, need to redraw and recompute folds. */
4387 if (redraw_for_dummy)
4388 {
4389#ifdef FEAT_FOLDING
4390 foldUpdateAll(curwin);
4391#else
4392 redraw_later(NOT_VALID);
4393#endif
4394 }
4395
Bram Moolenaar86b68352004-12-27 21:59:20 +00004396theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004397 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004398 vim_free(dirname_now);
4399 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004400 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004401 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004402}
4403
4404/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004405 * Restore current working directory to "dirname_start" if they differ, taking
4406 * into account whether it is set locally or globally.
4407 */
4408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004409restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004410{
4411 char_u *dirname_now = alloc(MAXPATHL);
4412
4413 if (NULL != dirname_now)
4414 {
4415 mch_dirname(dirname_now, MAXPATHL);
4416 if (STRCMP(dirname_start, dirname_now) != 0)
4417 {
4418 /* If the directory has changed, change it back by building up an
4419 * appropriate ex command and executing it. */
4420 exarg_T ea;
4421
4422 ea.arg = dirname_start;
4423 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4424 ex_cd(&ea);
4425 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004426 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004427 }
4428}
4429
4430/*
4431 * Load file "fname" into a dummy buffer and return the buffer pointer,
4432 * placing the directory resulting from the buffer load into the
4433 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4434 * prior to calling this function. Restores directory to "dirname_start" prior
4435 * to returning, if autocmds or the 'autochdir' option have changed it.
4436 *
4437 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4438 * or wipe_dummy_buffer() later!
4439 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004440 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004441 */
4442 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004443load_dummy_buffer(
4444 char_u *fname,
4445 char_u *dirname_start, /* in: old directory */
4446 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004447{
4448 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004449 bufref_T newbufref;
4450 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004451 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004452 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00004453
4454 /* Allocate a buffer without putting it in the buffer list. */
4455 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4456 if (newbuf == NULL)
4457 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004458 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004459
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004460 /* Init the options. */
4461 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4462
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004463 /* need to open the memfile before putting the buffer in a window */
4464 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004465 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004466 /* set curwin/curbuf to buf and save a few things */
4467 aucmd_prepbuf(&aco, newbuf);
4468
4469 /* Need to set the filename for autocommands. */
4470 (void)setfname(curbuf, fname, NULL, FALSE);
4471
Bram Moolenaar81695252004-12-29 20:58:21 +00004472 /* Create swap file now to avoid the ATTENTION message. */
4473 check_need_swap(TRUE);
4474
4475 /* Remove the "dummy" flag, otherwise autocommands may not
4476 * work. */
4477 curbuf->b_flags &= ~BF_DUMMY;
4478
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004479 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004480 if (readfile(fname, NULL,
4481 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
4482 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004483 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004484 && !(curbuf->b_flags & BF_NEW))
4485 {
4486 failed = FALSE;
4487 if (curbuf != newbuf)
4488 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004489 /* Bloody autocommands changed the buffer! Can happen when
4490 * using netrw and editing a remote file. Use the current
4491 * buffer instead, delete the dummy one after restoring the
4492 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004493 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004494 newbuf = curbuf;
4495 }
4496 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004497
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004498 /* restore curwin/curbuf and a few other things */
4499 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004500 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4501 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004502
4503 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4504 * skip it. */
4505 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004506 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004507
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004508 /*
4509 * When autocommands/'autochdir' option changed directory: go back.
4510 * Let the caller know what the resulting dir was first, in case it is
4511 * important.
4512 */
4513 mch_dirname(resulting_dir, MAXPATHL);
4514 restore_start_dir(dirname_start);
4515
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004516 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004517 return NULL;
4518 if (failed)
4519 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004520 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004521 return NULL;
4522 }
4523 return newbuf;
4524}
4525
4526/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004527 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4528 * directory to "dirname_start" prior to returning, if autocmds or the
4529 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004530 */
4531 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004532wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004533{
4534 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004535 {
4536#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4537 cleanup_T cs;
4538
4539 /* Reset the error/interrupt/exception state here so that aborting()
4540 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4541 * work when got_int is set. */
4542 enter_cleanup(&cs);
4543#endif
4544
Bram Moolenaar81695252004-12-29 20:58:21 +00004545 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004546
4547#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4548 /* Restore the error/interrupt/exception state if not discarded by a
4549 * new aborting error, interrupt, or uncaught exception. */
4550 leave_cleanup(&cs);
4551#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004552 /* When autocommands/'autochdir' option changed directory: go back. */
4553 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004554 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004555}
4556
4557/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004558 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4559 * directory to "dirname_start" prior to returning, if autocmds or the
4560 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004561 */
4562 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004563unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004564{
4565 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004566 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004567 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004568
4569 /* When autocommands/'autochdir' option changed directory: go back. */
4570 restore_start_dir(dirname_start);
4571 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004572}
4573
Bram Moolenaar05159a02005-02-26 23:04:13 +00004574#if defined(FEAT_EVAL) || defined(PROTO)
4575/*
4576 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004577 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004578 */
4579 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004580get_errorlist(win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004581{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004582 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004583 dict_T *dict;
4584 char_u buf[2];
4585 qfline_T *qfp;
4586 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004587 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004588
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004589 if (wp != NULL)
4590 {
4591 qi = GET_LOC_LIST(wp);
4592 if (qi == NULL)
4593 return FAIL;
4594 }
4595
Bram Moolenaard823fa92016-08-12 16:29:27 +02004596 if (qf_idx == -1)
4597 qf_idx = qi->qf_curlist;
4598
4599 if (qf_idx >= qi->qf_listcount
4600 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004601 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004602
Bram Moolenaard823fa92016-08-12 16:29:27 +02004603 qfp = qi->qf_lists[qf_idx].qf_start;
4604 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004605 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004606 /* Handle entries with a non-existing buffer number. */
4607 bufnum = qfp->qf_fnum;
4608 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4609 bufnum = 0;
4610
Bram Moolenaar05159a02005-02-26 23:04:13 +00004611 if ((dict = dict_alloc()) == NULL)
4612 return FAIL;
4613 if (list_append_dict(list, dict) == FAIL)
4614 return FAIL;
4615
4616 buf[0] = qfp->qf_type;
4617 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004618 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004619 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4620 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4621 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4622 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004623 || dict_add_nr_str(dict, "pattern", 0L,
4624 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4625 || dict_add_nr_str(dict, "text", 0L,
4626 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004627 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4628 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4629 return FAIL;
4630
4631 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004632 if (qfp == NULL)
4633 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004634 }
4635 return OK;
4636}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004637
4638/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004639 * Flags used by getqflist()/getloclist() to determine which fields to return.
4640 */
4641enum {
4642 QF_GETLIST_NONE = 0x0,
4643 QF_GETLIST_TITLE = 0x1,
4644 QF_GETLIST_ITEMS = 0x2,
4645 QF_GETLIST_NR = 0x4,
4646 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004647 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004648 QF_GETLIST_ALL = 0xFF
4649};
4650
4651/*
4652 * Return quickfix/location list details (title) as a
4653 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4654 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004655 */
4656 int
Bram Moolenaard823fa92016-08-12 16:29:27 +02004657get_errorlist_properties(win_T *wp, dict_T *what, dict_T *retdict)
4658{
4659 qf_info_T *qi = &ql_info;
4660 int status = OK;
4661 int qf_idx;
4662 dictitem_T *di;
4663 int flags = QF_GETLIST_NONE;
4664
4665 if (wp != NULL)
4666 {
4667 qi = GET_LOC_LIST(wp);
4668 if (qi == NULL)
4669 return FAIL;
4670 }
4671
4672 qf_idx = qi->qf_curlist; /* default is the current list */
4673 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4674 {
4675 /* Use the specified quickfix/location list */
4676 if (di->di_tv.v_type == VAR_NUMBER)
4677 {
Bram Moolenaar890680c2016-09-27 21:28:56 +02004678 /* for zero use the current list */
4679 if (di->di_tv.vval.v_number != 0)
4680 {
4681 qf_idx = di->di_tv.vval.v_number - 1;
4682 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4683 return FAIL;
4684 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004685 flags |= QF_GETLIST_NR;
4686 }
4687 else
4688 return FAIL;
4689 }
4690
4691 if (dict_find(what, (char_u *)"all", -1) != NULL)
4692 flags |= QF_GETLIST_ALL;
4693
4694 if (dict_find(what, (char_u *)"title", -1) != NULL)
4695 flags |= QF_GETLIST_TITLE;
4696
4697 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4698 flags |= QF_GETLIST_WINID;
4699
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004700 if (dict_find(what, (char_u *)"context", -1) != NULL)
4701 flags |= QF_GETLIST_CONTEXT;
4702
Bram Moolenaard823fa92016-08-12 16:29:27 +02004703 if (flags & QF_GETLIST_TITLE)
4704 {
4705 char_u *t;
4706 t = qi->qf_lists[qf_idx].qf_title;
4707 if (t == NULL)
4708 t = (char_u *)"";
4709 status = dict_add_nr_str(retdict, "title", 0L, t);
4710 }
4711 if ((status == OK) && (flags & QF_GETLIST_NR))
4712 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4713 if ((status == OK) && (flags & QF_GETLIST_WINID))
4714 {
4715 win_T *win;
4716 win = qf_find_win(qi);
4717 if (win != NULL)
4718 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL);
4719 }
4720
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004721 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4722 {
4723 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
4724 {
4725 di = dictitem_alloc((char_u *)"context");
4726 if (di != NULL)
4727 {
4728 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02004729 if (dict_add(retdict, di) == FAIL)
4730 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004731 }
4732 }
4733 else
4734 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4735 }
4736
Bram Moolenaard823fa92016-08-12 16:29:27 +02004737 return status;
4738}
4739
4740/*
4741 * Add list of entries to quickfix/location list. Each list entry is
4742 * a dictionary with item information.
4743 */
4744 static int
4745qf_add_entries(
4746 qf_info_T *qi,
4747 list_T *list,
4748 char_u *title,
4749 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004750{
4751 listitem_T *li;
4752 dict_T *d;
4753 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004754 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004755 long lnum;
4756 int col, nr;
4757 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004758#ifdef FEAT_WINDOWS
4759 qfline_T *old_last = NULL;
4760#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004761 int valid, status;
4762 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004763 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004764
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004765 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004766 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004767 qf_new_list(qi, title);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004768#ifdef FEAT_WINDOWS
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004769 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
4770 /* Adding to existing list, use last entry. */
4771 old_last = qi->qf_lists[qi->qf_curlist].qf_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004772#endif
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004773 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02004774 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004775 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02004776 qf_store_title(qi, title);
4777 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004778
4779 for (li = list->lv_first; li != NULL; li = li->li_next)
4780 {
4781 if (li->li_tv.v_type != VAR_DICT)
4782 continue; /* Skip non-dict items */
4783
4784 d = li->li_tv.vval.v_dict;
4785 if (d == NULL)
4786 continue;
4787
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004788 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004789 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
4790 lnum = (int)get_dict_number(d, (char_u *)"lnum");
4791 col = (int)get_dict_number(d, (char_u *)"col");
4792 vcol = (int)get_dict_number(d, (char_u *)"vcol");
4793 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004794 type = get_dict_string(d, (char_u *)"type", TRUE);
4795 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
4796 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004797 if (text == NULL)
4798 text = vim_strsave((char_u *)"");
4799
4800 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004801 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004802 valid = FALSE;
4803
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004804 /* Mark entries with non-existing buffer number as not valid. Give the
4805 * error message only once. */
4806 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4807 {
4808 if (!did_bufnr_emsg)
4809 {
4810 did_bufnr_emsg = TRUE;
4811 EMSGN(_("E92: Buffer %ld not found"), bufnum);
4812 }
4813 valid = FALSE;
4814 bufnum = 0;
4815 }
4816
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02004817 /* If the 'valid' field is present it overrules the detected value. */
4818 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
4819 valid = (int)get_dict_number(d, (char_u *)"valid");
4820
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004821 status = qf_add_entry(qi,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004822 NULL, /* dir */
4823 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004824 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004825 text,
4826 lnum,
4827 col,
4828 vcol, /* vis_col */
4829 pattern, /* search pattern */
4830 nr,
4831 type == NULL ? NUL : *type,
4832 valid);
4833
4834 vim_free(filename);
4835 vim_free(pattern);
4836 vim_free(text);
4837 vim_free(type);
4838
4839 if (status == FAIL)
4840 {
4841 retval = FAIL;
4842 break;
4843 }
4844 }
4845
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004846 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02004847 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02004848 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
4849 else
4850 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004851 if (action != 'a')
4852 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004853 qi->qf_lists[qi->qf_curlist].qf_ptr =
4854 qi->qf_lists[qi->qf_curlist].qf_start;
4855 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4856 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4857 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004858
4859#ifdef FEAT_WINDOWS
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004860 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004861 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004862#endif
4863
4864 return retval;
4865}
Bram Moolenaard823fa92016-08-12 16:29:27 +02004866
4867 static int
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004868qf_set_properties(qf_info_T *qi, dict_T *what, int action)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004869{
4870 dictitem_T *di;
4871 int retval = FAIL;
4872 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004873 int newlist = FALSE;
4874
4875 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
4876 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004877
4878 qf_idx = qi->qf_curlist; /* default is the current list */
4879 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4880 {
4881 /* Use the specified quickfix/location list */
4882 if (di->di_tv.v_type == VAR_NUMBER)
4883 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004884 /* for zero use the current list */
4885 if (di->di_tv.vval.v_number != 0)
4886 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004887 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4888 return FAIL;
4889 }
4890 else
4891 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02004892 newlist = FALSE; /* use the specified list */
4893 }
4894
4895 if (newlist)
4896 {
4897 qf_new_list(qi, NULL);
4898 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004899 }
4900
4901 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
4902 {
4903 if (di->di_tv.v_type == VAR_STRING)
4904 {
4905 vim_free(qi->qf_lists[qf_idx].qf_title);
4906 qi->qf_lists[qf_idx].qf_title =
4907 get_dict_string(what, (char_u *)"title", TRUE);
4908 if (qf_idx == qi->qf_curlist)
4909 qf_update_win_titlevar(qi);
4910 retval = OK;
4911 }
4912 }
4913
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004914 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
4915 {
4916 typval_T *ctx;
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004917 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004918 ctx = alloc_tv();
4919 if (ctx != NULL)
4920 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02004921 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004922 }
4923
Bram Moolenaard823fa92016-08-12 16:29:27 +02004924 return retval;
4925}
4926
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004927/*
4928 * Find the non-location list window with the specified location list.
4929 */
4930 static win_T *
4931find_win_with_ll(qf_info_T *qi)
4932{
4933 win_T *wp = NULL;
4934
4935 FOR_ALL_WINDOWS(wp)
4936 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
4937 return wp;
4938
4939 return NULL;
4940}
4941
4942/*
4943 * Free the entire quickfix/location list stack.
4944 * If the quickfix/location list window is open, then clear it.
4945 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004946 static void
4947qf_free_stack(win_T *wp, qf_info_T *qi)
4948{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004949 win_T *qfwin = qf_find_win(qi);
4950 win_T *llwin = NULL;
4951 win_T *orig_wp = wp;
4952
4953 if (qfwin != NULL)
4954 {
4955 /* If the quickfix/location list window is open, then clear it */
4956 if (qi->qf_curlist < qi->qf_listcount)
4957 qf_free(qi, qi->qf_curlist);
4958 qf_update_buffer(qi, NULL);
4959 }
4960
4961 if (wp != NULL && IS_LL_WINDOW(wp))
4962 {
4963 /* If in the location list window, then use the non-location list
4964 * window with this location list (if present)
4965 */
4966 llwin = find_win_with_ll(qi);
4967 if (llwin != NULL)
4968 wp = llwin;
4969 }
4970
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004971 qf_free_all(wp);
4972 if (wp == NULL)
4973 {
4974 /* quickfix list */
4975 qi->qf_curlist = 0;
4976 qi->qf_listcount = 0;
4977 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004978 else if (IS_LL_WINDOW(orig_wp))
4979 {
4980 /* If the location list window is open, then create a new empty
4981 * location list */
4982 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02004983
Bram Moolenaard788f6f2017-04-23 17:19:43 +02004984 /* first free the list reference in the location list window */
4985 ll_free_all(&orig_wp->w_llist_ref);
4986
Bram Moolenaar69f40be2017-04-02 15:15:49 +02004987 orig_wp->w_llist_ref = new_ll;
4988 if (llwin != NULL)
4989 {
4990 llwin->w_llist = new_ll;
4991 new_ll->qf_refcount++;
4992 }
4993 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02004994}
4995
Bram Moolenaard823fa92016-08-12 16:29:27 +02004996/*
4997 * Populate the quickfix list with the items supplied in the list
4998 * of dictionaries. "title" will be copied to w:quickfix_title.
4999 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5000 */
5001 int
5002set_errorlist(
5003 win_T *wp,
5004 list_T *list,
5005 int action,
5006 char_u *title,
5007 dict_T *what)
5008{
5009 qf_info_T *qi = &ql_info;
5010 int retval = OK;
5011
5012 if (wp != NULL)
5013 {
5014 qi = ll_get_or_alloc_list(wp);
5015 if (qi == NULL)
5016 return FAIL;
5017 }
5018
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005019 if (action == 'f')
5020 {
5021 /* Free the entire quickfix or location list stack */
5022 qf_free_stack(wp, qi);
5023 }
5024 else if (what != NULL)
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005025 retval = qf_set_properties(qi, what, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005026 else
5027 retval = qf_add_entries(qi, list, title, action);
5028
5029 return retval;
5030}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005031
5032 static int
5033mark_quickfix_ctx(qf_info_T *qi, int copyID)
5034{
5035 int i;
5036 int abort = FALSE;
5037 typval_T *ctx;
5038
5039 for (i = 0; i < LISTCOUNT && !abort; ++i)
5040 {
5041 ctx = qi->qf_lists[i].qf_ctx;
5042 if (ctx != NULL && ctx->v_type != VAR_NUMBER &&
5043 ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
5044 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5045 }
5046
5047 return abort;
5048}
5049
5050/*
5051 * Mark the context of the quickfix list and the location lists (if present) as
5052 * "in use". So that garabage collection doesn't free the context.
5053 */
5054 int
5055set_ref_in_quickfix(int copyID)
5056{
5057 int abort = FALSE;
5058 tabpage_T *tp;
5059 win_T *win;
5060
5061 abort = mark_quickfix_ctx(&ql_info, copyID);
5062 if (abort)
5063 return abort;
5064
5065 FOR_ALL_TAB_WINDOWS(tp, win)
5066 {
5067 if (win->w_llist != NULL)
5068 {
5069 abort = mark_quickfix_ctx(win->w_llist, copyID);
5070 if (abort)
5071 return abort;
5072 }
5073 }
5074
5075 return abort;
5076}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005077#endif
5078
Bram Moolenaar81695252004-12-29 20:58:21 +00005079/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005080 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005081 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005082 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005083 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005084 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005085 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005086 */
5087 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005088ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005089{
5090 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005091 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005092#ifdef FEAT_AUTOCMD
5093 char_u *au_name = NULL;
5094#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005095
Bram Moolenaardb552d602006-03-23 22:59:57 +00005096 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5097 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005098 {
5099 qi = ll_get_or_alloc_list(curwin);
5100 if (qi == NULL)
5101 return;
5102 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005103
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005104#ifdef FEAT_AUTOCMD
5105 switch (eap->cmdidx)
5106 {
5107 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5108 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5109 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5110 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5111 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5112 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5113 default: break;
5114 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005115 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5116 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005117 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005118# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005119 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005120 return;
5121# endif
5122 }
5123#endif
5124
Bram Moolenaar86b68352004-12-27 21:59:20 +00005125 if (*eap->arg == NUL)
5126 buf = curbuf;
5127 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5128 buf = buflist_findnr(atoi((char *)eap->arg));
5129 if (buf == NULL)
5130 EMSG(_(e_invarg));
5131 else if (buf->b_ml.ml_mfp == NULL)
5132 EMSG(_("E681: Buffer is not loaded"));
5133 else
5134 {
5135 if (eap->addr_count == 0)
5136 {
5137 eap->line1 = 1;
5138 eap->line2 = buf->b_ml.ml_line_count;
5139 }
5140 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5141 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5142 EMSG(_(e_invrange));
5143 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005144 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005145 char_u *qf_title = *eap->cmdlinep;
5146
5147 if (buf->b_sfname)
5148 {
5149 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5150 (char *)qf_title, (char *)buf->b_sfname);
5151 qf_title = IObuff;
5152 }
5153
Bram Moolenaardb552d602006-03-23 22:59:57 +00005154 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
5155 (eap->cmdidx != CMD_caddbuffer
5156 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005157 eap->line1, eap->line2,
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005158 qf_title, NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005159 {
5160#ifdef FEAT_AUTOCMD
5161 if (au_name != NULL)
5162 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5163 curbuf->b_fname, TRUE, curbuf);
5164#endif
5165 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer)
5166 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5167 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00005168 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005169 }
5170}
5171
Bram Moolenaar1e015462005-09-25 22:16:38 +00005172#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005173/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005174 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5175 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005176 */
5177 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005178ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005179{
5180 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005181 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005182#ifdef FEAT_AUTOCMD
5183 char_u *au_name = NULL;
5184#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005185
Bram Moolenaardb552d602006-03-23 22:59:57 +00005186 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5187 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005188 {
5189 qi = ll_get_or_alloc_list(curwin);
5190 if (qi == NULL)
5191 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005192 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005193
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005194#ifdef FEAT_AUTOCMD
5195 switch (eap->cmdidx)
5196 {
5197 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5198 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5199 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5200 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5201 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5202 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5203 default: break;
5204 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005205 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5206 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005207 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005208# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005209 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005210 return;
5211# endif
5212 }
5213#endif
5214
Bram Moolenaar4770d092006-01-12 23:22:24 +00005215 /* Evaluate the expression. When the result is a string or a list we can
5216 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005217 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005218 if (tv != NULL)
5219 {
5220 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5221 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5222 {
Bram Moolenaard6357e82016-01-21 21:48:09 +01005223 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005224 (eap->cmdidx != CMD_caddexpr
5225 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005226 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
5227 NULL) > 0)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005228 {
5229#ifdef FEAT_AUTOCMD
5230 if (au_name != NULL)
5231 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5232 curbuf->b_fname, TRUE, curbuf);
5233#endif
5234 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
5235 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
5236 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005237 }
5238 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005239 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005240 free_tv(tv);
5241 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005242}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005243#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005244
5245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 * ":helpgrep {pattern}"
5247 */
5248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005249ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
5251 regmatch_T regmatch;
5252 char_u *save_cpo;
5253 char_u *p;
5254 int fcount;
5255 char_u **fnames;
5256 FILE *fd;
5257 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005259#ifdef FEAT_MULTI_LANG
5260 char_u *lang;
5261#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005262 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005263 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005264 int new_qi = FALSE;
5265 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005266#ifdef FEAT_AUTOCMD
5267 char_u *au_name = NULL;
5268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005270#ifdef FEAT_MULTI_LANG
5271 /* Check for a specified language */
5272 lang = check_help_lang(eap->arg);
5273#endif
5274
Bram Moolenaar73633f82012-01-20 13:39:07 +01005275#ifdef FEAT_AUTOCMD
5276 switch (eap->cmdidx)
5277 {
5278 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5279 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5280 default: break;
5281 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005282 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5283 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005284 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005285# ifdef FEAT_EVAL
5286 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005287 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005288# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005289 }
5290#endif
5291
5292 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5293 save_cpo = p_cpo;
5294 p_cpo = empty_option;
5295
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005296 if (eap->cmdidx == CMD_lhelpgrep)
5297 {
5298 /* Find an existing help window */
5299 FOR_ALL_WINDOWS(wp)
5300 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
5301 break;
5302
5303 if (wp == NULL) /* Help window not found */
5304 qi = NULL;
5305 else
5306 qi = wp->w_llist;
5307
5308 if (qi == NULL)
5309 {
5310 /* Allocate a new location list for help text matches */
5311 if ((qi = ll_new_list()) == NULL)
5312 return;
5313 new_qi = TRUE;
5314 }
5315 }
5316
Bram Moolenaaree85df32017-03-19 14:19:50 +01005317 /* Autocommands may change the list. Save it for later comparison */
5318 save_qi = qi;
5319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5321 regmatch.rm_ic = FALSE;
5322 if (regmatch.regprog != NULL)
5323 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005324#ifdef FEAT_MBYTE
5325 vimconv_T vc;
5326
5327 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5328 * differs. */
5329 vc.vc_type = CONV_NONE;
5330 if (!enc_utf8)
5331 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5332#endif
5333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005335 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 /* Go through all directories in 'runtimepath' */
5338 p = p_rtp;
5339 while (*p != NUL && !got_int)
5340 {
5341 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5342
5343 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5344 add_pathsep(NameBuff);
5345 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5346 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5347 &fnames, EW_FILE|EW_SILENT) == OK
5348 && fcount > 0)
5349 {
5350 for (fi = 0; fi < fcount && !got_int; ++fi)
5351 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005352#ifdef FEAT_MULTI_LANG
5353 /* Skip files for a different language. */
5354 if (lang != NULL
5355 && STRNICMP(lang, fnames[fi]
5356 + STRLEN(fnames[fi]) - 3, 2) != 0
5357 && !(STRNICMP(lang, "en", 2) == 0
5358 && STRNICMP("txt", fnames[fi]
5359 + STRLEN(fnames[fi]) - 3, 3) == 0))
5360 continue;
5361#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005362 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 if (fd != NULL)
5364 {
5365 lnum = 1;
5366 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5367 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005368 char_u *line = IObuff;
5369#ifdef FEAT_MBYTE
5370 /* Convert a line if 'encoding' is not utf-8 and
5371 * the line contains a non-ASCII character. */
5372 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005373 && has_non_ascii(IObuff))
5374 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005375 line = string_convert(&vc, IObuff, NULL);
5376 if (line == NULL)
5377 line = IObuff;
5378 }
5379#endif
5380
5381 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005383 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
5385 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005386 while (l > 0 && line[l - 1] <= ' ')
5387 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005389 if (qf_add_entry(qi,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 NULL, /* dir */
5391 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005392 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005393 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005395 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005396 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005397 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005398 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 0, /* nr */
5400 1, /* type */
5401 TRUE /* valid */
5402 ) == FAIL)
5403 {
5404 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005405#ifdef FEAT_MBYTE
5406 if (line != IObuff)
5407 vim_free(line);
5408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 break;
5410 }
5411 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005412#ifdef FEAT_MBYTE
5413 if (line != IObuff)
5414 vim_free(line);
5415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 ++lnum;
5417 line_breakcheck();
5418 }
5419 fclose(fd);
5420 }
5421 }
5422 FreeWild(fcount, fnames);
5423 }
5424 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005425
Bram Moolenaar473de612013-06-08 18:19:48 +02005426 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005427#ifdef FEAT_MBYTE
5428 if (vc.vc_type != CONV_NONE)
5429 convert_setup(&vc, NULL, NULL);
5430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005432 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5433 qi->qf_lists[qi->qf_curlist].qf_ptr =
5434 qi->qf_lists[qi->qf_curlist].qf_start;
5435 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
5437
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005438 if (p_cpo == empty_option)
5439 p_cpo = save_cpo;
5440 else
5441 /* Darn, some plugin changed the value. */
5442 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
5444#ifdef FEAT_WINDOWS
Bram Moolenaar864293a2016-06-02 13:40:04 +02005445 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446#endif
5447
Bram Moolenaar73633f82012-01-20 13:39:07 +01005448#ifdef FEAT_AUTOCMD
5449 if (au_name != NULL)
5450 {
5451 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5452 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005453 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005454 /* autocommands made "qi" invalid */
5455 return;
5456 }
5457#endif
5458
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005460 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005461 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005462 else
5463 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005464
5465 if (eap->cmdidx == CMD_lhelpgrep)
5466 {
5467 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005468 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005469 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
5470 {
5471 if (new_qi)
5472 ll_free_all(&qi);
5473 }
5474 else if (curwin->w_llist == NULL)
5475 curwin->w_llist = qi;
5476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477}
5478
5479#endif /* FEAT_QUICKFIX */